diff --git a/src/mango/towasm/control/block.rs b/src/mango/towasm/control/block.rs index 697702f8..234cace7 100644 --- a/src/mango/towasm/control/block.rs +++ b/src/mango/towasm/control/block.rs @@ -5,6 +5,7 @@ use mango::towasm::Wasm; use std::fs::File; use std::io; use std::io::Write; +use std::rc::Rc; pub struct Group { statements: Vec>, @@ -34,7 +35,7 @@ impl Wasm for Group { } pub struct Block { - name: Name, + name: Rc, group: Group, } @@ -44,7 +45,10 @@ impl Block { Block::new_named(Name::new("b".to_owned()).unwrap(), statements_gen) } - pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec>) -> Box { + pub fn new_named( + name: Rc, + statements_gen: &Fn(Label) -> Vec>, + ) -> Box { Box::new(Block { name: name.clone(), group: Group::new(Label::internal(name), statements_gen), diff --git a/src/mango/towasm/control/branch.rs b/src/mango/towasm/control/branch.rs index 1623dcb6..d516c9be 100644 --- a/src/mango/towasm/control/branch.rs +++ b/src/mango/towasm/control/branch.rs @@ -6,15 +6,16 @@ use mango::towasm::Wasm; use std::fs::File; use std::io; use std::io::Write; +use std::rc::Rc; /// Label for a block, if or loop pub struct Label { - name: Name, + name: Rc, } impl Label { /// This constructor should not be called directly; blocks should create their own references. - pub fn internal(name: Name) -> Self { + pub fn internal(name: Rc) -> Self { Label { name } } } diff --git a/src/mango/towasm/control/repeat.rs b/src/mango/towasm/control/repeat.rs index 50c32d76..24f85ec7 100644 --- a/src/mango/towasm/control/repeat.rs +++ b/src/mango/towasm/control/repeat.rs @@ -5,9 +5,10 @@ use mango::towasm::util::Name; use mango::towasm::Wasm; use std::fs::File; use std::io; +use std::rc::Rc; pub struct Loop { - name: Name, + name: Rc, group: Group, } @@ -17,20 +18,15 @@ impl Loop { Loop::new_named(Name::new("l".to_owned()).unwrap(), statements_gen) } - pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec>) -> Box { + pub fn new_named( + name: Rc, + statements_gen: &Fn(Label) -> Vec>, + ) -> Box { Box::new(Loop { name: name.clone(), group: Group::new(Label::internal(name), statements_gen), }) } - - // pub fn add(&mut self, statement: Statement) { - // self.group.add(statement); - // } - - // fn label(&self) -> Label { - // Label::internal(self.name.clone()) - // } } impl Wasm for Loop { diff --git a/src/mango/towasm/scope/function.rs b/src/mango/towasm/scope/function.rs index e175f7ac..a0a34c6b 100644 --- a/src/mango/towasm/scope/function.rs +++ b/src/mango/towasm/scope/function.rs @@ -9,6 +9,7 @@ use mango::towasm::Wasm; use std::fs::File; use std::io; use std::io::Write; +use std::rc::Rc; pub struct Parameter { // Don't box here, it's just to reference those fields @@ -16,9 +17,9 @@ pub struct Parameter { } impl Parameter { - pub fn new(name: Name, typ: Type) -> Box { + pub fn new(name: Rc, typ: Type) -> Box { // todo: should this store declare local AND name/type? - let declare_local = DeclareLocal::new_unboxed(name.clone(), typ.clone()); + let declare_local = DeclareLocal::new_unboxed(name, typ.clone()); Box::new(Parameter { declare_local }) } @@ -66,13 +67,13 @@ impl Wasm for Output { } pub struct FunctionSignature { - name: Name, + name: Rc, parameters: Vec>, results: Vec>, } impl FunctionSignature { - pub fn new(name: Name, parameters: Vec>, results: Vec>) -> Self { + pub fn new(name: Rc, parameters: Vec>, results: Vec>) -> Self { assert!(results.len() <= 1); // FunctionSignature { name, @@ -114,7 +115,7 @@ pub struct Function { impl Function { // This uses group, so it has a label, but this isn't final... It might be useless. pub fn new( - name: Name, + name: Rc, parameters: Vec>, results: Vec>, statements_gen: &Fn(Label) -> Vec>, diff --git a/src/mango/towasm/tests.rs b/src/mango/towasm/tests.rs index c6d5f245..09d54c70 100644 --- a/src/mango/towasm/tests.rs +++ b/src/mango/towasm/tests.rs @@ -38,30 +38,18 @@ fn test_example_1() { // todo: get rid of clones fac_result_decl.clone(), loop_condition_decl.clone(), - Assign::new( - fac_result.clone(), - Const::new(Type::Int32, Value::Int(1)), - ), + Assign::new(fac_result.clone(), Const::new(Type::Int32, Value::Int(1))), // Statement::Block(Block::new_named("".to_owned(), vec![])), Loop::new_named(loop_name.clone(), &|loop_label: Label| { vec![ - Assign::new( - fac_result.clone(), - Mul::new(fac_result.get(), var_n.get()), - ), + Assign::new(fac_result.clone(), Mul::new(fac_result.get(), var_n.get())), Assign::new( loop_condition.clone(), - Gt::new( - var_n.get(), - Const::new(Type::Int32, Value::Int(2)), - ), + Gt::new(var_n.get(), Const::new(Type::Int32, Value::Int(2))), ), Assign::new( var_n.clone(), - Add::new( - var_n.get(), - Const::new(Type::Int32, Value::Int(-1)), - ), + Add::new(var_n.get(), Const::new(Type::Int32, Value::Int(-1))), ), BranchIf::new(loop_condition.get(), loop_label), ] diff --git a/src/mango/towasm/util/name.rs b/src/mango/towasm/util/name.rs index 59ff71d7..0dd90c38 100644 --- a/src/mango/towasm/util/name.rs +++ b/src/mango/towasm/util/name.rs @@ -1,6 +1,7 @@ use mango::towasm::Wasm; use std::fs::File; use std::io; +use std::rc::Rc; #[derive(Clone)] pub struct Name { @@ -8,10 +9,10 @@ pub struct Name { } impl Name { - pub fn new(name: String) -> Option { + pub fn new(name: String) -> Option> { // todo: filter out illegal names assert!(!name.starts_with("$")); - return Some(Name { name }); + return Some(Rc::new(Name { name })); } pub fn pure_name(&self) -> String { diff --git a/src/mango/towasm/values/localvar.rs b/src/mango/towasm/values/localvar.rs index 12bf8754..593ccd02 100644 --- a/src/mango/towasm/values/localvar.rs +++ b/src/mango/towasm/values/localvar.rs @@ -5,6 +5,7 @@ use mango::towasm::values::Expression; use mango::towasm::Wasm; use std::fs::File; use std::io; +use std::rc::Rc; #[derive(Clone)] pub struct DeclareLocal { @@ -12,11 +13,11 @@ pub struct DeclareLocal { } impl DeclareLocal { - pub fn new(name: Name, typ: Type) -> Box { + pub fn new(name: Rc, typ: Type) -> Box { Box::new(DeclareLocal::new_unboxed(name, typ)) } - pub fn new_unboxed(name: Name, typ: Type) -> Self { + pub fn new_unboxed(name: Rc, typ: Type) -> Self { DeclareLocal { local: Local { name, typ }, } @@ -54,7 +55,7 @@ impl Statement for DeclareLocal {} /// To create an instance of Local, make a [DeclareLocal] and call [local()] #[derive(Clone)] pub struct Local { - name: Name, + name: Rc, pub typ: Type, }