From 20128627c7e04460ddbc4c3cb86decdc9ef088a3 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 10 May 2014 12:54:10 +0300 Subject: [PATCH] implements inspect for copy paste into tests --- lib/ast/basic_expressions.rb | 21 +++++++++++++++------ lib/ast/conditional_expression.rb | 5 +++-- lib/ast/expression.rb | 8 +++++++- lib/ast/function_expression.rb | 9 +++++++-- lib/ast/operator_expressions.rb | 15 +++++++++++---- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/lib/ast/basic_expressions.rb b/lib/ast/basic_expressions.rb index ba4cc8c4..fc0a187f 100644 --- a/lib/ast/basic_expressions.rb +++ b/lib/ast/basic_expressions.rb @@ -7,11 +7,14 @@ module Ast def initialize val @value = val end + def inspect + self.class.name + ".new(" + value.to_s+ ")" + end def compile context Vm::Signed.new value end - def == other - compare other , [:value] + def attributes + [:value] end end @@ -20,8 +23,11 @@ module Ast def initialize name @name = name end - def == other - compare other , [:name] + def inspect + self.class.name + ".new(" + name + ")" + end + def attributes + [:name] end end @@ -30,14 +36,17 @@ module Ast def initialize str @string = str end + def inspectt + "#{string}" + end def compile context value = Vm::StringLiteral.new(string) context.program.add_object value value end - def == other - compare other , [:string] + def attributes + [:string] end end diff --git a/lib/ast/conditional_expression.rb b/lib/ast/conditional_expression.rb index 1882559e..ce609cf9 100644 --- a/lib/ast/conditional_expression.rb +++ b/lib/ast/conditional_expression.rb @@ -4,8 +4,9 @@ module Ast 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] + + def attributes + [:cond, :if_true, :if_false] end end diff --git a/lib/ast/expression.rb b/lib/ast/expression.rb index 74b6aafd..88ad6c7d 100644 --- a/lib/ast/expression.rb +++ b/lib/ast/expression.rb @@ -15,7 +15,13 @@ module Ast def compile context raise "abstract #{self}" 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 attributes.each do |a| left = send(a) diff --git a/lib/ast/function_expression.rb b/lib/ast/function_expression.rb index 88aadf56..7bf6cf4e 100644 --- a/lib/ast/function_expression.rb +++ b/lib/ast/function_expression.rb @@ -4,8 +4,13 @@ module Ast def initialize name, params, block @name, @params, @block = name, params, block end - def == other - compare other , [:name, :params, :block] + def attributes + [: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 def compile context diff --git a/lib/ast/operator_expressions.rb b/lib/ast/operator_expressions.rb index 10dfa0a6..882d1bf6 100644 --- a/lib/ast/operator_expressions.rb +++ b/lib/ast/operator_expressions.rb @@ -15,9 +15,13 @@ module Ast fun.do_call fun end + def inspect + self.class.name + ".new(" + name.inspect + ", ["+ + args.collect{|m| m.inspect }.join( ",") +"] )" + end - def == other - compare other , [:name , :args] + def attributes + [:name , :args] end end @@ -26,14 +30,17 @@ module Ast def initialize assignee, assigned @assignee, @assigned = assignee, assigned end + def inspect + self.class.name + ".new(" + assignee + ", " + assigned.inspect+ ")" + end def compile context var = @assigned.compile(context) context.locals[@assignee] = var end - def == other - compare other , [:assignee, :assigned] + def attributes + [:assignee, :assigned] end end end \ No newline at end of file