Skip to content

Commit

Permalink
feat: add pow to binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
crnvl committed Aug 21, 2023
1 parent 4927a05 commit eaac19c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
4 changes: 1 addition & 3 deletions examples/spec_test.nl
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
i !false {
p("true")
}
p(2^5)
1 change: 1 addition & 0 deletions src/logic/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Parser {
|| self.current_token() == Token::Asterisk
|| self.current_token() == Token::Slash
|| self.current_token() == Token::Percent
|| self.current_token() == Token::Pow
|| self.current_token() == Token::LessThan
|| self.current_token() == Token::GreaterThan
|| self.current_token() == Token::LessThanEquals
Expand Down
8 changes: 8 additions & 0 deletions src/logic/interpeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl Interpreter {
Token::Asterisk => self.evaluate_multiplication(left, right),
Token::Slash => self.evaluate_division(left, right),
Token::Percent => self.evaluate_modulo(left, right),
Token::Pow => self.evaluate_pow(left, right),
Token::Equals => self.evaluate_equal(left, right),
Token::EqualEqual => self.evaluate_equal_equal(left, right),
Token::Bang => self.evaluate_not_equal(left, right),
Expand Down Expand Up @@ -197,6 +198,13 @@ impl Interpreter {
}
}

fn evaluate_pow(&mut self, left: ASTNode, right: ASTNode) -> ASTNode {
match (left.clone(), right.clone()) {
(ASTNode::Number(left), ASTNode::Number(right)) => ASTNode::Number(left.pow(right as u32)),
_ => panic!("Unexpected operands: {:?} ^ {:?}", left, right),
}
}

fn evaluate_equal(&mut self, left: ASTNode, right: ASTNode) -> ASTNode {
match (left.clone(), right.clone()) {
(ASTNode::Identifier(left), ASTNode::Identifier(right)) => {
Expand Down
2 changes: 2 additions & 0 deletions src/logic/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Token {
Asterisk,
Slash,
Percent,
Pow,
LeftParen,
RightParen,
LeftBrace,
Expand Down Expand Up @@ -101,6 +102,7 @@ pub fn tokenize(code: &str) -> Vec<Token> {
'*' => tokens.push(Token::Asterisk),
'/' => tokens.push(Token::Slash),
'%' => tokens.push(Token::Percent),
'^' => tokens.push(Token::Pow),
'(' => tokens.push(Token::LeftParen),
')' => tokens.push(Token::RightParen),
'{' => tokens.push(Token::LeftBrace),
Expand Down

0 comments on commit eaac19c

Please sign in to comment.