about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-05-23 00:10:48 +0200
committerBaitinq <[email protected]>2025-05-23 00:10:48 +0200
commit1833c90d53422bee88a2061a0f226c8c96877c76 (patch)
tree40636c7dc99d4643c8be10ae113a28b42d4c5668
parentCodegen: Typecheck function return types and change null type (diff)
downloadinterpreter-1833c90d53422bee88a2061a0f226c8c96877c76.tar.gz
interpreter-1833c90d53422bee88a2061a0f226c8c96877c76.tar.bz2
interpreter-1833c90d53422bee88a2061a0f226c8c96877c76.zip
Parser: Fix cast statement parsing
-rw-r--r--grammar.ebnf8
-rw-r--r--src/parser.zig15
2 files changed, 12 insertions, 11 deletions
diff --git a/grammar.ebnf b/grammar.ebnf
index 3806ee6..ddb0cef 100644
--- a/grammar.ebnf
+++ b/grammar.ebnf
@@ -1,6 +1,6 @@
 Program      ::= Statement+
 
-Statement    ::= (AssignmentStatement | ImportDeclaration | ExternDeclaration | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement | "break" | "continue") SEMICOLON
+Statement    ::= (AssignmentStatement | ImportDeclaration | ExternDeclaration | CastStatement| FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement | "break" | "continue") SEMICOLON
 
 AssignmentStatement ::= ("let")? ("*")? Expression EQUALS Expression
 
@@ -18,9 +18,9 @@ ReturnStatement ::= RETURN (Expression)?
 
 FunctionArguments ::= Expression ("," Expression)*
 
-Expression ::= EqualityExpression | AdditiveExpression | CastExpression
+Expression ::= EqualityExpression | AdditiveExpression
 
-CastExpression ::= "cast" LPAREN TYPE "," Expression RPAREN
+CastStatement ::= "cast" LPAREN TYPE "," Expression RPAREN
 
 EqualityExpression ::= AdditiveExpression ("==" | "!=" | "<=" | ">=" | "<" | ">") AdditiveExpression
 
@@ -30,7 +30,7 @@ MultiplicativeExpression ::= UnaryExpression (("*" | "/" | "%") UnaryExpression)
 
 UnaryExpression ::= ("!" | "-" | "*") UnaryExpression | PrimaryExpression
 
-PrimaryExpression ::= NULL | NUMBER | BOOLEAN | CHAR | STRING | IDENTIFIER | FunctionCallStatement | FunctionDefinition | LPAREN Expression RPAREN
+PrimaryExpression ::= NULL | NUMBER | BOOLEAN | CHAR | STRING | IDENTIFIER | CastStatement | FunctionCallStatement | FunctionDefinition | LPAREN Expression RPAREN
 
 FunctionDefinition ::= LPAREN FunctionParameters? RPAREN ARROW IDENTIFIER LBRACE Statement* ReturnStatement SEMICOLON RBRACE
 
diff --git a/src/parser.zig b/src/parser.zig
index 1e130f8..25ab6aa 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -154,11 +154,12 @@ pub const Parser = struct {
         } });
     }
 
-    // Statement    ::= (AssignmentStatement | ImportDeclaration | ExternDeclaration | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement | "break" | "continue") SEMICOLON
+    // Statement    ::= (AssignmentStatement | ImportDeclaration | ExternDeclaration | CastStatement | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement | "break" | "continue") SEMICOLON
     fn parse_statement(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing statement {any}\n", .{self.peek_token()});
 
         const statement =
+            self.accept_parse(parse_cast_statement) orelse
             self.accept_parse(parse_function_call_statement) orelse
             self.accept_parse(parse_if_statement) orelse
             self.accept_parse(parse_while_statement) orelse
@@ -406,12 +407,11 @@ pub const Parser = struct {
         } });
     }
 
-    // Expression ::= EqualityExpression | AdditiveExpression | CastExpression
+    // Expression ::= EqualityExpression | AdditiveExpression
     fn parse_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing expression {any}\n", .{self.peek_token()});
 
-        return self.accept_parse(parse_cast_expression) orelse
-            self.accept_parse(parse_equality_expression) orelse
+        return self.accept_parse(parse_equality_expression) orelse
             self.accept_parse(parse_additive_expression) orelse
             return ParserError.ParsingError;
     }
@@ -553,10 +553,11 @@ pub const Parser = struct {
         } });
     }
 
-    // PrimaryExpression ::= NULL | NUMBER | BOOLEAN | CHAR | STRING | IDENTIFIER | FunctionCallStatement | FunctionDefinition | LPAREN Expression RPAREN
+    // PrimaryExpression ::= NULL | NUMBER | BOOLEAN | CHAR | STRING | IDENTIFIER | CastStatement | FunctionCallStatement | FunctionDefinition | LPAREN Expression RPAREN
     fn parse_primary_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing primary expression {any}\n", .{self.peek_token()});
 
+        if (self.accept_parse(parse_cast_statement)) |stmt| return stmt;
         if (self.accept_parse(parse_function_call_statement)) |stmt| return stmt;
         if (self.accept_parse(parse_function_definition)) |stmt| return stmt;
 
@@ -684,8 +685,8 @@ pub const Parser = struct {
         });
     }
 
-    // CastExpression ::= "cast" LPAREN TYPE "," Expression RPAREN
-    fn parse_cast_expression(self: *Parser) ParserError!*Node {
+    // CastStatement ::= "cast" LPAREN TYPE "," Expression RPAREN
+    fn parse_cast_statement(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing cast statement {any}\n", .{self.peek_token()});
 
         const ident = try self.parse_token(tokenizer.TokenType.IDENTIFIER);