summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-06 16:29:58 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-06 16:29:58 +0100
commita859fe1526ce9afc1bdd36e53990902c6fbea958 (patch)
tree2e59255d141273c8373e47c7535f1f01dbe4ee9e /src
parentParser: implement parse_identifier (diff)
downloadinterpreter-a859fe1526ce9afc1bdd36e53990902c6fbea958.tar.gz
interpreter-a859fe1526ce9afc1bdd36e53990902c6fbea958.tar.bz2
interpreter-a859fe1526ce9afc1bdd36e53990902c6fbea958.zip
Parser: implement parse_number
Diffstat (limited to 'src')
-rw-r--r--src/parser.zig25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/parser.zig b/src/parser.zig
index 088b98a..c5aa045 100644
--- a/src/parser.zig
+++ b/src/parser.zig
@@ -22,7 +22,7 @@ pub const Node = union(NodeType) {
         expression: *Node,
     },
     NUMBER: struct {
-        value: i32,
+        value: i64,
     },
     IDENTIFIER: struct {
         name: []const u8,
@@ -62,6 +62,18 @@ pub const Parser = struct {
         } };
     }
 
+    fn parse_number(self: *Parser) !Node {
+        const token = self.peek_token() orelse return error.InvalidArgument;
+
+        if (token != .NUMBER) return error.InvalidArgument;
+
+        _ = self.consume_token();
+
+        return Node{ .NUMBER = .{
+            .value = token.NUMBER,
+        } };
+    }
+
     fn consume_token(self: *Parser) ?tokenizer.Token {
         if (self.offset >= self.tokens.len) return null;
 
@@ -88,6 +100,17 @@ test "parse identifier" {
     } }, ident);
 }
 
+test "parse number" {
+    const tokens: []tokenizer.Token = @constCast(&[_]tokenizer.Token{
+        tokenizer.Token{ .NUMBER = 7 },
+    });
+    var parser = Parser.init(tokens);
+    const number = try parser.parse_number();
+    try std.testing.expectEqualDeep(Node{ .NUMBER = .{
+        .value = 7,
+    } }, number);
+}
+
 test "simple e2e" {
     const tokens: []tokenizer.Token = @constCast(&[_]tokenizer.Token{
         tokenizer.Token{ .LET = void{} },