From 9bf5eb26213f669b4fef7ceedf16296f1f0b0426 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 6 May 2015 15:15:33 +0300 Subject: [PATCH] somewhat correct that message at compile-time idea --- lib/virtual/compiled_method.rb | 36 ++++++++++++++++----------------- lib/virtual/instruction.rb | 5 ++--- lib/virtual/instructions/set.rb | 3 +-- lib/virtual/machine.rb | 2 +- lib/virtual/message.rb | 10 --------- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/lib/virtual/compiled_method.rb b/lib/virtual/compiled_method.rb index 437f4b60..0bfcd799 100644 --- a/lib/virtual/compiled_method.rb +++ b/lib/virtual/compiled_method.rb @@ -105,35 +105,35 @@ module Virtual return new_b end - - # determine whether this method has a variable by the given name # variables are locals and and arguments # used to determine if a send must be issued + # return index of the name into the message if so def has_var name name = name.to_sym - var = @arg_names.find {|a| a == name } - var = @locals.find {|a| a == name } unless var - var = @tmps.find {|a| a == name } unless var - var + index = has_arg(name) + return index if index + has_local(name) end # determine whether this method has an argument by the name def has_arg name - name = name.to_sym - var = @arg_names.find {|a| a == name } - var + @arg_names.index name.to_sym end - def set_var name , var - v = has_var name - if( v ) - v.type = var - else - v = Local.new(name , var) - @locals << v - end - v + # determine if method has a local variable or tmp (anonymous local) by given name + def has_local name + name = name.to_sym + index = @locals.index(name) + index = @tmps.index(name) unless index + index + end + + def ensure_local name + index = has_local name + return index if index + @locals << name + @locals.length end def get_var name diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index f70c4345..a7be31e4 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -6,10 +6,9 @@ module Virtual # Derived classes make up the actual functionality of the machine. # All functions on the machine are captured as instances of instructions # - # It is actually the point of the virtual machine layer to express oo functionality in the set of instructions, - # thus defining a minimal set of instructions needed to implement oo. + # It is actually the point of the virtual machine layer to express oo functionality in the set of + # instructions, thus defining a minimal set of instructions needed to implement oo. - # This is partly because jumping over this layer and doing in straight in assember was too big a step class Instruction < Virtual::Object # simple thought: don't recurse for Blocks, just check their names diff --git a/lib/virtual/instructions/set.rb b/lib/virtual/instructions/set.rb index d3f66edf..0fc1ec55 100644 --- a/lib/virtual/instructions/set.rb +++ b/lib/virtual/instructions/set.rb @@ -11,7 +11,6 @@ module Virtual raise "From must be slot or constant, not symbol #{from}" if from.is_a? Symbol @from = from end - attr_reader :to , :from + attr_reader :from , :to end - end diff --git a/lib/virtual/machine.rb b/lib/virtual/machine.rb index 3577b0b7..e4c07213 100644 --- a/lib/virtual/machine.rb +++ b/lib/virtual/machine.rb @@ -60,7 +60,7 @@ module Virtual syntax = @parser.parse_with_debug(bytes) parts = Parser::Transform.new.apply(syntax) main = Virtual::CompiledMethod.main - Compiler.compile( parts , main , self.message ) + Compiler.compile( parts , main ) end end diff --git a/lib/virtual/message.rb b/lib/virtual/message.rb index 4f99193b..3969cfb7 100644 --- a/lib/virtual/message.rb +++ b/lib/virtual/message.rb @@ -52,15 +52,5 @@ module Virtual end method.get_var(name) end - - def compile_set method , name , val - method.set_var(name,val) - if method.has_arg(name) - method.add_code MessageSet.new(name , val ) - else - method.add_code FrameSet.new(name , val ) - end - method.get_var(name) - end end end