commit 7b7c19c
shrub
·
2026-11-21 17:07:03 +0000 UTC
parent 538541c
extern functions, new integer types
16 files changed,
+363,
-54
M
ast.c
+14,
-2
1@@ -223,6 +223,13 @@ exprput(struct expr *e) {
2 exprput(e->body);
3 fputs(")", stdout);
4 break;
5+ case ECAST:
6+ fputs("(as ", stdout);
7+ exprput(e->args);
8+ fputs(" ", stdout);
9+ exprput(e->body);
10+ fputs(")", stdout);
11+ break;
12 default:
13 fputs("[?]", stdout);
14 break;
15@@ -260,6 +267,8 @@ declput(struct decl *d) {
16 fputs("pub ", stdout);
17 if(d->linear)
18 fputs("linear ", stdout);
19+ if(d->ext)
20+ fputs("extern ", stdout);
21 switch(d->kind) {
22 case DFN:
23 fputs("fn ", stdout);
24@@ -270,8 +279,11 @@ declput(struct decl *d) {
25 paramsput(d->params);
26 fputs(" ", stdout);
27 exprput(d->ret);
28- fputs(" ", stdout);
29- exprput(d->body);
30+ /* extern fns have no body */
31+ if(d->body) {
32+ fputs(" ", stdout);
33+ exprput(d->body);
34+ }
35 break;
36 case DSTRUCT:
37 fputs("struct ", stdout);
+7,
-0
1@@ -1,4 +1,11 @@
2 BUILTIN(STYPE, BTYPE, "int", bint)
3+BUILTIN(STYPE, BTYPE, "i8", bi8)
4+BUILTIN(STYPE, BTYPE, "i16", bi16)
5+BUILTIN(STYPE, BTYPE, "i32", bi32)
6+BUILTIN(STYPE, BTYPE, "u8", bu8)
7+BUILTIN(STYPE, BTYPE, "u16", bu16)
8+BUILTIN(STYPE, BTYPE, "u32", bu32)
9+BUILTIN(STYPE, BTYPE, "u64", bu64)
10 BUILTIN(STYPE, BTYPE, "bool", bbool)
11 BUILTIN(STYPE, BTYPE, "one", bone)
12
M
check.c
+93,
-17
1@@ -188,6 +188,22 @@ instantiate(struct binding *b, struct location *loc) {
2 }
3
4
5+static void
6+checklit(struct token tok, struct type *t) {
7+ u64 n = 0;
8+ u64 d;
9+ usize i;
10+
11+ for(i = 0; i < tok.length; i++) {
12+ d = tok.str[i] - '0';
13+ if(n > (UINT64_MAX - d) / 10)
14+ errorat(&tok.loc, "number too big");
15+ n = n * 10 + d;
16+ }
17+ if(n > intmax(t))
18+ errorat(&tok.loc, tokdup(tok), " doesn't fit in ", typestr(t));
19+}
20+
21 static void
22 bindpat(struct pat *p, struct type *t) {
23 struct pat *a;
24@@ -204,9 +220,14 @@ bindpat(struct pat *p, struct type *t) {
25 p->binding->type = t;
26 break;
27 case PNUM:
28+ if(isint(t)) {
29+ checklit(p->name, t);
30+ break;
31+ }
32 u = mktype(YNAME, bint);
33 if(!unify(u, t))
34 errorat(&p->name.loc, "type mismatch: expected ", typestr(t), ", got ", typestr(u));
35+ checklit(p->name, u);
36 break;
37 case PTUPLE:
38 if(t->kind != YTUPLE)
39@@ -240,6 +261,7 @@ bindpat(struct pat *p, struct type *t) {
40
41 static struct type *trup(struct expr *);
42 static void trdn(struct expr *, struct type *);
43+static struct type *apply(struct expr *, struct type *);
44
45 /* type a block's vals and give the names in their patterns types */
46 static void
47@@ -310,8 +332,18 @@ trdn(struct expr *e, struct type *t) {
48 for(arm = e->arm; arm; arm = arm->next)
49 trdn(arm->body, t);
50 break;
51+ case ENUMBER:
52+ if(isint(t)) {
53+ checklit(e->tok, t);
54+ break;
55+ }
56+ /* from here the case is the same */
57 default:
58- u = trup(e);
59+ /* operators get told the type we want */
60+ if(e->kind == ECALL && e->tok.kind != TIDENT)
61+ u = e->type = apply(e, t);
62+ else
63+ u = trup(e);
64 if(!unify(u, t))
65 errorat(&e->tok.loc, "type mismatch: expected ", typestr(t), ", got ", typestr(u));
66 }
67@@ -319,26 +351,57 @@ trdn(struct expr *e, struct type *t) {
68 }
69
70
71+static int
72+iscmp(enum tokenkind k) {
73+ switch(k) {
74+ case TEQ:
75+ case TNEQ:
76+ case TLEQ:
77+ case TLESS:
78+ case TMORE:
79+ case TMEQ:
80+ return 1;
81+ default:
82+ return 0;
83+ }
84+}
85+
86 static struct type *
87-apply(struct expr *e) {
88+apply(struct expr *e, struct type *want) {
89 struct type *ft;
90 struct type *y;
91 struct expr *a;
92+ struct expr *first = nil;
93+ struct type *t;
94
95 if(e->tok.kind != TIDENT) {
96- for(a = e->args; a; a = a->next)
97- trdn(a, mktype(YNAME, bint));
98- switch(e->tok.kind) {
99- case TEQ:
100- case TNEQ:
101- case TLEQ:
102- case TLESS:
103- case TMORE:
104- case TMEQ:
105- return mktype(YNAME, bbool);
106- default:
107- return mktype(YNAME, bint);
108+ if(want && isint(want) && !iscmp(e->tok.kind)) {
109+ /* we know the type */
110+ t = want;
111+ } else {
112+ /* take the type from the first operand thats not a literal */
113+ for(a = e->args; a; a = a->next) {
114+ if(a->kind != ENUMBER) {
115+ first = a;
116+ break;
117+ }
118+ }
119+ if(first)
120+ t = trup(first);
121+ else
122+ t = mktype(YNAME, bint);
123+ if(!isint(t))
124+ if(!unify(t, mktype(YNAME, bint)))
125+ errorat(&e->tok.loc, "operator needs integers, got ", typestr(t));
126+ }
127+ for(a = e->args; a; a = a->next) {
128+ if(a == first)
129+ continue;
130+ trdn(a, t);
131 }
132+ if(iscmp(e->tok.kind))
133+ return mktype(YNAME, bbool);
134+ return t;
135 }
136
137 ft = instantiate(e->binding, &e->tok.loc);
138@@ -358,12 +421,15 @@ apply(struct expr *e) {
139 static struct type *
140 trup(struct expr *e) {
141 struct type *t;
142+ struct type *to;
143 struct type **link;
144 struct expr *a;
145
146 switch(e->kind) {
147 case ENUMBER:
148- return e->type = mktype(YNAME, bint);
149+ e->type = mktype(YNAME, bint);
150+ checklit(e->tok, e->type);
151+ return e->type;
152 case EIDENT:
153 if(!e->binding->type)
154 errorat(&e->tok.loc, "sorry, cant type this yet");
155@@ -374,7 +440,7 @@ trup(struct expr *e) {
156 vals(e);
157 return e->type = trup(e->body);
158 case ECALL:
159- return e->type = apply(e);
160+ return e->type = apply(e, nil);
161 case ETUPLE:
162 t = mktype(YTUPLE, nil);
163 link = &t->args;
164@@ -383,6 +449,15 @@ trup(struct expr *e) {
165 link = &(*link)->next;
166 }
167 return e->type = t;
168+ case ECAST:
169+ t = trup(e->args);
170+ if(!isint(t))
171+ if(!unify(t, mktype(YNAME, bint)))
172+ errorat(&e->tok.loc, "can only cast intergers, got ", typestr(t));
173+ to = totype(e->body);
174+ if(!isint(to))
175+ errorat(&e->tok.loc, "can only cast to an interger type, got ", typestr(to));
176+ return e->type = to;
177 default:
178 errorat(&e->tok.loc, "sorry, can't type this yet");
179 case EGUARD:
180@@ -401,7 +476,8 @@ checkfn(struct decl *d) {
181
182 for(p = d->params, y = d->binding->type->args; p; p = p->next, y = y->next)
183 p->binding->type = y;
184- trdn(d->body, d->binding->type->ret);
185+ if(d->body)
186+ trdn(d->body, d->binding->type->ret);
187 }
188
189
+2,
-0
1@@ -1,2 +1,4 @@
2+pub typedef i64 = int
3+
4 pub sortdef nat = [n: int | n >= 0]
5 pub sortdef pos = [n: int | n >= 1]
+8,
-0
1@@ -0,0 +1,8 @@
2+extern fn putchar(c: i32) -> i32
3+
4+fn main() -> int {
5+ val _ = putchar(104)
6+ val _ = putchar(105)
7+ val _ = putchar(10)
8+ 0
9+}
M
exprs.h
+1,
-0
1@@ -11,3 +11,4 @@ EXPR(EHOLE)
2 EXPR(ETUPLE)
3 EXPR(EBORROW)
4 EXPR(EFNTYPE)
5+EXPR(ECAST) /* in struct expr we use args for the value being converted and body for the type expression */
M
kav.h
+5,
-0
1@@ -134,6 +134,10 @@ struct type *tydup(struct type *);
2 char *typestr(struct type *);
3 struct type *mkmeta(void);
4 struct type *deref(struct type *);
5+int isint(struct type *);
6+i64 wrap(struct type *, i64);
7+int isu64(struct type *);
8+u64 intmax(struct type *);
9 struct type *subst(struct type *, struct param *, struct type *);
10 int unify(struct type *, struct type *);
11
12@@ -214,6 +218,7 @@ struct decl {
13 const char *qname; /* name for qbe */
14 int pub;
15 int linear;
16+ int ext;
17 int newtype;
18 int expanding; /* catching self-refrential typedef */
19 struct param *sparams;
M
kws.h
+1,
-0
1@@ -9,6 +9,7 @@ TOKEN(TCASE, "case")
2 TOKEN(TOF, "of")
3 TOKEN(TSTRUCT, "struct")
4 TOKEN(TLINEAR, "linear")
5+TOKEN(TEXTERN, "extern")
6 TOKEN(TDATATYPE, "datatype")
7 TOKEN(TNEWTYPE, "newtype")
8 TOKEN(TTYPEDEF, "typedef")
M
linear.c
+5,
-1
1@@ -311,6 +311,9 @@ use(struct expr *e) {
2 case EGUARD:
3 arms(e);
4 break;
5+ case ECAST:
6+ use(e->args);
7+ break;
8 default:
9 break;
10 }
11@@ -324,7 +327,8 @@ linfn(struct decl *d) {
12 for(p = d->params; p; p = p->next)
13 if(islinear(p->binding->type))
14 track(p->binding, p->binding->type->borrow);
15- use(d->body);
16+ if(d->body)
17+ use(d->body);
18 endscope(nil);
19 }
20
M
ops.h
+12,
-0
1@@ -11,6 +11,18 @@ OP(ICLT, "csltl")
2 OP(ICGT, "csgtl")
3 OP(ICLE, "cslel")
4 OP(ICGE, "csgel")
5+OP(IUDIV, "udiv") /* u64 */
6+OP(IUREM, "urem")
7+OP(ICULT, "cultl")
8+OP(ICUGT, "cugtl")
9+OP(ICULE, "culel")
10+OP(ICUGE, "cugel")
11+OP(IEXTSB, "extsb")
12+OP(IEXTUB, "extub")
13+OP(IEXTSH, "extsh")
14+OP(IEXTUH, "extuh")
15+OP(IEXTSW, "extsw")
16+OP(IEXTUW, "extuw")
17 OP(ILOAD, "loadl")
18 OP(ISTORE, "storel")
19 OP(ICALL, "call")
M
parse.c
+30,
-4
1@@ -223,6 +223,22 @@ unary(void) {
2 return call();
3 }
4
5+static struct expr *
6+cast(void) {
7+ struct expr *e;
8+ struct expr *c;
9+
10+ e = unary();
11+ while(tok.kind == TAS) {
12+ c = mkexpr(ECAST, tok);
13+ next();
14+ c->args = e;
15+ c->body = type();
16+ e = c;
17+ }
18+ return e;
19+}
20+
21 static int
22 prec(enum tokenkind k) {
23 switch(k) {
24@@ -251,7 +267,7 @@ binary(int min) {
25 struct token op;
26 int p;
27
28- l = unary();
29+ l = cast();
30 while((p = prec(tok.kind)) >= min) {
31 op = tok;
32 next();
33@@ -469,18 +485,23 @@ params(enum tokenkind closer) {
34 }
35
36 static struct decl *
37-fndecl(void) {
38+fndecl(int ext) {
39 struct decl *d;
40
41 expect(TFN);
42 d = mkdecl(DFN, tok);
43+ if(ext && tokis(d->name, "main"))
44+ errorat(&d->name.loc, "main can't be extern");
45 expect(TIDENT);
46 d->sparams = sortparams();
47 expect(TLPAREN);
48 d->params = params(TRPAREN);
49 expect(TARROW);
50 d->ret = type();
51- d->body = block();
52+ if(ext) {
53+ if(tok.kind == TLBRACE)
54+ errorat(&tok.loc, "extern functions can't have a body");
55+ } else d->body = block();
56 return d;
57 }
58
59@@ -618,13 +639,17 @@ decl(void) {
60 struct decl *d;
61 int pub;
62 int linear;
63+ int ext;
64
65 pub = consume(TPUB);
66 linear = consume(TLINEAR);
67+ ext = consume(TEXTERN);
68+ if(ext && tok.kind != TFN)
69+ errorat(&tok.loc, "only functions can be extern");
70 if(tok.kind == TFN) {
71 if(linear)
72 errorat(&tok.loc, "functions cannot be linear");
73- d = fndecl();
74+ d = fndecl(ext);
75 } else if(tok.kind == TSTRUCT)
76 d = structdecl();
77 else if(tok.kind == TDATATYPE)
78@@ -648,6 +673,7 @@ decl(void) {
79
80 d->pub = pub;
81 d->linear = linear;
82+ d->ext = ext;
83 return d;
84 }
85
M
qbe.c
+100,
-16
1@@ -35,6 +35,7 @@ struct inst {
2 struct inst *next;
3 enum instkind kind;
4 int class;
5+ const char *abi; /* extern calls: the c type for the result or arg, nil is l */
6 struct operand res;
7 struct operand *arg[2];
8 };
9@@ -130,12 +131,13 @@ funcappend(struct func *f, struct inst *inst) {
10 }
11
12 static struct operand *
13-funcinst(struct func *f, enum instkind op, int class, struct operand *a0, struct operand *a1) {
14+funcinstabi(struct func *f, enum instkind op, int class, const char *abi, struct operand *a0, struct operand *a1) {
15 struct inst *inst;
16
17 inst = zalloc(sizeof(*inst));
18 inst->kind = op;
19 inst->class = class;
20+ inst->abi = abi;
21 inst->arg[0] = a0;
22 inst->arg[1] = a1;
23 if(class)
24@@ -144,6 +146,11 @@ funcinst(struct func *f, enum instkind op, int class, struct operand *a0, struct
25 return &inst->res;
26 }
27
28+static struct operand *
29+funcinst(struct func *f, enum instkind op, int class, struct operand *a0, struct operand *a1) {
30+ return funcinstabi(f, op, class, nil, a0, a1);
31+}
32+
33 static void
34 funccopy(struct func *f, struct operand *dst, struct operand *src) {
35 struct inst *inst;
36@@ -265,6 +272,61 @@ functuple(struct func *f, struct expr *e) {
37 }
38
39
40+static struct operand *
41+funcwrap(struct func *f, struct type *t, struct operand *v) {
42+ enum instkind op;
43+
44+ t = deref(t);
45+ if(t->binding == bi8)
46+ op = IEXTSB;
47+ else if(t->binding == bu8)
48+ op = IEXTUB;
49+ else if(t->binding == bi16)
50+ op = IEXTSH;
51+ else if(t->binding == bu16)
52+ op = IEXTUH;
53+ else if(t->binding == bi32)
54+ op = IEXTSW;
55+ else if(t->binding == bu32)
56+ op = IEXTUW;
57+ else
58+ return v;
59+ return funcinst(f, op, 'l', v, nil);
60+}
61+
62+/* the qbe abi type c uses for t for extern calls */
63+static const char *
64+abity(struct type *t) {
65+ t = deref(t);
66+ if(t->binding == bi8)
67+ return "sb";
68+ if(t->binding == bu8)
69+ return "ub";
70+ if(t->binding == bi16)
71+ return "sh";
72+ if(t->binding == bu16)
73+ return "uh";
74+ if(t->binding == bi32 || t->binding == bu32)
75+ return "w";
76+ return "l";
77+}
78+
79+/* an extern call passes and returns c types, the result has junk
80+ * in its top bits so it gets cut down to its type after */
81+static struct operand *
82+funcextcall(struct func *f, struct operand *callee, struct type *ft, struct operand **args, usize n) {
83+ struct operand *r;
84+ struct type *y;
85+ const char *abi;
86+ usize i;
87+
88+ abi = abity(ft->ret);
89+ r = funcinstabi(f, ICALL, strcmp(abi, "l") ? 'w' : 'l', abi, callee, nil);
90+ for(i = 0, y = ft->args; i < n; i++, y = y->next)
91+ funcinstabi(f, IARG, 0, abity(y), args[i], nil);
92+ return funcwrap(f, ft->ret, r);
93+}
94+
95 static struct operand *
96 funccall(struct func *f, struct expr *e) {
97 struct operand *callee;
98@@ -287,39 +349,51 @@ funccall(struct func *f, struct expr *e) {
99 args = alloc(n * sizeof(*args));
100 for(a = e->args, i = 0; a; a = a->next, i++)
101 args[i] = funcexpr(f, a);
102+ if(e->binding->kind == BFN && e->binding->decl->ext)
103+ return funcextcall(f, callee, e->binding->type, args, n);
104 r = funcinst(f, ICALL, 'l', callee, nil);
105 for(i = 0; i < n; i++)
106 funcinst(f, IARG, 0, args[i], nil);
107 return r;
108 }
109
110+
111 /* one arg is negation & two is a binop */
112 static struct operand *
113 funcop(struct func *f, struct expr *e) {
114 struct operand *l;
115 struct operand *r;
116+ struct operand *res;
117+ struct type *t;
118 enum instkind op;
119+ int u;
120+ int cmp = 0;
121
122+ t = e->args->type;
123+ u = isu64(t);
124 l = funcexpr(f, e->args);
125 if(!e->args->next)
126- return funcinst(f, INEG, 'l', l, nil);
127+ return funcwrap(f, t, funcinst(f, INEG, 'l', l, nil));
128 r = funcexpr(f, e->args->next);
129 switch(e->tok.kind) {
130 case TADD: op = IADD; break;
131 case TSUB: op = ISUB; break;
132 case TMUL: op = IMUL; break;
133- case TDIV: op = IDIV; break;
134- case TMOD: op = IREM; break;
135- case TEQ: op = ICEQ; break;
136- case TNEQ: op = ICNE; break;
137- case TLESS: op = ICLT; break;
138- case TMORE: op = ICGT; break;
139- case TLEQ: op = ICLE; break;
140- case TMEQ: op = ICGE; break;
141+ case TDIV: op = u ? IUDIV : IDIV; break;
142+ case TMOD: op = u ? IUREM : IREM; break;
143+ case TEQ: op = ICEQ; cmp = 1; break;
144+ case TNEQ: op = ICNE; cmp = 1; break;
145+ case TLESS: op = u ? ICULT : ICLT; cmp = 1; break;
146+ case TMORE: op = u ? ICUGT : ICGT; cmp = 1; break;
147+ case TLEQ: op = u ? ICULE : ICLE; cmp = 1; break;
148+ case TMEQ: op = u ? ICUGE : ICGE; cmp = 1; break;
149 default:
150 errorat(&e->tok.loc, "sorry, no idea how to compile this operator");
151 }
152- return funcinst(f, op, 'l', l, r);
153+ res = funcinst(f, op, 'l', l, r);
154+ if(cmp)
155+ return res;
156+ return funcwrap(f, t, res);
157 }
158
159 /* an arm's body, in tail position it returns or loops by itself,
160@@ -599,6 +673,8 @@ funcexpr(struct func *f, struct expr *e) {
161 return funccase(f, e, 0);
162 case ETUPLE:
163 return functuple(f, e);
164+ case ECAST:
165+ return funcwrap(f, e->type, funcexpr(f, e->args));
166 case ECALL:
167 if(e->tok.kind == TIDENT)
168 return funccall(f, e);
169@@ -675,8 +751,12 @@ emitinst(struct inst *i) {
170 if(i->res.kind != VNONE) {
171 emitoperand(&i->res);
172 fputs(" =", stdout);
173- c = i->class;
174- fwrite(&c, 1, 1, stdout);
175+ if(i->abi) {
176+ fputs(i->abi, stdout);
177+ } else {
178+ c = i->class;
179+ fwrite(&c, 1, 1, stdout);
180+ }
181 fputs(" ", stdout);
182 }
183 fputs(instname[i->kind], stdout);
184@@ -687,7 +767,8 @@ emitinst(struct inst *i) {
185 for(j = i->next; j && j->kind == IARG; j = j->next) {
186 if(j != i->next)
187 fputs(", ", stdout);
188- fputs("l ", stdout);
189+ fputs(j->abi ? j->abi : "l", stdout);
190+ fputs(" ", stdout);
191 emitoperand(j->arg[0]);
192 }
193 fputs(")\n", stdout);
194@@ -846,13 +927,16 @@ qbe(struct module *m) {
195 die("modules ", y->path, " and ", x->path, " have the same name");
196 for(d = x->decls; d; d = d->next)
197 if(d->kind == DFN)
198- d->qname = mangle(x->qname, d->name);
199+ if(d->ext)
200+ d->qname = tokdup(d->name);
201+ else
202+ d->qname = mangle(x->qname, d->name);
203 }
204
205 for(x = m; x; x = x->next) {
206 last = x;
207 for(d = x->decls; d; d = d->next) {
208- if(d->kind != DFN)
209+ if(d->kind != DFN || d->ext)
210 continue;
211 f = mkfunc(d, d->qname);
212 functail(f, d->body);
+4,
-0
1@@ -238,6 +238,10 @@ walk(struct scope *s, enum space sp, struct expr *e) {
2 case EBORROW:
3 walk(s, sp, e->body);
4 break;
5+ case ECAST:
6+ walk(s, sp, e->args);
7+ walk(s, STYPE, e->body);
8+ break;
9 case EFNTYPE:
10 for(a = e->args; a; a = a->next)
11 walk(s, sp, a);
M
run.c
+24,
-12
1@@ -86,35 +86,42 @@ getvar(struct env *env, struct binding *b) {
2 die("unbound variable");
3 }
4
5+/* t is the operands' type. the maths is done in u64 so overflow wraps
6+ * instead of being undefined, then wrap() cuts it down to t */
7 static struct value *
8-binop(struct token op, i64 a, i64 b) {
9+binop(struct token op, struct type *t, i64 a, i64 b) {
10+ u64 ua = a;
11+ u64 ub = b;
12+ int u = isu64(t);
13+
14 switch(op.kind) {
15 case TADD:
16- return mkint(a + b);
17+ return mkint(wrap(t, (i64)(ua + ub)));
18 case TSUB:
19- return mkint(a - b);
20+ return mkint(wrap(t, (i64)(ua - ub)));
21 case TMUL:
22- return mkint(a * b);
23+ return mkint(wrap(t, (i64)(ua * ub)));
24 case TDIV:
25 if(b == 0)
26 errorat(&op.loc, "division by zero");
27- return mkint(a / b);
28+ return mkint(wrap(t, u ? (i64)(ua / ub) : a / b));
29 case TMOD:
30 if(b == 0)
31 errorat(&op.loc, "mod by zero");
32- return mkint(a % b);
33+ return mkint(wrap(t, u ? (i64)(ua % ub) : a % b));
34 case TEQ:
35 return mkbool(a == b);
36 case TNEQ:
37 return mkbool(a != b);
38+ /* only u64 needs unsigned compares, the narrow types are already extended */
39 case TLESS:
40- return mkbool(a < b);
41+ return mkbool(u ? ua < ub : a < b);
42 case TMORE:
43- return mkbool(a > b);
44+ return mkbool(u ? ua > ub : a > b);
45 case TLEQ:
46- return mkbool(a <= b);
47+ return mkbool(u ? ua <= ub : a <= b);
48 case TMEQ:
49- return mkbool(a >= b);
50+ return mkbool(u ? ua >= ub : a >= b);
51 default:
52 errorat(&op.loc, "sorry, no idea how to handle this operator");
53 }
54@@ -193,13 +200,16 @@ eval(struct env *env, struct expr *e) {
55 if(e->binding->kind == BFN)
56 return mkfn(e->binding->decl);
57 return getvar(env, e->binding);
58+ case ECAST:
59+ l = eval(env, e->args);
60+ return mkint(wrap(e->type, l->num));
61 case ECALL:
62 if(e->tok.kind != TIDENT) {
63 l = eval(env, e->args);
64 if(!e->args->next)
65- return mkint(-l->num);
66+ return mkint(wrap(e->type, (i64)-(u64)l->num));
67 r = eval(env, e->args->next);
68- return binop(e->tok, l->num, r->num);
69+ return binop(e->tok, e->args->type, l->num, r->num);
70 }
71 if(e->binding->kind == BCTOR) {
72 n = 0;
73@@ -227,6 +237,8 @@ eval(struct env *env, struct expr *e) {
74 }
75 for(p = f->params, a = e->args; p && a; p = p->next, a = a->next)
76 callenv = bind(callenv, p->binding, eval(env, a));
77+ if (!f->body)
78+ errorat(&e->tok.loc, "cant call extern function '", tokdup(f->name), "' in the interpreter");
79 return eval(callenv, f->body);
80 case ETUPLE:
81 n = 0;
M
type.c
+55,
-0
1@@ -150,6 +150,61 @@ deref(struct type *t) {
2 return t;
3 }
4
5+/* one of the builtin integer types */
6+int
7+isint(struct type *t) {
8+ t = deref(t);
9+ if(t->kind != YNAME)
10+ return 0;
11+ return t->binding == bint || t->binding == bi8 || t->binding == bi16 || t->binding == bi32 ||
12+ t->binding == bu8 || t->binding == bu16 || t->binding == bu32 || t->binding == bu64;
13+}
14+
15+u64
16+intmax(struct type *t) {
17+ t = deref(t);
18+ if(t->binding == bi8)
19+ return INT8_MAX;
20+ if(t->binding == bu8)
21+ return UINT8_MAX;
22+ if(t->binding == bi16)
23+ return INT16_MAX;
24+ if(t->binding == bu16)
25+ return UINT16_MAX;
26+ if(t->binding == bi32)
27+ return INT32_MAX;
28+ if(t->binding == bu32)
29+ return UINT32_MAX;
30+ if(t->binding == bu64)
31+ return UINT64_MAX;
32+ return INT64_MAX;
33+}
34+
35+/* u64 is the only type that needs unsigned compares and division */
36+int
37+isu64(struct type *t) {
38+ return deref(t)->binding == bu64;
39+}
40+
41+i64
42+wrap(struct type *t, i64 n) {
43+ t = deref(t);
44+ if(t->binding == bi8)
45+ return (i8)n;
46+ if(t->binding == bi16)
47+ return (i16)n;
48+ if(t->binding == bi32)
49+ return (i32)n;
50+ if(t->binding == bu8)
51+ return (u8)n;
52+ if(t->binding == bu16)
53+ return (u16)n;
54+ if(t->binding == bu32)
55+ return (u32)n;
56+ /* int and u64 are already 64 bits */
57+ return n;
58+}
59+
60 /* a copy of t with each param in from replaced by the matching type in to */
61 struct type *
62 subst(struct type *t, struct param *from, struct type *to) {
M
util.c
+2,
-2
1@@ -144,12 +144,12 @@ putnum(usize u) {
2 /* the value of a number token */
3 i64
4 tonum(struct token t) {
5- i64 n = 0;
6+ u64 n = 0;
7 usize i;
8
9 for(i = 0; i < t.length; i++)
10 n = n * 10 + (t.str[i] - '0');
11- return n;
12+ return (i64)n;
13 }
14
15 char *