-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt.rs
194 lines (174 loc) · 6.37 KB
/
stmt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use pest::iterators::Pair;
use super::{Arexpr, Cond, Expr};
use crate::{
error::SyntaxError,
generate::Generate,
graphviz::{Node, ToNode},
optimize::Optimize,
parser::Rule,
symbol_table::{SymbolTable, Type},
syntax_tree::COND_COUNT,
};
#[derive(Debug)]
pub enum Stmt {
End,
Goto(u32),
If {
cond: Cond,
then: Box<Stmt>,
els: Option<Box<Stmt>>,
},
Input(String),
Let(String, Arexpr),
Print(Expr),
/// This is mainly useful for optimization purposes
NoOp,
}
impl Stmt {
pub fn from_pair(
value: Pair<'_, Rule>,
symbol_table: &mut SymbolTable,
) -> Result<Self, SyntaxError> {
let mut pairs = value.into_inner();
let stmt = pairs.next().unwrap();
Ok(match stmt.as_rule() {
Rule::end | Rule::r#return => Stmt::End,
Rule::goto | Rule::gosub => {
let line = pairs.next().unwrap();
Stmt::Goto(line.as_str().parse()?)
}
Rule::r#if => Stmt::If {
cond: pairs.next().unwrap().into(),
then: Box::new(Self::from_pair(pairs.next().unwrap(), symbol_table)?),
els: {
if pairs.next().is_some() {
Some(Box::new(Self::from_pair(
pairs.next().unwrap(),
symbol_table,
)?))
} else {
None
}
},
},
Rule::input => {
let ident = pairs.next().unwrap().as_str();
symbol_table.insert(ident, Type::Int);
Stmt::Input(ident.into())
}
Rule::r#let => {
let ident = pairs.next().unwrap();
let expr = pairs.next().unwrap();
let ident = ident.as_str().trim().to_string();
let expr = Arexpr::from_pair(expr, symbol_table);
symbol_table.insert(&ident, Type::Int);
Stmt::Let(ident, expr)
}
Rule::print => Stmt::Print(Expr::from_pair(pairs.next().unwrap(), symbol_table)),
rule => unimplemented!("Unknown statement {:?}", rule),
})
}
}
impl ToNode for Stmt {
fn to_node(&self) -> Node {
match self {
Stmt::End => Node::new("end"),
Stmt::Goto(line) => Node::new("goto").add(line.to_node()),
Stmt::If { cond, then, els } => {
let node = Node::new("if").add(cond.to_node()).add(then.to_node());
if let Some(els) = els {
node.add(els.to_node())
} else {
node
}
}
Stmt::Input(ident) => Node::new("input").add(ident.to_node()),
Stmt::Let(ident, value) => Node::new("let").add(ident.to_node()).add(value.to_node()),
Stmt::Print(expr) => Node::new("print").add(expr.to_node()),
Stmt::NoOp => Node::new(""),
}
}
}
impl Generate for Stmt {
fn generate(
&self,
program: tiny_elf::asm::Program,
symbol_table: &mut SymbolTable,
) -> tiny_elf::asm::Program {
use tiny_elf::asm::{Memory, Mnemonic::*, Register::*};
static mut LITERAL_COUNT: usize = 0;
match self {
Stmt::End => program.add(Jmp("exit".into())),
Stmt::Goto(line) => program.add(Jmp(Memory::from(format!("line{line}")))),
Stmt::If { cond, then, els } => {
let mut program = cond.generate(program, symbol_table);
let count = unsafe { COND_COUNT };
let endif_label = format!("fi{}", count);
// else
if let Some(els) = els {
program = program.label(&format!("else{}", count));
program = els
.generate(program, symbol_table)
.add(Jmp(endif_label.clone().into()));
} else {
program = program.add(Jmp(endif_label.clone().into()))
}
// then
program = program.label(&format!("then{}", count));
program = then.generate(program, symbol_table);
program.label(&endif_label)
}
Stmt::Input(ident) => {
let end_addr = symbol_table.get(ident).unwrap().end_addr() as i32;
let program = program
.add(Mov(Rsi, R15.into()))
.add(Sub(Rsi, end_addr.into()))
.add(Mov(Rdx, 8.into()))
.add(Call("read".into()));
let program = symbol_table.access(ident, program).add(Sub(Rbx, 48.into()));
symbol_table.write(ident, Rbx.into(), program)
}
Stmt::Let(ident, arexpr) => {
let program = arexpr.generate(program, symbol_table).add(Pop(Rbx));
symbol_table.write(ident, Rbx.into(), program)
}
Stmt::Print(Expr::String(str)) => {
let str = str.replace(r"\n", "\n").replace(r"\t", "\t");
let label = format!("literal{}", unsafe {
LITERAL_COUNT += 1;
LITERAL_COUNT
});
program
.add(Mov(Rsi, Memory::from(label.as_str()).into()))
.add(Mov(Rdx, (str.len() as i32).into()))
.add(Call("print".into()))
.insert_data(&label, &str)
}
Stmt::Print(Expr::Arexpr(arexpr)) => arexpr
.generate(program, symbol_table)
.add(Pop(Rsi))
.add(Call("printn".into())),
Stmt::NoOp => program,
}
}
}
impl Optimize for Stmt {
fn optimize(self) -> Self {
match self {
Stmt::Print(expr) => Stmt::Print(expr.optimize()),
Stmt::Let(ident, expr) => Stmt::Let(ident, expr.optimize()),
Stmt::If { cond, then, els } => {
if let Ok(b) = cond.try_execute() {
if b {
*then
} else {
els.map_or(Stmt::NoOp, |s| *s)
}
} else {
Stmt::If { cond, then, els }
}
}
_ => self,
}
}
}