fixes the tests after fixing hierarchy

This commit is contained in:
Torsten Ruger 2014-07-15 10:35:29 +03:00
parent 49f73c090d
commit 930740e1db
11 changed files with 79 additions and 25 deletions

View File

@ -34,7 +34,7 @@ module Ast
# otherwise it's a method without args and a send is ussued. # otherwise it's a method without args and a send is ussued.
# this makes the namespace static, ie when eval and co are implemented method needs recompilation # this makes the namespace static, ie when eval and co are implemented method needs recompilation
def compile frame , method def compile frame , method
return Virtual::SelfReference.new() if name == :self return Virtual::Self.new( Virtual::Mystery.new ) if name == :self
if method.has_var(name) if method.has_var(name)
frame.compile_get(method , name ) frame.compile_get(method , name )
else else
@ -75,6 +75,7 @@ module Ast
class VariableExpression < NameExpression class VariableExpression < NameExpression
def compile frame ,method def compile frame ,method
method.add Virtual::ObjectGet.new(name) method.add Virtual::ObjectGet.new(name)
Virtual::Return.new( Virtual::Mystery.new )
end end
end end
end end

View File

@ -6,7 +6,7 @@ module Ast
@@counter = 0 @@counter = 0
def compile frame , method def compile frame , method
with = args.collect{|a| a.compile(frame , method)} with = args.collect{|a| a.compile(frame , method)}
frame.compile_send( method , @name , with ) frame.compile_send( method , name , with )
end end
def scratch def scratch

View File

@ -2,12 +2,10 @@ 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
tmp = method.get_tmp call = CallSiteExpression.new( operator , [right] , left )
ass = AssignmentExpression.new( tmp , left )
l = ass.compile(frame , method)
call = CallSiteExpression.new( operator , [right] , l)
call.compile(frame , method) call.compile(frame , method)
end end
def scratch def scratch
into = context.function into = context.function
puts "compiling operator #{to_s}" puts "compiling operator #{to_s}"

View File

@ -3,10 +3,13 @@ module Virtual
class Constant < ::Virtual::Value class Constant < ::Virtual::Value
end end
class TrueValue < Constant class TrueValue < Constant
def attributes ; [] ; end
end end
class FalseValue < Constant class FalseValue < Constant
def attributes ; [] ; end
end end
class NilValue < Constant class NilValue < Constant
def attributes ; [] ; end
end end
# another abstract "marker" class (so we can check for it) # another abstract "marker" class (so we can check for it)

View File

@ -30,14 +30,17 @@ module Virtual
# #
def compile_get method , name def compile_get method , name
method.add FrameGet.new(name) method.add FrameGet.new(name)
method.get_var(name)
end end
def compile_send method , name , with = [] def compile_send method , name , with = []
method.add FrameSend.new(name , with ) method.add FrameSend.new(name , with )
Return.new( method.return_type )
end end
def compile_set method , name , val def compile_set method , name , val
method.add FrameSet.new(name , val ) method.add FrameSet.new(name , val )
method.get_var(name)
end end
end end
end end

View File

@ -18,9 +18,6 @@ module Virtual
def initialize nex = nil def initialize nex = nil
@next = nex @next = nex
end end
def type
Reference
end
end end
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just # the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just

View File

@ -17,7 +17,7 @@ module Virtual
def attributes def attributes
[:name , :args , :receiver , :return_type , :start] [:name , :args , :receiver , :return_type , :start]
end end
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Reference , start = MethodEnter.new def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodEnter.new
@name = name.to_sym @name = name.to_sym
@args = args @args = args
@locals = [] @locals = []
@ -34,7 +34,6 @@ module Virtual
raise instruction.inspect unless instruction.is_a? 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
@ -46,6 +45,7 @@ module Virtual
var = @tmps.find {|a| a == name } unless var var = @tmps.find {|a| a == name } unless var
var var
end end
alias :get_var :has_var
def get_tmp def get_tmp
name = "__tmp__#{@tmps.length}" name = "__tmp__#{@tmps.length}"

View File

@ -48,7 +48,9 @@ module Virtual
class Mystery < Type class Mystery < Type
def initialize def initialize
end end
def attributes
[]
end
def as type def as type
type.new type.new
end end

View File

@ -9,15 +9,65 @@ module Virtual
def == other def == other
other.class == self.class other.class == self.class
end end
def inspect
self.class.name + ".new()"
end
def type def type
raise "abstract called" raise "abstract called for #{self.class}"
end
def attributes
raise "abstract called for #{self.class}"
end
def == other
return false unless other.class == self.class
attributes.each do |a|
left = send(a)
right = other.send(a)
return false unless left.class == right.class
return false unless left == right
end
return true
end
def inspect
self.class.name + ".new(" + attributes.collect{|a| send(a).inspect }.join(",")+ ")"
end end
private private
def initialize def initialize
end end
end end
class Variable < Value
def initialize name , type
@name = name
@type = type
end
attr_accessor :name , :type
def attributes
[:name , :type]
end
end
# The subclasses are not strictly speaking neccessary at this def point
# i just don't want to destroy the information for later optimizations
#
# All variables are stored in frames and quite possibly in order arg,local,tmp
class Return < Variable
def initialize type
super(:return , type)
end
def attributes
[:type]
end
end
class Self < Variable
def initialize type
super(:self , type)
end
def attributes
[:type]
end
end
class Argument < Variable
end
class Local < Variable
end
class Temp < Variable
end
end end

View File

@ -27,19 +27,19 @@ class TestBasic < MiniTest::Test
def test_name def test_name
@string_input = 'foo ' @string_input = 'foo '
@output = [Virtual::Reference] @output = [Virtual::Return.new(Virtual::Mystery)]
check check
end end
def test_self def test_self
@string_input = 'self ' @string_input = 'self '
@output = [Virtual::SelfReference.new()] @output = [Virtual::Self.new(Virtual::Mystery.new())]
check check
end end
def test_instance_variable def test_instance_variable
@string_input = '@foo_bar ' @string_input = '@foo_bar '
@output = [Virtual::Reference] @output = [Virtual::Return.new( Virtual::Mystery.new())]
check check
end end

View File

@ -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::IntegerConstant.new(5),Virtual::MethodEnter.new(nil))] @output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(nil),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(Virtual::ObjectGet.new(:length)))] @output = [Virtual::Method.new(:length,[Ast::NameExpression.new(:x)],Boot::BootClass.new(:String,:Object),Virtual::Return.new( Virtual::Mystery.new()),Virtual::MethodEnter.new(Virtual::ObjectGet.new(:length)))]
check check
end end
@ -30,7 +30,7 @@ def foo(x)
2 + 5 2 + 5
end end
HERE HERE
@output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(),Virtual::Reference,Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5))))] @output = [Virtual::Method.new(:foo,[Ast::NameExpression.new(:x)],Virtual::SelfReference.new(nil),Virtual::Return.new( Virtual::Mystery),Virtual::MethodEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5))))]
check check
end end