-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.test.js
185 lines (173 loc) · 4.59 KB
/
interpreter.test.js
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
const { Interpreter } = require('./interpreter')
const Tokenizer = require('./tokenizer')
const Parser = require('./parser')
const parseExpression = code => {
const tokenizer = new Tokenizer(code)
const tokens = tokenizer.scanTokens()
const parser = new Parser(tokens)
return parser.expression()
}
const parseStatements = code => {
const tokenizer = new Tokenizer(code)
const tokens = tokenizer.scanTokens()
const parser = new Parser(tokens)
return parser.parse()
}
describe('Interpreter', () => {
describe('Expressions', () => {
let interpreter
beforeEach(() => {
interpreter = new Interpreter()
})
test('interprets literals and simple groupings correctly', () => {
const literalMap = {
// Numbers
'3': 3,
'321': 321,
'.3': 0.3,
'.321': 0.321,
'(321.123)': 321.123,
// Strings
'""': '',
'"hello"': 'hello',
[`"
<strong>
Independent Woman
</strong>
"`]: `
<strong>
Independent Woman
</strong>
`,
// True
true: true,
'(true)': true,
// False
false: false,
// Nil
'(nil)': null
}
for (let literal in literalMap) {
const value = literalMap[literal]
const interpreter = new Interpreter()
expect(interpreter.interpret(parseExpression(literal))).toBe(value)
}
})
test('interprets unaries correctly', () => {
const unaryMap = {
'-23': -23,
'-.23': -0.23,
'-0.52': -0.52,
'!true': false,
'!false': true
}
for (let unary in unaryMap) {
const value = unaryMap[unary]
expect(interpreter.interpret(parseExpression(unary))).toBe(value)
}
})
test('interprets math correctly', () => {
const math = [
// Addition
'1 + 1',
'9999999999999 + 1',
'1 + -20',
'-20 + 5',
'1.22 + -1.33',
'1.11 + -4.23212321',
'213.213 - 12314.123123',
'1.22 + -1.33',
'1.11 + -4.23212321',
'213.213 + -12314.123123',
// Subtraction
'4 - 2',
'1231 - 123123',
'21312.123123 - 123123.123123',
'.12312 - .12323',
// Multiplication
'1 * 1',
'10 * 0',
'9999 * 123812 ',
'-10 * -20',
'9999 * -1',
'-12 * -23',
// Division
'100 / 0',
'100 / 10',
'3.33 / 1.11',
'21312.312312 / 123.21312',
// Groupings
'(5 - (3 - 1)) + -1',
'(5 - ((3/1) * 23)) + (12 * (2 + 1))'
]
for (let eq of math) {
expect(interpreter.interpret(parseExpression(eq))).toBe(eval(eq))
}
})
test('interprets comparisons correctly', () => {
const comparisons = [
'3 > 2',
'3 >= 2',
'3 >= 3',
'2 >= 10',
'2 < 3',
'3 < 2',
'4 <= 2',
'2 <= 4',
'3 == 2',
'2 == 2',
'3 != 2',
'2 != 2'
]
for (let comp of comparisons) {
expect(interpreter.interpret(parseExpression(comp))).toBe(eval(comp))
}
})
test('catches basic binary type errors', () => {
const comparisons = [['2', '"hello"'], ['false', '"hello"']]
const operators = ['-', '*', '/', '>', '>=', '<', '<=']
for (let operator of operators) {
for (let comparison of comparisons) {
const [left, right] = comparison
const stmt1 = left + operator + right
const stmt2 = right + operator + left
expect(() => {
interpreter.interpret(parseExpression(stmt1))
}).toThrow('Operand must be a number!')
expect(() => {
interpreter.interpret(parseExpression(stmt2))
}).toThrow('Operand must be a number!')
}
}
})
test('handles variables and lexical scoping', () => {
const long = `
var a = "global a";
var b = "global b";
var c = "global c";
{
var a = "outer a";
var b = "outer b";
{
var a = "inner a";
print a;
print b;
print c;
}
print a;
print b;
print c;
}
print a;
print b;
print c;
`
const statements = parseStatements(long)
const values = []
for (let stmt of statements) {
values.push(interpreter.interpret(stmt))
}
expect(values).toEqual([null, null, null, null, 'global a', 'global b', 'global c'])
})
})
})