Skip to content

Instantly share code, notes, and snippets.

@FireyFly
Created February 13, 2014 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FireyFly/8985786 to your computer and use it in GitHub Desktop.
Save FireyFly/8985786 to your computer and use it in GitHub Desktop.
GTB compiler thing
// Use \ to escape []%
var map = { "\\[": "[",
"\\]": "]",
"\\%": "%",
"[": ":Lbl 0:",
"]": ":Goto 0:",
"@": ":If ",
"#": ":Else",
"$": ":Then:",
"&": ":End:",
"&&": " and ",
"\\": " or ",
"^^": " xor ",
"!!": "not(",
"~": ":Disp ",
"`": ":Input ",
"_": "Str0",
"p;": ":Prompt ",
"(;": "(((",
");": ")))",
"r;": "rand",
"r?": "randInt(",
"n?": "randIntNoRep(",
"++": "+100",
"--": "-100",
"g;": ":Goto ",
"l;": ":Lbl ",
"h;": ":Horizontal",
"v;": ":Vertical",
"p;": ":Pause:",
"r?": ":Repeat",
"%": ":While ",
"x?": "expr(",
"4;": ":For(",
"s;": "sub(",
"S;": "inString(",
"A;": "Ans",
"l?": "length(",
"w;": "1",
"eq;": "=",
"4;": ":For(",
"c;": ":ClrDraw:",
"i;": ":DrawInv",
"%;": "fpart(",
"A?": ":SortA(",
"S?": ":{0,1→L₁:{0,Ans→L₂:LinReg(ax+b) Y₁:Equ►String(Y₁,Str0:sub(Str0,1,length(Str0)-3→Str0" }
function expand(str) {
var pattern = new RegExp(Object.keys(map).map(escape).join("|"), 'g')
return str.replace(pattern, function (from) { return map[from] })
function escape(str) { return str.replace(/[\[\]()^$*+?\\]/g, "\\$&") }
}
if (process.argv.length != 3) {
console.warn("Usage: %s %s <GTB source code>", process.argv[0], process.argv[1])
process.exit(1)
}
console.log(expand(process.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment