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:
|
if self.loop.loopEnd != -1:
|
||||||
self.patchJump(self.loop.loopEnd)
|
self.patchJump(self.loop.loopEnd)
|
||||||
self.emitByte(OpCode.Pop)
|
self.emitByte(OpCode.Pop)
|
||||||
var i = self.loop.body
|
|
||||||
while i < self.currentChunk.code.len:
|
for brk in self.loop.breaks:
|
||||||
if self.currentChunk.code[i] == uint OpCode.Break:
|
when DEBUG_TRACE_COMPILER:
|
||||||
self.currentChunk.code[i] = uint8 OpCode.Jump
|
setForegroundColor(fgYellow)
|
||||||
self.patchJump(i + 1)
|
write stdout, &"DEBUG - Compiler: patching break at {brk}\n"
|
||||||
i += 3
|
setForegroundColor(fgDefault)
|
||||||
else:
|
self.currentChunk.code[brk] = OpCode.Jump.uint8
|
||||||
i += 1
|
self.patchJump(brk + 1)
|
||||||
|
|
||||||
self.loop = self.loop.outer
|
self.loop = self.loop.outer
|
||||||
|
|
||||||
|
|
||||||
|
@ -843,6 +844,7 @@ proc parseBreak(self: Compiler) =
|
||||||
self.emitByte(OpCode.Pop)
|
self.emitByte(OpCode.Pop)
|
||||||
i -= 1
|
i -= 1
|
||||||
discard self.emitJump(OpCode.Break)
|
discard self.emitJump(OpCode.Break)
|
||||||
|
self.loop.breaks.add(self.currentChunk.code.len() - 3)
|
||||||
|
|
||||||
|
|
||||||
proc parseAnd(self: Compiler, canAssign: bool) =
|
proc parseAnd(self: Compiler, canAssign: bool) =
|
||||||
|
|
|
@ -21,3 +21,4 @@ type Loop* = ref object
|
||||||
alive*: bool
|
alive*: bool
|
||||||
body*: int
|
body*: int
|
||||||
loopEnd*: 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.Xor, OpCode.Not, OpCode.Equal,
|
||||||
OpCode.Greater, OpCode.Less, OpCode.GetItem,
|
OpCode.Greater, OpCode.Less, OpCode.GetItem,
|
||||||
OpCode.Slice, OpCode.Pop, OpCode.Negate,
|
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,
|
const constantInstructions* = {OpCode.Constant, OpCode.DefineGlobal,
|
||||||
OpCode.GetGlobal, OpCode.SetGlobal,
|
OpCode.GetGlobal, OpCode.SetGlobal,
|
||||||
OpCode.DeleteGlobal}
|
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