loxxy 0.0.18 → 0.0.19
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/CHANGELOG.md +16 -3
- data/README.md +9 -0
- data/lib/loxxy/ast/all_lox_nodes.rb +1 -0
- data/lib/loxxy/ast/ast_builder.rb +11 -0
- data/lib/loxxy/ast/ast_visitor.rb +8 -0
- data/lib/loxxy/ast/lox_grouping_expr.rb +23 -0
- data/lib/loxxy/back_end/engine.rb +4 -0
- data/lib/loxxy/front_end/grammar.rb +1 -1
- data/lib/loxxy/version.rb +1 -1
- data/spec/interpreter_spec.rb +12 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eced55bddfd7918cbc7aea8eff5f7ecd37aa88baa6e5a6f7350f5bd7606e99af
|
4
|
+
data.tar.gz: f9b8104a0f3a273466ff568a014e345437f4c568621828de8da44961fb52f0e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5b23defe380228bda4e32d9cb94909767fdc42242111f00be214bfbead6323a5e6acda0d78dcaf4e1c2e1fa7d13cd0363661af959a62b093bfcc50c34c290a0
|
7
|
+
data.tar.gz: 96a6a77824cd97b248ad6a78d66d4757c81549fa199021f446883d690f2702dbf4d9879950505d9aa54f45376bd7e6204be8b054e27b43a5576aca744be40047
|
data/CHANGELOG.md
CHANGED
@@ -1,15 +1,28 @@
|
|
1
|
+
## [0.0.18] - 2021-01-14
|
2
|
+
- The interpreter supports expressions between parentheses (grouping).
|
3
|
+
|
4
|
+
## Added
|
5
|
+
- Class `Ast::LoxLogicalExpr`
|
6
|
+
- Method `Ast::ASTBuilder#reduce_grouping_expr` as semantic action for grouping expression
|
7
|
+
- Method `Ast::ASTVisitor#visit_grouping_expr` for visiting grouping expressions
|
8
|
+
- Method `Engine::after_grouping_expr`for the evaluation of grouping expressions
|
9
|
+
|
1
10
|
## [0.0.18] - 2021-01-13
|
2
11
|
- The interpreter can evaluate `and`, `or`expressions.
|
3
12
|
|
4
|
-
|
13
|
+
## Added
|
14
|
+
- Class `Ast::LoxLogicalExpr`
|
15
|
+
- Method `Ast::ASTBuilder#reduce_logical_expr` for the semantic action require for `and`, `or`
|
16
|
+
- Method `Ast::ASTVisitor#visit_logical_expr` for visiting logical expressions
|
17
|
+
- Method `Backend::Engine#after_logical_expr` implements the evaluation of the logical expressions
|
5
18
|
|
6
19
|
## [0.0.17] - 2021-01-12
|
7
20
|
- The interpreter can evaluate all arithmetic and comparison operations.
|
8
21
|
- It implements `==`, `!=` and the unary operations `!`, `-`
|
9
22
|
|
10
23
|
## Added
|
11
|
-
- Class `
|
12
|
-
- Method `
|
24
|
+
- Class `Ast::LoxUnaryExpr`
|
25
|
+
- Method `Ast::ASTBuilder#reduce_unary_expr` to support the evaluation of `!` and ``-@`
|
13
26
|
- Method `Ast::ASTVisitor#visit_unnary_expr` for visiting unary expressions
|
14
27
|
- Method `Backend::Engine#after_unary_expr` evaluating an unary expression
|
15
28
|
- In class `Datatype::BuiltinDatatype` the methods `falsey?`, `truthy?`, `!`, `!=`
|
data/README.md
CHANGED
@@ -245,6 +245,15 @@ REMINDER: In __Lox__, `false` and `nil` are considered falsey, everything else i
|
|
245
245
|
`!!true; // => true`
|
246
246
|
`!0; // => false`
|
247
247
|
|
248
|
+
##### Grouping expressions
|
249
|
+
Use parentheses `(` `)` for a better control in expression/operator precedence.
|
250
|
+
|
251
|
+
``` javascript
|
252
|
+
print 3 + 4 * 5; // => 23
|
253
|
+
print (3 + 4) * 5; // => 35
|
254
|
+
```
|
255
|
+
|
256
|
+
|
248
257
|
##### Print Statement
|
249
258
|
|
250
259
|
The statement print + expression + ; prints the result of the expression to stdout.
|
@@ -134,6 +134,11 @@ module Loxxy
|
|
134
134
|
[[operator, operand2]]
|
135
135
|
end
|
136
136
|
|
137
|
+
# Return the AST node corresponding to the second symbol in the rhs
|
138
|
+
def reduce_keep_symbol2(_production, _range, _tokens, theChildren)
|
139
|
+
theChildren[1]
|
140
|
+
end
|
141
|
+
|
137
142
|
#####################################
|
138
143
|
# SEMANTIC ACTIONS
|
139
144
|
#####################################
|
@@ -253,6 +258,12 @@ module Loxxy
|
|
253
258
|
LoxUnaryExpr.new(tokens[0].position, operator, operand)
|
254
259
|
end
|
255
260
|
|
261
|
+
# rule('primary' => 'LEFT_PAREN expression RIGHT_PAREN')
|
262
|
+
def reduce_grouping_expr(_production, _range, tokens, theChildren)
|
263
|
+
subexpr = theChildren[1]
|
264
|
+
LoxGroupingExpr.new(tokens[0].position, subexpr)
|
265
|
+
end
|
266
|
+
|
256
267
|
# rule('primary' => 'FALSE' | TRUE').as 'literal_expr'
|
257
268
|
def reduce_literal_expr(_production, _range, _tokens, theChildren)
|
258
269
|
first_child = theChildren.first
|
@@ -89,6 +89,14 @@ module Loxxy
|
|
89
89
|
broadcast(:after_unary_expr, anUnaryExpr)
|
90
90
|
end
|
91
91
|
|
92
|
+
# Visit event. The visitor is about to visit a grouping expression.
|
93
|
+
# @param aGroupingExpr [AST::LoxGroupingExpr] grouping expression to visit
|
94
|
+
def visit_grouping_expr(aGroupingExpr)
|
95
|
+
broadcast(:before_grouping_expr, aGroupingExpr)
|
96
|
+
traverse_subnodes(aGroupingExpr)
|
97
|
+
broadcast(:after_grouping_expr, aGroupingExpr)
|
98
|
+
end
|
99
|
+
|
92
100
|
# Visit event. The visitor is visiting the
|
93
101
|
# given terminal node containing a datatype object.
|
94
102
|
# @param aLiteralExpr [AST::LoxLiteralExpr] the leaf node to visit.
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lox_compound_expr'
|
4
|
+
|
5
|
+
module Loxxy
|
6
|
+
module Ast
|
7
|
+
class LoxGroupingExpr < LoxCompoundExpr
|
8
|
+
# @param aPosition [Rley::Lexical::Position] Position of the entry in the input stream.
|
9
|
+
# @param subExpr [Loxxy::Ast::LoxNode]
|
10
|
+
def initialize(aPosition, subExpr)
|
11
|
+
super(aPosition, [subExpr])
|
12
|
+
end
|
13
|
+
|
14
|
+
# Part of the 'visitee' role in Visitor design pattern.
|
15
|
+
# @param visitor [Ast::ASTVisitor] the visitor
|
16
|
+
def accept(visitor)
|
17
|
+
visitor.visit_grouping_expr(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
alias operands subnodes
|
21
|
+
end # class
|
22
|
+
end # module
|
23
|
+
end # module
|
@@ -95,6 +95,10 @@ module Loxxy
|
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
+
def after_grouping_expr(_groupingExpr)
|
99
|
+
# Do nothing: work was already done by visiting /evaluating the subexpression
|
100
|
+
end
|
101
|
+
|
98
102
|
# @param literalExpr [Ast::LoxLiteralExpr]
|
99
103
|
def before_literal_expr(literalExpr)
|
100
104
|
stack.push(literalExpr.literal)
|
@@ -138,7 +138,7 @@ module Loxxy
|
|
138
138
|
rule('primary' => 'NUMBER').as 'literal_expr'
|
139
139
|
rule('primary' => 'STRING').as 'literal_expr'
|
140
140
|
rule('primary' => 'IDENTIFIER')
|
141
|
-
rule('primary' => 'LEFT_PAREN expression RIGHT_PAREN')
|
141
|
+
rule('primary' => 'LEFT_PAREN expression RIGHT_PAREN').as 'grouping_expr'
|
142
142
|
rule('primary' => 'SUPER DOT IDENTIFIER')
|
143
143
|
|
144
144
|
# Utility rules
|
data/lib/loxxy/version.rb
CHANGED
data/spec/interpreter_spec.rb
CHANGED
@@ -211,6 +211,18 @@ module Loxxy
|
|
211
211
|
end
|
212
212
|
end
|
213
213
|
|
214
|
+
it 'should supprt expressions between parentheses' do
|
215
|
+
[
|
216
|
+
['3 + 4 * 5;', 23],
|
217
|
+
['(3 + 4) * 5;', 35],
|
218
|
+
['(5 - (3 - 1)) + -(1);', 2]
|
219
|
+
].each do |(source, predicted)|
|
220
|
+
lox = Loxxy::Interpreter.new
|
221
|
+
result = lox.evaluate(source)
|
222
|
+
expect(result.value == predicted).to be_truthy
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
214
226
|
it 'should print the hello world message' do
|
215
227
|
expect { subject.evaluate(hello_world) }.not_to raise_error
|
216
228
|
expect(sample_cfg[:ostream].string).to eq('Hello, world!')
|
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.0.
|
4
|
+
version: 0.0.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimitri Geshef
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
11
|
+
date: 2021-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rley
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/loxxy/ast/ast_visitor.rb
|
90
90
|
- lib/loxxy/ast/lox_binary_expr.rb
|
91
91
|
- lib/loxxy/ast/lox_compound_expr.rb
|
92
|
+
- lib/loxxy/ast/lox_grouping_expr.rb
|
92
93
|
- lib/loxxy/ast/lox_literal_expr.rb
|
93
94
|
- lib/loxxy/ast/lox_logical_expr.rb
|
94
95
|
- lib/loxxy/ast/lox_node.rb
|