Class: Loxxy::BackEnd::LoxClass
- Inherits:
-
Object
- Object
- Loxxy::BackEnd::LoxClass
- Defined in:
- lib/loxxy/back_end/lox_class.rb
Overview
Runtime representation of a Lox class.
Instance Attribute Summary collapse
-
#meths ⇒ Hash{String => LoxFunction}
readonly
The list of methods.
-
#name ⇒ String
readonly
The name of the class.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#superclass ⇒ Object
readonly
Returns the value of attribute superclass.
Instance Method Summary collapse
-
#! ⇒ Datatype::False
Logical negation.
- #accept(_visitor) ⇒ Object
- #arity ⇒ Object
- #call(engine, visitor) ⇒ Object
- #find_method(aName) ⇒ Object
-
#initialize(aName, aSuperclass, theMethods, anEngine) ⇒ LoxClass
constructor
Create a class with given name.
-
#to_str ⇒ Object
Text representation of a Lox class.
Constructor Details
#initialize(aName, aSuperclass, theMethods, anEngine) ⇒ LoxClass
Create a class with given name
24 25 26 27 28 29 30 31 32 |
# File 'lib/loxxy/back_end/lox_class.rb', line 24 def initialize(aName, aSuperclass, theMethods, anEngine) @name = aName.dup @superclass = aSuperclass @meths = {} theMethods.each do |func| meths[func.name] = func end @stack = anEngine.stack end |
Instance Attribute Details
#meths ⇒ Hash{String => LoxFunction} (readonly)
Returns the list of methods.
17 18 19 |
# File 'lib/loxxy/back_end/lox_class.rb', line 17 def meths @meths end |
#name ⇒ String (readonly)
Returns The name of the class.
13 14 15 |
# File 'lib/loxxy/back_end/lox_class.rb', line 13 def name @name end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
18 19 20 |
# File 'lib/loxxy/back_end/lox_class.rb', line 18 def stack @stack end |
#superclass ⇒ Object (readonly)
Returns the value of attribute superclass.
14 15 16 |
# File 'lib/loxxy/back_end/lox_class.rb', line 14 def superclass @superclass end |
Instance Method Details
#! ⇒ Datatype::False
Logical negation. As a function is a truthy thing, its negation is thus false.
67 68 69 |
# File 'lib/loxxy/back_end/lox_class.rb', line 67 def ! Datatype::False.instance end |
#accept(_visitor) ⇒ Object
34 35 36 |
# File 'lib/loxxy/back_end/lox_class.rb', line 34 def accept(_visitor) stack.push self end |
#arity ⇒ Object
38 39 40 41 |
# File 'lib/loxxy/back_end/lox_class.rb', line 38 def arity initializer = find_method('init') initializer ? initializer.arity : 0 end |
#call(engine, visitor) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/loxxy/back_end/lox_class.rb', line 43 def call(engine, visitor) instance = LoxInstance.new(self, engine) initializer = find_method('init') if initializer constructor = initializer.bind(instance) constructor.call(engine, visitor) end engine.stack.push(instance) end |
#find_method(aName) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/loxxy/back_end/lox_class.rb', line 55 def find_method(aName) found = meths[aName] unless found || superclass.nil? found = superclass.find_method(aName) end found end |
#to_str ⇒ Object
Text representation of a Lox class
72 73 74 |
# File 'lib/loxxy/back_end/lox_class.rb', line 72 def to_str name end |