Skip to content

Commit

Permalink
Implement synchronization.
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawal-d committed Apr 15, 2024
1 parent 8d3317a commit 42a68e9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions compiler/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum Opcode {
True,
False,
Greater,
Pop,
Equal,
Less,
}
Expand Down
38 changes: 38 additions & 0 deletions compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ impl Parser {
self.error_at_current(current_source.as_ref());
}
}

fn synchronize(&mut self) {
self.panic_mode = false;

while self.current.typ != TokenType::EOF {
if self.previous.typ == TokenType::Semicolon {
return;
}

match self.current.typ {
TokenType::Class
| TokenType::Fun
| TokenType::Var
| TokenType::For
| TokenType::If
| TokenType::While
| TokenType::Print
| TokenType::Return => {
return;
}
_ => {}
}

self.advance()
}
}
}

pub struct Compiler<'src> {
Expand Down Expand Up @@ -271,6 +297,12 @@ impl<'src> Compiler<'src> {
self.parse_precedence(Precedence::Assignment);
}

fn expression_statement(&mut self) {
self.expression();
self.parser.consume(TokenType::Semicolon, "Expect ';' after expression");
self.emit_byte(Opcode::Pop as u8);
}

fn print_statement(&mut self) {
self.expression();
self.parser.consume(TokenType::Semicolon, "Expect ';' after expression");
Expand All @@ -279,11 +311,17 @@ impl<'src> Compiler<'src> {

fn declaration(&mut self) {
self.statement();

if self.parser.panic_mode {
self.parser.synchronize();
}
}

fn statement(&mut self) {
if self.parser.match_tt(TokenType::Print) {
self.print_statement();
} else {
self.expression_statement();
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn disassemble_instruction(chunk: &Chunk, offset: usize, interner: &Interner
| Opcode::Greater
| Opcode::Less
| Opcode::Print
| Opcode::Pop
| Opcode::Not => simple_instruction(chunk, instruction, offset),
};

Expand Down
15 changes: 9 additions & 6 deletions compiler/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,16 @@ impl<'src> Vm<'src> {
}
}
}
Opcode::False => vm.stack.push(Bool(false)),
Opcode::True => vm.stack.push(Bool(true)),
Opcode::False => vm.stack.push(Bool(false)),
Opcode::Pop => {
vm.pop()?;
}
Opcode::Equal => {
let a = vm.pop()?;
let b = vm.pop()?;
vm.stack.push(Bool(a == b))
}
Opcode::Nil => vm.stack.push(Nil),
Opcode::Add => {
let b = vm.pop()?;
Expand Down Expand Up @@ -150,11 +158,6 @@ impl<'src> Vm<'src> {
let val = vm.pop()?;
vm.stack.push(Bool(vm.is_falsey(val)))
}
Opcode::Equal => {
let a = vm.pop()?;
let b = vm.pop()?;
vm.stack.push(Bool(a == b))
}
Opcode::Greater => binop!(vm, Bool, >),
Opcode::Less => binop!(vm, Bool, <),
}
Expand Down

0 comments on commit 42a68e9

Please sign in to comment.