remove debug fprintf
[nedit-bw.git] / change-sym-types.patch
blob633f2c201430654aa6a29c75c0c2043b75d693e8
1 ---
3 source/interpret.c | 123 +++++++++++++++++++++++++++++------------------------
4 source/interpret.h | 20 ++++++--
5 source/macro.c | 39 +++++++++-------
6 source/parse.y | 9 ++-
7 4 files changed, 111 insertions(+), 80 deletions(-)
9 diff --quilt old/source/interpret.c new/source/interpret.c
10 --- old/source/interpret.c
11 +++ new/source/interpret.c
12 @@ -77,7 +77,7 @@ static const char CVSID[] = "$Id: interp
13 #define NEEDS_BREAK 1
14 #define NEEDS_CONTINUE 2
16 -#define N_ARGS_ARG_SYM -1 /* special arg number meaning $n_args value */
17 +#define N_ARGS_ARG_VAR -1 /* special arg number meaning $n_args value */
19 enum opStatusCodes {STAT_OK=2, STAT_DONE, STAT_ERROR, STAT_PREEMPT};
21 @@ -229,26 +229,30 @@ void InitMacroGlobals(void)
23 /* Add action routines from NEdit menus and text widget */
24 actions = GetMenuActions(&nActions);
25 + dv.tag = ACTION_SUBR_TAG;
26 for (i=0; i<nActions; i++) {
27 dv.val.xtproc = actions[i].proc;
28 - InstallSymbol(actions[i].string, ACTION_ROUTINE_SYM, dv);
29 + InstallSymbol(actions[i].string, GLOBAL_SCOPE, READONLY_ATTR, dv);
31 actions = TextGetActions(&nActions);
32 + dv.tag = ACTION_SUBR_TAG;
33 for (i=0; i<nActions; i++) {
34 dv.val.xtproc = actions[i].proc;
35 - InstallSymbol(actions[i].string, ACTION_ROUTINE_SYM, dv);
36 + InstallSymbol(actions[i].string, GLOBAL_SCOPE, READONLY_ATTR, dv);
39 /* Add subroutine argument symbols ($1, $2, ..., $9) */
40 + dv.tag = ARG_VAR_TAG;
41 for (i=0; i<9; i++) {
42 argName[1] = '1' + i;
43 dv.val.n = i;
44 - InstallSymbol(argName, ARG_SYM, dv);
45 + InstallSymbol(argName, GLOBAL_SCOPE, READONLY_ATTR, dv);
48 /* Add special symbol $n_args */
49 - dv.val.n = N_ARGS_ARG_SYM;
50 - InstallSymbol("$n_args", ARG_SYM, dv);
51 + dv.tag = ARG_VAR_TAG;
52 + dv.val.n = N_ARGS_ARG_VAR;
53 + InstallSymbol("$n_args", GLOBAL_SCOPE, READONLY_ATTR, dv);
57 @@ -834,8 +838,8 @@ static Symbol *lookupSymbol(Symbol *syml
59 ** find a symbol in the symbol table
61 -** will create only LOCAL_SYM and GLOBAL_SYM (depending on first character)
62 -** with a NO_TAG value
63 +** will create only LOCAL_SCOPE and GLOBAL_SCOPE (depending on first character)
64 +** with a NO_TAG value and NO_ATTR
66 Symbol *LookupSymbol(const char *name, int create)
68 @@ -859,8 +863,8 @@ Symbol *LookupSymbol(const char *name, i
70 if (create) {
71 DataValue noValue = {NO_TAG, {0}};
72 - s = InstallSymbol(name, name[0] == '$' ? GLOBAL_SYM : LOCAL_SYM,
73 - noValue);
74 + s = InstallSymbol(name, name[0] == '$' ? GLOBAL_SCOPE : LOCAL_SCOPE,
75 + NO_ATTR, noValue);
78 return s;
79 @@ -869,16 +873,17 @@ Symbol *LookupSymbol(const char *name, i
81 ** install symbol name in symbol table
83 -Symbol *InstallSymbol(const char *name, enum symTypes type, DataValue value)
84 +Symbol *InstallSymbol(const char *name, enum symScope scope, unsigned attr,
85 + DataValue value)
87 Symbol *s;
89 s = XtNew(Symbol);
90 s->name = LookupString(name, True);
91 - s->type = type;
92 + s->attr = attr;
93 s->value = value;
94 s->hash = hashName(s->name);
95 - if (type == LOCAL_SYM) {
96 + if (scope == LOCAL_SCOPE) {
97 if (Interpreter) {
98 s->next = LocalSymList;
99 LocalSymList = s;
100 @@ -1447,36 +1452,49 @@ static void addToGlobalSymTab(Symbol *sy
101 static int pushSymVal(void)
103 Symbol *s;
104 - int nArgs, argNum;
105 - DataValue symVal;
106 + DataValue symVal = {NO_TAG, {0}};
108 DISASM_RT();
109 STACKDUMP(0, 3);
111 GET_SYM(s, False);
113 - if (s->type == LOCAL_SYM || s->type == GLOBAL_SYM) {
114 + switch (s->value.tag) {
115 + case INT_TAG:
116 + case STRING_TAG:
117 + case ARRAY_TAG:
118 symVal = s->value;
119 - } else if (s->type == ARG_SYM) {
120 - nArgs = FP_GET_ARG_COUNT(FrameP);
121 - argNum = s->value.val.n;
122 - if (argNum >= nArgs) {
123 - EXEC_ERROR("referenced undefined argument: %s", s->name);
125 - if (argNum == N_ARGS_ARG_SYM) {
126 - symVal.tag = INT_TAG;
127 - symVal.val.n = nArgs;
129 - else {
130 - symVal = FP_GET_ARG_N(FrameP, argNum);
131 + case NO_TAG:
132 + break;
134 + case ARG_VAR_TAG: {
135 + int nArgs = FP_GET_ARG_COUNT(FrameP);
136 + int argNum = s->value.val.n;
137 + if (argNum >= nArgs) {
138 + EXEC_ERROR("referenced undefined argument: %s", s->name);
140 + if (argNum == N_ARGS_ARG_VAR) {
141 + symVal.tag = INT_TAG;
142 + symVal.val.n = nArgs;
144 + else {
145 + symVal = FP_GET_ARG_N(FrameP, argNum);
148 - } else if (s->type == PROC_VALUE_SYM) {
149 - char *errMsg;
150 - if (!s->value.val.subr(FocusWindow, NULL, 0, &symVal, &errMsg)) {
151 - EXEC_ERROR(errMsg, s->name);
152 + break;
154 + case BUILT_IN_VAR_TAG: {
155 + char *errMsg;
156 + if (!s->value.val.subr(FocusWindow, NULL, 0, &symVal, &errMsg)) {
157 + EXEC_ERROR(errMsg, s->name);
160 - } else
161 + break;
163 + default:
164 EXEC_ERROR("reading non-variable: %s", s->name);
167 if (symVal.tag == NO_TAG && !inTypeOfMode) {
168 EXEC_ERROR("variable not set: %s", s->name);
170 @@ -1608,7 +1626,7 @@ static int pushArraySymVal(void)
171 GET_IMMED(initEmpty);
172 GET_SYM(sym, initEmpty);
174 - if (sym->type != LOCAL_SYM && sym->type != GLOBAL_SYM) {
175 + if (sym->attr & READONLY_ATTR) {
176 EXEC_ERROR("assigning to non-lvalue array or non-array: %s", sym->name);
178 dataPtr = &sym->value;
179 @@ -1915,16 +1933,8 @@ static int assign(void)
181 GET_SYM(sym, True);
183 - if (sym->type != GLOBAL_SYM && sym->type != LOCAL_SYM) {
184 - if (sym->type == ARG_SYM) {
185 - EXEC_ERROR("assignment to function argument: %s", sym->name);
187 - else if (sym->type == PROC_VALUE_SYM) {
188 - EXEC_ERROR("assignment to read-only variable: %s", sym->name);
190 - else {
191 - EXEC_ERROR("assignment to non-variable: %s", sym->name);
193 + if (sym->attr & READONLY_ATTR) {
194 + EXEC_ERROR("assignment to read-only variable: %s", sym->name);
196 dataPtr = &sym->value;
198 @@ -2642,7 +2652,7 @@ static int callSubroutineFromSymbol(Symb
200 ** If the subroutine is built-in, call the built-in routine
202 - if (sym->type == C_FUNCTION_SYM) {
203 + if (sym->value.tag == BUILT_IN_SUBR_TAG) {
204 DataValue result;
205 char *errMsg;
207 @@ -2667,7 +2677,7 @@ static int callSubroutineFromSymbol(Symb
208 ** stack for local variables (and initialize them), on top of the argument
209 ** values which are already there.
211 - if (sym->type == MACRO_FUNCTION_SYM) {
212 + if (sym->value.tag == MACRO_SUBR_TAG) {
213 prog = sym->value.val.prog;
214 prog->refcount++;
215 /* -nArgs means 'arguments are on stack' */
216 @@ -2677,7 +2687,7 @@ static int callSubroutineFromSymbol(Symb
218 ** Call an action routine
220 - if (sym->type == ACTION_ROUTINE_SYM) {
221 + if (sym->value.tag == ACTION_SUBR_TAG) {
222 String *argList;
223 Cardinal numArgs = nArgs;
224 XKeyEvent key_event;
225 @@ -2721,7 +2731,7 @@ static int callSubroutineFromSymbol(Symb
228 /* Calling a non subroutine symbol */
229 - EXEC_ERROR("%s is not a function or subroutine", sym->name);
230 + EXEC_ERROR("Calling non subroutine %s", sym->name);
234 @@ -2884,9 +2894,9 @@ int OverlayRoutineFromProg(Program *prog
236 Symbol sym;
238 - sym.type = MACRO_FUNCTION_SYM;
239 + sym.attr = READONLY_ATTR;
240 sym.name = LookupString(prog->name, True);
241 - sym.value.tag = NO_TAG;
242 + sym.value.tag = MACRO_SUBR_TAG;
243 sym.value.val.prog = prog;
244 sym.next = NULL;
246 @@ -3597,14 +3607,14 @@ static int arrayIter(void)
248 POP(iterator);
250 - if (keySym->type != LOCAL_SYM && keySym->type != GLOBAL_SYM) {
251 + if (keySym->attr & READONLY_ATTR) {
252 EXEC_ERROR("can't assign to: %s", keySym->name);
254 keyValPtr = &keySym->value;
255 keyValPtr->tag = NO_TAG;
257 if (withVal) {
258 - if (valSym->type != LOCAL_SYM && valSym->type != GLOBAL_SYM) {
259 + if (valSym->attr & READONLY_ATTR) {
260 EXEC_ERROR("can't assign to: %s", valSym->name);
262 valPtr = &valSym->value;
263 @@ -3755,7 +3765,7 @@ static int arrayIterArray(void)
264 POP(iterator);
265 PEEK_INT(nDims, 0);
267 - if (keyArraySym->type != LOCAL_SYM && keyArraySym->type != GLOBAL_SYM) {
268 + if (keyArraySym->attr & READONLY_ATTR) {
269 EXEC_ERROR("can't assign to: %s", keyArraySym->name);
271 keyArrayPtr = &keyArraySym->value;
272 @@ -3763,7 +3773,7 @@ static int arrayIterArray(void)
273 keyArrayPtr->val.arrayPtr = NULL;
275 if (withVal) {
276 - if (valSym->type != LOCAL_SYM && valSym->type != GLOBAL_SYM) {
277 + if (valSym->attr & READONLY_ATTR) {
278 EXEC_ERROR("can't assign to: %s", valSym->name);
280 valPtr = &valSym->value;
281 @@ -3941,6 +3951,8 @@ static int typeOfOut(void)
282 case ARRAY_TAG:
283 retVal.val.str.rep = PERM_ALLOC_STR("ARRAY");
284 break;
285 + default:
286 + retVal.val.str.rep = PERM_ALLOC_STR("UNKNOWN");
288 retVal.val.str.len = strlen(retVal.val.str.rep);
290 @@ -4830,7 +4842,8 @@ static void stackdumpframe(DataValue *ar
291 if (dv == lsFP) {
292 Symbol *s = dv->val.sym;
293 while (s) {
294 - printd("\n%*s", 22, s->name);
295 + printd("\n%*s%s", 22, s->name,
296 + s->attr & READONLY_ATTR ? " readonly" : "");
297 dumpVal(s->value);
298 s = s->next;
300 diff --quilt old/source/interpret.h new/source/interpret.h
301 --- old/source/interpret.h
302 +++ new/source/interpret.h
303 @@ -40,8 +40,10 @@
304 #define LOOP_STACK_SIZE 256 /* (Approx.) Number of break/continue stmts
305 allowed per program */
307 -enum symTypes {GLOBAL_SYM, LOCAL_SYM, ARG_SYM, PROC_VALUE_SYM,
308 - C_FUNCTION_SYM, MACRO_FUNCTION_SYM, ACTION_ROUTINE_SYM};
309 +/* LOCAL_SYM -> LOCAL_SCOPE */
310 +/* GLOBAL_SYM -> GLOBAL_SCOPE */
311 +enum symScope {LOCAL_SCOPE, GLOBAL_SCOPE};
312 +enum symAttr {NO_ATTR = 0, READONLY_ATTR = 1 << 0};
314 enum operations {
315 #define OP(name, fn) OP_##name,
316 @@ -50,7 +52,14 @@ enum operations {
317 N_OPS
320 -enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG};
321 +/* ARG_SYM -> ARG_VAR_TAG */
322 +/* MACRO_FUNCTION_SYM -> MACRO_SUBR_TAG */
323 +/* C_FUNCTION_SYM -> BUILT_IN_SUBR_TAG */
324 +/* PROC_VALUE_SYM -> BUILT_IN_VAR_TAG */
325 +/* ACTION_ROUTINE_SYM -> ACTION_SUBR_TAG */
326 +enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG, ARG_VAR_TAG,
327 + BUILT_IN_VAR_TAG, BUILT_IN_SUBR_TAG, MACRO_SUBR_TAG,
328 + ACTION_SUBR_TAG};
330 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
332 @@ -105,7 +114,7 @@ typedef struct SparseArrayEntryTag {
333 /* symbol table entry */
334 typedef struct SymbolRec {
335 const char *name;
336 - enum symTypes type;
337 + unsigned attr;
338 DataValue value;
339 unsigned int hash;
340 struct SymbolRec *next; /* to link to another */
341 @@ -161,7 +170,8 @@ int AddBranchOffset(Inst *to, char **msg
342 int SetBranchOffset(Inst *from, Inst *to, char **msg);
343 Inst *GetPC(void);
344 Symbol *LookupSymbol(const char *name, int create);
345 -Symbol *InstallSymbol(const char *name, enum symTypes type, DataValue value);
346 +Symbol *InstallSymbol(const char *name, enum symScope scope, unsigned attr,
347 + DataValue value);
348 const char *LookupString(const char *str, int create);
349 Inst *SwapCode(Inst *start, Inst *boundary, Inst *end);
350 int StartLoopAddrList(char **msg);
351 diff --quilt old/source/macro.c new/source/macro.c
352 --- old/source/macro.c
353 +++ new/source/macro.c
354 @@ -299,14 +299,15 @@ static char EscapeChars[] = "\\\"\n\t\b\
356 ** Installs a set of built-in macro subroutines as symbols of given type.
358 -void RegisterMacroSubroutineSet(const BuiltInSubrName *set, enum symTypes type)
359 +void RegisterMacroSubroutineSet(const BuiltInSubrName *set, enum typeTags tag)
361 static DataValue subrPtr = {NO_TAG, {0}};
362 unsigned i;
364 + subrPtr.tag = tag;
365 for (i = 0; set[i].name; i++) {
366 subrPtr.val.subr = set[i].macroSubr;
367 - InstallSymbol(set[i].name, type, subrPtr);
368 + InstallSymbol(set[i].name, GLOBAL_SCOPE, READONLY_ATTR, subrPtr);
372 @@ -314,7 +315,7 @@ void RegisterMacroSubroutineSet(const Bu
373 ** Installs a set of built-in macro secondary return values as symbols of
374 ** given type.
376 -void RegisterGlobalReturnValuesSet(ReturnGlobalName *set, enum symTypes type)
377 +void RegisterGlobalReturnValuesSet(ReturnGlobalName *set)
379 int n, i, oldn;
380 Symbol **newRetGlobals;
381 @@ -338,7 +339,8 @@ void RegisterGlobalReturnValuesSet(Retur
382 /* now assign the (new) symbols at the newly allocated end of the table
383 and store the indices in set */
384 for (i = 0, n = oldn; set[i].name; i++, n++) {
385 - ReturnGlobals[n] = InstallSymbol(set[i].name, type, noValue);
386 + ReturnGlobals[n] = InstallSymbol(set[i].name, GLOBAL_SCOPE,
387 + READONLY_ATTR, noValue);
388 *set[i].pIndex = n;
390 /* add the sentinel */
391 @@ -353,10 +355,10 @@ void RegisterMacroSubroutines(void)
393 /* Install symbols for built-in routines and variables, with pointers
394 to the appropriate c routines to do the work */
395 - RegisterMacroSubroutineSet(MacroSubrs, C_FUNCTION_SYM);
396 - RegisterMacroSubroutineSet(SpecialVars, PROC_VALUE_SYM);
397 + RegisterMacroSubroutineSet(MacroSubrs, BUILT_IN_SUBR_TAG);
398 + RegisterMacroSubroutineSet(SpecialVars, BUILT_IN_VAR_TAG);
399 /* Define global variables used for return values */
400 - RegisterGlobalReturnValuesSet(ReturnGlobalNames, GLOBAL_SYM);
401 + RegisterGlobalReturnValuesSet(ReturnGlobalNames);
404 #define MAX_LEARN_MSG_LEN ((2 * MAX_ACCEL_LEN) + 60)
405 @@ -3536,20 +3538,25 @@ static int defineMS(WindowInfo *window,
406 FreeProgram(prog);
407 *errMsg = "Try to override existing function.";
408 return False;
409 - } else {
410 - if (sym->type != MACRO_FUNCTION_SYM) {
412 + else if (sym->attr & READONLY_ATTR) {
413 + FreeProgram(prog);
414 + *errMsg = "Try to override read only symbol.";
415 + return False;
417 + else if (sym->value.tag != MACRO_SUBR_TAG) {
418 FreeProgram(prog);
419 *errMsg = "Try to override a non macro function.";
420 return False;
422 - FreeProgram(sym->value.val.prog);
423 - sym->value.val.prog = prog;
425 - } else {
426 + FreeProgram(sym->value.val.prog);
427 + sym->value.val.prog = prog;
429 + else {
430 DataValue subrPtr;
431 - subrPtr.tag = NO_TAG;
432 + subrPtr.tag = MACRO_SUBR_TAG;
433 subrPtr.val.prog = prog;
434 - sym = InstallSymbol(name, MACRO_FUNCTION_SYM, subrPtr);
435 + sym = InstallSymbol(name, GLOBAL_SCOPE, NO_ATTR, subrPtr);
438 return True;
439 @@ -6938,7 +6945,7 @@ Boolean MacroApplyHook(WindowInfo *docum
440 hookSymbol = LookupSymbol(hook, False);
441 if (document &&
442 NULL != hookSymbol &&
443 - MACRO_FUNCTION_SYM == hookSymbol->type) {
444 + MACRO_SUBR_TAG == hookSymbol->value.tag) {
445 Program *hookProg = hookSymbol->value.val.prog;
446 RestartData *restartData;
447 DataValue dummyResultDV;
448 diff --quilt old/source/parse.y new/source/parse.y
449 --- old/source/parse.y
450 +++ new/source/parse.y
451 @@ -213,16 +213,17 @@ define: definekw blank definesym bla
452 prog = FinishCreatingProgram($3);
453 sym = LookupSymbol(prog->name, False);
454 if (sym) {
455 - if (sym->type != MACRO_FUNCTION_SYM) {
456 + if (sym->value.tag != MACRO_SUBR_TAG) {
457 FreeProgram(prog);
458 - yyerror("try to override built-in subroutine"); YYERROR;
459 + yyerror("try to override built-in subroutine");
460 + YYERROR;
462 FreeProgram(sym->value.val.prog);
464 else {
465 DataValue subrPtr;
466 - subrPtr.tag = NO_TAG;
467 - sym = InstallSymbol(prog->name, MACRO_FUNCTION_SYM,
468 + subrPtr.tag = MACRO_SUBR_TAG;
469 + sym = InstallSymbol(prog->name, GLOBAL_SCOPE, READONLY_ATTR,
470 subrPtr);
472 sym->value.val.prog = prog;