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



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...

Returns:

  • (FalseClass)


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_strObject

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

Returns:

  • (TrueClass)


33
34
35
# File 'lib/loxxy/back_end/lox_instance.rb', line 33

def truthy?
  true # Default implementation
end