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
|
#include "interpreter.h"
#include "program.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void loop_end(Program* program) {
uint32_t count = 1;
Symbol s;
while (true) {
program_next(program, &s);
if (s == StartLoop) {
count++;
} else if (s == EndLoop) {
count--;
} else if (s == Eof) {
return;
}
if (count == 0) return;
}
}
static void loop_start(Program* program) {
uint32_t count = 1;
Symbol s;
program_last(program, &s);
while (true) {
program_last(program, &s);
if (s == StartLoop) {
count--;
} else if (s == EndLoop) {
count++;
} else if (s == Eof) {
return;
}
if (count == 0) {
return;
};
}
}
static void recurse_program(Program* program, Tape* tape) {
Symbol s;
next:
program_next(program, &s);
// printf("[");
// for(uint32_t i = 0; i < tape->len; i++) {
// printf("0x%02x ", tape->data[i]);
// }
// printf("\b]\n ");
// for(uint32_t i = 0; i < tape->len; i++) {
// if (i == tape->index) {
// printf(" ^ ");
// } else {
// printf(" ");
// }
// }
// printf(" \ + tape->indexn");
switch (s) {
case MoveLeft:
tape_left(tape);
break;
case MoveRight:
tape_right(tape);
break;
case Increment:
tape_set(tape, tape_get(tape) + 1);
break;
case Decrement:
tape_set(tape, tape_get(tape) - 1);
break;
case StartLoop:
if (tape_get(tape) == 0)
loop_end(program);
break;
case EndLoop:
if (tape_get(tape) != 0)
loop_start(program);
break;
case PutChar: {
printf("%c", tape_get(tape));
break;
}
case GetChar: {
uint8_t c = (uint8_t) getchar();
tape_set(tape, c);
break;
}
case Allocate: {
uint8_t len = tape_get(tape);
Tape* new = malloc(sizeof(Tape));
tape_init(len, new);
void* ptr = (void*) tape_ptr(tape);
memcpy(ptr, &new, sizeof(Tape*));
break;
}
case Free: {
Tape* old;
void* ptr = (void*) tape_ptr(tape);
memcpy(&old, ptr, sizeof(Tape*));
memset(ptr, 0, sizeof(Tape*));
tape_free(tape);
free(old);
break;
}
case EnterTape: {
Tape* cur;
void* ptr = (void*) tape_ptr(tape);
memcpy(&cur, ptr, sizeof(Tape*));
recurse_program(program, cur);
break;
}
case GetString: {
uint8_t len = tape_get(tape);
void* ptr = (void*) tape_ptr(tape);
fgets(ptr, len, stdin);
break;
}
case PutString: {
char* ptr = (char*) tape_ptr(tape);
printf("%s", ptr);
break;
}
case Clear:
printf("\033c");
break;
case LeaveTape:
case Eof:
case Invalid:
return;
}
goto next;
}
void run_program(Program* program) {
Tape tape;
tape_init(sizeof(void*), &tape);
recurse_program(program, &tape);
tape_free(&tape);
}
|