Skip to content

Commit

Permalink
feat: evaluation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Feb 17, 2024
1 parent 2eea15b commit 88eacc9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 42 deletions.
2 changes: 1 addition & 1 deletion program/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub struct Context {

}

impl Default for Context {
Expand Down
57 changes: 57 additions & 0 deletions program/src/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{value::Value, Context};
use parser::{ArithmeticOp, Atom, Expression, RelationOp};

impl From<Atom> for Value {
fn from(atom: Atom) -> Self {
match atom {
Atom::Int(i) => Value::Int(i),
Atom::UInt(ui) => Value::UInt(ui),
Atom::Float(f) => Value::Float(f),
Atom::Bool(b) => Value::Bool(b),
Atom::Null => Value::Null,
Atom::Bytes(b) => Value::Bytes(b),
Atom::String(s) => Value::String(s),
}
}
}

pub struct Eval {
ctx: Context,
}

impl Eval {
pub fn new(ctx: Context) -> Self {
Eval { ctx }
}
pub fn eval(&self, expr: Expression) -> Value {
match expr {
Expression::Atom(atom) => atom.into(),
Expression::Relation(left, op, right) => {
let left = self.eval(*left);
let right = self.eval(*right);
let result = match op {
RelationOp::Equals => left.eq(&right),
RelationOp::LessThan => todo!("lt"),
RelationOp::LessThanEq => todo!("lte"),
RelationOp::GreaterThan => todo!("gt"),
RelationOp::GreaterThanEq => todo!("gte"),
RelationOp::NotEquals => todo!("ne"),
RelationOp::In => todo!("in"),
};
Value::Bool(result)
}
Expression::Arithmetic(left, op, right) => {
let left = self.eval(*left);
let right = self.eval(*right);
match op {
ArithmeticOp::Add => left + right,
ArithmeticOp::Subtract => left - right,
ArithmeticOp::Divide => left / right,
ArithmeticOp::Multiply => left * right,
ArithmeticOp::Modulus => todo!("modulus"),
}
}
_ => todo!(),
}
}
}
3 changes: 2 additions & 1 deletion program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod context;
pub mod program;
pub use crate::program::Program;
pub use crate::context::Context;
mod value;
mod value;
mod eval;
10 changes: 5 additions & 5 deletions program/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use parser::{Expression};
use parser::Expression;
use parser::parser::ExpressionParser;

use std::fmt;

use std::result::Result;
use crate::context::Context;
use crate::value::Value;
use crate::eval::Eval;

pub struct Program {
expr: Expression
Expand All @@ -31,8 +30,9 @@ impl Program {
}
}

pub fn execute(&self, context: Context) -> bool {
match Value::from(&self.expr) {
pub fn execute(self, context: Context) -> bool {
let e = Eval::new(context);
match e.eval(self.expr) {
Value::Bool(b) => b,
_ => panic!("this was not supposed to happen!")
}
Expand Down
45 changes: 10 additions & 35 deletions program/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,19 @@ pub enum Value {
String(Rc<String>),
}

impl From<&Atom> for Value {
fn from(atom: &Atom) -> Self {
match atom {
Atom::Int(i) => Value::Int(*i),
Atom::UInt(ui) => Value::UInt(*ui),
Atom::Float(f) => Value::Float(*f),
Atom::Bool(b) => Value::Bool(*b),
Atom::Bytes(b) => Value::Bytes(b.clone()),
Atom::Null => Value::Null,
Atom::String(s) => Value::String(s.clone()),
}
impl std::ops::Mul for Value {
type Output = Value;

fn mul(self, rhs: Self) -> Self::Output {
todo!()
}
}

impl<'a> Value {
pub fn from(expr: &'a Expression) -> Value {
match expr {
Expression::Atom(atom) => atom.into(),
Expression::Relation(left, op, right) => {
let left = Value::from(left);
let right = Value::from(right);
let result = match op {
RelationOp::Equals => left.eq(&right),
_ => unimplemented!(),
};
Value::Bool(result)
}
Expression::Arithmetic(left, op, right) => {
let left = Value::from(left);
let right = Value::from(right);
match op {
ArithmeticOp::Add => left + right,
ArithmeticOp::Subtract => left - right,
_ => todo!(),
}
}
_ => todo!(),
}
impl std::ops::Div for Value {
type Output = Value;

fn div(self, rhs: Self) -> Self::Output {
todo!()
}
}

Expand Down

0 comments on commit 88eacc9

Please sign in to comment.