bitwise
kav.h
1#pragma once
2
3#include "util.h"
4
5enum tokenkind {
6#define TOKEN(t, s) t,
7#include "tokens.h"
8#undef TOKEN
9};
10
11struct location {
12 usize line, col;
13 const char *file;
14};
15
16struct token {
17 enum tokenkind kind;
18 struct location loc;
19 const char *str;
20 usize length;
21};
22
23struct lexer {
24 const char *cur;
25 const char *mar;
26 const char *tok;
27 const char *linestart;
28 const char *file;
29 usize line;
30 int afteruse;
31};
32
33void lex(struct lexer *, struct token *);
34const char *tokstr(enum tokenkind);
35const char *tokwhat(enum tokenkind);
36
37struct decl *parse(const char *, const char *);
38
39struct scope;
40
41struct module {
42 struct module *next;
43 struct module *nextseen;
44 struct scope *scope;
45 const char *path;
46 const char *canonical;
47 stralloc src;
48 struct decl *decls;
49 const char *name; /* name from the use path */
50 const char *qname;
51 int sys; /* got here from <> */
52 int state;
53};
54
55struct module *loadall(const char *);
56void resolve(struct module *);
57void check(struct module *);
58void linear(struct module *);
59int run(struct decl *);
60void qbe(struct module *);
61void compile(struct module *, const char *);
62noreturn void erroratv(const struct location *, const char *const *, usize);
63char *tokdup(struct token);
64i64 tonum(struct token);
65int tokeqb(struct token, const char *, usize);
66#define tokis(t, lit) tokeqb((t), (lit), sizeof(lit) - 1)
67#define tokeq(a, b) tokeqb((a), (b).str, (b).length)
68
69/* if we're going to have variatic arity we're going to do it properly. */
70#define errorat(loc, ...) erroratv(loc, STRS(__VA_ARGS__))
71
72enum exprkind {
73#define EXPR(e) e,
74#include "exprs.h"
75#undef EXPR
76};
77
78enum bindkind {
79 BFN,
80 BCTOR,
81 BTYPE,
82 BSORT,
83 BPARAM,
84 BVAL,
85 BPAT,
86};
87
88struct operand;
89
90struct binding {
91 struct decl *decl;
92 struct binding *next;
93 enum bindkind kind;
94 struct type *type;
95 struct token name;
96 struct ctor *ctor; /*constructors only */
97 struct param *param;
98 struct operand *qval; /* qbe */
99};
100
101#define BUILTIN(sp, kind, str, var) extern struct binding *var;
102#include "builtins.h"
103#undef BUILTIN
104
105struct val {
106 struct val *next;
107 struct pat *pat;
108 struct expr *ann; /* the type annotation */
109 struct expr *init;
110};
111
112enum typekind {
113 YNAME,
114 YVAR,
115 YTUPLE,
116 YFN,
117 YMETA,
118};
119
120struct type {
121 struct type *next;
122 enum typekind kind;
123 struct binding *binding;
124 struct type *args; /* type args, tuple elements, or a function's params */
125 struct type *ret; /* functions only */
126 struct type *link; /* metas only: the type it was solved to or nil if blank */
127 int borrow;
128};
129
130struct param;
131
132struct type *mktype(enum typekind, struct binding *);
133struct type *mkfntype(struct type *, struct type *);
134struct type *tydup(struct type *);
135char *typestr(struct type *);
136struct type *mkmeta(void);
137struct type *deref(struct type *);
138int isint(struct type *);
139i64 wrap(struct type *, i64);
140int isu64(struct type *);
141u64 intmax(struct type *);
142struct type *subst(struct type *, struct param *, struct type *);
143int unify(struct type *, struct type *);
144
145
146struct expr {
147 struct expr *next;
148 enum exprkind kind;
149 struct token tok; /* the token it came from */
150 struct expr *args; /* the first argument for calls only */
151 struct arm *arm; /* the first arm, only gaurds use it */
152 struct val *vals; /* the first val, only blocks use it*/
153 struct expr *body; /* the final expression, the blocks value */
154 struct param *sparams; /* sort params for existentials */
155 struct token ns; /* namespace */
156 struct binding *binding;
157 struct type *type;
158 struct expr *scrut;
159};
160
161enum patkind {
162 PWILD,
163 PNAME,
164 PCON,
165 PTUPLE,
166 PNUM,
167};
168
169struct pat {
170 struct pat *next;
171 enum patkind kind;
172 struct token name;
173 int tilde;
174 struct pat *args;
175 struct token ns;
176 struct binding *binding;
177 struct type *type;
178 struct operand *qval; /* qbe: the value this pattern matched */
179};
180
181struct arm {
182 struct arm *next;
183 struct location loc;
184 struct expr *cond;
185 struct expr *body;
186 struct pat *pat;
187};
188
189struct param {
190 struct param *next;
191 struct token name;
192 struct expr *ann;
193 struct expr *cond;
194 struct binding *binding;
195};
196
197enum declkind {
198 DFN,
199 DSTRUCT,
200 DDATA,
201 DTYPEDEF,
202 DSORTDEF,
203 DUSE,
204};
205
206struct ctor {
207 struct ctor *next;
208 struct token name;
209 struct param *sparams;
210 struct expr *args;
211 struct expr *ret;
212 struct binding *binding;
213};
214
215struct decl {
216 struct decl *next;
217 enum declkind kind;
218 struct token name;
219 const char *qname; /* name for qbe */
220 int pub;
221 int linear;
222 int abstract;
223 int ext;
224 int newtype;
225 int expanding; /* catching self-refrential typedef */
226 struct param *sparams;
227 struct param *params;
228 struct expr *ret;
229 struct expr *body;
230 struct ctor *ctors;
231 struct token alias;
232 struct binding *binding;
233 struct module *mod;
234};
235
236struct expr *mkexpr(enum exprkind, struct token);
237struct expr *mkbinary(struct token, struct expr *, struct expr *);
238struct arm *mkarm(struct location);
239struct val *mkval(struct pat *);
240struct param *mkparam(struct token);
241struct ctor *mkctor(struct token);
242struct decl *mkdecl(enum declkind, struct token);
243struct pat *mkpat(enum patkind, struct token);
244struct binding *mkbinding(enum bindkind, struct token);
245void declput(struct decl *);
246int islinear(struct type *);
247int isnamed(struct type *, struct binding *);