Skip to content

Commit 2780b8c

Browse files
committed
Allow identifiers to contain digits after the first character
This technically goes beyond what's specified in the book, but it appears to be common practice in publicly available Jack codebases, so it's worth supporting.
1 parent d0645ac commit 2780b8c

4 files changed

Lines changed: 21 additions & 19 deletions

File tree

src/jack/tokenizer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ impl<'a> Tokenizer<'a> {
208208
}
209209

210210
fn read_word(&mut self, first: char) -> Option<String> {
211-
self.read_while(first, is_identifier)
211+
// Identifiers may contain digits, as long as they're not the first
212+
// character in the word.
213+
self.read_while(first, |ch| is_identifier(ch) || ch.is_ascii_digit())
212214
}
213215

214216
fn read_int_const(&mut self, first: char) -> Option<u16> {

tests/parser_test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn array_test_jack_test() {
2626
},
2727
LocalVars {
2828
typ: VarType::Int,
29-
names: vec!["length".into()],
29+
names: vec!["l3ngth".into()],
3030
},
3131
LocalVars {
3232
typ: VarType::Int,
@@ -35,7 +35,7 @@ fn array_test_jack_test() {
3535
],
3636
statements: vec![
3737
Statement::Let {
38-
lhs: "length".into(),
38+
lhs: "l3ngth".into(),
3939
index: None,
4040
rhs: Expr::Term(Term::SubroutineCall(SubroutineCall {
4141
receiver: Some("Keyboard".into()),
@@ -49,7 +49,7 @@ fn array_test_jack_test() {
4949
rhs: Expr::Term(Term::SubroutineCall(SubroutineCall {
5050
receiver: Some("Array".into()),
5151
subroutine: "new".into(),
52-
args: vec![Expr::Term(Term::Var("length".into()))],
52+
args: vec![Expr::Term(Term::Var("l3ngth".into()))],
5353
})),
5454
},
5555
Statement::Let {
@@ -61,7 +61,7 @@ fn array_test_jack_test() {
6161
condition: Expr::Binary(
6262
BinaryOp::LessThan,
6363
Term::Var("i".into()),
64-
Box::new(Expr::Term(Term::Var("length".into())))
64+
Box::new(Expr::Term(Term::Var("l3ngth".into())))
6565
),
6666
body: vec![
6767
Statement::Let {
@@ -100,7 +100,7 @@ fn array_test_jack_test() {
100100
condition: Expr::Binary(
101101
BinaryOp::LessThan,
102102
Term::Var("i".into()),
103-
Box::new(Expr::Term(Term::Var("length".into())))
103+
Box::new(Expr::Term(Term::Var("l3ngth".into())))
104104
),
105105
body: vec![
106106
Statement::Let {
@@ -137,7 +137,7 @@ fn array_test_jack_test() {
137137
args: vec![Expr::Binary(
138138
BinaryOp::Divide,
139139
Term::Var("sum".into()),
140-
Box::new(Expr::Term(Term::Var("length".into())))
140+
Box::new(Expr::Term(Term::Var("l3ngth".into())))
141141
)],
142142
}),
143143
Statement::Do(SubroutineCall {

tests/testdata/ArrayTest.jack

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99
class Main {
1010
function void main() {
1111
var Array a;
12-
var int length;
12+
var int l3ngth;
1313
var int i, sum;
1414

15-
let length = Keyboard.readInt("HOW MANY NUMBERS? ");
16-
let a = Array.new(length);
15+
let l3ngth = Keyboard.readInt("HOW MANY NUMBERS? ");
16+
let a = Array.new(l3ngth);
1717
let i = 0;
1818

19-
while (i < length) {
19+
while (i < l3ngth) {
2020
let a[i] = Keyboard.readInt("ENTER THE NEXT NUMBER: ");
2121
let i = i + 1;
2222
}
2323

2424
let i = 0;
2525
let sum = 0;
2626

27-
while (i < length) {
27+
while (i < l3ngth) {
2828
let sum = sum + a[i];
2929
let i = i + 1;
3030
}
3131

3232
do Output.printString("THE AVERAGE IS: ");
33-
do Output.printInt(sum / length);
33+
do Output.printInt(sum / l3ngth);
3434
do Output.println();
3535

3636
return;

tests/tokenizer_test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn array_test_jack_test() {
2525
Token::Symbol(';'),
2626
Token::Keyword(Keyword::Var),
2727
Token::Keyword(Keyword::Int),
28-
Token::Identifier("length".to_string()),
28+
Token::Identifier("l3ngth".to_string()),
2929
Token::Symbol(';'),
3030
Token::Keyword(Keyword::Var),
3131
Token::Keyword(Keyword::Int),
@@ -34,7 +34,7 @@ fn array_test_jack_test() {
3434
Token::Identifier("sum".to_string()),
3535
Token::Symbol(';'),
3636
Token::Keyword(Keyword::Let),
37-
Token::Identifier("length".to_string()),
37+
Token::Identifier("l3ngth".to_string()),
3838
Token::Symbol('='),
3939
Token::Identifier("Keyboard".to_string()),
4040
Token::Symbol('.'),
@@ -50,7 +50,7 @@ fn array_test_jack_test() {
5050
Token::Symbol('.'),
5151
Token::Identifier("new".to_string()),
5252
Token::Symbol('('),
53-
Token::Identifier("length".to_string()),
53+
Token::Identifier("l3ngth".to_string()),
5454
Token::Symbol(')'),
5555
Token::Symbol(';'),
5656
Token::Keyword(Keyword::Let),
@@ -62,7 +62,7 @@ fn array_test_jack_test() {
6262
Token::Symbol('('),
6363
Token::Identifier("i".to_string()),
6464
Token::Symbol('<'),
65-
Token::Identifier("length".to_string()),
65+
Token::Identifier("l3ngth".to_string()),
6666
Token::Symbol(')'),
6767
Token::Symbol('{'),
6868
Token::Keyword(Keyword::Let),
@@ -100,7 +100,7 @@ fn array_test_jack_test() {
100100
Token::Symbol('('),
101101
Token::Identifier("i".to_string()),
102102
Token::Symbol('<'),
103-
Token::Identifier("length".to_string()),
103+
Token::Identifier("l3ngth".to_string()),
104104
Token::Symbol(')'),
105105
Token::Symbol('{'),
106106
Token::Keyword(Keyword::Let),
@@ -136,7 +136,7 @@ fn array_test_jack_test() {
136136
Token::Symbol('('),
137137
Token::Identifier("sum".to_string()),
138138
Token::Symbol('/'),
139-
Token::Identifier("length".to_string()),
139+
Token::Identifier("l3ngth".to_string()),
140140
Token::Symbol(')'),
141141
Token::Symbol(';'),
142142
Token::Keyword(Keyword::Do),

0 commit comments

Comments
 (0)