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 0291047
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 47 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
64 changes: 64 additions & 0 deletions program/src/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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"),
}
}
Expression::Ternary(_, _, _) => todo!(),
Expression::Or(_, _) => todo!(),
Expression::And(_, _) => todo!(),
Expression::Unary(_, _) => todo!(),
Expression::Member(_, _) => todo!(),
Expression::List(_) => todo!(),
Expression::Map(_) => todo!(),
Expression::Ident(_) => 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
66 changes: 30 additions & 36 deletions program/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use parser::{ArithmeticOp, Atom, Expression, RelationOp};
use std::rc::Rc;

#[derive(PartialEq)]
Expand All @@ -12,43 +11,38 @@ 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::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::Int(v) => write!(f, "int({})", v),
Value::UInt(v) => write!(f, "uint({})", v),
Value::Float(v) => write!(f, "float({})", v),
Value::Null => write!(f, "null"),
Self::Bool(v) => write!(f, "bool({})", v),
Value::Bytes(v) => write!(f, "bytes(len = {})", v.len()),
Value::String(v) => write!(f, "string({})", v),
}
}
}

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::Mul for Value {
type Output = Value;

fn mul(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Value::Int(l), Value::Int(r)) => Value::Int(l * r),
(a, b) => todo!("mul {} {}", a, b),
}
}
}

impl std::ops::Div for Value {
type Output = Value;

fn div(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Value::Int(l), Value::Int(r)) => Value::Int(l / r),
(a, b) => todo!("div {} {}", a, b),
}
}
}
Expand All @@ -59,7 +53,7 @@ impl std::ops::Add<Value> for Value {
fn add(self, o: Value) -> Self::Output {
match (self, o) {
(Value::Int(l), Value::Int(r)) => Value::Int(l + r),
_ => todo!("add"),
(a, b) => todo!("add {} {}", a, b),
}
}
}
Expand All @@ -69,7 +63,7 @@ impl std::ops::Sub<Value> for Value {
fn sub(self, o: Value) -> Self::Output {
match (self, o) {
(Value::Int(l), Value::Int(r)) => Value::Int(l - r),
_ => todo!("sub"),
(a, b) => todo!("sub {} {}", a, b),
}
}
}
12 changes: 8 additions & 4 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@

const editor = document.querySelector("wc-codemirror");

requestAnimationFrame(() => editor.setValue("5 == -5" + "\n".repeat(8)))
requestAnimationFrame(() => editor.setValue("(2000 / 2 == 1000) - 2 == 998"))

const card = document.querySelector("sp-card");

window.run = function() {
const r = execute(editor.value);
card.subheading = `Result is: ${r}`;
console.log("here");
try {
const r = execute(editor.value);
card.subheading = `Result is: ${r}`;
} catch (e) {
card.subheading = `Failed to evaluate: ${e.message}`
}

}
</script>
<style>
Expand Down

0 comments on commit 0291047

Please sign in to comment.