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

View File

@ -8,19 +8,14 @@ module Ast
@body_exp = body_exp @body_exp = body_exp
end 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 def attributes
[:call_exp , :args , :body_exp] [:call_exp , :args , :body_exp]
end end
def to_s
call_exp.inspect + ", ["+
args.collect{|m| m.inspect }.join( ",") + "] ," + body_exp.inspect
end
end end
end end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,17 +1,17 @@
module Ast module Ast
class WhileExpression < Expression class WhileExpression < Expression
attr_reader :condition, :body attr_reader :condition, :body
def initialize condition, body def initialize condition, body
@condition , @body = condition , body @condition , @body = condition , body
end 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 def attributes
[:condition, :body] [:condition, :body]
end end
def to_s
condition.inspect + ", " + body.inspect
end
end end
end end

View File

@ -151,10 +151,16 @@ grammar Bosl
end end
rule call_site rule call_site
(basic_expression "." name_expression argument_list space?) { (field_expression argument_list space?) {
Ast::CallSiteExpression.new(capture(:name_expression).to_str , Ast::CallSiteExpression.new(capture(:field_expression).value ,
capture(:argument_list).value , capture(:argument_list).value )
capture(:basic_expression).value ) }
end
rule field_expression
(basic_expression "." name_expression space?) {
Ast::FieldExpression.new(capture(:basic_expression).value ,
capture(:name_expression).value.name )
} }
end end
@ -234,7 +240,7 @@ grammar Bosl
end end
rule statement rule statement
conditional | while | return | variable_definition | assignment conditional | while | return | variable_definition | assignment | call_site
end end

View File

@ -5,44 +5,38 @@ class TestCallSite < MiniTest::Test
def test_single_self def test_single_self
@input = 'self.foo(42)' @input = 'self.foo(42)'
@output = Ast::CallSiteExpression.new :foo, [Ast::IntegerExpression.new(42)] @output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::NameExpression.new(:self),:foo), [Ast::IntegerExpression.new(42)] )
check :call_site check :call_site
end end
def test_single_name def test_single_name
@input = 'my_my.foo(42)' @input = 'my_my.foo(42)'
@parse_output = {:receiver=>{:name=>"my_my"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]} @parse_output = {:receiver=>{:name=>"my_my"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
@output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::NameExpression.new("my_my")) @output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::NameExpression.new(:my_my),:foo), [Ast::IntegerExpression.new(42)] )
check :call_site check :call_site
end end
def test_int_receiver def test_int_receiver
@input = '42.put()' @input = '42.put()'
@output = Ast::CallSiteExpression.new(:put, [] ,Ast::IntegerExpression.new(42)) @output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::IntegerExpression.new(42),:put), [] )
check :call_site check :call_site
end end
def test_string_receiver def test_string_receiver
@input = '"hello".puts()' @input = '"hello".puts()'
@output = Ast::CallSiteExpression.new(:puts, [] ,Ast::StringExpression.new("hello")) @output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::StringExpression.new("hello"),:puts), [] )
check :call_site check :call_site
end end
def test_call_site_2 def test_call_site_2
@input = 'self.baz(42, foo)' @input = 'self.baz(42, foo)'
@output = Ast::CallSiteExpression.new :baz, @output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::NameExpression.new(:self),:baz), [Ast::IntegerExpression.new(42),Ast::NameExpression.new(:foo)] )
[Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo") ] ,
Ast::NameExpression.new(:self)
check :call_site check :call_site
end end
def test_call_site_3 def test_call_site_3
@input = 'self.baz(42, foo , bar)' @input = 'self.baz(42, foo , bar)'
@output = Ast::CallSiteExpression.new :baz, @output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::NameExpression.new(:self),:baz), [Ast::IntegerExpression.new(42),Ast::NameExpression.new(:foo),Ast::NameExpression.new(:bar)] )
[Ast::IntegerExpression.new(42) ,
Ast::NameExpression.new("foo") ,
Ast::NameExpression.new("bar")] ,
Ast::NameExpression.new(:self)
check :call_site check :call_site
end end

View File

@ -27,18 +27,18 @@ if( 1 )
int num = 42 int num = 42
end end
HERE HERE
@output = Ast::IfExpression.new(Ast::IntegerExpression.new(1), [Ast::VariableDefinition.new(:int,:num,Ast::IntegerExpression.new(42))],nil ) @output = Ast::IfExpression.new(Ast::IntegerExpression.new(1), [Ast::VariableDefinition.new(:int,:num,Ast::IntegerExpression.new(42))],nil)
check :conditional check :conditional
end end
def ttest_conditional_with_calls def test_conditional_with_calls
@input = <<HERE @input = <<HERE
if(var) if(var)
42.add(5)
end end
HERE HERE
@output = Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::IntegerExpression.new(3),Ast::NameExpression.new("var")), [Ast::CallSiteExpression.new(:initialize, [Ast::IntegerExpression.new(3)] ,Ast::ModuleName.new("Object"))],[Ast::CallSiteExpression.new(:new, [Ast::IntegerExpression.new(33)] ,Ast::NameExpression.new("var"))] ) @output = Ast::IfExpression.new(Ast::NameExpression.new(:var), [Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::IntegerExpression.new(42),:add), [Ast::IntegerExpression.new(5)] )],nil)
check check :conditional
end end
def ttest_conditional_nil def ttest_conditional_nil
@ -67,10 +67,4 @@ HERE
@root = :conditional @root = :conditional
end end
def pest_reverse_if
@input = "3 if(3 == nil)"
@parse_output = {:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"== ", :r=>{:nil=>"nil"}}, :if_true=>{:expressions=>[{:integer=>"3"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"4"}], :end=>"end"}}
@output = Ast::IfExpression.new(Ast::OperatorExpression.new("==", Ast::IntegerExpression.new(3),Ast::NilExpression.new()), [Ast::IntegerExpression.new(3)],[Ast::IntegerExpression.new(4)] )
@root = :conditional
end
end end