Skip to content

Commit

Permalink
#48 Also get rid of Expression enum
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 12, 2018
1 parent 9da057b commit 29c7238
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/mango/towasm/control/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ impl Wasm for Branch {
}

pub struct BranchIf {
condition: Expression,
condition: Box<Expression>,
label: Label,
}

impl BranchIf {
pub fn new(condition: Expression, label: Label) -> Self {
pub fn new(condition: Box<Expression>, label: Label) -> Self {
assert!(condition.typ() == &Type::Bool);
BranchIf { condition, label }
}
Expand Down
4 changes: 2 additions & 2 deletions src/mango/towasm/control/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ impl Wasm for Call {
}

pub struct Return {
expression: Expression,
expression: Box<Expression>,
}

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<Expression>) -> Self {
Return { expression }
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/mango/towasm/numeric/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ pub struct Add {
}

impl Add {
pub fn new(left: Expression, right: Expression) -> Self {
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
assert!(left.typ() == right.typ());
Add {
left: Box::new(left),
right: Box::new(right),
left: left,
right: right,
}
}

Expand All @@ -40,17 +40,23 @@ impl Wasm for Add {
}
}

impl Expression for Add {
fn typ(&self) -> &Type {
self.typ()
}
}

pub struct Mul {
left: Box<Expression>,
right: Box<Expression>,
}

impl Mul {
pub fn new(left: Expression, right: Expression) -> Self {
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
assert!(left.typ() == right.typ());
Mul {
left: Box::new(left),
right: Box::new(right),
left: left,
right: right,
}
}

Expand All @@ -74,3 +80,9 @@ impl Wasm for Mul {
Ok(())
}
}

impl Expression for Mul {
fn typ(&self) -> &Type {
self.typ()
}
}
28 changes: 18 additions & 10 deletions src/mango/towasm/numeric/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ pub struct Gt {
}

impl Gt {
pub fn new(left: Expression, right: Expression) -> Self {
pub fn new(left: Box<Expression>, right: Box<Expression>) -> 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 {
Expand All @@ -40,17 +36,23 @@ impl Wasm for Gt {
}
}

impl Expression for Gt {
fn typ(&self) -> &Type {
&Type::Bool
}
}

pub struct Lt {
left: Box<Expression>,
right: Box<Expression>,
}

impl Lt {
pub fn new(left: Expression, right: Expression) -> Self {
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
assert!(left.typ() == right.typ());
Lt {
left: Box::new(left),
right: Box::new(right),
left: left,
right: right,
}
}

Expand All @@ -74,3 +76,9 @@ impl Wasm for Lt {
Ok(())
}
}

impl Expression for Lt {
fn typ(&self) -> &Type {
&Type::Bool
}
}
26 changes: 10 additions & 16 deletions src/mango/towasm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
]
},
)]);
Expand Down
2 changes: 1 addition & 1 deletion src/mango/towasm/values/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io;
#[derive(new)]
pub struct Assign {
assignee: Local, // todo
value: Expression,
value: Box<Expression>,
}

impl Wasm for Assign {
Expand Down
7 changes: 7 additions & 0 deletions src/mango/towasm/values/constant.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,3 +26,9 @@ impl Wasm for Const {
unimplemented!()
}
}

impl Expression for Const {
fn typ(&self) -> &Type {
&self.typ
}
}
63 changes: 20 additions & 43 deletions src/mango/towasm/values/expression.rs
Original file line number Diff line number Diff line change
@@ -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(),
// }
// }
//}
14 changes: 10 additions & 4 deletions src/mango/towasm/values/localvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ pub struct Local {
}

impl Local {
pub fn get(&self) -> GetLocal {
GetLocal {
pub fn get(&self) -> Box<GetLocal> {
Box::new(GetLocal {
local: self.clone(),
}
})
}

pub fn set(&self, expression: Expression) -> Assign {
pub fn set(&self, expression: Box<Expression>) -> Assign {
Assign::new(self.clone(), expression)
}
}
Expand Down Expand Up @@ -97,3 +97,9 @@ impl Wasm for GetLocal {
unimplemented!()
}
}

impl Expression for GetLocal {
fn typ(&self) -> &Type {
self.typ()
}
}

0 comments on commit 29c7238

Please sign in to comment.