diff --git a/lib/ast/README.md b/lib/ast/README.md index 5ca3b81b..5c95ea36 100644 --- a/lib/ast/README.md +++ b/lib/ast/README.md @@ -51,7 +51,7 @@ Then a new Method receives the message, creates a Frame for local and temporary The important thing here is that Messages and Frames are normal objects. -Anf interestingly we can partly use ruby to find the method, so in a way it is not just a top down transformation. but +And interestingly we can partly use ruby to find the method, so in a way it is not just a top down transformation. but the sending goes back up and then down again. The Message object is the second parameter to the compile method, the run-time part as it were. diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index f757d716..01c375a4 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -103,12 +103,14 @@ module Virtual class ImplicitBranch < Branch end - # A note: future branch conditions include OverflowBranch and other non-c + class MessageGet < Instruction + include Named + end class FrameGet < Instruction include Named end - class FrameSend < Instruction + class MessageSend < Instruction def initialize name , args = [] , nex = nil super(nex) @name = name.to_sym @@ -132,6 +134,18 @@ module Virtual end end + class MessageSet < Instruction + def initialize name , val , nex = nil + super(nex) + @name = name.to_sym + @value = val + end + attr_reader :name , :value + def attributes + [:name , :value] + super + end + end + class LoadSelf < Instruction def initialize val , nex = nil super(nex) diff --git a/lib/virtual/message.rb b/lib/virtual/message.rb index b7de5ad3..74862408 100644 --- a/lib/virtual/message.rb +++ b/lib/virtual/message.rb @@ -36,19 +36,27 @@ module Virtual end # def compile_get method , name - method.add FrameGet.new(name) + if method.has_arg(name) + method.add MessageGet.new(name) + else + method.add FrameGet.new(name) + end method.get_var(name) end def compile_send method , name , me , with = [] method.add Virtual::LoadSelf.new(me) - method.add FrameSend.new(name , with ) + method.add MessageSend.new(name , with ) Return.new( method.return_type ) end def compile_set method , name , val method.set_var(name,val) - method.add FrameSet.new(name , val ) + if method.has_arg(name) + method.add MessageSet.new(name , val ) + else + method.add FrameSet.new(name , val ) + end method.get_var(name) end end diff --git a/lib/virtual/method_definition.rb b/lib/virtual/method_definition.rb index 849dab4d..b7ff0402 100644 --- a/lib/virtual/method_definition.rb +++ b/lib/virtual/method_definition.rb @@ -49,6 +49,13 @@ module Virtual var end + # determine whether this method has an argument by the name + def has_arg name + name = name.to_sym + var = @args.find {|a| a.name == name } + var + end + def set_var name , var v = has_var name if( v ) diff --git a/test/virtual/test_methods.rb b/test/virtual/test_methods.rb index c9542a5d..8ef877e1 100644 --- a/test/virtual/test_methods.rb +++ b/test/virtual/test_methods.rb @@ -30,7 +30,7 @@ def foo(x) 2 + 5 end HERE - @output ="---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :xRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :abbaRETURN_MARKER type: &1 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &2 !ruby/object:Virtual::FrameSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER name: :abbaRETURN_MARKER value: *1RETURN_MARKER current: *2RETURN_MARKER" + @output ="---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :xRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :abbaRETURN_MARKER type: &1 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &2 !ruby/object:Virtual::MessageSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER name: :abbaRETURN_MARKER value: *1RETURN_MARKER current: *2RETURN_MARKER" check end @@ -40,7 +40,7 @@ def foo() 2 + 5 end HERE - @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args: []RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &1 !ruby/object:Virtual::FrameSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER current: *1RETURN_MARKER" + @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args: []RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &1 !ruby/object:Virtual::MessageSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER current: *1RETURN_MARKER" check end @@ -68,7 +68,7 @@ def fibonaccit(n) end end HERE - @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fibonaccitRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :aRETURN_MARKER type: &2 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 0RETURN_MARKER - &4 !ruby/object:Virtual::LocalRETURN_MARKER name: :someRETURN_MARKER type: &3 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 43RETURN_MARKER - &1 !ruby/object:Virtual::LocalRETURN_MARKER name: :otherRETURN_MARKER type: &5 !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: *1RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :aRETURN_MARKER value: *2RETURN_MARKER next: &6 !ruby/object:Virtual::LabelRETURN_MARKER name: while_startRETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :nRETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER name: :while_39010RETURN_MARKER other: &7 !ruby/object:Virtual::LabelRETURN_MARKER name: :while_39010RETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :someRETURN_MARKER value: *3RETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :someRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER value: *4RETURN_MARKER next: !ruby/object:Virtual::FrameSendRETURN_MARKER name: :*RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 4RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :otherRETURN_MARKER value: *5RETURN_MARKER next: *6RETURN_MARKER current: *7RETURN_MARKER" + @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fibonaccitRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :aRETURN_MARKER type: &2 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 0RETURN_MARKER - &4 !ruby/object:Virtual::LocalRETURN_MARKER name: :someRETURN_MARKER type: &3 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 43RETURN_MARKER - &1 !ruby/object:Virtual::LocalRETURN_MARKER name: :otherRETURN_MARKER type: &5 !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: *1RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :aRETURN_MARKER value: *2RETURN_MARKER next: &6 !ruby/object:Virtual::LabelRETURN_MARKER name: while_startRETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :nRETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER name: :while_39010RETURN_MARKER other: &7 !ruby/object:Virtual::LabelRETURN_MARKER name: :while_39010RETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :someRETURN_MARKER value: *3RETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :someRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER value: *4RETURN_MARKER next: !ruby/object:Virtual::MessageSendRETURN_MARKER name: :*RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 4RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :otherRETURN_MARKER value: *5RETURN_MARKER next: *6RETURN_MARKER current: *7RETURN_MARKER" check end @@ -93,7 +93,7 @@ def retvar(n) end end HERE - @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :retvarRETURN_MARKER args:RETURN_MARKER - &2 !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReferenceRETURN_MARKER clazz: RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: !ruby/object:Virtual::FrameSendRETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER next: &1 !ruby/object:Virtual::LabelRETURN_MARKER next: RETURN_MARKER name: :if_merge_5246RETURN_MARKER name: :if_merge_5246RETURN_MARKER other: *1RETURN_MARKER name: :>RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: *2RETURN_MARKER name: :nRETURN_MARKER current: *1RETURN_MARKER" + @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :retvarRETURN_MARKER args:RETURN_MARKER - &1 !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReferenceRETURN_MARKER clazz: RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::MessageGetRETURN_MARKER name: :nRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER value: *1RETURN_MARKER next: !ruby/object:Virtual::MessageSendRETURN_MARKER name: :>RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER name: :if_merge_5246RETURN_MARKER other: &2 !ruby/object:Virtual::LabelRETURN_MARKER name: :if_merge_5246RETURN_MARKER next: RETURN_MARKER next: *2RETURN_MARKER current: *2RETURN_MARKER" check end