refresh InterpretDebug5.diff
[nedit-bw.git] / array-next-num-index.patch
blobc76d02817d775d710d8667692b7e93c4b0eb86e4
1 Subject: Helper syntax for arrays with numerical indexes
3 Working with arrays which have only numerical indexes is a little
4 cumbersome. Especially to append/push an element to the end of the array.
6 arr[@]
8 gives an index which is one greater than all used numerical indexes in the
9 array arr. If none numerical index is used, 0 is returned.
11 arr[@] = expr
13 assigns expr to the array at position arr[@].
15 ---
17 source/interpret.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++
18 source/ops.h | 2 +
19 source/parse.y | 6 +++
20 3 files changed, 99 insertions(+)
22 diff --quilt old/source/interpret.c new/source/interpret.c
23 --- old/source/interpret.c
24 +++ new/source/interpret.c
25 @@ -3231,6 +3231,27 @@ SparseArrayEntry *arrayIterateNext(Spars
26 return(nextPos);
29 +static int arrayMaxNumIdx(DataValue *theArray)
31 + int maxKey = -1;
32 + SparseArrayEntry *iter;
34 + iter = arrayIterateFirst(theArray);
35 + while (iter) {
36 + int thisKey;
38 + if (StringToNum(iter->key, &thisKey)) {
39 + /* the rbTree is sorted, but "10" < "2" */
40 + if (thisKey > maxKey)
41 + maxKey = thisKey;
42 + }
44 + iter = arrayIterateNext(iter);
45 + }
47 + return maxKey;
51 ** evaluate an array element and push the result onto the stack
53 @@ -3884,6 +3905,76 @@ static int typeOfOut(void)
57 +** assign to the next numerical index array element of a referenced
58 +** array on the stack
59 +**
60 +** Before: Prog-> next, ...
61 +** TheStack-> rhs, ArraySym, next, ...
62 +** After: Prog-> next, ... (unchanged)
63 +** TheStack-> next, ...
64 +*/
65 +static int arrayAssignNext(void)
67 + char *keyString = NULL;
68 + DataValue srcValue, dstArray;
69 + int errNum;
71 + DISASM_RT(PC-1, 1);
72 + STACKDUMP(2, 3);
74 + POP(srcValue);
75 + POP(dstArray);
77 + if (dstArray.tag != ARRAY_TAG && dstArray.tag != NO_TAG) {
78 + return execError("cannot assign array element of non-array", NULL);
79 + }
81 + if (srcValue.tag == ARRAY_TAG) {
82 + DataValue arrayCopyValue;
84 + errNum = ArrayCopy(&arrayCopyValue, &srcValue);
85 + srcValue = arrayCopyValue;
86 + if (errNum != STAT_OK) {
87 + return errNum;
88 + }
89 + }
91 + keyString = AllocStringOfNumber(arrayMaxNumIdx(&dstArray) + 1);
93 + if (!ArrayInsert(&dstArray, keyString, &srcValue)) {
94 + return execError("array member allocation failure", NULL);
95 + }
97 + return STAT_OK;
101 +** return the free numeric index that is greater than all others
103 +** Before: Prog-> next, ...
104 +** TheStack-> ArraySym, next, ...
105 +** After: Prog-> next, ... (unchanged)
106 +** TheStack-> numIdx, next, ...
108 +static int arrayNextNumIdx(void)
110 + DataValue srcArray;
111 + SparseArrayEntry *iter;
113 + DISASM_RT(PC-1, 1);
114 + STACKDUMP(1, 3);
116 + POP(srcArray);
117 + if (srcArray.tag != ARRAY_TAG) {
118 + return execError("operator [@] on non-array", NULL);
121 + PUSH_INT(arrayMaxNumIdx(&srcArray) + 1);
123 + return STAT_OK;
127 ** checks errno after operations which can set it. If an error occured,
128 ** creates appropriate error messages and returns false
130 diff --quilt old/source/parse.y new/source/parse.y
131 --- old/source/parse.y
132 +++ new/source/parse.y
133 @@ -397,6 +397,9 @@ simpstmt: /* simple variable assignmen
134 ADD_OP($1);
135 ADD_OP(OP_ARRAY_ASSIGN); ADD_IMMED(1);
137 + | initarraylv '[' '@' ']' '=' blank expr {
138 + ADD_OP(OP_ARRAY_ASSIGN_NEXT);
140 | funccall
141 | lvlistexpr
143 @@ -709,6 +712,9 @@ numexpr: '(' blank expr blank ')'
144 | numexpr dot field {
145 ADD_OP(OP_ARRAY_REF); ADD_IMMED(1);
147 + | numexpr '[' '@' ']' {
148 + ADD_OP(OP_ARRAY_NEXT_NUM_IDX);
150 | '-' blank numexpr %prec UNARY_MINUS { ADD_OP(OP_NEGATE); }
151 | NOT blank numexpr { ADD_OP(OP_NOT); }
152 | numexpr '+' blank numexpr { ADD_OP(OP_ADD); }
153 diff --quilt old/source/ops.h new/source/ops.h
154 --- old/source/ops.h
155 +++ new/source/ops.h
156 @@ -39,6 +39,7 @@ OP(BRANCH_FALSE, branchFalse)
157 OP(BRANCH_NEVER, branchNever) /* off */ /* */
158 OP(ARRAY_REF, arrayRef) /* N */ /* pop(kN..k1,a), push(a[k1..kN]) */
159 OP(ARRAY_ASSIGN, arrayAssign) /* N */ /* pop(v,kN..k1,a), a[k1..kN]=v */
160 +OP(ARRAY_ASSIGN_NEXT, arrayAssignNext) /* pop(v, a), a[a.nextidx] = v */
161 OP(BEGIN_ARRAY_ITER, beginArrayIter) /* it */ /* pop(a), it=a.begin */
162 OP(ARRAY_ITER, arrayIter) /*w,k[,v],it,pc*/ /* it?(k.v=it.k,(w?v.v=it.v:),it++):PC=pc */
163 OP(BEGIN_ARRAY_ITER_ARRAY, beginArrayIterArray) /*it*/ /* */
164 @@ -47,6 +48,7 @@ OP(IN_ARRAY, inArray)
165 OP(ARRAY_DELETE, deleteArrayElement) /*N*/ /* N>0 ? (pop(kN..k1,a), del(a[k])) : (pop(a), delall(a)) */
166 OP(PUSH_ARRAY_SYM, pushArraySymVal) /*s,i*/ /* if i: s.v=ary()), push(s.v) */
167 OP(ARRAY_REF_ASSIGN_SETUP, arrayRefAndAssignSetup) /*op,N*/ /* pop(v,kN..a), a[k1..kN] op= v */
168 +OP(ARRAY_NEXT_NUM_IDX, arrayNextNumIdx) /* pop(a), push(a.nextidx) */
169 OP(PUSH_ARG, pushArgVal) /* pop(num), push($num) */
170 OP(PUSH_ARG_COUNT, pushArgCount) /* push($n_args) */
171 OP(PUSH_ARG_ARRAY, pushArgArray) /* push($args) */