feat: implement logical AND operator with short-circuit evaluation and new CODE_AND instruction

Add TOKEN_AMPAMP token to the lexer for parsing '&&' operator, introduce PREC_LOGIC precedence level for proper expression parsing, implement the `and()` compiler function that emits CODE_AND with a patchable jump offset, and add runtime support in the VM with IS_FALSE macro for truthiness checking. Include comprehensive test cases in test/and.wren verifying short-circuit behavior and return values.
This commit is contained in:
Bob Nystrom
2013-11-19 15:35:25 +00:00
parent ed9f4ec9f3
commit 9e8eed6c29
5 changed files with 90 additions and 12 deletions
+20
View File
@@ -0,0 +1,20 @@
// Note: These tests implicitly depend on ints being truthy.
// Also rely on io.write() returning its argument.
// Return the first non-true argument.
io.write(false && 1) // expect: false
io.write(true && 1) // expect: 1
io.write(1 && 2 && false) // expect: false
// Return the last argument if all are true.
io.write(1 && true) // expect: true
io.write(1 && 2 && 3) // expect: 3
// Short-circuit at the first false argument.
io.write(true) && // expect: true
io.write(false) && // expect: false
io.write(false) // should not print
// Swallow a trailing newline.
io.write(true &&
true) // expect: true