3 source/interpret.c | 181 +++++++++++++++++------------------------------------
6 source/parse.y | 40 ++++-------
7 4 files changed, 81 insertions(+), 149 deletions(-)
9 diff --quilt old/source/interpret.c new/source/interpret.c
10 --- old/source/interpret.c
11 +++ new/source/interpret.c
12 @@ -835,24 +835,6 @@ Symbol *InstallMultiAssignExpr(void)
16 -** install an array iteration symbol
17 -** it is tagged as an integer but holds an array node pointer
19 -#define ARRAY_ITER_SYM_PREFIX "aryiter #"
20 -Symbol *InstallIteratorSymbol(void)
22 - char symbolName[sizeof(ARRAY_ITER_SYM_PREFIX) + TYPE_INT_STR_SIZE(int)];
24 - static int interatorNameIndex;
26 - sprintf(symbolName, ARRAY_ITER_SYM_PREFIX "%d", interatorNameIndex);
27 - ++interatorNameIndex;
28 - value.tag = INT_TAG;
29 - value.val.arrayPtr = NULL;
30 - return(InstallSymbol(symbolName, LOCAL_SYM, value));
34 ** Lookup a constant string by its value. This allows reuse of string
35 ** constants and fixing a leak in the interpreter.
37 @@ -3562,10 +3544,10 @@ static int arrayRefAndAssignSetup(void)
39 ** setup symbol values for array iteration in interpreter
41 -** Before: Prog-> [iter], ARRAY_ITER, withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
42 +** Before: Prog-> [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), endLoopBranch, next, ...
43 ** TheStack-> [arrayVal], next, ...
44 -** After: Prog-> iter, [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
45 -** TheStack-> [next], ...
46 +** After: Prog-> [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), endLoopBranch, next, ...
47 +** TheStack-> [iter], next, ...
49 ** iter is a symbol which gives the position of the iterator value in
51 @@ -3573,30 +3555,24 @@ static int arrayRefAndAssignSetup(void)
53 static int beginArrayIter(void)
56 - DataValue *iteratorValPtr;
67 - if (iterator->type == LOCAL_SYM) {
68 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
71 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
74 - iteratorValPtr->tag = INT_TAG;
75 if (arrayVal.tag != ARRAY_TAG) {
76 EXEC_ERROR("can't iterate non-array", NULL);
79 - iteratorValPtr->val.arrayPtr = arrayIterateFirst(&arrayVal);
80 + /* maybe a new ARYITER_TAG */
81 + iterator.tag = NO_TAG;
82 + iterator.val.arrayPtr = arrayIterateFirst(&arrayVal);
89 @@ -3606,10 +3582,10 @@ static int beginArrayIter(void)
90 ** this allows iterators to progress even if you delete any node in the array
91 ** except the item just after the current key
93 -** Before: Prog-> iter, ARRAY_ITER, [withVal], iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
94 -** TheStack-> [next], ...
95 -** After: Prog-> iter, ARRAY_ITER, withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, [next], ...
96 -** TheStack-> [next], ... (unchanged)
97 +** Before: Prog-> ARRAY_ITER, [withVal], iterVarKey(, iterVarVal), endLoopBranch, next, ...
98 +** TheStack-> [iter], ...
99 +** After: Prog-> ARRAY_ITER, withVal, iterVarKey(, iterVarVal), endLoopBranch, [next], ...
100 +** TheStack-> [iter], ... (unchanged)
102 ** iter is a symbol which gives the position of the iterator value in
103 ** the stack frame (set up by OP_BEGIN_ARRAY_ITER); that value refers
104 @@ -3626,10 +3602,9 @@ static int beginArrayIter(void)
106 static int arrayIter(void)
111 - DataValue *iteratorValPtr;
112 + DataValue iterator;
113 DataValue *keyValPtr;
115 SparseArrayEntry *thisEntry;
116 @@ -3637,16 +3612,17 @@ static int arrayIter(void)
129 GET_BRANCH(branchAddr);
133 if (keySym->type == LOCAL_SYM) {
134 keyValPtr = &FP_GET_SYM_VAL(FrameP, keySym);
136 @@ -3671,14 +3647,7 @@ static int arrayIter(void)
137 valPtr->tag = NO_TAG;
140 - if (iterator->type == LOCAL_SYM) {
141 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
144 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
147 - thisEntry = iteratorValPtr->val.arrayPtr;
148 + thisEntry = iterator.val.arrayPtr;
149 if (thisEntry && thisEntry->nodePtrs.color != -1) {
151 keyValPtr->tag = STRING_TAG;
152 @@ -3691,52 +3660,48 @@ static int arrayIter(void)
155 /* advance iterator */
156 - iteratorValPtr->val.arrayPtr = arrayIterateNext(thisEntry);
157 + iterator.val.arrayPtr = arrayIterateNext(thisEntry);
169 -** Before: Prog-> [iter], ARRAY_ITER_ARRAY, withVal, keyArraySym(, valSym), iter, endLoopBranch, next, ...
170 +** Before: Prog-> [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), endLoopBranch, next, ...
171 ** TheStack-> [arrayVal], nDims, next, ...
172 -** After: Prog-> iter, [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), iter, endLoopBranch, next, ...
173 -** TheStack-> [nDims], next, ...
174 +** After: Prog-> [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), endLoopBranch, next, ...
175 +** TheStack-> [iter], nDims, next, ...
177 static int beginArrayIterArray(void)
180 - DataValue *iteratorValPtr;
181 + DataValue iterator;
193 - if (iterator->type == LOCAL_SYM) {
194 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
197 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
201 EXEC_ERROR("bad multi dimension", NULL);
204 - iteratorValPtr->tag = INT_TAG;
205 if (arrayVal.tag != ARRAY_TAG) {
206 EXEC_ERROR("can't iterate non-array", NULL);
209 - iteratorValPtr->val.arrayPtr = arrayIterateFirst(&arrayVal);
210 + /* maybe a new ARYITER_TAG */
211 + iterator.tag = NO_TAG;
212 + iterator.val.arrayPtr = arrayIterateFirst(&arrayVal);
218 @@ -3795,17 +3760,16 @@ static Boolean splitKeyIntoArray(const c
222 -** Before: Prog-> iter, ARRAY_ITER_ARRAY, [withVal], keyArraySym, (, valSym), iter, endLoopBranch, next, ...
223 -** TheStack-> [nDims], next, ...
224 -** After: Prog-> iter, ARRAY_ITER_ARRAY, withVal, keyArraySym, (, valSym), iter, endLoopBranch, [next], ...
225 -** TheStack-> [nDims], next, ... (unchanged)
226 +** Before: Prog-> ARRAY_ITER_ARRAY, [withVal], keyArraySym, (, valSym), endLoopBranch, next, ...
227 +** TheStack-> [iter], nDims, next, ...
228 +** After: Prog-> ARRAY_ITER_ARRAY, withVal, keyArraySym, (, valSym), endLoopBranch, [next], ...
229 +** TheStack-> [iter], nDims, next, ... (unchanged)
231 static int arrayIterArray(void)
236 - DataValue *iteratorValPtr;
237 + DataValue iterator;
238 DataValue *keyArrayPtr;
240 SparseArrayEntry *thisEntry;
241 @@ -3815,16 +3779,16 @@ static int arrayIterArray(void)
242 Boolean keyFound = False;
249 GET_SYM(keyArraySym);
254 GET_BRANCH(branchAddr);
259 if (keyArraySym->type == LOCAL_SYM) {
260 @@ -3852,14 +3816,7 @@ static int arrayIterArray(void)
261 valPtr->tag = NO_TAG;
264 - if (iterator->type == LOCAL_SYM) {
265 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
268 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
271 - thisEntry = iteratorValPtr->val.arrayPtr;
272 + thisEntry = iterator.val.arrayPtr;
273 while (thisEntry && thisEntry->nodePtrs.color != -1) {
275 /* check if this is an nDims key, but only if requested */
276 @@ -3889,12 +3846,14 @@ static int arrayIterArray(void)
277 thisEntry = arrayIterateNext(thisEntry);
280 - iteratorValPtr->val.arrayPtr = thisEntry;
281 + iterator.val.arrayPtr = thisEntry;
283 if (!keyFound && (!thisEntry || thisEntry->nodePtrs.color == -1)) {
292 @@ -4593,39 +4552,26 @@ static void disasmInternal(Inst *inst, i
296 - case OP_BEGIN_ARRAY_ITER:
297 - case OP_BEGIN_ARRAY_ITER_ARRAY:
298 - CHECK_OPERANDS(1, SYM_INST);
300 - dumpInst(&inst[i+1], "aryIter");
305 CHECK_OPERANDS(1, IMMED_INST);
306 if (!inst[i+1].val.immed) {
308 - CHECK_OPERANDS(4, IMMED_INST,
309 - SYM_INST, SYM_INST, BRANCH_INST);
310 + CHECK_OPERANDS(3, IMMED_INST, SYM_INST, BRANCH_INST);
311 dumpInst(&inst[i+2], "key");
313 - dumpInst(&inst[i+3], "aryIter");
315 - dumpInst(&inst[i+4], "end-loop");
317 + printd(" := aryIter++");
318 + dumpInst(&inst[i+3], "end-loop");
323 - CHECK_OPERANDS(5, IMMED_INST,
324 - SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
325 + CHECK_OPERANDS(4, IMMED_INST,
326 + SYM_INST, SYM_INST, BRANCH_INST);
327 dumpInst(&inst[i+2], "key");
329 dumpInst(&inst[i+3], "val");
331 - dumpInst(&inst[i+4], "aryIter");
333 - dumpInst(&inst[i+5], "end-loop");
335 + printd(" := aryIter++");
336 + dumpInst(&inst[i+4], "end-loop");
341 @@ -4633,27 +4579,22 @@ static void disasmInternal(Inst *inst, i
342 CHECK_OPERANDS(1, IMMED_INST);
343 if (!inst[i+1].val.immed) {
345 - CHECK_OPERANDS(4, IMMED_INST,
346 - SYM_INST, SYM_INST, BRANCH_INST);
347 + CHECK_OPERANDS(3, IMMED_INST, SYM_INST, BRANCH_INST);
348 dumpInst(&inst[i+2], "keyArr");
350 - dumpInst(&inst[i+3], "aryIter");
352 - dumpInst(&inst[i+4], "end-loop");
354 + printd(" := aryIter++");
355 + dumpInst(&inst[i+3], "end-loop");
360 - CHECK_OPERANDS(5, IMMED_INST,
361 - SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
362 + CHECK_OPERANDS(4, IMMED_INST,
363 + SYM_INST, SYM_INST, BRANCH_INST);
364 dumpInst(&inst[i+2], "keyArr");
366 dumpInst(&inst[i+3], "val");
368 - dumpInst(&inst[i+4], "aryIter");
370 - dumpInst(&inst[i+5], "end-loop");
372 + printd(" := aryIter++");
373 + dumpInst(&inst[i+4], "end-loop");
378 diff --quilt old/source/parse.y new/source/parse.y
379 --- old/source/parse.y
380 +++ new/source/parse.y
381 @@ -256,71 +256,63 @@ stmt: ';' blank
382 ADD_OP(OP_BRANCH); ADD_BR_OFF($3); SET_BR_OFF($5, GetPC());
384 | for '(' blank SYMBOL IN blank arrayexpr blank ')' {
385 - Symbol *iterSym = InstallIteratorSymbol();
386 ADD_OP(OP_BEGIN_ARRAY_ITER);
388 ADD_OP(OP_ARRAY_ITER);
389 ADD_IMMED(0); /* without val symbol */
397 - SET_BR_OFF($7+6, GetPC());
398 - FillLoopAddrs(GetPC(), $7+2);
400 + SET_BR_OFF($7+4, GetPC());
401 + FillLoopAddrs(GetPC(), $7+1);
402 + ADD_OP(OP_POP); /* remove iter from stack */
404 | for '(' blank SYMBOL KEYVAL SYMBOL IN blank arrayexpr blank ')' {
405 - Symbol *iterSym = InstallIteratorSymbol();
406 ADD_OP(OP_BEGIN_ARRAY_ITER);
408 ADD_OP(OP_ARRAY_ITER);
409 ADD_IMMED(1); /* with val symbol */
418 - SET_BR_OFF($9+7, GetPC());
419 - FillLoopAddrs(GetPC(), $9+2);
421 + SET_BR_OFF($9+5, GetPC());
422 + FillLoopAddrs(GetPC(), $9+1);
423 + ADD_OP(OP_POP); /* remove iter from stack */
425 | for '(' blank SYMBOL '[' numexpropt ']' IN blank arrayexpr blank ')' {
426 - Symbol *iterSym = InstallIteratorSymbol();
427 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
429 ADD_OP(OP_ARRAY_ITER_ARRAY);
430 ADD_IMMED(0); /* without val symbol */
438 - SET_BR_OFF($10+6, GetPC());
439 - FillLoopAddrs(GetPC(), $10+2);
441 + SET_BR_OFF($10+4, GetPC());
442 + FillLoopAddrs(GetPC(), $10+1);
443 + ADD_OP(OP_POP); /* remove iter from stack */
444 ADD_OP(OP_POP); /* remove nDim from stack */
446 | for '(' blank SYMBOL '[' numexpropt ']' KEYVAL SYMBOL IN blank arrayexpr blank ')' {
447 - Symbol *iterSym = InstallIteratorSymbol();
448 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
450 ADD_OP(OP_ARRAY_ITER_ARRAY);
451 ADD_IMMED(1); /* with val symbol */
460 - SET_BR_OFF($12+7, GetPC());
461 - FillLoopAddrs(GetPC(), $12+2);
463 + SET_BR_OFF($12+5, GetPC());
464 + FillLoopAddrs(GetPC(), $12+1);
465 + ADD_OP(OP_POP); /* remove iter from stack */
466 ADD_OP(OP_POP); /* remove nDim from stack */
468 | BREAK stmtend blank {
469 diff --quilt old/source/ops.h new/source/ops.h
472 @@ -40,10 +40,10 @@ OP(BRANCH_NEVER, branchNever)
473 OP(ARRAY_REF, arrayRef) /* N */ /* pop(kN..k1,a), push(a[k1..kN]) */
474 OP(ARRAY_ASSIGN, arrayAssign) /* N */ /* pop(v,kN..k1,a), a[k1..kN]=v */
475 OP(ARRAY_ASSIGN_NEXT, arrayAssignNext) /* pop(v, a), a[a.nextidx] = v */
476 -OP(BEGIN_ARRAY_ITER, beginArrayIter) /* it */ /* pop(a), it=a.begin */
477 -OP(ARRAY_ITER, arrayIter) /*w,k[,v],it,pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
478 -OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /*it*/ /* */
479 -OP(ARRAY_ITER_ARRAY, arrayIterArray) /*w,ka[,v],it,pc*/ /* top(N),while it: (if dim(it.k)==N: (ka.v=split(it.k),(w?v.v=it.v:),return),it++), PC=pc */
480 +OP(BEGIN_ARRAY_ITER, beginArrayIter) /* pop(a), it=a.begin, push(it) */
481 +OP(ARRAY_ITER, arrayIter) /*w,k[,v],pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
482 +OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /* pop(a), it=a.begin, push(it) */
483 +OP(ARRAY_ITER_ARRAY, arrayIterArray) /*w,ka[,v],pc*/ /* top(N),while it: (if dim(it.k)==N: (ka.v=split(it.k),(w?v.v=it.v:),return),it++), PC=pc */
484 OP(IN_ARRAY, inArray) /* pop(a,k), push(a[k]?1:0) */
485 OP(ARRAY_DELETE, deleteArrayElement) /*N*/ /* N>0 ? (pop(kN..k1,a), del(a[k])) : (pop(a), delall(a)) */
486 OP(PUSH_ARRAY_SYM, pushArraySymVal) /*s,i*/ /* if i: s.v=ary()), push(s.v) */
487 diff --quilt old/source/interpret.h new/source/interpret.h
488 --- old/source/interpret.h
489 +++ new/source/interpret.h
490 @@ -161,7 +161,6 @@ int AddImmediate(int immed, char **msg);
491 int AddBranchOffset(Inst *to, char **msg);
492 int SetBranchOffset(Inst *from, Inst *to, char **msg);
494 -Symbol *InstallIteratorSymbol(void);
495 Symbol *LookupStringConstSymbol(const char *value);
496 Symbol *InstallStringConstSymbol(const char *str);
497 Symbol *LookupSymbol(const char *name);