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
|
#include "cccl.h"
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
static struct cccl_Variables globals = {0};
static struct cccl_Function functions[52] = {0};
static struct cccl_Stack stack = {0};
static void expand_stack(void)
{
if (!stack.buffer)
{
stack.allocated = sizeof(*stack.buffer) * stack.length;
stack.buffer = malloc(stack.allocated);
} else if (stack.length > stack.allocated / sizeof(*stack.buffer))
{
stack.allocated = sizeof(*stack.buffer) * stack.length;
stack.buffer = realloc(stack.buffer, stack.allocated);
}
assert(stack.buffer && "buy more ram");
}
static size_t geti(char name)
{
return islower(name) ? name - 'a' : name - 'A' + 26;
}
static short *get_variable(char name, struct cccl_Variables *scope)
{
size_t i = geti(name);
if (scope->used[i])
return &scope->buffer[i];
if (globals.used[i])
return &globals.buffer[i];
return NULL;
}
enum cccl_ExecutorStatus cccl_execute(struct cccl_Node *code, struct cccl_Variables *scope)
{
switch (code->type)
{
case cccl_Node_CODE:
{
int res;
for (size_t i = 0; i < code->in_length; ++i)
if ((res = cccl_execute(code->in[i], scope)) != 0)
goto code_end;
code_end:
if (res == cccl_Executor_ERROR)
return res;
} break;
case cccl_Node_PUSHZERO:
{
++stack.length;
expand_stack();
stack.buffer[stack.length - 1] = 0;
} break;
case cccl_Node_INCREMENT:
{
assert(stack.length >= 1);
++stack.buffer[stack.length - 1];
} break;
case cccl_Node_DECREMENT:
{
assert(stack.length >= 1);
--stack.buffer[stack.length - 1];
} break;
case cccl_Node_ADD:
{
--stack.length;
stack.buffer[stack.length - 1] += stack.buffer[stack.length];
} break;
case cccl_Node_SUBTRACT:
{
--stack.length;
stack.buffer[stack.length - 1] -= stack.buffer[stack.length];
} break;
case cccl_Node_REVERSE:
{
size_t torev;
if (code->value == '_')
{
torev = stack.length;
} else
{
short *p = get_variable(code->value, scope);
if (!p)
errx(1, "Variable %c doesn't exist!", code->value);
if (*p > stack.length || *p < 0)
errx(1, "Cannot reverse %d elements in a stack of length %lu", *p, stack.length);
torev = *p;
}
short *buffer = stack.buffer + (stack.length - torev);
for (size_t i = 0; i < torev / 2; ++i)
{
buffer[i] ^= buffer[torev - i - 1];
buffer[torev - i - 1] ^= buffer[i];
buffer[i] ^= buffer[torev - i - 1];
}
} break;
case cccl_Node_ASSIGN:
{
assert(stack.length >= 1);
--stack.length;
if (code->value == '_')
break;
size_t i = geti(code->value);
short *p = get_variable(code->value, scope);
if (!p)
{
globals.used[i] = 1;
p = &globals.buffer[i];
}
*p = stack.buffer[stack.length];
} break;
case cccl_Node_DELETE:
{
if (code->value == '_')
errx(1, "_ is not allowed with '%c'", '!');
size_t i = geti(code->value);
if (scope->used[i])
scope->used[i] = 0;
else if (!globals.used[i])
errx(1, "Cannot delete non-existent variable %c", code->value);
else
globals.used[i] = 0;
} break;
case cccl_Node_PUSHVAR:
{
++stack.length;
expand_stack();
if (code->value == '_')
errx(1, "_ is not allowed with '%c'", '$');
short *p = get_variable(code->value, scope);
if (!p)
errx(1, "Cannot push %c on the stack is it doesn't exist", code->value);
stack.buffer[stack.length - 1] = *p;
} break;
case cccl_Node_ASSIGNLOCAL:
{
if (code->value == '_')
errx(1, "_ is not allowed with '%c'", '&');
size_t i = geti(code->value);
scope->used[i] = 1;
scope->buffer[i] = 0;
} break;
case cccl_Node_OUTPUT:
{
if (code->value == '_')
errx(1, "_ is not allowed with '%c'", '<');
short *p = get_variable(code->value, scope);
if (!p)
errx(1, "Cannot print non-existent variable %c", code->value);
putchar(*p);
} break;
case cccl_Node_INPUT:
{
if (code->value == '_')
errx(1, "_ is not allowed with '%c'", '>');
short *p = get_variable(code->value, scope);
if (!p)
errx(1, "Cannot save input in a non-existent variable %c", code->value);
int c = getchar();
if (c < 0)
err(1, "getchar()");
*p = c;
} break;
case cccl_Node_PROCEDURE:
{
functions[geti(code->value)] = (struct cccl_Function)
{
.body = code->in,
.length = code->in_length,
};
} break;
case cccl_Node_CALL:
{
if (functions[geti(code->value)].body == NULL)
errx(1, "Cannot call non-existent function %c", code->value);
size_t i = geti(code->value);
struct cccl_Variables localscope = {0};
int res;
for (size_t j = 0; j < functions[i].length; ++j)
if ((res = cccl_execute(functions[i].body[j], &localscope)) != 0)
goto call_end;
call_end:
if (res == cccl_Executor_ERROR)
return res;
} break;
case cccl_Node_INFINITE:
{
int res;
if (code->value == '_')
for (;;)
for (size_t i = 0; i < code->in_length; ++i)
if ((res = cccl_execute(code->in[i], scope)) != 0)
goto infinite_end;
short *p = get_variable(code->value, scope);
if (!p)
errx(1, "Cannot loop using non-existent variable %c", code->value);
while (*p > 0)
for (size_t i = 0; i < code->in_length; ++i)
if ((res = cccl_execute(code->in[i], scope)) != 0)
goto infinite_end;
infinite_end:
if (res == cccl_Executor_ERROR)
return res;
} break;
case cccl_Node_REPEAT:
{
short *p = get_variable(code->value, scope);
if (!p)
errx(1, "Cannot loop using non-existent variable %c", code->value);
else if (*p < 0)
errx(1, "Cannot iterate %c=%d times", code->value, *p);
int res;
for (size_t i = 0; i < *p; ++i)
for (size_t j = 0; j < code->in_length; ++j)
if ((res = cccl_execute(code->in[j], scope)) != 0)
goto repeat_end;
repeat_end:
if (res == cccl_Executor_ERROR)
return res;
};
case cccl_Node_CONDITIONAL:
{
} break;
}
return 0;
}
|