Chapter 17: Compiling Expressions done

This commit is contained in:
Moritz Gmeiner 2023-01-31 22:54:12 +01:00
commit 1cca1494a4
20 changed files with 702 additions and 129 deletions

View file

@ -1,11 +1,16 @@
use itertools::Itertools;
use rlox2_frontend::lexer::LexerError;
use rlox2_frontend::lexer::{CodePos, LexerError};
use thiserror::Error;
use crate::{Opcode, Value};
#[derive(Error, Debug)]
pub enum CompileError {}
pub enum CompilerError {
#[error("Missing closing parenthesis at {code_pos}")]
MissingRightParen { code_pos: CodePos },
#[error("{msg}")]
Todo { msg: String },
}
#[derive(Error, Debug)]
pub enum RuntimeError {
@ -18,32 +23,32 @@ pub enum RuntimeError {
}
#[derive(Error, Debug)]
pub enum InterpretError {
pub enum LoxError {
#[error("{0}", format_multiple_errors(inner))]
LexerError { inner: Vec<LexerError> },
#[error("{inner}")]
CompileError { inner: CompileError },
CompileError { inner: CompilerError },
#[error("{inner}")]
RuntimeError { inner: RuntimeError },
#[error("Called exit() with exit code {exit_code}")]
Exit { exit_code: i32 },
}
impl From<Vec<LexerError>> for InterpretError {
impl From<Vec<LexerError>> for LoxError {
fn from(lexer_errs: Vec<LexerError>) -> Self {
InterpretError::LexerError { inner: lexer_errs }
LoxError::LexerError { inner: lexer_errs }
}
}
impl From<CompileError> for InterpretError {
fn from(compile_err: CompileError) -> Self {
InterpretError::CompileError { inner: compile_err }
impl From<CompilerError> for LoxError {
fn from(compile_err: CompilerError) -> Self {
LoxError::CompileError { inner: compile_err }
}
}
impl From<RuntimeError> for InterpretError {
impl From<RuntimeError> for LoxError {
fn from(runtime_err: RuntimeError) -> Self {
InterpretError::RuntimeError { inner: runtime_err }
LoxError::RuntimeError { inner: runtime_err }
}
}