loxxy 0.0.14 → 0.0.15

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: 18b1d3e9f498ca8249775f55cc3950622d9f5a49cd67db912816cc60f15b3d16
4
- data.tar.gz: 5766130089ebbe6d85a90a9f59ac1a505202c0e2646d4d7da93af025a80b0651
3
+ metadata.gz: 7624afc97e5e8d3cee171dc9fbbe7a45c45698595109a5281d9f58fcaebd73ec
4
+ data.tar.gz: 67ec959b7758245f414314ea9c8b0a2bbf40a5e01133f17325fa40ab720858f0
5
5
  SHA512:
6
- metadata.gz: 621832b48a5625081a0b82a8f8992d78709a1d4a053d8e4719140510242ce14d4418ca23c82d9f52186453c42d5d432203e7af496d7f92726f75622c743119e2
7
- data.tar.gz: aa67901fae29e13d3154e274c7014fe2002fb7e054c62cf253e87e44ac421747db9f5f05ef1e108cd92c8c0124ceb93b31fa537de64f875607c096465f89aee4
6
+ metadata.gz: adcb9cf6b353b09cf623df330fbdff35255571f8781dfd0b46ea6ab32c02cf55bd0130f94414cd91aa8cb798b1ea7cd4aef4844acc556147b5e4b387d099b8c0
7
+ data.tar.gz: 47230281481b965fe1dddd74f763568a0ca125454800984c41fb29629d26c37ae8f7fa0320a38ca784abe605dfb5dc14f4a9c26b0d269240ea07c5e2feb6241b
@@ -1,3 +1,30 @@
1
+ ## [0.0.15] - 2021-01-11
2
+ - The interpreter can evaluate substraction between two numbers.
3
+
4
+ ## Added
5
+ - Method `Datatype::Number#-` implmenting the subtraction operation
6
+
7
+ ## Changed
8
+ - File `README.md` minor editorial changes.
9
+ - File `lx_string_spec.rb` Added test for string concatentation
10
+ - File `number_spec.rb` Added tests for addition and subtraction operations
11
+ - File `interpreter_spec.rb` Added tests for subtraction operation
12
+
13
+ ## [0.0.14] - 2021-01-10
14
+ - The interpreter can evaluate addition of numbers and string concatenation
15
+
16
+ ## Added
17
+ - Method `Ast::ASTVisitor::visit_binary_expr` for visiting binary expressions
18
+ - Method `Ast::LoxBinaryExpr#accept` for visitor pattern
19
+ - Method `BackEnd::Engine#after_binary_expr` to trigger execution of binary operator
20
+ - `Boolean` class hierarchy: added methos `true?` and `false?` to ease spec test writing
21
+ - Method `Datatype::LXString#+` implementation of the string concatenation
22
+ - Method `Datatype::Number#+` implementation of the addition of numbers
23
+
24
+ ## Changed
25
+ - File `interpreter_spec.rb` Added tests for addition operation and string concatenation
26
+
27
+
1
28
  ## [0.0.13] - 2021-01-10
2
29
  - The interpreter can evaluate directly simple literals.
3
30
 
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![Gem Version](https://badge.fury.io/rb/loxxy.svg)](https://badge.fury.io/rb/loxxy)
3
3
  [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/famished-tiger/loxxy/blob/main/LICENSE.txt)
4
4
 
5
-
5
+ ### What is loxxy?
6
6
  A Ruby implementation of the [Lox programming language](https://craftinginterpreters.com/the-lox-language.html ),
7
7
  a simple language used in Bob Nystrom's online book [Crafting Interpreters](https://craftinginterpreters.com/ ).
8
8
 
@@ -16,7 +16,7 @@ a simple language used in Bob Nystrom's online book [Crafting Interpreters](http
16
16
  The project is still in inception and the interpreter is being implemented...
17
17
  Currently it can execute a tiny subset of __Lox__ language.
18
18
 
19
- The __loxxy__ gem also a parser class `RawPaser` that can parse, in principle, any valid Lox input.
19
+ But the __loxxy__ gem hosts also a parser class `RawPaser` that can parse, in principle, any valid Lox input.
20
20
 
21
21
  ## What's the fuss about Lox?
22
22
  ... Nothing...
@@ -74,7 +74,7 @@ require 'loxxy'
74
74
 
75
75
  lox = Loxxy::Interpreter.new
76
76
 
77
- lox_program = '37 + 5; // THE answer'
77
+ lox_program = '47 - 5; // THE answer'
78
78
  result = lox.evaluate(lox_program) # => Loxxy::Datatype::Number
79
79
 
80
80
  # `result` is a Ruby object, so let's use it...
@@ -138,7 +138,6 @@ Here are the language features currently supported by the interpreter:
138
138
  - [Operators and Special Chars](#operators-and-special-chars)
139
139
  - [Datatypes](#datatypes)
140
140
  - [Statements](#statements)
141
- - `print` statement.
142
141
 
143
142
  ### Comments
144
143
 
@@ -166,6 +165,7 @@ The parser recognizes all the __Lox__ operators, delimiters and separators:
166
165
 
167
166
  Of these, the interpreter implements:
168
167
  `+` (addition of two numbers or string concatenation)
168
+ `-` (difference between two numbers)
169
169
 
170
170
  #### Delimiters
171
171
  The parser knows all the __Lox__ grouping delimiters:
@@ -183,19 +183,18 @@ The parser recognizes them all but the interpreter accepts the semicolons only.
183
183
 
184
184
  ### Datatypes
185
185
 
186
- Loxxy supports the following standard __Lox__ datatypes:
186
+ loxxy supports all the standard __Lox__ datatypes:
187
187
  - `Boolean`: Can be `true` or `false`
188
188
  - `Number`: Can be an integer or a floating-point numbers. For example: `123, 12.34, -45.67`
189
189
  - `String`: Sequence of characters surrounded by `"`. For example: `"Hello!"`
190
190
  - `Nil`: Used to define a null value, denoted by the `nil` keyword
191
191
 
192
- All the Lox literals (booleans, numbers, strings and nil),
193
-
194
192
  ## Statements
195
193
  ### Implemented expressions
196
194
  Loxxy implements expressions:
197
- - Consisting of literals only; or,
195
+ - Plain literals only; or,
198
196
  - Addition of two numbers; or,
197
+ - Subtraction of two numbers; or,
199
198
  - Concatenation of two strings
200
199
 
201
200
  ### Implemented statements
@@ -208,8 +207,6 @@ Loxxy implements the following statements:
208
207
  print "Hello" + ", " + "world!";
209
208
  ```
210
209
 
211
-
212
-
213
210
  ## Installation
214
211
 
215
212
  Add this line to your application's Gemfile:
@@ -44,7 +44,7 @@ module Loxxy
44
44
  op = aBinaryExpr.operator
45
45
  operand2 = stack.pop
46
46
  operand1 = stack.pop
47
- implemented = [:+].include?(op)
47
+ implemented = %i[+ -].include?(op)
48
48
  if implemented && operand1.respond_to?(op)
49
49
  stack.push operand1.send(op, operand2)
50
50
  else
@@ -24,16 +24,32 @@ module Loxxy
24
24
 
25
25
  # Perform the addition of two Lox numbers or
26
26
  # one Lox number and a Ruby Numeric
27
- # @param numeric [Loxxy::Datatype::Number, Numeric]
27
+ # @param other [Loxxy::Datatype::Number, Numeric]
28
28
  # @return [Loxxy::Datatype::Number]
29
- def +(numeric)
30
- case numeric
29
+ def +(other)
30
+ case other
31
+ when Number
32
+ self.class.new(value + other.value)
33
+ when Numeric
34
+ self.class.new(value + other)
35
+ else
36
+ err_msg = "`+': #{other.class} can't be coerced into #{self.class} (TypeError)"
37
+ raise TypeError, err_msg
38
+ end
39
+ end
40
+
41
+ # Perform the subtraction of two Lox numbers or
42
+ # one Lox number and a Ruby Numeric
43
+ # @param other [Loxxy::Datatype::Number, Numeric]
44
+ # @return [Loxxy::Datatype::Number]
45
+ def -(other)
46
+ case other
31
47
  when Number
32
- self.class.new(value + numeric.value)
48
+ self.class.new(value - other.value)
33
49
  when Numeric
34
- self.class.new(value + numeric)
50
+ self.class.new(value - other)
35
51
  else
36
- err_msg = "`+': #{numeric.class} can't be coerced into #{self.class} (TypeError)"
52
+ err_msg = "`-': #{other.class} can't be coerced into #{self.class} (TypeError)"
37
53
  raise TypeError, err_msg
38
54
  end
39
55
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loxxy
4
- VERSION = '0.0.14'
4
+ VERSION = '0.0.15'
5
5
  end
@@ -51,6 +51,11 @@ module Loxxy
51
51
  result = subject == ''
52
52
  expect(result).to be_false
53
53
  end
54
+
55
+ it 'performs the concatenation with another string' do
56
+ concatenation = LXString.new('str') + LXString.new('ing')
57
+ expect(concatenation == 'string').to be_true
58
+ end
54
59
  end
55
60
  end # describe
56
61
  end # module
@@ -41,6 +41,16 @@ module Loxxy
41
41
  it 'should give its display representation' do
42
42
  expect(subject.to_str).to eq(sample_value.to_s)
43
43
  end
44
+
45
+ it 'should compute the addition with another number' do
46
+ addition = subject + Number.new(10)
47
+ expect(addition == -2.34).to be_true
48
+ end
49
+
50
+ it 'should compute the subtraction with another number' do
51
+ subtraction = subject - Number.new(10)
52
+ expect(subtraction == -22.34).to be_true
53
+ end
44
54
  end
45
55
  end # describe
46
56
  end # module
@@ -33,12 +33,18 @@ module Loxxy
33
33
  expect(result).to be_kind_of(Loxxy::Datatype::True)
34
34
  end
35
35
 
36
- it 'should evaluate arithmetic operation' do
36
+ it 'should evaluate an addition of two numbers' do
37
37
  result = subject.evaluate('123 + 456; // => 579')
38
38
  expect(result).to be_kind_of(Loxxy::Datatype::Number)
39
39
  expect(result == 579).to be_true
40
40
  end
41
41
 
42
+ it 'should evaluate a substraction of two numbers' do
43
+ result = subject.evaluate('4 - 3; // => 1')
44
+ expect(result).to be_kind_of(Loxxy::Datatype::Number)
45
+ expect(result == 1).to be_true
46
+ end
47
+
42
48
  it 'should evaluate string concatenation' do
43
49
  result = subject.evaluate('"str" + "ing"; // => "string"')
44
50
  expect(result).to be_kind_of(Loxxy::Datatype::LXString)
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.14
4
+ version: 0.0.15
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-10 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rley