You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
526 B
29 lines
526 B
require_relative "error"
|
|
|
|
module Lox
|
|
class Instance
|
|
|
|
def initialize(klass)
|
|
@klass = klass
|
|
|
|
@fields = {}
|
|
end
|
|
|
|
def get(name)
|
|
return @fields.fetch(name.lexeme) if @fields.has_key?(name.lexeme)
|
|
|
|
method = @klass.find_method(name.lexeme)
|
|
return method.bind(self) unless method.nil?
|
|
|
|
raise RuntimeError.new(name, "Undefined property '#{name.lexeme}'.")
|
|
end
|
|
|
|
def set(name, value)
|
|
@fields[name.lexeme] = value
|
|
end
|
|
|
|
def to_s = "#{@klass.name} instance"
|
|
|
|
end
|
|
end
|