2014-07-14 10:29:38 +02:00
|
|
|
require_relative "object"
|
2014-07-10 16:14:38 +02:00
|
|
|
module Virtual
|
|
|
|
# static description of a method
|
|
|
|
# name
|
|
|
|
# args (with defaults)
|
|
|
|
# code
|
|
|
|
# return arg (usually mystery, but for coded ones can be more specific)
|
|
|
|
# known local variable names
|
|
|
|
# temp variables (numbered)
|
|
|
|
#
|
2014-07-14 10:29:38 +02:00
|
|
|
class Method < Virtual::Object
|
2014-07-10 16:14:38 +02:00
|
|
|
#return the main function (the top level) into which code is compiled
|
|
|
|
def Method.main
|
|
|
|
Method.new(:main)
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
def attributes
|
|
|
|
[:name , :args]
|
|
|
|
end
|
2014-07-12 20:59:17 +02:00
|
|
|
def initialize name , args = [] , receiver = Virtual::Reference , return_type = Virtual::Reference
|
2014-07-14 10:29:38 +02:00
|
|
|
@name = name.to_sym
|
2014-07-10 16:14:38 +02:00
|
|
|
@args = args
|
|
|
|
@locals = []
|
2014-07-12 20:59:17 +02:00
|
|
|
@receiver = receiver
|
|
|
|
@return_type = return_type
|
2014-07-10 16:14:38 +02:00
|
|
|
@start = MethodEnter.new
|
|
|
|
@current = @start
|
|
|
|
end
|
2014-07-14 10:29:38 +02:00
|
|
|
attr_reader :name , :args
|
2014-07-10 16:14:38 +02:00
|
|
|
|
|
|
|
def add instruction
|
|
|
|
@current.next = instruction
|
|
|
|
@current = instruction
|
|
|
|
end
|
|
|
|
|
|
|
|
# determine whether this method has a variable by the given name
|
|
|
|
# variables are locals and and arguments
|
|
|
|
# used to determine if a send must be issued
|
|
|
|
def has_var name
|
|
|
|
var = @args.find {|a| a == name }
|
|
|
|
var = @locals.find {|a| a == name } unless var
|
|
|
|
var
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|