lib/kaiser_ruby/rockstar_transform.rb in kaiser-ruby-0.2.1 vs lib/kaiser_ruby/rockstar_transform.rb in kaiser-ruby-0.2.2
- old
+ new
@@ -1,5 +1,6 @@
+require 'pry'
module KaiserRuby
class RockstarTransform < Parslet::Transform
rule(variable_name: simple(:str)) { |c| parameterize(c[:str]) }
rule(nil_value: simple(:_)) { 'nil' }
@@ -28,19 +29,28 @@
rule(print: { output: simple(:output) }) { "puts #{output}" }
rule(equals: { left: simple(:left), right: simple(:right) }) { "#{left} == #{right}" }
rule(if: { if_condition: simple(:if_condition), if_block: sequence(:if_block_lines), endif: simple(:_)} ) do
- "if #{if_condition}\n" +
- if_block_lines.map { |l| "#{l}\n" }.join +
- "end"
+ output = "#{' ' * KaiserRuby.indent}if #{if_condition}\n"
+ KaiserRuby.up_indent
+ output += if_block_lines.map { |l| "#{' ' * KaiserRuby.indent}#{l}\n" }.join
+ KaiserRuby.down_indent
+ output += "#{' ' * KaiserRuby.indent}end"
+ output
end
+
rule(if_else: { if_condition: simple(:if_condition), if_block: sequence(:if_block_lines), else_block: sequence(:else_block_lines), endif: simple(:_)} ) do
- "if #{if_condition}\n" +
- if_block_lines.map { |l| "#{l}\n" }.join +
- "else\n" +
- else_block_lines.map { |l| "#{l}\n" }.join +
- "end"
+ output = "#{' ' * KaiserRuby.indent}if #{if_condition}\n"
+ KaiserRuby.up_indent
+ output += if_block_lines.map { |l| "#{' ' * KaiserRuby.indent}#{l}\n" }.join
+ KaiserRuby.down_indent
+ output += "#{' ' * KaiserRuby.indent}else\n"
+ KaiserRuby.up_indent
+ output += else_block_lines.map { |l| "#{' ' * KaiserRuby.indent}#{l}\n" }.join
+ KaiserRuby.down_indent
+ output += "#{' ' * KaiserRuby.indent}end"
+ output
end
rule(line: simple(:line)) { line == "\n" ? nil : line }
rule(lyrics: sequence(:lines)) { lines.join("\n") + "\n" }
\ No newline at end of file