From b59a9da34ed3f2c7124908680adc64a1cf5179ae Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 14 Jul 2014 21:28:21 +0300 Subject: [PATCH] reduce method return type to type and introduce a single instruction for instance get --- Gemfile.lock | 2 +- lib/ast/basic_expressions.rb | 11 +++++++++++ lib/ast/call_site_expression.rb | 8 -------- lib/ast/if_expression.rb | 4 +++- lib/ast/operator_expressions.rb | 2 +- lib/ast/return_expression.rb | 2 +- lib/ast/while_expression.rb | 2 +- lib/virtual/constants.rb | 3 +++ lib/virtual/frame.rb | 4 ++++ lib/virtual/instruction.rb | 30 +++++++++++++++++++++++++----- test/virtual/test_basic.rb | 2 +- 11 files changed, 51 insertions(+), 19 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a55ff13b..25e86f39 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,7 +7,7 @@ GIT GIT remote: https://github.com/crystal-vm/crystal-reader.git - revision: bc8697c733113b19863df632b546c7eab2b429c0 + revision: 0c88e7eff569a6c1b0f354418daacb9ad2ac621a specs: crystal-reader (0.1.0) diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index 754f2c04..6710db61 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -63,7 +63,18 @@ module Ast end end class AssignmentExpression < Expression + #attr_reader :left, :right + def compile frame , method + raise "must assign to NameExpression , not #{left}" unless left.instance_of? NameExpression + r = right.compile(frame,method) + frame.compile_set( method , left.name , r ) + end + end + + class VariableExpression < NameExpression + def compile frame ,method + Virtual::ObjectGet.new(name) end end end \ No newline at end of file diff --git a/lib/ast/call_site_expression.rb b/lib/ast/call_site_expression.rb index cd575d36..6b0ae3ba 100644 --- a/lib/ast/call_site_expression.rb +++ b/lib/ast/call_site_expression.rb @@ -53,12 +53,4 @@ module Ast end end - class VariableExpression < CallSiteExpression -# super( :_get_instance_variable , [StringExpression.new(name)] ) - def make_setter - @name = :_set_instance_variable - @args << StringExpression.new("value") - end - end - end \ No newline at end of file diff --git a/lib/ast/if_expression.rb b/lib/ast/if_expression.rb index a237abc0..c8d7fb47 100644 --- a/lib/ast/if_expression.rb +++ b/lib/ast/if_expression.rb @@ -2,7 +2,9 @@ module Ast class IfExpression < Expression # attr_reader :cond, :if_true, :if_false def compile frame , method - return nil + Virtual::Reference + end + def old f = context.function # to execute the logic as the if states it, the blocks are the other way around # so we can the jump over the else if true ,and the else joins unconditionally after the true_block diff --git a/lib/ast/operator_expressions.rb b/lib/ast/operator_expressions.rb index 3efc7200..39375d5c 100644 --- a/lib/ast/operator_expressions.rb +++ b/lib/ast/operator_expressions.rb @@ -2,7 +2,7 @@ module Ast class OperatorExpression < Expression # attr_reader :operator, :left, :right def compile frame , method - nil + Virtual::Reference.new end def scratch into = context.function diff --git a/lib/ast/return_expression.rb b/lib/ast/return_expression.rb index de0f47b1..1654cbd4 100644 --- a/lib/ast/return_expression.rb +++ b/lib/ast/return_expression.rb @@ -2,7 +2,7 @@ module Ast class ReturnExpression < Expression # attr_reader :expression def compile frame ,method - nil + Virtual::Reference.new end def sc into = context.function diff --git a/lib/ast/while_expression.rb b/lib/ast/while_expression.rb index a0b57c21..a4ff4a39 100644 --- a/lib/ast/while_expression.rb +++ b/lib/ast/while_expression.rb @@ -2,7 +2,7 @@ module Ast class WhileExpression < Expression # attr_reader :condition, :body def compile frame , method - nil + Virtual::Reference.new end def old into = context.function diff --git a/lib/virtual/constants.rb b/lib/virtual/constants.rb index c49c5718..64adbfd1 100644 --- a/lib/virtual/constants.rb +++ b/lib/virtual/constants.rb @@ -19,6 +19,9 @@ module Virtual def inspect self.class.name + ".new(#{@integer})" end + def type + Virtual::Integer + end end # The name really says it all. diff --git a/lib/virtual/frame.rb b/lib/virtual/frame.rb index f10a1885..e34616a7 100644 --- a/lib/virtual/frame.rb +++ b/lib/virtual/frame.rb @@ -35,5 +35,9 @@ module Virtual def compile_send method , name , with = [] method.add FrameSend.new(name , with ) end + + def compile_set method , name , val + method.add FrameSet.new(name , val ) + end end end diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index 7edb5f65..14a08e92 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -39,6 +39,10 @@ module Virtual def initialize name @name = name end + attr_reader :name + def attributes + [:name] + end end class FrameSend < Instruction @@ -48,13 +52,29 @@ module Virtual @args = args end attr_reader :name , :args - - def == other - self.class == other.class && self.name == other.name + def attributes + [:name , :args] end - def inspect - self.class.name + ".new(:#{@name} , [ " + args.collect{|a| a.inspect}.join(",")+ "])" + end + class FrameSet < Instruction + + def initialize name , val + @name = name.to_sym + @value = val + end + attr_reader :name , :value + def attributes + [:name , :value] end end + class ObjectGet < Instruction + def initialize name + @name = name + end + attr_reader :name + def attributes + [:name] + end + end end diff --git a/test/virtual/test_basic.rb b/test/virtual/test_basic.rb index 43815340..ff91dba8 100644 --- a/test/virtual/test_basic.rb +++ b/test/virtual/test_basic.rb @@ -39,7 +39,7 @@ class TestBasic < MiniTest::Test def test_instance_variable @string_input = '@foo_bar ' - @output = [Virtual::FrameSend.new(:_get_instance_variable , [ Virtual::StringConstant.new('foo_bar')])] + @output = [Virtual::ObjectGet.new(:foo_bar)] check end