comment reduxed MultipleAssignment
[nedit-bw.git] / aryiter-on-stack.patch
blob345031c8880f603bec141433f789cc393f3e326e
1 ---
3 source/interpret.c | 169 ++++++++++++++++++-----------------------------------
4 source/ops.h | 8 +-
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
17 -*/
18 -#define ARRAY_ITER_SYM_PREFIX "aryiter #"
19 -Symbol *InstallIteratorSymbol(void)
21 - char symbolName[sizeof(ARRAY_ITER_SYM_PREFIX) + TYPE_INT_STR_SIZE(int)];
22 - DataValue value;
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));
32 -/*
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, ...
47 ** Where:
48 ** iter is a symbol which gives the position of the iterator value in
49 ** the stack frame
50 @@ -3617,30 +3599,24 @@ static int arrayRefAndAssignSetup(void)
52 static int beginArrayIter(void)
54 - Symbol *iterator;
55 - DataValue *iteratorValPtr;
56 + DataValue iterator;
57 DataValue arrayVal;
59 DISASM_RT();
60 STACKDUMP(1, 3);
62 - GET_SYM(iterator);
64 POP(arrayVal);
66 - if (iterator->type == LOCAL_SYM) {
67 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
68 - }
69 - else {
70 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
71 - }
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);
83 + PUSH(iterator);
85 return STAT_OK;
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)
100 ** Where:
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)
107 - Symbol *iterator;
108 Symbol *keySym;
109 Symbol *valSym;
110 - DataValue *iteratorValPtr;
111 + DataValue iterator;
112 DataValue *keyValPtr;
113 DataValue *valPtr;
114 SparseArrayEntry *thisEntry;
115 @@ -3681,16 +3656,17 @@ static int arrayIter(void)
116 int withVal;
118 DISASM_RT();
119 - STACKDUMP(0, 4);
120 + STACKDUMP(1, 4);
122 GET_IMMED(withVal);
123 GET_SYM(keySym);
124 if (withVal) {
125 GET_SYM(valSym);
127 - GET_SYM(iterator);
128 GET_BRANCH(branchAddr);
130 + POP(iterator);
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);
142 - else {
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) {
149 /* set key */
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);
158 else {
159 JUMP(branchAddr);
162 + PUSH(iterator);
164 return STAT_OK;
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)
178 - Symbol *iterator;
179 - DataValue *iteratorValPtr;
180 + DataValue iterator;
181 DataValue arrayVal;
182 int nDims;
184 DISASM_RT();
185 STACKDUMP(2, 3);
187 - GET_SYM(iterator);
189 POP(arrayVal);
190 PEEK_INT(nDims, 0);
192 - if (iterator->type == LOCAL_SYM) {
193 - iteratorValPtr = &FP_GET_SYM_VAL(FrameP, iterator);
195 - else {
196 - EXEC_ERROR("bad temporary iterator: %s", iterator->name);
199 if (nDims < 0) {
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);
213 + PUSH(iterator);
215 return STAT_OK;
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)
232 - Symbol *iterator;
233 Symbol *keyArraySym;
234 Symbol *valSym;
235 - DataValue *iteratorValPtr;
236 + DataValue iterator;
237 DataValue *keyArrayPtr;
238 DataValue *valPtr;
239 SparseArrayEntry *thisEntry;
240 @@ -3859,16 +3823,16 @@ static int arrayIterArray(void)
241 Boolean keyFound = False;
243 DISASM_RT();
244 - STACKDUMP(1, 4);
245 + STACKDUMP(2, 4);
247 GET_IMMED(withVal);
248 GET_SYM(keyArraySym);
249 if (withVal) {
250 GET_SYM(valSym);
252 - GET_SYM(iterator);
253 GET_BRANCH(branchAddr);
255 + POP(iterator);
256 PEEK_INT(nDims, 0);
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);
266 - else {
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);
277 break;
279 - iteratorValPtr->val.arrayPtr = thisEntry;
280 + iterator.val.arrayPtr = thisEntry;
282 if (!keyFound && (!thisEntry || thisEntry->nodePtrs.color == -1)) {
283 JUMP(branchAddr);
286 + PUSH(iterator);
288 return STAT_OK;
291 @@ -4598,53 +4557,43 @@ static void disasmInternal(Inst *inst, i
292 ++i;
293 break;
295 - case OP_BEGIN_ARRAY_ITER:
296 - case OP_BEGIN_ARRAY_ITER_ARRAY:
297 - printd(" %s in", inst[i+1].val.sym->name);
298 - ++i;
299 - break;
301 case OP_ARRAY_ITER:
302 if (!inst[i+1].val.immed) {
303 /* without val */
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);
310 - i += 4;
311 + inst[i+3].val.branch,
312 + &inst[i+3] + inst[i+3].val.branch);
313 + i += 3;
315 else {
316 /* with val */
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);
324 - i += 5;
325 + inst[i+4].val.branch,
326 + &inst[i+4] + inst[i+4].val.branch);
327 + i += 4;
329 break;
331 case OP_ARRAY_ITER_ARRAY:
332 if (!inst[i+1].val.immed) {
333 /* without val */
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);
340 - i += 4;
341 + inst[i+3].val.branch,
342 + &inst[i+3] + inst[i+3].val.branch);
343 + i += 3;
345 else {
346 /* with val */
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);
354 - i += 5;
355 + inst[i+4].val.branch,
356 + &inst[i+4] + inst[i+4].val.branch);
357 + i += 4;
359 break;
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);
370 - ADD_SYM(iterSym);
371 ADD_OP(OP_ARRAY_ITER);
372 ADD_IMMED(0); /* without val symbol */
373 ADD_SYM($4);
374 - ADD_SYM(iterSym);
375 ADD_BR_OFF(0);
377 blank block {
378 ADD_OP(OP_BRANCH);
379 - ADD_BR_OFF($7+2);
380 - SET_BR_OFF($7+6, GetPC());
381 - FillLoopAddrs(GetPC(), $7+2);
382 + ADD_BR_OFF($7+1);
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);
390 - ADD_SYM(iterSym);
391 ADD_OP(OP_ARRAY_ITER);
392 ADD_IMMED(1); /* with val symbol */
393 ADD_SYM($4);
394 ADD_SYM($6);
395 - ADD_SYM(iterSym);
396 ADD_BR_OFF(0);
398 blank block {
399 ADD_OP(OP_BRANCH);
400 - ADD_BR_OFF($9+2);
401 - SET_BR_OFF($9+7, GetPC());
402 - FillLoopAddrs(GetPC(), $9+2);
403 + ADD_BR_OFF($9+1);
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);
411 - ADD_SYM(iterSym);
412 ADD_OP(OP_ARRAY_ITER_ARRAY);
413 ADD_IMMED(0); /* without val symbol */
414 ADD_SYM($4);
415 - ADD_SYM(iterSym);
416 ADD_BR_OFF(0);
418 blank block {
419 ADD_OP(OP_BRANCH);
420 - ADD_BR_OFF($10+2);
421 - SET_BR_OFF($10+6, GetPC());
422 - FillLoopAddrs(GetPC(), $10+2);
423 + ADD_BR_OFF($10+1);
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);
432 - ADD_SYM(iterSym);
433 ADD_OP(OP_ARRAY_ITER_ARRAY);
434 ADD_IMMED(1); /* with val symbol */
435 ADD_SYM($4);
436 ADD_SYM($9);
437 - ADD_SYM(iterSym);
438 ADD_BR_OFF(0);
440 blank block {
441 ADD_OP(OP_BRANCH);
442 - ADD_BR_OFF($12+2);
443 - SET_BR_OFF($12+7, GetPC());
444 - FillLoopAddrs(GetPC(), $12+2);
445 + ADD_BR_OFF($12+1);
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
453 --- old/source/ops.h
454 +++ 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) */