2017-04-01 15:27:32 +02:00
|
|
|
module Vool
|
2017-04-08 16:22:53 +02:00
|
|
|
class MethodStatement < Statement
|
|
|
|
attr_reader :name, :args , :body , :clazz
|
2017-04-01 15:27:32 +02:00
|
|
|
|
|
|
|
def initialize( name , args , body)
|
2017-04-12 10:52:23 +02:00
|
|
|
@name , @args , @body = name , args , body
|
|
|
|
unless( body.is_a?(ScopeStatement))
|
|
|
|
@body = ScopeStatement.new([])
|
|
|
|
@body.statements << body if body
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# compile to mom instructions. methods themselves do no result in instructions (yet)
|
|
|
|
# instead the resulting instruction tree is saved into the method object that
|
|
|
|
# represents the method
|
|
|
|
def to_mom( _ )
|
|
|
|
method = @clazz.get_method( @name )
|
|
|
|
@body.to_mom(method)
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
|
2017-04-08 11:10:42 +02:00
|
|
|
def collect(arr)
|
2017-04-08 16:22:53 +02:00
|
|
|
@body.collect(arr)
|
2017-04-08 11:10:42 +02:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-04-08 16:22:53 +02:00
|
|
|
def set_class(clazz)
|
|
|
|
@clazz = clazz
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_objects
|
|
|
|
args_type = make_type
|
|
|
|
locals_type = make_locals
|
2017-08-31 15:11:06 +02:00
|
|
|
method = Vool::VoolMethod.new(name , args_type , locals_type , body )
|
2017-04-08 16:22:53 +02:00
|
|
|
@clazz.add_method( method )
|
|
|
|
# compile_methods(clazz,methods)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def make_type( )
|
|
|
|
type_hash = {}
|
2017-04-09 09:14:28 +02:00
|
|
|
@args.each {|arg| type_hash[arg] = :Object }
|
2017-04-08 16:22:53 +02:00
|
|
|
Parfait::NamedList.type_for( type_hash )
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_locals
|
|
|
|
type_hash = {}
|
|
|
|
vars = []
|
2017-04-08 18:20:11 +02:00
|
|
|
@body.collect([]).each { |node| node.add_local(vars) }
|
|
|
|
vars.each { |var| type_hash[var] = :Object }
|
2017-04-08 16:22:53 +02:00
|
|
|
Parfait::NamedList.type_for( type_hash )
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_methods(clazz , body)
|
|
|
|
methods = Passes::MethodCollector.new.collect(body)
|
|
|
|
methods.each do |method|
|
|
|
|
clazz.add_method( method )
|
|
|
|
normalizer = Passes::Normalizer.new(method)
|
|
|
|
method.normalize_source { |sourc| normalizer.process( sourc ) }
|
|
|
|
end
|
|
|
|
methods
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile_methods(clazz , methods)
|
|
|
|
methods.each do |method|
|
|
|
|
code = Passes::MethodCompiler.new(method).get_code
|
2017-09-11 13:21:57 +02:00
|
|
|
typed_method = method.create_parfait_method(clazz.instance_type)
|
2017-04-08 16:22:53 +02:00
|
|
|
Vm::MethodCompiler.new( typed_method ).init_method.process( code )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-01 15:27:32 +02:00
|
|
|
end
|
|
|
|
end
|