about summary refs log tree commit diff
path: root/src/parser.zig
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-03-23 01:21:49 +0100
committerBaitinq <[email protected]>2025-03-23 01:21:49 +0100
commitfe47528e65e4690a5600154978515cc29133e5da (patch)
treefa25e6a8601d2c0ebf950e80a98c8dccc67857fb /src/parser.zig
parentMisc: Fix building on linux (diff)
downloadinterpreter-fe47528e65e4690a5600154978515cc29133e5da.tar.gz
interpreter-fe47528e65e4690a5600154978515cc29133e5da.tar.bz2
interpreter-fe47528e65e4690a5600154978515cc29133e5da.zip
Parser: Fix ambiguity with symbol declaration
Diffstat (limited to 'src/parser.zig')
-rw-r--r--src/parser.zig38
1 files changed, 25 insertions, 13 deletions
diff --git a/src/parser.zig b/src/parser.zig
index d2067a1..64e670e 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -123,7 +123,7 @@ pub const Parser = struct {
         } });
     }
 
-    // Statement    ::= (AssignmentStatement | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement) SEMICOLON
+    // Statement    ::= (AssignmentStatement | ExternDeclaration | FunctionCallStatement | IfStatement | WhileStatement | ReturnStatement) SEMICOLON
     fn parse_statement(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing statement {any}\n", .{self.peek_token()});
 
@@ -131,7 +131,8 @@ pub const Parser = struct {
             self.accept_parse(parse_if_statement) orelse
             self.accept_parse(parse_while_statement) orelse
             self.accept_parse(parse_return_statement) orelse
-            try self.parse_assignment_statement();
+            self.accept_parse(parse_assignment_statement) orelse
+            try self.parse_extern_declaration();
 
         _ = try self.parse_token(tokenizer.TokenType.SEMICOLON);
 
@@ -142,7 +143,7 @@ pub const Parser = struct {
         });
     }
 
-    // AssignmentStatement ::= "let" IDENTIFIER EQUALS (Type | Expression)
+    // AssignmentStatement ::= "let"? IDENTIFIER EQUALS Expression
     fn parse_assignment_statement(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing assignment statement {any}\n", .{self.peek_token()});
 
@@ -155,16 +156,6 @@ pub const Parser = struct {
 
         _ = try self.parse_token(tokenizer.TokenType.EQUALS);
 
-        if (self.accept_parse(parse_type)) |typ| {
-            return self.create_node(.{
-                .ASSIGNMENT_STATEMENT = .{
-                    .is_declaration = is_declaration,
-                    .name = try self.arena.dupe(u8, identifier.type.IDENTIFIER),
-                    .expression = @constCast(typ),
-                },
-            });
-        }
-
         const expression = try self.parse_expression();
 
         return self.create_node(.{
@@ -176,6 +167,27 @@ pub const Parser = struct {
         });
     }
 
+    // ExternDeclaration ::= "extern" IDENTIFIER EQUALS Type
+    fn parse_extern_declaration(self: *Parser) ParserError!*Node {
+        errdefer if (!self.try_context) std.debug.print("Error parsing extern declaration {any}\n", .{self.peek_token()});
+
+        _ = try self.parse_token(.EXTERN);
+
+        const identifier = try self.parse_token(tokenizer.TokenType.IDENTIFIER);
+
+        _ = try self.parse_token(tokenizer.TokenType.EQUALS);
+
+        const typ = try self.parse_type();
+
+        return self.create_node(.{
+            .ASSIGNMENT_STATEMENT = .{
+                .is_declaration = true,
+                .name = try self.arena.dupe(u8, identifier.type.IDENTIFIER),
+                .expression = @constCast(typ),
+            },
+        });
+    }
+
     // FunctionCallStatement ::= (IDENTIFIER | FunctionDefinition) LPAREN FunctionArguments? RPAREN
     fn parse_function_call_statement(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing function call statement {any}\n", .{self.peek_token()});