-
Notifications
You must be signed in to change notification settings - Fork 3
/
stack_ops.c
253 lines (230 loc) · 5.45 KB
/
stack_ops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* stack operations */
void push(DCLANG_FLT a)
{
if (data_stack_ptr >= DATA_STACK_SIZE) {
printf("push -- stack overflow!\n");
data_stack_ptr = 0;
}
data_stack[data_stack_ptr++] = a;
}
void push_no_check(DCLANG_FLT a)
{
data_stack[data_stack_ptr++] = a;
}
DCLANG_FLT dclang_pop()
// special case -- has a return value b/c it can
// be used in other calls to give a value, which
// also means it can be an API call.
{
return data_stack[--data_stack_ptr];
}
void dropfunc()
{
if (data_stack_ptr < 1) {
printf("drop -- stack underflow!\n");
return;
}
--data_stack_ptr;
}
void dupfunc()
{
if (data_stack_ptr < 1) {
printf("dup -- stack underflow!\n");
return;
}
push(data_stack[data_stack_ptr - 1]);
}
void overfunc()
{
if (data_stack_ptr < 2) {
printf("over -- stack underflow!\n");
return;
}
push(data_stack[data_stack_ptr - 2]);
}
void pickfunc()
{
if (data_stack_ptr < 1) {
printf("pick -- stack underflow!\n");
return;
}
DCLANG_PTR pick_idx = (DCLANG_PTR) dclang_pop();
if (data_stack_ptr < (pick_idx + 1)) {
printf("pick -- stack not deep enough!\n");
return;
}
push(data_stack[data_stack_ptr - (pick_idx + 1)]);
}
void swapfunc()
{
if (data_stack_ptr < 2) {
printf("swap -- stack underflow!\n");
return;
}
DCLANG_FLT val1 = dclang_pop();
DCLANG_FLT val2 = dclang_pop();
push_no_check(val1);
push_no_check(val2);
}
void drop2func()
{
if (data_stack_ptr < 2) {
printf("2drop -- stack underflow!\n");
return;
}
--data_stack_ptr;
--data_stack_ptr;
}
void dup2func()
{
if (data_stack_ptr < 2) {
printf("2dup -- stack underflow!\n");
return;
}
DCLANG_FLT val2 = data_stack[data_stack_ptr - 1];
push(data_stack[data_stack_ptr - 2]);
push(data_stack[data_stack_ptr - 2]);
}
void over2func()
{
if (data_stack_ptr < 4) {
printf("2over -- stack underflow!\n");
return;
}
push(data_stack[data_stack_ptr - 4]);
push(data_stack[data_stack_ptr - 4]);
}
void depthfunc()
{
DCLANG_PTR size = data_stack_ptr;
push((DCLANG_FLT)size);
}
void clearfunc()
{
// clears the stack!
data_stack_ptr = 0;
}
/////////////////////
// save data stack //
/////////////////////
void svpushfunc()
{
if (save_data_stack_ptr >= DATA_STACK_SIZE) {
printf("svpush -- stack overflow!\n");
save_data_stack_ptr = 0;
}
DCLANG_FLT val = dclang_pop();
data_stack[save_data_stack_ptr++ + DATA_STACK_SIZE] = val;
}
void svpopfunc()
{
if (save_data_stack_ptr <= 0) {
printf("svdrop -- stack underflow!\n");
save_data_stack_ptr = 0;
return;
}
DCLANG_FLT val = data_stack[--save_data_stack_ptr + DATA_STACK_SIZE];
push_no_check(val);
}
void svdropfunc()
{
if (save_data_stack_ptr <= 0) {
printf("svdrop -- stack underflow!\n");
return;
}
--save_data_stack_ptr;
}
void svpickfunc()
{
if (save_data_stack_ptr <= 0) {
printf("svpick -- stack underflow!\n");
return;
}
DCLANG_PTR svpick_idx = (DCLANG_PTR) dclang_pop();
if (save_data_stack_ptr < (svpick_idx + 1)) {
printf("svpick -- stack not deep enough!\n");
return;
}
push(data_stack[(save_data_stack_ptr - (svpick_idx + 1)) + DATA_STACK_SIZE]);
}
void svdepthfunc()
{
DCLANG_PTR size = save_data_stack_ptr;
push((DCLANG_FLT)size);
}
void svclearfunc()
{
// clears the stack!
save_data_stack_ptr = 0;
}
////////////
/* output */
////////////
void showfunc()
{
if (data_stack_ptr < 1) {
printf(". (pop) -- stack underflow! ");
return;
}
fprintf(ofp, "%0.19g ", dclang_pop());
fflush(ofp);
}
void shownospacefunc()
{
if (data_stack_ptr < 1) {
printf("stack underflow! ");
return;
}
fprintf(ofp, "%0.19g", dclang_pop());
fflush(ofp);
}
void showstackfunc()
{
DCLANG_LONG x;
char *joiner;
x = data_stack_ptr > 16 ? data_stack_ptr - 16 : 0;
joiner = x == 0 ? " " : " ... ";
fprintf(ofp, "data_stack: <%lu>%s", data_stack_ptr, joiner);
fflush(ofp);
for (x=0; x < data_stack_ptr; x++) {
fprintf(ofp, "%0.19g ", data_stack[x]);
fflush(ofp);
}
fprintf(ofp, "\n");
// do the save data stack as well:
DCLANG_LONG y;
char *sv_joiner;
y = save_data_stack_ptr > 16 ? save_data_stack_ptr - 16 : 0;
sv_joiner = y == 0 ? " " : " ... ";
fprintf(ofp, "save_stack: <%lu>%s", save_data_stack_ptr, sv_joiner);
fflush(ofp);
for (y=0; y < save_data_stack_ptr; y++) {
fprintf(ofp, "%0.19g ", data_stack[y + DATA_STACK_SIZE]);
fflush(ofp);
}
fprintf(ofp, "\n");
}
void showrjfunc()
{
if (data_stack_ptr < 3) {
printf("Stack underflow! '.rj' needs: value, width, precision on the stack\n");
return;
}
// right-justified for pretty printing!
int precision = (DCLANG_LONG) dclang_pop();
int width = (DCLANG_LONG) dclang_pop();
fprintf(ofp, "%*.*f ", width, precision, dclang_pop());
fflush(ofp);
}
void showpzfunc()
{
// left pad with zeros
if (data_stack_ptr < 3) {
printf("Stack underflow! '.pz' needs: value, width, precision on the stack\n");
return;
}
int precision = (DCLANG_LONG) dclang_pop();
int width = (DCLANG_LONG) dclang_pop();
fprintf(ofp, "%0*.*f ", width, precision, dclang_pop());
fflush(ofp);
}