commit 433c30e

ricmaps  ·  2026-11-25 22:47:06 +0000 UTC
parent 7de2e02
add boolean operators
5 files changed,  +63, -6
M punc.h
M qbe.c
+5, -0
 1@@ -378,6 +378,11 @@ apply(struct expr *e, struct type *want) {
 2 	struct type *t;
 3 
 4 	if(e->tok.kind != TIDENT) {
 5+		if(e->tok.kind == TAND || e->tok.kind == TOR || e->tok.kind == TBANG) {
 6+			for(a = e->args; a; a = a->next)
 7+				trdn(a, mktype(YNAME, bbool));
 8+			return mktype(YNAME, bbool);
 9+		}
10 		if(want && isint(want) && !iscmp(e->tok.kind)) {
11 			/* we know the type */
12 			t = want;
+13, -0
 1@@ -246,6 +246,8 @@ use(struct expr *e) {
 2 	struct type *ps;
 3 	struct lvar **locked;
 4 	usize nlocked;
 5+	int *pre;
 6+	int i;
 7 
 8 	switch(e->kind) {
 9 	case EIDENT:
10@@ -260,6 +262,17 @@ use(struct expr *e) {
11 		}
12 		break;
13 	case ECALL:
14+		/* the right side of && and || only runs sometimes, so it
15+		 * can't consume anything or the consume would be conditional */
16+		if(e->tok.kind == TAND || e->tok.kind == TOR) {
17+			use(e->args);
18+			pre = snapshot(vars);
19+			use(e->args->next);
20+			for(v = vars, i = 0; v; v = v->next, i++)
21+				if(v->used != pre[i])
22+					errorat(&e->tok.loc, "right side of '", tokdup(e->tok), "' consumes '", tokdup(v->binding->name), "'");
23+			break;
24+		}
25 		if(e->tok.kind == TIDENT && e->binding->type->kind == YFN)
26 			ps = e->binding->type->args;
27 		else
+9, -5
 1@@ -225,7 +225,7 @@ static struct expr *
 2 unary(void) {
 3 	struct expr *e;
 4 
 5-	if(tok.kind == TSUB) {
 6+	if(tok.kind == TSUB || tok.kind == TBANG) {
 7 		e = mkexpr(ECALL, tok);
 8 		next();
 9 		e->args = unary();
10@@ -253,20 +253,24 @@ cast(void) {
11 static int
12 prec(enum tokenkind k) {
13 	switch(k) {
14+	case TOR:
15+		return 1;
16+	case TAND:
17+		return 2;
18 	case TEQ:
19 	case TNEQ:
20 	case TLESS:
21 	case TMORE:
22 	case TLEQ:
23 	case TMEQ:
24-		return 1;
25+		return 3;
26 	case TADD:
27 	case TSUB:
28-		return 2;
29+		return 4;
30 	case TMUL:
31 	case TDIV:
32 	case TMOD:
33-		return 3;
34+		return 5;
35 	default:
36 		return 0;
37 	}
38@@ -283,7 +287,7 @@ binary(int min) {
39 		op = tok;
40 		next();
41 		l = mkbinary(op, l, binary(p + 1));
42-		if(p == 1 && prec(tok.kind) == 1)
43+		if(p == 3 && prec(tok.kind) == 3)
44 			errorat(&tok.loc, "comparisons can't be chained");
45 	}
46 	return l;
M punc.h
+2, -0
1@@ -26,3 +26,5 @@ TOKEN(TTILDE,       "~")
2 TOKEN(TDCOLON,     "::")
3 TOKEN(TBANG,        "!")
4 TOKEN(TPIPE,       "|>")
5+TOKEN(TAND,        "&&")
6+TOKEN(TOR,         "||")
M qbe.c
+34, -1
 1@@ -403,7 +403,36 @@ funccall(struct func *f, struct expr *e) {
 2 }
 3 
 4 
 5-/* one arg is negation & two is a binop */
 6+static struct operand *
 7+funcshort(struct func *f, struct expr *e) {
 8+	struct operand *r;
 9+	struct block *rhs;
10+	struct block *skip;
11+	struct block *join;
12+	struct operand *l;
13+	int conj;
14+
15+	conj = e->tok.kind == TAND;
16+	r = zalloc(sizeof(*r));
17+	functemp(f, r);
18+	rhs = mkblock("rhs");
19+	skip = mkblock("skip");
20+	join = mkblock("join");
21+	l = funcexpr(f, e->args);
22+	if(conj)
23+		funcjnz(f, l, rhs, skip);
24+	else
25+		funcjnz(f, l, skip, rhs);
26+	funclabel(f, rhs);
27+	funccopy(f, r, funcexpr(f, e->args->next));
28+	funcjmp(f, join);
29+	funclabel(f, skip);
30+	funccopy(f, r, mkconst(conj ? 0 : 1));
31+	funcjmp(f, join);
32+	funclabel(f, join);
33+	return r;
34+}
35+
36 static struct operand *
37 funcop(struct func *f, struct expr *e) {
38 	struct operand *l;
39@@ -414,6 +443,10 @@ funcop(struct func *f, struct expr *e) {
40 	int u;
41 	int cmp = 0;
42 
43+	if(e->tok.kind == TAND || e->tok.kind == TOR)
44+		return funcshort(f, e);
45+	if(e->tok.kind == TBANG)
46+		return funcinst(f, ICEQ, 'l', funcexpr(f, e->args), mkconst(0));
47 	t = e->args->type;
48 	u = isu64(t);
49 	l = funcexpr(f, e->args);