3 source/interpret.c | 94 ++++++++++++++++++++++++++++++++++++++++-------------
6 3 files changed, 76 insertions(+), 25 deletions(-)
8 diff --quilt old/source/interpret.c new/source/interpret.c
9 --- old/source/interpret.c
10 +++ new/source/interpret.c
11 @@ -1297,7 +1297,7 @@ static void addToGlobalSymTab(Symbol *sy
12 #define OK_TO_PUSH(n) \
13 (StackP + (n) <= (TheStack + STACK_SIZE))
15 - #define PUSH_CHECK(n) \
16 +#define PUSH_CHECK(n) \
18 if (!OK_TO_PUSH(n)) { \
19 EXEC_ERROR(StackOverflowMsg, ""); \
20 @@ -1314,28 +1314,64 @@ static void addToGlobalSymTab(Symbol *sy
24 +#define _POP() (*--StackP)
25 +#define _PUSH() (*StackP++)
26 +#define _PEEK(index) (*(StackP - (index) - 1))
28 #define POP(dataVal) \
31 - dataVal = *--StackP; \
36 #define PUSH(dataVal) \
39 - *StackP++ = dataVal; \
40 + _PUSH() = dataVal; \
43 #define PEEK(dataVal, peekIndex) \
45 PEEK_CHECK(peekIndex); \
46 - dataVal = *(StackP - (peekIndex) - 1); \
47 + dataVal = _PEEK(peekIndex); \
50 #define SUBST(dataVal, substIndex) \
52 PEEK_CHECK(substIndex); \
53 - *(StackP - (substIndex) - 1) = dataVal; \
54 + _PEEK(substIndex) = dataVal; \
57 +#define MOVE(from, to) \
61 + _PEEK(to) = _PEEK(from); \
64 +#define PEEK_PUSH(index) \
68 + PEEK(__val, index); \
69 + /* shift down elements */ \
70 + for (__i = (index); __i > 0; __i--) { \
71 + MOVE(__i - 1, __i); \
77 +#define PEEK_POP(value, index) \
80 + PEEK(value, index); \
81 + /* shift down elements */ \
82 + for (__i = (index); __i > 0; __i--) { \
83 + MOVE(__i - 1, __i); \
89 #define TO_INT(dataVal, number) \
90 @@ -1984,15 +2020,14 @@ static int dupStack(void)
94 -** copy the value at stack postion index onto the stack
95 -** Before: Prog-> [index], next, ...
96 -** TheStack-> val0, ... valIndex, next, ...
97 -** After: Prog-> index, [next], ...
98 -** TheStack-> valIndex, val0, ... valIndex, next, ...
99 +** brings index on top of the stack and shifts above elements down
100 +** Before: Prog-> [Index], next, ...
101 +** TheStack-> val0, ... val{Index-1}, valIndex, next, ...
102 +** After: Prog-> Index, [next], ...
103 +** TheStack-> valIndex, val0, ... val{Index-1}, next, ...
105 static int peekPush(void)
111 @@ -2001,8 +2036,7 @@ static int peekPush(void)
113 STACKDUMP(index + 1, 3);
115 - PEEK(value, index);
121 @@ -2025,15 +2059,31 @@ static int peekPop(void)
123 STACKDUMP(index + 1, 3);
126 + PEEK_POP(value, index);
128 - /* shift down elements */
129 - for (i = index; i > 0; i--) {
130 - PEEK(value, i - 1);
139 +** duplicate the value at stack postion index onto the stack
140 +** Before: Prog-> [index], next, ...
141 +** TheStack-> val0, ... valIndex, next, ...
142 +** After: Prog-> index, [next], ...
143 +** TheStack-> valIndex, val0, ... valIndex, next, ...
145 +static int peekDup(void)
154 + STACKDUMP(index + 1, 3);
156 + PEEK(value, index);
161 diff --quilt old/source/ops.h new/source/ops.h
164 @@ -10,8 +10,9 @@ OP(PUSH_IMMED, pushImmed)
165 OP(PUSH_STRING, pushString) /* str */ /* push(str) */
166 OP(POP, popStack) /* pop(v) */
167 OP(DUP, dupStack) /* pop(v), push(v,v) */
168 -OP(PEEK_PUSH, peekPush) /* n */ /* peek(v, n), push(v) */
169 -OP(PEEK_POP, peekPop) /* n */ /* pop(v0,..,vn), push(v0,...,v{n-1}) */
170 +OP(PEEK_PUSH, peekPush) /* n */ /* pop(v0,..,vn), push(v{n-1},...,v0,vn) */
171 +OP(PEEK_POP, peekPop) /* n */ /* pop(v0,..,vn), push(v{n-1},...,v0) */
172 +OP(PEEK_DUP, peekDup) /* n */ /* peek(v, n), push(v) */
173 OP(ADD, add) /* pop(v2,v1), push(v1 + v2) */
174 OP(SUB, subtract) /* pop(v2,v1), push(v1 - v2) */
175 OP(MUL, multiply) /* pop(v2,v1), push(v1 * v2) */
176 diff --quilt old/source/parse.y new/source/parse.y
177 --- old/source/parse.y
178 +++ new/source/parse.y
179 @@ -602,7 +602,7 @@ lventry: SYMBOL {
182 /* above the rvalue is the array + nDim strings */
183 - ADD_OP(OP_PEEK_PUSH); ADD_IMMED($2 + 1);
184 + ADD_OP(OP_PEEK_DUP); ADD_IMMED($2 + 1);
186 ADD_OP(OP_ARRAY_ASSIGN); ADD_IMMED($2);