3 source/interpret.c | 181 +++++++++++++++++------------------------------------
5 source/parse.y | 40 ++++-------
6 3 files changed, 81 insertions(+), 148 deletions(-)
8 diff --quilt old/source/interpret.c new/source/interpret.c
9 --- old/source/interpret.c
10 +++ new/source/interpret.c
11 @@ -835,24 +835,6 @@ Symbol *InstallMultiAssignExpr(void)
15 -** install an array iteration symbol
16 -** it is tagged as an integer but holds an array node pointer
18 -#define ARRAY_ITER_SYM_PREFIX "aryiter #"
19 -Symbol *InstallIteratorSymbol(void)
21 - char symbolName[sizeof(ARRAY_ITER_SYM_PREFIX) + TYPE_INT_STR_SIZE(int)];
23 - static int interatorNameIndex;
25 - sprintf(symbolName, ARRAY_ITER_SYM_PREFIX "%d", interatorNameIndex);
26 - ++interatorNameIndex;
27 - value.tag = INT_TAG;
28 - value.val.arrayPtr = NULL;
29 - return(InstallSymbol(symbolName, LOCAL_SYM, value));
33 ** Lookup a constant string by its value. This allows reuse of string
34 ** constants and fixing a leak in the interpreter.
36 @@ -3574,10 +3556,10 @@ static int arrayRefAndAssignSetup(void)
38 ** setup symbol values for array iteration in interpreter
40 -** Before: Prog-> [iter], ARRAY_ITER, withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
41 +** Before: Prog-> [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), endLoopBranch, next, ...
42 ** TheStack-> [arrayVal], next, ...
43 -** After: Prog-> iter, [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
44 -** TheStack-> [next], ...
45 +** After: Prog-> [ARRAY_ITER], withVal, iterVarKey(, iterVarVal), endLoopBranch, next, ...
46 +** TheStack-> [iter], next, ...
48 ** iter is a symbol which gives the position of the iterator value in
50 @@ -3585,30 +3567,24 @@ static int arrayRefAndAssignSetup(void)
52 static int beginArrayIter(void)
55 - DataValue *iteratorValPtr;
66 - if (iterator->type == LOCAL_SYM) {
67 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
70 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
73 - iteratorValPtr->tag = INT_TAG;
74 if (arrayVal.tag != ARRAY_TAG) {
75 EXEC_ERROR("can't iterate non-array", NULL);
78 - iteratorValPtr->val.arrayPtr = arrayIterateFirst(&arrayVal);
79 + /* maybe a new ARYITER_TAG */
80 + iterator.tag = NO_TAG;
81 + iterator.val.arrayPtr = arrayIterateFirst(&arrayVal);
88 @@ -3618,10 +3594,10 @@ static int beginArrayIter(void)
89 ** this allows iterators to progress even if you delete any node in the array
90 ** except the item just after the current key
92 -** Before: Prog-> iter, ARRAY_ITER, [withVal], iterVarKey(, iterVarVal), iter, endLoopBranch, next, ...
93 -** TheStack-> [next], ...
94 -** After: Prog-> iter, ARRAY_ITER, withVal, iterVarKey(, iterVarVal), iter, endLoopBranch, [next], ...
95 -** TheStack-> [next], ... (unchanged)
96 +** Before: Prog-> ARRAY_ITER, [withVal], iterVarKey(, iterVarVal), endLoopBranch, next, ...
97 +** TheStack-> [iter], ...
98 +** After: Prog-> ARRAY_ITER, withVal, iterVarKey(, iterVarVal), endLoopBranch, [next], ...
99 +** TheStack-> [iter], ... (unchanged)
101 ** iter is a symbol which gives the position of the iterator value in
102 ** the stack frame (set up by OP_BEGIN_ARRAY_ITER); that value refers
103 @@ -3638,10 +3614,9 @@ static int beginArrayIter(void)
105 static int arrayIter(void)
110 - DataValue *iteratorValPtr;
111 + DataValue iterator;
112 DataValue *keyValPtr;
114 SparseArrayEntry *thisEntry;
115 @@ -3649,16 +3624,17 @@ static int arrayIter(void)
128 GET_BRANCH(branchAddr);
132 if (keySym->type == LOCAL_SYM) {
133 keyValPtr = &FP_GET_SYM_VAL(FrameP, keySym);
135 @@ -3683,14 +3659,7 @@ static int arrayIter(void)
136 valPtr->tag = NO_TAG;
139 - if (iterator->type == LOCAL_SYM) {
140 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
143 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
146 - thisEntry = iteratorValPtr->val.arrayPtr;
147 + thisEntry = iterator.val.arrayPtr;
148 if (thisEntry && thisEntry->nodePtrs.color != -1) {
150 keyValPtr->tag = STRING_TAG;
151 @@ -3703,52 +3672,48 @@ static int arrayIter(void)
154 /* advance iterator */
155 - iteratorValPtr->val.arrayPtr = arrayIterateNext(thisEntry);
156 + iterator.val.arrayPtr = arrayIterateNext(thisEntry);
168 -** Before: Prog-> [iter], ARRAY_ITER_ARRAY, withVal, keyArraySym(, valSym), iter, endLoopBranch, next, ...
169 +** Before: Prog-> [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), endLoopBranch, next, ...
170 ** TheStack-> [arrayVal], nDims, next, ...
171 -** After: Prog-> iter, [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), iter, endLoopBranch, next, ...
172 -** TheStack-> [nDims], next, ...
173 +** After: Prog-> [ARRAY_ITER_ARRAY], withVal, keyArraySym(, valSym), endLoopBranch, next, ...
174 +** TheStack-> [iter], nDims, next, ...
176 static int beginArrayIterArray(void)
179 - DataValue *iteratorValPtr;
180 + DataValue iterator;
192 - if (iterator->type == LOCAL_SYM) {
193 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
196 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
200 EXEC_ERROR("bad multi dimension", NULL);
203 - iteratorValPtr->tag = INT_TAG;
204 if (arrayVal.tag != ARRAY_TAG) {
205 EXEC_ERROR("can't iterate non-array", NULL);
208 - iteratorValPtr->val.arrayPtr = arrayIterateFirst(&arrayVal);
209 + /* maybe a new ARYITER_TAG */
210 + iterator.tag = NO_TAG;
211 + iterator.val.arrayPtr = arrayIterateFirst(&arrayVal);
217 @@ -3807,17 +3772,16 @@ static Boolean splitKeyIntoArray(const c
221 -** Before: Prog-> iter, ARRAY_ITER_ARRAY, [withVal], keyArraySym, (, valSym), iter, endLoopBranch, next, ...
222 -** TheStack-> [nDims], next, ...
223 -** After: Prog-> iter, ARRAY_ITER_ARRAY, withVal, keyArraySym, (, valSym), iter, endLoopBranch, [next], ...
224 -** TheStack-> [nDims], next, ... (unchanged)
225 +** Before: Prog-> ARRAY_ITER_ARRAY, [withVal], keyArraySym, (, valSym), endLoopBranch, next, ...
226 +** TheStack-> [iter], nDims, next, ...
227 +** After: Prog-> ARRAY_ITER_ARRAY, withVal, keyArraySym, (, valSym), endLoopBranch, [next], ...
228 +** TheStack-> [iter], nDims, next, ... (unchanged)
230 static int arrayIterArray(void)
235 - DataValue *iteratorValPtr;
236 + DataValue iterator;
237 DataValue *keyArrayPtr;
239 SparseArrayEntry *thisEntry;
240 @@ -3827,16 +3791,16 @@ static int arrayIterArray(void)
241 Boolean keyFound = False;
248 GET_SYM(keyArraySym);
253 GET_BRANCH(branchAddr);
258 if (keyArraySym->type == LOCAL_SYM) {
259 @@ -3864,14 +3828,7 @@ static int arrayIterArray(void)
260 valPtr->tag = NO_TAG;
263 - if (iterator->type == LOCAL_SYM) {
264 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
267 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
270 - thisEntry = iteratorValPtr->val.arrayPtr;
271 + thisEntry = iterator.val.arrayPtr;
272 while (thisEntry && thisEntry->nodePtrs.color != -1) {
274 /* check if this is an nDims key, but only if requested */
275 @@ -3901,12 +3858,14 @@ static int arrayIterArray(void)
276 thisEntry = arrayIterateNext(thisEntry);
279 - iteratorValPtr->val.arrayPtr = thisEntry;
280 + iterator.val.arrayPtr = thisEntry;
282 if (!keyFound && (!thisEntry || thisEntry->nodePtrs.color == -1)) {
291 @@ -4606,39 +4565,26 @@ static void disasmInternal(Inst *inst, i
295 - case OP_BEGIN_ARRAY_ITER:
296 - case OP_BEGIN_ARRAY_ITER_ARRAY:
297 - CHECK_OPERANDS(1, SYM_INST);
299 - dumpInst(&inst[i+1], "aryIter");
304 CHECK_OPERANDS(1, IMMED_INST);
305 if (!inst[i+1].val.immed) {
307 - CHECK_OPERANDS(4, IMMED_INST,
308 - SYM_INST, SYM_INST, BRANCH_INST);
309 + CHECK_OPERANDS(3, IMMED_INST, SYM_INST, BRANCH_INST);
310 dumpInst(&inst[i+2], "key");
312 - dumpInst(&inst[i+3], "aryIter");
314 - dumpInst(&inst[i+4], "end-loop");
316 + printd(" := aryIter++");
317 + dumpInst(&inst[i+3], "end-loop");
322 - CHECK_OPERANDS(5, IMMED_INST,
323 - SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
324 + CHECK_OPERANDS(4, IMMED_INST,
325 + SYM_INST, SYM_INST, BRANCH_INST);
326 dumpInst(&inst[i+2], "key");
328 dumpInst(&inst[i+3], "val");
330 - dumpInst(&inst[i+4], "aryIter");
332 - dumpInst(&inst[i+5], "end-loop");
334 + printd(" := aryIter++");
335 + dumpInst(&inst[i+4], "end-loop");
340 @@ -4646,27 +4592,22 @@ static void disasmInternal(Inst *inst, i
341 CHECK_OPERANDS(1, IMMED_INST);
342 if (!inst[i+1].val.immed) {
344 - CHECK_OPERANDS(4, IMMED_INST,
345 - SYM_INST, SYM_INST, BRANCH_INST);
346 + CHECK_OPERANDS(3, IMMED_INST, SYM_INST, BRANCH_INST);
347 dumpInst(&inst[i+2], "keyArr");
349 - dumpInst(&inst[i+3], "aryIter");
351 - dumpInst(&inst[i+4], "end-loop");
353 + printd(" := aryIter++");
354 + dumpInst(&inst[i+3], "end-loop");
359 - CHECK_OPERANDS(5, IMMED_INST,
360 - SYM_INST, SYM_INST, SYM_INST, BRANCH_INST);
361 + CHECK_OPERANDS(4, IMMED_INST,
362 + SYM_INST, SYM_INST, BRANCH_INST);
363 dumpInst(&inst[i+2], "keyArr");
365 dumpInst(&inst[i+3], "val");
367 - dumpInst(&inst[i+4], "aryIter");
369 - dumpInst(&inst[i+5], "end-loop");
371 + printd(" := aryIter++");
372 + dumpInst(&inst[i+4], "end-loop");
377 diff --quilt old/source/parse.y new/source/parse.y
378 --- old/source/parse.y
379 +++ new/source/parse.y
380 @@ -256,71 +256,63 @@ stmt: ';' blank
381 ADD_OP(OP_BRANCH); ADD_BR_OFF($3); SET_BR_OFF($5, GetPC());
383 | for '(' blank SYMBOL IN blank arrayexpr blank ')' {
384 - Symbol *iterSym = InstallIteratorSymbol();
385 ADD_OP(OP_BEGIN_ARRAY_ITER);
387 ADD_OP(OP_ARRAY_ITER);
388 ADD_IMMED(0); /* without val symbol */
396 - SET_BR_OFF($7+6, GetPC());
397 - FillLoopAddrs(GetPC(), $7+2);
399 + SET_BR_OFF($7+4, GetPC());
400 + FillLoopAddrs(GetPC(), $7+1);
401 + ADD_OP(OP_POP); /* remove iter from stack */
403 | for '(' blank SYMBOL KEYVAL SYMBOL IN blank arrayexpr blank ')' {
404 - Symbol *iterSym = InstallIteratorSymbol();
405 ADD_OP(OP_BEGIN_ARRAY_ITER);
407 ADD_OP(OP_ARRAY_ITER);
408 ADD_IMMED(1); /* with val symbol */
417 - SET_BR_OFF($9+7, GetPC());
418 - FillLoopAddrs(GetPC(), $9+2);
420 + SET_BR_OFF($9+5, GetPC());
421 + FillLoopAddrs(GetPC(), $9+1);
422 + ADD_OP(OP_POP); /* remove iter from stack */
424 | for '(' blank SYMBOL '[' numexpropt ']' IN blank arrayexpr blank ')' {
425 - Symbol *iterSym = InstallIteratorSymbol();
426 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
428 ADD_OP(OP_ARRAY_ITER_ARRAY);
429 ADD_IMMED(0); /* without val symbol */
437 - SET_BR_OFF($10+6, GetPC());
438 - FillLoopAddrs(GetPC(), $10+2);
440 + SET_BR_OFF($10+4, GetPC());
441 + FillLoopAddrs(GetPC(), $10+1);
442 + ADD_OP(OP_POP); /* remove iter from stack */
443 ADD_OP(OP_POP); /* remove nDim from stack */
445 | for '(' blank SYMBOL '[' numexpropt ']' KEYVAL SYMBOL IN blank arrayexpr blank ')' {
446 - Symbol *iterSym = InstallIteratorSymbol();
447 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
449 ADD_OP(OP_ARRAY_ITER_ARRAY);
450 ADD_IMMED(1); /* with val symbol */
459 - SET_BR_OFF($12+7, GetPC());
460 - FillLoopAddrs(GetPC(), $12+2);
462 + SET_BR_OFF($12+5, GetPC());
463 + FillLoopAddrs(GetPC(), $12+1);
464 + ADD_OP(OP_POP); /* remove iter from stack */
465 ADD_OP(OP_POP); /* remove nDim from stack */
467 | BREAK stmtend blank {
468 diff --quilt old/source/ops.h new/source/ops.h
471 @@ -40,10 +40,10 @@ OP(BRANCH_NEVER, branchNever)
472 OP(ARRAY_REF, arrayRef) /* N */ /* pop(kN..k1,a), push(a[k1..kN]) */
473 OP(ARRAY_ASSIGN, arrayAssign) /* N */ /* pop(v,kN..k1,a), a[k1..kN]=v */
474 OP(ARRAY_ASSIGN_NEXT, arrayAssignNext) /* pop(v, a), a[a.nextidx] = v */
475 -OP(BEGIN_ARRAY_ITER, beginArrayIter) /* it */ /* pop(a), it=a.begin */
476 -OP(ARRAY_ITER, arrayIter) /*w,k[,v],it,pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
477 -OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /*it*/ /* */
478 -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 */
479 +OP(BEGIN_ARRAY_ITER, beginArrayIter) /* pop(a), it=a.begin, push(it) */
480 +OP(ARRAY_ITER, arrayIter) /*w,k[,v],pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
481 +OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /* pop(a), it=a.begin, push(it) */
482 +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 */
483 OP(IN_ARRAY, inArray) /* pop(a,k), push(a[k]?1:0) */
484 OP(ARRAY_DELETE, deleteArrayElement) /*N*/ /* N>0 ? (pop(kN..k1,a), del(a[k])) : (pop(a), delall(a)) */
485 OP(PUSH_ARRAY_SYM, pushArraySymVal) /*s,i*/ /* if i: s.v=ary()), push(s.v) */