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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user