diff options
| author | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-01-11 15:13:52 +0100 |
|---|---|---|
| committer | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-01-11 15:13:52 +0100 |
| commit | 3a488562804af56f5df47e887a884b80acaa0f81 (patch) | |
| tree | aa908c588e458734ba02d66168695098906859a6 /src | |
| parent | b81e250fab7f54838a9b205e51a49bd5b932c618 (diff) | |
| download | sylt-3a488562804af56f5df47e887a884b80acaa0f81.tar.gz | |
Unreachable statement
Diffstat (limited to 'src')
| -rw-r--r-- | src/compiler.rs | 8 | ||||
| -rw-r--r-- | src/error.rs | 4 | ||||
| -rw-r--r-- | src/vm.rs | 7 |
3 files changed, 17 insertions, 2 deletions
diff --git a/src/compiler.rs b/src/compiler.rs index 4bd519d..94bc0cf 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -354,10 +354,11 @@ impl Compiler { self.scope(block); if Token::Else == self.peek() { + self.eat(); + let else_jmp = block.add(Op::Illegal, self.line()); block.patch(Op::JmpFalse(block.curr()), jump); - self.eat(); match self.peek() { Token::If => self.if_statment(block), Token::LeftBrace => self.scope(block), @@ -407,6 +408,11 @@ impl Compiler { self.if_statment(block); } + (Token::Unreachable, _, _, _) => { + self.eat(); + block.add(Op::Unreachable, self.line()); + } + (Token::LeftBrace, _, _, _) => { self.scope(block); } diff --git a/src/error.rs b/src/error.rs index d79a5fb..abf8810 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,6 +8,7 @@ pub enum ErrorKind { TypeError(Op, Vec<Value>), AssertFailed(Value, Value), InvalidProgram, + Unreachable, SyntaxError(usize, Token), } @@ -35,6 +36,9 @@ impl fmt::Display for ErrorKind { ErrorKind::SyntaxError(line, token) => { write!(f, "Syntax error on line {} at token {:?}", line, token) } + ErrorKind::Unreachable => { + write!(f, "Reached unreachable code.") + } ErrorKind::InvalidProgram => { write!(f, "[!!!] Invalid program") } @@ -44,6 +44,7 @@ pub enum Op { Greater, // > AssertEqual, + Unreachable, ReadLocal(usize), Assign(usize), @@ -148,7 +149,7 @@ impl VM { } pub fn run(&mut self) -> Result<(), Error>{ - const PRINT_WHILE_RUNNING: bool = true; + const PRINT_WHILE_RUNNING: bool = false; const PRINT_BLOCK: bool = true; if PRINT_BLOCK { @@ -176,6 +177,10 @@ impl VM { error!(self, ErrorKind::InvalidProgram); } + Op::Unreachable => { + error!(self, ErrorKind::Unreachable); + } + Op::Pop => { self.stack.pop(); } |
