Commit 08c3bad2

08c3bad28a82Merged

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

Description

Add Ruby truthiness for the ! operator

!x follows Ruby semantics (only nil and false are falsy): bool -> C !; nil -> true; any other value (int 0, "", :sym, ...) is truthy, so !x is false (evaluate x for side effects, yield 0). Fixes bang_op_ruby_truthy.

make test: 34 (1 fail left: int_overflow_promote, needs promote mode).

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

Details

Commit
08c3bad28a82
Parent
807830746553

src/codegen.cmodifiedView file

@@ -495,7 +495,10 @@
495495 return;
496496 }
497497 if (!strcmp(name, "!") && recv >= 0 && argc == 0) {
498- buf_puts(b, "(!"); emit_expr(c, recv, b); buf_puts(b, ")");
498+ /* Ruby truthiness: only nil and false are falsy */
499+ if (rt == TY_BOOL) { buf_puts(b, "(!"); emit_expr(c, recv, b); buf_puts(b, ")"); }
500+ else if (rt == TY_NIL) { buf_puts(b, "1"); }
501+ else { buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), 0)"); } /* truthy -> false */
499502 return;
500503 }
501504