commit d457f7469580741d57e7ad843627b62c0b2a5a9a
parent a6e0ba6a4a067a88ff4e3c0a5ded65045c74b85b
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 5 Apr 2021 19:05:31 -0400
hare::parse: fix nested cast expressions
Diffstat:
2 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/hare/parse/+test.ha b/hare/parse/+test.ha
@@ -201,5 +201,7 @@ fn roundtrip(src: str) void = {
@test fn cast() void = {
roundtrip("export fn main() void = void: int;\n"
"export fn main() void = void as int;\n"
- "export fn main() void = void is 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");
};
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -10,7 +10,7 @@ fn binarithm(
// Precedence climbing parser
// https://en.wikipedia.org/wiki/Operator-precedence_parser
let lvalue = match (lvalue) {
- void => cast(lexer)?,
+ void => cast(lexer, void)?,
expr: ast::expr => expr,
};
@@ -24,7 +24,7 @@ fn binarithm(
for (j >= i; j = precedence(tok)) {
const op = binop_for_tok(tok);
- let rvalue = cast(lexer)?;
+ let rvalue = cast(lexer, void)?;
tok = match (lex::lex(lexer)?) {
io::EOF => return syntaxerr(mkloc(lexer),
"Unexpected EOF, expecting simple expression"),
@@ -54,8 +54,11 @@ fn binarithm(
return lvalue;
};
-fn cast(lexer: *lex::lexer) (ast::expr | error) = {
- let lvalue = unarithm(lexer)?;
+fn cast(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = {
+ let lvalue = match (lvalue) {
+ void => unarithm(lexer)?,
+ e: ast::expr => e,
+ };
const tok = match (try_btoken(lexer, btoken::COLON,
btoken::AS, btoken::IS)?) {
void => return lvalue,
@@ -67,11 +70,11 @@ fn cast(lexer: *lex::lexer) (ast::expr | error) = {
btoken::IS => ast::cast_kind::TEST,
* => abort(),
};
- return ast::cast_expr {
+ return cast(lexer, ast::cast_expr {
kind = kind,
value = alloc(lvalue),
_type = alloc(_type(lexer)?),
- };
+ });
};
fn unarithm(lexer: *lex::lexer) (ast::expr | error) = {