aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-01-12 23:35:47 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-01-12 23:35:47 +0100
commit4db5a471562130947830ad3edc127a7a994f8e7d (patch)
treebf05efc8fd5173e1073e70c8157c63a22bbab6b4
parentb9710f5de083e8205dbb51921f43d96b17fd43cf (diff)
downloadsylt-4db5a471562130947830ad3edc127a7a994f8e7d.tar.gz
test for-loops and scope loop variables
-rw-r--r--src/compiler.rs13
-rw-r--r--src/lib.rs1
-rw-r--r--tests/for.tdy19
3 files changed, 31 insertions, 2 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index f84f9f8..b7e946c 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -380,9 +380,14 @@ impl Compiler {
}
}
+ //TODO de-complexify
fn for_loop(&mut self, block: &mut Block) {
expect!(self, Token::For, "Expected 'for' at start of for-loop.");
+ // push outer scope for loop variable
+ self.level += 1;
+ let h = self.stack.len();
+
// Definition
match self.peek_four() {
(Token::Identifier(name), Token::Identifier(typ), Token::ColonEqual, ..) => {
@@ -433,8 +438,12 @@ impl Compiler {
block.patch(Op::JmpFalse(block.curr()), cond_out);
- // Loop variable
- block.add(Op::Pop, self.line());
+ // pop outer scope
+ self.level -= 1;
+ for _ in h..self.stack.len() {
+ block.add(Op::Pop, self.line());
+ }
+ self.stack.truncate(h);
}
fn statement(&mut self, block: &mut Block) {
diff --git a/src/lib.rs b/src/lib.rs
index 53fa0f3..5bef0ae 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -64,4 +64,5 @@ mod tests {
test_file!(variables, "tests/variables.tdy");
test_file!(scoping, "tests/scoping.tdy");
test_file!(if_, "tests/if.tdy");
+ test_file!(for_, "tests/for.tdy");
}
diff --git a/tests/for.tdy b/tests/for.tdy
new file mode 100644
index 0000000..a9f8cd2
--- /dev/null
+++ b/tests/for.tdy
@@ -0,0 +1,19 @@
+a := 0
+for i := 0, i < 3, i = i + 1 {
+ a = a + i
+}
+a <=> 3
+
+a = 0
+for i := 0, i <= 3, i = i + 1 {
+ a = a + i
+}
+a <=> 6
+
+a = 0
+for i := 0, i < 3, i = i + 1 {
+ for j := 0, j < 3, j = j + 1 {
+ a = a + i * j
+ }
+}
+a <=> 9