From e69b782f62577212a79bd15b7345873298337fdd Mon Sep 17 00:00:00 2001 From: ChaseParate Date: Thu, 17 Oct 2024 09:51:57 -0600 Subject: [PATCH 1/2] Make CompilerError enum have tuple variants --- src/compiler/mod.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index b2d0a80..d3a95af 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -16,17 +16,17 @@ use crate::Args; #[derive(Error, Debug)] pub enum CompileError { - Lex { e: lexer::LexError }, - Parse { e: parser::ParseError }, - FileIo { e: std::io::Error }, + Lex(lexer::LexError), + Parse(parser::ParseError), + FileIo(std::io::Error), } impl Display for CompileError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Lex { e } => write!(f, "{}", e), - Self::Parse { e } => write!(f, "{}", e), - Self::FileIo { e } => write!(f, "{}", e), + Self::Lex(e) => write!(f, "{}", e), + Self::Parse(e) => write!(f, "{}", e), + Self::FileIo(e) => write!(f, "{}", e), } } } @@ -40,11 +40,11 @@ impl Display for CompileError { pub fn compile(input_file: String, args: Args) -> Result { let source = match fs::read_to_string(format!("{}.i", input_file)) { Ok(s) => s, - Err(e) => return Err(CompileError::FileIo { e }), + Err(e) => return Err(CompileError::FileIo(e)), }; let tokens = match tokenize(source) { - Err(e) => return Err(CompileError::Lex { e }), + Err(e) => return Err(CompileError::Lex(e)), Ok(ts) => { if args.lex { ts.into_iter().for_each(|t| println!("TOKEN!!! {}", t)); @@ -56,7 +56,7 @@ pub fn compile(input_file: String, args: Args) -> Result { }; let c_ast = match parse(tokens) { - Err(e) => return Err(CompileError::Parse { e }), + Err(e) => return Err(CompileError::Parse(e)), Ok(ast) => { if args.parse { println!("VALID AST RETURNED: {}", ast); @@ -76,7 +76,7 @@ pub fn compile(input_file: String, args: Args) -> Result { return Ok(String::from("magic words")); } if let Err(e) = emit_asm(asm_ast, format!("{}.s", input_file)) { - return Err(CompileError::FileIo { e }); + return Err(CompileError::FileIo(e)); } Ok(format!("{}.s", input_file)) From f428244eb320ba5d0e54cd28a90b27457f53060a Mon Sep 17 00:00:00 2001 From: ChaseParate Date: Thu, 17 Oct 2024 09:59:53 -0600 Subject: [PATCH 2/2] Refactor CompilerError handling --- src/compiler/mod.rs | 50 ++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index d3a95af..9cf5410 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -16,9 +16,9 @@ use crate::Args; #[derive(Error, Debug)] pub enum CompileError { - Lex(lexer::LexError), - Parse(parser::ParseError), - FileIo(std::io::Error), + Lex(#[from] lexer::LexError), + Parse(#[from] parser::ParseError), + FileIo(#[from] std::io::Error), } impl Display for CompileError { @@ -38,46 +38,32 @@ impl Display for CompileError { /// - p: bool, stop after parsing /// - c: bool, stop after assembly code generation pub fn compile(input_file: String, args: Args) -> Result { - let source = match fs::read_to_string(format!("{}.i", input_file)) { - Ok(s) => s, - Err(e) => return Err(CompileError::FileIo(e)), - }; + let source = fs::read_to_string(format!("{}.i", input_file))?; - let tokens = match tokenize(source) { - Err(e) => return Err(CompileError::Lex(e)), - Ok(ts) => { - if args.lex { - ts.into_iter().for_each(|t| println!("TOKEN!!! {}", t)); - return Ok(String::from("magic words")); - } else { - ts - } - } - }; + let tokens = tokenize(source)?; + if args.lex { + tokens.into_iter().for_each(|t| println!("TOKEN!!! {}", t)); + return Ok(String::from("magic words")); + } + + let c_ast = parse(tokens)?; + if args.parse { + println!("VALID AST RETURNED: {}", c_ast); + return Ok(String::from("magic words")); + } - let c_ast = match parse(tokens) { - Err(e) => return Err(CompileError::Parse(e)), - Ok(ast) => { - if args.parse { - println!("VALID AST RETURNED: {}", ast); - return Ok(String::from("magic words")); - } else { - ast - } - } - }; let tacky = tacky::TackyEmitter::gen_tacky(c_ast); if args.tacky { return Ok(String::from("magic words")); } + let asm_ast = gen_asm(tacky); if args.codegen { println!("GENERATED ASSEMBLY: {}", asm_ast); return Ok(String::from("magic words")); } - if let Err(e) = emit_asm(asm_ast, format!("{}.s", input_file)) { - return Err(CompileError::FileIo(e)); - } + + emit_asm(asm_ast, format!("{}.s", input_file))?; Ok(format!("{}.s", input_file)) }