loxxy 0.0.21 → 0.0.26
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 +93 -33
- data/README.md +46 -11
- data/lib/loxxy/ast/all_lox_nodes.rb +5 -0
- data/lib/loxxy/ast/ast_builder.rb +35 -3
- data/lib/loxxy/ast/ast_visitor.rb +47 -1
- data/lib/loxxy/ast/lox_assign_expr.rb +27 -0
- data/lib/loxxy/ast/lox_block_stmt.rb +23 -0
- data/lib/loxxy/ast/lox_if_stmt.rb +1 -1
- data/lib/loxxy/ast/lox_seq_decl.rb +17 -0
- data/lib/loxxy/ast/lox_var_stmt.rb +1 -1
- data/lib/loxxy/ast/lox_variable_expr.rb +26 -0
- data/lib/loxxy/ast/lox_while_stmt.rb +32 -0
- data/lib/loxxy/back_end/engine.rb +48 -1
- data/lib/loxxy/back_end/entry.rb +2 -2
- data/lib/loxxy/back_end/environment.rb +3 -3
- data/lib/loxxy/back_end/symbol_table.rb +3 -3
- data/lib/loxxy/back_end/variable.rb +7 -2
- data/lib/loxxy/datatype/builtin_datatype.rb +6 -0
- data/lib/loxxy/front_end/grammar.rb +6 -6
- data/lib/loxxy/version.rb +1 -1
- data/spec/back_end/engine_spec.rb +1 -0
- data/spec/back_end/environment_spec.rb +2 -2
- data/spec/back_end/symbol_table_spec.rb +3 -3
- data/spec/back_end/variable_spec.rb +4 -4
- data/spec/front_end/parser_spec.rb +21 -19
- data/spec/interpreter_spec.rb +102 -3
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1258e225c3c22ba2c8a1df645f2ee39fc6fc2330a62b288a15f59e1ca40596b2
|
4
|
+
data.tar.gz: 102ac797634d4abf2e72902f568c5b0cc5ec161433e5e4c4e0888e7142508d65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1715b6c93b1861fadafac38327ea2fdee592f2b0b46320b04acd7e64db166911dc614d59412dae9d0bbccea9ca95f9299aac276eba9ee2a87a58963a2ec7d13
|
7
|
+
data.tar.gz: 62915b07a3066c8068f41fd622d9d9be4d8baf29b6c3ef0849b3e410be5d52343fa8c00b8bbff1665deefc838cd29a423852f9666af3e410aee77b538f4030f0
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,67 @@
|
|
1
|
-
## [0.0.
|
1
|
+
## [0.0.26] - 2021-01-22
|
2
|
+
- The interpreter implements `while` loops.
|
3
|
+
|
4
|
+
### Added
|
5
|
+
- Class `Ast::LoxWhileStmt` a node that represents a while statement
|
6
|
+
- Method `Ast::ASTBuilder#reduce_while_stmt` creates an `Ast::LoxWhileStmt` node
|
7
|
+
- Method `Ast::ASTVisitor#visit_while_stmt` for visiting an `Ast::LoxWhileStmt` node
|
8
|
+
- Method `BackEnd::Engine#after_while_stmt` implements the while looping structure
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
- File `README.md` updated.
|
12
|
+
|
13
|
+
## [0.0.25] - 2021-01-21
|
14
|
+
- The interpreter implements blocks of code.
|
15
|
+
|
16
|
+
### Added
|
17
|
+
- Class `Ast::LoxBlockStmt` a node that represents a block of code
|
18
|
+
- Method `Ast::ASTBuilder#reduce_block_stmt` creates an `Ast::LoxBlockStmt` node
|
19
|
+
- Method `Ast::ASTVisitor#visit_block_stmt` for visiting an `Ast::LoxBlockStmt` node
|
20
|
+
- Method `BackEnd::Engine#before_block_stmt` creates an new enclosed Environment
|
21
|
+
- Method `BackEnd::Engine#after_block_stmt` close enclosed Environment and make parent Environment the current one
|
22
|
+
|
23
|
+
### Changed
|
24
|
+
- File `README.md` updated.
|
25
|
+
|
26
|
+
## [0.0.24] - 2021-01-20
|
27
|
+
- The interpreter implements the assignment of variables.
|
28
|
+
|
29
|
+
### Added
|
30
|
+
- Class `Ast::LoxAssignExpr` a node that represents the assignment of a value to a variable
|
31
|
+
- Method `Ast::ASTBuilder#reduce_assign_expr` creates an `Ast::LoxAssignExpr` node
|
32
|
+
- Method `Ast::ASTVisitor#visit_assign_expr` for visiting an `Ast::LoxAssignExpr` node
|
33
|
+
- Method `BackEnd::Engine#after_assign_expr` implementation of the assignment
|
34
|
+
- Method `BackEnd::Variable#assign` to assign a value to a variable
|
35
|
+
|
36
|
+
## [0.0.23] - 2021-01-20
|
37
|
+
- Fix for variables without explicit initialization.
|
38
|
+
|
39
|
+
### Added
|
40
|
+
- Method `Ast::ASTVisitor#visit_builtin` for visiting `Datatype::BuiltinDatatype` value.
|
41
|
+
- Method `BackEnd::Engine#before_visit_builtin` push the data value onto the stack.
|
42
|
+
|
43
|
+
### Fixed
|
44
|
+
- Method `Ast::LoxVarStmt#initialize`: in case no explicit value provided then use `Datatype::Nil.instance`instead of Ruby `nil`
|
45
|
+
|
46
|
+
## [0.0.22] - 2021-01-17
|
47
|
+
- The interpreter can retrieve the value of a variable.
|
48
|
+
|
49
|
+
### Added
|
50
|
+
- Method `Ast::ASTBuilder#declaration_plus_more` and `Ast::ASTBuilder#declaration_plus_end` to allow multiple expressions/statements
|
51
|
+
- Method `Ast::ASTBuilder#reduce_var_expression` creates an `Ast::LoxVariableExpr` node
|
52
|
+
- Method `Ast::ASTVisitor#visit_var_expr` for visiting `Ast::LoxVariableExpr` nodes
|
53
|
+
- Class `Ast::LoxSeqDecl` a node that represents a sequence of declarations/statements
|
54
|
+
- Class `Ast::LoxVarExpr` a node that represents a variable occurrence in an expression
|
55
|
+
- Method `Engine::after_variable_expr`: retrieve the value of variable with given name
|
56
|
+
|
57
|
+
### Changed
|
58
|
+
- Method `Ast::ASTBuilder#reduce_lox_program` to support multiple statements/declarations
|
59
|
+
- File `README.md` updated.
|
60
|
+
|
61
|
+
## [0.0.21] - 2021-01-16
|
2
62
|
- The interpreter supports the declaration global variables.
|
3
63
|
|
4
|
-
|
64
|
+
### Added
|
5
65
|
- Class `BackEnd::Entry`, mixin module for objects put in the symbol table
|
6
66
|
- Class `BackEnd::Environment` that keeps track of variables in a given context.
|
7
67
|
- Class `BackEnd::SymbolTable` that keeps track of environments.
|
@@ -14,7 +74,7 @@
|
|
14
74
|
## [0.0.20] - 2021-01-15
|
15
75
|
- The interpreter supports the `if` ... `else` statement.
|
16
76
|
|
17
|
-
|
77
|
+
### Added
|
18
78
|
- Class `Ast::LoxItStmt`, AST node specific for `if` `else` statements
|
19
79
|
- Method `Ast::ASTBuilder#reduce_if_stmt` as semantic action for if ... else
|
20
80
|
- Method `Ast::ASTVisitor#visit_if_stmt` for visiting `LoxIfStmt` nodes
|
@@ -23,20 +83,20 @@
|
|
23
83
|
## [0.0.19] - 2021-01-14
|
24
84
|
- The interpreter supports expressions between parentheses (grouping).
|
25
85
|
|
26
|
-
|
86
|
+
### Added
|
27
87
|
- Class `Ast::LoxLogicalExpr`
|
28
88
|
- Method `Ast::ASTBuilder#reduce_grouping_expr` as semantic action for grouping expression
|
29
89
|
- Method `Ast::ASTVisitor#visit_grouping_expr` for visiting grouping expressions
|
30
90
|
- Method `Engine::after_grouping_expr`for the evaluation of grouping expressions
|
31
91
|
|
32
|
-
|
92
|
+
### Changed
|
33
93
|
- File `grammar.rb` rules for if ... else were given names in order to activate semantic actions.
|
34
94
|
- File `README.md` updated with little `if ... else` documentation.
|
35
95
|
|
36
96
|
## [0.0.18] - 2021-01-13
|
37
97
|
- The interpreter can evaluate `and`, `or`expressions.
|
38
98
|
|
39
|
-
|
99
|
+
### Added
|
40
100
|
- Class `Ast::LoxLogicalExpr`
|
41
101
|
- Method `Ast::ASTBuilder#reduce_logical_expr` for the semantic action require for `and`, `or`
|
42
102
|
- Method `Ast::ASTVisitor#visit_logical_expr` for visiting logical expressions
|
@@ -46,7 +106,7 @@
|
|
46
106
|
- The interpreter can evaluate all arithmetic and comparison operations.
|
47
107
|
- It implements `==`, `!=` and the unary operations `!`, `-`
|
48
108
|
|
49
|
-
|
109
|
+
### Added
|
50
110
|
- Class `Ast::LoxUnaryExpr`
|
51
111
|
- Method `Ast::ASTBuilder#reduce_unary_expr` to support the evaluation of `!` and ``-@`
|
52
112
|
- Method `Ast::ASTVisitor#visit_unnary_expr` for visiting unary expressions
|
@@ -54,14 +114,14 @@
|
|
54
114
|
- In class `Datatype::BuiltinDatatype` the methods `falsey?`, `truthy?`, `!`, `!=`
|
55
115
|
- In class `Datatype::Number`the methods `<`, `<=`, ´>´, `>=` and `-@`
|
56
116
|
|
57
|
-
|
117
|
+
### Changed
|
58
118
|
- File `README.md` updated.
|
59
119
|
|
60
120
|
## [0.0.16] - 2021-01-11
|
61
121
|
- The interpreter can evaluate product and division of two numbers.
|
62
122
|
- It also implements equality `==` and inequality `!=` operators
|
63
123
|
|
64
|
-
|
124
|
+
### Added
|
65
125
|
- Method `Datatype::False#==` for equality testing
|
66
126
|
- Method `Datatype::False#!=` for inequality testing
|
67
127
|
- Method `Datatype::LXString#==` for equality testing
|
@@ -75,17 +135,17 @@
|
|
75
135
|
- Method `Datatype::True#==` for equality testing
|
76
136
|
- Method `Datatype::True#!=` for inequality testing
|
77
137
|
|
78
|
-
|
138
|
+
### Changed
|
79
139
|
- Method `BackEnd::Engine#after_binary_expr` to allow `*`, `/`, `==`, `!=` operators
|
80
140
|
- File `README.md` updated for the newly implemented operators
|
81
141
|
|
82
142
|
## [0.0.15] - 2021-01-11
|
83
143
|
- The interpreter can evaluate substraction between two numbers.
|
84
144
|
|
85
|
-
|
145
|
+
### Added
|
86
146
|
- Method `Datatype::Number#-` implmenting the subtraction operation
|
87
147
|
|
88
|
-
|
148
|
+
### Changed
|
89
149
|
- File `README.md` minor editorial changes.
|
90
150
|
- File `lx_string_spec.rb` Added test for string concatentation
|
91
151
|
- File `number_spec.rb` Added tests for addition and subtraction operations
|
@@ -94,7 +154,7 @@
|
|
94
154
|
## [0.0.14] - 2021-01-10
|
95
155
|
- The interpreter can evaluate addition of numbers and string concatenation
|
96
156
|
|
97
|
-
|
157
|
+
### Added
|
98
158
|
- Method `Ast::ASTVisitor#visit_binary_expr` for visiting binary expressions
|
99
159
|
- Method `Ast::LoxBinaryExpr#accept` for visitor pattern
|
100
160
|
- Method `BackEnd::Engine#after_binary_expr` to trigger execution of binary operator
|
@@ -102,79 +162,79 @@
|
|
102
162
|
- Method `Datatype::LXString#+` implementation of the string concatenation
|
103
163
|
- Method `Datatype::Number#+` implementation of the addition of numbers
|
104
164
|
|
105
|
-
|
165
|
+
### Changed
|
106
166
|
- File `interpreter_spec.rb` Added tests for addition operation and string concatenation
|
107
167
|
|
108
168
|
|
109
169
|
## [0.0.13] - 2021-01-10
|
110
170
|
- The interpreter can evaluate directly simple literals.
|
111
171
|
|
112
|
-
|
172
|
+
### Changed
|
113
173
|
- Class `AST::ASTBuilder` added `reduce_exprStmt` to support the evaluation of literals.
|
114
174
|
- File `README.md` added one more example.
|
115
175
|
- File `parser_spec.rb` Updated the tests to reflect the change in the AST.
|
116
176
|
- File `interpreter_spec.rb` Added a test for literal expression.
|
117
177
|
|
118
|
-
|
178
|
+
### Fixed
|
119
179
|
- File `loxxy.rb`: shorthand method `lox_true` referenced the ... false object (oops).
|
120
180
|
|
121
181
|
## [0.0.12] - 2021-01-09
|
122
182
|
- Initial interpreter capable of evaluating a tiny subset of Lox language.
|
123
183
|
|
124
|
-
|
184
|
+
### Added
|
125
185
|
- Class `AST::LoxNoopExpr`
|
126
186
|
- Class `AST::LoxPrintStmt`
|
127
187
|
- Class `BackEnd::Engine` implementation of the print statement logic
|
128
188
|
- Class `Interpreter`
|
129
189
|
|
130
|
-
|
190
|
+
### Changed
|
131
191
|
- Class `Ast::ASTVisitor` Added visit method
|
132
192
|
- File `README.md` added Hello world example.
|
133
193
|
|
134
194
|
## [0.0.11] - 2021-01-08
|
135
195
|
- AST node generation for logical expression (and, or).
|
136
196
|
|
137
|
-
|
197
|
+
### Changed
|
138
198
|
- Class `AST::ASTBuilder` added `reduce_` methods for logical operations.
|
139
199
|
- File `grammar.rb`added name to logical expression rules
|
140
200
|
- File `README.md` added gem version and license badges, expanded roadmap section.
|
141
201
|
|
142
|
-
|
202
|
+
### Fixed
|
143
203
|
- File `grammar.rb`: a rule had incomplete non-terminal name `conjunct_` in its lhs.
|
144
204
|
|
145
205
|
|
146
206
|
## [0.0.10] - 2021-01-08
|
147
207
|
- AST node generation for equality expression.
|
148
208
|
|
149
|
-
|
209
|
+
### Changed
|
150
210
|
- Class `AST::ASTBuilder` refactoring and added `reduce_` methods for equality operations.
|
151
211
|
- File `grammar.rb`added name to equality rules
|
152
212
|
- File `README.md` added gem version and license badges, expanded roadmap section.
|
153
213
|
|
154
|
-
|
214
|
+
### Fixed
|
155
215
|
- File `grammar.rb`: a rule had still the discarded non-terminal `equalityTest_star` in its lhs.
|
156
216
|
|
157
217
|
## [0.0.9] - 2021-01-07
|
158
218
|
- AST node generation for comparison expression.
|
159
219
|
|
160
|
-
|
220
|
+
### Changed
|
161
221
|
- Class `AST::ASTBuilder` added `reduce_` methods for comparison operations.
|
162
222
|
- File `grammar.rb`added name to comparison rules
|
163
223
|
|
164
224
|
## [0.0.8] - 2021-01-07
|
165
225
|
- AST node generation for arithmetic operations of number literals.
|
166
226
|
|
167
|
-
|
227
|
+
### Changed
|
168
228
|
- Class `AST::ASTBuilder` added `reduce_` methods for arithmetic operations.
|
169
229
|
- File `grammar.rb`added name to arithmetic rules
|
170
230
|
|
171
|
-
|
231
|
+
### Fixed
|
172
232
|
- File `grammar.rb`: second rule for `factor` had a missing member in rhs.
|
173
233
|
|
174
234
|
## [0.0.7] - 2021-01-06
|
175
235
|
- Lox grammar reworked, initial AST classes created.
|
176
236
|
|
177
|
-
|
237
|
+
### Added
|
178
238
|
- Class `Parser` this one generates AST's (Abstract Syntax Tree)
|
179
239
|
- Class `AST::ASTVisitor` draft initial implementation.
|
180
240
|
- Class `AST::BinaryExpr` draft initial implementation.
|
@@ -182,7 +242,7 @@
|
|
182
242
|
- Class `AST::LiteralExpr` draft initial implementation.
|
183
243
|
- Class `AST::LoxNode` draft initial implementation.
|
184
244
|
|
185
|
-
|
245
|
+
### Changed
|
186
246
|
- File `spec_helper.rb`: removed Bundler dependency
|
187
247
|
- Class `AST::ASTBuilder` added initial `reduce_` methods.
|
188
248
|
- File `README.md` Removed example with AST generation since this is in flux.
|
@@ -190,31 +250,31 @@
|
|
190
250
|
## [0.0.6] - 2021-01-03
|
191
251
|
- First iteration of a parser with AST generation.
|
192
252
|
|
193
|
-
|
253
|
+
### Added
|
194
254
|
- Class `Parser` this one generates AST's (Abstract Syntax Tree)
|
195
255
|
- Class `AST::ASTBuilder` default code to generate an AST.
|
196
256
|
|
197
|
-
|
257
|
+
### Changed
|
198
258
|
- File `spec_helper.rb`: removed Bundler dependency
|
199
259
|
- File `README.md` Added example with AST visualization.
|
200
260
|
|
201
|
-
|
261
|
+
### Fixed
|
202
262
|
- File `grammar.rb` ensure that the constant `Grammar` is created once only.
|
203
263
|
|
204
264
|
## [0.0.5] - 2021-01-02
|
205
265
|
- Improved example in `README.md`, code re-styling to please `Rubocop` 1.7
|
206
266
|
|
207
|
-
|
267
|
+
### Changed
|
208
268
|
- Code re-styling to please `Rubocop` 1.7
|
209
269
|
- File `README.md` Improved example with better parse tree visualization.
|
210
270
|
|
211
271
|
## [0.0.4] - 2021-01-01
|
212
272
|
- A first parser implementation able to parse Lox source code.
|
213
273
|
|
214
|
-
|
274
|
+
### Added
|
215
275
|
- Method `LXString::==` equality operator.
|
216
276
|
|
217
|
-
|
277
|
+
### Changed
|
218
278
|
- class `Parser` renamed to `RawParser`
|
219
279
|
- File `README.md` Added an example showing the use of `RawParser` class.
|
220
280
|
|
data/README.md
CHANGED
@@ -131,7 +131,7 @@ program
|
|
131
131
|
On one hand, the parser covers the complete Lox grammar and should therefore, in principle,
|
132
132
|
parse any valid Lox program.
|
133
133
|
|
134
|
-
On the other hand, the interpreter is under development and currently it can evaluate only a
|
134
|
+
On the other hand, the interpreter is under development and currently it can evaluate only a subset of __Lox__.
|
135
135
|
But the situation is changing almost daily, stay tuned...
|
136
136
|
|
137
137
|
Here are the language features currently supported by the interpreter:
|
@@ -144,6 +144,8 @@ Here are the language features currently supported by the interpreter:
|
|
144
144
|
- [Variable declarations](#var-statement)
|
145
145
|
- [If Statement](#if-statement)
|
146
146
|
- [Print Statement](#print-statement)
|
147
|
+
- [While Statement](#while-statement)
|
148
|
+
- [Block Statement](#block-statement)
|
147
149
|
|
148
150
|
### Comments
|
149
151
|
|
@@ -176,10 +178,13 @@ Loxxy supports the following statements:
|
|
176
178
|
-[Comparison expressions](#comparison-expressions)
|
177
179
|
-[Logical expressions](#logical-expressions)
|
178
180
|
-[Grouping expressions](#grouping-expressions)
|
181
|
+
-[Variable expressions and assignments](#variable-expressions)
|
179
182
|
|
180
183
|
-[Variable declarations](#var-statement)
|
181
184
|
-[If Statement](#if-statement)
|
182
|
-
-[Print Statement](#print-statement)
|
185
|
+
-[Print Statement](#print-statement)
|
186
|
+
-[While Statement](#while-statement)
|
187
|
+
-[Block Statement](#block-statement)
|
183
188
|
|
184
189
|
#### Expressions
|
185
190
|
|
@@ -258,16 +263,22 @@ print 3 + 4 * 5; // => 23
|
|
258
263
|
print (3 + 4) * 5; // => 35
|
259
264
|
```
|
260
265
|
|
266
|
+
#### Variable expressions and assignments
|
267
|
+
In __Lox__, a variable expression is nothing than retrieving the value of a variable.
|
268
|
+
``` javascript
|
269
|
+
var foo = "bar;" // Variable declaration
|
270
|
+
print foo; // Variable expression (= use its value)
|
271
|
+
foo = "baz"; // Variable assignment
|
272
|
+
print foo; // Output: baz
|
273
|
+
```
|
274
|
+
|
261
275
|
#### Variable declarations
|
262
276
|
``` javascript
|
263
277
|
var iAmAVariable = "my-initial-value";
|
264
278
|
var iAmNil; // __Lox__ initializes variables to nil by default;
|
279
|
+
print iAmNil; // output: nil
|
265
280
|
```
|
266
281
|
|
267
|
-
Warning: current version cannot retrieve the value of a variable.
|
268
|
-
Expect this capability to be implemented in the coming days.
|
269
|
-
|
270
|
-
|
271
282
|
#### If statement
|
272
283
|
|
273
284
|
Based on a given condition, an if statement executes one of two statements:
|
@@ -288,20 +299,21 @@ The problem in a nutshell: in a nested if ... else ... statement like this:
|
|
288
299
|
``` javascript
|
289
300
|
'if (true) if (false) print "bad"; else print "good";
|
290
301
|
```
|
291
|
-
... there is an ambiguity.
|
302
|
+
... there is an ambiguity.
|
303
|
+
Indeed, according to the __Lox__ grammar, the `else` could be bound
|
292
304
|
either to the first `if` or to the second one.
|
293
305
|
This ambiguity is usually lifted by applying an ad-hoc rule: an `else` is aways bound to the most
|
294
306
|
recent (rightmost) `if`.
|
295
|
-
Being a generic parsing library, `Rley` doesn't apply any of these supplemental rules.
|
307
|
+
Being a generic parsing library, `Rley` doesn't apply any of these supplemental rules.
|
296
308
|
As a consequence,it complains about the found ambiguity and stops the parsing...
|
297
309
|
Although `Rley` can cope with ambiguities, this requires the use of an advanced data structure
|
298
310
|
called `Shared Packed Parse Forest (SPPF)`.
|
299
|
-
SPPF are much more complex to handle than the
|
311
|
+
SPPF are much more complex to handle than the "common" parse trees present in most compiler or interpreter books.
|
300
312
|
Therefore, a future version of `Rley` will incorporate the capability to define disambuiguation rules.
|
301
313
|
|
302
314
|
In the meantime, the `Loxxy` will progress on other __Lox__ features like:
|
303
|
-
- Variables,
|
304
315
|
- Block structures...
|
316
|
+
- Iteration structures (`for` and `while` loops)
|
305
317
|
|
306
318
|
|
307
319
|
#### Print Statement
|
@@ -313,6 +325,29 @@ print "Hello, world!"; // Output: Hello, world!
|
|
313
325
|
|
314
326
|
```
|
315
327
|
|
328
|
+
#### While Statement
|
329
|
+
|
330
|
+
``` javascript
|
331
|
+
var a = 1;
|
332
|
+
while (a < 10) {
|
333
|
+
print a;
|
334
|
+
a = a + 1;
|
335
|
+
} // Output: 123456789
|
336
|
+
```
|
337
|
+
|
338
|
+
#### Block Statement
|
339
|
+
__Lox__ has code blocks.
|
340
|
+
``` javascript
|
341
|
+
var a = "outer";
|
342
|
+
|
343
|
+
{
|
344
|
+
var a = "inner";
|
345
|
+
print a; // output: inner
|
346
|
+
}
|
347
|
+
|
348
|
+
print a; // output: outer
|
349
|
+
```
|
350
|
+
|
316
351
|
## Installation
|
317
352
|
|
318
353
|
Add this line to your application's Gemfile:
|
@@ -335,7 +370,7 @@ TODO: Write usage instructions here
|
|
335
370
|
|
336
371
|
## Other Lox implementations in Ruby
|
337
372
|
|
338
|
-
For Ruby, there is the [lox](https://github.com/rdodson41/ruby-lox) gem.
|
373
|
+
For Ruby, there is the [lox](https://github.com/rdodson41/ruby-lox) gem.
|
339
374
|
There are other Ruby-based projects as well:
|
340
375
|
- [SlowLox](https://github.com/ArminKleinert/SlowLox), described as a "1-to-1 conversion of JLox to Ruby"
|
341
376
|
- [rulox](https://github.com/LevitatingBusinessMan/rulox)
|
@@ -1,11 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'lox_variable_expr'
|
3
4
|
require_relative 'lox_literal_expr'
|
4
5
|
require_relative 'lox_noop_expr'
|
5
6
|
require_relative 'lox_grouping_expr'
|
6
7
|
require_relative 'lox_unary_expr'
|
7
8
|
require_relative 'lox_binary_expr'
|
8
9
|
require_relative 'lox_logical_expr'
|
10
|
+
require_relative 'lox_assign_expr'
|
11
|
+
require_relative 'lox_block_stmt'
|
12
|
+
require_relative 'lox_while_stmt'
|
9
13
|
require_relative 'lox_print_stmt'
|
10
14
|
require_relative 'lox_if_stmt'
|
11
15
|
require_relative 'lox_var_stmt'
|
16
|
+
require_relative 'lox_seq_decl'
|
@@ -149,8 +149,18 @@ module Loxxy
|
|
149
149
|
end
|
150
150
|
|
151
151
|
# rule('program' => 'declaration_plus EOF').as ''
|
152
|
-
def reduce_lox_program(_production,
|
153
|
-
|
152
|
+
def reduce_lox_program(_production, _range, tokens, theChildren)
|
153
|
+
LoxSeqDecl.new(tokens[0].position, theChildren[0])
|
154
|
+
end
|
155
|
+
|
156
|
+
# rule('declaration_plus' => 'declaration_plus declaration').as ''
|
157
|
+
def reduce_declaration_plus_more(_production, _range, _tokens, theChildren)
|
158
|
+
theChildren[0] << theChildren[1]
|
159
|
+
end
|
160
|
+
|
161
|
+
# rule('declaration_plus' => 'declaration')
|
162
|
+
def reduce_declaration_plus_end(_production, _range, _tokens, theChildren)
|
163
|
+
[theChildren[0]]
|
154
164
|
end
|
155
165
|
|
156
166
|
# rule('exprStmt' => 'expression SEMICOLON')
|
@@ -170,7 +180,6 @@ module Loxxy
|
|
170
180
|
Ast::LoxVarStmt.new(tokens[1].position, var_name, theChildren[3])
|
171
181
|
end
|
172
182
|
|
173
|
-
|
174
183
|
# rule('ifStmt' => 'IF ifCondition statement elsePart_opt')
|
175
184
|
def reduce_if_stmt(_production, _range, tokens, theChildren)
|
176
185
|
condition = theChildren[1]
|
@@ -184,6 +193,23 @@ module Loxxy
|
|
184
193
|
Ast::LoxPrintStmt.new(tokens[1].position, theChildren[1])
|
185
194
|
end
|
186
195
|
|
196
|
+
# rule('whileStmt' => 'WHILE LEFT_PAREN expression RIGHT_PAREN statement').as ''
|
197
|
+
def reduce_while_stmt(_production, _range, tokens, theChildren)
|
198
|
+
Ast::LoxWhileStmt.new(tokens[1].position, theChildren[2], theChildren[4])
|
199
|
+
end
|
200
|
+
|
201
|
+
# rule('block' => 'LEFT_BRACE declaration_plus RIGHT_BRACE')
|
202
|
+
def reduce_block_stmt(_production, _range, tokens, theChildren)
|
203
|
+
decls = LoxSeqDecl.new(tokens[1].position, theChildren[1])
|
204
|
+
Ast::LoxBlockStmt.new(tokens[1].position, decls)
|
205
|
+
end
|
206
|
+
|
207
|
+
# rule('assignment' => 'owner_opt IDENTIFIER EQUAL assignment')
|
208
|
+
def reduce_assign_expr(_production, _range, tokens, theChildren)
|
209
|
+
var_name = theChildren[1].token.lexeme.dup
|
210
|
+
Ast::LoxAssignExpr.new(tokens[1].position, var_name, theChildren[3])
|
211
|
+
end
|
212
|
+
|
187
213
|
# rule('logic_or' => 'logic_and disjunct_plus')
|
188
214
|
def reduce_logic_or_plus(production, range, tokens, theChildren)
|
189
215
|
reduce_logical_expr(production, range, tokens, theChildren)
|
@@ -292,6 +318,12 @@ module Loxxy
|
|
292
318
|
literal = first_child.token.value
|
293
319
|
LoxLiteralExpr.new(pos, literal)
|
294
320
|
end
|
321
|
+
|
322
|
+
# rule('primary' => 'IDENTIFIER')
|
323
|
+
def reduce_variable_expr(_production, _range, tokens, theChildren)
|
324
|
+
var_name = theChildren[0].token.lexeme
|
325
|
+
LoxVariableExpr.new(tokens[0].position, var_name)
|
326
|
+
end
|
295
327
|
end # class
|
296
328
|
end # module
|
297
329
|
end # module
|