lib/to_source/visitor.rb in to_source-0.0.1 vs lib/to_source/visitor.rb in to_source-0.1.0

- old
+ new

@@ -85,10 +85,16 @@ node.start.lazy_visit self, node emit '..' node.finish.lazy_visit self, node end + def range_exclude(node, parent) + node.start.lazy_visit self, node + emit '...' + node.finish.lazy_visit self, node + end + def regex_literal(node, parent) emit '/' emit node.source emit '/' end @@ -213,9 +219,93 @@ def scoped_constant(node, parent) node.parent.lazy_visit self, node emit '::' emit node.name + end + + def if(node, parent) + body, else_body = node.body, node.else + keyword = 'if' + + if node.body.is_a?(Rubinius::AST::NilLiteral) && !node.else.is_a?(Rubinius::AST::NilLiteral) + + body, else_body = else_body, body + keyword = 'unless' + end + + emit keyword << ' ' + node.condition.lazy_visit self, node + emit "\n" + + @indentation += 1 + + if body.is_a?(Rubinius::AST::Block) + body.lazy_visit self, parent, true + else + emit current_indentation + body.lazy_visit self, parent + end + + emit "\n" + + if else_body.is_a?(Rubinius::AST::NilLiteral) + emit 'end' + return + end + + emit "else\n" + + if else_body.is_a?(Rubinius::AST::Block) + else_body.lazy_visit self, parent, true + else + emit current_indentation + else_body.lazy_visit self, parent + end + + emit "\n" + emit 'end' + end + + def while(node, parent) + emit 'while ' + node.condition.lazy_visit self, node + emit "\n" + + @indentation += 1 + + if node.body.is_a?(Rubinius::AST::Block) + node.body.lazy_visit self, parent, true + else + emit current_indentation + node.body.lazy_visit self, parent + end + + emit "\n" + emit "end" + end + + def until(node, parent) + emit 'until ' + node.condition.lazy_visit self, node + emit "\n" + + @indentation += 1 + + if node.body.is_a?(Rubinius::AST::Block) + node.body.lazy_visit self, parent, true + else + emit current_indentation + node.body.lazy_visit self, parent + end + + emit "\n" + emit "end" + end + + def return(node, parent) + emit 'return ' + node.value.lazy_visit self, parent end private def process_binary_operator(node, parent)