summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-15 23:17:01 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-15 23:27:55 +0100
commit061f072db682b0b9f750410a3867bfd4c2b36072 (patch)
tree1960c6e6646a9f8fce94cf3821909a9788a2992b
parentAdd basic support for parsing functions (diff)
downloadinterpreter-061f072db682b0b9f750410a3867bfd4c2b36072.tar.gz
interpreter-061f072db682b0b9f750410a3867bfd4c2b36072.tar.bz2
interpreter-061f072db682b0b9f750410a3867bfd4c2b36072.zip
Parser: clean
-rw-r--r--examples/4.src6
-rw-r--r--src/parser.zig33
2 files changed, 17 insertions, 22 deletions
diff --git a/examples/4.src b/examples/4.src
index 25dcc7a..ddbc5d4 100644
--- a/examples/4.src
+++ b/examples/4.src
@@ -1,6 +1,8 @@
 let print_one = () => {
 	print(1);
-	return 1;
+	return 0;
 };
 
-print_one();
+let y = print_one();
+
+print(y);
diff --git a/src/parser.zig b/src/parser.zig
index 55c3e28..3732b2b 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -88,7 +88,7 @@ pub const Parser = struct {
     }
 
     pub fn parse(self: *Parser) !*Node {
-        return self.parse_program();
+        return try self.parse_program();
     }
 
     // Program ::= Statement+
@@ -107,19 +107,16 @@ pub const Parser = struct {
     fn parse_statement(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing statement\n", .{});
 
-        var statement: ?*Node = undefined;
-        if (self.accept_parse(parse_print_statement)) |stmt| {
-            statement = stmt;
-        } else if (self.accept_parse(parse_function_call_statement)) |stmt| {
-            statement = stmt;
-        } else {
-            statement = try self.parse_assignment_statement();
-        }
+        const statement =
+            self.accept_parse(parse_print_statement) orelse
+            self.accept_parse(parse_function_call_statement) orelse
+            try self.parse_assignment_statement();
+
         _ = try self.accept_token(tokenizer.TokenType.SEMICOLON);
 
         return self.create_node(.{
             .STATEMENT = .{
-                .statement = statement.?,
+                .statement = statement,
             },
         });
     }
@@ -182,13 +179,9 @@ pub const Parser = struct {
     fn parse_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing expression\n", .{});
 
-        if (self.accept_parse(parse_additive_expression)) |expression| {
-            return expression;
-        } else if (self.accept_parse(parse_function_definition)) |expression| {
-            return expression;
-        }
-
-        return ParserError.ParsingError;
+        return self.accept_parse(parse_additive_expression) orelse
+            self.accept_parse(parse_function_definition) orelse
+            return ParserError.ParsingError;
     }
 
     // AdditiveExpression ::= PrimaryExpression ("+" AdditiveExpression)
@@ -212,10 +205,10 @@ pub const Parser = struct {
     fn parse_primary_expression(self: *Parser) ParserError!*Node {
         errdefer if (!self.try_context) std.debug.print("Error parsing primary expression\n", .{});
 
-        const token = self.consume_token() orelse return ParserError.ParsingError;
-
         if (self.accept_parse(parse_function_call_statement)) |stmt| return stmt;
 
+        const token = self.consume_token() orelse return ParserError.ParsingError;
+
         return switch (token) {
             .NUMBER => |number_token| try self.create_node(.{
                 .PRIMARY_EXPRESSION = .{
@@ -277,11 +270,11 @@ pub const Parser = struct {
     fn accept_parse(self: *Parser, parsing_func: *const fn (_: *Parser) ParserError!*Node) ?*Node {
         const prev_offset = self.offset;
         self.try_context = true;
-        defer self.try_context = false;
         const node = parsing_func(self) catch {
             self.offset = prev_offset;
             return null;
         };
+        self.try_context = false;
         return node;
     }