Skip to content

Commit 3071ee1

Browse files
committed
#48 Make names boxed to avoid cloning
1 parent 1f6b6be commit 3071ee1

File tree

7 files changed

+32
-40
lines changed

7 files changed

+32
-40
lines changed

src/mango/towasm/control/block.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use mango::towasm::Wasm;
55
use std::fs::File;
66
use std::io;
77
use std::io::Write;
8+
use std::rc::Rc;
89

910
pub struct Group {
1011
statements: Vec<Box<Statement>>,
@@ -34,7 +35,7 @@ impl Wasm for Group {
3435
}
3536

3637
pub struct Block {
37-
name: Name,
38+
name: Rc<Name>,
3839
group: Group,
3940
}
4041

@@ -44,7 +45,10 @@ impl Block {
4445
Block::new_named(Name::new("b".to_owned()).unwrap(), statements_gen)
4546
}
4647

47-
pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Box<Self> {
48+
pub fn new_named(
49+
name: Rc<Name>,
50+
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,
51+
) -> Box<Self> {
4852
Box::new(Block {
4953
name: name.clone(),
5054
group: Group::new(Label::internal(name), statements_gen),

src/mango/towasm/control/branch.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ use mango::towasm::Wasm;
66
use std::fs::File;
77
use std::io;
88
use std::io::Write;
9+
use std::rc::Rc;
910

1011
/// Label for a block, if or loop
1112
pub struct Label {
12-
name: Name,
13+
name: Rc<Name>,
1314
}
1415

1516
impl Label {
1617
/// This constructor should not be called directly; blocks should create their own references.
17-
pub fn internal(name: Name) -> Self {
18+
pub fn internal(name: Rc<Name>) -> Self {
1819
Label { name }
1920
}
2021
}

src/mango/towasm/control/repeat.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use mango::towasm::util::Name;
55
use mango::towasm::Wasm;
66
use std::fs::File;
77
use std::io;
8+
use std::rc::Rc;
89

910
pub struct Loop {
10-
name: Name,
11+
name: Rc<Name>,
1112
group: Group,
1213
}
1314

@@ -17,20 +18,15 @@ impl Loop {
1718
Loop::new_named(Name::new("l".to_owned()).unwrap(), statements_gen)
1819
}
1920

20-
pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Box<Self> {
21+
pub fn new_named(
22+
name: Rc<Name>,
23+
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,
24+
) -> Box<Self> {
2125
Box::new(Loop {
2226
name: name.clone(),
2327
group: Group::new(Label::internal(name), statements_gen),
2428
})
2529
}
26-
27-
// pub fn add(&mut self, statement: Statement) {
28-
// self.group.add(statement);
29-
// }
30-
31-
// fn label(&self) -> Label {
32-
// Label::internal(self.name.clone())
33-
// }
3430
}
3531

3632
impl Wasm for Loop {

src/mango/towasm/scope/function.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ use mango::towasm::Wasm;
99
use std::fs::File;
1010
use std::io;
1111
use std::io::Write;
12+
use std::rc::Rc;
1213

1314
pub struct Parameter {
1415
// Don't box here, it's just to reference those fields
1516
declare_local: DeclareLocal,
1617
}
1718

1819
impl Parameter {
19-
pub fn new(name: Name, typ: Type) -> Box<Self> {
20+
pub fn new(name: Rc<Name>, typ: Type) -> Box<Self> {
2021
// todo: should this store declare local AND name/type?
21-
let declare_local = DeclareLocal::new_unboxed(name.clone(), typ.clone());
22+
let declare_local = DeclareLocal::new_unboxed(name, typ.clone());
2223
Box::new(Parameter { declare_local })
2324
}
2425

@@ -66,13 +67,13 @@ impl Wasm for Output {
6667
}
6768

6869
pub struct FunctionSignature {
69-
name: Name,
70+
name: Rc<Name>,
7071
parameters: Vec<Box<Parameter>>,
7172
results: Vec<Box<Output>>,
7273
}
7374

7475
impl FunctionSignature {
75-
pub fn new(name: Name, parameters: Vec<Box<Parameter>>, results: Vec<Box<Output>>) -> Self {
76+
pub fn new(name: Rc<Name>, parameters: Vec<Box<Parameter>>, results: Vec<Box<Output>>) -> Self {
7677
assert!(results.len() <= 1); //
7778
FunctionSignature {
7879
name,
@@ -114,7 +115,7 @@ pub struct Function {
114115
impl Function {
115116
// This uses group, so it has a label, but this isn't final... It might be useless.
116117
pub fn new(
117-
name: Name,
118+
name: Rc<Name>,
118119
parameters: Vec<Box<Parameter>>,
119120
results: Vec<Box<Output>>,
120121
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,

src/mango/towasm/tests.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,18 @@ fn test_example_1() {
3838
// todo: get rid of clones
3939
fac_result_decl.clone(),
4040
loop_condition_decl.clone(),
41-
Assign::new(
42-
fac_result.clone(),
43-
Const::new(Type::Int32, Value::Int(1)),
44-
),
41+
Assign::new(fac_result.clone(), Const::new(Type::Int32, Value::Int(1))),
4542
// Statement::Block(Block::new_named("".to_owned(), vec![])),
4643
Loop::new_named(loop_name.clone(), &|loop_label: Label| {
4744
vec![
48-
Assign::new(
49-
fac_result.clone(),
50-
Mul::new(fac_result.get(), var_n.get()),
51-
),
45+
Assign::new(fac_result.clone(), Mul::new(fac_result.get(), var_n.get())),
5246
Assign::new(
5347
loop_condition.clone(),
54-
Gt::new(
55-
var_n.get(),
56-
Const::new(Type::Int32, Value::Int(2)),
57-
),
48+
Gt::new(var_n.get(), Const::new(Type::Int32, Value::Int(2))),
5849
),
5950
Assign::new(
6051
var_n.clone(),
61-
Add::new(
62-
var_n.get(),
63-
Const::new(Type::Int32, Value::Int(-1)),
64-
),
52+
Add::new(var_n.get(), Const::new(Type::Int32, Value::Int(-1))),
6553
),
6654
BranchIf::new(loop_condition.get(), loop_label),
6755
]

src/mango/towasm/util/name.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use mango::towasm::Wasm;
22
use std::fs::File;
33
use std::io;
4+
use std::rc::Rc;
45

56
#[derive(Clone)]
67
pub struct Name {
78
name: String,
89
}
910

1011
impl Name {
11-
pub fn new(name: String) -> Option<Self> {
12+
pub fn new(name: String) -> Option<Rc<Self>> {
1213
// todo: filter out illegal names
1314
assert!(!name.starts_with("$"));
14-
return Some(Name { name });
15+
return Some(Rc::new(Name { name }));
1516
}
1617

1718
pub fn pure_name(&self) -> String {

src/mango/towasm/values/localvar.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ use mango::towasm::values::Expression;
55
use mango::towasm::Wasm;
66
use std::fs::File;
77
use std::io;
8+
use std::rc::Rc;
89

910
#[derive(Clone)]
1011
pub struct DeclareLocal {
1112
local: Local,
1213
}
1314

1415
impl DeclareLocal {
15-
pub fn new(name: Name, typ: Type) -> Box<Self> {
16+
pub fn new(name: Rc<Name>, typ: Type) -> Box<Self> {
1617
Box::new(DeclareLocal::new_unboxed(name, typ))
1718
}
1819

19-
pub fn new_unboxed(name: Name, typ: Type) -> Self {
20+
pub fn new_unboxed(name: Rc<Name>, typ: Type) -> Self {
2021
DeclareLocal {
2122
local: Local { name, typ },
2223
}
@@ -54,7 +55,7 @@ impl Statement for DeclareLocal {}
5455
/// To create an instance of Local, make a [DeclareLocal] and call [local()]
5556
#[derive(Clone)]
5657
pub struct Local {
57-
name: Name,
58+
name: Rc<Name>,
5859
pub typ: Type,
5960
}
6061

0 commit comments

Comments
 (0)