diff --git a/lib/mom/mom_compiler.rb b/lib/mom/mom_compiler.rb index fa42e3da..3ac3b552 100644 --- a/lib/mom/mom_compiler.rb +++ b/lib/mom/mom_compiler.rb @@ -6,6 +6,11 @@ module Mom @method_compilers = Risc::Builtin.boot_functions + compilers end + # collects constants from all compilers into one array + def constants + @method_compilers.inject([]){|sum ,comp| sum + comp.constants } + end + # Translate code to whatever cpu is specified. # Currently only :arm and :interpret # @@ -15,7 +20,7 @@ module Mom platform_sym = platform_sym.to_s.capitalize platform = Risc::Platform.for(platform_sym) assemblers = translate_methods( platform.translator ) - Risc::Linker.new(platform , assemblers) + Risc::Linker.new(platform , assemblers , constants) end # go through all methods and translate them to cpu, given the translator @@ -35,7 +40,7 @@ module Mom cpu_instructions << cpu if cpu nekst = nekst.next end - Risc::Assembler.new(compiler.method , cpu_instructions , compiler.constants) + Risc::Assembler.new(compiler.method , cpu_instructions ) end end diff --git a/lib/risc/assembler.rb b/lib/risc/assembler.rb index a10705e2..3841437e 100644 --- a/lib/risc/assembler.rb +++ b/lib/risc/assembler.rb @@ -1,10 +1,10 @@ module Risc class Assembler - attr_reader :method , :instructions , :constants - def initialize( method , instructions, constants) + attr_reader :method , :instructions + + def initialize( method , instructions) @method = method @instructions = instructions - @constants = constants total = instructions.total_byte_length / 4 + 1 method.binary.extend_to( total ) end diff --git a/lib/risc/linker.rb b/lib/risc/linker.rb index 4bced8ce..792787a7 100644 --- a/lib/risc/linker.rb +++ b/lib/risc/linker.rb @@ -13,7 +13,7 @@ module Risc include Util::Logging log_level :info - def initialize(platform , assemblers) + def initialize(platform , assemblers , constants) if(platform.is_a?(Symbol)) platform = platform.to_s.capitalize platform = Risc::Platform.for(platform) @@ -21,7 +21,7 @@ module Risc raise "Platform must be platform, not #{platform.class}" unless platform.is_a?(Platform) @platform = platform @assemblers = assemblers - @constants = [] + @constants = constants @cpu_init = cpu_init_init end diff --git a/test/mom/test_mom_compiler.rb b/test/mom/test_mom_compiler.rb index ac6aec4f..3c58504a 100644 --- a/test/mom/test_mom_compiler.rb +++ b/test/mom/test_mom_compiler.rb @@ -6,7 +6,7 @@ module Mom def setup Parfait.boot! - @comp = compile_mom( "class Test ; def main(); return 1; end; end;") + @comp = compile_mom( "class Test ; def main(); return 'Hi'; end; end;") end def test_class @@ -15,6 +15,12 @@ module Mom def test_compilers assert_equal 24 , @comp.method_compilers.length end + def test_returns_constants + assert_equal Array , @comp.constants.class + end + def test_has_constant + assert_equal "Hi" , @comp.constants.first.to_string + end def test_has_translate assert @comp.translate(:interpreter) end diff --git a/test/mom/test_mom_compiler1.rb b/test/mom/test_mom_compiler1.rb index 4cd399a9..917d8add 100644 --- a/test/mom/test_mom_compiler1.rb +++ b/test/mom/test_mom_compiler1.rb @@ -6,13 +6,22 @@ module Mom def setup Parfait.boot! - @comp = compile_mom( "class Test ; def main(); return 1; end; end;") + @comp = compile_mom( "class Test ; def main(); return 'Hi'; end; end;") @linker = @comp.translate(:interpreter) end def test_translate_class assert_equal Risc::Linker , @linker.class end + def test_linker_has_constants + assert_equal Array , @linker.constants.class + end + def test_linker_constants_not_empty + assert !@linker.constants.empty? + end + def test_linker_constants_contains_hi + assert @linker.constants.include?("Hi") + end def test_translate_platform assert_kind_of Risc::Platform , @linker.platform end