implements inspect for copy paste into tests

This commit is contained in:
Torsten Ruger 2014-05-10 12:54:10 +03:00
parent 051b0ea7dc
commit 20128627c7
5 changed files with 43 additions and 15 deletions

View File

@ -7,11 +7,14 @@ module Ast
def initialize val def initialize val
@value = val @value = val
end end
def inspect
self.class.name + ".new(" + value.to_s+ ")"
end
def compile context def compile context
Vm::Signed.new value Vm::Signed.new value
end end
def == other def attributes
compare other , [:value] [:value]
end end
end end
@ -20,8 +23,11 @@ module Ast
def initialize name def initialize name
@name = name @name = name
end end
def == other def inspect
compare other , [:name] self.class.name + ".new(" + name + ")"
end
def attributes
[:name]
end end
end end
@ -30,14 +36,17 @@ module Ast
def initialize str def initialize str
@string = str @string = str
end end
def inspectt
"#{string}"
end
def compile context def compile context
value = Vm::StringLiteral.new(string) value = Vm::StringLiteral.new(string)
context.program.add_object value context.program.add_object value
value value
end end
def == other def attributes
compare other , [:string] [:string]
end end
end end

View File

@ -4,8 +4,9 @@ module Ast
def initialize cond, if_true, if_false def initialize cond, if_true, if_false
@cond, @if_true, @if_false = cond, if_true, if_false @cond, @if_true, @if_false = cond, if_true, if_false
end end
def == other
compare other , [:cond, :if_true, :if_false] def attributes
[:cond, :if_true, :if_false]
end end
end end

View File

@ -15,7 +15,13 @@ module Ast
def compile context def compile context
raise "abstract #{self}" raise "abstract #{self}"
end end
def compare other , attributes def inspectt
self.class.name + ".new(" + self.attributes.collect{|m| self.send(m).inspect }.join( ",") +")"
end
def attributes
raise "abstract #{self}"
end
def == other
return false unless other.class == self.class return false unless other.class == self.class
attributes.each do |a| attributes.each do |a|
left = send(a) left = send(a)

View File

@ -4,8 +4,13 @@ module Ast
def initialize name, params, block def initialize name, params, block
@name, @params, @block = name, params, block @name, @params, @block = name, params, block
end end
def == other def attributes
compare other , [:name, :params, :block] [:name, :params, :block]
end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
params.collect{|m| m.inspect }.join( ",") +"] , [" +
block.collect{|m| m.inspect }.join( ",") +"] )"
end end
def compile context def compile context

View File

@ -15,9 +15,13 @@ module Ast
fun.do_call fun.do_call
fun fun
end end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") +"] )"
end
def == other def attributes
compare other , [:name , :args] [:name , :args]
end end
end end
@ -26,14 +30,17 @@ module Ast
def initialize assignee, assigned def initialize assignee, assigned
@assignee, @assigned = assignee, assigned @assignee, @assigned = assignee, assigned
end end
def inspect
self.class.name + ".new(" + assignee + ", " + assigned.inspect+ ")"
end
def compile context def compile context
var = @assigned.compile(context) var = @assigned.compile(context)
context.locals[@assignee] = var context.locals[@assignee] = var
end end
def == other def attributes
compare other , [:assignee, :assigned] [:assignee, :assigned]
end end
end end
end end