feat: add while loop token, parser support, CODE_LOOP opcode, and patchJump helper

Add TOKEN_WHILE to the token enum and keyword lookup in the compiler, enabling the parser to recognize 'while' statements. Introduce the CODE_LOOP opcode in the VM for backward jumps, and implement the patchJump helper to resolve forward jump offsets during compilation. Include comprehensive test cases for while loops covering single-expression bodies, block bodies, newline handling, and null result semantics.
This commit is contained in:
Bob Nystrom
2013-11-18 06:38:59 +00:00
parent 9c663aa13c
commit dc2f053d76
5 changed files with 277 additions and 158 deletions
+38
View File
@@ -0,0 +1,38 @@
// Evaluate the 'then' expression if the condition is true.
if (true) io.write("good") // expect: good
if (false) io.write("bad")
// Evaluate the 'else' expression if the condition is false.
if (true) io.write("good") else io.write("bad") // expect: good
if (false) io.write("bad") else io.write("good") // expect: good
// Allow statements for then branch.
if (true) { io.write("block") } // expect: block
if (true) if (true) io.write("double") // expect: double
// Allow statements for else branch.
if (false) null else { io.write("block") } // expect: block
if (false) null else if (true) io.write("double") // expect: double
// Return the 'then' expression if the condition is true.
var a = if (true) "good"
io.write(a) // expect: good
// Return null if the condition is false and there is no else.
var b = if (false) "bad"
io.write(b) // expect: null
// Return the 'else' expression if the condition is false.
var c = if (false) "bad" else "good"
io.write(c) // expect: good
// Assignment in if condition.
if (a = true) io.write(a) // expect: true
// Newline after "if".
if
(true) io.write("good") // expect: good
// Newline after "else".
if (false) io.write("bad") else
io.write("good") // expect: good