75 lines
2.1 KiB
Rust
75 lines
2.1 KiB
Rust
use super::Interpret;
|
|
use super::{types::Value, RuntimeError};
|
|
use crate::ast::expression::{
|
|
self,
|
|
operator::{Operator, UnaryOperator},
|
|
};
|
|
use crate::interpreter::world::World;
|
|
|
|
impl Interpret for expression::ExpressionNode {
|
|
fn interpret(&self, w: &mut World) -> Result<Value, RuntimeError> {
|
|
expression::all_variants!(self, n => n.interpret(w))
|
|
}
|
|
}
|
|
|
|
impl Interpret for expression::Literal {
|
|
fn interpret(&self, _: &mut World) -> Result<Value, RuntimeError> {
|
|
Ok(self.clone().into())
|
|
}
|
|
}
|
|
|
|
impl Interpret for expression::BinaryExpr {
|
|
fn interpret(&self, w: &mut World) -> Result<Value, RuntimeError> {
|
|
let left_val = self.left.interpret(w).expect("expected lval");
|
|
let right_val = self.right.interpret(w).expect("expected rval");
|
|
match self.operator {
|
|
Operator::Plus => todo!(),
|
|
Operator::Minus => todo!(),
|
|
Operator::BangEqual => Ok((left_val != right_val).into()),
|
|
Operator::Less => Ok((left_val < right_val).into()),
|
|
Operator::LessEqual => Ok((left_val <= right_val).into()),
|
|
Operator::Greater => Ok((left_val > right_val).into()),
|
|
Operator::GreaterEqual => Ok((left_val >= right_val).into()),
|
|
Operator::EqualEqual => Ok((left_val == right_val).into()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Interpret for expression::UnaryExpr {
|
|
fn interpret(&self, w: &mut World) -> Result<Value, RuntimeError> {
|
|
let val = self.right.interpret(w)?;
|
|
match self.operator {
|
|
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 expression::GroupingExpr {
|
|
fn interpret(&self, w: &mut World) -> Result<Value, RuntimeError> {
|
|
self.0.interpret(w)
|
|
}
|
|
}
|
|
|
|
impl Interpret for expression::VariableExpr {
|
|
fn interpret(&self, world: &mut World) -> Result<Value, RuntimeError> {
|
|
match world.get_var(&self.var_name) {
|
|
Some(v) => Ok(v.clone()),
|
|
None => Err(RuntimeError),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Interpret for expression::BlockExpr {
|
|
fn interpret(&self, world: &mut World) -> Result<Value, RuntimeError> {
|
|
for stmnt in self.statements.iter() {
|
|
stmnt.interpret(world)?;
|
|
}
|
|
Ok(Value::Nil)
|
|
}
|
|
}
|