Skip to content

Commit

Permalink
#48 Make names boxed to avoid cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 12, 2018
1 parent 1f6b6be commit 3071ee1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 40 deletions.
8 changes: 6 additions & 2 deletions src/mango/towasm/control/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<Statement>>,
Expand Down Expand Up @@ -34,7 +35,7 @@ impl Wasm for Group {
}

pub struct Block {
name: Name,
name: Rc<Name>,
group: Group,
}

Expand All @@ -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<Statement>>) -> Box<Self> {
pub fn new_named(
name: Rc<Name>,
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,
) -> Box<Self> {
Box::new(Block {
name: name.clone(),
group: Group::new(Label::internal(name), statements_gen),
Expand Down
5 changes: 3 additions & 2 deletions src/mango/towasm/control/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name>,
}

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<Name>) -> Self {
Label { name }
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/mango/towasm/control/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name>,
group: Group,
}

Expand All @@ -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<Statement>>) -> Box<Self> {
pub fn new_named(
name: Rc<Name>,
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,
) -> Box<Self> {
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 {
Expand Down
11 changes: 6 additions & 5 deletions src/mango/towasm/scope/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ 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
declare_local: DeclareLocal,
}

impl Parameter {
pub fn new(name: Name, typ: Type) -> Box<Self> {
pub fn new(name: Rc<Name>, typ: Type) -> Box<Self> {
// 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 })
}

Expand Down Expand Up @@ -66,13 +67,13 @@ impl Wasm for Output {
}

pub struct FunctionSignature {
name: Name,
name: Rc<Name>,
parameters: Vec<Box<Parameter>>,
results: Vec<Box<Output>>,
}

impl FunctionSignature {
pub fn new(name: Name, parameters: Vec<Box<Parameter>>, results: Vec<Box<Output>>) -> Self {
pub fn new(name: Rc<Name>, parameters: Vec<Box<Parameter>>, results: Vec<Box<Output>>) -> Self {
assert!(results.len() <= 1); //
FunctionSignature {
name,
Expand Down Expand Up @@ -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<Name>,
parameters: Vec<Box<Parameter>>,
results: Vec<Box<Output>>,
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,
Expand Down
20 changes: 4 additions & 16 deletions src/mango/towasm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand Down
5 changes: 3 additions & 2 deletions src/mango/towasm/util/name.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use mango::towasm::Wasm;
use std::fs::File;
use std::io;
use std::rc::Rc;

#[derive(Clone)]
pub struct Name {
name: String,
}

impl Name {
pub fn new(name: String) -> Option<Self> {
pub fn new(name: String) -> Option<Rc<Self>> {
// 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 {
Expand Down
7 changes: 4 additions & 3 deletions src/mango/towasm/values/localvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ 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 {
local: Local,
}

impl DeclareLocal {
pub fn new(name: Name, typ: Type) -> Box<Self> {
pub fn new(name: Rc<Name>, typ: Type) -> Box<Self> {
Box::new(DeclareLocal::new_unboxed(name, typ))
}

pub fn new_unboxed(name: Name, typ: Type) -> Self {
pub fn new_unboxed(name: Rc<Name>, typ: Type) -> Self {
DeclareLocal {
local: Local { name, typ },
}
Expand Down Expand Up @@ -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<Name>,
pub typ: Type,
}

Expand Down

0 comments on commit 3071ee1

Please sign in to comment.