moving from collect to each

when iterating over tree.
Much cleaner, less hokuspukus methods that are noops

Mom is coming back out, but not linked yet
This commit is contained in:
Torsten Ruger 2018-03-15 17:22:56 +05:30
parent 3702411043
commit 3247c2036c
10 changed files with 37 additions and 35 deletions

View File

@ -28,10 +28,6 @@ module Common
@statements << o @statements << o
self self
end end
def collect(arr)
@statements.each { |s| s.collect(arr) }
super
end
def add_array(a) def add_array(a)
@statements += a @statements += a
end end

View File

@ -31,10 +31,6 @@ module Vool
raise self.class.name raise self.class.name
end end
def each()
yield self
end
def to_mom( _ ) def to_mom( _ )
# temporary warning to find unimplemented kids # temporary warning to find unimplemented kids
raise "Not implemented for #{self}" raise "Not implemented for #{self}"

View File

@ -14,19 +14,17 @@ module Vool
# compilation to the next layer, mom # compilation to the next layer, mom
# context coming in for class is nil, also for methods, henceafter a method is passed down # context coming in for class is nil, also for methods, henceafter a method is passed down
def to_mom( _ ) def to_mom( _ )
methods = @body.statements.collect { |meth| meth.to_mom( nil ) } @body.to_mom(nil)
Mom::Statements.new(methods)
end end
def each() def each(&block)
@body.collect(arr) block.call(self)
super @body.each(&block)
end end
def create_objects def create_objects
create_class_object create_class_object
body.collect([]).each {|node| node.set_class(@clazz) } self.each {|node| node.create_objects(@clazz) if node.is_a?(MethodStatement) }
body.create_objects
end end
def create_class_object def create_class_object
@ -36,10 +34,11 @@ module Vool
#existing class, don't overwrite type (parfait only?) #existing class, don't overwrite type (parfait only?)
else else
@clazz = Parfait.object_space.create_class(@name , @super_class_name ) @clazz = Parfait.object_space.create_class(@name , @super_class_name )
vars = []
@body.collect([]).each { |node| node.add_ivar(vars) }
ivar_hash = {} ivar_hash = {}
vars.each { |var| ivar_hash[var] = :Object } self.each do |node|
next unless node.is_a?(InstanceVariable) or node.is_a?(IvarAssignment)
ivar_hash[node.name] = :Object
end
@clazz.set_instance_type( Parfait::Type.for_hash( @clazz , ivar_hash ) ) @clazz.set_instance_type( Parfait::Type.for_hash( @clazz , ivar_hash ) )
end end
end end

View File

@ -16,16 +16,18 @@ module Vool
@body.to_mom(method) @body.to_mom(method)
end end
def collect(arr) def each(&block)
@body.collect(arr) block.call(self)
super @body.each(&block)
end end
def normalize def normalize
MethodStatement.new( @name , @args , @body.normalize) MethodStatement.new( @name , @args , @body.normalize)
end end
def create_objects def create_objects(clazz)
@clazz = clazz
raise "no class" unless clazz
args_type = make_type args_type = make_type
frame_type = make_frame frame_type = make_frame
method = Parfait::VoolMethod.new(name , args_type , frame_type , body ) method = Parfait::VoolMethod.new(name , args_type , frame_type , body )
@ -46,9 +48,10 @@ module Vool
def make_frame def make_frame
type_hash = {} type_hash = {}
vars = [] @body.each do |node|
@body.each([]).each { |node| node.add_local(vars) } next unless node.is_a?(LocalVariable) or node.is_a?(LocalAssignment)
vars.each { |var| type_hash[var] = :Object } type_hash[node.name] = :Object
end
Parfait::NamedList.type_for( type_hash ) Parfait::NamedList.type_for( type_hash )
end end

View File

@ -18,12 +18,16 @@ module Vool
@dynamic = nil @dynamic = nil
end end
def collect(arr) def normalize
@receiver.collect(arr) SendStatement.new(@name, @receiver , @arguments)
@arguments.each do |arg| end
arg.collect(arr)
def each(&block)
block.call(self)
block.call(@receiver)
@arguments.each do |arg|
block.call(arg)
end end
super
end end
# A Send breaks down to 2 steps: # A Send breaks down to 2 steps:

View File

@ -12,6 +12,11 @@ module Vool
@statements.each{ |s| s.create_objects } @statements.each{ |s| s.create_objects }
end end
def each(&block)
block.call(self)
@statements.each{|a| a.each(block)}
end
end end
class ScopeStatement < Statements class ScopeStatement < Statements

View File

@ -6,7 +6,7 @@ module Vool
def self.ruby_to_vool( ruby_source ) def self.ruby_to_vool( ruby_source )
statements = RubyCompiler.compile( ruby_source ) statements = RubyCompiler.compile( ruby_source )
statements = statements.normalize statements = statements.normalize
#statements.create_objects statements.create_objects
statements statements
end end
def self.ruby_to_mom(source) def self.ruby_to_mom(source)

View File

@ -23,7 +23,7 @@ module MomCompile
def compile_first_method input def compile_first_method input
lst = Vool::VoolCompiler.ruby_to_vool as_test_main( input ) lst = Vool::VoolCompiler.ruby_to_vool as_test_main( input )
assert_equal Parfait::Class , lst.clazz.class , input assert_equal Parfait::Class , lst.clazz.class , lst
@method = lst.clazz.get_method(:main) @method = lst.clazz.get_method(:main)
assert_equal Parfait::VoolMethod , @method.class assert_equal Parfait::VoolMethod , @method.class
res = lst.to_mom( nil ) res = lst.to_mom( nil )

View File

@ -5,10 +5,10 @@ module Vool
assert Array != @stats.class , @stats assert Array != @stats.class , @stats
end end
def test_class_compiles def test_class_compiles
assert_equal Mom::MessageSetup , @stats.first.class , @stats assert_equal Mom::MessageSetup , @stats.class , @stats
end end
def test_two_instructions_are_returned def test_two_instructions_are_returned
assert_equal 3 , @stats.length assert_equal 3 , @stats.length , @stats.to_rxf
end end
def test_receiver_move_class def test_receiver_move_class
assert_equal Mom::ArgumentTransfer, @stats[1].class assert_equal Mom::ArgumentTransfer, @stats[1].class

View File

@ -8,8 +8,7 @@ module Vool
def setup def setup
Risc.machine.boot Risc.machine.boot
@stats = compile_first_method( "'5'.get_internal_byte(1)").first @stats = compile_first_method( "'5'.get_internal_byte(1)")
@first = @stats.first
end end
def receiver def receiver
[Mom::StringConstant , "5"] [Mom::StringConstant , "5"]