Class: LogoNxc
- Inherits:
-
Object
- Object
- LogoNxc
- Defined in:
- lib/logonxc.rb
Class Method Summary collapse
- .nxc(logo) ⇒ Object
-
.parse(text) ⇒ Object
ok so this probably isn’t the best way to write a language parser :).
- .wrap(nxc_block) ⇒ Object
Class Method Details
.nxc(logo) ⇒ Object
43 44 45 |
# File 'lib/logonxc.rb', line 43 def self.nxc(logo) wrap(parse(logo)) end |
.parse(text) ⇒ Object
ok so this probably isn’t the best way to write a language parser :)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/logonxc.rb', line 12 def self.parse text output = "" text.each_line do |line| case when line =~ /print\s+["']([^"']+)["']/ output << "print(\"#{$1}\");\n" when line =~ /sleep\s+(\d+)/ output << "wait(#{$1});\n" when line =~ /beep/ output << "beep();\n" when line =~ /repeat\s+(\d+)\s+\[/ output << "repeat(#{$1}) {\n" when line =~ /(?:right|rt)\s*(\d+)/ output << "right(#{$1});\n" when line =~ /(?:left|lt)\s*(\d+)/ output << "left(#{$1});\n" when line =~ /(?:forward|fd)\s*(\d+)/ output << "forward(#{$1});\n" when line =~ /(?:back|bk)\s*(\d+)/ output << "back(#{$1});\n" when line =~ /penup/ output << "penup();\n" when line =~ /pendown/ output << "pendown();\n" when line =~ /\]/ output << "}\n" end end output end |
.wrap(nxc_block) ⇒ Object
5 6 7 8 9 |
# File 'lib/logonxc.rb', line 5 def self.wrap(nxc_block) tpl = File.(File.dirname(__FILE__) + "/../templates/logonxc.erb") template = Erubis::Eruby.new File.new(tpl).read template.result(:nxc_block => nxc_block) end |