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.
32 lines
512 B
32 lines
512 B
require_relative "instance"
|
|
|
|
module Lox
|
|
class LoxClass
|
|
|
|
attr_reader :name
|
|
|
|
def initialize(name, methods)
|
|
@name, @methods = name, methods
|
|
end
|
|
|
|
def find_method(name) = @methods[name]
|
|
def to_s = name
|
|
|
|
def call(interpreter, args)
|
|
instance = Instance.new(self)
|
|
|
|
if init = find_method("init")
|
|
init.bind(instance).call(interpreter, args)
|
|
end
|
|
|
|
instance
|
|
end
|
|
|
|
def arity
|
|
init = find_method("init")
|
|
init ? init.arity : 0
|
|
end
|
|
|
|
end
|
|
end
|