fixed and tightened transformation tests, also by implementing node ==

This commit is contained in:
Torsten Ruger
2014-04-28 16:07:34 +03:00
parent e04973fc3c
commit cfcb9ded2f
3 changed files with 48 additions and 19 deletions

View File

@ -5,6 +5,15 @@ module Vm
def eval
raise "abstract"
end
def compare other , attributes
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
end
class IntegerExpression < Expression
@ -12,6 +21,9 @@ module Vm
def initialize val
@value = val
end
def == other
compare other , [:value]
end
def eval(context, builder)
builder.mov "r0" , value
end
@ -22,11 +34,13 @@ module Vm
def initialize name
@name = name
end
def == other
compare other , [:name]
end
def eval(context, builder)
param_names = context[:params] || []
position = param_names.index(name)
raise "Unknown parameter #{name}" unless position
builder.iload position
end
end
@ -36,6 +50,9 @@ module Vm
def initialize name, args
@name , @args = name , args
end
def == other
compare other , [:name , :args]
end
def eval(context, builder)
args.each { |a| a.eval(context, builder) }
types = [builder.int] * (args.length + 1)
@ -48,6 +65,9 @@ module Vm
def initialize cond, if_true, if_false
@cond, @if_true, @if_false = cond, if_true, if_false
end
def == other
compare other , [:cond, :if_true, :if_false]
end
def eval(context, builder)
cond.eval context, builder
@ -68,6 +88,9 @@ module Vm
def initialize name, params, block
@name, @params, @block = name, params, block
end
def == other
compare other , [:name, :params, :block]
end
def eval(context, builder)
param_names = [params].flatten.map(&:name)
context[:params] = param_names