$aFile = File.open("riddle.rb", "r") $strings = [] $comments = [] $body = "" def escapeInHex(c) case c when "a" then return 0x07 # Bell when "b" then return 0x08 # Backspace when "e" then return 0x1b # Escape when "f" then return 0x0c # Formfeed when "n" then return 0x0a # New Line when "r" then return 0x0d # Carriage Return when "s" then return 0x20 # Space when "t" then return 0x09 # Tab when "v" then return 0x0b # Vertical Tab when "\\" then return "\\" # BackSlash end return '' end def comment str = $aFile.gets $body << "#comments[#{$comments.size}]\n" $comments << str end def singleString str = "" while(c2 = $aFile.getc) c = c2.chr if(c == "'") break elsif(c == "\\") c2 = $aFile.getc if(!c2) break elsif(c2.chr =~ /[abefnrstv]|\\/) str << escapeInHex(c2.chr) else str << c + c2.chr end else str << c end end return str end def doubleString str = "" last = "" while (c2 = $aFile.getc) c = c2.chr if c == "\"" and last == "\\" str << c last = "" elsif c == "\"" str << last break elsif (last == "\\") and (c =~ /[abefnrstv]|\\/) str << escapeInHex(c) last = "" elsif (last == "\\") and (c =~ /[0-7]/) print "octal parsing not yet implemented" str << last last == c elsif (last == "\\") and (c =~ /[xX]/) print "hex parsing not yet implemented" str << last last == c elsif (last == "\\") and (c =~ "[cC]") print "control parsing not yet implemented" str << last last == c elsif (last == "\\") and (c == "M") print "Meta parsing not yet implemented" str << last last == c elsif (last == "\\") and (c == "M") print "Meta-control parsing not yet implemented" str << last last == c elsif (last == "#") and (c == "{") print "expression parsing not yet implemented" str << last last == c else str << last last = c end end $body << "strings[#{$strings.size}]" $strings << str end while (ch = $aFile.getc) case ch.chr when "#" comment when "'" str = singleString $body << "strings[#{$strings.size}]" $strings << str when '"' doubleString else $body << ch end # case end # while print "\nstrings: #{$strings.size}\n", $strings.inspect print "\ncomments: #{$comments.size}\n", $comments.inspect, "\n" print $body print "\n"