Skip to content

Commit 0276044

Browse files
committed
Working TACKY IR construction and test
1 parent 2ee1965 commit 0276044

1 file changed

Lines changed: 51 additions & 33 deletions

File tree

rust_src/tacky.rs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ pub struct Function {
1111
pub body: Vec<Instruction>,
1212
}
1313

14+
#[derive(Debug, Clone, PartialEq)]
1415
pub enum Value {
1516
IntConstant(i32),
1617
Var(Identifier),
1718
}
1819

20+
#[derive(Debug, Clone, PartialEq)]
1921
pub enum Instruction {
2022
Return(Value),
2123
UnaryMinus {
@@ -61,55 +63,71 @@ mod tests {
6163
let var0 = tacky::Value::Var(Identifier::new("tmp0"));
6264
let var1 = tacky::Value::Var(Identifier::new("tmp1"));
6365
let c0 = tacky::Value::IntConstant(37);
64-
let comp = tacky::Instruction::Complement { src: c0, dst: var0 };
65-
let uminus = tacky::Instruction::UnaryMinus { src: c0, dst: var1 };
66-
let ret = tacky::Instruction::Return(var1);
66+
let comp = tacky::Instruction::Complement {
67+
src: c0.clone(),
68+
dst: var0.clone(),
69+
};
70+
let uminus = tacky::Instruction::UnaryMinus {
71+
src: c0.clone(),
72+
dst: var1.clone(),
73+
};
74+
let ret = tacky::Instruction::Return(var1.clone());
6775
let func = tacky::Function {
6876
name: Identifier::new("main"),
69-
body: vec![comp, uminus, ret],
77+
body: vec![comp.clone(), uminus.clone(), ret.clone()],
7078
};
79+
assert_eq!(func.name, "main");
80+
assert_eq!(func.body.len(), 3);
7181
let prog = tacky::Program { function: func };
72-
let div_op = tacky::Instruction::BinaryOp {
82+
assert_eq!(prog.function.name, "main");
83+
let _div_op = tacky::Instruction::BinaryOp {
7384
op: ast::BinaryOp::Divide,
74-
lhs: c0,
75-
rhs: var0,
76-
dst: var1,
85+
lhs: c0.clone(),
86+
rhs: var0.clone(),
87+
dst: var1.clone(),
7788
};
78-
let plus_op = tacky::Instruction::BinaryOp {
89+
let _plus_op = tacky::Instruction::BinaryOp {
7990
op: ast::BinaryOp::Plus,
80-
lhs: c0,
81-
rhs: var0,
82-
dst: var1,
91+
lhs: c0.clone(),
92+
rhs: var0.clone(),
93+
dst: var1.clone(),
8394
};
84-
let minus_op = tacky::Instruction::BinaryOp {
95+
let _minus_op = tacky::Instruction::BinaryOp {
8596
op: ast::BinaryOp::Subtract,
86-
lhs: c0,
87-
rhs: var0,
88-
dst: var1,
97+
lhs: c0.clone(),
98+
rhs: var0.clone(),
99+
dst: var1.clone(),
89100
};
90-
let times_op = tacky::Instruction::BinaryOp {
101+
let _times_op = tacky::Instruction::BinaryOp {
91102
op: ast::BinaryOp::Multiply,
92-
lhs: c0,
93-
rhs: var0,
94-
dst: var1,
103+
lhs: c0.clone(),
104+
rhs: var0.clone(),
105+
dst: var1.clone(),
95106
};
96-
let remainder_op = tacky::Instruction::BinaryOp {
107+
let _remainder_op = tacky::Instruction::BinaryOp {
97108
op: ast::BinaryOp::Remainder,
98-
lhs: c0,
99-
rhs: var0,
100-
dst: var1,
109+
lhs: c0.clone(),
110+
rhs: var0.clone(),
111+
dst: var1.clone(),
112+
};
113+
let _not = tacky::Instruction::LogicalNot {
114+
src: c0.clone(),
115+
dst: var0.clone(),
101116
};
102-
let not = tacky::Instruction::LogicalNot { src: c0, dst: var0 };
103117
let label_name = Identifier::new("label1");
104-
let label = tacky::Instruction::Label(label_name);
105-
let jmp = tacky::Instruction::Jump(label_name);
106-
let j0 = tacky::Instruction::JumpIfZero {
107-
cond: var1,
108-
target: label_name,
118+
let _label = tacky::Instruction::Label(label_name.clone());
119+
let _jmp = tacky::Instruction::Jump(label_name.clone());
120+
let _j0 = tacky::Instruction::JumpIfZero {
121+
cond: var1.clone(),
122+
target: label_name.clone(),
123+
};
124+
let _jn0 = tacky::Instruction::JumpIfNotZero {
125+
cond: c0.clone(),
126+
target: label_name.clone(),
109127
};
110-
let jn0 = tacky::Instruction::JumpIfNotZero {
111-
cond: c0,
112-
target: label_name,
128+
let _cpy = tacky::Instruction::Copy {
129+
src: c0.clone(),
130+
dst: var0.clone(),
113131
};
114132
}
115133
}

0 commit comments

Comments
 (0)