From 1e7093c63fbeba707a4d2b66d221fff24f0904c8 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 9 Dec 2016 13:20:48 +0200 Subject: [PATCH] remodel assignment a bit --- lib/register.rb | 8 ++++++ lib/typed/ast/assignment.rb | 19 +++++++------- lib/typed/compiler.rb | 16 +++++++----- lib/typed/compiler/assignment.rb | 44 +++++++++++++++----------------- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/lib/register.rb b/lib/register.rb index 55fbca3a..a18a58ee 100644 --- a/lib/register.rb +++ b/lib/register.rb @@ -1,3 +1,10 @@ +class String + def camelize + self.split("_").collect( &:capitalize ).join + end +end + + require "register/padding" require "register/positioned" require "typed/compiler" @@ -11,6 +18,7 @@ class Fixnum end end + require "register/instruction" require "register/register_value" require "register/assembler" diff --git a/lib/typed/ast/assignment.rb b/lib/typed/ast/assignment.rb index 77c7cfe5..499d6358 100644 --- a/lib/typed/ast/assignment.rb +++ b/lib/typed/ast/assignment.rb @@ -1,13 +1,14 @@ module Typed - class Assignment < Statement - attr_accessor :name , :value - def initialize(n = nil , v = nil ) - @name , @value = n , v + module Tree + class Assignment < Statement + attr_accessor :name , :value + def initialize(n = nil , v = nil ) + @name , @value = n , v + end + end + + class FieldDef < Statement + attr_accessor :name , :type , :value end end - - class FieldDef < Statement - attr_accessor :name , :type , :value - end - end diff --git a/lib/typed/compiler.rb b/lib/typed/compiler.rb index 830a1f81..69a6651a 100644 --- a/lib/typed/compiler.rb +++ b/lib/typed/compiler.rb @@ -1,8 +1,10 @@ -["call_site", "name_expression"].each do |mod| - require_relative "compiler/" + mod -end - module Typed + + CompilerModules = ["assignment" , "call_site", "name_expression"] + CompilerModules.each do |mod| + require_relative "compiler/" + mod + end + # Compiling is the conversion of the AST into 2 things: # - code (ie sequences of Instructions inside Methods) # - an object graph containing all the Methods, their classes and Constants @@ -41,8 +43,9 @@ module Typed end class Compiler - include NameExpression - include CallSite + CompilerModules.each do |mod| + include Typed.const_get( mod.camelize ) + end def initialize( method = nil ) @regs = [] @@ -184,7 +187,6 @@ end require_relative "ast_helper" require_relative "ast/code" -require_relative "compiler/assignment" require_relative "compiler/basic_values" require_relative "compiler/class_field" require_relative "compiler/class_statement" diff --git a/lib/typed/compiler/assignment.rb b/lib/typed/compiler/assignment.rb index f2b93c77..14c98a6b 100644 --- a/lib/typed/compiler/assignment.rb +++ b/lib/typed/compiler/assignment.rb @@ -1,33 +1,31 @@ module Typed - Compiler.class_eval do + module Assignment - def on_Assignment statement + def on_Assignment( statement ) + # name , value = *statement reset_regs # statements reset registers, ie have all at their disposal - #puts statement.inspect -# name , value = *statement name_s = no_space statement.name v = process(statement.value) raise "Not register #{v}" unless v.is_a?(Register::RegisterValue) - code = nil - if( index = @method.has_arg(name_s.name)) - # TODO, check type @method.arguments[index].type - code = Register.set_slot(statement , v , :message , Parfait::Message.get_indexed(index) ) - else # or a local so it is in the frame - index = @method.has_local( name_s.name ) - if(index) - # TODO, check type @method.locals[index].type - frame = use_reg(:Frame) - add_code Register.get_slot(statement , :message , :frame , frame ) - code = Register.set_slot(statement , v , frame , Parfait::Frame.get_indexed(index) ) - end - end - if( code ) - #puts "addin code #{code}" - add_code code - else - raise "must define variable #{name} before using it in #{@method.inspect}" - end + code = get_code( statement , name_s ) + raise "must define variable #{name} before using it in #{@method.inspect}" unless code + add_code code end + private + + def get_code( statement , name_s ) + if( index = @method.has_arg(name_s.name)) + # TODO, check type @method.arguments[index].type + return Register.set_slot(statement , v , :message , Parfait::Message.get_indexed(index) ) + end + # or a local so it is in the frame + index = @method.has_local( name_s.name ) + return nil unless index + # TODO, check type @method.locals[index].type + frame = use_reg(:Frame) + add_code Register.get_slot(statement , :message , :frame , frame ) + return Register.set_slot(statement , v , frame , Parfait::Frame.get_indexed(index) ) + end end end