-
Notifications
You must be signed in to change notification settings - Fork 2
/
transpile.c
184 lines (159 loc) · 3.88 KB
/
transpile.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
#include <stdlib.h>
#include <stdio.h>
#include <cbm.h>
#include <string.h>
#include "transpile.h"
static unsigned int pc;
static unsigned int *line_start;
static unsigned int basic_line;
#define emitB(b) { *((char*)pc++) = b; }
#define emitW(w) { *(((unsigned int*)pc)++) = w; }
#define emitS(s) { strcpy((char*)pc, s); pc += strlen(s); }
#define start_line() { line_start = (unsigned int*)pc; emitW(0); emitW(basic_line++)}
#define end_line() { emitB(0); *line_start = pc; }
#define rem(s) { emitS(" : "); emitB(0x8f); emitB(' '); emitS(s); }
static void transpile_bunch (char instr, char bunch_size)
{
if (!bunch_size)
return;
switch (instr)
{
case '+':
start_line();
emitS("m%(p%)");
emitB(0xb2); // =
emitS("m%(p%)");
emitB(0xaa); // +
pc += sprintf((char*)pc, "%u", bunch_size);
emitB(0xaf); // AND
emitS("255");
end_line();
break;
case '-':
start_line();
emitS("m%(p%)");
emitB(0xb2); // =
emitS("m%(p%)");
emitB(0xab); // -
pc += sprintf((char*)pc, "%u", bunch_size);
emitB(0xaf); // AND
emitS("255");
end_line();
break;
case '>':
start_line();
emitS("p%");
emitB(0xb2); // =
emitS("p%");
emitB(0xaa); // +
pc += sprintf((char*)pc, "%u", bunch_size);
emitB(0xaf); // AND
emitS("255");
end_line();
break;
case '<':
start_line();
emitS("p%");
emitB(0xb2); // =
emitS("p%");
emitB(0xab); // -
pc += sprintf((char*)pc, "%u", bunch_size);
emitB(0xaf); // AND
emitS("255");
end_line();
break;
case '.':
// TODO: generate a BASIC FOR loop when it's worth it
for (;bunch_size; --bunch_size)
{
start_line();
emitB(0x99); // PRINT
emitB(' ');
emitB(0xc7); // CHR$
emitS("(m%(p%));");
end_line();
}
break;
}
}
static unsigned int loop_lines[20], loop_pcs[20];
static unsigned int *loop_line = loop_lines, *loop_pc = loop_pcs;
static char bunch_size = 0;
static char last_instr = 0;
void transpile_init (unsigned int prog_origin)
{
pc = prog_origin;
basic_line = 0;
emitB(0);
start_line();
emitB(0x8f); // REM
emitS(" brainfuck");
end_line();
start_line();
emitB(0x86); // DIM;
emitS(" m%(255)");
rem("memory");
end_line();
start_line();
emitS("p%");
emitB(0xb2); // =
emitS("0");
rem("pointer");
end_line();
}
unsigned int transpile_finish ()
{
transpile_bunch(last_instr, bunch_size);
start_line();
emitB(0x80); // END
end_line ();
emitW(0);
return pc;
}
void transpile (char instr)
{
switch (instr)
{
case '+':
case '-':
case '>':
case '<':
case '.':
if (last_instr == instr && bunch_size < 255)
{
++bunch_size;
}
else
{
transpile_bunch(last_instr, bunch_size);
last_instr = instr;
bunch_size = 1;
}
break;
case '[':
transpile_bunch(last_instr, bunch_size);
last_instr = 0;
bunch_size = 0;
*(loop_line++) = basic_line;
start_line();
emitB(0x8b); // IF
emitS(" m%(p%) ");
emitB(0xb2); // =
emitS(" 0 ");
emitB(0xa7); // THEN
*(loop_pc++) = pc;
emitS(" "); // will be filled in by ]
end_line();
break;
case ']':
transpile_bunch(last_instr, bunch_size);
last_instr = 0;
bunch_size = 0;
start_line();
emitB(0x89); // GOTO
pc += sprintf((char*)pc, " %u", *(--loop_line));
sprintf((char*)(*(--loop_pc)), " %5u", basic_line);
end_line();
break;
}
}