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 8, 2024
1 parent ceea6ac commit fd9ccd0
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/back.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod code_generator;
mod code_generator;
19 changes: 12 additions & 7 deletions src/back/code_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::middle::format::ir_types::FunctionName;
use crate::back::code_generator::generator::CodeGenerator;
use crate::middle::format::ir_types::FunctionName;
use crate::middle::format::types::Program;

mod generator;
Expand Down Expand Up @@ -32,7 +32,6 @@ impl Context {
}
}


pub fn generate_code(program: &Program) -> GeneratedCode {
let mut generated_code = GeneratedCode { functions: vec![] };
for (name, def) in &program.function_definitions {
Expand Down Expand Up @@ -69,16 +68,19 @@ pub fn flatten_to_hmasm(generated_code: &GeneratedCode) -> String {

#[cfg(test)]
mod tests {
use crate::back::code_generator::{flatten_to_hmasm, generate_code};
use crate::front::file_system::fs::FileSystem;
use crate::front::file_system::mock_fs::MockFileSystem;
use crate::front::mergers::program::ProgramMerger;
use crate::front::file_system::fs::FileSystem;
use crate::back::code_generator::{flatten_to_hmasm, generate_code};
// TODO: these tests don't do anything at the moment, you should review it and make sure the output is correct.
// TODO: should add a mcfunction interpreter to test the output of the code generator
#[test]
fn test_generate_code() {
let mut mock_fs = MockFileSystem::new("/".to_string());
mock_fs.insert_file("/main.ing", "pub fn main() { let a: int = 5; if (a - 5 == 0) { a = 7; }; }");
mock_fs.insert_file(
"/main.ing",
"pub fn main() { let a: int = 5; if (a - 5 == 0) { a = 7; }; }",
);

let mut program_merger = ProgramMerger::new("test");
program_merger.read_package("test", mock_fs);
Expand All @@ -92,7 +94,10 @@ mod tests {
#[test]
fn test_generate_for_code() {
let mut mock_fs = MockFileSystem::new("/".to_string());
mock_fs.insert_file("/main.ing", "pub fn main() { let a: int = 0; for (let i: int = 0; i < 9; i += 1) { a = 5; }; }");
mock_fs.insert_file(
"/main.ing",
"pub fn main() { let a: int = 0; for (let i: int = 0; i < 9; i += 1) { a = 5; }; }",
);

let mut program_merger = ProgramMerger::new("test");
program_merger.read_package("test", mock_fs);
Expand All @@ -103,4 +108,4 @@ mod tests {

println!("{}", hmasm);
}
}
}
153 changes: 84 additions & 69 deletions src/back/code_generator/generator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::middle::format::ir_types::{CompareOp};
use crate::back::code_generator::{Context, GeneratedCode, MFunction};
use crate::middle::format::ir_types::{Cond, IrUnless};
use crate::middle::format::ir_types::CompareOp;
use crate::middle::format::ir_types::{Address, IrBlock, IrIf, IrScoreSet, IrStatement};
use crate::middle::format::ir_types::{
AddressOrigin, IrScoreAddI, IrScoreOperation, IrScoreOperationType,
};
use crate::middle::format::ir_types::{Cond, IrUnless};

static BLASTFURNACE_OBJECTIVE: &str = "blst";
static BLASTFURNACE_CONST: &str = "blst";
Expand Down Expand Up @@ -76,40 +76,37 @@ impl CodeGenerator for IrScoreOperation {
_ => "",
};
match self.right.name {
AddressOrigin::Const(x) => {
match self.op {
IrScoreOperationType::Add
| IrScoreOperationType::Sub => {
let mut con = if self.op == IrScoreOperationType::Sub {
-x
} else {
x
};
AddressOrigin::Const(x) => match self.op {
IrScoreOperationType::Add | IrScoreOperationType::Sub => {
let mut con = if self.op == IrScoreOperationType::Sub {
-x
} else {
x
};

return if con >= 0 {
vec![format!(
"scoreboard players add {} {}",
self.left.to_score(),
con
)]
} else {
vec![format!(
"scoreboard players remove {} {}",
self.left.to_score(),
-con
)]
};
}
IrScoreOperationType::Assign => {
return vec![format!(
"scoreboard players set {} {}",
return if con >= 0 {
vec![format!(
"scoreboard players add {} {}",
self.left.to_score(),
con
)]
} else {
vec![format!(
"scoreboard players remove {} {}",
self.left.to_score(),
x
)];
}
_ => {}
-con
)]
};
}
}
IrScoreOperationType::Assign => {
return vec![format!(
"scoreboard players set {} {}",
self.left.to_score(),
x
)];
}
_ => {}
},
_ => {}
}

Expand All @@ -131,11 +128,11 @@ impl CodeGenerator for IrScoreOperation {
vec![]
}
}
IrScoreOperationType::Leq |
IrScoreOperationType::Geq |
IrScoreOperationType::Lt |
IrScoreOperationType::Gt |
IrScoreOperationType::Eq => {
IrScoreOperationType::Leq
| IrScoreOperationType::Geq
| IrScoreOperationType::Lt
| IrScoreOperationType::Gt
| IrScoreOperationType::Eq => {
vec![format!(
"execute store result score {} if score {} {op} {}",
self.left.to_score(),
Expand Down Expand Up @@ -176,7 +173,13 @@ impl CodeGenerator for IrScoreOperation {
}
}

fn if_unless_helper(generated_code: &mut GeneratedCode, context: &mut Context, cond: &Cond, body: &Box<IrStatement>, type_: &str) -> Vec<String> {
fn if_unless_helper(
generated_code: &mut GeneratedCode,
context: &mut Context,
cond: &Cond,
body: &Box<IrStatement>,
type_: &str,
) -> Vec<String> {
let mut statements = body.generate(generated_code, context);

let statement = if statements.len() == 1 {
Expand All @@ -185,36 +188,44 @@ fn if_unless_helper(generated_code: &mut GeneratedCode, context: &mut Context, c
wrap_in_function(statements, generated_code, context)
};

vec![format!("{} {}", match &cond {
Cond::CheckVal(x) => {
if x.min == x.max {
format!("execute {type_} score {} matches {} run", x.var_name.to_score(), x.min)
} else {
vec![format!(
"{} {}",
match &cond {
Cond::CheckVal(x) => {
if x.min == x.max {
format!(
"execute {type_} score {} matches {} run",
x.var_name.to_score(),
x.min
)
} else {
format!(
"execute {type_} score {} matches {}..{} run",
x.var_name.to_score(),
x.min,
x.max
)
}
}
Cond::CompareVal(x) => {
let op = match x.op {
CompareOp::Eq => "matches",
CompareOp::Neq => "matches",
CompareOp::Lt => "matches",
CompareOp::Gt => "matches",
CompareOp::Leq => "matches",
CompareOp::Geq => "matches",
};
format!(
"execute {type_} score {} matches {}..{} run",
x.var_name.to_score(),
x.min,
x.max
"execute {type_} score {} {} {} run",
x.var_0.to_score(),
op,
x.var_1.to_score()
)
}
}
Cond::CompareVal(x) => {
let op = match x.op {
CompareOp::Eq => "matches",
CompareOp::Neq => "matches",
CompareOp::Lt => "matches",
CompareOp::Gt => "matches",
CompareOp::Leq => "matches",
CompareOp::Geq => "matches",
};
format!(
"execute {type_} score {} {} {} run",
x.var_0.to_score(),
op,
x.var_1.to_score()
)
}
}, statement)]
},
statement
)]
}

impl CodeGenerator for IrIf {
Expand All @@ -239,7 +250,7 @@ impl CodeGenerator for IrStatement {
IrStatement::Unless(x) => x.generate(generated_code, context),
IrStatement::FnCall(x) => vec![format!("function {}", x.fn_name)],
IrStatement::Return => vec!["return".to_string()],
IrStatement::Block(x) => x.generate(generated_code, context)
IrStatement::Block(x) => x.generate(generated_code, context),
}
}
}
Expand All @@ -259,7 +270,11 @@ impl CodeGenerator for IrBlock {
}
}

fn wrap_in_function(statements: Vec<String>, generated_code: &mut GeneratedCode, context: &mut Context) -> String {
fn wrap_in_function(
statements: Vec<String>,
generated_code: &mut GeneratedCode,
context: &mut Context,
) -> String {
let block_name = context.new_block();

generated_code.add_function(MFunction {
Expand All @@ -268,4 +283,4 @@ fn wrap_in_function(statements: Vec<String>, generated_code: &mut GeneratedCode,
});

format!("function {}", block_name)
}
}
60 changes: 27 additions & 33 deletions src/front/mergers/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::front::ast_types::{
use crate::front::mergers::convert::context::Context;
use crate::front::mergers::definition_table::DefinitionTable;
use crate::middle::format::ir_types::{
Address, CheckVal, Cond, IrBlock, IrFnCall, IrFnDef, IrIf,
IrScoreOperation, IrScoreOperationType, IrScoreSet, IrStatement, IrUnless,
Address, CheckVal, Cond, IrBlock, IrFnCall, IrFnDef, IrIf, IrScoreOperation,
IrScoreOperationType, IrScoreSet, IrStatement, IrUnless,
};
use crate::middle::format::types::GlobalName;
use std::rc::Rc;
Expand Down Expand Up @@ -57,29 +57,23 @@ fn convert_fn_call(context: &mut Context, ast_node: &FnCall) -> Vec<IrStatement>
fn set_from_atomic(
context: &mut Context,
ast_node: &AtomicExpression,
result_var_name: &Address,
_result_var_name: &Address,
) -> ExprEval {
match ast_node {
AtomicExpression::Literal(x) => {
match x {
LiteralValue::Null => {
ExprEval {
statements: vec![],
existing_address: Some(context.const_generator.get_const(0)),
}
}
LiteralValue::Bool(b) => {
ExprEval {
statements: vec![],
existing_address: Some(context.const_generator.get_const(b.clone() as i32)),
}
}
LiteralValue::Int(x) => {
ExprEval {
statements: vec![],
existing_address: Some(context.const_generator.get_const(x.clone())),
}
}
LiteralValue::Null => ExprEval {
statements: vec![],
existing_address: Some(context.const_generator.get_const(0)),
},
LiteralValue::Bool(b) => ExprEval {
statements: vec![],
existing_address: Some(context.const_generator.get_const(b.clone() as i32)),
},
LiteralValue::Int(x) => ExprEval {
statements: vec![],
existing_address: Some(context.const_generator.get_const(x.clone())),
},
// LiteralValue::Decimal(_) => {}
// LiteralValue::String(_) => {}
// LiteralValue::Compound(_) => {}
Expand Down Expand Up @@ -131,13 +125,11 @@ fn rec_convert_expr(
}

match unop {
UnOp::Neg => {
s.push(IrStatement::ScoreOperation(IrScoreOperation {
left: result_var_name.clone(),
op: IrScoreOperationType::Mul,
right: context.const_generator.get_const(-1),
}))
}
UnOp::Neg => s.push(IrStatement::ScoreOperation(IrScoreOperation {
left: result_var_name.clone(),
op: IrScoreOperationType::Mul,
right: context.const_generator.get_const(-1),
})),
UnOp::Not => {
s.push(IrStatement::ScoreOperation(IrScoreOperation {
left: result_var_name.clone(),
Expand Down Expand Up @@ -211,7 +203,6 @@ fn rec_convert_expr(
context.forfeit_variable(&a0);
}


ExprEval {
statements: s,
existing_address: None,
Expand All @@ -220,7 +211,6 @@ fn rec_convert_expr(
};
}


fn convert_expr(
context: &mut Context,
ast_node: &Expression,
Expand Down Expand Up @@ -517,7 +507,9 @@ mod tests {
impl Vars {
fn get(&self, address: &Address) -> i32 {
match &address.name {
AddressOrigin::Const(c) => { return *c; }
AddressOrigin::Const(c) => {
return *c;
}
_ => {}
}
return *self.vars.get(address).unwrap();
Expand Down Expand Up @@ -547,7 +539,7 @@ mod tests {
IrStatement::ScoreOperation(x) => {
let left = match x.op {
IrScoreOperationType::Assign => 0,
_ => vars.get(&x.left)
_ => vars.get(&x.left),
};
let right = vars.get(&x.right);
let result = match x.op {
Expand Down Expand Up @@ -620,7 +612,9 @@ mod tests {
run_statement(&statement, vars);
}
}
_ => { panic!("Not implemented") }
_ => {
panic!("Not implemented")
}
}
}

Expand Down
Loading

0 comments on commit fd9ccd0

Please sign in to comment.