forked from akjmicro/dclang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_ops.c
172 lines (154 loc) · 3.37 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
/* 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();
save_data_stack[save_data_stack_ptr++] = val;
}
void svpopfunc()
{
DCLANG_FLT val = save_data_stack[--save_data_stack_ptr];
push_no_check(val);
}
void svdropfunc()
{
if (save_data_stack_ptr < 1) {
printf("svdrop -- stack underflow!\n");
return;
}
--save_data_stack_ptr;
}
void svpickfunc()
{
if (save_data_stack_ptr < 1) {
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(save_data_stack[save_data_stack_ptr - (svpick_idx + 1)]);
}
void svdepthfunc()
{
DCLANG_PTR size = save_data_stack_ptr;
push((DCLANG_FLT)size);
}
void svclearfunc()
{
// clears the stack!
save_data_stack_ptr = 0;
}