loxxy 0.0.8 → 0.0.9
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 +8 -0
- data/lib/loxxy/ast/ast_builder.rb +30 -11
- data/lib/loxxy/front_end/grammar.rb +3 -3
- data/lib/loxxy/version.rb +1 -1
- data/spec/front_end/parser_spec.rb +49 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74cd4aa3bd58cad64cad8ac452535052c761f166da883b06b593590310edd97d
|
4
|
+
data.tar.gz: 15af4b4a4c86c7b8e6a14bda16433a9d1663ab581eae78331d29adeed02313d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dbd9244c36913eed955b1ff92bdc63fd77cc6f7cda3d6b037f3ee87cdcc8d4450673f852426a7395d968711aaf9a3a539efeeaf5e2a0d46865f4f4fa7276c74
|
7
|
+
data.tar.gz: c8c65b58fa53d0cce81a3e7508317dfbeb5d8e10f883a0a011d278ca33ee647967bfd34e79fc4703ad7a5b4a3a88aece8d17ca0d0db107f0ec0fcb9ac12e4280
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
+
## [0.0.9] - 2021-01-07
|
2
|
+
- AST node generation for comparison expression.
|
3
|
+
|
4
|
+
## Changed
|
5
|
+
- Class `AST::ASTBuilder` added `reduce_` methods for comparison operations.
|
6
|
+
- File `grammar.rb`added name to comparison rules
|
7
|
+
|
1
8
|
## [0.0.8] - 2021-01-07
|
2
9
|
- AST node generation for arithmetic operations of number literals.
|
3
10
|
|
4
11
|
## Changed
|
5
12
|
- Class `AST::ASTBuilder` added `reduce_` methods for arithmetic operations.
|
13
|
+
- File `grammar.rb`added name to arithmetic rules
|
6
14
|
|
7
15
|
## Fixed
|
8
16
|
- File `grammar.rb`: second rule for `factor` had a missing member in rhs.
|
@@ -72,8 +72,8 @@ module Loxxy
|
|
72
72
|
node
|
73
73
|
end
|
74
74
|
|
75
|
-
# rule('
|
76
|
-
def
|
75
|
+
# rule('lhs' => 'nonterm_i nonterm_k_plus')
|
76
|
+
def reduce_binary_operator(_production, _range, tokens, theChildren)
|
77
77
|
operand1 = theChildren[0]
|
78
78
|
|
79
79
|
# Second child is anray with couples [operator, operand2]
|
@@ -84,6 +84,32 @@ module Loxxy
|
|
84
84
|
operand1
|
85
85
|
end
|
86
86
|
|
87
|
+
# rule('comparison' => 'term comparisonTest_plus')
|
88
|
+
def reduce_comparison_plus(production, range, tokens, theChildren)
|
89
|
+
reduce_binary_operator(production, range, tokens, theChildren)
|
90
|
+
end
|
91
|
+
|
92
|
+
# rule('comparisonTest_plus' => 'comparisonTest_plus comparisonTest term').as 'comparison_t_plus_more'
|
93
|
+
# TODO: is it meaningful to implement this rule?
|
94
|
+
|
95
|
+
# rule('comparisonTest_plus' => 'comparisonTest term')
|
96
|
+
def reduce_comparison_t_plus_end(_production, _range, _tokens, theChildren)
|
97
|
+
name2operators = {
|
98
|
+
'GREATER' => '>',
|
99
|
+
'GREATER_EQUAL' => '>=',
|
100
|
+
'LESS' => '<',
|
101
|
+
'LESS_EQUAL' => '<='
|
102
|
+
}
|
103
|
+
operator = name2operators[theChildren[0].symbol.name].to_sym
|
104
|
+
operand2 = theChildren[1]
|
105
|
+
[[operator, operand2]]
|
106
|
+
end
|
107
|
+
|
108
|
+
# rule('term' => 'factor additive_plus')
|
109
|
+
def reduce_term_additive(production, range, tokens, theChildren)
|
110
|
+
reduce_binary_operator(production, range, tokens, theChildren)
|
111
|
+
end
|
112
|
+
|
87
113
|
# rule('additive_star' => 'additive_star additionOp factor').as 'additionOp_expr'
|
88
114
|
def reduce_additive_plus_more(_production, _range, _tokens, theChildren)
|
89
115
|
result = theChildren[0]
|
@@ -100,15 +126,8 @@ module Loxxy
|
|
100
126
|
end
|
101
127
|
|
102
128
|
# rule('factor' => 'multiplicative_plus')
|
103
|
-
def reduce_factor_multiplicative(
|
104
|
-
|
105
|
-
|
106
|
-
# Second child is anray with couples [operator, operand2]
|
107
|
-
theChildren[1].each do |(operator, operand2)|
|
108
|
-
operand1 = LoxBinaryExpr.new(tokens[0].position, operator, operand1, operand2)
|
109
|
-
end
|
110
|
-
|
111
|
-
operand1
|
129
|
+
def reduce_factor_multiplicative(production, range, tokens, theChildren)
|
130
|
+
reduce_binary_operator(production, range, tokens, theChildren)
|
112
131
|
end
|
113
132
|
|
114
133
|
# rule('multiplicative_plus' => 'multiplicative_plus multOp unary')
|
@@ -102,9 +102,9 @@ module Loxxy
|
|
102
102
|
rule('equalityTest' => 'BANG_EQUAL')
|
103
103
|
rule('equalityTest' => 'EQUAL_EQUAL')
|
104
104
|
rule('comparison' => 'term')
|
105
|
-
rule('comparison' => 'term comparisonTest_plus')
|
106
|
-
rule('comparisonTest_plus' => 'comparisonTest_plus comparisonTest term')
|
107
|
-
rule('comparisonTest_plus' => 'comparisonTest term')
|
105
|
+
rule('comparison' => 'term comparisonTest_plus').as 'comparison_plus'
|
106
|
+
rule('comparisonTest_plus' => 'comparisonTest_plus comparisonTest term').as 'comparison_t_plus_more'
|
107
|
+
rule('comparisonTest_plus' => 'comparisonTest term').as 'comparison_t_plus_end'
|
108
108
|
rule('comparisonTest' => 'GREATER')
|
109
109
|
rule('comparisonTest' => 'GREATER_EQUAL')
|
110
110
|
rule('comparisonTest' => 'LESS')
|
data/lib/loxxy/version.rb
CHANGED
@@ -206,7 +206,7 @@ LOX_END
|
|
206
206
|
expect(expr.operands[1].literal.value).to eq(0.3)
|
207
207
|
end
|
208
208
|
|
209
|
-
it 'should parse multiple
|
209
|
+
it 'should parse multiple multiplicative operations' do
|
210
210
|
input = '5 * 2 / 3;'
|
211
211
|
ptree = subject.parse(input)
|
212
212
|
parent = ptree.root.subnodes[0]
|
@@ -221,6 +221,54 @@ LOX_END
|
|
221
221
|
expect(expr.operands[0].operands[1].literal.value).to eq(2)
|
222
222
|
expect(expr.operands[1].literal.value).to eq(3)
|
223
223
|
end
|
224
|
+
|
225
|
+
it 'should parse combination of terms and factors' do
|
226
|
+
input = '5 + 2 / 3;'
|
227
|
+
ptree = subject.parse(input)
|
228
|
+
parent = ptree.root.subnodes[0]
|
229
|
+
expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
|
230
|
+
expect(parent.symbol.name).to eq('exprStmt')
|
231
|
+
expr = parent.subnodes[0]
|
232
|
+
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
|
233
|
+
expect(expr.operator).to eq(:+)
|
234
|
+
expect(expr.operands[0].literal.value).to eq(5)
|
235
|
+
expect(expr.operands[1]).to be_kind_of(Ast::LoxBinaryExpr)
|
236
|
+
expect(expr.operands[1].operator).to eq(:/)
|
237
|
+
expect(expr.operands[1].operands[0].literal.value).to eq(2)
|
238
|
+
expect(expr.operands[1].operands[1].literal.value).to eq(3)
|
239
|
+
end
|
240
|
+
end # context
|
241
|
+
|
242
|
+
context 'Parsing string concatenation' do
|
243
|
+
it 'should parse the concatenation of two string literals' do
|
244
|
+
input = '"Lo" + "ve";'
|
245
|
+
ptree = subject.parse(input)
|
246
|
+
parent = ptree.root.subnodes[0]
|
247
|
+
expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
|
248
|
+
expect(parent.symbol.name).to eq('exprStmt')
|
249
|
+
expr = parent.subnodes[0]
|
250
|
+
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
|
251
|
+
expect(expr.operator).to eq(:+)
|
252
|
+
expect(expr.operands[0].literal.value).to eq('Lo')
|
253
|
+
expect(expr.operands[1].literal.value).to eq('ve')
|
254
|
+
end
|
255
|
+
end # context
|
256
|
+
|
257
|
+
context 'Parsing comparison expressions' do
|
258
|
+
it 'should parse the comparison of two number literals' do
|
259
|
+
%w[> >= < <=].each do |predicate|
|
260
|
+
input = "3 #{predicate} 2;"
|
261
|
+
ptree = subject.parse(input)
|
262
|
+
parent = ptree.root.subnodes[0]
|
263
|
+
expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode)
|
264
|
+
expect(parent.symbol.name).to eq('exprStmt')
|
265
|
+
expr = parent.subnodes[0]
|
266
|
+
expect(expr).to be_kind_of(Ast::LoxBinaryExpr)
|
267
|
+
expect(expr.operator).to eq(predicate.to_sym)
|
268
|
+
expect(expr.operands[0].literal.value).to eq(3)
|
269
|
+
expect(expr.operands[1].literal.value).to eq(2)
|
270
|
+
end
|
271
|
+
end
|
224
272
|
end # context
|
225
273
|
end # describe
|
226
274
|
end # module
|