drop extend-if-key-in-array-syntax.patch add yacc debug
[nedit-bw.git] / InterpretDebug-mods.patch
blobc4eff89ffbe452184cccdf5cdc47247260dc2305
1 Subject: modifications to the InterpretDebug patch
3 ---
5 source/interpret.c | 117 ++++++++++++++++++++++++++++++++++-------------------
6 source/interpret.h | 5 +-
7 source/parse.y | 14 ++----
8 source/userCmds.c | 4 -
9 4 files changed, 86 insertions(+), 54 deletions(-)
11 diff --quilt old/source/interpret.c new/source/interpret.c
12 --- old/source/interpret.c
13 +++ new/source/interpret.c
14 @@ -114,21 +114,21 @@ static const char *tagToStr(enum typeTag
15 #define DEBUG_DISASSEMBLER
16 static const char *printd(const char *f, ...);
17 static int outPrintd();
18 -static void disasm(Inst *inst, int nInstr);
19 +static void disasm(const char *name, Inst *inst, int nInstr);
20 static void disasmInternal(Inst *inst, int nInstr);
21 #endif /* #if defined(DEBUG_ASSEMBLY) || defined(DEBUG_STACK) */
23 #ifdef DEBUG_ASSEMBLY /* for disassembly */
24 -#define DISASM(i, n) disasm(i, n)
25 +#define DISASM(name, i, n) disasm(name, i, n)
26 #else /* #ifndef DEBUG_ASSEMBLY */
27 -#define DISASM(i, n)
28 +#define DISASM(name, i, n)
29 #endif /* #ifndef DEBUG_ASSEMBLY */
31 #ifdef DEBUG_STACK /* for run-time instruction and stack trace */
32 static void stackdump(int n, int extra);
33 static void stackdumpInternal(int n, int extra);
34 #define STACKDUMP(n, x) stackdump(n, x)
35 -#define DISASM_RT(i, n) disasm(i, n)
36 +#define DISASM_RT(i, n) disasm(NULL, i, n)
37 #else /* #ifndef DEBUG_STACK */
38 #define STACKDUMP(n, x)
39 #define DISASM_RT(i, n)
40 @@ -161,6 +161,8 @@ static Inst *ProgP; /* next free spot
41 static Inst *LoopStack[LOOP_STACK_SIZE]; /* addresses of break, cont stmts */
42 static Inst **LoopStackPtr = LoopStack; /* to fill at the end of a loop */
44 +static const char *ProgramName = "";
46 /* Global data for the interpreter */
47 static DataValue *TheStack; /* the stack */
48 static DataValue *StackP; /* next free spot on stack */
49 @@ -253,7 +255,7 @@ void InitMacroGlobals(void)
50 ** Start collecting instructions for a program. Clears the program
51 ** and the symbol table.
53 -void BeginCreatingProgram(AccumulatorData *acc)
54 +void BeginCreatingProgram(const char *name, AccumulatorData *acc)
56 /* save state */
57 acc->localSymList = LocalSymList;
58 @@ -261,10 +263,12 @@ void BeginCreatingProgram(AccumulatorDat
59 acc->progP = ProgP;
60 memcpy(acc->loopStack, LoopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
61 acc->loopStackPtr = LoopStackPtr;
62 + acc->name = ProgramName;
64 LocalSymList = NULL;
65 ProgP = Prog;
66 LoopStackPtr = LoopStack;
67 + ProgramName = name;
71 @@ -278,12 +282,12 @@ Program *FinishCreatingProgram(Accumulat
72 int progLen, fpOffset = 0;
73 Symbol *s;
75 - newProg = (Program *)XtMalloc(sizeof(Program));
76 + newProg = (Program *)XtMalloc(sizeof(Program) + strlen(ProgramName));
77 progLen = ((char *)ProgP) - ((char *)Prog);
78 newProg->code = (Inst *)XtMalloc(progLen);
79 memcpy(newProg->code, Prog, progLen);
80 newProg->localSymList = LocalSymList;
81 - newProg->name = NULL;
82 + strcpy(newProg->name, ProgramName);
83 newProg->refcount = 1;
85 /* Local variables' values are stored on the stack. Here we assign
86 @@ -291,7 +295,7 @@ Program *FinishCreatingProgram(Accumulat
87 for (s = newProg->localSymList; s != NULL; s = s->next)
88 s->value.val.n = fpOffset++;
90 - DISASM(newProg->code, ProgP - Prog);
91 + DISASM(newProg->name, newProg->code, ProgP - Prog);
93 /* restore state */
94 LocalSymList = acc->localSymList;
95 @@ -299,6 +303,7 @@ Program *FinishCreatingProgram(Accumulat
96 ProgP = acc->progP;
97 memcpy(LoopStack, acc->loopStack, sizeof(*LoopStack) * LOOP_STACK_SIZE);
98 LoopStackPtr = acc->loopStackPtr;
99 + ProgramName = acc->name;
101 return newProg;
103 @@ -308,7 +313,6 @@ void FreeProgram(Program *prog)
104 if (--prog->refcount == 0) {
105 freeSymbolTable(prog->localSymList);
106 XtFree((char *)prog->code);
107 - XtFree((char *)prog->name);
108 XtFree((char *)prog);
111 @@ -3074,7 +3078,7 @@ static void arrayDisposeNode(rbTreeNode
113 SparseArrayEntry *ArrayNew(void)
115 - return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
116 + return((SparseArrayEntry *)rbTreeNew(arrayEmptyAllocator));
120 @@ -3779,28 +3783,46 @@ static void dumpVal(DataValue dv)
122 switch (dv.tag) {
123 case INT_TAG:
124 - printd("i=%d", dv.val.n);
125 + printd("<integer> %d", dv.val.n);
126 break;
127 case STRING_TAG:
129 - int k;
130 - char s[21];
131 + int k, l;
132 + char s[64];
133 char *src = dv.val.str.rep;
134 if (!src) {
135 - printd("s=<NULL>");
136 + printd("<string> <NULL>");
138 else {
139 - for (k = 0; src[k] && k < sizeof s - 1; k++) {
140 - s[k] = isprint(src[k]) ? src[k] : '?';
141 + for (k = 0, l = 0; src[k] && l < sizeof s - 1; k++, l++) {
142 + char *e;
143 + const char to[] = "\\\"ntbrfave";
144 +#ifdef EBCDIC_CHARSET
145 + const char from[] = "\\\"\n\t\b\r\f\a\v\x27"; /* EBCDIC escape */
146 +#else
147 + const char from[] = "\\\"\n\t\b\r\f\a\v\x1B"; /* ASCII escape */
148 +#endif
149 + if ((e = strchr(from, src[k]))) {
150 + if (l < sizeof s - 2) {
151 + s[l++] = '\\';
152 + s[l] = to[e - from];
155 + else if (isprint(src[k])) {
156 + s[l] = src[k];
158 + else {
159 + s[l] = '?';
162 - s[k] = 0;
163 - printd("s=\"%s\"%s[%d]", s,
164 + s[l] = 0;
165 + printd("<string> \"%s\"%s[%d]", s,
166 src[k] ? "..." : "", strlen(src));
169 break;
170 case ARRAY_TAG:
171 - printd("%08p <array>[%d]", dv.val.arrayPtr, ArraySize(&dv));
172 + printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
173 break;
174 case NO_TAG:
175 if (!dv.val.inst) {
176 @@ -3826,57 +3848,66 @@ static void disasmInternal(Inst *inst, i
177 #undef OP
179 int i, j;
180 + static size_t opLen;
182 - printd("\n");
183 + if (!opLen) {
184 + for (j = 0; j < N_OPS; ++j) {
185 + if (opLen < strlen(opNames[j])) {
186 + opLen = strlen(opNames[j]);
191 for (i = 0; i < nInstr; ++i) {
192 - printd("Prog %8p ", &inst[i]);
193 + printd("Prog %8p", &inst[i]);
194 for (j = 0; j < N_OPS; ++j) {
195 if (inst[i].func == OpFns[j]) {
196 - printd("%22s ", opNames[j]);
197 + printd(" %*s", (int)opLen, opNames[j]);
198 if (j == OP_PUSH_SYM || j == OP_ASSIGN) {
199 Symbol *sym = inst[i+1].sym;
200 - printd("%s", sym->name);
201 + printd(" %s", sym->name);
202 if (sym->value.tag == STRING_TAG &&
203 strncmp(sym->name, "string #", 8) == 0) {
204 + printd(" ");
205 dumpVal(sym->value);
207 ++i;
209 else if (j == OP_PUSH_IMMED) {
210 - printd("%d", inst[i+1].value);
211 + printd(" %d", inst[i+1].value);
212 ++i;
214 else if (j == OP_BRANCH || j == OP_BRANCH_FALSE ||
215 j == OP_BRANCH_NEVER || j == OP_BRANCH_TRUE) {
216 - printd("to=(%d) %p", inst[i+1].value,
217 + printd(" to=(%+d) %8p", inst[i+1].value,
218 &inst[i+1] + inst[i+1].value);
219 ++i;
221 else if (j == OP_CONCAT) {
222 - printd("nExpr=%d", inst[i+1].value);
223 + printd(" nExpr=%d", inst[i+1].value);
224 ++i;
226 else if (j == OP_SUBR_CALL) {
227 int args = inst[i+2].value;
228 - printd("%s ", inst[i+1].sym->name);
229 + printd(" %s", inst[i+1].sym->name);
230 if (args < 0) {
231 - printd("%d+args[] (%d)", -args - 1, args);
232 + printd(" %d+args[] (%d)", -args - 1, args);
234 else {
235 - printd("%d args", args);
236 + printd(" %d args", args);
238 i += 2;
240 else if (j == OP_SUBR_CALL_STACKED_N) {
241 - printd("%s args[] (?)", inst[i+1].sym->name);
242 + printd(" %s args[] (?)", inst[i+1].sym->name);
243 ++i;
245 else if (j == OP_BEGIN_ARRAY_ITER) {
246 - printd("%s in", inst[i+1].sym->name);
247 + printd(" %s in", inst[i+1].sym->name);
248 ++i;
250 else if (j == OP_ARRAY_ITER) {
251 - printd("%s = %s++ end-loop=(%d) %p",
252 + printd(" %s = %s++ end-loop=(%+d) %8p",
253 inst[i+1].sym->name,
254 inst[i+2].sym->name,
255 inst[i+3].value, &inst[i+3] + inst[i+3].value);
256 @@ -3888,18 +3919,20 @@ static void disasmInternal(Inst *inst, i
257 j == OP_ANONARRAY_INDEX_VAL ||
258 j == OP_NAMED_ARG1 ||
259 j == OP_NAMED_ARGN) {
260 - printd("nDim=%d", inst[i+1].value);
261 + printd(" nDim=%d", inst[i+1].value);
262 ++i;
264 else if (j == OP_ARRAY_REF_ASSIGN_SETUP) {
265 - printd("binOp=%s ", inst[i+1].value ? "true" : "false");
266 - printd("nDim=%d", inst[i+2].value);
267 + printd(" binOp=%s nDim=%d",
268 + inst[i+1].value ? "true" : "false",
269 + inst[i+2].value);
270 i += 2;
272 else if (j == OP_PUSH_ARRAY_SYM) {
273 - printd("%s", inst[++i].sym->name);
274 - printd(" %s", inst[i+1].value ? "createAndRef" : "refOnly");
275 - ++i;
276 + printd(" %s %s",
277 + inst[i+1].sym->name,
278 + inst[i+2].value ? "createAndRef" : "refOnly");
279 + i += 2;
282 printd("\n");
283 @@ -3907,18 +3940,20 @@ static void disasmInternal(Inst *inst, i
286 if (j == N_OPS) {
287 - printd("%x\n", inst[i].value);
288 + printd(" %x\n", inst[i].value);
293 -static void disasm(Inst *inst, int nInstr)
294 +static void disasm(const char *name, Inst *inst, int nInstr)
296 static int outIsTTY = -1;
297 if (outIsTTY == -1) outIsTTY = isatty(fileno(stdout));
298 if (outIsTTY) { printd("\033[H"); }
299 + if (name) printd(">> %s\n", name);
300 disasmInternal(inst, nInstr);
301 if (outIsTTY) { printd("\033[J\n"); }
302 + if (name) printd("\n");
303 outPrintd();
305 #endif /* #ifdef DEBUG_DISASSEMBLER */
306 @@ -4047,7 +4082,7 @@ static void stackdumpInternal(int n, int
307 if (outpt < TheStack)
308 printd("--------------Stack base--------------\n");
309 stackdumpframe(arrow, outpt, FrameP, StackP, '*');
310 - printd("Stack ----->\n");
311 + printd("Stack ----->\n\n");
312 #endif /* #ifdef DEBUG_STACK_HEADFIRST */
315 diff --quilt old/source/interpret.h new/source/interpret.h
316 --- old/source/interpret.h
317 +++ new/source/interpret.h
318 @@ -108,7 +108,7 @@ typedef struct ProgramTag {
319 Symbol *localSymList;
320 Inst *code;
321 unsigned refcount;
322 - char *name;
323 + char name[1];
324 } Program;
326 /* Information needed to re-start a preempted macro */
327 @@ -128,6 +128,7 @@ typedef struct AccumulatorDataTag {
328 Inst *progP;
329 Inst *loopStack[LOOP_STACK_SIZE];
330 Inst **loopStackPtr;
331 + const char *name;
332 } AccumulatorData;
334 void InitMacroGlobals(void);
335 @@ -144,7 +145,7 @@ int ArrayCopy(DataValue *dstArray, DataV
337 /* Routines for creating a program, (accumulated beginning with
338 BeginCreatingProgram and returned via FinishCreatingProgram) */
339 -void BeginCreatingProgram(AccumulatorData *acc);
340 +void BeginCreatingProgram(const char *name, AccumulatorData *acc);
341 int AddOp(int op, char **msg);
342 int AddSym(Symbol *sym, char **msg);
343 int AddImmediate(int value, char **msg);
344 diff --quilt old/source/parse.y new/source/parse.y
345 --- old/source/parse.y
346 +++ new/source/parse.y
347 @@ -195,7 +195,7 @@ define: DEFINE {
348 yyerror("try to override built-in subroutine"); YYERROR;
350 $<define>$.sym = PromoteToGlobal($4);
351 - BeginCreatingProgram($<define>$.acc);
352 + BeginCreatingProgram($<define>$.sym->name, $<define>$.acc);
354 blank blockwb {
355 ADD_OP(OP_RETURN_NO_VAL);
356 @@ -745,14 +745,16 @@ Program *ParseMacro(char *expr, char **m
358 Program *prog;
359 AccumulatorData *acc = (AccumulatorData *)XtMalloc(sizeof(*acc));
360 - static const char *prefix = ">> ";
362 #if YYDEBUG
363 int oldyydebug = yydebug;
364 yydebug = 1;
365 #endif
367 - BeginCreatingProgram(acc);
368 + if (!name)
369 + name = "--unknown--";
371 + BeginCreatingProgram(name, acc);
373 /* whether we allow the "define" keyword */
374 AllowDefine = allowDefine;
375 @@ -780,12 +782,6 @@ Program *ParseMacro(char *expr, char **m
376 prog = FinishCreatingProgram(acc);
377 XtFree((char *)acc);
379 - if (!name)
380 - name = "--unknown--";
382 - prog->name = XtMalloc(strlen(name) + strlen(prefix) + 1);
383 - strcat(strcpy(prog->name, prefix), name);
385 /* parse succeeded */
386 *msg = "";
387 *stoppedAt = InPtr;
388 diff --quilt old/source/userCmds.c new/source/userCmds.c
389 --- old/source/userCmds.c
390 +++ new/source/userCmds.c
391 @@ -1286,13 +1286,13 @@ static int doMacroMenuCmd(WindowInfo *wi
392 int DoNamedMacroMenuCmd(WindowInfo *window, const char *itemName)
394 return doMacroMenuCmd(window, itemName, MacroMenuItems, NMacroMenuItems,
395 - "macro-menu>");
396 + "Macro Menu>");
399 int DoNamedBGMenuCmd(WindowInfo *window, const char *itemName)
401 return doMacroMenuCmd(window, itemName, BGMenuItems, NBGMenuItems,
402 - "background-menu>");
403 + "Background Menu>");