-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
264 lines (234 loc) · 5.37 KB
/
stack.h
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
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define TData double
#define BASESIZE 5
#define MULTBASE 2
#define DUMPSPEC "%x" //Example: "%d" for int, "%c" for char
#pragma region Define checks
#ifndef TData
#define TData int
#endif
#ifndef DUMPSPEC
#define DUMPSPEC "%x"
#endif
#ifdef BASESIZE
#if !(BASESIZE>0 && BASESIZE<=INT_MAX)
#define BASESIZE 1
#endif
#endif
#ifndef BASESIZE
#define BASESIZE 1
#endif
#ifdef MULTBASE
#if !(MULTBASE>1 && MULTBASE<=SHRT_MAX)
#define MULTBASE 2
#endif
#endif
#ifndef MULTBASE
#define MULTBASE 2
#endif
#pragma endregion Checks defines validity
typedef enum {
ERROR_OK = 0,
ERROR_UNKNOWN, //Unknown error
ERROR_INVALID_SIZE, //Size != BASESIZE * 2^(n-1)
ERROR_OVERFLOW, //Stack size not within (0 - SIZE) range
ERROR_EMPTY, //Popping empty stack
ERROR_NULL_DATA_POINTER, //Data == NULL
ERROR_NULL_STACK_POINTER //NULL stack pointer
} TError;
enum {
ERROR_CPU_OK = 0,
ERROR_null_cpu_p = 1, // null pointer
ERROR_null_cmds_p = 2, // no comands
ERROR_inv_cpu_data_stack = 3, // problems with stack
ERROR_cpu_null_data_p = 4
};
typedef struct TStack
{
int count;
int size;
TData* data;
} TStack;
/*Stack CPU*/
typedef struct
{
double* commands_to_execute;
TStack * cpu_data;
double a_reg;
double b_reg;
double c_reg;
} Stack_cpu;
TError stack_push(TStack* const stack, const TData value);
TError stack_pop(TStack* const stack, TData* const var);
TError stack_ok(const TStack* const stack);
TError stack_construct(TStack** const stack);
TError stack_destruct(TStack** const stack);
void stack_dump(const TStack* const stack);
void stack_error(const TStack* const stack, const TError error);
TError stack_pop(TStack* const stack, TData* const var)
{
int i = 0;
stack_error(stack, stack_ok(stack));
if (stack->size == 0)
{
stack_error(stack, ERROR_EMPTY);
}
*var = stack->data[0];
for (i = 0; i < stack->count - 1; i++)
{
stack->data[i] = stack->data[i + 1];
}
stack->count--;
if ((stack->count) >= BASESIZE)
{
if ((stack->size / stack->count == MULTBASE) && (stack->size % stack->count == 0))
{
stack->size = stack->size / MULTBASE;
stack->data = (TData*)realloc(stack->data, sizeof(TData)*(stack->size));
}
}
return ERROR_OK;
}
TError stack_push(TStack* const stack, const TData value)
{
stack_error(stack, stack_ok(stack));
if (stack->size == stack->count)
{
stack->size = (stack->size) * MULTBASE;
stack->data = (TData*)realloc(stack->data, sizeof(TData)*(stack->size));
}
if (stack->data != NULL)
{
stack->data[stack->count++] = value;
}
stack_error(stack, stack_ok(stack));
return ERROR_OK;
}
TError stack_destruct(TStack** const stack)
{
stack_error(*stack, stack_ok(*stack));
free((*stack)->data);
free((*stack));
*stack = NULL;
return ERROR_OK;
}
TError stack_construct(TStack** const stack)
{
*stack = (TStack*)malloc(sizeof(TStack));
if (*stack != NULL)
{
(*stack)->count = 0;
(*stack)->size = BASESIZE;
(*stack)->data = (TData*)malloc(sizeof(TData)*BASESIZE);
}
stack_error((*stack), stack_ok(*stack));
return ERROR_OK;
}
void stack_error(const TStack* const stack, const TError error)
{
switch (error)
{
case ERROR_OK: return; break;
case ERROR_UNKNOWN: printf("\nERROR: Unknown error\n"); break;
case ERROR_INVALID_SIZE: printf("\nERROR: Size != BASESIZE * 2^(n-1)\n"); break;
case ERROR_OVERFLOW: printf("\nERROR: Stack size not within (0 - SIZE) range\n"); break;
case ERROR_NULL_DATA_POINTER: printf("\nERROR: Data == NULL\n"); break;
case ERROR_EMPTY: printf("\nERROR: Popping empty stack\n"); break;
case ERROR_NULL_STACK_POINTER: printf("\nERROR: NULL stack pointer\n"); assert(0); break;
default: printf("\nERROR: Unknown error\n"); break;
};
stack_dump(stack);
assert(0);
}
TError stack_ok(const TStack* stack)
{
if (stack == NULL)
{
return ERROR_NULL_STACK_POINTER;
}
else
{
if (stack->size < BASESIZE)
{
return ERROR_INVALID_SIZE;
}
else
{
if (stack->size % BASESIZE == 0)
{
int i;
for (i = (stack->size / BASESIZE); (i % 2 == 0); i = i / 2)
{
}
if (i == 1)
{
if ((stack->count < 0) || (stack->count > stack->size))
{
return ERROR_OVERFLOW;
}
else
{
if (stack->data == NULL)
{
return ERROR_NULL_DATA_POINTER;
}
else
{
return ERROR_OK;
}
}
}
else
{
return ERROR_INVALID_SIZE;
}
}
else
{
return ERROR_INVALID_SIZE;
}
}
}
return ERROR_UNKNOWN;
}
void stack_dump(const TStack* const stack)
{
int i = 0;
printf("------STACK DUMP-------\nSize: %d\nCount: %d\nData pointer: %p\n", stack->size, stack->count, stack->data);
for (i = 0; i < stack->size; i++)
{
printf("%10i: "DUMPSPEC"\n", i, stack->data[i]);
}
printf("------END OF DUMP------\n");
}
int Stack_cpu_constructor(Stack_cpu* cpu_p);
int Stack_cpu_destruct(Stack_cpu* cpu_p);
int Stack_cpu_read_comands(Stack_cpu* cpu_p, FILE* cmds_file);
void Comands(Stack_cpu* cpu_p);
int Stack_cpu_ok(Stack_cpu* cpu_p);
int Stack_cpu_dump(Stack_cpu* cpu_p);
void Stack_cpu_check(Stack_cpu* cpu);
int Stack_cpu_err_num = 0;
enum {
PUSH = 1,
ADD = 2,
MUL = 3,
SUB = 4,
DIV = 5,
END = 6,
JMP = 7,
JA = 8,
JAE = 9,
JB = 10,
JBE = 11,
JE = 12,
JNE = 13,
PUSHA = 15,
PUSHB = 16,
PUSHC = 17,
POPA = 18,
POPB = 19,
POPC = 20
};