3 source/interpret.c | 169 ++++++++++++++++++-----------------------------------
5 source/parse.y | 40 +++++-------
6 3 files changed, 79 insertions(+), 138 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 @@ -3606,10 +3588,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 @@ -3617,30 +3599,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 @@ -3650,10 +3626,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 @@ -3670,10 +3646,9 @@ static int beginArrayIter(void)
105 static int arrayIter(void)
110 - DataValue *iteratorValPtr;
111 + DataValue iterator;
112 DataValue *keyValPtr;
114 SparseArrayEntry *thisEntry;
115 @@ -3681,16 +3656,17 @@ static int arrayIter(void)
128 GET_BRANCH(branchAddr);
132 if (keySym->type == LOCAL_SYM) {
133 keyValPtr = &FP_GET_SYM_VAL(FrameP, keySym);
135 @@ -3715,14 +3691,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 @@ -3735,52 +3704,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 @@ -3839,17 +3804,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 @@ -3859,16 +3823,16 @@ static int arrayIterArray(void)
241 Boolean keyFound = False;
248 GET_SYM(keyArraySym);
253 GET_BRANCH(branchAddr);
258 if (keyArraySym->type == LOCAL_SYM) {
259 @@ -3896,14 +3860,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 @@ -3933,12 +3890,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 @@ -4598,53 +4557,43 @@ static void disasmInternal(Inst *inst, i
295 - case OP_BEGIN_ARRAY_ITER:
296 - case OP_BEGIN_ARRAY_ITER_ARRAY:
297 - printd(" %s in", inst[i+1].val.sym->name);
302 if (!inst[i+1].val.immed) {
304 - printd(" %s = %s++ end-loop=(%+td) %8p",
305 + printd(" %s = iter++ end-loop=(%+td) %8p",
306 inst[i+2].val.sym->name,
307 - inst[i+3].val.sym->name,
308 - inst[i+4].val.branch,
309 - &inst[i+4] + inst[i+4].val.branch);
311 + inst[i+3].val.branch,
312 + &inst[i+3] + inst[i+3].val.branch);
317 - printd(" %s=%s = %s++ end-loop=(%+td) %8p",
318 + printd(" %s=%s = iter++ end-loop=(%+td) %8p",
319 inst[i+2].val.sym->name,
320 inst[i+3].val.sym->name,
321 - inst[i+4].val.sym->name,
322 - inst[i+5].val.branch,
323 - &inst[i+5] + inst[i+5].val.branch);
325 + inst[i+4].val.branch,
326 + &inst[i+4] + inst[i+4].val.branch);
331 case OP_ARRAY_ITER_ARRAY:
332 if (!inst[i+1].val.immed) {
334 - printd(" %s[] = %s++ end-loop=(%+td) %8p",
335 + printd(" %s[] = iter++ end-loop=(%+td) %8p",
336 inst[i+2].val.sym->name,
337 - inst[i+3].val.sym->name,
338 - inst[i+4].val.branch,
339 - &inst[i+4] + inst[i+4].val.branch);
341 + inst[i+3].val.branch,
342 + &inst[i+3] + inst[i+3].val.branch);
347 - printd(" %s[]=%s = %s++ end-loop=(%+td) %8p",
348 + printd(" %s[]=%s = iter++ end-loop=(%+td) %8p",
349 inst[i+2].val.sym->name,
350 inst[i+3].val.sym->name,
351 - inst[i+4].val.sym->name,
352 - inst[i+5].val.branch,
353 - &inst[i+5] + inst[i+5].val.branch);
355 + inst[i+4].val.branch,
356 + &inst[i+4] + inst[i+4].val.branch);
361 diff --quilt old/source/parse.y new/source/parse.y
362 --- old/source/parse.y
363 +++ new/source/parse.y
364 @@ -256,71 +256,63 @@ stmt: ';' blank
365 ADD_OP(OP_BRANCH); ADD_BR_OFF($3); SET_BR_OFF($5, GetPC());
367 | for '(' blank SYMBOL IN blank arrayexpr blank ')' {
368 - Symbol *iterSym = InstallIteratorSymbol();
369 ADD_OP(OP_BEGIN_ARRAY_ITER);
371 ADD_OP(OP_ARRAY_ITER);
372 ADD_IMMED(0); /* without val symbol */
380 - SET_BR_OFF($7+6, GetPC());
381 - FillLoopAddrs(GetPC(), $7+2);
383 + SET_BR_OFF($7+4, GetPC());
384 + FillLoopAddrs(GetPC(), $7+1);
385 + ADD_OP(OP_POP); /* remove iter from stack */
387 | for '(' blank SYMBOL KEYVAL SYMBOL IN blank arrayexpr blank ')' {
388 - Symbol *iterSym = InstallIteratorSymbol();
389 ADD_OP(OP_BEGIN_ARRAY_ITER);
391 ADD_OP(OP_ARRAY_ITER);
392 ADD_IMMED(1); /* with val symbol */
401 - SET_BR_OFF($9+7, GetPC());
402 - FillLoopAddrs(GetPC(), $9+2);
404 + SET_BR_OFF($9+5, GetPC());
405 + FillLoopAddrs(GetPC(), $9+1);
406 + ADD_OP(OP_POP); /* remove iter from stack */
408 | for '(' blank SYMBOL '[' numexpropt ']' IN blank arrayexpr blank ')' {
409 - Symbol *iterSym = InstallIteratorSymbol();
410 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
412 ADD_OP(OP_ARRAY_ITER_ARRAY);
413 ADD_IMMED(0); /* without val symbol */
421 - SET_BR_OFF($10+6, GetPC());
422 - FillLoopAddrs(GetPC(), $10+2);
424 + SET_BR_OFF($10+4, GetPC());
425 + FillLoopAddrs(GetPC(), $10+1);
426 + ADD_OP(OP_POP); /* remove iter from stack */
427 ADD_OP(OP_POP); /* remove nDim from stack */
429 | for '(' blank SYMBOL '[' numexpropt ']' KEYVAL SYMBOL IN blank arrayexpr blank ')' {
430 - Symbol *iterSym = InstallIteratorSymbol();
431 ADD_OP(OP_BEGIN_ARRAY_ITER_ARRAY);
433 ADD_OP(OP_ARRAY_ITER_ARRAY);
434 ADD_IMMED(1); /* with val symbol */
443 - SET_BR_OFF($12+7, GetPC());
444 - FillLoopAddrs(GetPC(), $12+2);
446 + SET_BR_OFF($12+5, GetPC());
447 + FillLoopAddrs(GetPC(), $12+1);
448 + ADD_OP(OP_POP); /* remove iter from stack */
449 ADD_OP(OP_POP); /* remove nDim from stack */
451 | BREAK stmtend blank {
452 diff --quilt old/source/ops.h new/source/ops.h
455 @@ -40,10 +40,10 @@ OP(BRANCH_NEVER, branchNever)
456 OP(ARRAY_REF, arrayRef) /* N */ /* pop(kN..k1,a), push(a[k1..kN]) */
457 OP(ARRAY_ASSIGN, arrayAssign) /* N */ /* pop(v,kN..k1,a), a[k1..kN]=v */
458 OP(ARRAY_ASSIGN_NEXT, arrayAssignNext) /* pop(v, a), a[a.nextidx] = v */
459 -OP(BEGIN_ARRAY_ITER, beginArrayIter) /* it */ /* pop(a), it=a.begin */
460 -OP(ARRAY_ITER, arrayIter) /*w,k[,v],it,pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
461 -OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /*it*/ /* */
462 -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 */
463 +OP(BEGIN_ARRAY_ITER, beginArrayIter) /* pop(a), it=a.begin, push(it) */
464 +OP(ARRAY_ITER, arrayIter) /*w,k[,v],pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
465 +OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /* pop(a), it=a.begin, push(it) */
466 +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 */
467 OP(IN_ARRAY, inArray) /* pop(a,k), push(a[k]?1:0) */
468 OP(ARRAY_DELETE, deleteArrayElement) /*N*/ /* N>0 ? (pop(kN..k1,a), del(a[k])) : (pop(a), delall(a)) */
469 OP(PUSH_ARRAY_SYM, pushArraySymVal) /*s,i*/ /* if i: s.v=ary()), push(s.v) */