Class: Loxxy::BackEnd::LoxInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/loxxy/back_end/lox_instance.rb

Overview

Runtime representation of a Lox object (instance).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aClass, anEngine) ⇒ LoxInstance

Create an instance from given class

Parameters:



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

#engineObject (readonly)

Returns the value of attribute engine.



12
13
14
# File 'lib/loxxy/back_end/lox_instance.rb', line 12

def engine
  @engine
end

#fieldsHash{String => BuiltinDatatype | LoxFunction | LoxInstance } (readonly)

Returns:



15
16
17
# File 'lib/loxxy/back_end/lox_instance.rb', line 15

def fields
  @fields
end

#klassObject (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_strObject

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