diff options
author | Flavian Kaufmann <flavian@flaviankaufmann.ch> | 2025-05-20 14:18:59 +0200 |
---|---|---|
committer | Flavian Kaufmann <flavian@flaviankaufmann.ch> | 2025-05-20 14:18:59 +0200 |
commit | 56cd967554e39dd9053b2b28c1f603ae63808527 (patch) | |
tree | 1d89a38c46a929e162ed8c6b555a9f1684053650 | |
parent | fbbe2f207a26b410d485c9dea07a22256a02d50b (diff) | |
download | imp-56cd967554e39dd9053b2b28c1f603ae63808527.tar.gz imp-56cd967554e39dd9053b2b28c1f603ae63808527.zip |
optionally omit parenthesis with sequential composition
-rw-r--r-- | example/example.imp | 5 | ||||
-rw-r--r-- | src/interpreter.c | 2 | ||||
-rw-r--r-- | src/parser.y | 4 |
3 files changed, 8 insertions, 3 deletions
diff --git a/example/example.imp b/example/example.imp index d87c891..e1ddf22 100644 --- a/example/example.imp +++ b/example/example.imp @@ -1 +1,4 @@ -(x := 1; while x < 10 do x := (x * 2) end) +x := 1; +while x < 10 do + x := (x * 2) +end
\ No newline at end of file diff --git a/src/interpreter.c b/src/interpreter.c index 4449847..6b1ebe1 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -101,7 +101,7 @@ void exec_stmt(hashmap_t context, ASTNode *node) { 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); + exec_stmt(context, node->u.d_let.stm); context_set(context, name, old_val); return; } diff --git a/src/parser.y b/src/parser.y index cb3544e..c1e1d0e 100644 --- a/src/parser.y +++ b/src/parser.y @@ -27,7 +27,7 @@ void yyerror(const char *s) { %token <num> TOKEN_NUMERAL %token TOKEN_ASSIGN %token TOKEN_LEFT_PARENTHESIS TOKEN_RIGHT_PARENTHESIS -%token TOKEN_SEMICOLON +%left TOKEN_SEMICOLON %token TOKEN_SKIP %token TOKEN_IF TOKEN_THEN TOKEN_ELSE TOKEN_END TOKEN_WHILE TOKEN_DO TOKEN_VAR TOKEN_IN %token TOKEN_PLUS TOKEN_MINUS TOKEN_MULTIPLY @@ -50,6 +50,8 @@ statement : TOKEN_SKIP { $$ = ast_assign($1, $3); } | TOKEN_LEFT_PARENTHESIS statement TOKEN_SEMICOLON statement TOKEN_RIGHT_PARENTHESIS { $$ = ast_seq($2, $4); } + | statement TOKEN_SEMICOLON statement + { $$ = ast_seq($1, $3); } | TOKEN_IF boolean_expression TOKEN_THEN statement TOKEN_ELSE statement TOKEN_END { $$ = ast_if($2, $4, $6); } | TOKEN_WHILE boolean_expression TOKEN_DO statement TOKEN_END |