10 static const char *astname[] = {
14 static const char *opname[] = {
18 const char *jsP_aststring(enum js_AstType type)
20 if (type < nelem(astname))
25 const char *jsC_opcodestring(enum js_OpCode opcode)
27 if (opcode < nelem(opname))
28 return opname[opcode];
32 static int prec(enum js_AstType type)
98 case EXP_BITAND: return 80;
99 case EXP_BITXOR: return 70;
100 case EXP_BITOR: return 60;
101 case EXP_LOGAND: return 50;
102 case EXP_LOGOR: return 40;
131 static void pc(int c)
136 static void ps(const char *s)
141 static void in(int d)
152 /* Pretty-printed Javascript syntax */
154 static void pstmlist(int d, js_Ast *list);
155 static void pexpi(int d, int i, js_Ast *exp);
156 static void pstm(int d, js_Ast *stm);
157 static void slist(int d, js_Ast *list);
158 static void sblock(int d, js_Ast *list);
160 static void pargs(int d, js_Ast *list)
163 assert(list->type == AST_LIST);
164 pexpi(d, COMMA, list->a);
171 static void parray(int d, js_Ast *list)
175 assert(list->type == AST_LIST);
176 pexpi(d, COMMA, list->a);
184 static void pobject(int d, js_Ast *list)
188 js_Ast *kv = list->a;
189 assert(list->type == AST_LIST);
192 pexpi(d, COMMA, kv->a);
194 pexpi(d, COMMA, kv->b);
198 pexpi(d, COMMA, kv->a);
205 pexpi(d, COMMA, kv->a);
220 static void pstr(const char *s)
222 static const char *HEX = "0123456789ABCDEF";
226 s += chartorune(&c, s);
228 case '"': ps("\\\""); break;
229 case '\\': ps("\\\\"); break;
230 case '\b': ps("\\b"); break;
231 case '\f': ps("\\f"); break;
232 case '\n': ps("\\n"); break;
233 case '\r': ps("\\r"); break;
234 case '\t': ps("\\t"); break;
236 if (c < ' ' || c > 127) {
250 static void pregexp(const char *prog, int flags)
255 if (flags & JS_REGEXP_G) pc('g');
256 if (flags & JS_REGEXP_I) pc('i');
257 if (flags & JS_REGEXP_M) pc('m');
260 static void pbin(int d, int p, js_Ast *exp, const char *op)
267 static void puna(int d, int p, js_Ast *exp, const char *pre, const char *suf)
274 static void pexpi(int d, int p, js_Ast *exp)
276 int tp = prec(exp->type);
285 case AST_IDENTIFIER: ps(exp->string); break;
286 case EXP_IDENTIFIER: ps(exp->string); break;
287 case EXP_NUMBER: printf("%.9g", exp->number); break;
288 case EXP_STRING: pstr(exp->string); break;
289 case EXP_REGEXP: pregexp(exp->string, exp->number); break;
291 case EXP_UNDEF: break;
292 case EXP_NULL: ps("null"); break;
293 case EXP_TRUE: ps("true"); break;
294 case EXP_FALSE: ps("false"); break;
295 case EXP_THIS: ps("this"); break;
297 case EXP_OBJECT: pobject(d, exp->a); break;
298 case EXP_ARRAY: parray(d, exp->a); break;
300 case EXP_DELETE: puna(d, p, exp, "delete ", ""); break;
301 case EXP_VOID: puna(d, p, exp, "void ", ""); break;
302 case EXP_TYPEOF: puna(d, p, exp, "typeof ", ""); break;
303 case EXP_PREINC: puna(d, p, exp, "++", ""); break;
304 case EXP_PREDEC: puna(d, p, exp, "--", ""); break;
305 case EXP_POSTINC: puna(d, p, exp, "", "++"); break;
306 case EXP_POSTDEC: puna(d, p, exp, "", "--"); break;
307 case EXP_POS: puna(d, p, exp, "+", ""); break;
308 case EXP_NEG: puna(d, p, exp, "-", ""); break;
309 case EXP_BITNOT: puna(d, p, exp, "~", ""); break;
310 case EXP_LOGNOT: puna(d, p, exp, "!", ""); break;
312 case EXP_LOGOR: pbin(d, p, exp, " || "); break;
313 case EXP_LOGAND: pbin(d, p, exp, " && "); break;
314 case EXP_BITOR: pbin(d, p, exp, " | "); break;
315 case EXP_BITXOR: pbin(d, p, exp, " ^ "); break;
316 case EXP_BITAND: pbin(d, p, exp, " & "); break;
317 case EXP_EQ: pbin(d, p, exp, " == "); break;
318 case EXP_NE: pbin(d, p, exp, " != "); break;
319 case EXP_STRICTEQ: pbin(d, p, exp, " === "); break;
320 case EXP_STRICTNE: pbin(d, p, exp, " !== "); break;
321 case EXP_LT: pbin(d, p, exp, " < "); break;
322 case EXP_GT: pbin(d, p, exp, " > "); break;
323 case EXP_LE: pbin(d, p, exp, " <= "); break;
324 case EXP_GE: pbin(d, p, exp, " >= "); break;
325 case EXP_INSTANCEOF: pbin(d, p, exp, " instanceof "); break;
326 case EXP_IN: pbin(d, p, exp, " in "); break;
327 case EXP_SHL: pbin(d, p, exp, " << "); break;
328 case EXP_SHR: pbin(d, p, exp, " >> "); break;
329 case EXP_USHR: pbin(d, p, exp, " >>> "); break;
330 case EXP_ADD: pbin(d, p, exp, " + "); break;
331 case EXP_SUB: pbin(d, p, exp, " - "); break;
332 case EXP_MUL: pbin(d, p, exp, " * "); break;
333 case EXP_DIV: pbin(d, p, exp, " / "); break;
334 case EXP_MOD: pbin(d, p, exp, " % "); break;
335 case EXP_ASS: pbin(d, p, exp, " = "); break;
336 case EXP_ASS_MUL: pbin(d, p, exp, " *= "); break;
337 case EXP_ASS_DIV: pbin(d, p, exp, " /= "); break;
338 case EXP_ASS_MOD: pbin(d, p, exp, " %= "); break;
339 case EXP_ASS_ADD: pbin(d, p, exp, " += "); break;
340 case EXP_ASS_SUB: pbin(d, p, exp, " -= "); break;
341 case EXP_ASS_SHL: pbin(d, p, exp, " <<= "); break;
342 case EXP_ASS_SHR: pbin(d, p, exp, " >>= "); break;
343 case EXP_ASS_USHR: pbin(d, p, exp, " >>>= "); break;
344 case EXP_ASS_BITAND: pbin(d, p, exp, " &= "); break;
345 case EXP_ASS_BITXOR: pbin(d, p, exp, " ^= "); break;
346 case EXP_ASS_BITOR: pbin(d, p, exp, " |= "); break;
348 case EXP_COMMA: pbin(d, p, exp, ", "); break;
389 if (exp->a) pexpi(d, 0, exp->a);
406 static void pexp(int d, js_Ast *exp)
411 static void pvar(int d, js_Ast *var)
413 assert(var->type == EXP_VAR);
421 static void pvarlist(int d, js_Ast *list)
424 assert(list->type == AST_LIST);
432 static void pblock(int d, js_Ast *block)
434 assert(block->type == STM_BLOCK);
436 pstmlist(d, block->a);
440 static void pstmh(int d, js_Ast *stm)
442 if (stm->type == STM_BLOCK)
450 static void pcaselist(int d, js_Ast *list)
453 js_Ast *stm = list->a;
454 if (stm->type == STM_CASE) {
455 in(d); ps("case "); pexp(d, stm->a); ps(":\n");
458 if (stm->type == STM_DEFAULT) {
459 in(d); ps("default:\n");
466 static void pstm(int d, js_Ast *stm)
468 if (stm->type == STM_BLOCK) {
497 ps("if ("); pexp(d, stm->a); ps(")");
500 nl(); in(d); ps("else");
509 in(d); ps("while ("); pexp(d, stm->b); ps(");");
513 ps("while ("); pexp(d, stm->a); ps(")");
519 pexp(d, stm->a); ps("; ");
520 pexp(d, stm->b); ps("; ");
521 pexp(d, stm->c); ps(")");
526 pvarlist(d, stm->a); ps("; ");
527 pexp(d, stm->b); ps("; ");
528 pexp(d, stm->c); ps(")");
533 pexp(d, stm->a); ps(" in ");
534 pexp(d, stm->b); ps(")");
539 pvarlist(d, stm->a); ps(" in ");
540 pexp(d, stm->b); ps(")");
546 ps("continue "); pexp(d, stm->a); ps(";");
554 ps("break "); pexp(d, stm->a); ps(";");
562 ps("return "); pexp(d, stm->a); ps(";");
569 ps("with ("); pexp(d, stm->a); ps(")");
577 pcaselist(d, stm->b);
582 ps("throw "); pexp(d, stm->a); ps(";");
588 if (stm->b && stm->c) {
589 nl(); in(d); ps("catch ("); pexp(d, stm->b); ps(")");
593 nl(); in(d); ps("finally");
599 pexp(d, stm->a); ps(": "); pstm(d, stm->b);
607 pexp(d, stm); pc(';');
611 static void pstmlist(int d, js_Ast *list)
614 assert(list->type == AST_LIST);
621 void jsP_dumpsyntax(js_State *J, js_Ast *prog)
623 if (prog->type == AST_LIST)
631 /* S-expression list representation */
633 static void snode(int d, js_Ast *node)
635 void (*afun)(int,js_Ast*) = snode;
636 void (*bfun)(int,js_Ast*) = snode;
637 void (*cfun)(int,js_Ast*) = snode;
638 void (*dfun)(int,js_Ast*) = snode;
644 if (node->type == AST_LIST) {
650 ps(astname[node->type]);
651 switch (node->type) {
652 case AST_IDENTIFIER: pc(' '); ps(node->string); break;
653 case EXP_IDENTIFIER: pc(' '); ps(node->string); break;
654 case EXP_STRING: pc(' '); pstr(node->string); break;
655 case EXP_REGEXP: pc(' '); pregexp(node->string, node->number); break;
656 case EXP_NUMBER: printf(" %.9g", node->number); break;
657 case STM_BLOCK: afun = sblock; break;
658 case AST_FUNDEC: case EXP_FUN: cfun = sblock; break;
659 case EXP_PROP_GET: cfun = sblock; break;
660 case EXP_PROP_SET: cfun = sblock; break;
661 case STM_SWITCH: bfun = sblock; break;
662 case STM_CASE: bfun = sblock; break;
663 case STM_DEFAULT: afun = sblock; break;
665 if (node->a) { pc(' '); afun(d, node->a); }
666 if (node->b) { pc(' '); bfun(d, node->b); }
667 if (node->c) { pc(' '); cfun(d, node->c); }
668 if (node->d) { pc(' '); dfun(d, node->d); }
672 static void slist(int d, js_Ast *list)
676 assert(list->type == AST_LIST);
685 static void sblock(int d, js_Ast *list)
690 assert(list->type == AST_LIST);
698 nl(); in(d); pc(']');
701 void jsP_dumplist(js_State *J, js_Ast *prog)
703 if (prog->type == AST_LIST)
712 void jsC_dumpfunction(js_State *J, js_Function *F)
714 js_Instruction *p = F->code;
715 js_Instruction *end = F->code + F->codelen;
718 printf("%s(%d)\n", F->name, F->numparams);
719 if (F->lightweight) printf("\tlightweight\n");
720 if (F->arguments) printf("\targuments\n");
721 printf("\tsource %s:%d\n", F->filename, F->line);
722 for (i = 0; i < F->funlen; ++i)
723 printf("\tfunction %d %s\n", i, F->funtab[i]->name);
724 for (i = 0; i < F->varlen; ++i)
725 printf("\tlocal %d %s\n", i + 1, F->vartab[i]);
731 printf("% 5d: ", (int)(p - F->code) - 1);
736 printf(" %.9g", F->numtab[*p++]);
740 pstr(F->strtab[*p++]);
744 pregexp(F->strtab[p[0]], p[1]);
784 for (i = 0; i < F->funlen; ++i) {
785 if (F->funtab[i] != F) {
786 printf("function %d ", i);
787 jsC_dumpfunction(J, F->funtab[i]);
794 void js_dumpvalue(js_State *J, js_Value v)
797 case JS_TUNDEFINED: printf("undefined"); break;
798 case JS_TNULL: printf("null"); break;
799 case JS_TBOOLEAN: printf(v.u.boolean ? "true" : "false"); break;
800 case JS_TNUMBER: printf("%.9g", v.u.number); break;
801 case JS_TSHRSTR: printf("'%s'", v.u.shrstr); break;
802 case JS_TLITSTR: printf("'%s'", v.u.litstr); break;
803 case JS_TMEMSTR: printf("'%s'", v.u.memstr->p); break;
805 if (v.u.object == J->G) {
809 switch (v.u.object->type) {
810 case JS_COBJECT: printf("[Object %p]", v.u.object); break;
811 case JS_CARRAY: printf("[Array %p]", v.u.object); break;
813 printf("[Function %p, %s, %s:%d]",
815 v.u.object->u.f.function->name,
816 v.u.object->u.f.function->filename,
817 v.u.object->u.f.function->line);
819 case JS_CSCRIPT: printf("[Script %s]", v.u.object->u.f.function->filename); break;
820 case JS_CCFUNCTION: printf("[CFunction %p]", v.u.object->u.c.function); break;
821 case JS_CBOOLEAN: printf("[Boolean %d]", v.u.object->u.boolean); break;
822 case JS_CNUMBER: printf("[Number %g]", v.u.object->u.number); break;
823 case JS_CSTRING: printf("[String'%s']", v.u.object->u.s.string); break;
824 case JS_CERROR: printf("[Error %s]", v.u.object->u.s.string); break;
825 case JS_CITERATOR: printf("[Iterator %p]", v.u.object); break;
827 printf("[Userdata %s %p]", v.u.object->u.user.tag, v.u.object->u.user.data);
829 default: printf("[Object %p]", v.u.object); break;
835 static void js_dumpproperty(js_State *J, js_Property *node)
837 if (node->left->level)
838 js_dumpproperty(J, node->left);
839 printf("\t%s: ", node->name);
840 js_dumpvalue(J, node->value);
842 if (node->right->level)
843 js_dumpproperty(J, node->right);
846 void js_dumpobject(js_State *J, js_Object *obj)
849 if (obj->properties->level)
850 js_dumpproperty(J, obj->properties);