aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/compiler.rs36
-rw-r--r--tests/simple.tdy30
2 files changed, 59 insertions, 7 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 15d92fd..c542412 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -373,6 +373,38 @@ impl Compiler {
block.patch(Op::JmpFalse(block.curr()), jump);
}
+ self.expression(block);
+ self.scope(block);
+
+ // Loop variable
+ block.add(Op::Pop, self.line());
+ }
+
+ fn for(&mut self, block: &mut Block) {
+ expect!(self, Token::For, "Expected 'for' at start of for-loop.");
+
+ self.level += 1;
+ let h = self.stack.len();
+
+ match self.peek_four() {
+ (Token::Identifier(name), Token::Identifier(typ), Token::ColonEqual, _) => {
+ self.eat();
+ self.eat();
+ self.eat();
+ if let Ok(typ) = Type::try_from(typ.as_ref()) {
+ self.define_variable(&name, typ, block);
+ } else {
+ error!(self, format!("Failed to parse type '{}'.", typ));
+ }
+ }
+
+ (Token::Identifier(name), Token::ColonEqual, _, _) => {
+ self.eat();
+ self.eat();
+ self.define_variable(&name, Type::UnkownType, block);
+ }
+ }
+
}
fn statement(&mut self, block: &mut Block) {
@@ -412,6 +444,10 @@ impl Compiler {
self.if_statment(block);
}
+ (Token::For, _, _, _) => {
+ self.for(block);
+ }
+
(Token::Unreachable, _, _, _) => {
self.eat();
block.add(Op::Unreachable, self.line());
diff --git a/tests/simple.tdy b/tests/simple.tdy
index 3fe05a4..d07aad7 100644
--- a/tests/simple.tdy
+++ b/tests/simple.tdy
@@ -13,12 +13,28 @@
// print d
// print c
-a int := 2
-if a == 0 {
- print "123"
-} else if a == 1 {
- print "BCD"
-} else {
- print "ABC"
+
+for a := 0, a >= 0, a = a - 1 {
+ if a == 2 {
+ continue
+ }
+ print a
+}
+
+for a := 0, a >= 0, a = a - 1 {
+ if a == 2 {
+ continue
+ }
+ print a
+}
+
+a = 0
+
+fiv a in something {
}
+
+something.each((x) -> {
+
+}
+
// 1, 2, 3, 4