First batch of changes to turn Whitespace interpreter into VVhitespace interpreter...
[vvhitespace] / vv_interpreter.c
CommitLineData
1adfc4f4
AT
1/*
2 * (c) 2019 Aaron Taylor <ataylor at subgeniuskitty dot com>
3 * All rights reserved.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <string.h>
10#include <errno.h>
11#include <stdint.h>
12#include <sys/select.h>
13#include <getopt.h>
14
15#define VERSION 1
16
17#define STACKSIZE 1024
18#define HEAPSIZE 1024
19#define RETURNSTACKSIZE 1024
20
21void
22print_usage(char ** argv)
23{
bf43fa3f 24 printf( "VVhitespace Interpreter v%d (www.subgeniuskitty.com)\n"
1adfc4f4
AT
25 "Usage: %s -i <file>\n"
26 " -h Help (prints this message)\n"
bf43fa3f 27 " -i <file> Specify a VVhitespace source file to interpret.\n"
1adfc4f4
AT
28 , VERSION, argv[0]
29 );
30}
31
32int
33stdin_empty(void)
34{
35 fd_set read_fds;
36 FD_ZERO(&read_fds);
37 FD_SET(STDIN_FILENO, &read_fds);
38
39 struct timeval timeout;
40 timeout.tv_sec = 0;
41 timeout.tv_usec = 0;
42
43 int retval = select(1, &read_fds, NULL, NULL, &timeout);
44 /* retval could be -1. Ignoring that for now. */
45 if (retval > 0) return 0;
46 return 1;
47}
48
49void
50ws_die(size_t * pc, char * msg)
51{
52 printf("\n");
53 printf("SIM_ERROR @ PC %lu: %s\n", *pc, msg);
54 fflush(stdout);
55 exit(EXIT_FAILURE);
56}
57
58void
59stack_push(int32_t ** sp, int32_t word)
60{
61 *((*sp)++) = word;
62}
63
64int32_t
65stack_pop(int32_t ** sp)
66{
67 return *(--(*sp));
68}
69
70int32_t
71stack_peek(int32_t ** sp, size_t offset)
72/* offset=0 peeks TOS, offset=1 peeks NOS, etc. */
73{
74 return *((*sp)-offset-1);
75}
76
77uint8_t
78next_code_byte(uint8_t * code, size_t * pc)
79{
80 return code[(*pc)++];
81}
82
83uint16_t
84parse_label(uint8_t * code, size_t * pc)
85{
bf43fa3f 86 // TODO: Check for invalid label and die.
1adfc4f4
AT
87 uint16_t label = 0;
88 uint8_t c;
89 while ((c = code[(*pc)++]) != '\n') {
90 label = label << 1;
91 if (c == ' ') label++;
92 }
93 return label;
94}
95
bf43fa3f
AT
96void
97populate_labels(uint32_t * labels, uint8_t * code, size_t code_size)
98{
99 // TODO
100}
101
1adfc4f4
AT
102void
103process_imp_stack(uint8_t * code, size_t * pc, int32_t ** sp)
104{
105 switch (next_code_byte(code,pc)) {
106 case ' ':
107 /* Push number onto TOS. */
108 {
109 /* First, pick off the sign */
110 int32_t sign = 0;
111 switch (next_code_byte(code,pc)) {
112 case ' ' : sign = 1; break;
113 case '\t': sign = -1; break;
bf43fa3f 114 default : ws_die(pc, "expected sign"); break;
1adfc4f4
AT
115 }
116
117 /* Now, construct the number and push to TOS. */
118 /* I'm assuming the numbers are read MSb first. */
119 int32_t temp, number = 0;
120 while ((temp = next_code_byte(code,pc)) != '\n') {
bf43fa3f 121 if (temp == '\v') ws_die(pc, "non-binary digit in number");
1adfc4f4
AT
122 number <<= 1;
123 if (temp == '\t') number++;
124 }
125 stack_push(sp, number*sign);
126 }
127 break;
128 case '\n':
129 /* Stack sub-command */
130 {
131 switch (next_code_byte(code,pc)) {
132 /* Duplicate the TOS. */
133 case ' ':
134 stack_push(sp, stack_peek(sp,0));
135 break;
136 /* Swap TOS and NOS. */
137 case '\t':
138 {
139 int32_t t1 = stack_pop(sp);
140 int32_t t2 = stack_pop(sp);
141 stack_push(sp, t1);
142 stack_push(sp, t2);
143 }
144 break;
145 /* Discard TOS. */
146 case '\n':
147 stack_pop(sp);
148 break;
bf43fa3f
AT
149 default:
150 ws_die(pc, "malformed stack IMP");
151 break;
1adfc4f4
AT
152 }
153 }
154 break;
bf43fa3f 155 default: ws_die(pc, "malformed stack IMP"); break;
1adfc4f4
AT
156 }
157}
158
159void
160process_imp_arithmetic(uint8_t * code, size_t * pc, int32_t ** sp)
161{
162 int32_t temp;
163 switch (next_code_byte(code,pc)) {
164 case ' ':
165 {
166 switch (next_code_byte(code,pc)) {
167 case ' ':
168 /* Addition */
169 stack_push(sp, stack_pop(sp)+stack_pop(sp));
170 break;
171 case '\t':
172 /* Subtraction */
173 temp = stack_pop(sp);
174 stack_push(sp, stack_pop(sp)-temp);
175 break;
176 case '\n':
177 /* Multiplication */
178 stack_push(sp, stack_pop(sp)*stack_pop(sp));
179 break;
bf43fa3f
AT
180 default:
181 ws_die(pc, "malformed arithmetic IMP");
182 break;
1adfc4f4
AT
183 }
184 }
185 break;
186 case '\t':
187 {
188 switch (next_code_byte(code,pc)) {
189 case ' ':
190 /* Division */
191 temp = stack_pop(sp);
192 stack_push(sp, stack_pop(sp)/temp);
193 break;
194 case '\t':
195 /* Modulo */
196 temp = stack_pop(sp);
197 stack_push(sp, stack_pop(sp)%temp);
198 break;
bf43fa3f 199 default: ws_die(pc, "malformed arithmetic IMP"); break;
1adfc4f4
AT
200 }
201 }
202 break;
bf43fa3f 203 default: ws_die(pc, "malformed arithmetic IMP"); break;
1adfc4f4
AT
204 }
205}
206
207void
208process_imp_flowcontrol(uint8_t * code, size_t * pc, int32_t ** sp, uint32_t * labels,
209 uint32_t ** rsp)
210{
211 switch (next_code_byte(code,pc)) {
212 case '\n':
213 /* Technically another LF is required but we ignore it. */
214 printf("\n");
215 fflush(stdout);
216 exit(EXIT_SUCCESS);
217 case ' ':
218 {
219 switch (next_code_byte(code,pc)) {
220 case ' ':
221 /* Mark a location in the program. */
bf43fa3f 222 // TODO: Verify this is a label, but do nothing else.
1adfc4f4
AT
223 labels[parse_label(code, pc)] = *pc;
224 break;
225 case '\t':
226 /* Call a subroutine. */
227 *((*rsp)++) = *pc;
228 *pc = labels[parse_label(code, pc)];
229 break;
230 case '\n':
231 /* Jump unconditionally to a label. */
232 *pc = labels[parse_label(code, pc)];
233 break;
bf43fa3f
AT
234 default:
235 ws_die(pc, "malformed flow control IMP");
236 break;
1adfc4f4
AT
237 }
238 }
239 break;
240 case '\t':
241 {
242 switch (next_code_byte(code,pc)) {
243 case ' ':
244 /* Jump to a label if TOS == 0 */
245 if (stack_peek(sp,0) == 0) *pc = labels[parse_label(code, pc)];
246 break;
247 case '\t':
248 /* Jump to a label if TOS < 0. */
249 if (stack_peek(sp,0) < 0) *pc = labels[parse_label(code, pc)];
250 break;
251 case '\n':
252 /* Return from subroutine. */
253 *pc = *(--(*rsp));
254 break;
bf43fa3f
AT
255 default:
256 ws_die(pc, "malformed flow control IMP");
257 break;
1adfc4f4
AT
258 }
259 }
260 break;
bf43fa3f
AT
261 default:
262 ws_die(pc, "malformed flow control IMP");
263 break;
1adfc4f4
AT
264 }
265}
266
267void
268process_imp_heap(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
269{
270 switch (next_code_byte(code,pc)) {
271 case ' ' : /* Store to heap */ *(*hp + *((*sp)-1)) = **sp; *sp -= 2; break;
272 case '\t': /* Retrieve from heap */ **sp = *(*hp + **sp); break;
bf43fa3f 273 default: ws_die(pc, "malformed heap IMP"); break;
1adfc4f4
AT
274 }
275}
276
277void
278process_imp_io(uint8_t * code, size_t * pc, int32_t ** sp, int32_t ** hp)
279{
280 switch (next_code_byte(code,pc)) {
281 case ' ':
282 /* Output */
283 {
284 switch (next_code_byte(code,pc)) {
285 case ' ' : /* Output character from TOS */ printf("%c", stack_pop(sp)); break;
286 case '\t': /* Output number from TOS */ printf("%d", stack_pop(sp)); break;
bf43fa3f 287 default: ws_die(pc, "malformed output IMP"); break;
1adfc4f4
AT
288 }
289 fflush(stdout);
290 }
291 break;
292 case '\t':
293 /* Input */
294 {
295 while (stdin_empty()) continue;
296 char c = getchar();
297 switch (next_code_byte(code,pc)) {
298 case '\t': /* Input digit */ c -= '0'; /* fallthrough */
299 case ' ' : /* Input character */ *(*hp + *((*sp)--)) = c; break;
bf43fa3f 300 default: ws_die(pc, "malformed input IMP"); break;
1adfc4f4
AT
301 }
302 }
303 break;
bf43fa3f 304 default: ws_die(pc, "malformed i/o IMP"); break;
1adfc4f4
AT
305 }
306}
307
1adfc4f4
AT
308int
309main(int argc, char ** argv)
310{
311 /*
312 * Process command line arguments
313 */
314 int c;
315 FILE * input = NULL;
316 while ((c = getopt(argc,argv,"i:h")) != -1) {
317 switch (c) {
318 case 'i':
319 if ((input = fopen(optarg, "r")) == NULL) {
320 fprintf(stderr, "ERROR: %s: %s\n", optarg, strerror(errno));
321 }
322 break;
323 case 'h':
324 print_usage(argv);
325 exit(EXIT_SUCCESS);
326 break;
327 default:
328 break;
329 }
330 }
331 if (input == NULL) {
bf43fa3f 332 fprintf(stderr, "ERROR: Must specify a VVhitespace source file with -f flag.\n");
1adfc4f4
AT
333 print_usage(argv);
334 exit(EXIT_FAILURE);
335 }
336
337 /*
bf43fa3f 338 * Read just the VVhitespace source code into memory.
1adfc4f4
AT
339 * We will use the array indices as addresses for the virtual PC when jumping to labels.
340 */
341 size_t ws_code_size = 0;
342 uint8_t temp_byte;
343 while (fread(&temp_byte, 1, 1, input)) {
bf43fa3f
AT
344 if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') {
345 ws_code_size++;
346 }
1adfc4f4
AT
347 }
348 rewind(input);
349 uint8_t * ws_code_space = malloc(ws_code_size);
350 ws_code_size = 0;
351 while (fread(&temp_byte, 1, 1, input)) {
bf43fa3f
AT
352 if (temp_byte == ' ' || temp_byte == '\t' || temp_byte == '\n' || temp_byte == '\v') {
353 ws_code_space[ws_code_size++] = temp_byte;
354 }
1adfc4f4
AT
355 }
356 fclose(input);
357
358 /*
359 * Setup a stack and heap.
360 * Assume a 32-bit word size.
361 */
bf43fa3f 362 // TODO: Make everything 64-bit.
1adfc4f4
AT
363 int32_t * hp = malloc(HEAPSIZE*4);
364 int32_t * sp = malloc(STACKSIZE*4);
365
366 /*
367 * Setup the return stack and the label array.
368 */
369 uint32_t * rsp = malloc(RETURNSTACKSIZE*4);
bf43fa3f
AT
370 uint32_t labels[65536] = {0};
371 populate_labels(labels, ws_code_space, ws_code_size);
1adfc4f4
AT
372
373 /*
374 * Main Loop
375 */
376
377 size_t pc = 0; /* Virtual program counter. Operates in the ws_code_space[] address space. */
378 while (1) {
379 if (pc >= ws_code_size) {
bf43fa3f
AT
380 fprintf(stderr, "SIM_ERROR: PC Overrun\n Requested PC: %lu\n Max Address: %lu\n",
381 pc, ws_code_size-1);
1adfc4f4
AT
382 exit(EXIT_FAILURE);
383 }
384
385 /* Decode the IMPs */
386 switch (ws_code_space[pc++]) {
387 case ' ':
388 /* Stack Manipulation */
389 process_imp_stack(ws_code_space, &pc, &sp);
390 break;
391 case '\n':
392 /* Flow Control */
393 process_imp_flowcontrol(ws_code_space, &pc, &sp, labels, &rsp);
394 break;
395 case '\t':
396 /* Arithmetic, Heap Access, or I/O */
397 {
398 switch (ws_code_space[pc++]) {
399 case ' ':
400 /* Arithmetic */
401 process_imp_arithmetic(ws_code_space, &pc, &sp);
402 break;
403 case '\t':
404 /* Heap Access */
405 process_imp_heap(ws_code_space, &pc, &sp, &hp);
406 break;
407 case '\n':
408 /* I/O */
409 process_imp_io(ws_code_space, &pc, &sp, &hp);
410 break;
411 }
412 }
413 break;
bf43fa3f 414 default: ws_die(pc, "unexpected VTab"); break;
1adfc4f4
AT
415 }
416 }
417
418 printf("\n");
419 printf("Program executed.\n");
420
421 exit(EXIT_SUCCESS);
422}