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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::{iter::Peekable, slice::Iter};
use nand2tetris_vmtranslator_rust::data::{ArithmeticCommand, Segment};
use crate::{
compiler::{
ProgramCompilerUnit,
data::{ArithmeticOperator, KeywordConst},
},
data::{Identifier, IntegerConstant, KeywordType, SymbolType},
error::{CompilerError, CompilerResult},
token::Token,
};
/// Expression compiler unit
pub(super) struct ExpressionCompilerUnit;
impl ExpressionCompilerUnit {
/// The compiler flow unit of expression list
pub(super) fn compile_expression_list(
compiler: &mut ProgramCompilerUnit,
tokens: &mut Peekable<Iter<'_, Token>>,
) -> CompilerResult<u16> {
let mut num_args: u16 = 0;
if tokens.peek() != Some(&&Token::Symbol(SymbolType::CloseParenthesis)) {
Self::compile_expression(compiler, tokens)?;
num_args += 1;
while let Some(Token::Symbol(SymbolType::Comma)) = tokens.peek() {
// skip ,
let _ = tokens.next();
Self::compile_expression(compiler, tokens)?;
num_args += 1;
}
}
Ok(num_args)
}
/// The compiler flow unit of expression
pub(super) fn compile_expression(
compiler: &mut ProgramCompilerUnit,
tokens: &mut Peekable<Iter<'_, Token>>,
) -> CompilerResult<()> {
Self::compile_term(compiler, tokens)?;
while let Some(token) = tokens.peek() {
if let Ok(op) = Self::flow_unit_op(token) {
// skip op
let _ = tokens.next();
Self::compile_term(compiler, tokens)?;
compiler.vm_writer.write_arithmetic_operator(op);
} else {
break;
}
}
Ok(())
}
/// The compiler flow unit of subroutine call
///
/// # arguments
///
/// * tokens the compiler tokens
/// * first_identifier 因term需要判断是否varname还是subroutineCall,前序已经提取了第一个token
pub(super) fn compile_subroutine_call(
compiler: &mut ProgramCompilerUnit,
tokens: &mut Peekable<Iter<'_, Token>>,
first_identifier: Option<Identifier>,
) -> CompilerResult<()> {
let first_identifier = if let Some(first_identifier) = first_identifier {
first_identifier
} else {
Self::flow_unit_identifier(
tokens
.next()
.ok_or_else(|| CompilerError::SubroutineCall("miss subroutineName".into()))?,
)?
};
match tokens.next() {
Some(Token::Symbol(SymbolType::OpenParenthesis)) => {
// subroutineName(expressionList)
// equal to this.subroutineName(expressionList)
let num_args = 1 + Self::compile_expression_list(compiler, tokens)?;
let _ = crate::analyzer::Analyzer::match_symbol(
tokens
.next()
.ok_or_else(|| CompilerError::SubroutineCall("miss )".into()))?,
SymbolType::CloseParenthesis,
)?;
// push this to arguments
compiler.vm_writer.write_push(Segment::This, 0);
// generate code for subroutine call
compiler.vm_writer.write_call(
compiler.general_subroutine_name(&first_identifier),
num_args,
);
}
Some(Token::Symbol(SymbolType::Dot)) => {
//className|varName.subroutineName(expressionList)
if let Ok(subroutine_name) =
Self::flow_unit_identifier(tokens.next().ok_or_else(|| {
CompilerError::SubroutineCall("miss subroutineName".into())
})?)
{
let _ = crate::analyzer::Analyzer::match_symbol(
tokens
.next()
.ok_or_else(|| CompilerError::SubroutineCall("miss (".into()))?,
SymbolType::OpenParenthesis,
)?;
let (called_subroutine_name, mut num_args) = if let Some(var_name) =
compiler.get_variable(first_identifier.as_str())
{
// It's a method call on a variable
// varName.subroutineName(expressionList)
// push the variable to the stack as this
let subroutine_name = format!(
"{}.{}",
var_name.get_class_name().unwrap_or("this"),
subroutine_name.as_str()
);
// push the variable to the stack as this
compiler
.vm_writer
.write_push(var_name.var_kind.into(), var_name.index_of_kind);
(subroutine_name, 1)
} else {
let subroutine_name =
format!("{}.{}", first_identifier.as_str(), subroutine_name.as_str());
(subroutine_name, 0)
};
num_args += Self::compile_expression_list(compiler, tokens)?;
let _ = crate::analyzer::Analyzer::match_symbol(
tokens
.next()
.ok_or_else(|| CompilerError::SubroutineCall("miss )".into()))?,
SymbolType::CloseParenthesis,
)?;
// generate code for subroutine call
compiler
.vm_writer
.write_call(called_subroutine_name, num_args);
} else {
return Err(CompilerError::SubroutineCall("miss subroutine name".into()));
}
}
_ => return Err(CompilerError::SubroutineCall("miss ( or .".into())),
}
Ok(())
}
/// The compiler flow unit of term
fn compile_term(
compiler: &mut ProgramCompilerUnit,
tokens: &mut Peekable<Iter<'_, Token>>,
) -> CompilerResult<()> {
let token = tokens
.peek()
.cloned()
.ok_or_else(|| CompilerError::Term("no token".into()))?;
if let Ok(IntegerConstant(value)) = Self::flow_unit_int_const(token) {
// case 1 : integer const
let _ = tokens.next();
compiler.vm_writer.write_push(Segment::Constant, value);
} else if let Ok(keyword_const) = Self::flow_unit_keyword_const(token) {
// case 2 : true false null this
// skip the first peek
let _ = tokens.next();
compiler.vm_writer.write_keyword_const(keyword_const);
} else if let Ok(string) = Self::flow_unit_string_const(token) {
// case 3 : string const
// skip the first peek
let _ = tokens.next();
compiler.vm_writer.write_string_const(string);
} else if let Token::Symbol(SymbolType::OpenParenthesis) = token {
// case 4 : (expression)
// skip (
let _ = tokens.next();
Self::compile_expression(compiler, tokens)?;
let _ = crate::analyzer::Analyzer::match_symbol(
tokens
.next()
.ok_or_else(|| CompilerError::Term("miss )".into()))?,
SymbolType::CloseParenthesis,
)?;
} else if let Ok(unary_opr) = Self::flow_unit_unary_op(token) {
// case 5 : unary operator
// skip unary_opr
let _ = tokens.next();
// unaryOp term
if unary_opr == ArithmeticOperator::Minus {
// convert -1 to 0 - 1
compiler.vm_writer.write_push(Segment::Constant, 0);
}
// get the term value,it will push a result on the stack top
Self::compile_term(compiler, tokens)?;
// do the opr
compiler.vm_writer.write_arithmetic_operator(unary_opr);
} else if let Ok(var_name) = Self::flow_unit_identifier(token) {
// case 6 : array
// skip varName
let _ = tokens.next();
// maybe varName | subroutineCall
match tokens.peek() {
Some(Token::Symbol(SymbolType::OpenBracket)) => {
// varname[expression]
// arr[expression]
let var_name = compiler
.get_variable(var_name.as_str())
.ok_or_else(|| {
CompilerError::Indentifier(format!(
"can'nt found the variable : {}",
var_name.as_str()
))
})
.map(|item| item.to_owned())?;
// skip [
let _ = tokens.next();
// step 1 : push the expression value to the top of stack
Self::compile_expression(compiler, tokens)?;
// step 2 : push arr
compiler
.vm_writer
.write_push(var_name.var_kind.into(), var_name.index_of_kind);
// arr + expression
// write add
compiler
.vm_writer
.write_arithmetic_command(ArithmeticCommand::Add);
// set temp_0 = arr+expression
compiler.vm_writer.write_pop(Segment::Pointer, 1);
// read the temp_0's value to the top of stack
compiler.vm_writer.write_push(Segment::That, 0);
// ]
let _ = crate::analyzer::Analyzer::match_symbol(
tokens
.next()
.ok_or_else(|| CompilerError::Term("miss ]".into()))?,
SymbolType::CloseBracket,
)?;
}
Some(Token::Symbol(symbol))
if [SymbolType::OpenParenthesis, SymbolType::Dot].contains(symbol) =>
{
// subroutine call
Self::compile_subroutine_call(compiler, tokens, Some(var_name))?;
}
_ => {
let var_name = compiler
.get_variable(var_name.as_str())
.ok_or_else(|| {
CompilerError::Indentifier(format!(
"can'nt found the variable : {}",
var_name.as_str()
))
})
.map(|item| item.to_owned())?;
// push variable value to stack
compiler
.vm_writer
.write_push(var_name.var_kind.into(), var_name.index_of_kind);
}
}
} else {
return Err(CompilerError::Term("no a term".into()));
}
Ok(())
}
/// The compiler flow unit of int const
pub(super) fn flow_unit_int_const(token: &Token) -> CompilerResult<IntegerConstant> {
if let Token::IntConst(int_const) = token {
Ok(int_const.to_owned())
} else {
Err(CompilerError::IntConst(token.to_xml()))
}
}
/// The compiler flow unit of int const
fn flow_unit_string_const(token: &Token) -> CompilerResult<String> {
if let Token::StringCost(string) = token {
Ok(string.to_owned())
} else {
Err(CompilerError::StringConst(token.to_xml()))
}
}
/// The compiler flow unit of identifier
pub(super) fn flow_unit_identifier(token: &Token) -> CompilerResult<Identifier> {
if let Token::Identifier(identifier) = token {
Ok(identifier.to_owned())
} else {
Err(CompilerError::Indentifier(token.to_xml()))
}
}
/// The compiler flow unit of keyword const
fn flow_unit_keyword_const(token: &Token) -> CompilerResult<KeywordConst> {
if let Token::Keyword(keyword) = token
&& [
KeywordType::True,
KeywordType::False,
KeywordType::Null,
KeywordType::This,
]
.contains(keyword)
{
keyword.to_owned().try_into()
} else {
Err(CompilerError::Keyword(token.to_xml()))
}
}
/// The compiler flow unit of unary opertor
fn flow_unit_unary_op(token: &Token) -> CompilerResult<ArithmeticOperator> {
if let Token::Symbol(symbol) = &token
&& [SymbolType::Minus, SymbolType::Not].contains(symbol)
{
symbol.to_owned().try_into()
} else {
Err(CompilerError::Operator("not a unary operator".to_string()))
}
}
/// The compiler flow unit of opertor : + - * / & < > =
fn flow_unit_op(token: &Token) -> CompilerResult<ArithmeticOperator> {
if let Token::Symbol(symbol) = token
&& [
SymbolType::Plus,
SymbolType::Minus,
SymbolType::Multiply,
SymbolType::Divide,
SymbolType::And,
SymbolType::Or,
SymbolType::LessThan,
SymbolType::GreaterThan,
SymbolType::Equal,
]
.contains(symbol)
{
symbol.to_owned().try_into()
} else {
Err(CompilerError::Operator("not a operator".into()))
}
}
}