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

2 years ago
require_relative "error"
2 years ago
module Lox
class Instance
def initialize(klass)
@klass = klass
2 years ago
@fields = {}
end
def get(name)
2 years ago
return @fields.fetch(name.lexeme) if @fields.has_key?(name.lexeme)
2 years ago
2 years ago
method = @klass.find_method(name.lexeme)
2 years ago
return method.bind(self) unless method.nil?
2 years ago
raise RuntimeError.new(name, "Undefined property '#{name.lexeme}'.")
2 years ago
end
def set(name, value)
@fields[name.lexeme] = value
2 years ago
end
def to_s = "#{@klass.name} instance"
end
end