harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 9441c45d53ee5c8f13618915bef651127c123e2b
parent 10060824bc60fb02d532cffdf8fb0a989aff391a
Author: Eyal Sawady <ecs@d2evs.net>
Date:   Sun, 31 Jan 2021 16:08:06 -0500

Loosen compound expressions

Allow them to be complex or control expressions

Diffstat:
Msrc/gen.c | 8++++++--
Msrc/parse.c | 7++++++-
Mtests/09-funcs.ha | 3+++
Mtests/18-match.ha | 13++++++++-----
4 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/gen.c b/src/gen.c @@ -1597,8 +1597,12 @@ gen_match_tagged(struct gen_context *ctx, pushi(ctx->current, &val, Q_ADD, &mval, &temp, NULL); } - gen_expression(ctx, _case->value, out); - pushi(ctx->current, NULL, Q_JMP, &obranch, NULL); + if (_case->value->terminates) { + gen_expression(ctx, _case->value, NULL); + } else { + gen_expression(ctx, _case->value, out); + pushi(ctx->current, NULL, Q_JMP, &obranch, NULL); + } push(&ctx->current->body, &flabel); } diff --git a/src/parse.c b/src/parse.c @@ -2126,9 +2126,14 @@ parse_compound_expression(struct lexer *lexer) case T_LBRACE: unlex(lexer, &tok); return parse_expression_list(lexer); + case T_BREAK: + case T_CONTINUE: + case T_RETURN: + unlex(lexer, &tok); + return parse_control_statement(lexer); default: unlex(lexer, &tok); - return parse_simple_expression(lexer); + return parse_complex_expression(lexer); } } diff --git a/tests/09-funcs.ha b/tests/09-funcs.ha @@ -1,5 +1,7 @@ use rt; +fn simple() int = return 69; + fn addone(x: *int) void = { *x += 1; }; @@ -40,6 +42,7 @@ let x: int = 42; }; export fn main() void = { + assert(simple() == 69); pointers(); vaargs(); assert(x == 1337); diff --git a/tests/18-match.ha b/tests/18-match.ha @@ -13,12 +13,15 @@ fn tagged() void = { fn termination() void = { let x: (int | uint | str) = 1337; - let y: int = match (x) { - int => 42, - uint => abort(), - str => abort(), + for (true) { + let y: int = match (x) { + int => 42, + uint => abort(), + str => break, + }; + assert(y == 42); + x = "hi"; }; - assert(y == 42); }; fn default() void = {