3 source/interpret.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++----
4 source/interpret.h | 3 +-
5 source/macro.c | 25 ++++++++++++++-----
6 3 files changed, 83 insertions(+), 12 deletions(-)
8 diff --quilt old/source/interpret.c new/source/interpret.c
9 --- old/source/interpret.c
10 +++ new/source/interpret.c
11 @@ -184,11 +184,11 @@ static void disasmInternal(Inst *inst, i
12 #endif /* #ifndef DEBUG_ASSEMBLY */
14 #ifdef DEBUG_STACK /* for run-time instruction and stack trace */
15 static void stackdump(int n, int extra);
16 static void stackdumpInternal(int n, int extra);
17 -#define STACKDUMP(n, x) stackdump(n, x)
18 +#define STACKDUMP(n, x) stackdump(n, (x) + 10)
19 #define DISASM_RT(i, n) disasm(NULL, i, n)
20 #else /* #ifndef DEBUG_STACK */
21 #define STACKDUMP(n, x)
22 #define DISASM_RT(i, n)
23 #endif /* #ifndef DEBUG_STACK */
24 @@ -304,10 +304,11 @@ static int (*OpFns[N_OPS])() = {returnNo
25 return execError(StackOverflowMsg, ""); \
29 static const char *tagToStr(enum typeTags tag);
30 +static const char *typeToStr(enum symTypes type);
33 ** Initialize macro language global variables. Must be called before
34 ** any macros are even parsed, because the parser uses action routine
35 ** symbols to comprehend hyphenated names.
36 @@ -425,10 +426,11 @@ int AddSym(Symbol *sym, char **msg)
38 if (ProgP >= &Prog[PROGRAM_SIZE]) {
39 *msg = "macro too large";
48 @@ -866,10 +868,11 @@ Symbol *InstallSymbol(const char *name,
49 s = (Symbol *)malloc(sizeof(Symbol));
50 s->name = (char *)malloc(strlen(name)+1); /* +1 for '\0' */
51 strcpy(s->name, name);
55 if (type == LOCAL_SYM) {
56 s->next = LocalSymList;
59 s->next = GlobalSymList;
60 @@ -883,13 +886,12 @@ Symbol *InstallSymbol(const char *name,
63 Symbol *PromoteToGlobal(Symbol *sym)
66 - static DataValue noValue = {NO_TAG, {0}};
68 - if (sym->type != LOCAL_SYM)
69 + if (sym->type != LOCAL_SYM || sym->added)
72 /* Remove sym from the local symbol list */
73 if (sym == LocalSymList)
74 LocalSymList = sym->next;
75 @@ -903,11 +905,16 @@ Symbol *PromoteToGlobal(Symbol *sym)
78 s = LookupSymbol(sym->name);
81 - return InstallSymbol(sym->name, GLOBAL_SYM, noValue);
83 + sym->type = GLOBAL_SYM;
84 + sym->next = GlobalSymList;
85 + GlobalSymList = sym;
91 ** Convert a long value to its decimal string representation, returned in a
93 @@ -1380,10 +1387,15 @@ static int pushSymVal(void)
94 } else if (s->type == PROC_VALUE_SYM) {
96 if (!(s->value.val.subr)(FocusWindow, NULL, 0, &symVal, &errMsg)) {
97 return execError(errMsg, s->name);
99 + } else if (s->type == C_FUNCTION_SYM
100 + || s->type == MACRO_FUNCTION_SYM
101 + || s->type == ACTION_ROUTINE_SYM) {
102 + symVal.tag = SUBR_TAG;
103 + symVal.val.sym = s;
105 return execError("reading non-variable: %s", s->name);
108 if (symVal.tag == NO_TAG && !inTypeOfMode) {
109 @@ -2464,14 +2476,28 @@ static int callSubroutineFromSymbol(Symb
111 static DataValue noValue = {NO_TAG, {0}};
114 int haveNamedArgs = (nArgs < 0);
115 + DataValue *symValPtr = NULL;
120 + if (sym->type == LOCAL_SYM) {
121 + symValPtr = &FP_GET_SYM_VAL(FrameP, sym);
122 + } else if (sym->type == GLOBAL_SYM) {
123 + symValPtr = &sym->value;
126 + if (symValPtr->tag == SUBR_TAG) {
127 + sym = symValPtr->val.sym;
129 + return execError("%s is not a variable holding a subroutine pointer", sym->name);
134 ** If the subroutine is built-in, call the built-in routine
136 if (sym->type == C_FUNCTION_SYM) {
138 @@ -3972,10 +3998,13 @@ static int typeOfOut(void)
139 retVal.val.str.rep = PERM_ALLOC_STR("STRING");
142 retVal.val.str.rep = PERM_ALLOC_STR("ARRAY");
145 + retVal.val.str.rep = PERM_ALLOC_STR("SUBROUTINE");
148 retVal.val.str.len = strlen(retVal.val.str.rep);
150 if (PC->func == fetchRetVal) {
152 @@ -4124,16 +4153,43 @@ static const char *tagToStr(enum typeTag
159 + return "<subroutine>";
166 +static const char *typeToStr(enum symTypes type)
177 + case PROC_VALUE_SYM:
178 + return "<proc value>";
179 + case C_FUNCTION_SYM:
180 + return "<C function>";
181 + case MACRO_FUNCTION_SYM:
182 + return "<macro function>";
183 + case ACTION_ROUTINE_SYM:
184 + return "<action routine>";
191 #ifdef DEBUG_DISASSEMBLER /* dumping values in disassembly or stack dump */
192 static char *printdBuffer = NULL;
193 static int printdPos = 0;
194 static int printdSize = 0;
196 @@ -4248,10 +4304,13 @@ static void dumpVal(DataValue dv)
200 printd("<array> %8p[%d]", dv.val.arrayPtr, ArraySize(&dv));
203 + printd("<subroutine> %s", dv.val.sym->name);
207 printd("<no value>");
210 diff --quilt old/source/interpret.h new/source/interpret.h
211 --- old/source/interpret.h
212 +++ new/source/interpret.h
213 @@ -58,11 +58,11 @@ enum operations {OP_RETURN_NO_VAL, OP_RE
216 OP_BEGIN_ARRAY_MULTI_ITER_ARRAY, OP_ARRAY_MULTI_ITER_ARRAY, OP_POP,
219 -enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG};
220 +enum typeTags {NO_TAG, INT_TAG, STRING_TAG, ARRAY_TAG, SUBR_TAG};
222 enum execReturnCodes {MACRO_TIME_LIMIT, MACRO_PREEMPT, MACRO_DONE, MACRO_ERROR};
224 #define ARRAY_DIM_SEP "\034"
226 @@ -110,10 +110,11 @@ typedef struct SparseArrayEntryTag {
227 typedef struct SymbolRec {
231 struct SymbolRec *next; /* to link to another */
235 typedef struct ProgramTag {
236 Symbol *localSymList;
238 diff --quilt old/source/macro.c new/source/macro.c
239 --- old/source/macro.c
240 +++ new/source/macro.c
241 @@ -3870,31 +3870,39 @@ static int filenameDialogMS(WindowInfo*
242 * call(fnname, arg, ...)
244 static int callMS(WindowInfo *window, DataValue *argList,
245 int nArgs, DataValue *result, char **errMsg)
247 - char stringStorage[TYPE_INT_STR_SIZE(int)];
252 *errMsg = "subroutine %s called without arguments";
255 - if (!readStringArg(argList[0], &fnname, stringStorage, errMsg)) {
257 + if (argList[0].tag == SUBR_TAG) {
258 + sym = argList[0].val.sym;
260 + char stringStorage[TYPE_INT_STR_SIZE(int)];
263 + if (!readStringArg(argList[0], &fnname, stringStorage, errMsg)) {
267 + sym = LookupSymbol(fnname);
269 - sym = LookupSymbol(fnname);
272 *errMsg = "subroutine name invalid";
276 return OverlayRoutineFromSymbol(sym, nArgs, 1);
280 - * define(func_name, func_body[, "override"])
281 + * subr = define(func_name, func_body[, "override"])
283 static int defineMS(WindowInfo *window, DataValue *argList, int nArgs,
284 DataValue *result, char **errMsg)
286 char stringStorage[3][TYPE_INT_STR_SIZE(int)];
287 @@ -3904,11 +3912,10 @@ static int defineMS(WindowInfo *window,
288 char *stoppedAt = NULL;
290 char *override = NULL;
295 if (nArgs < 2 || nArgs > 3) {
296 return wrongNArgsErr(errMsg);
298 if (!readStringArg(argList[0], &name, stringStorage[0], errMsg)) {
299 @@ -3972,15 +3979,19 @@ static int defineMS(WindowInfo *window,
301 FreeProgram(sym->value.val.prog);
302 sym->value.val.prog = prog;
306 subrPtr.val.prog = prog;
307 subrPtr.tag = NO_TAG;
308 sym = InstallSymbol(name, MACRO_FUNCTION_SYM, subrPtr);
311 + result->tag = SUBR_TAG;
312 + result->val.sym = sym;
318 * set_window_title(format[, ("text"|"format")])