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:
@@ -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
|
||||
Reference in New Issue
Block a user