From f7dfa1c45e72ff23dad36ec4f75978152ca830c2 Mon Sep 17 00:00:00 2001
From: Torsten Ruger <torsten@villataika.fi>
Date: Sun, 1 Jul 2018 11:56:09 +0300
Subject: [PATCH] return assemblers from translation

result of translate is cpu instructions, our equivalent of assembly.
So return Assemblers for next stage
---
 lib/mom/class_compiler.rb   | 35 ++++++++++++++++++++++++-----------
 lib/vool/class_statement.rb |  6 +++---
 2 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/lib/mom/class_compiler.rb b/lib/mom/class_compiler.rb
index 25e25c86..1b185b9c 100644
--- a/lib/mom/class_compiler.rb
+++ b/lib/mom/class_compiler.rb
@@ -1,30 +1,43 @@
 module Mom
   class ClassCompiler
-    attr_reader :clazz , :methods
+    attr_reader :clazz , :method_compilers
 
-    def initialize(clazz , methods)
+    def initialize(clazz , compilers)
       @clazz = clazz
-      @methods = methods
+      @method_compilers = compilers
     end
-    
+
     # Translate code to whatever cpu is specified.
     # Currently only :arm and :interpret
     #
     # Translating means translating the initial jump
     # and then translating all methods
-    def translate( platform )
-      platform = platform.to_s.capitalize
-      @platform = Risc::Platform.for(platform)
-      translate_methods( @platform.translator )
+    def translate( platform_sym )
+      platform_sym = platform_sym.to_s.capitalize
+      platform = Risc::Platform.for(platform_sym)
+      translate_methods( platform.translator )
       #@cpu_init = risc_init.to_cpu(@platform.translator)
     end
 
     # go through all methods and translate them to cpu, given the translator
     def translate_methods(translator)
-      Parfait.object_space.get_all_methods.each do |method|
-        #log.debug "Translate method #{method.name}"
-        method.translate_cpu(translator)
+      method_compilers.collect do |compiler|
+        #log.debug "Translate method #{compiler.method.name}"
+        translate_cpu(compiler , translator)
       end
     end
+
+    def translate_cpu(compiler , translator)
+      risc = compiler.risc_instructions
+      cpu_instructions = risc.to_cpu(translator)
+      nekst = risc.next
+      while(nekst)
+        cpu = nekst.to_cpu(translator) # returning nil means no replace
+        cpu_instructions << cpu if cpu
+        nekst = nekst.next
+      end
+      Risc::Assembler.new(compiler.method , cpu_instructions)
+    end
+
   end
 end
diff --git a/lib/vool/class_statement.rb b/lib/vool/class_statement.rb
index 7c55f82c..23354e54 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
-      methods = []
+      method_compilers = []
       body.statements.each do |node|
         raise "Only methods for now #{node}" unless node.is_a?(MethodStatement)
-        methods << node.to_mom(@clazz)
+        method_compilers << node.to_mom(@clazz)
       end
-      Mom::ClassCompiler.new(@clazz , methods)
+      Mom::ClassCompiler.new(@clazz , method_compilers)
     end
 
     def each(&block)