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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed8bad2906a9ffd76c81a1fe14d89088f913f84996f0a6d98e7f104e0eccfdc9
4
- data.tar.gz: 40ee48dc0f4d7e0ce1028820b9c5cf56d0c78d9853916f3bf9e5f32a7106887a
3
+ metadata.gz: 74cd4aa3bd58cad64cad8ac452535052c761f166da883b06b593590310edd97d
4
+ data.tar.gz: 15af4b4a4c86c7b8e6a14bda16433a9d1663ab581eae78331d29adeed02313d3
5
5
  SHA512:
6
- metadata.gz: 69c3c6e6b2a60c3f27284be820ac8f2687de0073c28719bd9414578ab37ac59de267dc1e69897966fef33d4ba725597587fe991b8e20ce6d40ec1587f3aa53c4
7
- data.tar.gz: c296dcedcf97fe7426a59cfb2df2144685f2809c64becb18483e62a5aea536c2ca3ae2ab697b865c0945456e191a0a011f01123d34160acfa554761b8a2f917e
6
+ metadata.gz: 0dbd9244c36913eed955b1ff92bdc63fd77cc6f7cda3d6b037f3ee87cdcc8d4450673f852426a7395d968711aaf9a3a539efeeaf5e2a0d46865f4f4fa7276c74
7
+ data.tar.gz: c8c65b58fa53d0cce81a3e7508317dfbeb5d8e10f883a0a011d278ca33ee647967bfd34e79fc4703ad7a5b4a3a88aece8d17ca0d0db107f0ec0fcb9ac12e4280
@@ -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('term' => 'factor additive_plus')
76
- def reduce_term_additive(_production, _range, tokens, theChildren)
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(_production, _range, tokens, theChildren)
104
- operand1 = theChildren[0]
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')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loxxy
4
- VERSION = '0.0.8'
4
+ VERSION = '0.0.9'
5
5
  end
@@ -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 additive operations' do
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loxxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef