Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

Commit

Permalink
Pretty indented results from compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbeam committed May 5, 2016
1 parent 8c5c96b commit 3459bf4
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions lib/spoon/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,58 @@ def initialize
}
end

def compile(node, parent = nil)
@nodes[node.type].new(self, node, parent).compile.to_s
def compile(node, parent = nil, tab = "")
@nodes[node.type].new(self, node, parent, tab).compile.to_s
end
end

class Base
def initialize(compiler, node, parent)
def initialize(compiler, node, parent, tab)
@compiler = compiler
@node = node
@parent = parent
@content = ""
@tab = tab
end

def compile
@content = ""
@content
end

def compile_next(node)
@compiler.compile(node, self)
@compiler.compile(node, @node, @tab + " ")
end
end

class Root < Base
def compile
def initialize(compiler, node, parent, tab)
super
@tab = " "
end

def compile
imports = ""
@content << "class Main {\n"
@content << "static public function main() {\n"
@content << " static public function main() {\n"

@node.children.each do |child|
if child.type == :import
imports << compile_next(child) << ";\n"
else
@content << compile_next(child) << ";\n"
@content << @tab << compile_next(child) << ";\n"
end
end

@content = "#{imports}\n#{@content}"
@content << "}\n"
@content << " }\n"
@content << "}"

super
end
end

class Operation < Base
def compile
super
children = @node.children.dup
operator = children.shift.to_s

Expand All @@ -71,19 +77,20 @@ def compile
@content << compile_next(children.shift)
@content << operator
end

super
end
end

class Value < Base
def compile
super
@content << @node.children.dup.shift.to_s
super
end
end

class Call < Base
def compile
super
children = @node.children.dup
@content << children.shift.to_s
@content << "("
Expand All @@ -94,12 +101,12 @@ def compile
end

@content << ")"
super
end
end

class Import < Base
def compile
super
children = @node.children.dup

@content << "import "
Expand All @@ -109,7 +116,7 @@ def compile
@content << "." unless children.last == child
end

@content
super
end
end
end

0 comments on commit 3459bf4

Please sign in to comment.