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
|
// Some basic operations defined on AST nodes, mainly converting to
// strings for debugging.
#include <gc/cord.h>
#include <stdarg.h>
#include <printf.h>
#include "ast.h"
#include "builtins/text.h"
static const char *OP_NAMES[] = {
[BINOP_UNKNOWN]="unknown",
[BINOP_POWER]="^", [BINOP_MULT]="*", [BINOP_DIVIDE]="/",
[BINOP_MOD]="mod", [BINOP_MOD1]="mod1", [BINOP_PLUS]="+", [BINOP_MINUS]="minus",
[BINOP_CONCAT]="++", [BINOP_LSHIFT]="<<", [BINOP_RSHIFT]=">>", [BINOP_MIN]="min",
[BINOP_MAX]="max", [BINOP_EQ]="==", [BINOP_NE]="!=", [BINOP_LT]="<",
[BINOP_LE]="<=", [BINOP_GT]=">", [BINOP_GE]=">=", [BINOP_CMP]="<>",
[BINOP_AND]="and", [BINOP_OR]="or", [BINOP_XOR]="xor",
};
const char *binop_method_names[BINOP_XOR+1] = {
[BINOP_POWER]="__power", [BINOP_MULT]="__multiply", [BINOP_DIVIDE]="__divide",
[BINOP_MOD]="__mod", [BINOP_MOD1]="__mod1", [BINOP_PLUS]="__add", [BINOP_MINUS]="__subtract",
[BINOP_CONCAT]="__concatenate", [BINOP_LSHIFT]="__left_shift", [BINOP_RSHIFT]="__right_shift",
[BINOP_AND]="__and", [BINOP_OR]="__or", [BINOP_XOR]="__xor",
};
static CORD ast_list_to_xml(ast_list_t *asts);
static CORD arg_list_to_xml(arg_ast_t *args);
static CORD when_clauses_to_xml(when_clause_t *clauses);
static CORD tags_to_xml(tag_ast_t *tags);
static CORD xml_escape(CORD text);
static CORD optional_tagged(const char *tag, ast_t *ast);
static CORD optional_tagged_type(const char *tag, type_ast_t *ast);
CORD xml_escape(CORD text)
{
text = Text$replace(text, "&", "&", INT64_MAX);
text = Text$replace(text, "<", "<", INT64_MAX);
text = Text$replace(text, ">", ">", INT64_MAX);
return text;
}
CORD ast_list_to_xml(ast_list_t *asts)
{
CORD c = CORD_EMPTY;
for (; asts; asts = asts->next) {
c = CORD_cat(c, ast_to_xml(asts->ast));
}
return c;
}
CORD arg_list_to_xml(arg_ast_t *args) {
CORD c = "<args>";
for (; args; args = args->next) {
CORD arg_cord = args->name ? CORD_all("<arg name=\"", args->name, "\">") : "<arg>";
if (args->type)
arg_cord = CORD_all(arg_cord, "<type>", type_ast_to_xml(args->type), "</type>");
if (args->value)
arg_cord = CORD_all(arg_cord, "<value>", ast_to_xml(args->value), "</value>");
c = CORD_all(c, arg_cord, "</arg>");
}
return CORD_cat(c, "</args>");
}
CORD when_clauses_to_xml(when_clause_t *clauses) {
CORD c = CORD_EMPTY;
for (; clauses; clauses = clauses->next) {
c = CORD_all(c, "<case tag=\"", ast_to_xml(clauses->tag_name), "\">", ast_list_to_xml(clauses->args), ast_to_xml(clauses->body), "</case>");
}
return c;
}
CORD tags_to_xml(tag_ast_t *tags) {
CORD c = CORD_EMPTY;
for (; tags; tags = tags->next) {
c = CORD_all(c, "<tag name=\"", tags->name, "\" value=\"", CORD_asprintf("%ld", tags->value), "\">", arg_list_to_xml(tags->fields), "</tag>");
}
return c;
}
CORD optional_tagged(const char *tag, ast_t *ast)
{
return ast ? CORD_all("<", tag, ">", ast_to_xml(ast), "</", tag, ">") : CORD_EMPTY;
}
CORD optional_tagged_type(const char *tag, type_ast_t *ast)
{
return ast ? CORD_all("<", tag, ">", type_ast_to_xml(ast), "</", tag, ">") : CORD_EMPTY;
}
CORD ast_to_xml(ast_t *ast)
{
if (!ast) return CORD_EMPTY;
switch (ast->tag) {
#define T(type, ...) case type: { auto data = ast->__data.type; (void)data; return CORD_asprintf(__VA_ARGS__); }
T(Unknown, "<Unknown>")
T(Nil, "<Nil>%r</Nil>", type_ast_to_xml(data.type))
T(Bool, "<Bool value=\"%s\" />", data.b ? "yes" : "no")
T(Var, "<Var>%s</Var>", data.name)
T(Int, "<Int bits=\"%ld\">%ld</Int>", data.bits, data.i)
T(Num, "<Num bits=\"%ld\">%g</Num>", data.bits, data.n)
T(TextLiteral, "%r", xml_escape(data.cord))
T(TextJoin, "<Text%r>%r</Text>", data.lang ? CORD_all(" lang=\"", data.lang, "\"") : CORD_EMPTY, ast_list_to_xml(data.children))
T(Declare, "<Declare var=\"%r\">%r</Declare>", ast_to_xml(data.var), ast_to_xml(data.value))
T(Assign, "<Assign><targets>%r</targets><values>%r</values></Assign>", ast_list_to_xml(data.targets), ast_list_to_xml(data.values))
T(BinaryOp, "<BinaryOp op=\"%r\">%r %r</BinaryOp>", xml_escape(OP_NAMES[data.op]), ast_to_xml(data.lhs), ast_to_xml(data.rhs))
T(UpdateAssign, "<UpdateAssign op=\"%r\">%r %r</UpdateAssign>", xml_escape(OP_NAMES[data.op]), ast_to_xml(data.lhs), ast_to_xml(data.rhs))
T(Length, "<Length>%r</Length>", ast_to_xml(data.value))
T(Negative, "<Negative>%r</Negative>", ast_to_xml(data.value))
T(Not, "<Not>%r</Not>", ast_to_xml(data.value))
T(HeapAllocate, "<HeapAllocate>%r</HeapAllocate>", ast_to_xml(data.value))
T(StackReference, "<StackReference>%r</StackReference>", ast_to_xml(data.value))
T(Min, "<Min>%r%r%r</Min>", ast_to_xml(data.lhs), ast_to_xml(data.rhs), optional_tagged("key", data.key))
T(Max, "<Max>%r%r%r</Max>", ast_to_xml(data.lhs), ast_to_xml(data.rhs), optional_tagged("key", data.key))
T(Array, "<Array>%r%r</Array>", optional_tagged_type("item-type", data.type), ast_list_to_xml(data.items))
T(Set, "<Set>%r%r</Set>",
optional_tagged_type("item-type", data.item_type),
ast_list_to_xml(data.items))
T(Table, "<Table>%r%r%r%r</Table>",
optional_tagged_type("key-type", data.key_type), optional_tagged_type("value-type", data.value_type),
ast_list_to_xml(data.entries), optional_tagged("fallback", data.fallback))
T(TableEntry, "<TableEntry>%r%r</TableEntry>", ast_to_xml(data.key), ast_to_xml(data.value))
T(Comprehension, "<Comprehension>%r%r%r%r%r</Comprehension>", optional_tagged("expr", data.expr),
ast_list_to_xml(data.vars), optional_tagged("iter", data.iter),
optional_tagged("filter", data.filter))
T(FunctionDef, "<FunctionDef name=\"%r\">%r%r<body>%r</body></FunctionDef>", ast_to_xml(data.name),
arg_list_to_xml(data.args), optional_tagged_type("return-type", data.ret_type), ast_to_xml(data.body))
T(Lambda, "<Lambda>%r<body>%r</body></Lambda>)", arg_list_to_xml(data.args), ast_to_xml(data.body))
T(FunctionCall, "<FunctionCall><function>%r</function>%r</FunctionCall>", ast_to_xml(data.fn), arg_list_to_xml(data.args))
T(MethodCall, "<MethodCall><self>%r</self><method>%s</method>%r</MethodCall>", ast_to_xml(data.self), data.name, arg_list_to_xml(data.args))
T(Block, "<Block>%r</Block>", ast_list_to_xml(data.statements))
T(For, "<For>%r%r%r%r%r</For>", ast_list_to_xml(data.vars), optional_tagged("iterable", data.iter),
optional_tagged("body", data.body), optional_tagged("empty", data.empty))
T(While, "<While>%r%r</While>", optional_tagged("condition", data.condition), optional_tagged("body", data.body))
T(If, "<If>%r%r%r</If>", optional_tagged("condition", data.condition), optional_tagged("body", data.body), optional_tagged("else", data.else_body))
T(When, "<When><subject>%r</subject>%r%r</When>", ast_to_xml(data.subject), when_clauses_to_xml(data.clauses), optional_tagged("else", data.else_body))
T(Reduction, "<Reduction>%r%r%r</Reduction>", optional_tagged("iterable", data.iter),
optional_tagged("combination", data.combination), optional_tagged("fallback", data.fallback))
T(Skip, "<Skip>%r</Skip>", data.target)
T(Stop, "<Stop>%r</Stop>", data.target)
T(PrintStatement, "<PrintStatement>%r</PrintStatement>", ast_list_to_xml(data.to_print))
T(Pass, "<Pass/>")
T(Defer, "<Defer>%r<Defer/>", ast_to_xml(data.body))
T(Return, "<Return>%r</Return>", ast_to_xml(data.value))
T(Extern, "<Extern name=\"%s\">%r</Extern>", data.name, type_ast_to_xml(data.type))
T(StructDef, "<StructDef name=\"%s\">%r<namespace>%r</namespace></StructDef>", data.name, arg_list_to_xml(data.fields), ast_to_xml(data.namespace))
T(EnumDef, "<EnumDef name=\"%s\"><tags>%r</tags><namespace>%r</namespace></EnumDef>", data.name, tags_to_xml(data.tags), ast_to_xml(data.namespace))
T(LangDef, "<LangDef name=\"%s\">%r</LangDef>", data.name, ast_to_xml(data.namespace))
T(Index, "<Index>%r%r</Index>", optional_tagged("indexed", data.indexed), optional_tagged("index", data.index))
T(FieldAccess, "<FieldAccess field=\"%s\">%r</FieldAccess>", data.field, ast_to_xml(data.fielded))
T(Optional, "<Optional>%r</Optional>", ast_to_xml(data.value))
T(DocTest, "<DocTest>%r<output>%r</output></DocTest>", optional_tagged("expression", data.expr), xml_escape(data.output))
T(Import, "<Import>%r</Import>", xml_escape(data.path))
T(Use, "<Use>%r</Use>", xml_escape(data.name))
T(LinkerDirective, "<LinkerDirective>%r</LinkerDirective>", xml_escape(data.directive))
T(InlineCCode, "<InlineCode>%r</InlineCode>", xml_escape(data.code))
#undef T
}
return "???";
}
CORD type_ast_to_xml(type_ast_t *t)
{
if (!t) return "NULL";
switch (t->tag) {
#define T(type, ...) case type: { auto data = t->__data.type; (void)data; return CORD_asprintf(__VA_ARGS__); }
T(UnknownTypeAST, "<UnknownType/>")
T(VarTypeAST, "%s", data.name)
T(PointerTypeAST, "<PointerType is_optional=\"%s\" is_stack=\"%s\" is_readonly=\"%s\">%r</PointerType>",
data.is_optional ? "yes" : "no", data.is_stack ? "yes" : "no", data.is_readonly ? "yes" : "no", type_ast_to_xml(data.pointed))
T(ArrayTypeAST, "<ArrayType>%r</ArrayType>", type_ast_to_xml(data.item))
T(SetTypeAST, "<TableType>%r</TableType>", type_ast_to_xml(data.item))
T(TableTypeAST, "<TableType>%r %r</TableType>", type_ast_to_xml(data.key), type_ast_to_xml(data.value))
T(FunctionTypeAST, "<FunctionType>%r %r</FunctionType>", arg_list_to_xml(data.args), type_ast_to_xml(data.ret))
#undef T
}
return NULL;
}
int printf_ast(FILE *stream, const struct printf_info *info, const void *const args[])
{
ast_t *ast = *(ast_t**)(args[0]);
if (ast) {
if (info->alt)
return fprintf(stream, "%.*s", (int)(ast->end - ast->start), ast->start);
else
return CORD_put(ast_to_xml(ast), stream);
} else {
return fputs("(null)", stream);
}
}
bool is_idempotent(ast_t *ast)
{
switch (ast->tag) {
case Int: case Bool: case Num: case Var: case Nil: case TextLiteral: return true;
case Index: {
auto index = Match(ast, Index);
return (index->index == NULL) && is_idempotent(index->indexed);
}
case FieldAccess: {
auto access = Match(ast, FieldAccess);
return is_idempotent(access->fielded);
}
default: return false;
}
}
bool type_ast_eq(type_ast_t *x, type_ast_t *y)
{
if (x->tag != y->tag) return false;
switch (x->tag) {
case UnknownTypeAST: return true;
case VarTypeAST: return streq(Match(x, VarTypeAST)->name, Match(y, VarTypeAST)->name);
case PointerTypeAST: {
auto x_info = Match(x, PointerTypeAST);
auto y_info = Match(y, PointerTypeAST);
return (x_info->is_optional == y_info->is_optional
&& x_info->is_stack == y_info->is_stack
&& x_info->is_readonly == y_info->is_readonly
&& type_ast_eq(x_info->pointed, y_info->pointed));
}
case ArrayTypeAST: return type_ast_eq(Match(x, ArrayTypeAST)->item, Match(y, ArrayTypeAST)->item);
case SetTypeAST: return type_ast_eq(Match(x, SetTypeAST)->item, Match(y, SetTypeAST)->item);
case TableTypeAST: {
auto tx = Match(x, TableTypeAST);
auto ty = Match(y, TableTypeAST);
return type_ast_eq(tx->key, ty->key) && type_ast_eq(tx->value, ty->value);
}
case FunctionTypeAST: {
auto x_fn = Match(x, FunctionTypeAST);
auto y_fn = Match(y, FunctionTypeAST);
if (!type_ast_eq(x_fn->ret, y_fn->ret))
return false;
for (arg_ast_t *x_arg = x_fn->args, *y_arg = y_fn->args; x_arg || y_arg; x_arg = x_arg->next, y_arg = y_arg->next) {
if (!x_arg || !y_arg) return false;
if (!x_arg->type)
errx(1, "I can't compare function types that don't have all explicitly typed args");
if (!y_arg->type)
errx(1, "I can't compare function types that don't have all explicitly typed args");
if (!type_ast_eq(x_arg->type, y_arg->type))
return false;
}
return true;
}
}
return true;
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0
|