-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
pub struct Context { | ||
|
||
} | ||
|
||
impl Default for Context { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters