fix all tests
This commit is contained in:
parent
b59a9da34e
commit
9891da88a4
@ -74,7 +74,7 @@ module Ast
|
|||||||
|
|
||||||
class VariableExpression < NameExpression
|
class VariableExpression < NameExpression
|
||||||
def compile frame ,method
|
def compile frame ,method
|
||||||
Virtual::ObjectGet.new(name)
|
method.add Virtual::ObjectGet.new(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -8,8 +8,8 @@ module Ast
|
|||||||
frame = frame.new_frame
|
frame = frame.new_frame
|
||||||
return_type = nil
|
return_type = nil
|
||||||
body.each do |ex|
|
body.each do |ex|
|
||||||
val = ex.compile(frame , method )
|
return_type = ex.compile(frame , method )
|
||||||
return_type = val.type
|
raise return_type.inspect if return_type.is_a? Virtual::Instruction
|
||||||
end
|
end
|
||||||
method.return_type = return_type
|
method.return_type = return_type
|
||||||
method
|
method
|
||||||
|
@ -2,7 +2,11 @@ module Ast
|
|||||||
class OperatorExpression < Expression
|
class OperatorExpression < Expression
|
||||||
# attr_reader :operator, :left, :right
|
# attr_reader :operator, :left, :right
|
||||||
def compile frame , method
|
def compile frame , method
|
||||||
Virtual::Reference.new
|
tmp = method.get_tmp
|
||||||
|
ass = AssignmentExpression.new( tmp , left )
|
||||||
|
l = ass.compile(frame , method)
|
||||||
|
call = CallSiteExpression.new( operator , [right] , l)
|
||||||
|
call.compile(frame , method)
|
||||||
end
|
end
|
||||||
def scratch
|
def scratch
|
||||||
into = context.function
|
into = context.function
|
||||||
|
@ -21,6 +21,7 @@ module Virtual
|
|||||||
@name = name.to_sym
|
@name = name.to_sym
|
||||||
@args = args
|
@args = args
|
||||||
@locals = []
|
@locals = []
|
||||||
|
@tmps = []
|
||||||
@receiver = receiver
|
@receiver = receiver
|
||||||
@return_type = return_type
|
@return_type = return_type
|
||||||
@start = start
|
@start = start
|
||||||
@ -30,8 +31,10 @@ module Virtual
|
|||||||
attr_accessor :return_type
|
attr_accessor :return_type
|
||||||
|
|
||||||
def add instruction
|
def add instruction
|
||||||
|
raise instruction.inspect unless instruction.is_a? Instruction
|
||||||
@current.next = instruction
|
@current.next = instruction
|
||||||
@current = instruction
|
@current = instruction
|
||||||
|
instruction.type
|
||||||
end
|
end
|
||||||
|
|
||||||
# determine whether this method has a variable by the given name
|
# determine whether this method has a variable by the given name
|
||||||
@ -40,7 +43,14 @@ module Virtual
|
|||||||
def has_var name
|
def has_var name
|
||||||
var = @args.find {|a| a == name }
|
var = @args.find {|a| a == name }
|
||||||
var = @locals.find {|a| a == name } unless var
|
var = @locals.find {|a| a == name } unless var
|
||||||
|
var = @tmps.find {|a| a == name } unless var
|
||||||
var
|
var
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_tmp
|
||||||
|
name = "__tmp__#{@tmps.length}"
|
||||||
|
@tmps << name
|
||||||
|
Ast::NameExpression.new(name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -27,7 +27,7 @@ class TestBasic < MiniTest::Test
|
|||||||
|
|
||||||
def test_name
|
def test_name
|
||||||
@string_input = 'foo '
|
@string_input = 'foo '
|
||||||
@output = [Virtual::FrameSend.new(:foo)]
|
@output = [Virtual::Reference]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ class TestBasic < MiniTest::Test
|
|||||||
|
|
||||||
def test_instance_variable
|
def test_instance_variable
|
||||||
@string_input = '@foo_bar '
|
@string_input = '@foo_bar '
|
||||||
@output = [Virtual::ObjectGet.new(:foo_bar)]
|
@output = [Virtual::Reference]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
require_relative "virtual_helper"
|
require_relative "virtual_helper"
|
||||||
|
|
||||||
class TestFunctionDefinition < MiniTest::Test
|
class TestMethods < MiniTest::Test
|
||||||
include VirtualHelper
|
include VirtualHelper
|
||||||
|
|
||||||
def test_simplest_function
|
def test_simplest_function
|
||||||
@ -9,7 +9,7 @@ def foo(x)
|
|||||||
5
|
5
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(),Virtual::Integer,Virtual::MethodEnter.new(nil))]
|
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(),Virtual::IntegerConstant.new(5),Virtual::MethodEnter.new(nil))]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ def String.length(x)
|
|||||||
@length
|
@length
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [Virtual::Method.new(:length,[Ast::NameExpression.new(:x)],Boot::BootClass.new(:String,:Object),Virtual::Reference,Virtual::MethodEnter.new(nil))]
|
@output = [Virtual::Method.new(:length,[Ast::NameExpression.new(:x)],Boot::BootClass.new(:String,:Object),Virtual::Reference,Virtual::MethodEnter.new(Virtual::ObjectGet.new(:length)))]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user