commit c6151b58d1f2fccf006d9ac33cea7f4c835b8dbe
parent c44c88c9325935c90b57f0f6e4ebd3e4778ec894
Author: Sebastian <sebastian@sebsite.pw>
Date: Mon, 7 Mar 2022 21:03:28 -0500
parse: allow switch/match/compound within cast expr
Also adds a test for casting a compound expression.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
2 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha
@@ -89,7 +89,8 @@
"export fn main() void = void as int;\n"
"export fn main() void = void is int;\n"
"export fn main() void = void: int: uint: u16: u8;\n"
- "export fn main() void = void: int as uint: u16 is u8;\n");
+ "export fn main() void = void: int as uint: u16 is u8;\n"
+ "export fn main() void = {\n\tyield void;\n}: int;\n");
};
@test fn constant() void = {
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -38,20 +38,13 @@ export fn expr(lexer: *lex::lexer) (ast::expr | error) = {
operand = alloc(ex),
},
}, 0);
- } else match (peek(lexer, ltok::LBRACE, ltok::MATCH,
- ltok::SWITCH, ltok::IF, ltok::LABEL, ltok::FOR,
+ } else match (peek(lexer, ltok::IF, ltok::FOR,
ltok::BREAK, ltok::CONTINUE, ltok::RETURN, ltok::LET,
ltok::CONST, ltok::YIELD)?) {
case void =>
yield binarithm(lexer, void, 0)?;
case let tok: lex::token =>
yield switch (tok.0) {
- case ltok::LABEL, ltok::LBRACE =>
- yield compound_expr(lexer)?;
- case ltok::MATCH =>
- yield match_expr(lexer)?;
- case ltok::SWITCH =>
- yield switch_expr(lexer)?;
case ltok::IF =>
yield if_expr(lexer)?;
case ltok::FOR =>
@@ -1144,11 +1137,24 @@ fn match_expr(lexer: *lex::lexer) (ast::expr | error) = {
fn unarithm(lexer: *lex::lexer) (ast::expr | error) = {
const tok = match (try(lexer,
ltok::PLUS, ltok::MINUS, ltok::BNOT,
- ltok::LNOT, ltok::TIMES, ltok::BAND)) {
+ ltok::LNOT, ltok::TIMES, ltok::BAND,
+ ltok::SWITCH, ltok::MATCH, ltok::LABEL, ltok::LBRACE)) {
case void =>
return builtin(lexer);
case let tok: lex::token =>
- yield tok;
+ yield switch (tok.0) {
+ case ltok::SWITCH =>
+ lex::unlex(lexer, tok);
+ return switch_expr(lexer);
+ case ltok::MATCH =>
+ lex::unlex(lexer, tok);
+ return match_expr(lexer);
+ case ltok::LABEL, ltok::LBRACE =>
+ lex::unlex(lexer, tok);
+ return compound_expr(lexer);
+ case =>
+ yield tok;
+ };
};
const op = switch (tok.0) {