Properly handle errors in compilation pipeline
This commit is contained in:
parent
a219faf283
commit
a39b0c0d5a
@ -35,8 +35,12 @@ pub fn compile_file(input: &String) -> Result<(), ()> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(out_code) = compile(&contents) else {
|
let out_code = match compile(&contents) {
|
||||||
return Err(());
|
Ok(out_code) => out_code,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("{}", error);
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut output_path = Path::new(input)
|
let mut output_path = Path::new(input)
|
||||||
@ -54,44 +58,41 @@ pub fn compile_file(input: &String) -> Result<(), ()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes Lexical analysis, handles errors and calls build_ast for the next phase
|
/// THP source code goes in, PHP code or an error comes out
|
||||||
fn compile(input: &String) -> Option<String> {
|
fn compile(input: &String) -> Result<String, String> {
|
||||||
let tokens = lexic::get_tokens(input);
|
let tokens = lexic::get_tokens(input);
|
||||||
|
|
||||||
match tokens {
|
let tokens = match tokens {
|
||||||
Ok(tokens) => Some(build_ast(input, tokens)),
|
Ok(tokens) => tokens,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().into_iter().collect();
|
||||||
println!(
|
return Err(format!(
|
||||||
"{}:\n{}",
|
"{}:\n{}",
|
||||||
"syntax error".on_red(),
|
"syntax error".on_red(),
|
||||||
error.get_error_str(&chars)
|
error.get_error_str(&chars)
|
||||||
);
|
))
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
build_ast(input, tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes Syntax analysis, and for now, Semantic analysis and Code generation.
|
/// Executes Syntax analysis, and for now, Semantic analysis and Code generation.
|
||||||
///
|
///
|
||||||
/// Prints the generated code in stdin
|
/// Prints the generated code in stdin
|
||||||
fn build_ast(input: &String, tokens: Vec<Token>) -> String {
|
fn build_ast(input: &String, tokens: Vec<Token>) -> Result<String, String> {
|
||||||
let ast = syntax::construct_ast(&tokens);
|
let ast = syntax::construct_ast(&tokens);
|
||||||
|
|
||||||
match ast {
|
let ast = match ast {
|
||||||
Ok(ast) => {
|
Ok(ast) => ast,
|
||||||
match crate::semantic::check_semantics(&ast) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(reason) => {
|
|
||||||
panic!("{}", reason)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
codegen::codegen(&ast)
|
|
||||||
}
|
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
let chars: Vec<char> = input.chars().into_iter().collect();
|
let chars: Vec<char> = input.chars().into_iter().collect();
|
||||||
panic!("{}", reason.get_error_str(&chars))
|
let error = format!("{}: {}", "error".on_red(), reason.get_error_str(&chars));
|
||||||
|
return Err(error)
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
crate::semantic::check_semantics(&ast)?;
|
||||||
|
|
||||||
|
Ok(codegen::codegen(&ast))
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ mod utils;
|
|||||||
|
|
||||||
mod error_handling;
|
mod error_handling;
|
||||||
|
|
||||||
fn main() -> Result<(), ()> {
|
fn main() {
|
||||||
cli::run_cli()
|
match cli::run_cli() {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_) => std::process::exit(1),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user