Skip to content

Instantly share code, notes, and snippets.

@thomasfoster96
Created June 28, 2016 14:39
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 thomasfoster96/6fe11b6ed40dd3c1de86ecd53ce8e45f to your computer and use it in GitHub Desktop.
Save thomasfoster96/6fe11b6ed40dd3c1de86ecd53ce8e45f to your computer and use it in GitHub Desktop.
An ECMAScript to English transpiler.
function transpile(token){
switch(token.type){
case "Literal":
switch(typeof token.value){
case "string":
return `"${token.value}"`;
case "boolean":
return `${token.value ? "true" : "false"}`;
case "null":
return "nothing";
case "number":
return `${token.value}`;
}
case "RegExpLiteral":
return `the regular expression "${token.regex.pattern}" with the flags "${token.regex.flags}"`;
case "Program":
return token.body.map(statement => transpile(statement)).join('');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment