rubyx/lib/virtual/method.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

2014-07-14 10:29:38 +02:00
require_relative "object"
2014-07-14 13:06:09 +02:00
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
2014-07-14 13:06:09 +02:00
Method.new(:main , [] , Virtual::SelfReference )
2014-07-10 16:14:38 +02:00
end
2014-07-14 10:29:38 +02:00
def attributes
[:name , :args , :receiver , :start , :return_type]
2014-07-14 10:29:38 +02:00
end
2014-07-14 13:06:09 +02:00
def initialize name , args , receiver = Virtual::SelfReference.new , 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 = []
@receiver = receiver
@return_type = return_type
2014-07-10 16:14:38 +02:00
@start = MethodEnter.new
@current = @start
end
attr_reader :name , :args , :receiver , :start
attr_accessor :return_type
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