Fix continue statement

This commit is contained in:
Joey committed 2019-02-19 19:40:34 -08:00
1 parent 53bcd04f8a
commit 9face190ab
2 files changed
+34 -2

No files matched your search

+23
View File
@@ -1,3 +1,5 @@
# break from a nested for loop
def foo():
sum = 0
for i in range(10):
@@ -8,3 +10,24 @@ def foo():
return sum
assert foo() == 45
# continue statement
def primes(limit):
"""Finds all the primes from 2 up to a given number using the Sieve of Eratosthenes."""
sieve = [False] * (limit + 1)
for i in range(2, limit + 1):
if sieve[i]:
continue
yield i
for j in range(2 * i, limit + 1, i):
sieve[j] = True
assert list(primes(1)) == []
assert list(primes(2)) == [2]
assert list(primes(10)) == [2, 3, 5, 7]
assert list(primes(13)) == [2, 3, 5, 7, 11, 13]
+11 -2
View File
@@ -542,7 +542,10 @@ impl Frame {
bytecode::Instruction::Break => {
let block = self.unwind_loop(vm);
if let BlockType::Loop { end, .. } = block.typ {
self.pop_block();
self.jump(end);
} else {
unreachable!()
}
Ok(None)
}
@@ -555,7 +558,7 @@ impl Frame {
if let BlockType::Loop { start, .. } = block.typ {
self.jump(start);
} else {
assert!(false);
unreachable!();
}
Ok(None)
}
@@ -745,7 +748,7 @@ impl Frame {
fn unwind_loop(&mut self, vm: &mut VirtualMachine) -> Block {
loop {
let block = self.pop_block().expect("not in a loop");
let block = self.current_block().cloned().expect("not in a loop");
match block.typ {
BlockType::Loop { .. } => break block,
BlockType::TryExcept { .. } => {
@@ -760,6 +763,8 @@ impl Frame {
}
},
}
self.pop_block();
}
}
@@ -1080,6 +1085,10 @@ impl Frame {
Some(block)
}
fn current_block(&self) -> Option<&Block> {
self.blocks.last()
}
pub fn push_value(&mut self, obj: PyObjectRef) {
self.stack.push(obj);
}