-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter_d.d
348 lines (308 loc) · 7.48 KB
/
interpreter_d.d
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Copyright (C) 2017-2023 Mateus de Lima Oliveira */
import list_d;
enum TMPL_TRUE = 1;
enum TMPL_FALSE = 0;
enum ControlFlow {
NEXT_INSTRUCTION,
JMP_FOWARD,
JMP_BACKWARD
};
struct NodeStart {
string el;
string[] attr;
size_t num_attributes;
bool jmp;
};
struct NodeEnd {
string el;
bool jmp;
bool conditional_jmp;
};
struct NodeCharacterData {
string data;
};
enum NodeType {
NODE_START,
NODE_END,
NODE_CHARACTER_DATA
};
union NodeData {
NodeStart start;
NodeEnd end;
NodeCharacterData character_data;
};
union Node {
NodeType type;
NodeData data;
};
alias NodeList = List!Node;
enum InputType {
INPUT_FILLER_TEXT,
INPUT_CONTROL_FLOW
};
struct Input {
InputType type;
union InputData {
string filler_text;
bool control_flow;
};
InputData data;
};
alias InputList = List!Input;
struct Context {
NodeList nodes;
InputList input;
};
@trusted
static void putchar(const(int) c)
{
import stdio = core.stdc.stdio : putchar;
stdio.putchar(c);
}
@live @trusted
static void puts(scope const(string) s)
{
int c = 0;
for (size_t i = 0; s[i]; i++) {
c = s[i];
if (c == '\0')
break;
putchar(c);
}
}
@live @safe
static int strcmp(scope string a, scope string b)
{
char c1, c2;
for (int i = 0; i < a.length && i < b.length; i++) {
c1 = a[i]; c2 = b[i];
if (c1 == '\0' || c2 == '\0')
break;
if (c1 == c2)
continue;
if (c1 < c2)
return -1;
else
return 1;
}
return 0;
}
@live @safe
static void print_html_escaped(scope string s)
{
char c = '\0';
for (int i = 0; s[i]; i++) {
c = s[i];
switch (c) {
case '\"': puts("""); break; //quotation mark
case '\'': puts("'"); break; //apostrophe
case '&': puts("&"); break; //ampersand
case '<': puts("<"); break; //less-than
case '>': puts(">"); break; //greater-than
default:
putchar(c);
}
}
}
@trusted
string getFillerText(scope return const(Input) *p)
{
assert(p.type == InputType.INPUT_FILLER_TEXT);
return p.data.filler_text;
}
@live @safe
static void dump_string(scope Context *data)
{
if (data.input.isEmpty())
return;
scope const(Input) *p = data.input.getFirst();
//fputs(p->data.filler_text, stdout);
scope string s = p.getFillerText();
print_html_escaped(s);
data.input.removeHead();
}
// NOTE: not all keywords here are related to control flow anymore.
@live @safe
static bool is_control_flow_keyword(scope string el)
{
if (strcmp("if", el) == 0
|| strcmp("swhile", el) == 0
|| strcmp("ewhile", el) == 0)
return true;
return false;
}
@live @safe
static void print_start_node(scope Context *data, scope NodeStart *n)
{
int i;
if (strcmp("templatizer", n.el) == 0)
return;
if (is_control_flow_keyword(n.el))
return;
putchar('<');
puts(n.el);
if (n.attr) {
for (i = 0; i < n.num_attributes; i += 2) {
if (strcmp("@", n.attr[i + 1]) == 0) {
putchar(' ');
puts(n.attr[i]);
putchar('=');
putchar('\"');
dump_string(data);
putchar('\"');
} else {
putchar(' ');
puts(n.attr[i]);
putchar('=');
putchar('\"');
puts(n.attr[i + 1]);
putchar('\"');
}
}
}
putchar('>');
}
@live @safe
static void print_end_node(scope const(NodeEnd) *n)
{
if (strcmp("templatizer", n.el) == 0)
return;
if (is_control_flow_keyword(n.el))
return;
putchar('<');
putchar('/');
puts(n.el);
putchar('>');
}
@live @safe
static void print_character_data_node(scope Context *data, scope NodeCharacterData *n)
{
scope string s = n.data;
bool just_print = false;
char c = '\0';
for (int i = 0; i < s.length; i++) {
c = s[i];
if (c == '\0')
break;
if (just_print) {
putchar(c);
continue;
}
if (c == '\\')
just_print = true;
else if (c == '@')
dump_string(data);
else
putchar(c);
}
}
@trusted
static void printError(string s)
{
import core.stdc.stdio : fputc, stderr;
foreach(char c; s) {
if (c == '\0')
break;
fputc(c, stderr);
}
}
@trusted void exit(int errorCode)
{
import stdlib = core.stdc.stdlib : exit;
stdlib.exit(errorCode);
}
@trusted NodeStart *getNodeStart(scope return Node *n)
{
assert(n.type == NodeType.NODE_START);
return &n.data.start;
}
@trusted NodeEnd *getNodeEnd(scope return Node *n)
{
assert(n.type == NodeType.NODE_END);
return &n.data.end;
}
@trusted NodeCharacterData *getNodeCharacterData(scope return Node *n)
{
assert(n.type == NodeType.NODE_CHARACTER_DATA);
return &n.data.character_data;
}
@live @safe
static ControlFlow print_node(scope Context *data, scope Node *n)
{
ControlFlow r;
switch (n.type) {
case NodeType.NODE_START:
scope NodeStart *startNode = getNodeStart(n);
if (startNode.jmp) {
if (data.input.isEmpty()) {
printError("missing input for tag that requires input\n");
exit(1);
}
scope const(Input) *p = data.input.getFirst();
if (p.data.control_flow == TMPL_TRUE)
r = ControlFlow.NEXT_INSTRUCTION;
else
r = ControlFlow.JMP_FOWARD;
data.input.removeHead();
return r;
}
scope NodeStart *nodeStart = getNodeStart(n);
print_start_node(data, nodeStart);
break;
case NodeType.NODE_END:
scope NodeEnd *endNode = getNodeEnd(n);
if (endNode.jmp) {
if (endNode.conditional_jmp == false)
return ControlFlow.JMP_BACKWARD;
if (data.input.isEmpty()) {
printError("missing input for tag that requires input\n");
exit(1);
}
scope const(Input) *p = data.input.getFirst();
if (p.data.control_flow == TMPL_TRUE)
r = ControlFlow.JMP_BACKWARD;
else
r = ControlFlow.NEXT_INSTRUCTION;
data.input.removeHead();
return r;
}
//NodeEnd endNode = getNodeEnd(n);
print_end_node(endNode);
break;
case NodeType.NODE_CHARACTER_DATA:
scope NodeCharacterData *characterDataNode = getNodeCharacterData(n);
print_character_data_node(data, characterDataNode);
break;
default:
printError("Error: unknown node type.\n");
assert(0);
break;
}
return ControlFlow.NEXT_INSTRUCTION;
}
@trusted
void flush()
{
import core.stdc.stdio : fflush, stdout;
fflush(stdout);
}
@safe
void print_list(scope Context *data)
{
auto p = data.nodes.createCursor();
while (p.isValid()) {
scope Node *n = p.get();
switch (print_node(data, n)) {
case ControlFlow.JMP_FOWARD:
//p = TAILQ_NEXT(p->data.start.jmp, entries);
break;
case ControlFlow.JMP_BACKWARD:
//p = p.data.end.jmp;
break;
default:
p.next();
break;
}
flush();
}
}