diff options
| -rw-r--r-- | src/compiler.rs | 27 | ||||
| -rw-r--r-- | tests/simple.tdy | 3 |
2 files changed, 24 insertions, 6 deletions
diff --git a/src/compiler.rs b/src/compiler.rs index 218711c..5808abe 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -198,6 +198,27 @@ impl Compiler { } } + fn statement(&mut self, block: &mut Block) { + match self.peek() { + Token::Print => { + self.eat(); + self.expression(block); + block.add(Op::Print, self.line()); + if self.eat() != Token::Newline { + self.error("Expect newline after expression."); + } + }, + + _ => { + self.expression(block); + if self.eat() != Token::Newline { + self.error("Expect newline after expression."); + } + block.add(Op::Pop, None); + } + } + } + pub fn compile(&mut self, name: &str, file: &Path) -> Block { let mut block = Block::new(name, file); @@ -206,12 +227,8 @@ impl Compiler { break; } - self.expression(&mut block); - block.add(Op::Print, self.line()); + self.statement(&mut block); - if self.eat() != Token::Newline { - self.error("Invalid expression"); - } } block.add(Op::Return, self.line()); diff --git a/tests/simple.tdy b/tests/simple.tdy index ec50e91..d38c6af 100644 --- a/tests/simple.tdy +++ b/tests/simple.tdy @@ -1,3 +1,4 @@ 1 + 1 <=> 2 // asdlkjasl -1 + 2 <=> 2 +print 1 + 3 +// OwO |
