From ebc987f4aaf653c227d21bec5998c62e87da7f7b Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 26 Feb 2021 22:29:33 +0200 Subject: [PATCH] chore(tazjin/rlox): Implement From for bytecode errors Change-Id: I446c6e38cf239a132882d37df156884d319ca111 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2553 Tested-by: BuildkiteCI Reviewed-by: tazjin --- users/tazjin/rlox/src/bytecode/errors.rs | 23 +++++++++++++++++++++-- users/tazjin/rlox/src/bytecode/mod.rs | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/users/tazjin/rlox/src/bytecode/errors.rs b/users/tazjin/rlox/src/bytecode/errors.rs index c7871bc38..513708df7 100644 --- a/users/tazjin/rlox/src/bytecode/errors.rs +++ b/users/tazjin/rlox/src/bytecode/errors.rs @@ -1,15 +1,18 @@ +use crate::scanner::ScannerError; + use std::fmt; #[derive(Debug)] pub enum ErrorKind { - // CompileError, - // RuntimeError, + UnexpectedChar(char), + UnterminatedString, InternalError(&'static str), } #[derive(Debug)] pub struct Error { pub kind: ErrorKind, + pub line: usize, } impl fmt::Display for Error { @@ -18,4 +21,20 @@ impl fmt::Display for Error { } } +impl From for Error { + fn from(err: ScannerError) -> Self { + match err { + ScannerError::UnexpectedChar { line, unexpected } => Error { + line, + kind: ErrorKind::UnexpectedChar(unexpected), + }, + + ScannerError::UnterminatedString { line } => Error { + line, + kind: ErrorKind::UnterminatedString, + }, + } + } +} + pub type LoxResult = Result; diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs index 31278f8c4..34ceaf28b 100644 --- a/users/tazjin/rlox/src/bytecode/mod.rs +++ b/users/tazjin/rlox/src/bytecode/mod.rs @@ -19,7 +19,7 @@ impl crate::Lox for Interpreter { Interpreter {} } - fn interpret(&mut self, _: String) -> Result> { + fn interpret(&mut self, code: String) -> Result> { let chunk: Chunk = Default::default(); vm::interpret(chunk).map_err(|e| vec![e]) }