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

View File

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

View File

@ -5,44 +5,38 @@ class TestCallSite < MiniTest::Test
def test_single_self
@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
end
def test_single_name
@input = 'my_my.foo(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
end
def test_int_receiver
@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
end
def test_string_receiver
@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
end
def test_call_site_2
@input = 'self.baz(42, foo)'
@output = Ast::CallSiteExpression.new :baz,
[Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo") ] ,
Ast::NameExpression.new(:self)
@output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::NameExpression.new(:self),:baz), [Ast::IntegerExpression.new(42),Ast::NameExpression.new(:foo)] )
check :call_site
end
def test_call_site_3
@input = 'self.baz(42, foo , bar)'
@output = Ast::CallSiteExpression.new :baz,
[Ast::IntegerExpression.new(42) ,
Ast::NameExpression.new("foo") ,
Ast::NameExpression.new("bar")] ,
Ast::NameExpression.new(:self)
@output = Ast::CallSiteExpression.new(Ast::FieldExpression.new(Ast::NameExpression.new(:self),:baz), [Ast::IntegerExpression.new(42),Ast::NameExpression.new(:foo),Ast::NameExpression.new(:bar)] )
check :call_site
end

View File

@ -27,18 +27,18 @@ if( 1 )
int num = 42
end
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
end
def ttest_conditional_with_calls
def test_conditional_with_calls
@input = <<HERE
if(var)
42.add(5)
end
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"))] )
check
@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 :conditional
end
def ttest_conditional_nil
@ -67,10 +67,4 @@ HERE
@root = :conditional
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