Skip to content

Commit

Permalink
lint(all): lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hanmindev committed Feb 9, 2024
1 parent e741ba3 commit d1d8ddb
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 70 deletions.
64 changes: 37 additions & 27 deletions src/back/code_generator/generator.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -197,32 +195,46 @@ 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

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")
}
Expand All @@ -234,9 +246,7 @@ fn if_unless_helper(
}

let range = match op {
"=" => {
c1.to_string()
}
"=" => c1.to_string(),
"<" => {
format!("..{}", c1 - 1)
}
Expand Down
114 changes: 77 additions & 37 deletions src/front/mergers/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,12 @@ fn convert_var_assign(context: &mut Context, ast_node: &VarAssign) -> Vec<IrStat
)
}

fn convert_condition(context: &mut Context, cond: &Box<Expression>, invert_cond: bool, body: IrStatement) -> Vec<IrStatement> {
fn convert_condition(
context: &mut Context,
cond: &Box<Expression>,
invert_cond: bool,
body: IrStatement,
) -> Vec<IrStatement> {
let mut condition = vec![];
// if condition is 0, return
let (mut expr_statements, cond, invert) = convert_expr_for_comparison(context, cond);
Expand Down Expand Up @@ -412,30 +417,36 @@ fn convert_if(context: &mut Context, ast_node: &If) -> Vec<IrStatement> {
}));
}


// 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
});

Expand Down Expand Up @@ -488,7 +499,12 @@ fn convert_for(context: &mut Context, ast_node: &For) -> Vec<IrStatement> {

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
Expand Down Expand Up @@ -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<GlobalName, IrFnDef>, result_address: &Address) -> i32 {
fn test_calculation(
run_function: &str,
functions: &HashMap<GlobalName, IrFnDef>,
result_address: &Address,
) -> i32 {
struct Vars {
var_map: HashMap<Address, i32>,
}
Expand All @@ -605,7 +625,12 @@ mod tests {
}
}

fn run_statements(curr_fn_name: &str, statements: &Vec<IrStatement>, vars_ref: &mut Vars, functions: &HashMap<GlobalName, IrFnDef>) -> bool {
fn run_statements(
curr_fn_name: &str,
statements: &Vec<IrStatement>,
vars_ref: &mut Vars,
functions: &HashMap<GlobalName, IrFnDef>,
) -> bool {
for statement in statements {
match statement {
IrStatement::ScoreOperation(x) => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
}
}
Expand All @@ -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,
);
}
}
_ => {
Expand All @@ -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);
}
Expand All @@ -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,
});
}
_ => {}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -800,7 +843,6 @@ mod tests {
);
}


#[test]
fn test_while() {
let mut mock_file_system = MockFileSystem::new("/".to_string());
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions src/middle/format/ir_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand All @@ -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) => {
Expand Down

0 comments on commit d1d8ddb

Please sign in to comment.