change arg to arg_names
and a bit of clean up
This commit is contained in:
parent
c0dcf6feb0
commit
f8fe074af2
@ -3,27 +3,26 @@ require_relative "block"
|
|||||||
module Virtual
|
module Virtual
|
||||||
# static description of a method
|
# static description of a method
|
||||||
# name
|
# name
|
||||||
# args (with defaults)
|
# arg_names (with defaults)
|
||||||
|
# receiver
|
||||||
# code
|
# code
|
||||||
# return arg (usually mystery, but for coded ones can be more specific)
|
# return arg (usually mystery, but for coded ones can be more specific)
|
||||||
# known local variable names
|
# known local variable names
|
||||||
# temp variables (numbered)
|
# temp variables (numbered)
|
||||||
#
|
#
|
||||||
# Methods are similar to Blocks. Where Blocks can be jumped to, Methods can be called.
|
# Methods are one step up from to VM::Blocks. Where Blocks can be jumped to, Methods can be called.
|
||||||
|
|
||||||
# Methods also have arguments and a return. These are Value subclass instances, ie specify
|
# Methods also have arguments and a return. These are typed by subclass instances of Value
|
||||||
# type (by class type) and register by instance
|
|
||||||
|
|
||||||
# They also have local variables. Args take up the first n regs, then locals the rest. No
|
# They also have local variables.
|
||||||
# direct manipulating of registers (ie specifying the number) should be done.
|
|
||||||
|
|
||||||
# Code-wise Methods are made up from a list of Blocks, in a similar way blocks are made up of codes
|
# Code-wise Methods are made up from a list of Blocks, in a similar way blocks are made up of Instructions
|
||||||
# The function starts with one block, and that has a start and end (return)
|
# The function starts with one block, and that has a start and end (return)
|
||||||
|
|
||||||
# Blocks can be linked in two ways:
|
# Blocks can be linked in two ways:
|
||||||
# -linear: flow continues from one to the next as they are sequential both logically and "physically"
|
# -linear: flow continues from one to the next as they are sequential both logically and
|
||||||
# use the block set_next for this.
|
# "physically" use the block set_next for this.
|
||||||
# This "the straight line", there must be a continuous sequence from body to return
|
# This "straight line", there must be a continuous sequence from body to return
|
||||||
# Linear blocks may be created from an existing block with new_block
|
# Linear blocks may be created from an existing block with new_block
|
||||||
# - branched: You create new blocks using function.new_block which gets added "after" return
|
# - branched: You create new blocks using function.new_block which gets added "after" return
|
||||||
# These (eg if/while) blocks may themselves have linear blocks ,but the last of these
|
# These (eg if/while) blocks may themselves have linear blocks ,but the last of these
|
||||||
@ -34,10 +33,10 @@ module Virtual
|
|||||||
def CompiledMethod.main
|
def CompiledMethod.main
|
||||||
CompiledMethod.new(:main , [] )
|
CompiledMethod.new(:main , [] )
|
||||||
end
|
end
|
||||||
def initialize name , args , receiver = Virtual::Self.new , return_type = Virtual::Mystery
|
def initialize name , arg_names , receiver = Virtual::Self.new , return_type = Virtual::Mystery
|
||||||
@name = name.to_sym
|
@name = name.to_sym
|
||||||
@class_name = :Object
|
@class_name = :Object
|
||||||
@args = args
|
@arg_names = arg_names
|
||||||
@locals = []
|
@locals = []
|
||||||
@tmps = []
|
@tmps = []
|
||||||
@receiver = receiver
|
@receiver = receiver
|
||||||
@ -48,7 +47,7 @@ module Virtual
|
|||||||
@current = enter
|
@current = enter
|
||||||
new_block("return").add_code(MethodReturn.new)
|
new_block("return").add_code(MethodReturn.new)
|
||||||
end
|
end
|
||||||
attr_reader :name , :args , :receiver , :blocks
|
attr_reader :name , :arg_names , :receiver , :blocks
|
||||||
attr_accessor :return_type , :current , :class_name
|
attr_accessor :return_type , :current , :class_name
|
||||||
|
|
||||||
# add an instruction after the current (insertion point)
|
# add an instruction after the current (insertion point)
|
||||||
@ -113,7 +112,7 @@ module Virtual
|
|||||||
# used to determine if a send must be issued
|
# used to determine if a send must be issued
|
||||||
def has_var name
|
def has_var name
|
||||||
name = name.to_sym
|
name = name.to_sym
|
||||||
var = @args.find {|a| a.name == name }
|
var = @arg_names.find {|a| a.name == name }
|
||||||
var = @locals.find {|a| a.name == name } unless var
|
var = @locals.find {|a| a.name == name } unless var
|
||||||
var = @tmps.find {|a| a.name == name } unless var
|
var = @tmps.find {|a| a.name == name } unless var
|
||||||
var
|
var
|
||||||
@ -122,7 +121,7 @@ module Virtual
|
|||||||
# determine whether this method has an argument by the name
|
# determine whether this method has an argument by the name
|
||||||
def has_arg name
|
def has_arg name
|
||||||
name = name.to_sym
|
name = name.to_sym
|
||||||
var = @args.find {|a| a.name == name }
|
var = @arg_names.find {|a| a.name == name }
|
||||||
var
|
var
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -139,7 +138,7 @@ module Virtual
|
|||||||
|
|
||||||
def get_var name
|
def get_var name
|
||||||
var = has_var name
|
var = has_var name
|
||||||
raise "no var #{name} in method #{self.name} , #{@locals} #{@args}" unless var
|
raise "no var #{name} in method #{self.name} , #{@locals} #{@arg_names}" unless var
|
||||||
var
|
var
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -162,8 +161,8 @@ module Virtual
|
|||||||
# mov and add will be called on Machine and generate Inststuction that are then added
|
# mov and add will be called on Machine and generate Inststuction that are then added
|
||||||
# to the current block
|
# to the current block
|
||||||
# also symbols are supported and wrapped as register usages (for bare metal programming)
|
# also symbols are supported and wrapped as register usages (for bare metal programming)
|
||||||
def method_missing(meth, *args, &block)
|
def method_missing(meth, *arg_names, &block)
|
||||||
add_code ::Register::RegisterMachine.instance.send(meth , *args)
|
add_code ::Register::RegisterMachine.instance.send(meth , *arg_names)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mem_length
|
def mem_length
|
||||||
|
Loading…
Reference in New Issue
Block a user