make method creation class methods in MethodCompiler

and pass the wish to use main explicitly, which is really a test
feature
This commit is contained in:
Torsten Ruger 2017-01-17 21:23:58 +02:00
parent 0c64e367d5
commit cd211f970f
8 changed files with 21 additions and 22 deletions

View File

@ -11,7 +11,7 @@ module Register
def compiler_for( type , method_name , extra_args = {}) def compiler_for( type , method_name , extra_args = {})
args = {:index => :Integer}.merge( extra_args ) args = {:index => :Integer}.merge( extra_args )
Vm::MethodCompiler.new.create_method(type , method_name , args ).init_method Vm::MethodCompiler.create_method(type , method_name , args ).init_method
end end
# Load the value # Load the value

View File

@ -6,18 +6,18 @@ module Register
include AST::Sexp include AST::Sexp
def mod4 context def mod4 context
compiler = Vm::MethodCompiler.new.create_method(:Integer,:mod4 ).init_method compiler = Vm::MethodCompiler.create_method(:Integer,:mod4 ).init_method
return compiler.method return compiler.method
end end
def putint context def putint context
compiler = Vm::MethodCompiler.new.create_method(:Integer,:putint ).init_method compiler = Vm::MethodCompiler.create_method(:Integer,:putint ).init_method
return compiler.method return compiler.method
end end
def div10 context def div10 context
s = "div_10" s = "div_10"
compiler = Vm::MethodCompiler.new.create_method(:Integer,:div10 ).init_method compiler = Vm::MethodCompiler.create_method(:Integer,:div10 ).init_method
me = compiler.process( Vm::Tree::KnownName.new( :self) ) me = compiler.process( Vm::Tree::KnownName.new( :self) )
tmp = compiler.process( Vm::Tree::KnownName.new( :self) ) tmp = compiler.process( Vm::Tree::KnownName.new( :self) )
q = compiler.process( Vm::Tree::KnownName.new( :self) ) q = compiler.process( Vm::Tree::KnownName.new( :self) )

View File

@ -6,7 +6,7 @@ module Register
# it isn't really a function, ie it is jumped to (not called), exits and may not return # it isn't really a function, ie it is jumped to (not called), exits and may not return
# so it is responsible for initial setup # so it is responsible for initial setup
def __init__ context def __init__ context
compiler = Vm::MethodCompiler.new.create_method(:Kernel,:__init__ ) compiler = Vm::MethodCompiler.create_method(:Kernel,:__init__ )
new_start = Register.label("__init__ start" , "__init__" ) new_start = Register.label("__init__ start" , "__init__" )
compiler.method.set_instructions( new_start) compiler.method.set_instructions( new_start)
compiler.set_current new_start compiler.set_current new_start
@ -29,7 +29,7 @@ module Register
end end
def exit context def exit context
compiler = Vm::MethodCompiler.new.create_method(:Kernel,:exit ).init_method compiler = Vm::MethodCompiler.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 = Vm::MethodCompiler.new.create_method(:Space , :main ).init_method compiler = Vm::MethodCompiler.create_method(:Space , :main ).init_method
return compiler.method return compiler.method
end end

View File

@ -7,7 +7,7 @@ module Register
include CompileHelper include CompileHelper
def putstring context def putstring context
compiler = Vm::MethodCompiler.new.create_method(:Word , :putstring ).init_method compiler = Vm::MethodCompiler.create_method(:Word , :putstring ).init_method
compiler.add_slot_to_reg( "putstring" , :message , :receiver , :new_message ) compiler.add_slot_to_reg( "putstring" , :message , :receiver , :new_message )
index = Parfait::Word.get_length_index index = Parfait::Word.get_length_index
reg = RegisterValue.new(:r2 , :Integer) reg = RegisterValue.new(:r2 , :Integer)

View File

@ -56,7 +56,7 @@ module Vm
# Helper function to create a new compiler and compie the statement(s) # Helper function to create a new compiler and compie the statement(s)
# Statement must be and AST::Node as generated by s expressions # Statement must be and AST::Node as generated by s expressions
def self.compile_ast( statement ) def self.compile_ast( statement )
compiler = MethodCompiler.new compiler = MethodCompiler.new(:main)
code = Vm.ast_to_code statement code = Vm.ast_to_code statement
compiler.process code compiler.process code
end end
@ -66,15 +66,15 @@ module Vm
include Vm.const_get( mod.camelize ) include Vm.const_get( mod.camelize )
end end
def initialize( method = nil ) def initialize( method )
@regs = [] @regs = []
if method if method == :main
@method = method
@type = method.for_type
else
@type = Parfait.object_space.get_type() @type = Parfait.object_space.get_type()
@method = @type.get_method( :main ) @method = @type.get_method( :main )
@method = @type.create_method( :main ,{}) unless @method @method = @type.create_method( :main ,{}) unless @method
else
@method = method
@type = method.for_type
end end
@current = @method.instructions @current = @method.instructions
end end
@ -107,7 +107,7 @@ module Vm
# 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 self.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 = Parfait.object_space.get_class_by_name! class_name clazz = Parfait.object_space.get_class_by_name! class_name
create_method_for( clazz.instance_type , method_name , args) create_method_for( clazz.instance_type , method_name , args)
@ -118,13 +118,12 @@ module Vm
# args a hash that will be converted to a type # args a hash that will be converted to a type
# the created method is set as the current and the given type too # the created method is set as the current and the given type too
# return the compiler (for chaining) # return the compiler (for chaining)
def create_method_for( type , method_name , args ) def self.create_method_for( type , method_name , args )
@type = type
raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type
raise "Args must be Hash #{args}" unless args.is_a?(Hash) raise "Args must be Hash #{args}" unless args.is_a?(Hash)
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
@method = type.create_method( method_name , args) method = type.create_method( method_name , args)
self self.new(method)
end end
# add method entry and exit code. Mainly save_return for the enter and # add method entry and exit code. Mainly save_return for the enter and

View File

@ -22,7 +22,7 @@ require 'rubyx'
module Compiling module Compiling
def clean_compile(clazz_name , method_name , args , statements) def clean_compile(clazz_name , method_name , args , statements)
compiler = Vm::MethodCompiler.new.create_method(clazz_name,method_name,args ).init_method compiler = Vm::MethodCompiler.create_method(clazz_name,method_name,args ).init_method
compiler.process( Vm.ast_to_code( statements ) ) compiler.process( Vm.ast_to_code( statements ) )
end end

View File

@ -41,7 +41,7 @@ module Register
end end
def check_nil def check_nil
assert @expect , "No output given" assert @expect , "No output given"
compiler = Vm::MethodCompiler.new compiler = Vm::MethodCompiler.new(:main)
code = Vm.ast_to_code( @input ) code = Vm.ast_to_code( @input )
assert code.to_s , @input assert code.to_s , @input
produced = compiler.process( code ) produced = compiler.process( code )