pp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /* Pretty-print input source by emitting parse tree back as syntax.
  2. * with no flags: pretty-printed source
  3. * with -m: minified source with line breaks
  4. * with -mm: minified source without line breaks
  5. * with -s: s-expression syntax tree
  6. */
  7. #include <stdio.h>
  8. #include <assert.h>
  9. #include "jsi.h"
  10. #include "utf.h"
  11. static const char *astname[] = {
  12. #include "astnames.h"
  13. NULL
  14. };
  15. static const char *opname[] = {
  16. #include "opnames.h"
  17. NULL
  18. };
  19. static int format = 0;
  20. static int minify = 0;
  21. static void pc(int c)
  22. {
  23. putchar(c);
  24. }
  25. static void ps(const char *s)
  26. {
  27. fputs(s, stdout);
  28. }
  29. static void in(int d)
  30. {
  31. if (minify < 1)
  32. while (d-- > 0)
  33. putchar('\t');
  34. }
  35. static void nl(void)
  36. {
  37. if (minify < 2)
  38. putchar('\n');
  39. }
  40. static void sp(void)
  41. {
  42. if (minify < 1)
  43. putchar(' ');
  44. }
  45. static void comma(void)
  46. {
  47. putchar(',');
  48. sp();
  49. }
  50. static void pstr(const char *s)
  51. {
  52. static const char *HEX = "0123456789ABCDEF";
  53. Rune c;
  54. pc(minify ? '\'' : '"');
  55. while (*s) {
  56. s += chartorune(&c, s);
  57. switch (c) {
  58. case '\'': ps("\\'"); break;
  59. case '"': ps("\\\""); break;
  60. case '\\': ps("\\\\"); break;
  61. case '\b': ps("\\b"); break;
  62. case '\f': ps("\\f"); break;
  63. case '\n': ps("\\n"); break;
  64. case '\r': ps("\\r"); break;
  65. case '\t': ps("\\t"); break;
  66. default:
  67. if (c < ' ' || c > 127) {
  68. ps("\\u");
  69. pc(HEX[(c>>12)&15]);
  70. pc(HEX[(c>>8)&15]);
  71. pc(HEX[(c>>4)&15]);
  72. pc(HEX[c&15]);
  73. } else {
  74. pc(c); break;
  75. }
  76. }
  77. }
  78. pc(minify ? '\'' : '"');
  79. }
  80. static void pregexp(const char *prog, int flags)
  81. {
  82. pc('/');
  83. while (*prog) {
  84. if (*prog == '/')
  85. pc('\\');
  86. pc(*prog);
  87. ++prog;
  88. }
  89. pc('/');
  90. if (flags & JS_REGEXP_G) pc('g');
  91. if (flags & JS_REGEXP_I) pc('i');
  92. if (flags & JS_REGEXP_M) pc('m');
  93. }
  94. /* Bytecode */
  95. static void jsC_dumpfunction(js_State *J, js_Function *F)
  96. {
  97. js_Instruction *p = F->code;
  98. js_Instruction *end = F->code + F->codelen;
  99. char *s;
  100. double n;
  101. int i;
  102. printf("%s(%d)\n", F->name, F->numparams);
  103. if (F->strict) printf("\tstrict\n");
  104. if (F->lightweight) printf("\tlightweight\n");
  105. if (F->arguments) printf("\targuments\n");
  106. printf("\tsource %s:%d\n", F->filename, F->line);
  107. for (i = 0; i < F->funlen; ++i)
  108. printf("\tfunction %d %s\n", i, F->funtab[i]->name);
  109. for (i = 0; i < F->varlen; ++i)
  110. printf("\tlocal %d %s\n", i + 1, F->vartab[i]);
  111. printf("{\n");
  112. while (p < end) {
  113. int ln = *p++;
  114. int c = *p++;
  115. printf("%5d(%3d): ", (int)(p - F->code) - 2, ln);
  116. ps(opname[c]);
  117. switch (c) {
  118. case OP_INTEGER:
  119. printf(" %ld", (long)((*p++) - 32768));
  120. break;
  121. case OP_NUMBER:
  122. memcpy(&n, p, sizeof(n));
  123. p += sizeof(n) / sizeof(*p);
  124. printf(" %.9g", n);
  125. break;
  126. case OP_STRING:
  127. memcpy(&s, p, sizeof(s));
  128. p += sizeof(s) / sizeof(*p);
  129. pc(' ');
  130. pstr(s);
  131. break;
  132. case OP_NEWREGEXP:
  133. pc(' ');
  134. memcpy(&s, p, sizeof(s));
  135. p += sizeof(s) / sizeof(*p);
  136. pregexp(s, *p++);
  137. break;
  138. case OP_GETVAR:
  139. case OP_HASVAR:
  140. case OP_SETVAR:
  141. case OP_DELVAR:
  142. case OP_GETPROP_S:
  143. case OP_SETPROP_S:
  144. case OP_DELPROP_S:
  145. case OP_CATCH:
  146. memcpy(&s, p, sizeof(s));
  147. p += sizeof(s) / sizeof(*p);
  148. pc(' ');
  149. ps(s);
  150. break;
  151. case OP_GETLOCAL:
  152. case OP_SETLOCAL:
  153. case OP_DELLOCAL:
  154. printf(" %s", F->vartab[*p++ - 1]);
  155. break;
  156. case OP_CLOSURE:
  157. case OP_CALL:
  158. case OP_NEW:
  159. case OP_JUMP:
  160. case OP_JTRUE:
  161. case OP_JFALSE:
  162. case OP_JCASE:
  163. case OP_TRY:
  164. printf(" %ld", (long)*p++);
  165. break;
  166. }
  167. putchar('\n');
  168. }
  169. printf("}\n");
  170. for (i = 0; i < F->funlen; ++i) {
  171. if (F->funtab[i] != F) {
  172. printf("function %d ", i);
  173. jsC_dumpfunction(J, F->funtab[i]);
  174. }
  175. }
  176. }
  177. /* Pretty-printed Javascript syntax */
  178. static int prec(enum js_AstType type)
  179. {
  180. switch (type) {
  181. case AST_IDENTIFIER:
  182. case EXP_IDENTIFIER:
  183. case EXP_NUMBER:
  184. case EXP_STRING:
  185. case EXP_REGEXP:
  186. case EXP_ELISION:
  187. case EXP_NULL:
  188. case EXP_TRUE:
  189. case EXP_FALSE:
  190. case EXP_THIS:
  191. case EXP_ARRAY:
  192. case EXP_OBJECT:
  193. return 170;
  194. case EXP_FUN:
  195. case EXP_INDEX:
  196. case EXP_MEMBER:
  197. case EXP_CALL:
  198. case EXP_NEW:
  199. return 160;
  200. case EXP_POSTINC:
  201. case EXP_POSTDEC:
  202. return 150;
  203. case EXP_DELETE:
  204. case EXP_VOID:
  205. case EXP_TYPEOF:
  206. case EXP_PREINC:
  207. case EXP_PREDEC:
  208. case EXP_POS:
  209. case EXP_NEG:
  210. case EXP_BITNOT:
  211. case EXP_LOGNOT:
  212. return 140;
  213. case EXP_MOD:
  214. case EXP_DIV:
  215. case EXP_MUL:
  216. return 130;
  217. case EXP_SUB:
  218. case EXP_ADD:
  219. return 120;
  220. case EXP_USHR:
  221. case EXP_SHR:
  222. case EXP_SHL:
  223. return 110;
  224. case EXP_IN:
  225. case EXP_INSTANCEOF:
  226. case EXP_GE:
  227. case EXP_LE:
  228. case EXP_GT:
  229. case EXP_LT:
  230. return 100;
  231. case EXP_STRICTNE:
  232. case EXP_STRICTEQ:
  233. case EXP_NE:
  234. case EXP_EQ:
  235. return 90;
  236. case EXP_BITAND: return 80;
  237. case EXP_BITXOR: return 70;
  238. case EXP_BITOR: return 60;
  239. case EXP_LOGAND: return 50;
  240. case EXP_LOGOR: return 40;
  241. case EXP_COND:
  242. return 30;
  243. case EXP_ASS:
  244. case EXP_ASS_MUL:
  245. case EXP_ASS_DIV:
  246. case EXP_ASS_MOD:
  247. case EXP_ASS_ADD:
  248. case EXP_ASS_SUB:
  249. case EXP_ASS_SHL:
  250. case EXP_ASS_SHR:
  251. case EXP_ASS_USHR:
  252. case EXP_ASS_BITAND:
  253. case EXP_ASS_BITXOR:
  254. case EXP_ASS_BITOR:
  255. return 20;
  256. #define COMMA 15
  257. case EXP_COMMA:
  258. return 10;
  259. default:
  260. return 0;
  261. }
  262. }
  263. static void pstmlist(int d, js_Ast *list);
  264. static void pexpi(int d, int i, js_Ast *exp);
  265. static void pstm(int d, js_Ast *stm);
  266. static void slist(int d, js_Ast *list);
  267. static void sblock(int d, js_Ast *list);
  268. static void pargs(int d, js_Ast *list)
  269. {
  270. while (list) {
  271. assert(list->type == AST_LIST);
  272. pexpi(d, COMMA, list->a);
  273. list = list->b;
  274. if (list)
  275. comma();
  276. }
  277. }
  278. static void parray(int d, js_Ast *list)
  279. {
  280. pc('[');
  281. while (list) {
  282. assert(list->type == AST_LIST);
  283. pexpi(d, COMMA, list->a);
  284. list = list->b;
  285. if (list)
  286. comma();
  287. }
  288. pc(']');
  289. }
  290. static void pobject(int d, js_Ast *list)
  291. {
  292. pc('{');
  293. if (list) {
  294. nl();
  295. in(d+1);
  296. }
  297. while (list) {
  298. js_Ast *kv = list->a;
  299. assert(list->type == AST_LIST);
  300. switch (kv->type) {
  301. default: break;
  302. case EXP_PROP_VAL:
  303. pexpi(d+1, COMMA, kv->a);
  304. pc(':'); sp();
  305. pexpi(d+1, COMMA, kv->b);
  306. break;
  307. case EXP_PROP_GET:
  308. ps("get ");
  309. pexpi(d+1, COMMA, kv->a);
  310. ps("()"); sp(); pc('{'); nl();
  311. pstmlist(d+1, kv->c);
  312. in(d+1); pc('}');
  313. break;
  314. case EXP_PROP_SET:
  315. ps("set ");
  316. pexpi(d+1, COMMA, kv->a);
  317. pc('(');
  318. pargs(d+1, kv->b);
  319. pc(')'); sp(); pc('{'); nl();
  320. pstmlist(d+1, kv->c);
  321. in(d+1); pc('}');
  322. break;
  323. }
  324. list = list->b;
  325. if (list) {
  326. pc(',');
  327. nl();
  328. in(d+1);
  329. } else {
  330. nl();
  331. in(d);
  332. }
  333. }
  334. pc('}');
  335. }
  336. static void pbin(int d, int p, js_Ast *exp, const char *op)
  337. {
  338. pexpi(d, p, exp->a);
  339. sp();
  340. ps(op);
  341. sp();
  342. pexpi(d, p, exp->b);
  343. }
  344. static void puna(int d, int p, js_Ast *exp, const char *pre, const char *suf)
  345. {
  346. ps(pre);
  347. pexpi(d, p, exp->a);
  348. ps(suf);
  349. }
  350. static void pexpi(int d, int p, js_Ast *exp)
  351. {
  352. int tp, paren;
  353. if (!exp) return;
  354. tp = prec(exp->type);
  355. paren = 0;
  356. if (tp < p) {
  357. pc('(');
  358. paren = 1;
  359. }
  360. p = tp;
  361. switch (exp->type) {
  362. case AST_IDENTIFIER: ps(exp->string); break;
  363. case EXP_IDENTIFIER: ps(exp->string); break;
  364. case EXP_NUMBER: printf("%.9g", exp->number); break;
  365. case EXP_STRING: pstr(exp->string); break;
  366. case EXP_REGEXP: pregexp(exp->string, exp->number); break;
  367. case EXP_ELISION: ps("elision"); break;
  368. case EXP_NULL: ps("null"); break;
  369. case EXP_TRUE: ps("true"); break;
  370. case EXP_FALSE: ps("false"); break;
  371. case EXP_THIS: ps("this"); break;
  372. case EXP_OBJECT: pobject(d, exp->a); break;
  373. case EXP_ARRAY: parray(d, exp->a); break;
  374. case EXP_DELETE: puna(d, p, exp, "delete ", ""); break;
  375. case EXP_VOID: puna(d, p, exp, "void ", ""); break;
  376. case EXP_TYPEOF: puna(d, p, exp, "typeof ", ""); break;
  377. case EXP_PREINC: puna(d, p, exp, "++", ""); break;
  378. case EXP_PREDEC: puna(d, p, exp, "--", ""); break;
  379. case EXP_POSTINC: puna(d, p, exp, "", "++"); break;
  380. case EXP_POSTDEC: puna(d, p, exp, "", "--"); break;
  381. case EXP_POS: puna(d, p, exp, "+", ""); break;
  382. case EXP_NEG: puna(d, p, exp, "-", ""); break;
  383. case EXP_BITNOT: puna(d, p, exp, "~", ""); break;
  384. case EXP_LOGNOT: puna(d, p, exp, "!", ""); break;
  385. case EXP_LOGOR: pbin(d, p, exp, "||"); break;
  386. case EXP_LOGAND: pbin(d, p, exp, "&&"); break;
  387. case EXP_BITOR: pbin(d, p, exp, "|"); break;
  388. case EXP_BITXOR: pbin(d, p, exp, "^"); break;
  389. case EXP_BITAND: pbin(d, p, exp, "&"); break;
  390. case EXP_EQ: pbin(d, p, exp, "=="); break;
  391. case EXP_NE: pbin(d, p, exp, "!="); break;
  392. case EXP_STRICTEQ: pbin(d, p, exp, "==="); break;
  393. case EXP_STRICTNE: pbin(d, p, exp, "!=="); break;
  394. case EXP_LT: pbin(d, p, exp, "<"); break;
  395. case EXP_GT: pbin(d, p, exp, ">"); break;
  396. case EXP_LE: pbin(d, p, exp, "<="); break;
  397. case EXP_GE: pbin(d, p, exp, ">="); break;
  398. case EXP_IN: pbin(d, p, exp, "in"); break;
  399. case EXP_SHL: pbin(d, p, exp, "<<"); break;
  400. case EXP_SHR: pbin(d, p, exp, ">>"); break;
  401. case EXP_USHR: pbin(d, p, exp, ">>>"); break;
  402. case EXP_ADD: pbin(d, p, exp, "+"); break;
  403. case EXP_SUB: pbin(d, p, exp, "-"); break;
  404. case EXP_MUL: pbin(d, p, exp, "*"); break;
  405. case EXP_DIV: pbin(d, p, exp, "/"); break;
  406. case EXP_MOD: pbin(d, p, exp, "%"); break;
  407. case EXP_ASS: pbin(d, p, exp, "="); break;
  408. case EXP_ASS_MUL: pbin(d, p, exp, "*="); break;
  409. case EXP_ASS_DIV: pbin(d, p, exp, "/="); break;
  410. case EXP_ASS_MOD: pbin(d, p, exp, "%="); break;
  411. case EXP_ASS_ADD: pbin(d, p, exp, "+="); break;
  412. case EXP_ASS_SUB: pbin(d, p, exp, "-="); break;
  413. case EXP_ASS_SHL: pbin(d, p, exp, "<<="); break;
  414. case EXP_ASS_SHR: pbin(d, p, exp, ">>="); break;
  415. case EXP_ASS_USHR: pbin(d, p, exp, ">>>="); break;
  416. case EXP_ASS_BITAND: pbin(d, p, exp, "&="); break;
  417. case EXP_ASS_BITXOR: pbin(d, p, exp, "^="); break;
  418. case EXP_ASS_BITOR: pbin(d, p, exp, "|="); break;
  419. case EXP_INSTANCEOF:
  420. pexpi(d, p, exp->a);
  421. ps(" instanceof ");
  422. pexpi(d, p, exp->b);
  423. break;
  424. case EXP_COMMA:
  425. pexpi(d, p, exp->a);
  426. pc(','); sp();
  427. pexpi(d, p, exp->b);
  428. break;
  429. case EXP_COND:
  430. pexpi(d, p, exp->a);
  431. sp(); pc('?'); sp();
  432. pexpi(d, p, exp->b);
  433. sp(); pc(':'); sp();
  434. pexpi(d, p, exp->c);
  435. break;
  436. case EXP_INDEX:
  437. pexpi(d, p, exp->a);
  438. pc('[');
  439. pexpi(d, 0, exp->b);
  440. pc(']');
  441. break;
  442. case EXP_MEMBER:
  443. pexpi(d, p, exp->a);
  444. pc('.');
  445. pexpi(d, 0, exp->b);
  446. break;
  447. case EXP_CALL:
  448. pexpi(d, p, exp->a);
  449. pc('(');
  450. pargs(d, exp->b);
  451. pc(')');
  452. break;
  453. case EXP_NEW:
  454. ps("new ");
  455. pexpi(d, p, exp->a);
  456. pc('(');
  457. pargs(d, exp->b);
  458. pc(')');
  459. break;
  460. case EXP_FUN:
  461. if (p == 0) pc('(');
  462. ps("function ");
  463. pexpi(d, 0, exp->a);
  464. pc('(');
  465. pargs(d, exp->b);
  466. pc(')'); sp(); pc('{'); nl();
  467. pstmlist(d, exp->c);
  468. in(d); pc('}');
  469. if (p == 0) pc(')');
  470. break;
  471. default:
  472. ps("<UNKNOWN>");
  473. break;
  474. }
  475. if (paren) pc(')');
  476. }
  477. static void pexp(int d, js_Ast *exp)
  478. {
  479. pexpi(d, 0, exp);
  480. }
  481. static void pvar(int d, js_Ast *var)
  482. {
  483. assert(var->type == EXP_VAR);
  484. pexp(d, var->a);
  485. if (var->b) {
  486. sp(); pc('='); sp();
  487. pexp(d, var->b);
  488. }
  489. }
  490. static void pvarlist(int d, js_Ast *list)
  491. {
  492. while (list) {
  493. assert(list->type == AST_LIST);
  494. pvar(d, list->a);
  495. list = list->b;
  496. if (list)
  497. comma();
  498. }
  499. }
  500. static void pblock(int d, js_Ast *block)
  501. {
  502. assert(block->type == STM_BLOCK);
  503. pc('{'); nl();
  504. pstmlist(d, block->a);
  505. in(d); pc('}');
  506. }
  507. static void pstmh(int d, js_Ast *stm)
  508. {
  509. if (stm->type == STM_BLOCK) {
  510. sp();
  511. pblock(d, stm);
  512. } else {
  513. nl();
  514. pstm(d+1, stm);
  515. }
  516. }
  517. static void pcaselist(int d, js_Ast *list)
  518. {
  519. while (list) {
  520. js_Ast *stm = list->a;
  521. if (stm->type == STM_CASE) {
  522. in(d); ps("case "); pexp(d, stm->a); pc(':'); nl();
  523. pstmlist(d, stm->b);
  524. }
  525. if (stm->type == STM_DEFAULT) {
  526. in(d); ps("default:"); nl();
  527. pstmlist(d, stm->a);
  528. }
  529. list = list->b;
  530. }
  531. }
  532. static void pstm(int d, js_Ast *stm)
  533. {
  534. if (stm->type == STM_BLOCK) {
  535. pblock(d, stm);
  536. return;
  537. }
  538. in(d);
  539. switch (stm->type) {
  540. case AST_FUNDEC:
  541. ps("function ");
  542. pexp(d, stm->a);
  543. pc('(');
  544. pargs(d, stm->b);
  545. pc(')'); sp(); pc('{'); nl();
  546. pstmlist(d, stm->c);
  547. in(d); pc('}');
  548. break;
  549. case STM_EMPTY:
  550. pc(';');
  551. break;
  552. case STM_VAR:
  553. ps("var ");
  554. pvarlist(d, stm->a);
  555. pc(';');
  556. break;
  557. case STM_IF:
  558. ps("if"); sp(); pc('('); pexp(d, stm->a); pc(')');
  559. pstmh(d, stm->b);
  560. if (stm->c) {
  561. nl(); in(d); ps("else");
  562. pstmh(d, stm->c);
  563. }
  564. break;
  565. case STM_DO:
  566. ps("do");
  567. pstmh(d, stm->a);
  568. nl();
  569. in(d); ps("while"); sp(); pc('('); pexp(d, stm->b); pc(')'); pc(';');
  570. break;
  571. case STM_WHILE:
  572. ps("while"); sp(); pc('('); pexp(d, stm->a); pc(')');
  573. pstmh(d, stm->b);
  574. break;
  575. case STM_FOR:
  576. ps("for"); sp(); pc('(');
  577. pexp(d, stm->a); pc(';'); sp();
  578. pexp(d, stm->b); pc(';'); sp();
  579. pexp(d, stm->c); pc(')');
  580. pstmh(d, stm->d);
  581. break;
  582. case STM_FOR_VAR:
  583. ps("for"); sp(); ps("(var ");
  584. pvarlist(d, stm->a); pc(';'); sp();
  585. pexp(d, stm->b); pc(';'); sp();
  586. pexp(d, stm->c); pc(')');
  587. pstmh(d, stm->d);
  588. break;
  589. case STM_FOR_IN:
  590. ps("for"); sp(); pc('(');
  591. pexp(d, stm->a); ps(" in ");
  592. pexp(d, stm->b); pc(')');
  593. pstmh(d, stm->c);
  594. break;
  595. case STM_FOR_IN_VAR:
  596. ps("for"); sp(); ps("(var ");
  597. pvarlist(d, stm->a); ps(" in ");
  598. pexp(d, stm->b); pc(')');
  599. pstmh(d, stm->c);
  600. break;
  601. case STM_CONTINUE:
  602. ps("continue");
  603. if (stm->a) {
  604. pc(' '); pexp(d, stm->a);
  605. }
  606. pc(';');
  607. break;
  608. case STM_BREAK:
  609. ps("break");
  610. if (stm->a) {
  611. pc(' '); pexp(d, stm->a);
  612. }
  613. pc(';');
  614. break;
  615. case STM_RETURN:
  616. ps("return");
  617. if (stm->a) {
  618. pc(' '); pexp(d, stm->a);
  619. }
  620. pc(';');
  621. break;
  622. case STM_WITH:
  623. ps("with"); sp(); pc('('); pexp(d, stm->a); pc(')');
  624. pstmh(d, stm->b);
  625. break;
  626. case STM_SWITCH:
  627. ps("switch"); sp(); pc('(');
  628. pexp(d, stm->a);
  629. pc(')'); sp(); pc('{'); nl();
  630. pcaselist(d, stm->b);
  631. in(d); pc('}');
  632. break;
  633. case STM_THROW:
  634. ps("throw "); pexp(d, stm->a); pc(';');
  635. break;
  636. case STM_TRY:
  637. ps("try");
  638. if (minify && stm->a->type != STM_BLOCK)
  639. pc(' ');
  640. pstmh(d, stm->a);
  641. if (stm->b && stm->c) {
  642. nl(); in(d); ps("catch"); sp(); pc('('); pexp(d, stm->b); pc(')');
  643. pstmh(d, stm->c);
  644. }
  645. if (stm->d) {
  646. nl(); in(d); ps("finally");
  647. pstmh(d, stm->d);
  648. }
  649. break;
  650. case STM_LABEL:
  651. pexp(d, stm->a); pc(':'); sp(); pstm(d, stm->b);
  652. break;
  653. case STM_DEBUGGER:
  654. ps("debugger");
  655. pc(';');
  656. break;
  657. default:
  658. pexp(d, stm);
  659. pc(';');
  660. }
  661. }
  662. static void pstmlist(int d, js_Ast *list)
  663. {
  664. while (list) {
  665. assert(list->type == AST_LIST);
  666. pstm(d+1, list->a);
  667. nl();
  668. list = list->b;
  669. }
  670. }
  671. static void jsP_dumpsyntax(js_State *J, js_Ast *prog)
  672. {
  673. if (prog) {
  674. if (prog->type == AST_LIST)
  675. pstmlist(-1, prog);
  676. else {
  677. pstm(0, prog);
  678. nl();
  679. }
  680. }
  681. if (minify > 1)
  682. putchar('\n');
  683. }
  684. /* S-expression list representation */
  685. static void snode(int d, js_Ast *node)
  686. {
  687. void (*afun)(int,js_Ast*) = snode;
  688. void (*bfun)(int,js_Ast*) = snode;
  689. void (*cfun)(int,js_Ast*) = snode;
  690. void (*dfun)(int,js_Ast*) = snode;
  691. if (!node) {
  692. return;
  693. }
  694. if (node->type == AST_LIST) {
  695. slist(d, node);
  696. return;
  697. }
  698. pc('(');
  699. ps(astname[node->type]);
  700. switch (node->type) {
  701. default: break;
  702. case AST_IDENTIFIER: pc(' '); ps(node->string); break;
  703. case EXP_IDENTIFIER: pc(' '); ps(node->string); break;
  704. case EXP_STRING: pc(' '); pstr(node->string); break;
  705. case EXP_REGEXP: pc(' '); pregexp(node->string, node->number); break;
  706. case EXP_NUMBER: printf(" %.9g", node->number); break;
  707. case STM_BLOCK: afun = sblock; break;
  708. case AST_FUNDEC: case EXP_FUN: cfun = sblock; break;
  709. case EXP_PROP_GET: cfun = sblock; break;
  710. case EXP_PROP_SET: cfun = sblock; break;
  711. case STM_SWITCH: bfun = sblock; break;
  712. case STM_CASE: bfun = sblock; break;
  713. case STM_DEFAULT: afun = sblock; break;
  714. }
  715. if (node->a) { pc(' '); afun(d, node->a); }
  716. if (node->b) { pc(' '); bfun(d, node->b); }
  717. if (node->c) { pc(' '); cfun(d, node->c); }
  718. if (node->d) { pc(' '); dfun(d, node->d); }
  719. pc(')');
  720. }
  721. static void slist(int d, js_Ast *list)
  722. {
  723. pc('[');
  724. while (list) {
  725. assert(list->type == AST_LIST);
  726. snode(d, list->a);
  727. list = list->b;
  728. if (list)
  729. pc(' ');
  730. }
  731. pc(']');
  732. }
  733. static void sblock(int d, js_Ast *list)
  734. {
  735. ps("[\n");
  736. in(d+1);
  737. while (list) {
  738. assert(list->type == AST_LIST);
  739. snode(d+1, list->a);
  740. list = list->b;
  741. if (list) {
  742. nl();
  743. in(d+1);
  744. }
  745. }
  746. nl(); in(d); pc(']');
  747. }
  748. static void jsP_dumplist(js_State *J, js_Ast *prog)
  749. {
  750. if (prog) {
  751. if (prog->type == AST_LIST)
  752. sblock(0, prog);
  753. else
  754. snode(0, prog);
  755. nl();
  756. }
  757. }
  758. static void js_ppstring(js_State *J, const char *filename, const char *source)
  759. {
  760. js_Ast *P;
  761. js_Function *F;
  762. if (js_try(J)) {
  763. jsP_freeparse(J);
  764. js_throw(J);
  765. }
  766. P = jsP_parse(J, filename, source);
  767. F = jsC_compilescript(J, P, J->default_strict);
  768. switch (format) {
  769. case 0:
  770. jsP_dumpsyntax(J, P);
  771. break;
  772. case 1:
  773. jsP_dumplist(J, P);
  774. break;
  775. case 2:
  776. jsC_dumpfunction(J, F);
  777. break;
  778. }
  779. jsP_freeparse(J);
  780. js_endtry(J);
  781. }
  782. static void js_ppfile(js_State *J, const char *filename)
  783. {
  784. FILE * volatile f = NULL;
  785. char * volatile s = NULL;
  786. int n, t;
  787. if (js_try(J)) {
  788. js_free(J, s);
  789. if (f) fclose(f);
  790. js_throw(J);
  791. }
  792. f = fopen(filename, "rb");
  793. if (!f) {
  794. js_error(J, "cannot open file: '%s'", filename);
  795. }
  796. if (fseek(f, 0, SEEK_END) < 0) {
  797. js_error(J, "cannot seek in file: '%s'", filename);
  798. }
  799. n = ftell(f);
  800. if (n < 0) {
  801. js_error(J, "cannot tell in file: '%s'", filename);
  802. }
  803. if (fseek(f, 0, SEEK_SET) < 0) {
  804. js_error(J, "cannot seek in file: '%s'", filename);
  805. }
  806. s = js_malloc(J, n + 1); /* add space for string terminator */
  807. if (!s) {
  808. js_error(J, "cannot allocate storage for file contents: '%s'", filename);
  809. }
  810. t = fread(s, 1, (size_t)n, f);
  811. if (t != n) {
  812. js_error(J, "cannot read data from file: '%s'", filename);
  813. }
  814. s[n] = 0; /* zero-terminate string containing file data */
  815. js_ppstring(J, filename, s);
  816. js_endtry(J);
  817. js_free(J, s);
  818. fclose(f);
  819. }
  820. static void js_tryppfile(js_State *J, const char *file)
  821. {
  822. if (js_try(J)) {
  823. js_report(J, js_trystring(J, -1, "Error"));
  824. js_pop(J, 1);
  825. return;
  826. }
  827. js_ppfile(J, file);
  828. js_endtry(J);
  829. }
  830. int
  831. main(int argc, char **argv)
  832. {
  833. js_State *J;
  834. int i;
  835. if (argc < 2) {
  836. fprintf(stderr, "usage: mujs-pp [-m | -mm | -s | -c] input.js\n");
  837. fprintf(stderr, " -m\tminify output\n");
  838. fprintf(stderr, " -mm\tminify output more\n");
  839. fprintf(stderr, " -s\tprint syntax tree\n");
  840. fprintf(stderr, " -c\tprint bytecode\n");
  841. }
  842. J = js_newstate(NULL, NULL, 0);
  843. for (i = 1; i < argc; ++i) {
  844. if (!strcmp(argv[i], "-m"))
  845. format = 0, minify = 1;
  846. else if (!strcmp(argv[i], "-mm"))
  847. format = 0, minify = 2;
  848. else if (!strcmp(argv[i], "-s"))
  849. format = 1, minify = 0;
  850. else if (!strcmp(argv[i], "-c"))
  851. format = 2, minify = 0;
  852. else
  853. js_tryppfile(J, argv[i]);
  854. }
  855. js_gc(J, 0);
  856. js_freestate(J);
  857. return 0;
  858. }