unify to_s inspect stuff

This commit is contained in:
Torsten Ruger
2015-09-14 19:50:39 +03:00
parent d0980265fd
commit d5d26a3ac4
15 changed files with 93 additions and 108 deletions

View File

@ -10,9 +10,7 @@ module Ast
def attributes
[:value]
end
def inspect
self.class.name + ".new(" + value.to_s+ ")"
end
def to_s
value.to_s
end
@ -20,7 +18,7 @@ module Ast
class TrueExpression < Expression
def to_s
"true"
""
end
def attributes
[]
@ -28,7 +26,7 @@ module Ast
end
class FalseExpression < Expression
def to_s
"false"
""
end
def attributes
[]
@ -36,7 +34,7 @@ module Ast
end
class NilExpression < Expression
def to_s
"nil"
""
end
def attributes
[]

View File

@ -8,19 +8,14 @@ module Ast
@body_exp = body_exp
end
def attributes
[:call_exp, :args , :body_exp]
end
def inspect
self.class.call_exp + ".new(" + call_exp.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") + "] ," + body_exp.inspect + ")"
end
def to_s
"#{call_exp}(" + args.join(",") + ")"
end
def attributes
[:call_exp , :args , :body_exp]
end
def to_s
call_exp.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") + "] ," + body_exp.inspect
end
end
end

View File

@ -1,24 +1,35 @@
module Ast
# assignment, like operators are really function calls
class CallSiteExpression < Expression
attr_reader :name, :args , :receiver
class FieldExpression < Expression
attr_reader :receiver , :name
def initialize name, args , receiver = Ast::NameExpression.new(:self)
@name = name.to_sym
@args = args
def initialize receiver , name
@receiver = receiver
@name = name.to_sym
end
def attributes
[:name , :args , :receiver]
end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") + "] ," + receiver.inspect + ")"
[:receiver , :name]
end
def to_s
"#{name}(" + args.join(",") + ")"
receiver.inspect + "," + name.inspect
end
end
class CallSiteExpression < Expression
attr_reader :field , :args
def initialize field, args
@field = field
@args = args
end
def attributes
[:field , :args]
end
def to_s
field.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") + "] "
end
end
end

View File

@ -8,21 +8,24 @@ module Ast
def initialize vals
@values = vals
end
def inspect
self.class.name + ".new(" + values.to_s+ ")"
def to_s
values.to_s
end
end
class AssociationExpression < Expression
attr_reader :key , :value
def initialize key , value
@key , @value = key , value
end
def attributes
[:key , :value]
end
def inspect
self.class.name + ".new(" + key.inspect + " , " + value.inspect + ")"
def to_s
key.inspect + " , " + value.inspect
end
end

View File

@ -54,7 +54,7 @@ module Ast
end
def inspect
self.class.name + ".new()"
self.class.name + ".new(#{to_s})"
end
def == other
return false unless other.class == self.class

View File

@ -4,17 +4,14 @@ module Ast
def initialize expressions
@expressions = expressions
end
def attributes
[:expressions]
end
def inspect
self.class.name + ".new( ["+ expressions.collect(&:inspect).join( ",") +"])"
end
def to_s
expressions.collect(&:inspect).join("\n")
"["+ expressions.collect(&:inspect).join( ",") +"]"
end
end
end

View File

@ -10,16 +10,10 @@ module Ast
def attributes
[:name, :params, :body , :receiver]
end
def inspect
self.class.name + ".new(" + name.inspect + ", ["+
params.collect{|m| m.inspect }.join( ",") +"] , [" +
body.collect{|m| m.inspect }.join( ",") +"] ,"+ receiver.inspect + " )"
end
def to_s
ret = "def "
ret += "#{receiver}." if receiver
ret + "#{name}( " + params.join(",") + ") \n" + body.join("\n") + "end\n"
name.inspect + ", ["+
params.collect{|m| m.inspect }.join( ",") +"] , [" +
body.collect{|m| m.inspect }.join( ",") +"] ,"+ receiver.inspect
end
end
end
end

View File

@ -7,9 +7,9 @@ module Ast
def attributes
[:cond, :if_true, :if_false]
end
def inspect
self.class.name + ".new(" + cond.inspect + ", "+
if_true.inspect + "," + if_false.inspect + " )"
def to_s
cond.inspect + ", " + if_true.inspect + "," + if_false.inspect
end
end
end

View File

@ -10,11 +10,9 @@ module Ast
def attributes
[:name , :expressions]
end
def inspect
self.class.name + ".new(" + @name.inspect + " ," + @expressions.inspect + " )"
end
def to_s
"module #{name}\n #{expressions}\nend\n"
name.inspect + " ," + expressions.inspect
end
end
@ -27,15 +25,13 @@ module Ast
def attributes
[:name , :derived_from , :expressions]
end
def inspect
self.class.name + ".new(" + @name.inspect + " ," +
@derived_from.inspect + ", " + @expressions.inspect + " )"
end
def derived_from
@derived_from ? @derived_from : :Object
end
def to_s
s = "class #{name} < #{derived_from}\n #{expressions}\nend\n"
name.inspect + " ," + derived_from.inspect + ", " + expressions.inspect
end
end
end

View File

@ -6,14 +6,13 @@ module Ast
def initialize operator, left, right
@operator, @left, @right = operator, left, right
end
def attributes
[:operator, :left, :right]
end
def inspect
self.class.name + ".new(" + operator.inspect + ", " + left.inspect + "," + right.inspect + ")"
end
def to_s
"#{left} #{operator} #{right}"
operator.inspect + ", " + left.inspect + "," + right.inspect
end
end

View File

@ -4,14 +4,12 @@ module Ast
def initialize expression
@expression = expression
end
def inspect
self.class.name + ".new(" + expression.inspect + " )"
end
def to_s
"return #{expression}\n"
end
def attributes
[:expression]
end
def to_s
expression.inspect
end
end
end
end

View File

@ -1,17 +1,17 @@
module Ast
class WhileExpression < Expression
attr_reader :condition, :body
def initialize condition, body
@condition , @body = condition , body
end
def inspect
self.class.name + ".new(" + condition.inspect + ", " + body.inspect + " )"
end
def to_s
"while(#{condition}) do\n" + body.join("\n") + "\nend\n"
end
def attributes
[:condition, :body]
end
def to_s
condition.inspect + ", " + body.inspect
end
end
end
end