From 201d9da20c9e7d268e20a19b39ae6626750d2b3b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:31:44 +0000 Subject: [PATCH] and and or skip their right side after a constant that decides them false and f() and true or f() called f, as a value and under not, though 6.2 says the right side is evaluated only when needed: op2 computed y and dropped it. op1 now jumps over y's code to a label op2 places after it. conform/run/ShortConst holds it. --- diff --git a/conform/run/ShortConst.expect b/conform/run/ShortConst.expect new file mode 100644 index 0000000..3545c57 --- /dev/null +++ b/conform/run/ShortConst.expect @@ -0,0 +1 @@ +1gh diff --git a/conform/run/ShortConst.raz b/conform/run/ShortConst.raz new file mode 100644 index 0000000..2def0a0 --- /dev/null +++ b/conform/run/ShortConst.raz @@ -0,0 +1,29 @@ +(* and and or evaluate their right side only when needed, also when + the left side is a constant: false and f(), true or f() call no f, + as a value and as a condition, under not too. *) +module ShortConst; +import Out; + +(* Says c, so the output shows each call *) +proc f(c: U8): BOOL; +begin + Out.char(c); + return true; +end; + +proc main*; +var b: BOOL; +begin + b := false and f('a'); + b := true or f('b'); + b := not (false and f('c')); + if b then Out.char('1'); end; + if not (true or f('d')) then Out.char('x'); end; + b := false and f('e') and f('f'); + b := true and f('g'); + b := false or f('h'); + if false and true then Out.char('x'); end; + Out.char(10); +end; + +end ShortConst. diff --git a/doc/compiler.md b/doc/compiler.md index 107dd45..775c4e7 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1570,7 +1570,11 @@ operand, unless it is a constant; for `and` and `or` it makes the left one a condition (`makeCond`) and jumps past the right one, which `op2` joins to it: `(a < b) and (c < d)` in an `if` is two compares and two -jumps, as `not` of a condition is the other comparison. `op2` checks the +jumps, as `not` of a condition is the other comparison. A constant +left that decides the result, `false and y` or `true or y`, is the +constant itself: `op1` jumps over the right one's code, about to be +made, to a label `op2` places after it, so that `y` is never run, as +the report says. `op2` checks the types, folds constants (`foldInt`, `foldReal`), and otherwise pushes the right operand and has Gen do the operation; a constant on the left goes on after the right, and the two `swap`. A set's operations are diff --git a/src/Item.raz b/src/Item.raz index 0761526..96b2127 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1188,7 +1188,12 @@ begin L := Gen.NoLabel; if ((op = Scan.sAnd) or (op = Scan.sOr)) and (x.typ^.form = Sym.fBool) then - if x.mode = mConst then return nil; end; (* folded by op2 *) + if x.mode = mConst then + (* false and y, true or y: y isn't evaluated, so its code, + about to be made, is jumped over to L, which op2 places *) + if (x.val = 1) # (op = Scan.sAnd) then Gen.jump(L); end; + return nil; (* folded by op2 *) + end; try makeCond(x); if op = Scan.sAnd then Gen.jumpOut(negated(x.rel), x.relSigned, x.fj); @@ -1392,7 +1397,7 @@ return Scan.markAt(Diag.join(x.pos, y.pos), say2("{} doesn't apply to {}", Scan.symText(op), Sym.typeName(t))); end; -proc op2*(op: Scan.Sym; var x, y: Item; _L: INT): ERROR; +proc op2*(op: Scan.Sym; var x, y: Item; L: INT): ERROR; var xt, yt, t, e: ^Sym.Type; r: Item; scale: INT; isPtr: BOOL; a, b, v: I64; begin xt := x.typ; yt := y.typ; @@ -1404,10 +1409,12 @@ if yt^.form # Sym.fBool then return wrongLogic(op, y); end; if (x.mode = mConst) and (y.mode = mConst) then if op = Scan.sAnd then x.val := x.val & y.val; else x.val := x.val | y.val; end; + Gen.fix(L); (* op1's jump, if it made one, over no code *) elsif x.mode = mConst then - (* true and y, false or y: y; else x, y computed and dropped *) + (* true and y, false or y: y; else x, and y's code, which op1's + jump to L passes over, is never run *) if (x.val = 1) = (op = Scan.sAnd) then x := y; - else try discard(y); end; + else try discard(y); Gen.fix(L); end; else try makeCond(y); if op = Scan.sAnd then x.fj := Gen.joinJumps(x.fj, y.fj); x.tj := y.tj;