From 4fa78336d00e5a82d47850ce3b349a5fe4acdf01 Mon Sep 17 00:00:00 2001 From: bad Date: Wed, 6 Apr 2022 17:39:19 +0200 Subject: [PATCH] Implement the negation operator --- src/ast/parser.rs | 5 ++++- src/interpreter/ast_walker.rs | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/ast/parser.rs b/src/ast/parser.rs index 2f744a8..199bc77 100644 --- a/src/ast/parser.rs +++ b/src/ast/parser.rs @@ -1,6 +1,7 @@ use super::astnode; use super::astnode::{ASTNode, BinaryExpr}; use crate::lexer::{token, token::TokenType}; + use std::iter; use std::result::Result as StdResult; @@ -8,6 +9,7 @@ type Result = StdResult; #[derive(Debug)] pub enum ASTParsingError { + IncorrectToken(TokenType), UnmatchedBrace, } @@ -15,6 +17,7 @@ impl std::fmt::Display for ASTParsingError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match *self { Self::UnmatchedBrace => write!(f, "Unmatched brace"), + Self::IncorrectToken(ref token) => write!(f, "Incorrect token {:?}", token), } } } @@ -159,7 +162,7 @@ impl<'a, T: Iterator>> Parser<'a, T> { None => return Err(ASTParsingError::UnmatchedBrace), } } - Some(a) => panic!("{:#?}", a), + Some(a) => return Err(ASTParsingError::IncorrectToken(a)), None => todo!(), }; Ok(node) diff --git a/src/interpreter/ast_walker.rs b/src/interpreter/ast_walker.rs index 268e600..da478d5 100644 --- a/src/interpreter/ast_walker.rs +++ b/src/interpreter/ast_walker.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use super::types; +use super::types::Value; use crate::ast::astnode::{self, UnaryOperator}; #[derive(Debug)] @@ -14,23 +14,23 @@ impl Display for RuntimeError { impl std::error::Error for RuntimeError {} pub trait Interpret { - fn interpret(&self) -> Result; + fn interpret(&self) -> Result; } impl Interpret for astnode::ASTNode { - fn interpret(&self) -> Result { + fn interpret(&self) -> Result { astnode::all_variants!(self, n => n.interpret()) } } impl Interpret for astnode::Literal { - fn interpret(&self) -> Result { + fn interpret(&self) -> Result { Ok(self.clone().into()) } } impl Interpret for astnode::BinaryExpr { - fn interpret(&self) -> Result { + fn interpret(&self) -> Result { let left_val = self.left.interpret().expect("expected lval"); let right_val = self.right.interpret().expect("expected rval"); match self.operator { @@ -46,17 +46,21 @@ impl Interpret for astnode::BinaryExpr { } impl Interpret for astnode::UnaryExpr { - fn interpret(&self) -> Result { + fn interpret(&self) -> Result { let val = self.right.interpret()?; match self.operator { - UnaryOperator::Bang => Ok(types::Value::Bool(!val.truthy())), - UnaryOperator::Minus => todo!(), + UnaryOperator::Bang => Ok(Value::Bool(!val.truthy())), + UnaryOperator::Minus => match val { + Value::Int(i) => Ok(Value::Int(-i)), + Value::Float(f) => Ok(Value::Float(-f)), + _ => Err(RuntimeError), + }, } } } impl Interpret for astnode::GroupingExpr { - fn interpret(&self) -> Result { + fn interpret(&self) -> Result { self.0.interpret() } }