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
-
#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.
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
25 26 27 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 25 def accept(_visitor) engine.stack.push self end |
#get(aName) ⇒ Object
Look up the value of property with given name aName [String] name of object property
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 36 def get(aName) return fields[aName] if fields.include? aName method = klass.find_method(aName) unless method raise StandardError, "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
49 50 51 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 49 def set(aName, aValue) fields[aName] = aValue end |
#to_str ⇒ Object
Text representation of a Lox instance
30 31 32 |
# File 'lib/loxxy/back_end/lox_instance.rb', line 30 def to_str "#{klass.to_str} instance" end |