rename method

This commit is contained in:
Torsten Ruger 2014-07-16 19:24:41 +03:00
parent 55cb6bd2d6
commit 1ff7ae2b0a
13 changed files with 45 additions and 45 deletions

View File

@ -7,7 +7,7 @@ module Ast
Virtual::Argument.new( p.name , Virtual::Mystery.new ) Virtual::Argument.new( p.name , Virtual::Mystery.new )
end end
r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new r = receiver ? receiver.compile(frame,method) : Virtual::SelfReference.new
method = Virtual::Method.new(name , args , r ) method = Virtual::MethodDefinition.new(name , args , r )
frame = frame.new_frame frame = frame.new_frame
return_type = nil return_type = nil
body.each do |ex| body.each do |ex|

View File

@ -4,28 +4,28 @@ module Boot
# class is mainly a list of methods with a name (for now) # class is mainly a list of methods with a name (for now)
# layout of object is seperated into Layout # layout of object is seperated into Layout
class BootClass < Virtual::ObjectConstant class BootClass < Virtual::ObjectConstant
def initialize name , super_class = :Object def initialize name , super_class_name = :Object
super() super()
# class methods # class methods
@methods = [] @method_definitions = []
@name = name.to_sym @name = name.to_sym
@super_class = super_class @super_class_name = super_class_name.to_sym
@meta_class = MetaClass.new(self) @meta_class = MetaClass.new(self)
end end
attr_reader :name , :methods , :meta_class , :context , :super_class attr_reader :name , :methods , :meta_class , :context , :super_class
def attributes def attributes
[:name , :super_class] [:name , :super_class]
end end
def add_method method def add_method_definition method
raise "not a method #{method}" unless method.is_a? Virtual::Method raise "not a method #{method}" unless method.is_a? Virtual::MethodDefinition
raise "syserr " unless method.name.is_a? Symbol raise "syserr " unless method.name.is_a? Symbol
@methods << method @method_definitions << method
end end
def get_method fname def get_method_definition fname
fname = fname.to_sym fname = fname.to_sym
f = @methods.detect{ |f| f.name == fname } f = @method_definitions.detect{ |f| f.name == fname }
names = @methods.collect{|f| f.name } names = @method_definitions.collect{|f| f.name }
f f
end end
@ -33,7 +33,7 @@ module Boot
def resolve_method name def resolve_method name
fun = get_method name fun = get_method name
unless fun or name == :Object unless fun or name == :Object
supr = @context.object_space.get_or_create_class(@super_class) supr = @context.object_space.get_or_create_class(@super_class_name)
fun = supr.get_method name fun = supr.get_method name
puts "#{supr.methods.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of puts "#{supr.methods.collect(&:name)} for #{name} GOT #{fun.class}" if name == :index_of
end end

View File

@ -5,13 +5,13 @@ require "boot/object"
require "boot/string" require "boot/string"
module Boot module Boot
# The BootSpace is contains all objects for a program. In functional terms it is a program, but on oo # The BootSpace contains all objects for a program. In functional terms it is a program, but in oo
# it is a collection of objects, some of which are data, some classes, some functions # it is a collection of objects, some of which are data, some classes, some functions
# The main entry is a function called (of all things) "main", This _must be supplied by the compling # The main entry is a function called (of all things) "main", This _must be supplied by the compling
# There is a start and exit block that call main, which receives an array of strings # There is a start and exit block that call main, which receives an array of strings
# While data "ususally" would live in a .data section, we may also "inline" it into the code # While data ususally would live in a .data section, we may also "inline" it into the code
# in an oo system all data is represented as objects # in an oo system all data is represented as objects
class BootSpace class BootSpace
@ -21,7 +21,7 @@ module Boot
def initialize machine = nil def initialize machine = nil
super() super()
@classes = {} @classes = {}
@main = Virtual::Method.new("main" , [] ) @main = Virtual::MethodDefinition.new("main" , [] )
#global objects (data) #global objects (data)
@objects = [] @objects = []
boot_classes boot_classes
@ -43,24 +43,24 @@ module Boot
# boot the classes, ie create a minimal set of classes with a minimal set of functions # boot the classes, ie create a minimal set of classes with a minimal set of functions
# minimal means only that which can not be coded in ruby # minimal means only that which can not be coded in ruby
# Functions are grabbed from respective modules by sending the sunction name. This should return the # MethodDefinitions are grabbed from respective modules by sending the method name. This should return the
# implementation of the function (ie a function object), not actually try to implement it (as that's impossible in ruby) # implementation of the method (ie a method object), not actually try to implement it (as that's impossible in ruby)
def boot_classes def boot_classes
# very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we have to define some # very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we have to define some
# dummies, just for the other to compile # dummies, just for the other to compile
obj = get_or_create_class :Object obj = get_or_create_class :Object
[:index_of , :_get_instance_variable , :_set_instance_variable].each do |f| [:index_of , :_get_instance_variable , :_set_instance_variable].each do |f|
#puts "Boot Object::#{f}" #puts "Boot Object::#{f}"
obj.add_method Boot::Object.send(f , @context) obj.add_method_definition Boot::Object.send(f , @context)
end end
[:putstring,:putint,:fibo,:exit].each do |f| [:putstring,:putint,:fibo,:exit].each do |f|
#puts "Boot Kernel::#{f}" #puts "Boot Kernel::#{f}"
obj.add_method Crystal::Kernel.send(f , @context) obj.add_method_definition Crystal::Kernel.send(f , @context)
end end
obj = get_or_create_class :String obj = get_or_create_class :String
[:get , :set].each do |f| [:get , :set].each do |f|
#puts "Boot String::#{f}" #puts "Boot String::#{f}"
obj.add_method Boot::String.send(f , @context) obj.add_method_definition Boot::String.send(f , @context)
end end
end end

View File

@ -6,7 +6,7 @@ module Boot
# set/get instance variable use it. # set/get instance variable use it.
# This is just a placeholder, as we code this in ruby, but the instance methods need the definition before. # This is just a placeholder, as we code this in ruby, but the instance methods need the definition before.
def index_of context , name = Virtual::Integer def index_of context , name = Virtual::Integer
index_function = Virtual::Method.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer ) index_function = Virtual::MethodDefinition.new(:index_of , Virtual::Reference , [Virtual::Reference] , Virtual::Integer )
return index_function return index_function
end end
@ -23,7 +23,7 @@ module Boot
# end # end
# The at_index is just "below" the api, something we need but don't want to expose, so we can't code the above in ruby # The at_index is just "below" the api, something we need but don't want to expose, so we can't code the above in ruby
def _get_instance_variable context , name = Virtual::Integer def _get_instance_variable context , name = Virtual::Integer
get_function = Virtual::Method.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery ) get_function = Virtual::MethodDefinition.new(:_get_instance_variable , [ Virtual::Reference ] , Virtual::Reference ,Virtual::Mystery )
return get_function return get_function
me = get_function.receiver me = get_function.receiver
var_name = get_function.args.first var_name = get_function.args.first
@ -44,7 +44,7 @@ module Boot
end end
def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer ) def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer )
set_function = Virtual::Method.new(:_set_instance_variable ,[Virtual::Reference ,Virtual::Reference], Virtual::Reference ,Virtual::Mystery ) set_function = Virtual::MethodDefinition.new(:_set_instance_variable ,[Virtual::Reference ,Virtual::Reference], Virtual::Reference ,Virtual::Mystery )
return set_function return set_function
receiver set_function receiver set_function
me = set_function.receiver me = set_function.receiver

View File

@ -2,11 +2,11 @@ module Boot
class String class String
module ClassMethods module ClassMethods
def get context , index = Virtual::Integer def get context , index = Virtual::Integer
get_function = Virtual::Method.new(:get , [ Virtual::Integer] , Virtual::Integer , Virtual::Integer ) get_function = Virtual::MethodDefinition.new(:get , [ Virtual::Integer] , Virtual::Integer , Virtual::Integer )
return get_function return get_function
end end
def set context , index = Virtual::Integer , char = Virtual::Integer def set context , index = Virtual::Integer , char = Virtual::Integer
set_function = Virtual::Method.new(:set , [Virtual::Integer, Virtual::Integer] , Virtual::Integer ,Virtual::Integer ) set_function = Virtual::MethodDefinition.new(:set , [Virtual::Integer, Virtual::Integer] , Virtual::Integer ,Virtual::Integer )
return set_function return set_function
end end
end end

View File

@ -7,7 +7,7 @@ module Crystal
# As we write before we recurse (save a push) we write the number backwards # As we write before we recurse (save a push) we write the number backwards
# arguments: string address , integer # arguments: string address , integer
def self.utoa context def self.utoa context
utoa_function = Virtual::Method.new(:utoa , [ Virtual::Integer ] , Virtual::Integer ,Virtual::Integer ) utoa_function = Virtual::MethodDefinition.new(:utoa , [ Virtual::Integer ] , Virtual::Integer ,Virtual::Integer )
return utoa_function return utoa_function
str_addr = utoa_function.receiver str_addr = utoa_function.receiver
number = utoa_function.args.first number = utoa_function.args.first
@ -25,7 +25,7 @@ module Crystal
end end
def self.putint context def self.putint context
putint_function = Virtual::Method.new(:putint , [] , Virtual::Integer ,Virtual::Integer ) putint_function = Virtual::MethodDefinition.new(:putint , [] , Virtual::Integer ,Virtual::Integer )
return putint_function return putint_function
buffer = Virtual::StringConstant.new(" ") # create a buffer buffer = Virtual::StringConstant.new(" ") # create a buffer
context.object_space.add_object buffer # and save it (function local variable: a no no) context.object_space.add_object buffer # and save it (function local variable: a no no)
@ -52,7 +52,7 @@ module Crystal
# a hand coded version of the fibonachi numbers # a hand coded version of the fibonachi numbers
# not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html # not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html
def self.fibo context def self.fibo context
fibo_function = Virtual::Method.new(:fibo , [] , Virtual::Integer ,Virtual::Integer ) fibo_function = Virtual::MethodDefinition.new(:fibo , [] , Virtual::Integer ,Virtual::Integer )
return fibo_function return fibo_function
result = fibo_function.return_type result = fibo_function.return_type
int = fibo_function.receiver int = fibo_function.receiver

View File

@ -1,7 +1,7 @@
module Crystal module Crystal
module Kernel module Kernel
def self.putstring context def self.putstring context
function = Virtual::Method.new(:putstring , [] ) function = Virtual::MethodDefinition.new(:putstring , [] )
return function return function
ret = Virtual::RegisterMachine.instance.write_stdout(function) ret = Virtual::RegisterMachine.instance.write_stdout(function)
function.set_return ret function.set_return ret

View File

@ -1,7 +1,7 @@
module Crystal module Crystal
module Kernel module Kernel
def self.exit context def self.exit context
function = Virtual::Method.new(:exit , [] , Virtual::Integer) function = Virtual::MethodDefinition.new(:exit , [] , Virtual::Integer)
return function return function
ret = Virtual::RegisterMachine.instance.exit(function) ret = Virtual::RegisterMachine.instance.exit(function)
function.set_return ret function.set_return ret

View File

@ -29,7 +29,7 @@ module Virtual
end end
# following classes are stubs. currently in brainstorming mode, so anything may change anytime # following classes are stubs. currently in brainstorming mode, so anything may change anytime
class MethodEnter < Instruction class MethodDefinitionEnter < Instruction
end end
class FrameGet < Instruction class FrameGet < Instruction

View File

@ -52,7 +52,7 @@ end
require_relative "list" require_relative "list"
require_relative "instruction" require_relative "instruction"
require_relative "method" require_relative "method_definition"
require_relative "frame" require_relative "frame"
require_relative "value" require_relative "value"
require_relative "type" require_relative "type"

View File

@ -9,15 +9,15 @@ module Virtual
# known local variable names # known local variable names
# temp variables (numbered) # temp variables (numbered)
# #
class Method < Virtual::Object class MethodDefinition < Virtual::Object
#return the main function (the top level) into which code is compiled #return the main function (the top level) into which code is compiled
def Method.main def MethodDefinition.main
Method.new(:main , [] , Virtual::SelfReference ) MethodDefinition.new(:main , [] , Virtual::SelfReference )
end end
def attributes def attributes
[:name , :args , :receiver , :return_type , :start] [:name , :args , :receiver , :return_type , :start]
end end
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodEnter.new def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodDefinitionEnter.new
@name = name.to_sym @name = name.to_sym
@args = args @args = args
@locals = [] @locals = []

View File

@ -9,7 +9,7 @@ def foo(x)
5 5
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::IntegerConstant.new(5),Virtual::MethodEnter.new(nil))] @output = [Virtual::MethodDefinition.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::IntegerConstant.new(5),Virtual::MethodDefinitionEnter.new(nil))]
check check
end end
@ -19,7 +19,7 @@ def String.length(x)
@length @length
end end
HERE HERE
@output = [Virtual::Method.new(:length,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Boot::BootClass.new(:String,:Object),Virtual::Return.new(Virtual::Mystery.new()),Virtual::MethodEnter.new(Virtual::ObjectGet.new(:length,nil)))] @output = [Virtual::MethodDefinition.new(:length,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Boot::BootClass.new(:String,:Object),Virtual::Return.new(Virtual::Mystery.new()),Virtual::MethodDefinitionEnter.new(Virtual::ObjectGet.new(:length,nil)))]
check check
end end
@ -30,7 +30,7 @@ def foo(x)
2 + 5 2 + 5
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5),Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil)))))] @output = [Virtual::MethodDefinition.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodDefinitionEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5),Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil)))))]
check check
end end
@ -40,7 +40,7 @@ def foo()
2 + 5 2 + 5
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodEnter.new(Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil))))] @output = [Virtual::MethodDefinition.new(:foo,[],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodDefinitionEnter.new(Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil))))]
check check
end end
@ -65,7 +65,7 @@ def retvar(n)
return i return i
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])] @output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
check check
end end
@ -79,7 +79,7 @@ def retvar(n)
end end
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])] @output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
check check
end end
@ -92,7 +92,7 @@ def retvar(n)
end end
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])] @output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
check check
end end
@ -106,7 +106,7 @@ def fibonaccit(n)
end end
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])] @output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
check check
end end
@ -124,7 +124,7 @@ def fibonaccit(n)
end end
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)])] @output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
check check
end end
end end

View File

@ -12,7 +12,7 @@ module VirtualHelper
syntax = parser.parse_with_debug(@string_input) syntax = parser.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax) parts = Parser::Transform.new.apply(syntax)
machine = Virtual::Machine.new machine = Virtual::Machine.new
main = Virtual::Method.main main = Virtual::MethodDefinition.main
expressions = parts.compile(machine.frame , main ) expressions = parts.compile(machine.frame , main )
assert_equal @output , expressions assert_equal @output , expressions
end end