loxxy 0.3.01 → 0.4.01
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +24 -0
- data/CHANGELOG.md +42 -0
- data/README.md +63 -5
- data/lib/loxxy/ast/ast_builder.rb +55 -91
- data/lib/loxxy/ast/ast_visitor.rb +0 -2
- data/lib/loxxy/ast/lox_class_stmt.rb +8 -5
- data/lib/loxxy/ast/lox_fun_stmt.rb +12 -5
- data/lib/loxxy/ast/lox_node.rb +1 -1
- data/lib/loxxy/ast/lox_set_expr.rb +5 -2
- data/lib/loxxy/back_end/engine.rb +147 -50
- data/lib/loxxy/back_end/environment.rb +4 -0
- data/lib/loxxy/back_end/lox_class.rb +6 -8
- data/lib/loxxy/back_end/lox_function.rb +13 -7
- data/lib/loxxy/back_end/lox_instance.rb +14 -2
- data/lib/loxxy/back_end/resolver.rb +6 -0
- data/lib/loxxy/datatype/lx_string.rb +2 -3
- data/lib/loxxy/front_end/grammar.rb +16 -39
- data/lib/loxxy/front_end/scanner.rb +54 -28
- data/lib/loxxy/version.rb +1 -1
- data/loxxy.gemspec +2 -2
- data/spec/back_end/engine_spec.rb +13 -3
- data/spec/front_end/parser_spec.rb +4 -4
- data/spec/front_end/raw_parser_spec.rb +10 -3
- data/spec/front_end/scanner_spec.rb +13 -10
- data/spec/interpreter_spec.rb +44 -0
- data/spec/spec_helper.rb +1 -1
- metadata +5 -5
@@ -35,8 +35,8 @@ module Loxxy
|
|
35
35
|
let(:lit_expr) { Ast::LoxLiteralExpr.new(sample_pos, greeting) }
|
36
36
|
|
37
37
|
it "should react to 'after_var_stmt' event" do
|
38
|
-
# Precondition: value to assign is on top of stack
|
39
|
-
subject.
|
38
|
+
# Precondition: value to assign is on top of expr stack
|
39
|
+
subject.expr_stack.push(greeting)
|
40
40
|
|
41
41
|
expect { subject.after_var_stmt(var_decl) }.not_to raise_error
|
42
42
|
current_env = subject.symbol_table.current_env
|
@@ -46,7 +46,17 @@ module Loxxy
|
|
46
46
|
|
47
47
|
it "should react to 'before_literal_expr' event" do
|
48
48
|
expect { subject.before_literal_expr(lit_expr) }.not_to raise_error
|
49
|
-
expect(subject.
|
49
|
+
expect(subject.expr_stack.pop).to eq(greeting)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'Built-in functions:' do
|
54
|
+
it 'should provide built-in functions' do
|
55
|
+
symb_table = subject.symbol_table
|
56
|
+
%w[clock getc chr exit print_error].each do |name|
|
57
|
+
fun_var = symb_table.current_env.defns[name]
|
58
|
+
expect(fun_var.value).to be_kind_of(BackEnd::Engine::NativeFunction)
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
52
62
|
end # describe
|
@@ -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.
|
357
|
-
expect(expr.
|
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.
|
376
|
-
expect(expr.
|
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)
|
@@ -26,7 +26,10 @@ module Loxxy
|
|
26
26
|
# program => declaration_star EOF
|
27
27
|
# where the declaration_star MUST be empty
|
28
28
|
expect(aParseTree.root.symbol.name).to eq('program')
|
29
|
-
eof = aParseTree.root.subnodes
|
29
|
+
(decls, eof) = aParseTree.root.subnodes
|
30
|
+
expect(decls).to be_kind_of(Rley::PTree::NonTerminalNode)
|
31
|
+
expect(decls.symbol.name).to eq('declaration_star')
|
32
|
+
expect(decls.subnodes).to be_empty
|
30
33
|
expect(eof).to be_kind_of(Rley::PTree::TerminalNode)
|
31
34
|
expect(eof.symbol.name).to eq('EOF')
|
32
35
|
end
|
@@ -73,8 +76,12 @@ LOX_END
|
|
73
76
|
expect(root.symbol.name).to eq('program')
|
74
77
|
(decls, eof) = root.subnodes
|
75
78
|
expect(decls).to be_kind_of(Rley::PTree::NonTerminalNode)
|
76
|
-
expect(decls.symbol.name).to eq('
|
77
|
-
|
79
|
+
expect(decls.symbol.name).to eq('declaration_star')
|
80
|
+
expect(decls.subnodes[0]).to be_kind_of(Rley::PTree::NonTerminalNode)
|
81
|
+
expect(decls.subnodes[0].symbol.name).to eq('declaration_star')
|
82
|
+
expect(decls.subnodes[1]).to be_kind_of(Rley::PTree::NonTerminalNode)
|
83
|
+
expect(decls.subnodes[1].symbol.name).to eq('declaration')
|
84
|
+
stmt = decls.subnodes[1].subnodes[0]
|
78
85
|
expect(stmt).to be_kind_of(Rley::PTree::NonTerminalNode)
|
79
86
|
expect(stmt.symbol.name).to eq('stmt')
|
80
87
|
statement = stmt.subnodes[0]
|
@@ -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
|
-
|
216
|
-
|
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
|
-
|
222
|
-
|
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
|
-
|
229
|
-
|
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:
|
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
|
|
data/spec/interpreter_spec.rb
CHANGED
@@ -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) {
|
data/spec/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 0.4.01
|
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-
|
11
|
+
date: 2021-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rley
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.8.01
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.8.01
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -173,7 +173,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
173
173
|
requirements:
|
174
174
|
- - "~>"
|
175
175
|
- !ruby/object:Gem::Version
|
176
|
-
version: '2.
|
176
|
+
version: '2.5'
|
177
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
178
|
requirements:
|
179
179
|
- - ">="
|