Commit 7738084f

7738084fa9c8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Add M15: && / || (AndNode/OrNode) for boolean operands

A boolean-operand AndNode/OrNode lowers to C (a && b) / (a || b), which covers the very common compound condition (if a > 1 && b < 5). The value-returning form over non-bool operands (a || b yielding a value) needs truthiness/nullable and is left for later.

make test: see count above.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
7738084fa9c8
Parent
e571fbdbbf44

src/analyze.cmodifiedView file

@@ -317,6 +317,12 @@
317317 int mi = comp_method_in_chain(c, p, s->name, NULL);
318318 return mi >= 0 ? c->scopes[mi].ret : TY_UNKNOWN;
319319 }
320+ if (!strcmp(ty, "AndNode") || !strcmp(ty, "OrNode")) {
321+ TyKind lt = infer_type(c, nt_ref(nt, id, "left"));
322+ TyKind rt = infer_type(c, nt_ref(nt, id, "right"));
323+ if (lt == TY_BOOL && rt == TY_BOOL) return TY_BOOL;
324+ return ty_unify(lt, rt); /* value form: a || b -> common type */
325+ }
320326 if (!strcmp(ty, "CallNode")) return infer_call(c, id);
321327
322328 return TY_UNKNOWN;