chore(tazjin/rlox): Implement From<ScannerError> for bytecode errors

Change-Id: I446c6e38cf239a132882d37df156884d319ca111
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2553
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Vincent Ambo 2021-02-26 22:29:33 +02:00 committed by tazjin
parent 2974d4b4b6
commit ebc987f4aa
2 changed files with 22 additions and 3 deletions

View file

@ -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<ScannerError> 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<T> = Result<T, Error>;

View file

@ -19,7 +19,7 @@ impl crate::Lox for Interpreter {
Interpreter {}
}
fn interpret(&mut self, _: String) -> Result<Self::Value, Vec<Self::Error>> {
fn interpret(&mut self, code: String) -> Result<Self::Value, Vec<Self::Error>> {
let chunk: Chunk = Default::default();
vm::interpret(chunk).map_err(|e| vec![e])
}