Add if statements, unary minus, and fix GC safe points

- Lexer: recognize 'if' as keyword (TOK_IF)
- Parser: add NODE_IF with if_statement union, parse if/cond/body,
  handle unary minus in parse_term as 0 - expr
- Eval: add NODE_IF evaluation, move GC to NODE_BLOCK level to avoid
  destroying temporary values during sub-expression evaluation
This commit is contained in:
Jose Luis Montañes Ojados
2026-02-16 05:12:28 +01:00
parent 84b3abbfda
commit 65220f88c6
5 changed files with 64 additions and 11 deletions

View File

@@ -41,16 +41,8 @@ void env_set(Environment *env, const char *name, size_t value) {
int step = 0;
size_t eval(ASTNode *node, Environment *env, void *allocator, int debug, int gc) {
// Run GC
if (gc) {
size_t roots[256];
for (int i = 0; i < env->count; i++) {
roots[i] = env->vars[i].value;
}
gc_collect(allocator, roots, env->count);
}
size_t eval(ASTNode *node, Environment *env, void *allocator, int debug,
int gc) {
if (debug > 0) {
step++;
@@ -101,6 +93,15 @@ size_t eval(ASTNode *node, Environment *env, void *allocator, int debug, int gc)
return val;
}
case NODE_BLOCK:
// Run GC
if (gc) {
size_t roots[256];
for (int i = 0; i < env->count; i++) {
roots[i] = env->vars[i].value;
}
gc_collect(allocator, roots, env->count);
}
for (int i = 0; i < node->data.block.count; i++)
eval(node->data.block.stmts[i], env, allocator, debug, gc);
return 0;