From 995d024f03d99726de5b22bdf5aaa04a88e00b4a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 28 Feb 2021 01:13:52 +0200 Subject: feat(tazjin/rlox): Wire up bytecode interpreter & print results This makes the bytecode interpreter actually usable. Change-Id: I24afc7ce461c6673dc42581378f6e14da7aece5c Reviewed-on: https://cl.tvl.fyi/c/depot/+/2566 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/compiler.rs | 287 +++++++++++++++++++++++++ users/tazjin/rlox/src/bytecode/compiler/mod.rs | 287 ------------------------- users/tazjin/rlox/src/bytecode/mod.rs | 11 - users/tazjin/rlox/src/main.rs | 11 +- 4 files changed, 294 insertions(+), 302 deletions(-) create mode 100644 users/tazjin/rlox/src/bytecode/compiler.rs delete mode 100644 users/tazjin/rlox/src/bytecode/compiler/mod.rs (limited to 'users/tazjin/rlox') diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs new file mode 100644 index 000000000000..ca56bfe7cf50 --- /dev/null +++ b/users/tazjin/rlox/src/bytecode/compiler.rs @@ -0,0 +1,287 @@ +use super::chunk::{self, Chunk}; +use super::errors::{Error, ErrorKind, LoxResult}; +use super::opcode::OpCode; +use super::value::Value; +use crate::scanner::{self, Token, TokenKind}; + +struct Compiler> { + tokens: T, + chunk: Chunk, + panic: bool, + errors: Vec, + + // TODO(tazjin): Restructure so that these don't need to be Option? + current: Option, + previous: Option, +} + +#[derive(Debug, PartialEq, PartialOrd)] +enum Precedence { + None, + Assignment, // = + Or, // or + And, // and + Equality, // == != + Comparison, // < > <= >= + Term, // + - + Factor, // * / + Unary, // ! - + Call, // . () + Primary, +} + +type ParseFn = fn(&mut Compiler) -> LoxResult<()>; + +struct ParseRule> { + prefix: Option>, + infix: Option>, + precedence: Precedence, +} + +impl> ParseRule { + fn new( + prefix: Option>, + infix: Option>, + precedence: Precedence, + ) -> Self { + ParseRule { + prefix, + infix, + precedence, + } + } +} + +impl Precedence { + // Return the next highest precedence, if there is one. + fn next(&self) -> Self { + match self { + Precedence::None => Precedence::Assignment, + Precedence::Assignment => Precedence::Or, + Precedence::Or => Precedence::And, + Precedence::And => Precedence::Equality, + Precedence::Equality => Precedence::Comparison, + Precedence::Comparison => Precedence::Term, + Precedence::Term => Precedence::Factor, + Precedence::Factor => Precedence::Unary, + Precedence::Unary => Precedence::Call, + Precedence::Call => Precedence::Primary, + Precedence::Primary => panic!( + "invalid parser state: no higher precedence than Primary" + ), + } + } +} + +fn rule_for>(token: &TokenKind) -> ParseRule { + match token { + TokenKind::LeftParen => { + ParseRule::new(Some(Compiler::grouping), None, Precedence::None) + } + + TokenKind::Minus => ParseRule::new( + Some(Compiler::unary), + Some(Compiler::binary), + Precedence::Term, + ), + + TokenKind::Plus => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Term) + } + + TokenKind::Slash => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Factor) + } + + TokenKind::Star => { + ParseRule::new(None, Some(Compiler::binary), Precedence::Factor) + } + + TokenKind::Number(_) => { + ParseRule::new(Some(Compiler::number), None, Precedence::None) + } + + _ => ParseRule::new(None, None, Precedence::None), + } +} + +impl> Compiler { + fn compile(&mut self) -> LoxResult<()> { + self.advance(); + self.expression()?; + self.consume( + &TokenKind::Eof, + ErrorKind::ExpectedToken("Expected end of expression"), + ); + + self.end_compiler() + } + + fn advance(&mut self) { + self.previous = self.current.take(); + self.current = self.tokens.next(); + } + + fn expression(&mut self) -> LoxResult<()> { + self.parse_precedence(Precedence::Assignment) + } + + fn number(&mut self) -> LoxResult<()> { + if let TokenKind::Number(num) = self.previous().kind { + self.emit_constant(num); + return Ok(()); + } + + unreachable!("internal parser error: entered number() incorrectly") + } + + fn grouping(&mut self) -> LoxResult<()> { + self.expression()?; + self.consume( + &TokenKind::RightParen, + ErrorKind::ExpectedToken("Expected ')' after expression"), + ); + Ok(()) + } + + fn unary(&mut self) -> LoxResult<()> { + // TODO(tazjin): Avoid clone + let kind = self.previous().kind.clone(); + + // Compile the operand + self.parse_precedence(Precedence::Unary)?; + + // Emit operator instruction + match kind { + TokenKind::Minus => self.emit_op(OpCode::OpNegate), + _ => unreachable!("only called for unary operator tokens"), + } + + Ok(()) + } + + fn binary(&mut self) -> LoxResult<()> { + // Remember the operator + let operator = self.previous().kind.clone(); + + // Compile the right operand + let rule: ParseRule = rule_for(&operator); + self.parse_precedence(rule.precedence)?; + + // Emit operator instruction + match operator { + TokenKind::Minus => self.emit_op(OpCode::OpSubtract), + TokenKind::Plus => self.emit_op(OpCode::OpAdd), + TokenKind::Star => self.emit_op(OpCode::OpMultiply), + TokenKind::Slash => self.emit_op(OpCode::OpDivide), + _ => unreachable!("only called for binary operator tokens"), + } + + Ok(()) + } + + fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> { + self.advance(); + let rule: ParseRule = rule_for(&self.previous().kind); + let prefix_fn = match rule.prefix { + None => unimplemented!("expected expression or something, unclear"), + Some(func) => func, + }; + + prefix_fn(self)?; + + while precedence <= rule_for::(&self.current().kind).precedence { + self.advance(); + match rule_for::(&self.previous().kind).infix { + Some(func) => { + func(self)?; + } + None => { + unreachable!("invalid compiler state: error in parse rules") + } + } + } + + Ok(()) + } + + fn consume(&mut self, expected: &TokenKind, err: ErrorKind) { + if (self.current().kind == *expected) { + self.advance(); + return; + } + + self.error_at(self.current().line, err); + } + + fn current_chunk(&mut self) -> &mut Chunk { + &mut self.chunk + } + + fn end_compiler(&mut self) -> LoxResult<()> { + self.emit_op(OpCode::OpReturn); + + #[cfg(feature = "disassemble")] + { + chunk::disassemble_chunk(&self.chunk); + println!("== compilation finished =="); + } + + Ok(()) + } + + fn emit_op(&mut self, op: OpCode) { + let line = self.previous().line; + self.current_chunk().add_op(op, line); + } + + fn emit_constant(&mut self, val: Value) { + let idx = self.chunk.add_constant(val); + self.emit_op(OpCode::OpConstant(idx)); + } + + fn previous(&self) -> &Token { + self.previous + .as_ref() + .expect("invalid internal compiler state: missing previous token") + } + + fn current(&self) -> &Token { + self.current + .as_ref() + .expect("invalid internal compiler state: missing current token") + } + + fn error_at(&mut self, line: usize, kind: ErrorKind) { + if self.panic { + return; + } + + self.panic = true; + self.errors.push(Error { kind, line }) + } +} + +pub fn compile(code: &str) -> Result> { + let chars = code.chars().collect::>(); + let tokens = scanner::scan(&chars).map_err(|errors| { + errors.into_iter().map(Into::into).collect::>() + })?; + + let mut compiler = Compiler { + tokens: tokens.into_iter().peekable(), + chunk: Default::default(), + panic: false, + errors: vec![], + current: None, + previous: None, + }; + + compiler.compile()?; + + if compiler.errors.is_empty() { + Ok(compiler.chunk) + } else { + Err(compiler.errors) + } +} diff --git a/users/tazjin/rlox/src/bytecode/compiler/mod.rs b/users/tazjin/rlox/src/bytecode/compiler/mod.rs deleted file mode 100644 index 5de8fd9f6f06..000000000000 --- a/users/tazjin/rlox/src/bytecode/compiler/mod.rs +++ /dev/null @@ -1,287 +0,0 @@ -use super::chunk::{self, Chunk}; -use super::errors::{Error, ErrorKind, LoxResult}; -use super::opcode::OpCode; -use super::value::Value; -use crate::scanner::{self, Token, TokenKind}; - -#[cfg(test)] -mod tests; - -struct Compiler> { - tokens: T, - chunk: Chunk, - panic: bool, - errors: Vec, - - // TODO(tazjin): Restructure so that these don't need to be Option? - current: Option, - previous: Option, -} - -#[derive(Debug, PartialEq, PartialOrd)] -enum Precedence { - None, - Assignment, // = - Or, // or - And, // and - Equality, // == != - Comparison, // < > <= >= - Term, // + - - Factor, // * / - Unary, // ! - - Call, // . () - Primary, -} - -type ParseFn = fn(&mut Compiler) -> LoxResult<()>; - -struct ParseRule> { - prefix: Option>, - infix: Option>, - precedence: Precedence, -} - -impl> ParseRule { - fn new( - prefix: Option>, - infix: Option>, - precedence: Precedence, - ) -> Self { - ParseRule { - prefix, - infix, - precedence, - } - } -} - -impl Precedence { - // Return the next highest precedence, if there is one. - fn next(&self) -> Self { - match self { - Precedence::None => Precedence::Assignment, - Precedence::Assignment => Precedence::Or, - Precedence::Or => Precedence::And, - Precedence::And => Precedence::Equality, - Precedence::Equality => Precedence::Comparison, - Precedence::Comparison => Precedence::Term, - Precedence::Term => Precedence::Factor, - Precedence::Factor => Precedence::Unary, - Precedence::Unary => Precedence::Call, - Precedence::Call => Precedence::Primary, - Precedence::Primary => panic!( - "invalid parser state: no higher precedence than Primary" - ), - } - } -} - -fn rule_for>(token: &TokenKind) -> ParseRule { - match token { - TokenKind::LeftParen => { - ParseRule::new(Some(Compiler::grouping), None, Precedence::None) - } - - TokenKind::Minus => ParseRule::new( - Some(Compiler::unary), - Some(Compiler::binary), - Precedence::Term, - ), - - TokenKind::Plus => { - ParseRule::new(None, Some(Compiler::binary), Precedence::Term) - } - - TokenKind::Slash => { - ParseRule::new(None, Some(Compiler::binary), Precedence::Factor) - } - - TokenKind::Star => { - ParseRule::new(None, Some(Compiler::binary), Precedence::Factor) - } - - TokenKind::Number(_) => { - ParseRule::new(Some(Compiler::number), None, Precedence::None) - } - - _ => ParseRule::new(None, None, Precedence::None), - } -} - -impl> Compiler { - fn compile(&mut self) -> LoxResult<()> { - self.advance(); - self.expression()?; - self.consume( - &TokenKind::Eof, - ErrorKind::ExpectedToken("Expected end of expression"), - ); - - self.end_compiler() - } - - fn advance(&mut self) { - self.previous = self.current.take(); - self.current = self.tokens.next(); - } - - fn expression(&mut self) -> LoxResult<()> { - self.parse_precedence(Precedence::Assignment) - } - - fn number(&mut self) -> LoxResult<()> { - if let TokenKind::Number(num) = self.previous().kind { - self.emit_constant(num); - return Ok(()); - } - - unreachable!("internal parser error: entered number() incorrectly") - } - - fn grouping(&mut self) -> LoxResult<()> { - self.expression()?; - self.consume( - &TokenKind::RightParen, - ErrorKind::ExpectedToken("Expected ')' after expression"), - ); - Ok(()) - } - - fn unary(&mut self) -> LoxResult<()> { - // TODO(tazjin): Avoid clone - let kind = self.previous().kind.clone(); - - // Compile the operand - self.parse_precedence(Precedence::Unary)?; - - // Emit operator instruction - match kind { - TokenKind::Minus => self.emit_op(OpCode::OpNegate), - _ => unreachable!("only called for unary operator tokens"), - } - - Ok(()) - } - - fn binary(&mut self) -> LoxResult<()> { - // Remember the operator - let operator = self.previous().kind.clone(); - - // Compile the right operand - let rule: ParseRule = rule_for(&operator); - self.parse_precedence(rule.precedence)?; - - // Emit operator instruction - match operator { - TokenKind::Minus => self.emit_op(OpCode::OpSubtract), - TokenKind::Plus => self.emit_op(OpCode::OpAdd), - TokenKind::Star => self.emit_op(OpCode::OpMultiply), - TokenKind::Slash => self.emit_op(OpCode::OpDivide), - _ => unreachable!("only called for binary operator tokens"), - } - - Ok(()) - } - - fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> { - self.advance(); - let rule: ParseRule = rule_for(&self.previous().kind); - let prefix_fn = match rule.prefix { - None => unimplemented!("expected expression or something, unclear"), - Some(func) => func, - }; - - prefix_fn(self)?; - - while precedence <= rule_for::(&self.current().kind).precedence { - self.advance(); - match rule_for::(&self.previous().kind).infix { - Some(func) => { - func(self)?; - } - None => { - unreachable!("invalid compiler state: error in parse rules") - } - } - } - - Ok(()) - } - - fn consume(&mut self, expected: &TokenKind, err: ErrorKind) { - if (self.current().kind == *expected) { - self.advance(); - return; - } - - self.error_at(self.current().line, err); - } - - fn current_chunk(&mut self) -> &mut Chunk { - &mut self.chunk - } - - fn end_compiler(&mut self) -> LoxResult<()> { - self.emit_op(OpCode::OpReturn); - - #[cfg(feature = "disassemble")] - chunk::disassemble_chunk(&self.chunk); - - Ok(()) - } - - fn emit_op(&mut self, op: OpCode) { - let line = self.previous().line; - self.current_chunk().add_op(op, line); - } - - fn emit_constant(&mut self, val: Value) { - let idx = self.chunk.add_constant(val); - self.emit_op(OpCode::OpConstant(idx)); - } - - fn previous(&self) -> &Token { - self.previous - .as_ref() - .expect("invalid internal compiler state: missing previous token") - } - - fn current(&self) -> &Token { - self.current - .as_ref() - .expect("invalid internal compiler state: missing current token") - } - - fn error_at(&mut self, line: usize, kind: ErrorKind) { - if self.panic { - return; - } - - self.panic = true; - self.errors.push(Error { kind, line }) - } -} - -pub fn compile(code: &str) -> Result> { - let chars = code.chars().collect::>(); - let tokens = scanner::scan(&chars).map_err(|errors| { - errors.into_iter().map(Into::into).collect::>() - })?; - - let mut compiler = Compiler { - tokens: tokens.into_iter().peekable(), - chunk: Default::default(), - panic: false, - errors: vec![], - current: None, - previous: None, - }; - - compiler.compile()?; - - if compiler.errors.is_empty() { - Ok(compiler.chunk) - } else { - Err(compiler.errors) - } -} diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs index 97316e66aa05..0776225bec91 100644 --- a/users/tazjin/rlox/src/bytecode/mod.rs +++ b/users/tazjin/rlox/src/bytecode/mod.rs @@ -28,14 +28,3 @@ impl crate::Lox for Interpreter { vm::interpret(chunk).map_err(|e| vec![e]) } } - -// pub fn main() { -// let mut chunk: Chunk = Default::default(); -// let constant = chunk.add_constant(1.2); -// chunk.add_op(OpCode::OpConstant(constant), 1); -// let constant = chunk.add_constant(2.0); -// chunk.add_op(OpCode::OpConstant(constant), 2); -// chunk.add_op(OpCode::OpAdd, 3); -// chunk.add_op(OpCode::OpReturn, 4); -// vm::interpret(chunk).expect("it should work"); -// } diff --git a/users/tazjin/rlox/src/main.rs b/users/tazjin/rlox/src/main.rs index 2b4e365d4125..2d8cf4f354ea 100644 --- a/users/tazjin/rlox/src/main.rs +++ b/users/tazjin/rlox/src/main.rs @@ -11,7 +11,7 @@ mod treewalk; /// Trait for making the different interpreters callable in the same /// way. pub trait Lox { - type Value; + type Value: std::fmt::Debug; type Error: std::fmt::Display; fn create() -> Self; @@ -69,9 +69,12 @@ fn run_prompt() { } fn run(lox: &mut I, code: String) { - if let Err(errors) = lox.interpret(code) { - for error in errors { - eprintln!("{}", error); + match lox.interpret(code) { + Ok(result) => println!("=> {:?}", result), + Err(errors) => { + for error in errors { + eprintln!("{}", error); + } } } } -- cgit 1.4.1