loxxy 0.2.04 → 0.3.02

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)
@@ -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
 
@@ -217,6 +238,13 @@ LOX_END
217
238
  expect(token_nil.lexeme).to eq('nil')
218
239
  expect(token_nil.value).to be_kind_of(Datatype::Nil)
219
240
  end
241
+
242
+ it 'should differentiate nil from variable spelled same' do
243
+ subject.start_with('Nil')
244
+ similar = subject.tokens[0]
245
+ expect(similar.terminal).to eq('IDENTIFIER')
246
+ expect(similar.lexeme).to eq('Nil')
247
+ end
220
248
  end # context
221
249
 
222
250
  context 'Handling comments:' do
@@ -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.04
4
+ version: 0.3.02
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-25 00:00:00.000000000 Z
11
+ date: 2021-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rley
@@ -99,7 +99,6 @@ files:
99
99
  - lib/loxxy/ast/lox_call_expr.rb
100
100
  - lib/loxxy/ast/lox_class_stmt.rb
101
101
  - lib/loxxy/ast/lox_compound_expr.rb
102
- - lib/loxxy/ast/lox_for_stmt.rb
103
102
  - lib/loxxy/ast/lox_fun_stmt.rb
104
103
  - lib/loxxy/ast/lox_get_expr.rb
105
104
  - lib/loxxy/ast/lox_grouping_expr.rb
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'lox_compound_expr'
4
-
5
- module Loxxy
6
- module Ast
7
- class LoxForStmt < LoxCompoundExpr
8
- # @return [LoxNode] test expression
9
- attr_reader :test_expr
10
-
11
- # @return [LoxNode] update expression
12
- attr_reader :update_expr
13
-
14
- # @return [LoxNode] body statement
15
- attr_accessor :body_stmt
16
-
17
- # @param aPosition [Rley::Lexical::Position] Position of the entry in the input stream.
18
- # @param initialization [Loxxy::Ast::LoxNode]
19
- # @param testExpr [Loxxy::Ast::LoxNode]
20
- # @param updateExpr [Loxxy::Ast::LoxNode]
21
- def initialize(aPosition, initialization, testExpr, updateExpr)
22
- child = initialization ? [initialization] : []
23
- super(aPosition, child)
24
- @test_expr = testExpr
25
- @update_expr = updateExpr
26
- end
27
-
28
- # Accessor to the condition expression
29
- # @return [LoxNode]
30
- def condition
31
- subnodes[0]
32
- end
33
-
34
- define_accept # Add `accept` method as found in Visitor design pattern
35
- end # class
36
- end # module
37
- end # module