-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.c
295 lines (244 loc) · 7.25 KB
/
machine.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "machine.h"
#include "tables.h"
enum SpecialParamKind get_sparam_kind(char c) {
switch (c) {
case '#':
return ARGC_NUM;
case '@':
return ARGV;
case '*':
return ENVIRON;
case '~':
return HOME;
case '/':
return PWD;
case '-':
return SHELL;
case '$':
return USER;
default:
return UNKNOWN;
}
}
struct Word *new_word(char *word, size_t length) {
if (length >= WORD_SIZE) {
fprintf(stderr,
"Error: word is larger than maximum allowed size (of %lu)\n",
WORD_SIZE);
print_location_info();
exit(EXIT_FAILURE);
}
struct Word *word = allocate_memory(MEMTAG_STACK_VAL, sizeof(struct Word));
if (strncpy(&word->container[0], word) < 0) {
perror("strncpy");
exit(EXIT_FAILURE);
}
word->length = length;
}
struct Name *new_name(char *word, size_t length) {
if (length >= NAME_SIZE) {
fprintf(stderr,
"Error: name is larger than maximum allowed size (of %lu)\n",
WORD_SIZE);
print_location_info();
exit(EXIT_FAILURE);
}
struct Name *name = allocate_memory(MEMTAG_STACK_VAL, sizeof(struct Name));
if (strncpy(&word->container[0], word) < 0) {
perror("strncpy");
exit(EXIT_FAILURE);
}
word->length = length;
}
struct StackValue *create_word_value(struct Word *word) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_WORD;
newval->word = word;
}
return newval;
}
struct StackValue *create_name_value(struct Name *name) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_NAME;
newval->name = name;
}
return newval;
}
struct StackValue *create_bytecode_chunk(BytecodeChunk chunk) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_CHUNK;
newval->chunk = chunk;
}
return newval;
}
struct StackValue *create_parameter_value(struct Name *parameter) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_PARAMETER;
newval->parameter = parameter;
}
return newval;
}
struct StackValue *
create_special_param_value(enum SpecialParamKind specialParam) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_SPECIAL_PARAMETER;
newval->special_param = specialParam;
}
return newval;
}
struct StackValue *create_opcode_value(enum Opcode opcode) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_OPCODE;
newval->opcode = opcode;
}
return newval;
}
struct StackValue *create_regex_pattern_value(RegexPattern *pattern) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_REGEX_PATTER;
newval->re_pattern = pattern;
}
return newval;
}
struct StackValue *create_pos_param_value(PosParam pos_param) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_POSITIONAL_PARAMETER;
newval->pos_param = pos_param;
}
return newval;
}
struct StackValue *create_fdesc_value(FDesc fdesc) {
struct StackValue *newval =
allocate_memory(MEMTAG_STACK_VAL, sizeof(struct StackValue));
if (newval != NULL) {
newval->kind = VALUE_FDESC;
newval->fdesc = fdesc;
}
return newval;
}
bool value_valid_word(struct StackValue *value) {
return value != NULL && value->kind == VALUE_WORD;
}
bool value_valid_name(struct StackValue *value) {
return value != NULL && value->kind == VALUE_NAME;
}
bool value_valid_opcode(struct StackValue *value) {
return value != NULL && value->kind == VALUE_OPCODE;
}
bool value_valid_pos_param(struct StackValue *value) {
return value != NULL && value->kind == VALUE_POSITIONAL_PARAMETER;
}
bool value_valid_special_param(struct StackValue *value) {
return value != NULL && value->kind == VALUE_SPECIAL_PARAMETER;
}
bool value_valid_fdesc(struct StackValue *value) {
return value != NULL && value->kind == VALUE_FDESC;
}
bool value_valid_regex_pattern(struct StackValue *value) {
return value != NULL && value->kind == VALUE_REGEX_PATTERN;
}
bool value_valid_chunk(struct StackValue *value) {
return value != NULL && value->kind = VALUE_CHUNK;
}
struct Stack *create_stack(size_t ostack_size, size_t istack_size) {
struct Stack *stack = (struct Stack *)allocate_memory(sizeof(struct Stack));
if (stack == NULL) {
perror("Error creating stack");
exit(EXIT_FAILURE);
}
stack->operand_stack = (struct StackValue *)allocate_memory(
sizeof(struct StackValue) * ostack_size);
if (stack->operand_stack == NULL) {
perror("Error creating operand stack");
exit(EXIT_FAILURE);
}
stack->inst_stack =
(enum Opcode *)allocate_memory(sizeof(enum Opcode) * istack_size);
if (stack->inst_stack == NULL) {
perror("Error creating instruction stack");
exit(EXIT_FAILURE);
}
stack->ostack_size = ostack_size;
stack->istack_size = istack_size;
stack->sp = -1;
stack->ip = -1;
return stack;
}
void destroy_stack(struct Stack *stack) {
free(stack->operand_stack);
free(stack->inst_stack);
free(stack);
}
int is_stack_empty(struct Stack *stack) { return stack->sp == -1; }
int is_stack_full(struct Stack *stack) {
return stack->sp == (int)(stack->ostack_size - 1);
}
int push_operand(struct Stack *stack, struct StackValue *value) {
if (is_stack_full(stack)) {
fprintf(stderr, "Operand stack overflow\n");
return -1;
}
stack->operand_stack[++stack->sp] = *value;
return 0;
}
int push_inst(struct Stack *stack, enum Opcode inst) {
if (is_stack_full(stack)) {
fprintf(stderr, "Instruction stack overflow\n");
return -1;
}
stack->inst_stack[++stack->ip] = inst;
return 0;
}
struct StackValue *pop_operand(struct Stack *stack) {
if (is_stack_empty(stack)) {
fprintf(stderr, "Operand stack underflow\n");
return NULL;
}
return &stack->operand_stack[stack->sp--];
}
struct StackValue *peek_operand(struct Stack *stack) {
if (is_stack_empty(stack)) {
fprintf(stderr, "Operand stack is empty\n");
return NULL;
}
return &stack->operand_stack[stack->sp];
}
enum Opcode pop_inst(struct Stack *stack) {
if (is_stack_empty(stack)) {
fprintf(stderr, "Instruction stack underflow\n");
return OPCODE_TERM; // Return a default value for now
}
return stack->inst_stack[stack->ip--];
}
enum Opcode peek_inst(struct Stack *stack) {
if (is_stack_empty(stack)) {
fprintf(stderr, "Instruction stack is empty\n");
return OPCODE_TERM; // Return a default value for now
}
return stack->inst_stack[stack->ip];
}
void set_sp(struct Stack *stack, int sp) { stack->sp = sp; }
void set_ip(struct Stack *stack, int ip) { stack->ip = ip; }
int get_sp(struct Stack *stack) { return stack->sp; }
int get_ip(struct Stack *stack) { return stack->ip; }