Skip to content

Commit

Permalink
Port to use ErrorInner
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest1338 committed Jan 21, 2025
1 parent d0183f8 commit 7f7bccf
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 99 deletions.
57 changes: 34 additions & 23 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(dead_code)]

use crate::{
parser::{type_check, Ast, BinExpr, FuncCall, Variable, VariableDeclaration},
utils::{dbg, dbg_plain, escape_string, get_tmp_fname, measure_time, ErrorType},
parser::{type_check, Ast, BinExpr, FuncCall, Variable, VariableDeclaration, expr_to_line_number},
utils::{dbg, dbg_plain, escape_string, get_tmp_fname, measure_time, ErrorInner, ErrorType},
Expr,
};
use std::{
Expand Down Expand Up @@ -68,10 +68,12 @@ impl Compiler {

/// Retrieves a variable by its identifier, returning it or exiting with an error if not found
fn get_var(&self, ident: &str) -> Result<Variable, ErrorType> {
self.variables
.get(ident)
.cloned()
.ok_or_else(|| ErrorType::SyntaxError(format!("Variable doesn't exist: `{}`", ident)))
let err = ErrorType::SyntaxError(ErrorInner {
message: format!("Variable doesn't exist: `{ident}`"),
line_number: None,
});

self.variables.get(ident).cloned().ok_or(err)
}

/// Increments and returns the primary key, used for generating unique variable labels
Expand All @@ -90,10 +92,10 @@ impl Compiler {
match func_call.name.as_ref() {
"print" => self.handle_print(func_call)?,
_ => {
return Err(ErrorType::Generic(format!(
"Function `{}` is not implemented",
func_call.name
)))
return Err(ErrorType::Generic(ErrorInner {
message: format!("Function `{}` is not implemented", func_call.name),
line_number: None,
}))
}
}

Expand Down Expand Up @@ -141,7 +143,10 @@ impl Compiler {
}

_ => {
return Err(ErrorType::Generic("Invalid argument to print".to_string()));
return Err(ErrorType::Generic(ErrorInner {
message: "Invalid argument to print".to_string(),
line_number: None,
}));
}
}

Expand Down Expand Up @@ -178,9 +183,10 @@ impl Compiler {

Expr::BinExpr(bin_expr) => self.handle_bin_expr(bin_expr),

_ => Err(ErrorType::Generic(
"Cannot add variable which is not a number".to_string(),
)),
_ => Err(ErrorType::Generic(ErrorInner {
message: "Cannot add variable which is not a number".to_string(),
line_number: None,
})),
}
}

Expand Down Expand Up @@ -209,9 +215,10 @@ impl Compiler {

if let Some(var_type) = &variable_declaration.typ {
if !type_check(var_type, &variable_declaration.value) {
return Err(ErrorType::Generic(format!(
"Variable type `{var_type}` does not match value type",
)));
return Err(ErrorType::Generic(ErrorInner {
message: format!("Variable type `{var_type}` does not match value type",),
line_number: None,
}));
}
}

Expand Down Expand Up @@ -243,9 +250,10 @@ impl Compiler {
}

_ => {
return Err(ErrorType::Generic(
"Can only store strings and numbers in variables".to_string(),
));
return Err(ErrorType::Generic(ErrorInner {
message: "Can only store strings and numbers in variables".to_string(),
line_number: None,
}));
}
};

Expand All @@ -270,9 +278,12 @@ impl Compiler {
}

_ => {
return Err(ErrorType::Generic(format!(
"Expression `{node:?}` in this context is not yet implemented"
)));
return Err(ErrorType::Generic(ErrorInner {
message: format!(
"Expression `{node:?}` in this context is not yet implemented"
),
line_number: None,
}));
}
}
}
Expand Down
61 changes: 37 additions & 24 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
parser::{type_check, Ast, BinExpr, BinOpKind, FuncCall, Variable, VariableDeclaration},
utils::ErrorType,
utils::{ErrorInner, ErrorType},
Expr,
};
use std::{collections::HashMap, fmt};
Expand Down Expand Up @@ -57,9 +57,12 @@ impl Interpreter {
println!("{var}");
}
_ => {
return Err(ErrorType::Generic(format!(
"Expression `{node:?}` in this context is not yet implemented"
)))
return Err(ErrorType::Generic(ErrorInner {
message: format!(
"Expression `{node:?}` in this context is not yet implemented"
),
line_number: None,
}))
}
}
}
Expand All @@ -74,9 +77,10 @@ impl Interpreter {
return Ok(s.clone());
}
}
Err(ErrorType::SyntaxError(format!(
"Variable doesn't exist: `{ident}`"
)))
Err(ErrorType::SyntaxError(ErrorInner {
message: format!("Variable doesn't exist: `{ident}`"),
line_number: None,
}))
}

/// Evaluates an operand
Expand All @@ -86,13 +90,15 @@ impl Interpreter {
Expr::Number(n) => Ok(*n),
Expr::Identifier(id) => match self.get_var(id)? {
Variable::Number(n) => Ok(n),
_ => Err(ErrorType::Generic(
"Cannot add variable which is not a number".to_string(),
)),
_ => Err(ErrorType::Generic(ErrorInner {
message: "Cannot add variable which is not a number".to_string(),
line_number: None,
})),
},
_ => Err(ErrorType::Generic(
"Cannot add variable which is not a number".to_string(),
)),
_ => Err(ErrorType::Generic(ErrorInner {
message: "Cannot add variable which is not a number".to_string(),
line_number: None,
})),
}
}

Expand All @@ -115,10 +121,10 @@ impl Interpreter {
"print" => self.handle_print(func_call)?,
_ => {
// TODO: handle user defined functions
return Err(ErrorType::Generic(format!(
"Function `{}` is not implemented",
&func_call.name
)));
return Err(ErrorType::Generic(ErrorInner {
message: format!("Function `{}` is not implemented", &func_call.name),
line_number: None,
}));
}
}

Expand All @@ -136,7 +142,12 @@ impl Interpreter {
Expr::Number(n) => print!("{n}"),
Expr::Identifier(id) => print!("{}", self.get_var(id)?),
Expr::StringLiteral(s) => print!("{s}"),
_ => return Err(ErrorType::Generic("Invalid argument to print".to_string())),
_ => {
return Err(ErrorType::Generic(ErrorInner {
message: "Invalid argument to print".to_string(),
line_number: None,
}))
}
}
if i != args_count - 1 {
print!(" ");
Expand All @@ -156,9 +167,10 @@ impl Interpreter {
) -> Result<(), ErrorType> {
if let Some(var_type) = &variable_declaration.typ {
if !type_check(var_type, &variable_declaration.value) {
return Err(ErrorType::Generic(format!(
"Variable type `{var_type}` does not match value type",
)));
return Err(ErrorType::Generic(ErrorInner {
message: format!("Variable type `{var_type}` does not match value type",),
line_number: None,
}));
}
}

Expand All @@ -169,9 +181,10 @@ impl Interpreter {
Expr::StringLiteral(s) => Variable::StringLiteral(s.to_owned()),
Expr::BinExpr(bin_expr) => Variable::Number(self.handle_bin_expr(bin_expr)?),
_ => {
return Err(ErrorType::Generic(
"Can only store strings and number in variables".to_string(),
));
return Err(ErrorType::Generic(ErrorInner {
message: "Can only store strings and number in variables".to_string(),
line_number: None,
}));
}
},
);
Expand Down
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
compiler::Compiler,
interpreter::Interpreter,
parser::expr_to_line_number,
utils::{display_error, display_error_stdout, ErrorType},
utils::{display_error, display_error_stdout, ErrorInner, ErrorType},
};
use std::{
fs::{canonicalize, read_to_string},
Expand Down Expand Up @@ -83,15 +83,15 @@ fn main() {
let tokens = match lexer(&code) {
Ok(tokens) => tokens,
Err(err) => {
display_error_stdout(ErrorType::Generic(err));
display_error_stdout(err);
continue;
}
};
let mut parser = Parser::new(&tokens);
let ast = match parser.parse() {
Ok(ast) => ast,
Err(err) => {
display_error_stdout(ErrorType::Generic(err));
display_error_stdout(err);
continue;
}
};
Expand All @@ -114,10 +114,10 @@ fn main() {
Some(input) => match read_to_string(input) {
Ok(input) => input,
Err(_) => {
display_error(
ErrorType::Generic("Could not read source code file".to_string()),
Some(1),
);
display_error(ErrorType::Generic(ErrorInner {
message: "Could not read source code file".to_string(),
line_number: None,
}));
exit(1);
}
},
Expand All @@ -135,7 +135,7 @@ fn main() {
let tokens = measure_time("Lexical Analysis", || match lexer(&source_code) {
Ok(tokens) => tokens,
Err(err) => {
display_error(ErrorType::SyntaxError(err), Some(1));
display_error(err);
exit(1);
}
});
Expand All @@ -148,7 +148,7 @@ fn main() {
let ast = measure_time("Parsing", || match parser.parse() {
Ok(ast) => ast,
Err(err) => {
display_error(ErrorType::SyntaxError(err), Some(1));
display_error(err);
exit(1);
}
});
Expand All @@ -161,7 +161,7 @@ fn main() {
let mut interpreter = Interpreter::from_ast(ast);
measure_time("Interpreter Execution", || {
if let Err(err) = interpreter.run() {
display_error(err, Some(1));
display_error(err);
exit(1);
}
});
Expand All @@ -172,7 +172,7 @@ fn main() {
let mut compiler = Compiler::from_ast(ast);
measure_time("Full Compiler Execution", || {
if let Err(err) = compiler.compile(args.output.clone()) {
display_error(err, Some(1));
display_error(err);
exit(1);
}
});
Expand All @@ -193,7 +193,7 @@ fn main() {
let mut compiler = Compiler::from_ast(ast);
measure_time("Full Compiler Execution", || {
if let Err(err) = compiler.compile(args.output) {
display_error(err, Some(1));
display_error(err);
exit(1);
}
});
Expand Down
Loading

0 comments on commit 7f7bccf

Please sign in to comment.