loxxy 0.2.06 → 0.4.00

Sign up to get free protection for your applications and to get access to all the features.
@@ -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)
@@ -211,22 +211,25 @@ LOX_END
211
211
  end
212
212
 
213
213
  it 'should recognize escaped quotes' do
214
- embedded_quotes = %q{she said: \"Hello\"}
215
- result = subject.send(:unescape_string, embedded_quotes)
216
- 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"')
217
218
  end
218
219
 
219
220
  it 'should recognize escaped backslash' do
220
- embedded_backslash = 'backslash>\\\\'
221
- result = subject.send(:unescape_string, embedded_backslash)
222
- 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>\\')
223
225
  end
224
226
 
225
227
  # rubocop: disable Style/StringConcatenation
226
228
  it 'should recognize newline escape sequence' do
227
- embedded_newline = 'line1\\nline2'
228
- result = subject.send(:unescape_string, embedded_newline)
229
- 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')
230
233
  end
231
234
  # rubocop: enable Style/StringConcatenation
232
235
 
@@ -289,7 +292,7 @@ LOX_END
289
292
  it 'should complain if it finds an unterminated string' do
290
293
  subject.start_with('var a = "Unfinished;')
291
294
  err = Loxxy::ScanError
292
- err_msg = 'Error: [line 1:21]: Unterminated string.'
295
+ err_msg = 'Error: [line 1:9]: Unterminated string.'
293
296
  expect { subject.tokens }.to raise_error(err, err_msg)
294
297
  end
295
298
 
@@ -430,6 +430,50 @@ LOX_END
430
430
  expect(sample_cfg[:ostream].string).to eq('<fn foo><native fn>')
431
431
  end
432
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
+
433
477
  it 'should support return statements' do
434
478
  program = <<-LOX_END
435
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.06
4
+ version: 0.4.00
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-05-04 00:00:00.000000000 Z
11
+ date: 2021-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rley