aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.c
diff options
context:
space:
mode:
authorFlavian Kaufmann <flavian@flaviankaufmann.ch>2025-05-20 14:18:30 +0200
committerFlavian Kaufmann <flavian@flaviankaufmann.ch>2025-05-20 14:18:30 +0200
commitfbbe2f207a26b410d485c9dea07a22256a02d50b (patch)
tree164e2d30f43bd25ecc7c3f6ca4d49d1c30f4f1a7 /src/interpreter.c
parent9cf524df8c94920d7c7058692f2f83a95a4006e0 (diff)
downloadimp-fbbe2f207a26b410d485c9dea07a22256a02d50b.tar.gz
imp-fbbe2f207a26b410d485c9dea07a22256a02d50b.zip
local variables
Diffstat (limited to 'src/interpreter.c')
-rw-r--r--src/interpreter.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/interpreter.c b/src/interpreter.c
index 93b1bdb..4449847 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -76,9 +76,9 @@ void exec_stmt(hashmap_t context, ASTNode *node) {
case NT_SKIP:
return;
case NT_ASSIGN: {
- char *var = node->u.d_assign.var->u.d_var.name;
+ char *name = node->u.d_assign.var->u.d_var.name;
int val = eval_aexpr(context, node->u.d_assign.aexp);
- context_set(context, var, val);
+ context_set(context, name, val);
return;
}
case NT_SEQ:
@@ -96,6 +96,15 @@ void exec_stmt(hashmap_t context, ASTNode *node) {
exec_stmt(context, node->u.d_while.stm);
}
return;
+ case NT_LET: {
+ char *name = node->u.d_let.var->u.d_var.name;
+ int old_val = context_get(context, name);
+ int new_val = eval_aexpr(context, node->u.d_let.aexp);
+ context_set(context, name, new_val);
+ exec_stmt(context, node->u.d_let.stm);
+ context_set(context, name, old_val);
+ return;
+ }
default:
fprintf(stderr, "Bad stmt node %d\n", node->type);
exit(EXIT_FAILURE);