/
ast.h
544 lines (477 loc) · 17.9 KB
/
ast.h
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#pragma once
#include <iostream>
#include <vector>
#include "constraint.h"
#include "identifier.h"
#include "import_rules.h"
#include "infer.h"
#include "match.h"
#include "patterns.h"
#include "scheme_resolver.h"
#include "tarjan.h"
#include "token.h"
#include "types.h"
namespace zion {
namespace ast {
std::string fresh();
struct Expr {
virtual ~Expr() throw() {
}
virtual Location get_location() const = 0;
virtual std::ostream &render(std::ostream &os,
int parent_precedence) const = 0;
std::string str() const;
};
struct StaticPrint : public Expr {
StaticPrint(Location location, const Expr *expr)
: location(location), expr(expr) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Location location;
const Expr *expr;
};
struct Var : public Expr {
Var(Identifier id) : id(id) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Identifier id;
};
struct PatternBlock {
PatternBlock(const Predicate *predicate, const Expr *result)
: predicate(predicate), result(result) {
}
std::ostream &render(std::ostream &os) const;
const Predicate *predicate;
const Expr *result;
};
typedef std::vector<const PatternBlock *> PatternBlocks;
struct Match : public Expr {
Match(const Expr *scrutinee, PatternBlocks pattern_blocks)
: scrutinee(scrutinee), pattern_blocks(pattern_blocks) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *scrutinee;
const PatternBlocks pattern_blocks;
};
struct Predicate {
virtual ~Predicate() {
}
virtual std::ostream &render(std::ostream &os) const = 0;
virtual match::Pattern::ref get_pattern(
types::Ref type,
const DataCtorsMap &data_ctors_map) const = 0;
virtual types::Ref tracking_infer(
const DataCtorsMap &data_ctors_map,
const types::Ref &return_type,
// Predicates can implicitly bind symbols, hence the following mutable
// SchemeResolver.
types::SchemeResolver &scheme_resolver,
TrackedTypes &tracked_types,
types::Constraints &constraints,
types::ClassPredicates &instance_requirements) const = 0;
virtual Location get_location() const = 0;
virtual Identifier instantiate_name_assignment() const = 0;
virtual void get_bound_vars(
std::unordered_set<std::string> &bound_vars) const = 0;
virtual const Expr *translate(
const types::DefnId &defn_id,
const Identifier &scrutinee_id,
const types::Ref &scrutinee_type,
bool do_checks,
const DataCtorsMap &data_ctors_map,
const std::unordered_set<std::string> &bound_vars,
const TrackedTypes &tracked_types,
const types::TypeEnv &type_env,
TrackedTypes &typing,
types::NeededDefns &needed_defns,
bool &returns,
TranslateContinuationFn &matched,
TranslateContinuationFn &failed) const = 0;
virtual const Predicate *rewrite(
const RewriteImportRules &rewrite_import_rules) const = 0;
std::string str() const;
};
struct TuplePredicate : public Predicate {
TuplePredicate(Location location,
std::vector<const Predicate *> params,
maybe<Identifier> name_assignment)
: location(location), params(params), name_assignment(name_assignment) {
}
std::ostream &render(std::ostream &os) const override;
match::Pattern::ref get_pattern(
types::Ref type,
const DataCtorsMap &data_ctors_map) const override;
types::Ref tracking_infer(
const DataCtorsMap &data_ctors_map,
const types::Ref &return_type,
types::SchemeResolver &scheme_resolver,
TrackedTypes &tracked_types,
types::Constraints &constraints,
types::ClassPredicates &instance_requirements) const override;
Identifier instantiate_name_assignment() const override;
void get_bound_vars(
std::unordered_set<std::string> &bound_vars) const override;
const Expr *translate(const types::DefnId &defn_id,
const Identifier &scrutinee_id,
const types::Ref &scrutinee_type,
bool do_checks,
const DataCtorsMap &data_ctors_map,
const std::unordered_set<std::string> &bound_vars,
const TrackedTypes &tracked_types,
const types::TypeEnv &type_env,
TrackedTypes &typing,
types::NeededDefns &needed_defns,
bool &returns,
TranslateContinuationFn &matched,
TranslateContinuationFn &failed) const override;
const Predicate *rewrite(
const RewriteImportRules &rewrite_import_rules) const override;
Location get_location() const override;
Location location;
std::vector<const Predicate *> params;
maybe<Identifier> name_assignment;
};
struct IrrefutablePredicate : public Predicate {
IrrefutablePredicate(Location location, maybe<Identifier> name_assignment)
: location(location), name_assignment(name_assignment) {
}
std::ostream &render(std::ostream &os) const override;
match::Pattern::ref get_pattern(
types::Ref type,
const DataCtorsMap &data_ctors_map) const override;
types::Ref tracking_infer(
const DataCtorsMap &data_ctors_map,
const types::Ref &return_type,
types::SchemeResolver &scheme_resolver,
TrackedTypes &tracked_types,
types::Constraints &constraints,
types::ClassPredicates &instance_requirements) const override;
Identifier instantiate_name_assignment() const override;
void get_bound_vars(
std::unordered_set<std::string> &bound_vars) const override;
const Expr *translate(const types::DefnId &defn_id,
const Identifier &scrutinee_id,
const types::Ref &scrutinee_type,
bool do_checks,
const DataCtorsMap &data_ctors_map,
const std::unordered_set<std::string> &bound_vars,
const TrackedTypes &tracked_types,
const types::TypeEnv &type_env,
TrackedTypes &typing,
types::NeededDefns &needed_defns,
bool &returns,
TranslateContinuationFn &matched,
TranslateContinuationFn &failed) const override;
const Predicate *rewrite(
const RewriteImportRules &rewrite_import_rules) const override;
Location get_location() const override;
Location location;
maybe<Identifier> name_assignment;
};
struct CtorPredicate : public Predicate {
CtorPredicate(Location location,
std::vector<const Predicate *> params,
Identifier ctor_name,
maybe<Identifier> name_assignment)
: location(location), params(params), ctor_name(ctor_name),
name_assignment(name_assignment) {
}
std::ostream &render(std::ostream &os) const override;
match::Pattern::ref get_pattern(
types::Ref type,
const DataCtorsMap &data_ctors_map) const override;
types::Ref tracking_infer(
const DataCtorsMap &data_ctors_map,
const types::Ref &return_type,
types::SchemeResolver &scheme_resolver,
TrackedTypes &tracked_types,
types::Constraints &constraints,
types::ClassPredicates &instance_requirements) const override;
Identifier instantiate_name_assignment() const override;
void get_bound_vars(
std::unordered_set<std::string> &bound_vars) const override;
const Expr *translate(const types::DefnId &defn_id,
const Identifier &scrutinee_id,
const types::Ref &scrutinee_type,
bool do_checks,
const DataCtorsMap &data_ctors_map,
const std::unordered_set<std::string> &bound_vars,
const TrackedTypes &tracked_types,
const types::TypeEnv &type_env,
TrackedTypes &typing,
types::NeededDefns &needed_defns,
bool &returns,
TranslateContinuationFn &matched,
TranslateContinuationFn &failed) const override;
const Predicate *rewrite(
const RewriteImportRules &rewrite_import_rules) const override;
Location get_location() const override;
Location location;
std::vector<const Predicate *> params;
Identifier ctor_name;
maybe<Identifier> name_assignment;
};
struct Block : public Expr {
Block(std::vector<const Expr *> statements) : statements(statements) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
std::vector<const Expr *> const statements;
};
struct As : public Expr {
As(const Expr *expr, types::Ref type, bool force_cast)
: expr(expr), type(type), force_cast(force_cast) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *expr;
types::Ref type;
bool force_cast;
};
struct Sizeof : public Expr {
Sizeof(Location location, types::Ref type) : type(type) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Location location;
types::Ref type;
};
struct Application : public Expr {
Application(const Expr *a, const std::vector<const Expr *> ¶ms)
: a(a), params(params) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *a;
std::vector<const Expr *> params;
};
struct Lambda : public Expr {
Lambda(Identifiers vars,
types::Refs param_types,
types::Ref return_type,
const Expr *body)
: vars(vars), body(body), param_types(param_types),
return_type(return_type) {
assert(vars.size() != 0);
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Identifiers vars;
const Expr *body;
types::Refs param_types;
types::Ref return_type;
};
struct Let : public Expr {
Let(Identifier var, const Expr *value, const Expr *body)
: var(var), value(value), body(body) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Identifier var;
const Expr *value;
const Expr *body;
};
struct Tuple : public Expr {
Tuple(Location location, std::vector<const Expr *> dims)
: location(location), dims(dims) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Location const location;
std::vector<const Expr *> const dims;
};
struct TupleDeref : public Expr {
TupleDeref(const Expr *expr, int index, int max)
: expr(expr), index(index), max(max) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *expr;
int index, max;
};
struct Builtin : public Expr {
Builtin(const Var *var, std::vector<const Expr *> exprs)
: var(var), exprs(exprs) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Var *var;
std::vector<const Expr *> exprs;
};
struct Literal : public Expr, public Predicate {
Literal(Token token) : token(token) {
// Strings are currently passed in as quoted.
assert_implies(token.tk == tk_string, token.text[0] == '\"');
}
std::ostream &render(std::ostream &os, int parent_precedence) const override;
std::ostream &render(std::ostream &os) const override;
match::Pattern::ref get_pattern(
types::Ref type,
const DataCtorsMap &data_ctors_map) const override;
types::Ref tracking_infer(
const DataCtorsMap &data_ctors_map,
const types::Ref &return_type,
types::SchemeResolver &scheme_resolver,
TrackedTypes &tracked_types,
types::Constraints &constraints,
types::ClassPredicates &instance_requirements) const override;
types::Ref non_tracking_infer() const;
Identifier instantiate_name_assignment() const override;
void get_bound_vars(
std::unordered_set<std::string> &bound_vars) const override;
const Expr *translate(const types::DefnId &defn_id,
const Identifier &scrutinee_id,
const types::Ref &scrutinee_type,
bool do_checks,
const DataCtorsMap &data_ctors_map,
const std::unordered_set<std::string> &bound_vars,
const TrackedTypes &tracked_types,
const types::TypeEnv &type_env,
TrackedTypes &typing,
types::NeededDefns &needed_defns,
bool &returns,
TranslateContinuationFn &matched,
TranslateContinuationFn &failed) const override;
const Predicate *rewrite(const RewriteImportRules &) const override;
Location get_location() const override;
Token token;
};
struct Conditional : public Expr {
Conditional(const Expr *cond, const Expr *truthy, const Expr *falsey)
: cond(cond), truthy(truthy), falsey(falsey) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *cond;
const Expr *truthy;
const Expr *falsey;
};
struct ReturnStatement : public Expr {
ReturnStatement(const Expr *value) : value(value) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *value;
};
struct Continue : public Expr {
Continue(Location location) : location(location) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Location location;
};
struct Break : public Expr {
Break(Location location) : location(location) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
Location location;
};
struct Defer : public Expr {
Defer(const Application *application) : application(application) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Application *application;
};
struct While : public Expr {
While(const Expr *condition, const Expr *block)
: condition(condition), block(block) {
}
Location get_location() const override;
std::ostream &render(std::ostream &os, int parent_precedence) const override;
const Expr *condition;
const Expr *block;
};
struct Decl {
Decl(Identifier id, const Expr *value) : id(id), value(value) {
assert(id.name.find("0x7") == std::string::npos);
}
std::string str() const;
Location get_location() const;
types::SchemeRef get_early_scheme() const;
Identifier id;
const Expr *const value;
};
struct TypeDecl {
TypeDecl(Identifier id, const Identifiers ¶ms) : id(id), params(params) {
}
Identifier id;
Identifiers params;
types::Ref get_type() const;
int kind() const {
return params.size() + 1;
}
};
struct TypeClass {
TypeClass(Identifier id,
const Identifiers &type_var_ids,
const types::ClassPredicates &class_predicates,
const types::Map &overloads);
Location get_location() const;
std::string str() const;
Identifier id;
Identifiers type_var_ids;
types::ClassPredicates class_predicates;
types::Map overloads;
};
struct Instance {
Instance(const types::ClassPredicateRef &class_predicate,
const std::vector<const Decl *> &decls)
: class_predicate(class_predicate), decls(decls) {
}
std::string str() const;
Location get_location() const;
types::ClassPredicateRef const class_predicate;
std::vector<const Decl *> const decls;
};
struct Module {
Module(std::string name,
const std::vector<const Identifier> &imports,
const std::vector<const Decl *> &decls,
const std::vector<const TypeDecl *> &type_decls,
const std::vector<const TypeClass *> &type_classes,
const std::vector<const Instance *> &instances,
const ParsedCtorIdMap &ctor_id_map,
const ParsedDataCtorsMap &data_ctors_map,
const types::TypeEnv &type_env)
: name(name), imports(imports), decls(decls), type_decls(type_decls),
type_classes(type_classes), instances(instances),
ctor_id_map(ctor_id_map), data_ctors_map(data_ctors_map),
type_env(type_env) {
}
std::string const name;
std::vector<const Identifier> imports;
std::vector<const Decl *> decls;
std::vector<const TypeDecl *> type_decls;
std::vector<const TypeClass *> type_classes;
std::vector<const Instance *> instances;
ParsedCtorIdMap ctor_id_map;
ParsedDataCtorsMap data_ctors_map;
types::TypeEnv const type_env;
};
struct Program {
Program(const std::vector<const Decl *> &decls,
const std::vector<const TypeClass *> &type_classes,
const std::vector<const Instance *> &instances,
const Expr *expr)
: decls(decls), type_classes(type_classes), instances(instances),
expr(expr) {
}
std::vector<const Decl *> decls;
std::vector<const TypeClass *> type_classes;
std::vector<const Instance *> instances;
const Expr *const expr;
};
} // namespace ast
ast::Expr *unit_expr(Location location);
tarjan::Vertices get_free_vars(
const ast::Expr *expr,
const std::unordered_set<std::string> &bound_vars);
} // namespace zion
std::ostream &operator<<(std::ostream &os, zion::ast::Program *program);
std::ostream &operator<<(std::ostream &os, zion::ast::Decl *decl);
std::ostream &operator<<(std::ostream &os, zion::ast::Expr *expr);