Skip to content

Commit

Permalink
Use From<String> instead of errstr_to_errtype function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest1338 committed Jan 27, 2025
1 parent 9d1d52a commit 74ef5cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::{
args::AppArgs,
parser::{type_check, Ast, BinExpr, FuncCall, Variable, VariableDeclaration},
utils::{
dbg, dbg_file_if_env, dbg_plain, errstr_to_errtype, escape_string, get_tmp_fname,
measure_time, ErrorType,
dbg, dbg_file_if_env, dbg_plain, escape_string, get_tmp_fname, measure_time, ErrorType,
},
Expr,
};
Expand Down Expand Up @@ -273,10 +272,10 @@ impl Compiler {

for node in &ast {
match node {
Expr::FuncCall(func_call) => errstr_to_errtype(self.handle_func_call(func_call))?,
Expr::FuncCall(func_call) => self.handle_func_call(func_call)?,

Expr::VariableDeclaration(variable_declaration) => {
errstr_to_errtype(self.handle_var_decl(variable_declaration))?
self.handle_var_decl(variable_declaration)?
}

_ => {
Expand Down
8 changes: 3 additions & 5 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::{errstr_to_errtype, ErrorType},
utils::ErrorType,
Expr,
};
use std::{collections::HashMap, fmt};
Expand Down Expand Up @@ -45,10 +45,8 @@ impl Interpreter {

for node in &ast {
match node {
Expr::FuncCall(func_call) => errstr_to_errtype(self.handle_func_call(func_call))?,
Expr::VariableDeclaration(variable_declaration) => {
errstr_to_errtype(self.handle_var_decl(variable_declaration))?
}
Expr::FuncCall(func_call) => self.handle_func_call(func_call)?,
Expr::VariableDeclaration(var_decl) => self.handle_var_decl(var_decl)?,
Expr::Identifier(id) => {
// If it's a valid variable, print it
// Probably only useful in the interactive mode
Expand Down
9 changes: 4 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,10 @@ pub fn escape_string(s: &str) -> String {
s.replace("\\", "\\\\").replace("\"", "\\\"")
}

/// TODO
pub fn errstr_to_errtype<T>(result: Result<T, String>) -> Result<T, ErrorType> {
match result {
Ok(value) => Ok(value),
Err(message) => Err(ErrorType::Generic(message)),
impl From<String> for ErrorType {
/// TODO
fn from(message: String) -> Self {
ErrorType::Generic(message)
}
}

Expand Down

0 comments on commit 74ef5cc

Please sign in to comment.