OLD | NEW |
1 let global = this; | 1 let global = this; |
2 function decompileAST(ast) { | 2 function decompileAST(ast) { |
3 let func = global["decompile" + ast.type]; | 3 let func = global["decompile" + ast.type]; |
4 if (!func) | 4 if (!func) |
5 throw "Unknown type " + ast.type; | 5 throw "Unknown type " + ast.type; |
6 return func(ast); | 6 return func(ast); |
7 } | 7 } |
8 | 8 |
9 function decompileProgram(ast) { | 9 function decompileProgram(ast) { |
10 return ast.body.map(decompileAST).join('\n'); | 10 return ast.body.map(decompileAST).join('\n'); |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 function replace(x) { | 380 function replace(x) { |
381 if (x == q) return '\\' + q; | 381 if (x == q) return '\\' + q; |
382 if (x == '\\') return '\\\\'; | 382 if (x == '\\') return '\\\\'; |
383 if (x == '\b') return '\\b'; | 383 if (x == '\b') return '\\b'; |
384 if (x == '\f') return '\\f'; | 384 if (x == '\f') return '\\f'; |
385 if (x == '\n') return '\\n'; | 385 if (x == '\n') return '\\n'; |
386 if (x == '\r') return '\\r'; | 386 if (x == '\r') return '\\r'; |
387 if (x == '\t') return '\\t'; | 387 if (x == '\t') return '\\t'; |
388 if (x == '\v') return '\\v'; | 388 if (x == '\v') return '\\v'; |
389 let val = x.charCodeAt(0); | 389 let val = x.charCodeAt(0); |
390 if (x < ' ') return '\\x' + (val - val % 16) / 16 + (val % 16); | 390 if (x < ' ') return '\\x' + (val < 16 ? '0' : '') + val.toString(16); |
391 return x; | 391 return x; |
392 } | 392 } |
393 let result = ""; | 393 let result = ""; |
394 for (let char of str) | 394 for (let char of str) |
395 result += replace(char); | 395 result += replace(char); |
396 return result; | 396 return result; |
397 } | 397 } |
398 | 398 |
399 function decompileLiteral(ast) { | 399 function decompileLiteral(ast) { |
400 if (typeof ast.value == "string") | 400 if (typeof ast.value == "string") |
401 return '"' + sanitize(ast.value, '"') + '"'; | 401 return '"' + sanitize(ast.value, '"') + '"'; |
402 if (ast.value === null) | 402 if (ast.value === null) |
403 return "null"; | 403 return "null"; |
404 return ast.value; | 404 return ast.value; |
405 } | 405 } |
406 | 406 |
407 function process_js(ast) { | 407 function process_js(ast) { |
408 _print(decompileAST(ast)); | 408 _print(decompileAST(ast)); |
409 } | 409 } |
OLD | NEW |