loxxy 0.1.01 → 0.1.02

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: c477fc7db959df775fc4656e21d577dbb3fce65724b07c8a7ec020496c196100
4
- data.tar.gz: f941317a01b0dbcdca250697a4e84b88f85b572fa7f4f67be3de7c162bc8e2cb
3
+ metadata.gz: ddfc01e822c7e68c87d515649f6ef6e2c800c926ca289dfe9f65edeff24e7015
4
+ data.tar.gz: db5a6a8052c0c15f00920c6d76fcd8c6f9cfe380905c20b53f8e74cc7c6d4f84
5
5
  SHA512:
6
- metadata.gz: b379767ee5cdf986a1b5e704c0254bb8901123811504ba58c59d3996897c29413e84298f6d3c65db849ad289183261d30657e1605d076c4ab9a8e9b0c38318da
7
- data.tar.gz: 258f5a4d501bedbbec5f235ca7d858a4195c9088dcef8d63a6a16b9c25306167eecf93ff3b51aafc8ecf17fd6ed40f9dfe220797a033021f377eb62200de99c3
6
+ metadata.gz: f5a85f8a0a4a762f43a9dd51ab972f924a95ed15ed9e864cef0c60f4815902cbfb905fef624cc100bae9283985f8f6c7f960fbd72bd8d87e9ee3ab211eb7dc24
7
+ data.tar.gz: 10522ab655b99e31007dccaa479b4ec59a9340160f6768c084c3b96825f4e89f9f1c46c1655117763d66f28004f5d8e2c193315c80a65dd49b733587bd25823a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## [0.1.02] - 2021-02-21
2
+ - Function definition and call documented in `README.md`
3
+
4
+ ### Changed
5
+ - File `README.md` updated todescribe function definition and function call.
6
+
7
+ ### Fixed
8
+ - Method `BackEnd::Engine#after_print_stmt` now handles of empty stack or nil data.
9
+ - Method `BackEnd::Engine#after_call_expr` was pushing one spurious item onto data stack.
10
+
1
11
  ## [0.1.01] - 2021-02-20
2
12
  ### Fixed
3
13
  - Fixed most offences for Rubocop.
data/README.md CHANGED
@@ -14,14 +14,13 @@ a simple language used in Bob Nystrom's online book [Crafting Interpreters](http
14
14
 
15
15
  ### Current status
16
16
  The project is still in inception and the interpreter is being implemented...
17
- Currently it can execute all allowed __Lox__ expressions and statement except:
18
- - Functions and closures,
17
+ Currently it can execute all allowed __Lox__ expressions and statements except:
18
+ - Closures,
19
19
  - Classes and objects.
20
20
 
21
21
  These will be implemented soon.
22
22
 
23
23
 
24
-
25
24
  ## What's the fuss about Lox?
26
25
  ... Nothing...
27
26
  Bob Nystrom designed a language __simple__ enough so that he could present
@@ -36,11 +35,11 @@ Although __Lox__ is fairly simple, it is far from a toy language:
36
35
  - Functions and closures
37
36
  - Object-orientation (classes, methods, inheritance).
38
37
 
39
- In other words, __Lox__ contains interesting features expected from most general-purpose
38
+ In other words, __Lox__ contains interesting features found in most general-purpose
40
39
  languages.
41
40
 
42
41
  ### What's missing in Lox?
43
- __Lox__ was constrained by design and therefore was not aimed to be a language used in real-world applications.
42
+ __Lox__ was constrained by design and was therefore not aimed to be a language used in real-world applications.
44
43
  Here are some missing parts to make it a _practical_ language:
45
44
  - Collections (arrays, maps, ...)
46
45
  - Modules (importing stuff from other packages/files)
@@ -70,6 +69,23 @@ lox = Loxxy::Interpreter.new
70
69
  lox.evaluate(lox_program) # Output: Hello, world!
71
70
  ```
72
71
 
72
+ ## A function definition example
73
+ ```ruby
74
+ require 'loxxy'
75
+
76
+ lox_program = <<LOX_END
77
+ fun add4(n) {
78
+ n + 4;
79
+ }
80
+
81
+ print add4(6); // Output: 10
82
+ LOX_END
83
+
84
+ lox = Loxxy::Interpreter.new
85
+ lox.evaluate(lox_program) # Output 10
86
+ ```
87
+
88
+
73
89
  ## Retrieving the result from a Lox program
74
90
  The __Loxxy__ interpreter returns the value of the last evaluated expression.
75
91
 
@@ -151,6 +167,7 @@ Here are the language features currently supported by the interpreter:
151
167
  - [Print Statement](#print-statement)
152
168
  - [While Statement](#while-statement)
153
169
  - [Block Statement](#block-statement)
170
+ - [Function declaration](#func-statement)
154
171
 
155
172
  ### Comments
156
173
 
@@ -177,19 +194,21 @@ loxxy supports all the standard __Lox__ datatypes:
177
194
  ### Statements
178
195
 
179
196
  Loxxy supports the following statements:
180
- - [Expressions](#expressions)
197
+ - [Expressions](#expressions)
181
198
  -[Arithmetic expressions](#arithmetic-expressions)
182
199
  -[String concatenation](#string-concatenation)
183
200
  -[Comparison expressions](#comparison-expressions)
184
201
  -[Logical expressions](#logical-expressions)
185
202
  -[Grouping expressions](#grouping-expressions)
186
- -[Variable expressions and assignments](#variable-expressions)
187
-
203
+ -[Variable expressions and assignments](#variable-expressions)
204
+ -[Function call](#function-call)
205
+
188
206
  -[Variable declarations](#var-statement)
189
207
  -[If Statement](#if-statement)
190
208
  -[Print Statement](#print-statement)
191
- -[While Statement](#while-statement)
192
- -[Block Statement](#block-statement)
209
+ -[While Statement](#while-statement)
210
+ -[Block Statement](#block-statement)
211
+ -[Function Declaration](#function-declaration)
193
212
 
194
213
  #### Expressions
195
214
 
@@ -284,6 +303,15 @@ var iAmNil; // __Lox__ initializes variables to nil by default;
284
303
  print iAmNil; // output: nil
285
304
  ```
286
305
 
306
+ #### Function call
307
+ ``` javascript
308
+ // Calling a function without argument
309
+ print clock();
310
+
311
+ // Assumption: there exists a function `add` that takes two arguments
312
+ print add(2, 3);
313
+ ```
314
+
287
315
  #### For statement
288
316
 
289
317
  Similar to the `for` statement in `C` language
@@ -362,6 +390,19 @@ var a = "outer";
362
390
  print a; // output: outer
363
391
  ```
364
392
 
393
+ #### Function Declaration
394
+ The keyword `fun` is used to begin a function declaration.
395
+ In __Lox__ a function has a name and a body (which may be empty).
396
+
397
+ ``` javascript
398
+ fun add4(n) // `add4` will be the name of the function
399
+ {
400
+ n + 4;
401
+ }
402
+
403
+ print add4(6); // output: 10
404
+ ```
405
+
365
406
  ## Installation
366
407
 
367
408
  Add this line to your application's Gemfile:
@@ -83,7 +83,7 @@ module Loxxy
83
83
 
84
84
  def after_print_stmt(_printStmt)
85
85
  tos = stack.pop
86
- @ostream.print tos.to_str
86
+ @ostream.print tos ? tos.to_str : 'nil'
87
87
  end
88
88
 
89
89
  def after_while_stmt(aWhileStmt, aVisitor)
@@ -185,7 +185,7 @@ module Loxxy
185
185
  local = Variable.new(param_name, stack.pop)
186
186
  symbol_table.insert(local)
187
187
  end
188
- stack.push callee.call(aVisitor)
188
+ callee.call(aVisitor)
189
189
 
190
190
  symbol_table.leave_environment
191
191
  end
data/lib/loxxy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loxxy
4
- VERSION = '0.1.01'
4
+ VERSION = '0.1.02'
5
5
  end
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.1.01
4
+ version: 0.1.02
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-02-20 00:00:00.000000000 Z
11
+ date: 2021-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rley