add return instruction and fix the tests

This commit is contained in:
Torsten Ruger 2014-07-18 11:56:46 +03:00
parent cf00a6e490
commit c03c4f9cc9
3 changed files with 21 additions and 24 deletions

View File

@ -33,7 +33,13 @@ module Virtual
end
return true
end
# insert the given instruction as the next after this
# insert is not just set, but preserves the previous @next value as the next of the given instruction
# so if you think of the instructions as a linked list, it inserts the give instruction _after_ this one
def insert instruction
instruction.next = @next
@next = instruction
end
end
module Named
@ -57,6 +63,8 @@ module Virtual
# following classes are stubs. currently in brainstorming mode, so anything may change anytime
class MethodEnter < Instruction
end
class MethodReturn < Instruction
end
#resolves to nothing, but allows forward definition
class Label < Instruction
@ -79,19 +87,6 @@ module Virtual
end
@name = name
@other = other
if other
label = self.next
while label
break if label.is_a?(Label) and label.name == name
label = label.next
end
before_label = self.other
while before_label.next
break if before_label.next.is_a?(Label) and before_label.next.name == name
before_label = before_label.next
end
before_label.next = label
end
end
attr_reader :name
attr_accessor :other

View File

@ -17,7 +17,7 @@ module Virtual
def attributes
[:name , :args , :receiver , :return_type , :start]
end
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodEnter.new
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodEnter.new(MethodReturn.new)
@name = name.to_sym
@args = args
@locals = []
@ -30,9 +30,11 @@ module Virtual
attr_reader :name , :args , :receiver , :start
attr_accessor :return_type , :current
# add an instruction after the current (insertion point)
# the added instruction will become the new insertion point
def add instruction
raise instruction.inspect unless instruction.is_a? Instruction
@current.next = instruction
@current.insert(instruction) #insert after current
@current = instruction
end

View File

@ -9,7 +9,7 @@ def foo(x)
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 tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER start: &1 !ruby/object:Virtual::MethodEnterRETURN_MARKER next: RETURN_MARKER current: *1RETURN_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 tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER start: &1 !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER current: *1RETURN_MARKER"
check
end
@ -19,7 +19,7 @@ def String.length(x)
@length
end
HERE
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :lengthRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :xRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: &4 !ruby/object:Boot::BootClassRETURN_MARKER method_definitions:RETURN_MARKER - !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :getRETURN_MARKER args:RETURN_MARKER - &1 !ruby/class 'Virtual::Integer'RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: *1RETURN_MARKER return_type: *1RETURN_MARKER start: &2 !ruby/object:Virtual::MethodEnterRETURN_MARKER next: RETURN_MARKER current: *2RETURN_MARKER - !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :setRETURN_MARKER args:RETURN_MARKER - *1RETURN_MARKER - *1RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: *1RETURN_MARKER return_type: *1RETURN_MARKER start: &3 !ruby/object:Virtual::MethodEnterRETURN_MARKER next: RETURN_MARKER current: *3RETURN_MARKER name: :StringRETURN_MARKER super_class_name: :ObjectRETURN_MARKER meta_class: !ruby/object:Boot::MetaClassRETURN_MARKER layout: !ruby/object:Virtual::LayoutRETURN_MARKER members: []RETURN_MARKER functions: []RETURN_MARKER me_self: *4RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: &5 !ruby/object:Virtual::ObjectGetRETURN_MARKER next: RETURN_MARKER name: :lengthRETURN_MARKER current: *5RETURN_MARKER"
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :lengthRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :xRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: &4 !ruby/object:Boot::BootClassRETURN_MARKER method_definitions:RETURN_MARKER - !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :getRETURN_MARKER args:RETURN_MARKER - &1 !ruby/class 'Virtual::Integer'RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: *1RETURN_MARKER return_type: *1RETURN_MARKER start: &2 !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER current: *2RETURN_MARKER - !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :setRETURN_MARKER args:RETURN_MARKER - *1RETURN_MARKER - *1RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: *1RETURN_MARKER return_type: *1RETURN_MARKER start: &3 !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER current: *3RETURN_MARKER name: :StringRETURN_MARKER super_class_name: :ObjectRETURN_MARKER meta_class: !ruby/object:Boot::MetaClassRETURN_MARKER layout: !ruby/object:Virtual::LayoutRETURN_MARKER members: []RETURN_MARKER functions: []RETURN_MARKER me_self: *4RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: &5 !ruby/object:Virtual::ObjectGetRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :lengthRETURN_MARKER current: *5RETURN_MARKER"
check
end
@ -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: 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::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"
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: 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::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"
check
end
@ -54,7 +54,7 @@ def ofthen(n)
end
end
HERE
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :ofthenRETURN_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: :isitRETURN_MARKER type: &2 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 42RETURN_MARKER - &1 !ruby/object:Virtual::LocalRETURN_MARKER name: :maybenotRETURN_MARKER type: &4 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 667RETURN_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::ImplicitBranchRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER next: &3 !ruby/object:Virtual::LabelRETURN_MARKER next: RETURN_MARKER name: :if_merge_1RETURN_MARKER name: :isitRETURN_MARKER value: *2RETURN_MARKER name: :if_merge_1RETURN_MARKER other: !ruby/object:Virtual::FrameSetRETURN_MARKER next: *3RETURN_MARKER name: :maybenotRETURN_MARKER value: *4RETURN_MARKER current: *3RETURN_MARKER"
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :ofthenRETURN_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: :isitRETURN_MARKER type: &2 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 42RETURN_MARKER - &1 !ruby/object:Virtual::LocalRETURN_MARKER name: :maybenotRETURN_MARKER type: &4 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 667RETURN_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::ImplicitBranchRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER next: &3 !ruby/object:Virtual::LabelRETURN_MARKER next: RETURN_MARKER name: :if_merge_3RETURN_MARKER name: :isitRETURN_MARKER value: *2RETURN_MARKER name: :if_merge_3RETURN_MARKER other: !ruby/object:Virtual::FrameSetRETURN_MARKER next: *3RETURN_MARKER name: :maybenotRETURN_MARKER value: *4RETURN_MARKER current: *3RETURN_MARKER"
check
end
@ -79,11 +79,11 @@ def retvar(n)
return i
end
HERE
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :retvarRETURN_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: :iRETURN_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::ReferenceRETURN_MARKER clazz: RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: &2 !ruby/object:Virtual::FrameSetRETURN_MARKER next: RETURN_MARKER name: :iRETURN_MARKER value: *1RETURN_MARKER current: *2RETURN_MARKER"
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :retvarRETURN_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: :iRETURN_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::ReferenceRETURN_MARKER clazz: RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: &2 !ruby/object:Virtual::FrameSetRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :iRETURN_MARKER value: *1RETURN_MARKER current: *2RETURN_MARKER"
check
end
def ttest_function_return_if
def test_function_return_if
@string_input = <<HERE
def retvar(n)
if( n > 5)
@ -93,7 +93,7 @@ def retvar(n)
end
end
HERE
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
@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_1RETURN_MARKER name: :if_merge_1RETURN_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"
check
end