loxxy 0.2.05 → 0.3.03

Sign up to get free protection for your applications and to get access to all the features.
@@ -54,20 +54,6 @@ module Loxxy
54
54
  expect(var_b).to be_kind_of(Variable)
55
55
  expect(var_b.name).to eq('b')
56
56
  end
57
-
58
- # it 'should set the suffix of just created variable' do
59
- # subject.insert(var('a'))
60
- # var_a = subject.defns['a']
61
- # expect(var_a.suffix).to eq("_#{subject.object_id.to_s(16)}")
62
- # end
63
-
64
- # it 'should complain when variable names collide' do
65
- # subject.insert(var('c'))
66
- # expect(subject.defns['c']).to be_kind_of(Datatype::Variable)
67
- # err = StandardError
68
- # err_msg = "Variable with name 'c' already exists."
69
- # expect { subject.insert(var('c')) }.to raise_error(err, err_msg)
70
- # end
71
57
  end # context
72
58
  end # describe
73
59
  end # module
@@ -106,25 +106,6 @@ module Loxxy
106
106
  expect(subject.lookup('q')).to eq(subject.current_env.defns['q'])
107
107
  end
108
108
 
109
- # it 'should allow the search of an entry based on its i_name' do
110
- # subject.insert(var('q'))
111
- # i_name_x = subject.insert(var('x'))
112
- # subject.enter_environment(BackEnd::Environment.new)
113
- # i_name_q2 = subject.insert(var('q'))
114
- # i_name_y = subject.insert(var('y'))
115
-
116
- # # Search for unknown i_name
117
- # expect(subject.lookup_i_name('dummy')).to be_nil
118
-
119
- # curr_scope = subject.current_env
120
- # # # Search for existing unique names
121
- # expect(subject.lookup_i_name(i_name_y)).to eq(curr_scope.defns['y'])
122
- # expect(subject.lookup_i_name(i_name_x)).to eq(subject.root.defns['x'])
123
-
124
- # # Search for redefined name
125
- # expect(subject.lookup_i_name(i_name_q2)).to eq(curr_scope.defns['q'])
126
- # end
127
-
128
109
  it 'should list all the variables defined in all the szcope chain' do
129
110
  subject.insert(var('q'))
130
111
  subject.enter_environment(BackEnd::Environment.new)
@@ -38,41 +38,6 @@ module Loxxy
38
38
  instance = Variable.new(sample_name)
39
39
  expect(instance.value).to eq(Datatype::Nil.instance)
40
40
  end
41
-
42
- # it 'should know its default internal name' do
43
- # # By default: internal name == label
44
- # expect(subject.i_name).to eq(subject.label)
45
- # end
46
-
47
- # it 'should have a nil suffix' do
48
- # expect(subject.suffix).to be_nil
49
- # end
50
- end # context
51
-
52
- context 'Provided service:' do
53
- let(:sample_suffix) { 'sample-suffix' }
54
- it 'should have a label equal to its user-defined name' do
55
- # expect(subject.label).to eq(subject.name)
56
- end
57
-
58
- it 'should accept a suffix' do
59
- # expect { subject.suffix = sample_suffix }.not_to raise_error
60
- # expect(subject.suffix).to eq(sample_suffix)
61
- end
62
-
63
- it 'should calculate its internal name' do
64
- # # Rule: empty suffix => internal name == label
65
- # subject.suffix = ''
66
- # expect(subject.i_name).to eq(subject.label)
67
-
68
- # # Rule: suffix starting underscore: internal name = label + suffix
69
- # subject.suffix = '_10'
70
- # expect(subject.i_name).to eq(subject.label + subject.suffix)
71
-
72
- # # Rule: ... otherwise: internal name == suffix
73
- # subject.suffix = sample_suffix
74
- # expect(subject.i_name).to eq(subject.suffix)
75
- end
76
41
  end # context
77
42
  end # describe
78
43
  end # module
@@ -353,8 +353,8 @@ LOX_END
353
353
  expect(expr).to be_kind_of(Ast::LoxSetExpr)
354
354
  expect(expr.object.name).to eq('someObject')
355
355
  expect(expr.property).to eq('someProperty')
356
- expect(expr.subnodes[0]).to be_kind_of(Ast::LoxVariableExpr)
357
- expect(expr.subnodes[0].name).to eq('value')
356
+ expect(expr.value).to be_kind_of(Ast::LoxVariableExpr)
357
+ expect(expr.value.name).to eq('value')
358
358
  end
359
359
 
360
360
  it 'should parse complex set access' do
@@ -372,8 +372,8 @@ LOX_END
372
372
  expr = ptree.root.subnodes[0]
373
373
  expect(expr).to be_kind_of(Ast::LoxSetExpr)
374
374
  expect(expr.property).to eq('meat')
375
- expect(expr.subnodes[0]).to be_kind_of(Ast::LoxVariableExpr)
376
- expect(expr.subnodes[0].name).to eq('ham')
375
+ expect(expr.value).to be_kind_of(Ast::LoxVariableExpr)
376
+ expect(expr.value.name).to eq('ham')
377
377
  expect(expr.object).to be_kind_of(Ast::LoxGetExpr)
378
378
  expect(expr.object.property).to eq('filling')
379
379
  expect(expr.object.object).to be_kind_of(Ast::LoxGetExpr)
@@ -124,18 +124,15 @@ LOX_END
124
124
 
125
125
  it 'should recognize number values' do
126
126
  input = <<-LOX_END
127
- 123 987654
128
- 0 -0
129
- 123.456 -0.001
130
- LOX_END
127
+ 123 987654
128
+ 0 123.456
129
+ LOX_END
131
130
 
132
131
  expectations = [
133
132
  ['123', 123],
134
133
  ['987654', 987654],
135
134
  ['0', 0],
136
- ['-0', 0],
137
- ['123.456', 123.456],
138
- ['-0.001', -0.001]
135
+ ['123.456', 123.456]
139
136
  ]
140
137
 
141
138
  subject.start_with(input)
@@ -149,6 +146,30 @@ LOX_END
149
146
  end
150
147
  end
151
148
 
149
+ it 'should recognize negative number values' do
150
+ input = <<-LOX_END
151
+ -0
152
+ -0.001
153
+ LOX_END
154
+
155
+ expectations = [
156
+ ['-', '0'],
157
+ ['-', '0.001']
158
+ ].flatten
159
+
160
+ subject.start_with(input)
161
+ tokens = subject.tokens
162
+ tokens.pop
163
+ i = 0
164
+ tokens.each_slice(2) do |(sign, lit)|
165
+ expect(sign.terminal).to eq('MINUS')
166
+ expect(sign.lexeme).to eq(expectations[i])
167
+ expect(lit.terminal).to eq('NUMBER')
168
+ expect(lit.lexeme).to eq(expectations[i + 1])
169
+ i += 2
170
+ end
171
+ end
172
+
152
173
  it 'should recognize leading and trailing dots as distinct tokens' do
153
174
  input = '.456 123.'
154
175
 
@@ -190,22 +211,25 @@ LOX_END
190
211
  end
191
212
 
192
213
  it 'should recognize escaped quotes' do
193
- embedded_quotes = %q{she said: \"Hello\"}
194
- result = subject.send(:unescape_string, embedded_quotes)
195
- expect(result).to eq('she said: "Hello"')
214
+ embedded_quotes = %q{"she said: \"Hello\""}
215
+ subject.start_with(embedded_quotes)
216
+ result = subject.tokens[0]
217
+ expect(result.value).to eq('she said: "Hello"')
196
218
  end
197
219
 
198
220
  it 'should recognize escaped backslash' do
199
- embedded_backslash = 'backslash>\\\\'
200
- result = subject.send(:unescape_string, embedded_backslash)
201
- expect(result).to eq('backslash>\\')
221
+ embedded_backslash = '"backslash>\\\\"'
222
+ subject.start_with(embedded_backslash)
223
+ result = subject.tokens[0]
224
+ expect(result.value).to eq('backslash>\\')
202
225
  end
203
226
 
204
227
  # rubocop: disable Style/StringConcatenation
205
228
  it 'should recognize newline escape sequence' do
206
- embedded_newline = 'line1\\nline2'
207
- result = subject.send(:unescape_string, embedded_newline)
208
- expect(result).to eq('line1' + "\n" + 'line2')
229
+ embedded_newline = '"line1\\nline2"'
230
+ subject.start_with(embedded_newline)
231
+ result = subject.tokens[0]
232
+ expect(result.value).to eq('line1' + "\n" + 'line2')
209
233
  end
210
234
  # rubocop: enable Style/StringConcatenation
211
235
 
@@ -268,7 +292,7 @@ LOX_END
268
292
  it 'should complain if it finds an unterminated string' do
269
293
  subject.start_with('var a = "Unfinished;')
270
294
  err = Loxxy::ScanError
271
- err_msg = 'Error: [line 1:21]: Unterminated string.'
295
+ err_msg = 'Error: [line 1:9]: Unterminated string.'
272
296
  expect { subject.tokens }.to raise_error(err, err_msg)
273
297
  end
274
298
 
@@ -148,6 +148,19 @@ module Loxxy
148
148
  end
149
149
  end
150
150
 
151
+ it 'should ignore spaces surrounding minus in subtraction of two numbers' do
152
+ [
153
+ ['1 - 1;', 0],
154
+ ['1 -1;', 0],
155
+ ['1- 1;', 0],
156
+ ['1-1;', 0]
157
+ ].each do |(source, predicted)|
158
+ lox = Loxxy::Interpreter.new
159
+ result = lox.evaluate(source)
160
+ expect(result.value == predicted).to be_truthy
161
+ end
162
+ end
163
+
151
164
  it 'should evaluate the negation of an object' do
152
165
  [
153
166
  ['!true;', false],
@@ -417,6 +430,50 @@ LOX_END
417
430
  expect(sample_cfg[:ostream].string).to eq('<fn foo><native fn>')
418
431
  end
419
432
 
433
+ it "should implement 'getc' function" do
434
+ input_str = 'Abc'
435
+ cfg = { istream: StringIO.new(input_str) }
436
+ interpreter = Loxxy::Interpreter.new(cfg)
437
+ source = 'getc();'
438
+ result = interpreter.evaluate(source)
439
+ expect(result.value).to eq(65) # codepoint for letter 'A'
440
+ end
441
+
442
+ it "should implement 'chr' function" do
443
+ source = 'chr(65); // => "A"'
444
+ result = subject.evaluate(source)
445
+ expect(result.value).to eq('A')
446
+ end
447
+
448
+ # This test is disabled since it causes RSpec to stop immediately
449
+ # it "should implement 'exit' function" do
450
+ # source = 'exit(100); // Process halts with exit code 100'
451
+ # expect { subject.evaluate(source) }.to raise(SystemExit)
452
+ # end
453
+
454
+ it "should implement 'print_error' function" do
455
+ source = 'print_error("Some error"); // => Some error on stderr'
456
+ stderr_backup = $stderr
457
+ $stderr = StringIO.new
458
+ expect { subject.evaluate(source) }.not_to raise_error
459
+ expect($stderr.string).to eq('Some error')
460
+ $stderr = stderr_backup
461
+ end
462
+
463
+ # rubocop: disable Style/StringConcatenation
464
+ it 'should return in absence of explicit return statement' do
465
+ program = <<-LOX_END
466
+ fun foo() {
467
+ print "foo";
468
+ }
469
+
470
+ print foo();
471
+ LOX_END
472
+ expect { subject.evaluate(program) }.not_to raise_error
473
+ expect(sample_cfg[:ostream].string).to eq('foo' + 'nil')
474
+ end
475
+ # rubocop: enable Style/StringConcatenation
476
+
420
477
  it 'should support return statements' do
421
478
  program = <<-LOX_END
422
479
  fun max(a, b) {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loxxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.05
4
+ version: 0.3.03
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-26 00:00:00.000000000 Z
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rley