From 29c7238c9ce7ba68fcbe4d4e7b799a96aff33678 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 12 May 2018 21:59:17 +0200 Subject: [PATCH] #48 Also get rid of Expression enum --- src/mango/towasm/control/branch.rs | 4 +- src/mango/towasm/control/function.rs | 4 +- src/mango/towasm/numeric/arithmetic.rs | 24 +++++++--- src/mango/towasm/numeric/logic.rs | 28 ++++++++---- src/mango/towasm/tests.rs | 26 ++++------- src/mango/towasm/values/assign.rs | 2 +- src/mango/towasm/values/constant.rs | 7 +++ src/mango/towasm/values/expression.rs | 63 ++++++++------------------ src/mango/towasm/values/localvar.rs | 14 ++++-- 9 files changed, 88 insertions(+), 84 deletions(-) diff --git a/src/mango/towasm/control/branch.rs b/src/mango/towasm/control/branch.rs index 85b261ae..ce9d2ecf 100644 --- a/src/mango/towasm/control/branch.rs +++ b/src/mango/towasm/control/branch.rs @@ -50,12 +50,12 @@ impl Wasm for Branch { } pub struct BranchIf { - condition: Expression, + condition: Box, label: Label, } impl BranchIf { - pub fn new(condition: Expression, label: Label) -> Self { + pub fn new(condition: Box, label: Label) -> Self { assert!(condition.typ() == &Type::Bool); BranchIf { condition, label } } diff --git a/src/mango/towasm/control/function.rs b/src/mango/towasm/control/function.rs index c6cb82b1..0c904f42 100644 --- a/src/mango/towasm/control/function.rs +++ b/src/mango/towasm/control/function.rs @@ -29,12 +29,12 @@ impl Wasm for Call { } pub struct Return { - expression: Expression, + expression: Box, } impl Return { // Take label to make sure this inside a function, might be used in the future, or removed... - pub fn new(_label: Label, expression: Expression) -> Self { + pub fn new(_label: Label, expression: Box) -> Self { Return { expression } } } diff --git a/src/mango/towasm/numeric/arithmetic.rs b/src/mango/towasm/numeric/arithmetic.rs index 52cc0a33..bd4fddde 100644 --- a/src/mango/towasm/numeric/arithmetic.rs +++ b/src/mango/towasm/numeric/arithmetic.rs @@ -11,11 +11,11 @@ pub struct Add { } impl Add { - pub fn new(left: Expression, right: Expression) -> Self { + pub fn new(left: Box, right: Box) -> Self { assert!(left.typ() == right.typ()); Add { - left: Box::new(left), - right: Box::new(right), + left: left, + right: right, } } @@ -40,17 +40,23 @@ impl Wasm for Add { } } +impl Expression for Add { + fn typ(&self) -> &Type { + self.typ() + } +} + pub struct Mul { left: Box, right: Box, } impl Mul { - pub fn new(left: Expression, right: Expression) -> Self { + pub fn new(left: Box, right: Box) -> Self { assert!(left.typ() == right.typ()); Mul { - left: Box::new(left), - right: Box::new(right), + left: left, + right: right, } } @@ -74,3 +80,9 @@ impl Wasm for Mul { Ok(()) } } + +impl Expression for Mul { + fn typ(&self) -> &Type { + self.typ() + } +} diff --git a/src/mango/towasm/numeric/logic.rs b/src/mango/towasm/numeric/logic.rs index 8c337058..640955d3 100644 --- a/src/mango/towasm/numeric/logic.rs +++ b/src/mango/towasm/numeric/logic.rs @@ -11,17 +11,13 @@ pub struct Gt { } impl Gt { - pub fn new(left: Expression, right: Expression) -> Self { + pub fn new(left: Box, right: Box) -> Self { assert!(left.typ() == right.typ()); Gt { - left: Box::new(left), - right: Box::new(right), + left: left, + right: right, } } - - pub fn typ(&self) -> &Type { - &Type::Bool - } } impl Wasm for Gt { @@ -40,17 +36,23 @@ impl Wasm for Gt { } } +impl Expression for Gt { + fn typ(&self) -> &Type { + &Type::Bool + } +} + pub struct Lt { left: Box, right: Box, } impl Lt { - pub fn new(left: Expression, right: Expression) -> Self { + pub fn new(left: Box, right: Box) -> Self { assert!(left.typ() == right.typ()); Lt { - left: Box::new(left), - right: Box::new(right), + left: left, + right: right, } } @@ -74,3 +76,9 @@ impl Wasm for Lt { Ok(()) } } + +impl Expression for Lt { + fn typ(&self) -> &Type { + &Type::Bool + } +} diff --git a/src/mango/towasm/tests.rs b/src/mango/towasm/tests.rs index 350b5b4b..590f8b7d 100644 --- a/src/mango/towasm/tests.rs +++ b/src/mango/towasm/tests.rs @@ -41,39 +41,33 @@ fn test_example_1() { Box::new(loop_condition_decl.clone()), Box::new(Assign::new( fac_result.clone(), - Expression::Const(Const::new(Type::Int32, Value::Int(1))), + Box::new(Const::new(Type::Int32, Value::Int(1))), )), // Statement::Block(Block::new_named("".to_owned(), vec![])), Box::new(Loop::new_named(loop_name.clone(), &|loop_label: Label| { vec![ Box::new(Assign::new( fac_result.clone(), - Expression::Mul(Mul::new( - Expression::Local(fac_result.get()), - Expression::Local(var_n.get()), - )), + Box::new(Mul::new(fac_result.get(), var_n.get())), )), Box::new(Assign::new( loop_condition.clone(), - Expression::Gt(Gt::new( - Expression::Local(var_n.get()), - Expression::Const(Const::new(Type::Int32, Value::Int(2))), + Box::new(Gt::new( + var_n.get(), + Box::new(Const::new(Type::Int32, Value::Int(2))), )), )), Box::new(Assign::new( var_n.clone(), - Expression::Add(Add::new( - Expression::Local(var_n.get()), - Expression::Const(Const::new(Type::Int32, Value::Int(-1))), + Box::new(Add::new( + var_n.get(), + Box::new(Const::new(Type::Int32, Value::Int(-1))), )), )), - Box::new(BranchIf::new( - Expression::Local(loop_condition.get()), - loop_label, - )), + Box::new(BranchIf::new(loop_condition.get(), loop_label)), ] })), - Box::new(Return::new(func_label, Expression::Local(fac_result.get()))), + Box::new(Return::new(func_label, fac_result.get())), ] }, )]); diff --git a/src/mango/towasm/values/assign.rs b/src/mango/towasm/values/assign.rs index b17cbabe..570dcfc4 100644 --- a/src/mango/towasm/values/assign.rs +++ b/src/mango/towasm/values/assign.rs @@ -8,7 +8,7 @@ use std::io; #[derive(new)] pub struct Assign { assignee: Local, // todo - value: Expression, + value: Box, } impl Wasm for Assign { diff --git a/src/mango/towasm/values/constant.rs b/src/mango/towasm/values/constant.rs index 7000d34d..fd012c8d 100644 --- a/src/mango/towasm/values/constant.rs +++ b/src/mango/towasm/values/constant.rs @@ -1,5 +1,6 @@ use mango::towasm::collect::datatype::Value; use mango::towasm::collect::Type; +use mango::towasm::values::Expression; use mango::towasm::Wasm; use std::fs::File; use std::io; @@ -25,3 +26,9 @@ impl Wasm for Const { unimplemented!() } } + +impl Expression for Const { + fn typ(&self) -> &Type { + &self.typ + } +} diff --git a/src/mango/towasm/values/expression.rs b/src/mango/towasm/values/expression.rs index 6940c49f..76e2aa03 100644 --- a/src/mango/towasm/values/expression.rs +++ b/src/mango/towasm/values/expression.rs @@ -1,50 +1,27 @@ use mango::towasm::collect::Type; -use mango::towasm::numeric::Add; -use mango::towasm::numeric::Gt; -use mango::towasm::numeric::Lt; -use mango::towasm::numeric::Mul; -use mango::towasm::values::localvar::GetLocal; -use mango::towasm::values::Const; use mango::towasm::Wasm; -use std::fs::File; -use std::io; /// A (combination of) operations that has an output value -pub enum Expression { - Const(Const), - Local(GetLocal), - Mul(Mul), - Add(Add), - Gt(Gt), - Lt(Lt), -} +pub trait Expression: Wasm { + fn typ(&self) -> &Type; -impl Expression { - pub fn typ(&self) -> &Type { - match self { - Expression::Const(op) => &op.typ, - Expression::Local(op) => &op.typ(), - Expression::Mul(op) => &op.typ(), - Expression::Add(op) => &op.typ(), - Expression::Gt(op) => &op.typ(), - Expression::Lt(op) => &op.typ(), - } - } + // Const(Const), + // Local(GetLocal), + // Mul(Mul), + // Add(Add), + // Gt(Gt), + // Lt(Lt), } -impl Wasm for Expression { - fn as_wat(&self) -> String { - match self { - Expression::Const(op) => op.as_wat(), - Expression::Local(op) => op.as_wat(), - Expression::Mul(op) => op.as_wat(), - Expression::Add(op) => op.as_wat(), - Expression::Gt(op) => op.as_wat(), - Expression::Lt(op) => op.as_wat(), - } - } - - fn write_wasm(&self, file: &mut File) -> io::Result<()> { - unimplemented!() - } -} +//impl Expression { +// pub fn typ(&self) -> &Type { +// match self { +// Expression::Const(op) => &op.typ, +// Expression::Local(op) => &op.typ(), +// Expression::Mul(op) => &op.typ(), +// Expression::Add(op) => &op.typ(), +// Expression::Gt(op) => &op.typ(), +// Expression::Lt(op) => &op.typ(), +// } +// } +//} diff --git a/src/mango/towasm/values/localvar.rs b/src/mango/towasm/values/localvar.rs index a6f927cf..6b6e036e 100644 --- a/src/mango/towasm/values/localvar.rs +++ b/src/mango/towasm/values/localvar.rs @@ -56,13 +56,13 @@ pub struct Local { } impl Local { - pub fn get(&self) -> GetLocal { - GetLocal { + pub fn get(&self) -> Box { + Box::new(GetLocal { local: self.clone(), - } + }) } - pub fn set(&self, expression: Expression) -> Assign { + pub fn set(&self, expression: Box) -> Assign { Assign::new(self.clone(), expression) } } @@ -97,3 +97,9 @@ impl Wasm for GetLocal { unimplemented!() } } + +impl Expression for GetLocal { + fn typ(&self) -> &Type { + self.typ() + } +}