change args argument to create method to be hash

was array and didn’t carry types and names
This commit is contained in:
Torsten Ruger 2015-11-11 20:41:02 +02:00
parent 351a747bfd
commit 6137833140
7 changed files with 20 additions and 69 deletions

View File

@ -152,7 +152,7 @@ module Register
@space.get_class_by_name(:Word).add_instance_method Builtin::Word.send(:putstring , nil) @space.get_class_by_name(:Word).add_instance_method Builtin::Word.send(:putstring , nil)
obj = @space.get_class_by_name(:Integer) obj = @space.get_class_by_name(:Integer)
[:putint,:fibo ].each do |f| [:mod , :putint].each do |f|
obj.add_instance_method Builtin::Integer.send(f , nil) obj.add_instance_method Builtin::Integer.send(f , nil)
end end
end end

View File

@ -30,65 +30,15 @@ module Register
# # return utoa_function # # return utoa_function
# end # end
def putint context def mod context
compiler = Soml::Compiler.new.create_method(:Integer,:putint , [] ).init_method compiler = Soml::Compiler.new.create_method(:Integer,:mod , {:Integer => :by} ).init_method
return compiler.method
end
def putint context
compiler = Soml::Compiler.new.create_method(:Integer,:putint ).init_method
return compiler.method return compiler.method
# buffer = Parfait::Word.new(" ") # create a buffer
# context.object_space.add_object buffer # and save it (function local variable: a no no)
# int = putint_function.receiver
# moved_int = putint_function.new_local
# utoa = context.object_space.get_class_by_name(:Object).resolve_method(:utoa)
# putint_function.instance_eval do
# mov( moved_int , int ) # move arg up
# add( int , buffer ,nil ) # string to write to (add string address to pc)
# add( int , int , buffer.length - 3) # 3 for good measure , ahem.
# call( utoa )
# after = new_block("after_call")
# insert_at after
# # And now we "just" have to print it, using the write_stdout
# add( int , buffer , nil ) # string to write to
# mov( moved_int , buffer.length )
# end
# RegisterMachine.instance.write_stdout(putint_function)
# putint_function
end end
# testing method, hand coded fibo, expects arg in receiver_register
# result comes in return_register
# 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
def fibo context
compiler = Soml::Compiler.new.create_method(:Integer,:fibo , [] ).init_method
return compiler.method
# int = fibo_function.receiver
#
# last = fibo_function.new_block("return")
#
# f1 = fibo_function.new_local
# f2 = fibo_function.new_local
#
# fibo_function.instance_eval do
# cmp int , 1
# mov( result, int , condition_code: :le)
# ble( last ) #branch to return, rather than return (as the original)
# mov f1 , 1 #set up initial values
# mov f2 , 0
# end
#
# loop = fibo_function.new_block("loop")
# fibo_function.insert_at loop
#
# fibo_function.instance_eval do #loop through
# add f1 , f1 , f2 # f1 = f1 + f2
# sub f2 , f1 , f2 # f2 = f1 -f2
# sub int , int , 1 # todo: set.. should do below cmp, but doesn't , set_update_status: 1
# cmp int , 1
# bne( loop )
# mov( result , f1 )
# end
#
# fibo_function
end
end end
extend ClassMethods extend ClassMethods
end end

View File

@ -7,7 +7,7 @@ module Register
# so it is responsible for initial setup # so it is responsible for initial setup
def __init__ context def __init__ context
source = "__init__" source = "__init__"
compiler = Soml::Compiler.new.create_method(:Kernel,:__init__ , []) compiler = Soml::Compiler.new.create_method(:Kernel,:__init__ )
# no method enter or return (automatically added), remove # no method enter or return (automatically added), remove
new_start = Label.new(source , source ) new_start = Label.new(source , source )
compiler.method.instructions = new_start compiler.method.instructions = new_start
@ -34,7 +34,7 @@ module Register
end end
def exit context def exit context
compiler = Soml::Compiler.new.create_method(:Kernel,:exit , []).init_method compiler = Soml::Compiler.new.create_method(:Kernel,:exit ).init_method
emit_syscall( compiler , :exit ) emit_syscall( compiler , :exit )
return compiler.method return compiler.method
end end

View File

@ -9,7 +9,7 @@ module Register
# main entry point, ie __init__ calls this # main entry point, ie __init__ calls this
# defined here as empty, to be redefined # defined here as empty, to be redefined
def main context def main context
compiler = Soml::Compiler.new.create_method(:Object , :main , []).init_method compiler = Soml::Compiler.new.create_method(:Object , :main ).init_method
return compiler.method return compiler.method
end end
@ -17,7 +17,7 @@ module Register
# return is stored in return_value # return is stored in return_value
# (this method returns a new method off course, like all builtin) # (this method returns a new method off course, like all builtin)
def get_internal context def get_internal context
compiler = Soml::Compiler.new.create_method(:Object , :get_internal , []).init_method compiler = Soml::Compiler.new.create_method(:Object , :get_internal , {:Integer => :index}).init_method
source = "get_internal" source = "get_internal"
#Load self by "calling" on_name #Load self by "calling" on_name
me = compiler.process( s(:name , :self) ) me = compiler.process( s(:name , :self) )
@ -34,7 +34,8 @@ module Register
# self[index] = val basically. Index is the first arg , vlaue the second # self[index] = val basically. Index is the first arg , vlaue the second
# no return # no return
def set_internal context def set_internal context
compiler = Soml::Compiler.new.create_method(:Object , :set_internal , []).init_method compiler = Soml::Compiler.new.create_method(:Object , :set_internal ,
{:Integer => :index, :Object => :value} ).init_method
source = "set_internal" source = "set_internal"
#Load self by "calling" on_name #Load self by "calling" on_name
me = compiler.process( s(:name , :self) ) me = compiler.process( s(:name , :self) )

View File

@ -3,7 +3,7 @@ module Register
module Word module Word
module ClassMethods module ClassMethods
def putstring context def putstring context
compiler = Soml::Compiler.new.create_method(:Word , :putstring , [] ).init_method compiler = Soml::Compiler.new.create_method(:Word , :putstring ).init_method
compiler.add_code Register.get_slot( "putstring" , :message , :receiver , :new_message ) compiler.add_code Register.get_slot( "putstring" , :message , :receiver , :new_message )
Kernel.emit_syscall( compiler , :putstring ) Kernel.emit_syscall( compiler , :putstring )
compiler.method compiler.method

View File

@ -52,7 +52,7 @@ module Soml
# create the method, do some checks and set it as the current method to be added to # create the method, do some checks and set it as the current method to be added to
# class_name and method_name are pretty clear, args are given as a ruby array # class_name and method_name are pretty clear, args are given as a ruby array
def create_method( class_name , method_name , args) def create_method( class_name , method_name , args = {})
raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
clazz = Register.machine.space.get_class_by_name class_name clazz = Register.machine.space.get_class_by_name class_name
raise "No such class #{class_name}" unless clazz raise "No such class #{class_name}" unless clazz
@ -68,9 +68,9 @@ module Soml
@clazz = clazz @clazz = clazz
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
arguments = [] arguments = []
args.each_with_index do | arg , index | args.each do | arg , name |
unless arg.is_a? Parfait::Variable unless arg.is_a? Parfait::Variable
arg = Parfait::Variable.new arg , "arg#{index}".to_sym arg = Parfait::Variable.new arg , name.to_sym
end end
arguments << arg arguments << arg
end end
@ -145,11 +145,11 @@ module Soml
self.new.process( parts ) self.new.process( parts )
end end
end end
def self.each_parfait def self.each_parfait
["word","class","layout","message" ,"integer", "object"].each do |o| ["word","class","layout","message" ,"integer", "object"].each do |o|
str = File.open(File.expand_path("parfait/#{o}.soml", File.dirname(__FILE__))).read str = File.open(File.expand_path("parfait/#{o}.soml", File.dirname(__FILE__))).read
syntax = Parser::Salama.new.parse_with_debug(str) syntax = Parser::Salama.new.parse_with_debug(str, reporter: Parslet::ErrorReporter::Deepest.new)
parts = Parser::Transform.new.apply(syntax) parts = Parser::Transform.new.apply(syntax)
yield parts yield parts
end end

View File

@ -38,7 +38,7 @@ module Soml
return ret return ret
end end
end end
raise "must define variable #{name} before using it" raise "must define variable '#{name}' before using it"
end end
end #module end #module