mirror of https://github.com/japl-lang/japl.git
Fixed bug with break (breaking the test for.jpl)
This commit is contained in:
parent
fb11433477
commit
f94d4fecc8
|
@ -747,14 +747,15 @@ proc endLooping(self: Compiler) =
|
|||
if self.loop.loopEnd != -1:
|
||||
self.patchJump(self.loop.loopEnd)
|
||||
self.emitByte(OpCode.Pop)
|
||||
var i = self.loop.body
|
||||
while i < self.currentChunk.code.len:
|
||||
if self.currentChunk.code[i] == uint OpCode.Break:
|
||||
self.currentChunk.code[i] = uint8 OpCode.Jump
|
||||
self.patchJump(i + 1)
|
||||
i += 3
|
||||
else:
|
||||
i += 1
|
||||
|
||||
for brk in self.loop.breaks:
|
||||
when DEBUG_TRACE_COMPILER:
|
||||
setForegroundColor(fgYellow)
|
||||
write stdout, &"DEBUG - Compiler: patching break at {brk}\n"
|
||||
setForegroundColor(fgDefault)
|
||||
self.currentChunk.code[brk] = OpCode.Jump.uint8
|
||||
self.patchJump(brk + 1)
|
||||
|
||||
self.loop = self.loop.outer
|
||||
|
||||
|
||||
|
@ -843,6 +844,7 @@ proc parseBreak(self: Compiler) =
|
|||
self.emitByte(OpCode.Pop)
|
||||
i -= 1
|
||||
discard self.emitJump(OpCode.Break)
|
||||
self.loop.breaks.add(self.currentChunk.code.len() - 3)
|
||||
|
||||
|
||||
proc parseAnd(self: Compiler, canAssign: bool) =
|
||||
|
|
|
@ -21,3 +21,4 @@ type Loop* = ref object
|
|||
alive*: bool
|
||||
body*: int
|
||||
loopEnd*: int
|
||||
breaks*: seq[int]
|
||||
|
|
|
@ -83,7 +83,9 @@ const simpleInstructions* = {OpCode.Return, OpCode.Add, OpCode.Multiply,
|
|||
OpCode.Xor, OpCode.Not, OpCode.Equal,
|
||||
OpCode.Greater, OpCode.Less, OpCode.GetItem,
|
||||
OpCode.Slice, OpCode.Pop, OpCode.Negate,
|
||||
OpCode.Is, OpCode.As, GreaterOrEqual, LessOrEqual}
|
||||
OpCode.Is, OpCode.As, OpCode.GreaterOrEqual,
|
||||
OpCode.LessOrEqual, OpCode.Bor, OpCode.Band,
|
||||
OpCode.Bnot}
|
||||
const constantInstructions* = {OpCode.Constant, OpCode.DefineGlobal,
|
||||
OpCode.GetGlobal, OpCode.SetGlobal,
|
||||
OpCode.DeleteGlobal}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
//[Test: breaks]
|
||||
//[source:mixed]
|
||||
var x = 5;
|
||||
while (true) {
|
||||
var a = 1;
|
||||
x = x - a;
|
||||
if (x < 0) {
|
||||
break;
|
||||
}
|
||||
print(x);
|
||||
}
|
||||
//[end]
|
||||
/*
|
||||
[stdout]
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
||||
0
|
||||
[end]
|
||||
[end]
|
||||
*/
|
||||
|
Loading…
Reference in New Issue