From d1d8ddb473c314951a9773a988728a135304de8e Mon Sep 17 00:00:00 2001 From: Hanmin Kim Date: Fri, 9 Feb 2024 13:49:42 -0500 Subject: [PATCH] lint(all): lint --- src/back/code_generator/generator.rs | 64 ++++++++------- src/front/mergers/convert.rs | 114 ++++++++++++++++++--------- src/middle/format/ir_types.rs | 8 +- 3 files changed, 116 insertions(+), 70 deletions(-) diff --git a/src/back/code_generator/generator.rs b/src/back/code_generator/generator.rs index 82a40c7..d05cbcb 100644 --- a/src/back/code_generator/generator.rs +++ b/src/back/code_generator/generator.rs @@ -1,11 +1,9 @@ use crate::back::code_generator::{Context, GeneratedCode, MFunction}; +use crate::middle::format::ir_types::AddressOrigin::Const; use crate::middle::format::ir_types::CompareOp; +use crate::middle::format::ir_types::Cond; use crate::middle::format::ir_types::{Address, IrBlock, IrIf, IrStatement}; -use crate::middle::format::ir_types::{ - AddressOrigin, IrScoreOperation, IrScoreOperationType, -}; -use crate::middle::format::ir_types::{Cond}; -use crate::middle::format::ir_types::AddressOrigin::Const; +use crate::middle::format::ir_types::{AddressOrigin, IrScoreOperation, IrScoreOperationType}; static BLASTFURNACE_OBJECTIVE: &str = "blst"; static BLASTFURNACE_CONST: &str = "blst"; @@ -197,10 +195,34 @@ fn if_unless_helper( } "=" } - CompareOp::Lt => if invert { ">" } else { "<" }, - CompareOp::Gt => if invert { "<" } else { ">" }, - CompareOp::Leq => if invert { ">=" } else { "<=" }, - CompareOp::Geq => if invert { "<=" } else { ">=" }, + CompareOp::Lt => { + if invert { + ">" + } else { + "<" + } + } + CompareOp::Gt => { + if invert { + "<" + } else { + ">" + } + } + CompareOp::Leq => { + if invert { + ">=" + } else { + "<=" + } + } + CompareOp::Geq => { + if invert { + "<=" + } else { + ">=" + } + } }; // should be type_ var0 op var1 @@ -208,21 +230,11 @@ fn if_unless_helper( if let Const(c1) = x.var_1.name { if let Const(c0) = x.var_0.name { return if match op { - "=" => { - (c0 == c1 && type_ == "if") || c0 != c1 && type_ == "unless" - } - "<" => { - c0 < c1 - } - ">" => { - c0 > c1 - } - "<=" => { - c0 <= c1 - } - ">=" => { - c0 >= c1 - } + "=" => (c0 == c1 && type_ == "if") || c0 != c1 && type_ == "unless", + "<" => c0 < c1, + ">" => c0 > c1, + "<=" => c0 <= c1, + ">=" => c0 >= c1, _ => { panic!("Invalid op, match arms must be insufficient") } @@ -234,9 +246,7 @@ fn if_unless_helper( } let range = match op { - "=" => { - c1.to_string() - } + "=" => c1.to_string(), "<" => { format!("..{}", c1 - 1) } diff --git a/src/front/mergers/convert.rs b/src/front/mergers/convert.rs index 512836f..c406673 100644 --- a/src/front/mergers/convert.rs +++ b/src/front/mergers/convert.rs @@ -345,7 +345,12 @@ fn convert_var_assign(context: &mut Context, ast_node: &VarAssign) -> Vec, invert_cond: bool, body: IrStatement) -> Vec { +fn convert_condition( + context: &mut Context, + cond: &Box, + invert_cond: bool, + body: IrStatement, +) -> Vec { let mut condition = vec![]; // if condition is 0, return let (mut expr_statements, cond, invert) = convert_expr_for_comparison(context, cond); @@ -412,30 +417,36 @@ fn convert_if(context: &mut Context, ast_node: &If) -> Vec { })); } - // compute block for first if statement let block = IrStatement::Block({ let mut s = convert_block(context, &ast_node.body, true); if elses.len() > 0 { - s.statements.push(IrStatement::ScoreOperation(IrScoreOperation { - left: if_variable.clone(), - op: IrScoreOperationType::Assign, - right: context.const_generator.get_const(0), - })); + s.statements + .push(IrStatement::ScoreOperation(IrScoreOperation { + left: if_variable.clone(), + op: IrScoreOperationType::Assign, + right: context.const_generator.get_const(0), + })); } s }); // write if statement - s.append(&mut convert_condition(context, &ast_node.cond, false, block)); + s.append(&mut convert_condition( + context, + &ast_node.cond, + false, + block, + )); for (condition, body) in elses { let block = IrStatement::Block({ let mut s = convert_block(context, &body, true); - s.statements.push(IrStatement::ScoreOperation(IrScoreOperation { - left: if_variable.clone(), - op: IrScoreOperationType::Assign, - right: context.const_generator.get_const(0), - })); + s.statements + .push(IrStatement::ScoreOperation(IrScoreOperation { + left: if_variable.clone(), + op: IrScoreOperationType::Assign, + right: context.const_generator.get_const(0), + })); s }); @@ -488,7 +499,12 @@ fn convert_for(context: &mut Context, ast_node: &For) -> Vec { let mut condition = vec![]; if let Some(cond) = &ast_node.cond { - condition.append(&mut convert_condition(context, cond, true, IrStatement::Return)); + condition.append(&mut convert_condition( + context, + cond, + true, + IrStatement::Return, + )); } // parse body @@ -573,15 +589,19 @@ mod tests { use crate::front::file_system::fs::FileSystem; use crate::front::file_system::mock_fs::MockFileSystem; use crate::front::mergers::program::ProgramMerger; + use crate::middle::format::ir_types::IrFnDef; use crate::middle::format::ir_types::{ Address, AddressOrigin, CompareOp, Cond, IrScoreOperationType, IrStatement, }; + use crate::middle::format::types::GlobalName; use std::collections::HashMap; use std::ops::Deref; - use crate::middle::format::ir_types::IrFnDef; - use crate::middle::format::types::GlobalName; - fn test_calculation(run_function: &str, functions: &HashMap, result_address: &Address) -> i32 { + fn test_calculation( + run_function: &str, + functions: &HashMap, + result_address: &Address, + ) -> i32 { struct Vars { var_map: HashMap, } @@ -605,7 +625,12 @@ mod tests { } } - fn run_statements(curr_fn_name: &str, statements: &Vec, vars_ref: &mut Vars, functions: &HashMap) -> bool { + fn run_statements( + curr_fn_name: &str, + statements: &Vec, + vars_ref: &mut Vars, + functions: &HashMap, + ) -> bool { for statement in statements { match statement { IrStatement::ScoreOperation(x) => { @@ -636,7 +661,12 @@ mod tests { Cond::CheckVal(y) => { let a = vars_ref.get(&y.var_name); if (y.min <= a && a <= y.max) != x.invert { - run_statements(curr_fn_name, &vec![(*x.body.deref()).clone()], vars_ref, functions); + run_statements( + curr_fn_name, + &vec![(*x.body.deref()).clone()], + vars_ref, + functions, + ); } } Cond::CompareVal(y) => { @@ -651,7 +681,12 @@ mod tests { CompareOp::Geq => a >= b, } != x.invert { - if run_statements(curr_fn_name, &vec![(*x.body.deref()).clone()], vars_ref, functions) { + if run_statements( + curr_fn_name, + &vec![(*x.body.deref()).clone()], + vars_ref, + functions, + ) { return false; } } @@ -667,7 +702,12 @@ mod tests { if &x.fn_name == curr_fn_name { run_statements(&x.fn_name, statements, vars_ref, functions); } else { - run_statements(&x.fn_name, &functions.get(&x.fn_name).unwrap().statements, vars_ref, functions); + run_statements( + &x.fn_name, + &functions.get(&x.fn_name).unwrap().statements, + vars_ref, + functions, + ); } } _ => { @@ -681,7 +721,12 @@ mod tests { let mut vars = Vars { var_map: HashMap::new(), }; - run_statements("pkg/root/0_main", &functions.get("pkg/root/0_main").unwrap().statements, &mut vars, &functions); + run_statements( + "pkg/root/0_main", + &functions.get("pkg/root/0_main").unwrap().statements, + &mut vars, + &functions, + ); return vars.get(result_address); } @@ -704,14 +749,14 @@ mod tests { .statements[0] { IrStatement::ScoreOperation(x) => { + assert_eq!(x.left.name, AddressOrigin::User("pkg/root/0_a".to_string())); assert_eq!( - x.left.name, - AddressOrigin::User("pkg/root/0_a".to_string()) + x.right, + Address { + name: AddressOrigin::Const(1), + offset: 0, + } ); - assert_eq!(x.right, Address { - name: AddressOrigin::Const(1), - offset: 0, - }); } _ => {} } @@ -734,8 +779,7 @@ mod tests { assert_eq!( test_calculation( "pkg/root/0_main", - &program - .function_definitions, + &program.function_definitions, &Address { name: AddressOrigin::User("pkg/root/0_b".to_string()), offset: 0, @@ -789,8 +833,7 @@ mod tests { assert_eq!( test_calculation( "pkg/root/0_main", - &program - .function_definitions, + &program.function_definitions, &Address { name: AddressOrigin::User("pkg/root/0_r".to_string()), offset: 0, @@ -800,7 +843,6 @@ mod tests { ); } - #[test] fn test_while() { let mut mock_file_system = MockFileSystem::new("/".to_string()); @@ -818,8 +860,7 @@ mod tests { assert_eq!( test_calculation( "pkg/root/0_main", - &program - .function_definitions, + &program.function_definitions, &Address { name: AddressOrigin::User("pkg/root/0_a".to_string()), offset: 0, @@ -846,8 +887,7 @@ mod tests { assert_eq!( test_calculation( "pkg/root/0_main", - &program - .function_definitions, + &program.function_definitions, &Address { name: AddressOrigin::User("pkg/root/0_a".to_string()), offset: 0, diff --git a/src/middle/format/ir_types.rs b/src/middle/format/ir_types.rs index b361ff2..f5a4605 100644 --- a/src/middle/format/ir_types.rs +++ b/src/middle/format/ir_types.rs @@ -120,11 +120,7 @@ impl IrBlock { impl std::fmt::Display for IrBlock { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!( - f, - "block {} {{\n", - self.fn_block_index - )?; + write!(f, "block {} {{\n", self.fn_block_index)?; for statement in &self.statements { write!(f, " {:?}\n", statement)?; } @@ -134,7 +130,7 @@ impl std::fmt::Display for IrBlock { impl std::fmt::Display for IrFnDef { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "fn {} {{\n", self.fn_name, )?; + write!(f, "fn {} {{\n", self.fn_name,)?; for statement in &self.statements { match statement { IrStatement::Block(x) => {