Add * and / operators, VM step debugger, and visualizer tweaks

Support multiply and divide in lexer, parser and eval.
Add step-by-step VM debug output with AST and heap visualization.
Remove spacing in visualizer hex output.
This commit is contained in:
Jose Luis Montañes Ojados
2026-02-16 02:24:37 +01:00
parent 01740d4892
commit 14b6a2ddd2
5 changed files with 34 additions and 8 deletions

View File

@@ -44,7 +44,16 @@ void env_set(Environment *env, const char *name, size_t value)
env->count++;
}
int step = 0;
size_t eval(ASTNode *node, Environment *env, void *allocator) {
step++;
printf("===== VM Step: %d =====\n", step);
printf("executing node:\n");
ast_debug(node);
printf("\n");
JLANG_visualize(allocator);
switch (node->type) {
case NODE_INT_LIT:
return obj_new_int(allocator, node->data.int_val);
@@ -68,7 +77,11 @@ size_t eval(ASTNode *node, Environment *env, void *allocator) {
return obj_new_int(allocator, l->data.int_val + r->data.int_val);
} else if (node->data.binop.op == '-') {
return obj_new_int(allocator, l->data.int_val - r->data.int_val);
}
} else if (node->data.binop.op == '*') {
return obj_new_int(allocator, l->data.int_val * r->data.int_val);
} else if (node->data.binop.op == '/') {
return obj_new_int(allocator, l->data.int_val / r->data.int_val);
}
}
case NODE_PRINT: {
size_t val = eval(node->data.print.expr, env, allocator);