From 4ac89ece660fd0feed372ace95715ab57a377fc7 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 9 Jul 2018 17:53:56 +0300 Subject: [PATCH] start delegating scope matters to the compiler(s) slot_type_for to return the slot way to access variable this is off course version 0.0.1 alpha, no types are checked or errors handled --- lib/risc/block_compiler.rb | 18 ++++++++++++++++++ lib/risc/method_compiler.rb | 18 ++++++++++++++++++ lib/vool/local_assignment.rb | 8 ++------ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/risc/block_compiler.rb b/lib/risc/block_compiler.rb index 1dd75b8f..4821a687 100644 --- a/lib/risc/block_compiler.rb +++ b/lib/risc/block_compiler.rb @@ -17,5 +17,23 @@ module Risc @constants = [] end + # determine how given name need to be accsessed. + # For blocks the options are args or frame + # or then the methods arg or frame + def slot_type_for(name) + if @block.arguments_type.variable_index(name) + slot_def = [ :arguments] + elsif @block.frame_type.variable_index(name) + slot_def = [:frame] + elsif @method.arguments_type.variable_index(name) + slot_def = [:caller , :arguments ] + elsif @method.arguments_type.variable_index(name) + slot_def = [:caller , :frame ] + elsif + raise "no variable #{name} , need to resolve at runtime" + end + slot_def << name + end + end end diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index de2e350c..1e60c1fd 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -47,10 +47,28 @@ module Risc self.new(method) end + # determine how given name need to be accsessed. + # For methods the options are args or frame + def slot_type_for(name) + if @method.arguments_type.variable_index(name) + type = :arguments + else + type = :frame + end + [type , name] + end + def add_block_compiler(compiler) @block_compilers << compiler end + # return true or false if the given name is in scope (arg/local) + def in_scope?(name) + ret = true if @method.arguments_type.variable_index(name) + ret = @method.frame_type.variable_index(name) unless ret + ret + end + # convert the given mom instruction to_risc and then add it (see add_code) # continue down the instruction chain unti depleted # (adding moves the insertion point so the whole mom chain is added as a risc chain) diff --git a/lib/vool/local_assignment.rb b/lib/vool/local_assignment.rb index c65ee822..b8f08db9 100644 --- a/lib/vool/local_assignment.rb +++ b/lib/vool/local_assignment.rb @@ -3,12 +3,8 @@ module Vool class LocalAssignment < Assignment def to_mom( compiler ) - if compiler.method.arguments_type.variable_index(@name) - type = :arguments - else - type = :frame - end - to = Mom::SlotDefinition.new(:message ,[ type , @name]) + slot_def = compiler.slot_type_for(@name) + to = Mom::SlotDefinition.new(:message ,slot_def) from = @value.slot_definition(compiler) return chain_assign( Mom::SlotLoad.new(to,from) , compiler) end