aboutsummaryrefslogtreecommitdiff
path: root/ast.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-04-10 11:49:43 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-04-10 11:49:43 -0400
commit4f514378accd7a5ed8774d008761efa94f383148 (patch)
tree1682ac995b84327bee6ac29cd379aea4f02fa5ae /ast.c
parent438edf45c27da9aa2503a5bcf50f34475c8bc63d (diff)
Fix corecursive functions and global variables
Diffstat (limited to 'ast.c')
-rw-r--r--ast.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/ast.c b/ast.c
index 80681e10..25309e93 100644
--- a/ast.c
+++ b/ast.c
@@ -196,4 +196,27 @@ bool is_idempotent(ast_t *ast)
}
}
+bool is_constant(ast_t *ast)
+{
+ switch (ast->tag) {
+ case Bool: case Int: case Num: case Nil: case TextLiteral: return true;
+ case TextJoin: {
+ auto text = Match(ast, TextJoin);
+ return !text->children->next;
+ }
+ case Not: return is_constant(Match(ast, Not)->value);
+ case Negative: return is_constant(Match(ast, Negative)->value);
+ case BinaryOp: {
+ auto binop = Match(ast, BinaryOp);
+ switch (binop->op) {
+ case BINOP_UNKNOWN: case BINOP_POWER: case BINOP_CONCAT: case BINOP_MIN: case BINOP_MAX: case BINOP_CMP:
+ return false;
+ default:
+ return is_constant(binop->lhs) && is_constant(binop->rhs);
+ }
+ }
+ default: return false;
+ }
+}
+
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0