loxxy 0.4.09 → 0.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +32 -304
- data/CHANGELOG.md +3 -0
- data/LICENSE.txt +1 -1
- data/lib/loxxy/ast/ast_builder.rb +1 -1
- data/lib/loxxy/back_end/engine.rb +2 -0
- data/lib/loxxy/back_end/environment.rb +1 -1
- data/lib/loxxy/front_end/scanner.rb +9 -9
- data/lib/loxxy/interpreter.rb +3 -3
- data/lib/loxxy/version.rb +1 -1
- data/loxxy.gemspec +8 -7
- data/spec/back_end/engine_spec.rb +21 -19
- data/spec/back_end/environment_spec.rb +20 -19
- data/spec/back_end/symbol_table_spec.rb +67 -67
- data/spec/back_end/variable_spec.rb +14 -13
- data/spec/datatype/boolean_spec.rb +9 -8
- data/spec/datatype/lx_string_spec.rb +16 -15
- data/spec/datatype/nil_spec.rb +5 -5
- data/spec/datatype/number_spec.rb +18 -17
- data/spec/front_end/parser_spec.rb +110 -110
- data/spec/front_end/raw_parser_spec.rb +25 -25
- data/spec/front_end/scanner_spec.rb +77 -76
- data/spec/interpreter_spec.rb +114 -118
- data/spec/loxxy_spec.rb +1 -1
- metadata +33 -27
@@ -8,7 +8,7 @@ require_relative '../../lib/loxxy/front_end/parser'
|
|
8
8
|
module Loxxy
|
9
9
|
module FrontEnd
|
10
10
|
describe Parser do
|
11
|
-
subject {
|
11
|
+
subject(:parser) { described_class.new }
|
12
12
|
|
13
13
|
# Utility method to walk towards deeply nested node
|
14
14
|
# @param aNTNode [Rley::PTree::NonTerminalNode]
|
@@ -23,12 +23,12 @@ module Loxxy
|
|
23
23
|
end
|
24
24
|
|
25
25
|
context 'Initialization:' do
|
26
|
-
it '
|
27
|
-
expect {
|
26
|
+
it 'is initialized without argument' do
|
27
|
+
expect { described_class.new }.not_to raise_error
|
28
28
|
end
|
29
29
|
|
30
|
-
it '
|
31
|
-
expect(
|
30
|
+
it 'has its parse engine initialized' do
|
31
|
+
expect(parser.engine).to be_a(Rley::Engine)
|
32
32
|
end
|
33
33
|
end # context
|
34
34
|
|
@@ -37,173 +37,173 @@ module Loxxy
|
|
37
37
|
# Parse results MUST to comply to grammar rule:
|
38
38
|
# program => declaration_star EOF
|
39
39
|
# where the declaration_star MUST be empty
|
40
|
-
expect(aParseTree.root).to
|
40
|
+
expect(aParseTree.root).to be_a(Ast::LoxNoopExpr)
|
41
41
|
end
|
42
42
|
|
43
|
-
it '
|
44
|
-
ptree =
|
43
|
+
it 'copes with an empty input' do
|
44
|
+
ptree = parser.parse('')
|
45
45
|
check_empty_input_result(ptree)
|
46
46
|
end
|
47
47
|
|
48
|
-
it '
|
49
|
-
ptree =
|
48
|
+
it 'copes with whitespaces only input' do
|
49
|
+
ptree = parser.parse((' ' * 80) + ("\n" * 20))
|
50
50
|
check_empty_input_result(ptree)
|
51
51
|
end
|
52
52
|
|
53
|
-
it '
|
53
|
+
it 'copes with comments only input' do
|
54
54
|
input = +''
|
55
55
|
%w[First Second Third].each do |ordinal|
|
56
56
|
input << "// #{ordinal} comment line\r\n"
|
57
57
|
end
|
58
|
-
ptree =
|
58
|
+
ptree = parser.parse(input)
|
59
59
|
check_empty_input_result(ptree)
|
60
60
|
end
|
61
61
|
end # context
|
62
62
|
|
63
63
|
context 'Parsing literals:' do
|
64
|
-
it '
|
64
|
+
it 'parses a false literal' do
|
65
65
|
input = 'false;'
|
66
|
-
ptree =
|
67
|
-
expect(ptree.root).to
|
66
|
+
ptree = parser.parse(input)
|
67
|
+
expect(ptree.root).to be_a(Ast::LoxSeqDecl)
|
68
68
|
leaf = ptree.root.subnodes[0]
|
69
|
-
expect(leaf).to
|
70
|
-
expect(leaf.literal).to
|
69
|
+
expect(leaf).to be_a(Ast::LoxLiteralExpr)
|
70
|
+
expect(leaf.literal).to equal(Datatype::False.instance)
|
71
71
|
end
|
72
72
|
|
73
|
-
it '
|
73
|
+
it 'parses a true literal' do
|
74
74
|
input = 'true;'
|
75
|
-
ptree =
|
76
|
-
expect(ptree.root).to
|
75
|
+
ptree = parser.parse(input)
|
76
|
+
expect(ptree.root).to be_a(Ast::LoxSeqDecl)
|
77
77
|
leaf = ptree.root.subnodes[0]
|
78
|
-
expect(leaf).to
|
79
|
-
expect(leaf.literal).to
|
78
|
+
expect(leaf).to be_a(Ast::LoxLiteralExpr)
|
79
|
+
expect(leaf.literal).to equal(Datatype::True.instance)
|
80
80
|
end
|
81
81
|
|
82
|
-
it '
|
82
|
+
it 'parses number literals' do
|
83
83
|
inputs = %w[1234; 12.34;]
|
84
84
|
inputs.each do |source|
|
85
|
-
ptree =
|
85
|
+
ptree = parser.parse(source)
|
86
86
|
leaf = ptree.root.subnodes[0]
|
87
|
-
expect(leaf).to
|
88
|
-
expect(leaf.literal).to
|
87
|
+
expect(leaf).to be_a(Ast::LoxLiteralExpr)
|
88
|
+
expect(leaf.literal).to be_a(Datatype::Number)
|
89
89
|
expect(leaf.literal.value).to eq(source.to_f)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
it '
|
93
|
+
it 'parses string literals' do
|
94
94
|
inputs = [
|
95
95
|
'"I am a string";',
|
96
96
|
'"";',
|
97
97
|
'"123";'
|
98
98
|
]
|
99
99
|
inputs.each do |source|
|
100
|
-
ptree =
|
100
|
+
ptree = parser.parse(source)
|
101
101
|
leaf = ptree.root.subnodes[0]
|
102
|
-
expect(leaf).to
|
103
|
-
expect(leaf.literal).to
|
102
|
+
expect(leaf).to be_a(Ast::LoxLiteralExpr)
|
103
|
+
expect(leaf.literal).to be_a(Datatype::LXString)
|
104
104
|
expect(leaf.literal.value).to eq(source.gsub(/(^")|(";$)/, ''))
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
it '
|
108
|
+
it 'parses a nil literal' do
|
109
109
|
input = 'nil;'
|
110
|
-
ptree =
|
110
|
+
ptree = parser.parse(input)
|
111
111
|
leaf = ptree.root.subnodes[0]
|
112
|
-
expect(leaf).to
|
113
|
-
expect(leaf.literal).to
|
112
|
+
expect(leaf).to be_a(Ast::LoxLiteralExpr)
|
113
|
+
expect(leaf.literal).to equal(Datatype::Nil.instance)
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
117
|
context 'Parsing expressions:' do
|
118
|
-
it '
|
118
|
+
it 'parses a hello world program' do
|
119
119
|
program = <<-LOX_END
|
120
120
|
// Your first Lox program!
|
121
121
|
print "Hello, world!";
|
122
122
|
LOX_END
|
123
|
-
ptree =
|
123
|
+
ptree = parser.parse(program)
|
124
124
|
prnt_stmt = ptree.root.subnodes[0]
|
125
|
-
expect(prnt_stmt).to
|
126
|
-
expect(prnt_stmt.subnodes[0]).to
|
127
|
-
expect(prnt_stmt.subnodes[0].literal).to
|
125
|
+
expect(prnt_stmt).to be_a(Ast::LoxPrintStmt)
|
126
|
+
expect(prnt_stmt.subnodes[0]).to be_a(Ast::LoxLiteralExpr)
|
127
|
+
expect(prnt_stmt.subnodes[0].literal).to be_a(Loxxy::Datatype::LXString)
|
128
128
|
expect(prnt_stmt.subnodes[0].literal.value).to eq('Hello, world!')
|
129
129
|
end
|
130
130
|
end # context
|
131
131
|
|
132
132
|
context 'Parsing arithmetic operations' do
|
133
|
-
it '
|
133
|
+
it 'parses the addition of two number literals' do
|
134
134
|
input = '123 + 456;'
|
135
|
-
ptree =
|
135
|
+
ptree = parser.parse(input)
|
136
136
|
expr = ptree.root.subnodes[0]
|
137
|
-
expect(expr).to
|
137
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
138
138
|
expect(expr.operator).to eq(:+)
|
139
139
|
expect(expr.operands[0].literal.value).to eq(123)
|
140
140
|
expect(expr.operands[1].literal.value).to eq(456)
|
141
141
|
end
|
142
142
|
|
143
|
-
it '
|
143
|
+
it 'parses the subtraction of two number literals' do
|
144
144
|
input = '4 - 3;'
|
145
|
-
ptree =
|
145
|
+
ptree = parser.parse(input)
|
146
146
|
expr = ptree.root.subnodes[0]
|
147
|
-
expect(expr).to
|
147
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
148
148
|
expect(expr.operator).to eq(:-)
|
149
149
|
expect(expr.operands[0].literal.value).to eq(4)
|
150
150
|
expect(expr.operands[1].literal.value).to eq(3)
|
151
151
|
end
|
152
152
|
|
153
|
-
it '
|
153
|
+
it 'parses multiple additive operations' do
|
154
154
|
input = '5 + 2 - 3;'
|
155
|
-
ptree =
|
155
|
+
ptree = parser.parse(input)
|
156
156
|
expr = ptree.root.subnodes[0]
|
157
|
-
expect(expr).to
|
157
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
158
158
|
expect(expr.operator).to eq(:-)
|
159
|
-
expect(expr.operands[0]).to
|
159
|
+
expect(expr.operands[0]).to be_a(Ast::LoxBinaryExpr)
|
160
160
|
expect(expr.operands[0].operator).to eq(:+)
|
161
161
|
expect(expr.operands[0].operands[0].literal.value).to eq(5)
|
162
162
|
expect(expr.operands[0].operands[1].literal.value).to eq(2)
|
163
163
|
expect(expr.operands[1].literal.value).to eq(3)
|
164
164
|
end
|
165
165
|
|
166
|
-
it '
|
166
|
+
it 'parses the division of two number literals' do
|
167
167
|
input = '8 / 2;'
|
168
|
-
ptree =
|
168
|
+
ptree = parser.parse(input)
|
169
169
|
expr = ptree.root.subnodes[0]
|
170
|
-
expect(expr).to
|
170
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
171
171
|
expect(expr.operator).to eq(:/)
|
172
172
|
expect(expr.operands[0].literal.value).to eq(8)
|
173
173
|
expect(expr.operands[1].literal.value).to eq(2)
|
174
174
|
end
|
175
175
|
|
176
|
-
it '
|
176
|
+
it 'parses the product of two number literals' do
|
177
177
|
input = '12.34 * 0.3;'
|
178
|
-
ptree =
|
178
|
+
ptree = parser.parse(input)
|
179
179
|
expr = ptree.root.subnodes[0]
|
180
|
-
expect(expr).to
|
180
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
181
181
|
expect(expr.operator).to eq(:*)
|
182
182
|
expect(expr.operands[0].literal.value).to eq(12.34)
|
183
183
|
expect(expr.operands[1].literal.value).to eq(0.3)
|
184
184
|
end
|
185
185
|
|
186
|
-
it '
|
186
|
+
it 'parses multiple multiplicative operations' do
|
187
187
|
input = '5 * 2 / 3;'
|
188
|
-
ptree =
|
188
|
+
ptree = parser.parse(input)
|
189
189
|
expr = ptree.root.subnodes[0]
|
190
|
-
expect(expr).to
|
190
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
191
191
|
expect(expr.operator).to eq(:/)
|
192
|
-
expect(expr.operands[0]).to
|
192
|
+
expect(expr.operands[0]).to be_a(Ast::LoxBinaryExpr)
|
193
193
|
expect(expr.operands[0].operator).to eq(:*)
|
194
194
|
expect(expr.operands[0].operands[0].literal.value).to eq(5)
|
195
195
|
expect(expr.operands[0].operands[1].literal.value).to eq(2)
|
196
196
|
expect(expr.operands[1].literal.value).to eq(3)
|
197
197
|
end
|
198
198
|
|
199
|
-
it '
|
199
|
+
it 'parses combination of terms and factors' do
|
200
200
|
input = '5 + 2 / 3;'
|
201
|
-
ptree =
|
201
|
+
ptree = parser.parse(input)
|
202
202
|
expr = ptree.root.subnodes[0]
|
203
|
-
expect(expr).to
|
203
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
204
204
|
expect(expr.operator).to eq(:+)
|
205
205
|
expect(expr.operands[0].literal.value).to eq(5)
|
206
|
-
expect(expr.operands[1]).to
|
206
|
+
expect(expr.operands[1]).to be_a(Ast::LoxBinaryExpr)
|
207
207
|
expect(expr.operands[1].operator).to eq(:/)
|
208
208
|
expect(expr.operands[1].operands[0].literal.value).to eq(2)
|
209
209
|
expect(expr.operands[1].operands[1].literal.value).to eq(3)
|
@@ -211,11 +211,11 @@ LOX_END
|
|
211
211
|
end # context
|
212
212
|
|
213
213
|
context 'Parsing string concatenation' do
|
214
|
-
it '
|
214
|
+
it 'parses the concatenation of two string literals' do
|
215
215
|
input = '"Lo" + "ve";'
|
216
|
-
ptree =
|
216
|
+
ptree = parser.parse(input)
|
217
217
|
expr = ptree.root.subnodes[0]
|
218
|
-
expect(expr).to
|
218
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
219
219
|
expect(expr.operator).to eq(:+)
|
220
220
|
expect(expr.operands[0].literal.value).to eq('Lo')
|
221
221
|
expect(expr.operands[1].literal.value).to eq('ve')
|
@@ -223,12 +223,12 @@ LOX_END
|
|
223
223
|
end # context
|
224
224
|
|
225
225
|
context 'Parsing comparison expressions' do
|
226
|
-
it '
|
226
|
+
it 'parses the comparison of two number literals' do
|
227
227
|
%w[> >= < <=].each do |predicate|
|
228
228
|
input = "3 #{predicate} 2;"
|
229
|
-
ptree =
|
229
|
+
ptree = parser.parse(input)
|
230
230
|
expr = ptree.root.subnodes[0]
|
231
|
-
expect(expr).to
|
231
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
232
232
|
expect(expr.operator).to eq(predicate.to_sym)
|
233
233
|
expect(expr.operands[0].literal.value).to eq(3)
|
234
234
|
expect(expr.operands[1].literal.value).to eq(2)
|
@@ -237,25 +237,25 @@ LOX_END
|
|
237
237
|
end # context
|
238
238
|
|
239
239
|
context 'Parsing equality expressions' do
|
240
|
-
it '
|
240
|
+
it 'parses the equality of two number literals' do
|
241
241
|
%w[!= ==].each do |predicate|
|
242
242
|
input = "3 #{predicate} 2;"
|
243
|
-
ptree =
|
243
|
+
ptree = parser.parse(input)
|
244
244
|
expr = ptree.root.subnodes[0]
|
245
|
-
expect(expr).to
|
245
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
246
246
|
expect(expr.operator).to eq(predicate.to_sym)
|
247
247
|
expect(expr.operands[0].literal.value).to eq(3)
|
248
248
|
expect(expr.operands[1].literal.value).to eq(2)
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
252
|
-
it '
|
252
|
+
it 'parses combination of equality expressions' do
|
253
253
|
input = '5 != 2 == false; // A bit contrived example'
|
254
|
-
ptree =
|
254
|
+
ptree = parser.parse(input)
|
255
255
|
expr = ptree.root.subnodes[0]
|
256
|
-
expect(expr).to
|
256
|
+
expect(expr).to be_a(Ast::LoxBinaryExpr)
|
257
257
|
expect(expr.operator).to eq(:==)
|
258
|
-
expect(expr.operands[0]).to
|
258
|
+
expect(expr.operands[0]).to be_a(Ast::LoxBinaryExpr)
|
259
259
|
expect(expr.operands[0].operator).to eq(:!=)
|
260
260
|
expect(expr.operands[0].operands[0].literal.value).to eq(5)
|
261
261
|
expect(expr.operands[0].operands[1].literal.value).to eq(2)
|
@@ -264,42 +264,42 @@ LOX_END
|
|
264
264
|
end # context
|
265
265
|
|
266
266
|
context 'Parsing logical expressions' do
|
267
|
-
it '
|
267
|
+
it 'parses the logical operations between two sub-expression' do
|
268
268
|
%w[or and].each do |connector|
|
269
269
|
input = "5 > 2 #{connector} 3 <= 4;"
|
270
|
-
ptree =
|
270
|
+
ptree = parser.parse(input)
|
271
271
|
expr = ptree.root.subnodes[0]
|
272
|
-
expect(expr).to
|
272
|
+
expect(expr).to be_a(Ast::LoxLogicalExpr)
|
273
273
|
expect(expr.operator).to eq(connector.to_sym)
|
274
|
-
expect(expr.operands[0]).to
|
274
|
+
expect(expr.operands[0]).to be_a(Ast::LoxBinaryExpr)
|
275
275
|
expect(expr.operands[0].operator).to eq(:>)
|
276
276
|
expect(expr.operands[0].operands[0].literal.value).to eq(5)
|
277
277
|
expect(expr.operands[0].operands[1].literal.value).to eq(2)
|
278
|
-
expect(expr.operands[1]).to
|
278
|
+
expect(expr.operands[1]).to be_a(Ast::LoxBinaryExpr)
|
279
279
|
expect(expr.operands[1].operator).to eq(:<=)
|
280
280
|
expect(expr.operands[1].operands[0].literal.value).to eq(3)
|
281
281
|
expect(expr.operands[1].operands[1].literal.value).to eq(4)
|
282
282
|
end
|
283
283
|
end
|
284
284
|
|
285
|
-
it '
|
285
|
+
it 'parses a combinations of logical expressions' do
|
286
286
|
input = '4 > 3 and 1 < 2 or 4 >= 5;'
|
287
|
-
ptree =
|
287
|
+
ptree = parser.parse(input)
|
288
288
|
expr = ptree.root.subnodes[0]
|
289
|
-
expect(expr).to
|
289
|
+
expect(expr).to be_a(Ast::LoxLogicalExpr)
|
290
290
|
expect(expr.operator).to eq(:or) # or has lower precedence than and
|
291
|
-
expect(expr.operands[0]).to
|
291
|
+
expect(expr.operands[0]).to be_a(Ast::LoxLogicalExpr)
|
292
292
|
expect(expr.operands[0].operator).to eq(:and)
|
293
293
|
conjuncts = expr.operands[0].operands
|
294
|
-
expect(conjuncts[0]).to
|
294
|
+
expect(conjuncts[0]).to be_a(Ast::LoxBinaryExpr)
|
295
295
|
expect(conjuncts[0].operator).to eq(:>)
|
296
296
|
expect(conjuncts[0].operands[0].literal.value).to eq(4)
|
297
297
|
expect(conjuncts[0].operands[1].literal.value).to eq(3)
|
298
|
-
expect(conjuncts[1]).to
|
298
|
+
expect(conjuncts[1]).to be_a(Ast::LoxBinaryExpr)
|
299
299
|
expect(conjuncts[1].operator).to eq(:<)
|
300
300
|
expect(conjuncts[1].operands[0].literal.value).to eq(1)
|
301
301
|
expect(conjuncts[1].operands[1].literal.value).to eq(2)
|
302
|
-
expect(expr.operands[1]).to
|
302
|
+
expect(expr.operands[1]).to be_a(Ast::LoxBinaryExpr)
|
303
303
|
expect(expr.operands[1].operator).to eq(:>=)
|
304
304
|
expect(expr.operands[1].operands[0].literal.value).to eq(4)
|
305
305
|
expect(expr.operands[1].operands[1].literal.value).to eq(5)
|
@@ -307,18 +307,18 @@ LOX_END
|
|
307
307
|
end # context
|
308
308
|
|
309
309
|
context 'Object orientation:' do
|
310
|
-
it '
|
310
|
+
it 'parses object property get access' do
|
311
311
|
input = 'print someObject.someProperty;'
|
312
|
-
ptree =
|
312
|
+
ptree = parser.parse(input)
|
313
313
|
expr = ptree.root.subnodes[0]
|
314
|
-
expect(expr).to
|
314
|
+
expect(expr).to be_a(Ast::LoxPrintStmt)
|
315
315
|
get_expr = expr.subnodes[0]
|
316
|
-
expect(get_expr).to
|
316
|
+
expect(get_expr).to be_a(Ast::LoxGetExpr)
|
317
317
|
expect(get_expr.object.name).to eq('someObject')
|
318
318
|
expect(get_expr.property).to eq('someProperty')
|
319
319
|
end
|
320
320
|
|
321
|
-
it '
|
321
|
+
it 'parses nested call expressions' do
|
322
322
|
input = 'print egg.scramble(3).with(cheddar);'
|
323
323
|
# From section 12.3.1, one expects something like:
|
324
324
|
# LoxCallExpr
|
@@ -330,34 +330,34 @@ LOX_END
|
|
330
330
|
# +- callee = LoxGetExpr
|
331
331
|
# +- property = 'scramble'
|
332
332
|
# +- object = variable 'egg'
|
333
|
-
ptree =
|
333
|
+
ptree = parser.parse(input)
|
334
334
|
print_stmt = ptree.root.subnodes[0]
|
335
|
-
expect(print_stmt).to
|
335
|
+
expect(print_stmt).to be_a(Ast::LoxPrintStmt)
|
336
336
|
outer_call = print_stmt.subnodes[0]
|
337
|
-
expect(outer_call).to
|
337
|
+
expect(outer_call).to be_a(Ast::LoxCallExpr)
|
338
338
|
expect(outer_call.arguments[0].name).to eq('cheddar')
|
339
|
-
expect(outer_call.callee).to
|
339
|
+
expect(outer_call.callee).to be_a(Ast::LoxGetExpr)
|
340
340
|
expect(outer_call.callee.property).to eq('with')
|
341
341
|
inner_call = outer_call.callee.object
|
342
|
-
expect(inner_call).to
|
342
|
+
expect(inner_call).to be_a(Ast::LoxCallExpr)
|
343
343
|
expect(inner_call.arguments[0].literal).to eq(3)
|
344
|
-
expect(inner_call.callee).to
|
344
|
+
expect(inner_call.callee).to be_a(Ast::LoxGetExpr)
|
345
345
|
expect(inner_call.callee.property).to eq('scramble')
|
346
346
|
expect(inner_call.callee.object.name).to eq('egg')
|
347
347
|
end
|
348
348
|
|
349
|
-
it '
|
349
|
+
it 'parses object property set access' do
|
350
350
|
input = 'someObject.someProperty = value;'
|
351
|
-
ptree =
|
351
|
+
ptree = parser.parse(input)
|
352
352
|
expr = ptree.root.subnodes[0]
|
353
|
-
expect(expr).to
|
353
|
+
expect(expr).to be_a(Ast::LoxSetExpr)
|
354
354
|
expect(expr.object.name).to eq('someObject')
|
355
355
|
expect(expr.property).to eq('someProperty')
|
356
|
-
expect(expr.value).to
|
356
|
+
expect(expr.value).to be_a(Ast::LoxVariableExpr)
|
357
357
|
expect(expr.value.name).to eq('value')
|
358
358
|
end
|
359
359
|
|
360
|
-
it '
|
360
|
+
it 'parses complex set access' do
|
361
361
|
input = 'breakfast.omelette.filling.meat = ham;'
|
362
362
|
# From section 12.3.2, one expects something like:
|
363
363
|
# LoxSetExpr
|
@@ -368,17 +368,17 @@ LOX_END
|
|
368
368
|
# +- object = LoxGetExpr
|
369
369
|
# +- property = 'omelette'
|
370
370
|
# +- object = LoxVariableExpr 'breakfast'
|
371
|
-
ptree =
|
371
|
+
ptree = parser.parse(input)
|
372
372
|
expr = ptree.root.subnodes[0]
|
373
|
-
expect(expr).to
|
373
|
+
expect(expr).to be_a(Ast::LoxSetExpr)
|
374
374
|
expect(expr.property).to eq('meat')
|
375
|
-
expect(expr.value).to
|
375
|
+
expect(expr.value).to be_a(Ast::LoxVariableExpr)
|
376
376
|
expect(expr.value.name).to eq('ham')
|
377
|
-
expect(expr.object).to
|
377
|
+
expect(expr.object).to be_a(Ast::LoxGetExpr)
|
378
378
|
expect(expr.object.property).to eq('filling')
|
379
|
-
expect(expr.object.object).to
|
379
|
+
expect(expr.object.object).to be_a(Ast::LoxGetExpr)
|
380
380
|
expect(expr.object.object.property).to eq('omelette')
|
381
|
-
expect(expr.object.object.object).to
|
381
|
+
expect(expr.object.object.object).to be_a(Ast::LoxVariableExpr)
|
382
382
|
expect(expr.object.object.object.name).to eq('breakfast')
|
383
383
|
end
|
384
384
|
end # context
|
@@ -8,15 +8,15 @@ require_relative '../../lib/loxxy/front_end/raw_parser'
|
|
8
8
|
module Loxxy
|
9
9
|
module FrontEnd
|
10
10
|
describe RawParser do
|
11
|
-
subject {
|
11
|
+
subject(:raw_parser) { described_class.new }
|
12
12
|
|
13
13
|
context 'Initialization:' do
|
14
|
-
it '
|
15
|
-
expect {
|
14
|
+
it 'is initialized without argument' do
|
15
|
+
expect { described_class.new }.not_to raise_error
|
16
16
|
end
|
17
17
|
|
18
|
-
it '
|
19
|
-
expect(
|
18
|
+
it 'has its parse engine initialized' do
|
19
|
+
expect(raw_parser.engine).to be_a(Rley::Engine)
|
20
20
|
end
|
21
21
|
end # context
|
22
22
|
|
@@ -27,29 +27,29 @@ module Loxxy
|
|
27
27
|
# where the declaration_star MUST be empty
|
28
28
|
expect(aParseTree.root.symbol.name).to eq('program')
|
29
29
|
(decls, eof) = aParseTree.root.subnodes
|
30
|
-
expect(decls).to
|
30
|
+
expect(decls).to be_a(Rley::PTree::NonTerminalNode)
|
31
31
|
expect(decls.symbol.name).to eq('rep_declaration_star')
|
32
32
|
expect(decls.subnodes).to be_empty
|
33
|
-
expect(eof).to
|
33
|
+
expect(eof).to be_a(Rley::PTree::TerminalNode)
|
34
34
|
expect(eof.symbol.name).to eq('EOF')
|
35
35
|
end
|
36
36
|
|
37
|
-
it '
|
38
|
-
ptree =
|
37
|
+
it 'copes with an empty input' do
|
38
|
+
ptree = raw_parser.parse('')
|
39
39
|
check_empty_input_result(ptree)
|
40
40
|
end
|
41
41
|
|
42
|
-
it '
|
43
|
-
ptree =
|
42
|
+
it 'copes with whitespaces only input' do
|
43
|
+
ptree = raw_parser.parse((' ' * 80) + ("\n" * 20))
|
44
44
|
check_empty_input_result(ptree)
|
45
45
|
end
|
46
46
|
|
47
|
-
it '
|
47
|
+
it 'copes with comments only input' do
|
48
48
|
input = +''
|
49
49
|
%w[First Second Third].each do |ordinal|
|
50
50
|
input << "// #{ordinal} comment line\r\n"
|
51
51
|
end
|
52
|
-
ptree =
|
52
|
+
ptree = raw_parser.parse(input)
|
53
53
|
check_empty_input_result(ptree)
|
54
54
|
end
|
55
55
|
end # context
|
@@ -66,37 +66,37 @@ module Loxxy
|
|
66
66
|
|
67
67
|
curr_node
|
68
68
|
end
|
69
|
-
it '
|
69
|
+
it 'parses a hello world program' do
|
70
70
|
program = <<-LOX_END
|
71
71
|
// Your first Lox program!
|
72
72
|
print "Hello, world!";
|
73
73
|
LOX_END
|
74
|
-
ptree =
|
74
|
+
ptree = raw_parser.parse(program)
|
75
75
|
root = ptree.root
|
76
76
|
expect(root.symbol.name).to eq('program')
|
77
77
|
(decls, eof) = root.subnodes
|
78
|
-
expect(decls).to
|
78
|
+
expect(decls).to be_a(Rley::PTree::NonTerminalNode)
|
79
79
|
expect(decls.symbol.name).to eq('rep_declaration_star')
|
80
|
-
expect(decls.subnodes[0]).to
|
80
|
+
expect(decls.subnodes[0]).to be_a(Rley::PTree::NonTerminalNode)
|
81
81
|
expect(decls.subnodes[0].symbol.name).to eq('rep_declaration_star')
|
82
|
-
expect(decls.subnodes[1]).to
|
82
|
+
expect(decls.subnodes[1]).to be_a(Rley::PTree::NonTerminalNode)
|
83
83
|
expect(decls.subnodes[1].symbol.name).to eq('declaration')
|
84
84
|
statement = decls.subnodes[1].subnodes[0]
|
85
|
-
expect(statement).to
|
85
|
+
expect(statement).to be_a(Rley::PTree::NonTerminalNode)
|
86
86
|
expect(statement.symbol.name).to eq('statement')
|
87
87
|
prnt_stmt = statement.subnodes[0]
|
88
|
-
expect(prnt_stmt).to
|
88
|
+
expect(prnt_stmt).to be_a(Rley::PTree::NonTerminalNode)
|
89
89
|
expect(prnt_stmt.subnodes.size).to eq(3)
|
90
|
-
expect(prnt_stmt.subnodes[0]).to
|
90
|
+
expect(prnt_stmt.subnodes[0]).to be_a(Rley::PTree::TerminalNode)
|
91
91
|
expect(prnt_stmt.subnodes[0].symbol.name).to eq('PRINT')
|
92
|
-
expect(prnt_stmt.subnodes[1]).to
|
92
|
+
expect(prnt_stmt.subnodes[1]).to be_a(Rley::PTree::NonTerminalNode)
|
93
93
|
leaf_node = walk_subnodes(prnt_stmt, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
94
|
-
expect(leaf_node).to
|
94
|
+
expect(leaf_node).to be_a(Rley::PTree::TerminalNode)
|
95
95
|
expect(leaf_node.symbol.name).to eq('STRING')
|
96
96
|
expect(leaf_node.token.value).to eq('Hello, world!')
|
97
|
-
expect(prnt_stmt.subnodes[2]).to
|
97
|
+
expect(prnt_stmt.subnodes[2]).to be_a(Rley::PTree::TerminalNode)
|
98
98
|
expect(prnt_stmt.subnodes[2].symbol.name).to eq('SEMICOLON')
|
99
|
-
expect(eof).to
|
99
|
+
expect(eof).to be_a(Rley::PTree::TerminalNode)
|
100
100
|
expect(eof.symbol.name).to eq('EOF')
|
101
101
|
end
|
102
102
|
end
|