Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions crates/lean_compiler/src/a_simplify_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum SimpleLine {
condition: SimpleExpr,
then_branch: Vec<Self>,
else_branch: Vec<Self>,
line_number: SourceLineNumber,
},
TestZero {
// Test that the result of the given operation is zero
Expand All @@ -107,6 +108,7 @@ pub enum SimpleLine {
function_name: String,
args: Vec<SimpleExpr>,
return_data: Vec<Var>,
line_number: SourceLineNumber,
},
FunctionRet {
return_data: Vec<SimpleExpr>,
Expand Down Expand Up @@ -316,7 +318,7 @@ fn simplify_lines(
const_malloc,
);
}
Line::Assert(boolean) => match boolean {
Line::Assert(boolean, line_number) => match boolean {
Boolean::Different { left, right } => {
let left = simplify_expr(left, &mut res, counters, array_manager, const_malloc);
let right =
Expand All @@ -333,6 +335,7 @@ fn simplify_lines(
condition: diff_var.into(),
then_branch: vec![],
else_branch: vec![SimpleLine::Panic],
line_number: *line_number,
});
}
Boolean::Equal { left, right } => {
Expand All @@ -358,6 +361,7 @@ fn simplify_lines(
condition,
then_branch,
else_branch,
line_number,
} => {
let (condition_simplified, then_branch, else_branch) = match condition {
Condition::Comparison(condition) => {
Expand Down Expand Up @@ -468,6 +472,7 @@ fn simplify_lines(
condition: condition_simplified,
then_branch: then_branch_simplified,
else_branch: else_branch_simplified,
line_number: *line_number,
});
}
Line::ForLoop {
Expand All @@ -477,6 +482,7 @@ fn simplify_lines(
body,
rev,
unroll,
line_number,
} => {
if *unroll {
let (internal_variables, _) = find_variable_usage(body);
Expand Down Expand Up @@ -534,7 +540,7 @@ fn simplify_lines(
const_malloc.counter = loop_const_malloc.counter;
array_manager.valid = valid_aux_vars_in_array_manager_before; // restore the valid aux vars

let func_name = format!("@loop_{}", counters.loops);
let func_name = format!("@loop_{}_line_{}", counters.loops, line_number);
counters.loops += 1;

// Find variables used inside loop but defined outside
Expand Down Expand Up @@ -574,6 +580,7 @@ fn simplify_lines(
// Create recursive function body
let recursive_func = create_recursive_function(
func_name.clone(),
*line_number,
func_args,
iterator.clone(),
end_simplified,
Expand All @@ -590,12 +597,14 @@ fn simplify_lines(
function_name: func_name,
args: call_args,
return_data: vec![],
line_number: *line_number,
});
}
Line::FunctionCall {
function_name,
args,
return_data,
line_number,
} => {
let simplified_args = args
.iter()
Expand All @@ -605,6 +614,7 @@ fn simplify_lines(
function_name: function_name.clone(),
args: simplified_args,
return_data: return_data.clone(),
line_number: *line_number,
});
}
Line::FunctionRet { return_data } => {
Expand Down Expand Up @@ -872,6 +882,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
condition,
then_branch,
else_branch,
line_number: _,
} => {
on_new_condition(condition, &internal_vars, &mut external_vars);

Expand All @@ -894,7 +905,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
}
internal_vars.extend(return_data.iter().cloned());
}
Line::Assert(condition) => {
Line::Assert(condition, _line_number) => {
on_new_condition(
&Condition::Comparison(condition.clone()),
&internal_vars,
Expand Down Expand Up @@ -941,6 +952,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
body,
rev: _,
unroll: _,
line_number: _,
} => {
let (body_internal, body_external) = find_variable_usage(body);
internal_vars.extend(body_internal);
Expand Down Expand Up @@ -1040,6 +1052,7 @@ pub fn inline_lines(
condition,
then_branch,
else_branch,
line_number: _,
} => {
inline_condition(condition);

Expand All @@ -1058,7 +1071,7 @@ pub fn inline_lines(
inline_internal_var(return_var);
}
}
Line::Assert(condition) => {
Line::Assert(condition, _line_number) => {
inline_comparison(condition);
}
Line::FunctionRet { return_data } => {
Expand Down Expand Up @@ -1112,6 +1125,7 @@ pub fn inline_lines(
body,
rev: _,
unroll: _,
line_number: _,
} => {
inline_lines(body, args, res, inlining_count);
inline_internal_var(iterator);
Expand Down Expand Up @@ -1234,6 +1248,7 @@ fn handle_array_assignment(

fn create_recursive_function(
name: String,
line_number: SourceLineNumber,
args: Vec<Var>,
iterator: Var,
end: SimpleExpr,
Expand All @@ -1257,6 +1272,7 @@ fn create_recursive_function(
function_name: name.clone(),
args: recursive_args,
return_data: vec![],
line_number,
});
body.push(SimpleLine::FunctionRet {
return_data: vec![],
Expand All @@ -1277,6 +1293,7 @@ fn create_recursive_function(
else_branch: vec![SimpleLine::FunctionRet {
return_data: vec![],
}],
line_number,
},
];

Expand Down Expand Up @@ -1416,7 +1433,10 @@ fn replace_vars_for_unroll(
internal_vars,
);
}
Line::Assert(Boolean::Equal { left, right } | Boolean::Different { left, right }) => {
Line::Assert(
Boolean::Equal { left, right } | Boolean::Different { left, right },
_line_number,
) => {
replace_vars_for_unroll_in_expr(
left,
iterator,
Expand All @@ -1436,6 +1456,7 @@ fn replace_vars_for_unroll(
condition,
then_branch,
else_branch,
line_number: _,
} => {
match condition {
Condition::Comparison(
Expand Down Expand Up @@ -1489,6 +1510,7 @@ fn replace_vars_for_unroll(
body,
rev: _,
unroll: _,
line_number: _,
} => {
assert!(other_iterator != iterator);
*other_iterator =
Expand Down Expand Up @@ -1519,6 +1541,7 @@ fn replace_vars_for_unroll(
function_name: _,
args,
return_data,
line_number: _,
} => {
// Function calls are not unrolled, so we don't need to change them
for arg in args {
Expand Down Expand Up @@ -1709,6 +1732,7 @@ fn handle_inlined_functions_helper(
function_name,
args,
return_data,
line_number: _,
} => {
if let Some(func) = inlined_functions.get(&*function_name) {
let mut inlined_lines = vec![];
Expand Down Expand Up @@ -1860,6 +1884,7 @@ fn handle_const_arguments_helper(
function_name,
args,
return_data: _,
line_number: _,
} => {
if let Some(func) = constant_functions.get(function_name) {
// If the function has constant arguments, we need to handle them
Expand Down Expand Up @@ -2051,6 +2076,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
condition,
then_branch,
else_branch,
line_number: _,
} => {
match condition {
Condition::Comparison(Boolean::Equal { left, right })
Expand All @@ -2072,7 +2098,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
replace_vars_by_const_in_expr(end, map);
replace_vars_by_const_in_lines(body, map);
}
Line::Assert(condition) => match condition {
Line::Assert(condition, _line_number) => match condition {
Boolean::Equal { left, right } | Boolean::Different { left, right } => {
replace_vars_by_const_in_expr(left, map);
replace_vars_by_const_in_expr(right, map);
Expand Down Expand Up @@ -2212,6 +2238,7 @@ impl SimpleLine {
condition,
then_branch,
else_branch,
line_number: _,
} => {
let then_str = then_branch
.iter()
Expand All @@ -2237,6 +2264,7 @@ impl SimpleLine {
function_name,
args,
return_data,
line_number: _,
} => {
let args_str = args
.iter()
Expand Down
10 changes: 6 additions & 4 deletions crates/lean_compiler/src/b_compile_intermediate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,17 @@ fn compile_lines(
condition,
then_branch,
else_branch,
line_number,
} => {
validate_vars_declared(&[condition], declared_vars)?;

let if_id = compiler.if_counter;
compiler.if_counter += 1;

let (if_label, else_label, end_label) = (
Label::if_label(if_id),
Label::else_label(if_id),
Label::if_else_end(if_id),
Label::if_label(if_id, *line_number),
Label::else_label(if_id, *line_number),
Label::if_else_end(if_id, *line_number),
);

// c: condition
Expand Down Expand Up @@ -425,10 +426,11 @@ fn compile_lines(
function_name: callee_function_name,
args,
return_data,
line_number,
} => {
let call_id = compiler.call_counter;
compiler.call_counter += 1;
let return_label = Label::return_from_call(call_id);
let return_label = Label::return_from_call(call_id, *line_number);

let new_fp_pos = compiler.stack_size;
compiler.stack_size += 1;
Expand Down
10 changes: 8 additions & 2 deletions crates/lean_compiler/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ pub enum Line {
index: Expression,
value: Expression,
},
Assert(Boolean),
Assert(Boolean, SourceLineNumber),
IfCondition {
condition: Condition,
then_branch: Vec<Self>,
else_branch: Vec<Self>,
line_number: SourceLineNumber,
},
ForLoop {
iterator: Var,
Expand All @@ -345,11 +346,13 @@ pub enum Line {
body: Vec<Self>,
rev: bool,
unroll: bool,
line_number: SourceLineNumber,
},
FunctionCall {
function_name: String,
args: Vec<Expression>,
return_data: Vec<Var>,
line_number: SourceLineNumber,
},
FunctionRet {
return_data: Vec<Expression>,
Expand Down Expand Up @@ -445,11 +448,12 @@ impl Line {
} => {
format!("{array}[{index}] = {value}")
}
Self::Assert(condition) => format!("assert {condition}"),
Self::Assert(condition, _line_number) => format!("assert {condition}"),
Self::IfCondition {
condition,
then_branch,
else_branch,
line_number: _,
} => {
let then_str = then_branch
.iter()
Expand Down Expand Up @@ -481,6 +485,7 @@ impl Line {
body,
rev,
unroll,
line_number: _,
} => {
let body_str = body
.iter()
Expand All @@ -502,6 +507,7 @@ impl Line {
function_name,
args,
return_data,
line_number: _,
} => {
let args_str = args
.iter()
Expand Down
6 changes: 5 additions & 1 deletion crates/lean_compiler/src/parser/parsers/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::literal::VarListParser;
use super::statement::StatementParser;
use super::{Parse, ParseContext, next_inner_pair};
use crate::{
SourceLineNumber,
lang::{Expression, Function, Line, SimpleExpr},
parser::{
error::{ParseResult, SemanticError},
Expand Down Expand Up @@ -113,6 +114,7 @@ impl Parse<Line> for FunctionCallParser {
let mut return_data = Vec::new();
let mut function_name = String::new();
let mut args = Vec::new();
let line_number = pair.line_col().0;

for item in pair.into_inner() {
match item.as_rule() {
Expand Down Expand Up @@ -148,12 +150,13 @@ impl Parse<Line> for FunctionCallParser {
}

// Handle built-in functions
Self::handle_builtin_function(function_name, args, return_data)
Self::handle_builtin_function(line_number, function_name, args, return_data)
}
}

impl FunctionCallParser {
fn handle_builtin_function(
line_number: SourceLineNumber,
function_name: String,
args: Vec<Expression>,
return_data: Vec<String>,
Expand Down Expand Up @@ -249,6 +252,7 @@ impl FunctionCallParser {
function_name,
args,
return_data,
line_number,
})
}
}
Expand Down
Loading