forked from cqctlang/l1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpl.c
66 lines (61 loc) · 1.1 KB
/
cpl.c
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
#include "sys.h"
#include "util.h"
#include "syscqct.h"
static Expr*
compilel(U *ctx, Expr* e)
{
Expr *p;
Expr *se;
if(e == 0)
return 0;
switch(e->kind){
case Eland:
se = Zifelse(compilel(ctx, e->e1),
Zifelse(compilel(ctx, e->e2),
Zint(1),
Zint(0)),
Zint(0));
putsrc(se, e->src);
return se;
case Elor:
se = Zifelse(compilel(ctx, e->e1),
Zint(1),
Zifelse(compilel(ctx, e->e2),
Zint(1),
Zint(0)));
putsrc(se, e->src);
return se;
case Eelist:
p = e;
while(p->kind == Eelist){
sete1(p, compilel(ctx, p->e1));
p = p->e2;
}
return e;
default:
sete1(e, compilel(ctx, e->e1));
sete2(e, compilel(ctx, e->e2));
sete3(e, compilel(ctx, e->e3));
sete4(e, compilel(ctx, e->e4));
return e;
}
}
Expr*
docompilel(U *ctx, Expr *e)
{
compilel(ctx, e);
return e;
}
void
l1_cpl(VM *vm, Imm argc, Val *argv, Val *rv)
{
U ctx;
Expr *e;
if(argc != 1)
vmerr(vm, "wrong number of arguments to cpl");
checkarg(vm, argv, 0, Qexpr);
initctx(&ctx, vm);
e = docompilel(&ctx, valexpr(argv[0]));
if(e)
*rv = mkvalexpr(e);
}