diff --git a/lib/mom/class_compiler.rb b/lib/mom/class_compiler.rb new file mode 100644 index 00000000..44516d0c --- /dev/null +++ b/lib/mom/class_compiler.rb @@ -0,0 +1,10 @@ +module Mom + class ClassCompiler + attr_reader :clazz , :methods + + def initialize(clazz , methods) + @clazz = clazz + @methods = methods + end + end +end diff --git a/lib/mom/mom.rb b/lib/mom/mom.rb index 429851a6..54bb67ab 100644 --- a/lib/mom/mom.rb +++ b/lib/mom/mom.rb @@ -15,3 +15,4 @@ module Mom end require_relative "instruction/instruction.rb" +require_relative "class_compiler" diff --git a/lib/parfait/vool_method.rb b/lib/parfait/vool_method.rb index eef8fee0..43264ecf 100644 --- a/lib/parfait/vool_method.rb +++ b/lib/parfait/vool_method.rb @@ -20,6 +20,7 @@ module Parfait raise "args_type must be type" unless args_type.is_a?(Parfait::Type) raise "frame_type must be type" unless frame_type.is_a?(Parfait::Type) raise "source must be vool" unless source.is_a?(Vool::Statement) + raise "Empty bod" if(source.is_a?(Vool::Statements) and source.empty?) end def create_typed_method( type ) @@ -27,6 +28,11 @@ module Parfait type.create_method( @name , @args_type , @frame_type) end + def compile_to_mom(for_type) + typed_method = create_typed_method(for_type) + source.to_mom( typed_method ) + end + def compile_to_risc(for_type) typed_method = create_typed_method(for_type) head = source.to_mom( typed_method ) diff --git a/lib/vool/class_statement.rb b/lib/vool/class_statement.rb index c9ff8bab..7c55f82c 100644 --- a/lib/vool/class_statement.rb +++ b/lib/vool/class_statement.rb @@ -24,12 +24,12 @@ module Vool def to_mom( _ ) create_class_object - mom = [] + methods = [] body.statements.each do |node| raise "Only methods for now #{node}" unless node.is_a?(MethodStatement) - mom << node.to_mom(@clazz) + methods << node.to_mom(@clazz) end - mom + Mom::ClassCompiler.new(@clazz , methods) end def each(&block) diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index 0632a0b2..720c5d69 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -31,8 +31,9 @@ module Vool # create mom instructions def to_mom( method ) raise "Empty list ? #{statements.length}" if empty? - flat = @statements.shift.to_mom(method) - while( nekst = @statements.shift ) + stats = @statements.dup + flat = stats.shift.to_mom(method) + while( nekst = stats.shift ) flat.append nekst.to_mom(method) end flat diff --git a/test/risc/test_risc_value.rb b/test/risc/test_risc_value.rb index 20038feb..419279b0 100644 --- a/test/risc/test_risc_value.rb +++ b/test/risc/test_risc_value.rb @@ -10,6 +10,7 @@ module Risc class TestRegisterValue < MiniTest::Test def setup + Parfait.boot! Risc.machine.boot @r0 = RegisterValue.new(:r0 , :Message) @r1 = RegisterValue.new(:r1 , :Space) diff --git a/test/support/compiling.rb b/test/support/compiling.rb index 6037f257..2fe2931d 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -21,17 +21,17 @@ end module MomCompile include ScopeHelper - def compile_first_method( input ) - # works a lot like Vool.ruby_to_vool - # but here we return the intermediate mom instructions that are otherwise not available - statements = RubyX::RubyCompiler.compile as_test_main( input ) - statements = statements.normalize + def compile_mom(input) + statements = RubyX::RubyXCompiler.ruby_to_vool input res = statements.to_mom(nil) assert_equal Parfait::Class , statements.clazz.class , statements @method = statements.clazz.get_method(:main) assert_equal Parfait::VoolMethod , @method.class - #puts "#{res.class}" - res.first + res + end + def compile_first_method( input ) + res = compile_mom( as_test_main( input )) + res.clazz.instance_methods.first.compile_to_mom(res.clazz.instance_type) end def check_array( should , is ) diff --git a/test/vool/send/test_send_args_send.rb b/test/vool/send/test_send_args_send.rb index 4cc6d8f4..40d616c9 100644 --- a/test/vool/send/test_send_args_send.rb +++ b/test/vool/send/test_send_args_send.rb @@ -6,6 +6,7 @@ module Vool include Mom def setup + Parfait.boot! Risc.machine.boot @ins = compile_first_method( "a = main(1 + 2)" ) end diff --git a/test/vool/send/test_send_cached_simple.rb b/test/vool/send/test_send_cached_simple.rb index fcbdd67d..f191209b 100644 --- a/test/vool/send/test_send_cached_simple.rb +++ b/test/vool/send/test_send_cached_simple.rb @@ -6,6 +6,7 @@ module Vool include Mom def setup + Parfait.boot! Risc.machine.boot @ins = compile_first_method( "a = 5; a.div4") end diff --git a/test/vool/test_class_statement.rb b/test/vool/test_class_statement.rb new file mode 100644 index 00000000..24505db9 --- /dev/null +++ b/test/vool/test_class_statement.rb @@ -0,0 +1,17 @@ + +require_relative "helper" + +module Vool + class TestClassStatement < MiniTest::Test + include MomCompile + + def setup + Parfait.boot! + @ins = compile_mom( "class Test ; def main(); return 1; end; end;") + end + + def test_return + assert_equal Mom::ClassCompiler , @ins.class + end + end +end diff --git a/test/vool/test_return.rb b/test/vool/test_return.rb index 0d66e4ab..b6e5de12 100644 --- a/test/vool/test_return.rb +++ b/test/vool/test_return.rb @@ -43,6 +43,7 @@ module Vool include Mom def setup + Parfait.boot! Risc.machine.boot @ins = compile_first_method( "return 5.div4") end