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
- #engine ⇒ Loxxy::BackEnd::Engine readonly
-
#meths ⇒ Hash{String => LoxFunction}
readonly
The list of methods.
-
#name ⇒ String
readonly
The name of the class.
-
#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
22 23 24 25 26 27 28 29 30 |
# File 'lib/loxxy/back_end/lox_class.rb', line 22 def initialize(aName, aSuperclass, theMethods, anEngine) @name = aName.dup @superclass = aSuperclass @meths = {} theMethods.each do |func| meths[func.name] = func end @engine = anEngine end |
Instance Attribute Details
#engine ⇒ Loxxy::BackEnd::Engine (readonly)
18 19 20 |
# File 'lib/loxxy/back_end/lox_class.rb', line 18 def engine @engine end |
#meths ⇒ Hash{String => LoxFunction} (readonly)
Returns the list of methods.
15 16 17 |
# File 'lib/loxxy/back_end/lox_class.rb', line 15 def meths @meths end |
#name ⇒ String (readonly)
Returns The name of the class.
11 12 13 |
# File 'lib/loxxy/back_end/lox_class.rb', line 11 def name @name end |
#superclass ⇒ Object (readonly)
Returns the value of attribute superclass.
12 13 14 |
# File 'lib/loxxy/back_end/lox_class.rb', line 12 def superclass @superclass end |
Instance Method Details
#! ⇒ Datatype::False
Logical negation. As a function is a truthy thing, its negation is thus false.
65 66 67 |
# File 'lib/loxxy/back_end/lox_class.rb', line 65 def ! Datatype::False.instance end |
#accept(_visitor) ⇒ Object
32 33 34 |
# File 'lib/loxxy/back_end/lox_class.rb', line 32 def accept(_visitor) engine.expr_stack.push self end |
#arity ⇒ Object
36 37 38 39 |
# File 'lib/loxxy/back_end/lox_class.rb', line 36 def arity initializer = find_method('init') initializer ? initializer.arity : 0 end |
#call(engine, visitor) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/loxxy/back_end/lox_class.rb', line 41 def call(engine, visitor) instance = LoxInstance.new(self, engine) initializer = find_method('init') if initializer constructor = initializer.bind(instance) constructor.call(engine, visitor) else engine.expr_stack.push(instance) end end |
#find_method(aName) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/loxxy/back_end/lox_class.rb', line 53 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
70 71 72 |
# File 'lib/loxxy/back_end/lox_class.rb', line 70 def to_str name end |