diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-18 23:34:35 +0100 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2025-01-18 23:34:35 +0100 |
commit | 90679500271f9d9e19b252ee627a4fc86e147ef6 (patch) | |
tree | 8f8995827dce38bb5a9a57b753178f36935e4c85 | |
parent | Feature: Add basic support for if statements (diff) | |
download | interpreter-90679500271f9d9e19b252ee627a4fc86e147ef6.tar.gz interpreter-90679500271f9d9e19b252ee627a4fc86e147ef6.tar.bz2 interpreter-90679500271f9d9e19b252ee627a4fc86e147ef6.zip |
Feature: Add support for substraction
-rw-r--r-- | examples/7.src | 2 | ||||
-rw-r--r-- | grammar.ebnf | 4 | ||||
-rw-r--r-- | src/evaluator.zig | 3 | ||||
-rw-r--r-- | src/parser.zig | 8 | ||||
-rw-r--r-- | src/tokenizer.zig | 3 |
5 files changed, 13 insertions, 7 deletions
diff --git a/examples/7.src b/examples/7.src index 7b8fe88..0cb0721 100644 --- a/examples/7.src +++ b/examples/7.src @@ -1,7 +1,7 @@ let main = () => { let i = 4; - if i == 4 { + if i - 4 { print(i); return i; }; diff --git a/grammar.ebnf b/grammar.ebnf index caf8c73..86710a2 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -12,11 +12,9 @@ ReturnStatement ::= RETURN Expression FunctionArguments ::= Expression ("," Expression)* ---TODO: == - Expression ::= AdditiveExpression | FunctionDefinition -AdditiveExpression ::= PrimaryExpression ("+" AdditiveExpression) +AdditiveExpression ::= PrimaryExpression (("+" | "-") AdditiveExpression) PrimaryExpression ::= NUMBER | IDENTIFIER | FunctionCallStatement diff --git a/src/evaluator.zig b/src/evaluator.zig index ec5df8c..2fc5441 100644 --- a/src/evaluator.zig +++ b/src/evaluator.zig @@ -136,7 +136,8 @@ pub const Evaluator = struct { .ADDITIVE_EXPRESSION => |x| { const lhs = try self.get_expression_value(x.lhs); const rhs = try self.get_expression_value(x.rhs); - return lhs + rhs; + if (x.addition) return lhs + rhs; + return lhs - rhs; }, .PRIMARY_EXPRESSION => |x| { switch (x) { diff --git a/src/parser.zig b/src/parser.zig index 81006f8..36f7437 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -48,6 +48,7 @@ pub const Node = union(NodeType) { }, }, ADDITIVE_EXPRESSION: struct { + addition: bool, lhs: *Node, rhs: *Node, }, @@ -216,15 +217,18 @@ pub const Parser = struct { return ParserError.ParsingError; } - // AdditiveExpression ::= PrimaryExpression ("+" AdditiveExpression) + // AdditiveExpression ::= PrimaryExpression (("+" | "-") AdditiveExpression) fn parse_additive_expression(self: *Parser) ParserError!*Node { errdefer if (!self.try_context) std.debug.print("Error parsing additive expression\n", .{}); const lhs = try self.parse_primary_expression(); - if (self.accept_token(tokenizer.TokenType.PLUS) != null) { + const plus = self.accept_token(tokenizer.TokenType.PLUS); + const minus = self.accept_token(tokenizer.TokenType.MINUS); + if (plus != null or minus != null) { const rhs = try self.parse_additive_expression(); return self.create_node(.{ .ADDITIVE_EXPRESSION = .{ + .addition = plus != null, .lhs = lhs, .rhs = rhs, } }); diff --git a/src/tokenizer.zig b/src/tokenizer.zig index f5908b2..e395835 100644 --- a/src/tokenizer.zig +++ b/src/tokenizer.zig @@ -20,6 +20,7 @@ pub const TokenType = enum { // Operators EQUALS, PLUS, + MINUS, // Punctuation SEMICOLON, @@ -39,6 +40,7 @@ pub const Token = union(TokenType) { NUMBER: i64, EQUALS: void, PLUS: void, + MINUS: void, SEMICOLON: void, COMMA: void, LPAREN: void, @@ -74,6 +76,7 @@ pub const Tokenizer = struct { if (c == '}') return Token{ .RBRACE = void{} }; if (c == '=') return Token{ .EQUALS = void{} }; if (c == '+') return Token{ .PLUS = void{} }; + if (c == '-') return Token{ .MINUS = void{} }; const string = self.consume_string(); if (string.len == 0) return TokenizerError.TokenizingError; |