Skip to content

Commit 1f6b6be

Browse files
committed
#48 Move boxing to constructors
1 parent 29c7238 commit 1f6b6be

File tree

13 files changed

+81
-86
lines changed

13 files changed

+81
-86
lines changed

src/mango/towasm/collect/typ.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
use mango::towasm::control::Branch;
2-
use mango::towasm::control::Group;
3-
use mango::towasm::numeric::Add;
4-
use mango::towasm::numeric::Gt;
5-
use mango::towasm::numeric::Lt;
6-
use mango::towasm::numeric::Mul;
7-
use mango::towasm::scope::Module;
81
use std::fs::File;
92
use std::io;
103

src/mango/towasm/control/block.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct Group {
1111
}
1212

1313
impl Group {
14-
pub fn new(label: Label, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Group {
14+
pub fn new(label: Label, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Self {
1515
Group {
1616
statements: statements_gen(label),
1717
}
@@ -39,16 +39,16 @@ pub struct Block {
3939
}
4040

4141
impl Block {
42-
pub fn new(statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Self {
42+
pub fn new(statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Box<Self> {
4343
// todo: determine name automatically
4444
Block::new_named(Name::new("b".to_owned()).unwrap(), statements_gen)
4545
}
4646

47-
pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Self {
48-
Block {
47+
pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Box<Self> {
48+
Box::new(Block {
4949
name: name.clone(),
5050
group: Group::new(Label::internal(name), statements_gen),
51-
}
51+
})
5252
}
5353
}
5454

src/mango/towasm/control/branch.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl Wasm for Label {
3232
pub struct Branch {}
3333

3434
impl Branch {
35-
pub fn new() -> Self {
36-
Branch {}
35+
pub fn new() -> Box<Self> {
36+
Box::new(Branch {})
3737
}
3838
}
3939

@@ -55,9 +55,9 @@ pub struct BranchIf {
5555
}
5656

5757
impl BranchIf {
58-
pub fn new(condition: Box<Expression>, label: Label) -> Self {
58+
pub fn new(condition: Box<Expression>, label: Label) -> Box<Self> {
5959
assert!(condition.typ() == &Type::Bool);
60-
BranchIf { condition, label }
60+
Box::new(BranchIf { condition, label })
6161
}
6262
}
6363

src/mango/towasm/control/function.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::io::Write;
1111
pub struct Call {}
1212

1313
impl Call {
14-
pub fn new() -> Self {
15-
Call {}
14+
pub fn new() -> Box<Self> {
15+
Box::new(Call {})
1616
}
1717
}
1818

@@ -34,8 +34,8 @@ pub struct Return {
3434

3535
impl Return {
3636
// Take label to make sure this inside a function, might be used in the future, or removed...
37-
pub fn new(_label: Label, expression: Box<Expression>) -> Self {
38-
Return { expression }
37+
pub fn new(_label: Label, expression: Box<Expression>) -> Box<Self> {
38+
Box::new(Return { expression })
3939
}
4040
}
4141

src/mango/towasm/control/repeat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ pub struct Loop {
1212
}
1313

1414
impl Loop {
15-
pub fn new(statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Self {
15+
pub fn new(statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Box<Self> {
1616
// todo: determine name automatically
1717
Loop::new_named(Name::new("l".to_owned()).unwrap(), statements_gen)
1818
}
1919

20-
pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Self {
21-
Loop {
20+
pub fn new_named(name: Name, statements_gen: &Fn(Label) -> Vec<Box<Statement>>) -> Box<Self> {
21+
Box::new(Loop {
2222
name: name.clone(),
2323
group: Group::new(Label::internal(name), statements_gen),
24-
}
24+
})
2525
}
2626

2727
// pub fn add(&mut self, statement: Statement) {

src/mango/towasm/numeric/arithmetic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ pub struct Add {
1111
}
1212

1313
impl Add {
14-
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
14+
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Box<Self> {
1515
assert!(left.typ() == right.typ());
16-
Add {
16+
Box::new(Add {
1717
left: left,
1818
right: right,
19-
}
19+
})
2020
}
2121

2222
pub fn typ(&self) -> &Type {
@@ -52,12 +52,12 @@ pub struct Mul {
5252
}
5353

5454
impl Mul {
55-
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
55+
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Box<Self> {
5656
assert!(left.typ() == right.typ());
57-
Mul {
57+
Box::new(Mul {
5858
left: left,
5959
right: right,
60-
}
60+
})
6161
}
6262

6363
pub fn typ(&self) -> &Type {

src/mango/towasm/numeric/logic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ pub struct Gt {
1111
}
1212

1313
impl Gt {
14-
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
14+
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Box<Self> {
1515
assert!(left.typ() == right.typ());
16-
Gt {
16+
Box::new(Gt {
1717
left: left,
1818
right: right,
19-
}
19+
})
2020
}
2121
}
2222

@@ -48,12 +48,12 @@ pub struct Lt {
4848
}
4949

5050
impl Lt {
51-
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Self {
51+
pub fn new(left: Box<Expression>, right: Box<Expression>) -> Box<Self> {
5252
assert!(left.typ() == right.typ());
53-
Lt {
53+
Box::new(Lt {
5454
left: left,
5555
right: right,
56-
}
56+
})
5757
}
5858

5959
pub fn typ(&self) -> &Type {

src/mango/towasm/scope/function.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ use std::io;
1111
use std::io::Write;
1212

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

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

2425
pub fn name(&self) -> &Name {
@@ -49,8 +50,8 @@ pub struct Output {
4950
}
5051

5152
impl Output {
52-
pub fn new(typ: Type) -> Self {
53-
Output { typ }
53+
pub fn new(typ: Type) -> Box<Self> {
54+
Box::new(Output { typ })
5455
}
5556
}
5657

@@ -66,12 +67,12 @@ impl Wasm for Output {
6667

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

7374
impl FunctionSignature {
74-
pub fn new(name: Name, parameters: Vec<Parameter>, results: Vec<Output>) -> Self {
75+
pub fn new(name: Name, parameters: Vec<Box<Parameter>>, results: Vec<Box<Output>>) -> Self {
7576
assert!(results.len() <= 1); //
7677
FunctionSignature {
7778
name,
@@ -114,18 +115,18 @@ impl Function {
114115
// This uses group, so it has a label, but this isn't final... It might be useless.
115116
pub fn new(
116117
name: Name,
117-
parameters: Vec<Parameter>,
118-
results: Vec<Output>,
118+
parameters: Vec<Box<Parameter>>,
119+
results: Vec<Box<Output>>,
119120
statements_gen: &Fn(Label) -> Vec<Box<Statement>>,
120-
) -> Self {
121-
Function {
121+
) -> Box<Self> {
122+
Box::new(Function {
122123
signature: FunctionSignature {
123124
name: name.clone(),
124125
parameters,
125126
results,
126127
},
127128
body: Group::new(Label::internal(name), statements_gen),
128-
}
129+
})
129130
}
130131
}
131132

src/mango/towasm/scope/module.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::io;
55
use std::io::Write;
66

77
pub struct Module {
8-
functions: Vec<Function>,
8+
functions: Vec<Box<Function>>,
99
}
1010

1111
impl Module {
12-
pub fn new(functions: Vec<Function>) -> Self {
13-
Module { functions }
12+
pub fn new(functions: Vec<Box<Function>>) -> Box<Self> {
13+
Box::new(Module { functions })
1414
}
1515
}
1616

@@ -21,11 +21,9 @@ impl Wasm for Module {
2121
self.functions
2222
.iter()
2323
.map(|func| func.as_wat())
24-
// .collect()
2524
.collect::<Vec<_>>()
2625
.join("\n")
2726
)
28-
// format!(" add ")
2927
}
3028

3129
fn write_wasm(&self, file: &mut File) -> io::Result<()> {

src/mango/towasm/tests.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use mango::towasm::util::Name;
1616
use mango::towasm::values::Assign;
1717
use mango::towasm::values::Const;
1818
use mango::towasm::values::DeclareLocal;
19-
use mango::towasm::values::Expression;
2019

2120
#[test]
2221
fn test_example_1() {
@@ -37,37 +36,37 @@ fn test_example_1() {
3736
vec![
3837
// Function body
3938
// todo: get rid of clones
40-
Box::new(fac_result_decl.clone()),
41-
Box::new(loop_condition_decl.clone()),
42-
Box::new(Assign::new(
39+
fac_result_decl.clone(),
40+
loop_condition_decl.clone(),
41+
Assign::new(
4342
fac_result.clone(),
44-
Box::new(Const::new(Type::Int32, Value::Int(1))),
45-
)),
43+
Const::new(Type::Int32, Value::Int(1)),
44+
),
4645
// Statement::Block(Block::new_named("".to_owned(), vec![])),
47-
Box::new(Loop::new_named(loop_name.clone(), &|loop_label: Label| {
46+
Loop::new_named(loop_name.clone(), &|loop_label: Label| {
4847
vec![
49-
Box::new(Assign::new(
48+
Assign::new(
5049
fac_result.clone(),
51-
Box::new(Mul::new(fac_result.get(), var_n.get())),
52-
)),
53-
Box::new(Assign::new(
50+
Mul::new(fac_result.get(), var_n.get()),
51+
),
52+
Assign::new(
5453
loop_condition.clone(),
55-
Box::new(Gt::new(
54+
Gt::new(
5655
var_n.get(),
57-
Box::new(Const::new(Type::Int32, Value::Int(2))),
58-
)),
59-
)),
60-
Box::new(Assign::new(
56+
Const::new(Type::Int32, Value::Int(2)),
57+
),
58+
),
59+
Assign::new(
6160
var_n.clone(),
62-
Box::new(Add::new(
61+
Add::new(
6362
var_n.get(),
64-
Box::new(Const::new(Type::Int32, Value::Int(-1))),
65-
)),
66-
)),
67-
Box::new(BranchIf::new(loop_condition.get(), loop_label)),
63+
Const::new(Type::Int32, Value::Int(-1)),
64+
),
65+
),
66+
BranchIf::new(loop_condition.get(), loop_label),
6867
]
69-
})),
70-
Box::new(Return::new(func_label, fac_result.get())),
68+
}),
69+
Return::new(func_label, fac_result.get()),
7170
]
7271
},
7372
)]);

src/mango/towasm/values/assign.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ use mango::towasm::Wasm;
55
use std::fs::File;
66
use std::io;
77

8-
#[derive(new)]
98
pub struct Assign {
109
assignee: Local, // todo
1110
value: Box<Expression>,
1211
}
1312

13+
impl Assign {
14+
pub fn new(assignee: Local, value: Box<Expression>) -> Box<Self> {
15+
Box::new(Assign { assignee, value })
16+
}
17+
}
18+
1419
impl Wasm for Assign {
1520
fn as_wat(&self) -> String {
1621
format!(

src/mango/towasm/values/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub struct Const {
1111
}
1212

1313
impl Const {
14-
pub fn new(typ: Type, value: Value) -> Self {
14+
pub fn new(typ: Type, value: Value) -> Box<Self> {
1515
assert!(value.is_type(&typ));
16-
Const { typ, value }
16+
Box::new(Const { typ, value })
1717
}
1818
}
1919

src/mango/towasm/values/localvar.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use mango::towasm::collect::Statement;
22
use mango::towasm::collect::Type;
33
use mango::towasm::util::Name;
4-
use mango::towasm::values::Assign;
54
use mango::towasm::values::Expression;
65
use mango::towasm::Wasm;
76
use std::fs::File;
@@ -13,7 +12,11 @@ pub struct DeclareLocal {
1312
}
1413

1514
impl DeclareLocal {
16-
pub fn new(name: Name, typ: Type) -> Self {
15+
pub fn new(name: Name, typ: Type) -> Box<Self> {
16+
Box::new(DeclareLocal::new_unboxed(name, typ))
17+
}
18+
19+
pub fn new_unboxed(name: Name, typ: Type) -> Self {
1720
DeclareLocal {
1821
local: Local { name, typ },
1922
}
@@ -61,10 +64,6 @@ impl Local {
6164
local: self.clone(),
6265
})
6366
}
64-
65-
pub fn set(&self, expression: Box<Expression>) -> Assign {
66-
Assign::new(self.clone(), expression)
67-
}
6867
}
6968

7069
impl Wasm for Local {

0 commit comments

Comments
 (0)