diff --git a/examples/spec_test.nl b/examples/spec_test.nl index f5c3015..77c3e94 100644 --- a/examples/spec_test.nl +++ b/examples/spec_test.nl @@ -1,3 +1 @@ -i !false { - p("true") -} \ No newline at end of file +p(2^5) \ No newline at end of file diff --git a/src/logic/ast.rs b/src/logic/ast.rs index e291df6..593ed11 100644 --- a/src/logic/ast.rs +++ b/src/logic/ast.rs @@ -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 diff --git a/src/logic/interpeter.rs b/src/logic/interpeter.rs index 743445a..83cc371 100644 --- a/src/logic/interpeter.rs +++ b/src/logic/interpeter.rs @@ -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), @@ -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)) => { diff --git a/src/logic/tokenizer.rs b/src/logic/tokenizer.rs index 2faacb7..b136e90 100644 --- a/src/logic/tokenizer.rs +++ b/src/logic/tokenizer.rs @@ -25,6 +25,7 @@ pub enum Token { Asterisk, Slash, Percent, + Pow, LeftParen, RightParen, LeftBrace, @@ -101,6 +102,7 @@ pub fn tokenize(code: &str) -> Vec { '*' => 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),