From 759acbd4ddaa706d2f457616d4e5ea32ea4127f6 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 20 Jan 2014 16:18:49 +0100 Subject: [PATCH] Add enum-to-string functions for syntax tree and opcodes. --- jscompile.h | 2 +- jsdump.c | 16 ++++++++++++++++ jsparse.h | 1 + jsrun.c | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/jscompile.h b/jscompile.h index d76ef06..c92206c 100644 --- a/jscompile.h +++ b/jscompile.h @@ -113,7 +113,7 @@ struct js_Function }; js_Function *jsC_compile(js_State *J, js_Ast *prog); - +const char *jsC_opcodestring(int opcode); void jsC_dumpfunction(js_State *J, js_Function *fun); #endif diff --git a/jsdump.c b/jsdump.c index f3c68bc..8fb98d1 100644 --- a/jsdump.c +++ b/jsdump.c @@ -5,6 +5,8 @@ #include +#define nelem(a) (sizeof (a) / sizeof (a)[0]) + static const char *astname[] = { "list", "fundec", "ident", "number", "string", "regexp", "undef", "null", "true", "false", "this", "fun", "array", "object", "prop_val", @@ -25,6 +27,20 @@ static const char *opname[] = { #include "opnames.h" }; +const char *jsP_aststring(js_AstType type) +{ + if (type < 0 || type > nelem(astname)) + return ""; + return astname[type]; +} + +const char *jsC_opcodestring(int type) +{ + if (type < 0 || type > nelem(opname)) + return ""; + return opname[type]; +} + static inline void pc(int c) { putchar(c); diff --git a/jsparse.h b/jsparse.h index 58c7c22..44b217e 100644 --- a/jsparse.h +++ b/jsparse.h @@ -128,6 +128,7 @@ js_Ast *jsP_parse(js_State *J, const char *filename, const char *source); void jsP_optimize(js_State *J, js_Ast *prog); void jsP_freeparse(js_State *J); +const char *jsP_aststring(js_AstType type); void jsP_dumpsyntax(js_State *J, js_Ast *prog); void jsP_dumplist(js_State *J, js_Ast *prog); diff --git a/jsrun.c b/jsrun.c index 309af22..a89e00d 100644 --- a/jsrun.c +++ b/jsrun.c @@ -808,7 +808,7 @@ static void jsR_run(js_State *J, js_Function *F) return; default: - js_error(J, "illegal instruction: %d (pc=%d)", opcode, (int)(pc - F->code - 1)); + js_error(J, "illegal instruction: %s (pc=%d)", jsC_opcodestring(opcode), (int)(pc - F->code - 1)); } } }