fix to_s (mostly)

This commit is contained in:
2019-09-19 20:48:21 +03:00
parent 1e2c4d6678
commit 113ba8607c
14 changed files with 33 additions and 24 deletions

View File

@ -8,7 +8,7 @@ module Ruby
def to_s(depth = 0)
arg_str = @args.collect{|a| a.to_s}.join(', ')
at_depth(depth , "def self.#{name}(#{arg_str})" , @body.to_s(depth + 1) , "end")
at_depth(depth , "def self.#{name}(#{arg_str})\n#{@body.to_s(depth + 1)}\nend")
end
end

View File

@ -37,11 +37,12 @@ module Ruby
end
def to_s(depth = 0)
parts = ["if(#{@condition})" ]
parts << @if_true.to_s(depth + 1) if(@if_true)
parts += ["else" , @if_false.to_s(depth + 1)] if(@if_false)
parts << "end"
at_depth(depth , *parts )
parts = "if(#{@condition})\n"
parts += " #{@if_true.to_s(depth + 1)}\n" if(@if_true)
parts += "else\n" if(@if_false)
parts += " #{@if_false.to_s(depth + 1)}\n" if(@if_false)
parts += "end\n"
at_depth(depth , parts )
end
end
end

View File

@ -1,11 +1,10 @@
module Ruby
class MethodStatement < Statement
attr_reader :name, :args , :body , :clazz
attr_reader :name, :args , :body
def initialize( name , args , body , clazz = nil)
def initialize( name , args , body)
@name , @args , @body = name , args , body
raise "no bod" unless @body
@clazz = clazz
end
# At the moment normalizing means creating implicit returns for some cases

View File

@ -24,8 +24,9 @@ module Ruby
# helper method for formatting source code
# depth is the depth in the tree (os the ast)
# and the string are the ones to be indented (2 spaces)
def at_depth(depth , *strings)
def at_depth(depth , lines)
prefix = " " * 2 * depth
strings = lines.split("\n")
strings.collect{|str| prefix + str}.join("\n")
end
end

View File

@ -45,7 +45,7 @@ module Ruby
end
def to_s(depth = 0)
at_depth(depth , *@statements.collect{|st| st.to_s(depth)})
at_depth(depth , @statements.collect{|st| st.to_s(depth)}.join("\n"))
end
end