loxxy 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aee9fb6c5101bc39682ab401ffc0434aea9e6c4115f74a4f641d0df2d68a03ad
4
- data.tar.gz: ccc11c428ef206db9129fabbb97526df3477e03c5dce0b1b5a2f758425c8f9cb
3
+ metadata.gz: 665b2f201c4e369bb46b4b62cd8398f8d1f6f563b073fb6e1595b767f306c627
4
+ data.tar.gz: 7bd3ca2024f2f05150d3fd8af9cdaee28d6dc4d2bbe48ebfe6ae9fd78166fc7f
5
5
  SHA512:
6
- metadata.gz: dbb1a28eb85868636b304aa3ec3c2fb71f3f37f5c573740bd7d577ed4e88374ce1fea5446e39429168228e68bedbddfccd554bc6c88ec7996621956e11a42a3c
7
- data.tar.gz: 3b4c1d6b0996a351cfd7dae5d8f80683fcbdd1eeecc17c3a7932b0675285700dfecdabecc31f505243ffaf2f1bbc1bd8babb604c154f53dad0019c4ad96b007a
6
+ metadata.gz: 0cffb636754ac0140aa7873955c75d8e6a048a0806cacf22484de91bacecb4fdb7d55127f7ff6e2f5602ff02f1eb9f24de788a6be0053768c68f12b9cdd0caab
7
+ data.tar.gz: 331fb47a8a46a9ea6a086556814b95aba7d7a4d7a3c600f82acacfd38dd567c657ca79f6ce683e17e64e8104167692a2b49e7628bac9d8536a5bd0625ceab347
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## [0.1.12] - 2021-04-03
2
+ - Intermediate version: `Loxxy` does instance creation (default constructor)
3
+
4
+ ### New
5
+ - Method `BackEnd::LoxClass#call` made class callable (for invoking the constructor)
6
+ - Class `BackEnd::LoxInstance` runtime representation of a Lox instance (object).
7
+
8
+ ### Changed
9
+ - Method `BackEnd::Engine#after_call_expr` Added Lox class as callable thing.
10
+
11
+ ### Fixed
12
+ - Method `Ast::ASTBuilder#reduce_class_body` couldn't handle properly empty classes
13
+
1
14
  ## [0.1.11] - 2021-04-03
2
15
  - Intermediate version: `Loxxy` does class declarations
3
16
 
@@ -13,9 +26,9 @@
13
26
  - Method `BackEnd::Engine#after_class_stmt`
14
27
  - Method `BackEnd::Resolver#after_class_stmt`
15
28
  - Method `BackEnd::Resolver#before_class_stmt`
16
- - Class `BackEnd::LoxClass` implementation of a Lox class.
29
+ - Class `BackEnd::LoxClass` runtime representation of a Lox class.
17
30
 
18
- ### CHANGED
31
+ ### Changed
19
32
  - File `grammar.rb` refactoring of class declaration syntax rules
20
33
 
21
34
  ## [0.1.10] - 2021-03-31
@@ -28,7 +41,7 @@
28
41
  ## [0.1.09] - 2021-03-28
29
42
  - Fix and test suite for return statements
30
43
 
31
- ### Changed
44
+ ### CHANGED
32
45
  - `Loxxy` reports an error when a return statement occurs in top-level scope
33
46
 
34
47
  ### Fixed
@@ -40,7 +53,7 @@
40
53
  ### New
41
54
  - Class `BackEnd::Resolver` implements the variable resolution (whenever a variable is in use, locate the declaration of that variable)
42
55
 
43
- ### Changed
56
+ ### CHANGED
44
57
  - Class `Ast::Visitor` changes in some method signatures
45
58
  - Class `BackEnd::Engine` new attribute `resolver` that points to a `BackEnd::Resolver` instance
46
59
  - Class `BackEnd::Engine` several methods dealing with variables have been adapted to take the resolver into account.
@@ -132,7 +145,7 @@
132
145
  - Method `BackEnd::Engine#after_fun_stmt`
133
146
  - Method `BackEnd::NativeFunction#call`
134
147
  - Method `BackEnd::NativeFunction#to_str`
135
- - Method `BackEnd::LoxFunction` implementation of a function object.
148
+ - Method `BackEnd::LoxFunction` runtime representation of a Lox function.
136
149
 
137
150
  ### Changed
138
151
  - Method `BackEnd::Engine#after_call_expr`
@@ -175,7 +175,7 @@ module Loxxy
175
175
 
176
176
  # rule('class_body' => 'LEFT_BRACE methods_opt RIGHT_BRACE')
177
177
  def reduce_class_body(_production, _range, _tokens, theChildren)
178
- theChildren[1]
178
+ theChildren[1].nil? ? [] : theChildren[1]
179
179
  end
180
180
 
181
181
  # rule('method_plus' => 'method_plus function')
@@ -228,6 +228,8 @@ module Loxxy
228
228
  raise Loxxy::RuntimeError, msg
229
229
  end
230
230
  callee.call(self, aVisitor)
231
+ when LoxClass
232
+ callee.call(self, aVisitor)
231
233
  else
232
234
  raise Loxxy::RuntimeError, 'Can only call functions and classes.'
233
235
  end
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../datatype/all_datatypes'
4
+ require_relative 'lox_instance'
4
5
 
5
6
  module Loxxy
6
7
  module BackEnd
7
- # Representation of a Lox class.
8
+ # Runtime representation of a Lox class.
8
9
  class LoxClass
9
10
  # @return [String] The name of the class
10
11
  attr_reader :name
@@ -25,6 +26,15 @@ module Loxxy
25
26
  stack.push self
26
27
  end
27
28
 
29
+ def arity
30
+ 0
31
+ end
32
+
33
+ def call(engine, _visitor)
34
+ instance = LoxInstance.new(self, engine)
35
+ engine.stack.push(instance)
36
+ end
37
+
28
38
  # Logical negation.
29
39
  # As a function is a truthy thing, its negation is thus false.
30
40
  # @return [Datatype::False]
@@ -32,7 +42,7 @@ module Loxxy
32
42
  Datatype::False.instance
33
43
  end
34
44
 
35
- # Text representation of a Lox function
45
+ # Text representation of a Lox class
36
46
  def to_str
37
47
  name
38
48
  end
@@ -36,7 +36,6 @@ module Loxxy
36
36
  end
37
37
 
38
38
  def call(engine, aVisitor)
39
- # new_env = Environment.new(engine.symbol_table.current_env)
40
39
  new_env = Environment.new(closure)
41
40
  engine.symbol_table.enter_environment(new_env)
42
41
 
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../datatype/all_datatypes'
4
+
5
+ module Loxxy
6
+ module BackEnd
7
+ # Runtime representation of a Lox object (instance).
8
+ class LoxInstance
9
+ # @return BackEnd::LoxClass] the class this this object belong
10
+ attr_reader :klass
11
+
12
+ attr_reader :stack
13
+
14
+ # Create an instance from given class
15
+ # @param aClass [BackEnd::LoxClass] the class this this object belong
16
+ def initialize(aClass, anEngine)
17
+ @klass = aClass
18
+ @stack = anEngine.stack
19
+ end
20
+
21
+ def accept(_visitor)
22
+ stack.push self
23
+ end
24
+
25
+ # Text representation of a Lox instance
26
+ def to_str
27
+ "#{klass.to_str} instance"
28
+ end
29
+ end # class
30
+ end # module
31
+ end # module
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.11'
4
+ VERSION = '0.1.12'
5
5
  end
@@ -468,6 +468,20 @@ LOX_END
468
468
  expect(sample_cfg[:ostream].string).to eq('Duck')
469
469
  end
470
470
 
471
+ it 'should support instance creation' do
472
+ program = <<-LOX_END
473
+ class Duck {
474
+ quack() {
475
+ print "quack";
476
+ }
477
+ }
478
+ var daffy = Duck();
479
+ print daffy;
480
+ LOX_END
481
+ expect { subject.evaluate(program) }.not_to raise_error
482
+ expect(sample_cfg[:ostream].string).to eq('Duck instance')
483
+ end
484
+
471
485
  it 'should print the hello world message' do
472
486
  program = <<-LOX_END
473
487
  var greeting = "Hello"; // Declaring a variable
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.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef
@@ -116,6 +116,7 @@ files:
116
116
  - lib/loxxy/back_end/environment.rb
117
117
  - lib/loxxy/back_end/lox_class.rb
118
118
  - lib/loxxy/back_end/lox_function.rb
119
+ - lib/loxxy/back_end/lox_instance.rb
119
120
  - lib/loxxy/back_end/resolver.rb
120
121
  - lib/loxxy/back_end/symbol_table.rb
121
122
  - lib/loxxy/back_end/unary_operator.rb