Class: Loxxy::BackEnd::LoxInstance
- Inherits:
-
Object
- Object
- Loxxy::BackEnd::LoxInstance
- Defined in:
- lib/loxxy/back_end/lox_instance.rb
Overview
Runtime representation of a Lox object (instance).
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
- #fields ⇒ Hash{String => BuiltinDatatype | LoxFunction | LoxInstance } readonly
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #accept(_visitor) ⇒ Object
-
#falsey? ⇒ FalseClass
In Lox, only false and Nil have false value...
-
#get(aName) ⇒ Object
Look up the value of property with given name aName [String] name of object property.
-
#initialize(aClass, anEngine) ⇒ LoxInstance
constructor
Create an instance from given class.
-
#set(aName, aValue) ⇒ Object
Set the value of property with given name aName [String] name of object property.
-
#to_str ⇒ Object
Text representation of a Lox instance.
-
#truthy? ⇒ TrueClass
Any instance is truthy.
Constructor Details
#initialize(aClass, anEngine) ⇒ LoxInstance
Create an instance from given class
19 20 21 22 23 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 19 def initialize(aClass, anEngine) @klass = aClass @engine = anEngine @fields = {} end |
Instance Attribute Details
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
12 13 14 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 12 def engine @engine end |
#fields ⇒ Hash{String => BuiltinDatatype | LoxFunction | LoxInstance } (readonly)
15 16 17 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 15 def fields @fields end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
10 11 12 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 10 def klass @klass end |
Instance Method Details
#accept(_visitor) ⇒ Object
42 43 44 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 42 def accept(_visitor) engine.expr_stack.push self end |
#falsey? ⇒ FalseClass
In Lox, only false and Nil have false value...
27 28 29 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 27 def falsey? false # Default implementation end |
#get(aName) ⇒ Object
Look up the value of property with given name aName [String] name of object property
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 48 def get(aName) return fields[aName] if fields.include? aName method = klass.find_method(aName) unless method raise Loxxy::RuntimeError, "Undefined property '#{aName}'." end method.bind(self) end |
#set(aName, aValue) ⇒ Object
Set the value of property with given name aName [String] name of object property
61 62 63 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 61 def set(aName, aValue) fields[aName] = aValue end |
#to_str ⇒ Object
Text representation of a Lox instance
38 39 40 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 38 def to_str "#{klass.to_str} instance" end |
#truthy? ⇒ TrueClass
Any instance is truthy
33 34 35 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 33 def truthy? true # Default implementation end |