Skip to content

Commit a5b5aa6

Browse files
committed
add line numbers to condition labels
1 parent a2997c3 commit a5b5aa6

File tree

6 files changed

+83
-28
lines changed

6 files changed

+83
-28
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub enum SimpleLine {
9696
condition: SimpleExpr,
9797
then_branch: Vec<Self>,
9898
else_branch: Vec<Self>,
99+
line_number: SourceLineNumber,
99100
},
100101
TestZero {
101102
// Test that the result of the given operation is zero
@@ -107,6 +108,7 @@ pub enum SimpleLine {
107108
function_name: String,
108109
args: Vec<SimpleExpr>,
109110
return_data: Vec<Var>,
111+
line_number: SourceLineNumber,
110112
},
111113
FunctionRet {
112114
return_data: Vec<SimpleExpr>,
@@ -316,7 +318,7 @@ fn simplify_lines(
316318
const_malloc,
317319
);
318320
}
319-
Line::Assert(boolean) => match boolean {
321+
Line::Assert(boolean, line_number) => match boolean {
320322
Boolean::Different { left, right } => {
321323
let left = simplify_expr(left, &mut res, counters, array_manager, const_malloc);
322324
let right =
@@ -333,6 +335,7 @@ fn simplify_lines(
333335
condition: diff_var.into(),
334336
then_branch: vec![],
335337
else_branch: vec![SimpleLine::Panic],
338+
line_number: *line_number,
336339
});
337340
}
338341
Boolean::Equal { left, right } => {
@@ -358,6 +361,7 @@ fn simplify_lines(
358361
condition,
359362
then_branch,
360363
else_branch,
364+
line_number,
361365
} => {
362366
let (condition_simplified, then_branch, else_branch) = match condition {
363367
Condition::Comparison(condition) => {
@@ -468,6 +472,7 @@ fn simplify_lines(
468472
condition: condition_simplified,
469473
then_branch: then_branch_simplified,
470474
else_branch: else_branch_simplified,
475+
line_number: *line_number,
471476
});
472477
}
473478
Line::ForLoop {
@@ -575,6 +580,7 @@ fn simplify_lines(
575580
// Create recursive function body
576581
let recursive_func = create_recursive_function(
577582
func_name.clone(),
583+
*line_number,
578584
func_args,
579585
iterator.clone(),
580586
end_simplified,
@@ -591,12 +597,14 @@ fn simplify_lines(
591597
function_name: func_name,
592598
args: call_args,
593599
return_data: vec![],
600+
line_number: *line_number,
594601
});
595602
}
596603
Line::FunctionCall {
597604
function_name,
598605
args,
599606
return_data,
607+
line_number,
600608
} => {
601609
let simplified_args = args
602610
.iter()
@@ -606,6 +614,7 @@ fn simplify_lines(
606614
function_name: function_name.clone(),
607615
args: simplified_args,
608616
return_data: return_data.clone(),
617+
line_number: *line_number,
609618
});
610619
}
611620
Line::FunctionRet { return_data } => {
@@ -873,6 +882,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
873882
condition,
874883
then_branch,
875884
else_branch,
885+
line_number: _,
876886
} => {
877887
on_new_condition(condition, &internal_vars, &mut external_vars);
878888

@@ -895,7 +905,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
895905
}
896906
internal_vars.extend(return_data.iter().cloned());
897907
}
898-
Line::Assert(condition) => {
908+
Line::Assert(condition, _line_number) => {
899909
on_new_condition(
900910
&Condition::Comparison(condition.clone()),
901911
&internal_vars,
@@ -1042,6 +1052,7 @@ pub fn inline_lines(
10421052
condition,
10431053
then_branch,
10441054
else_branch,
1055+
line_number: _,
10451056
} => {
10461057
inline_condition(condition);
10471058

@@ -1060,7 +1071,7 @@ pub fn inline_lines(
10601071
inline_internal_var(return_var);
10611072
}
10621073
}
1063-
Line::Assert(condition) => {
1074+
Line::Assert(condition, _line_number) => {
10641075
inline_comparison(condition);
10651076
}
10661077
Line::FunctionRet { return_data } => {
@@ -1237,6 +1248,7 @@ fn handle_array_assignment(
12371248

12381249
fn create_recursive_function(
12391250
name: String,
1251+
line_number: SourceLineNumber,
12401252
args: Vec<Var>,
12411253
iterator: Var,
12421254
end: SimpleExpr,
@@ -1260,6 +1272,7 @@ fn create_recursive_function(
12601272
function_name: name.clone(),
12611273
args: recursive_args,
12621274
return_data: vec![],
1275+
line_number,
12631276
});
12641277
body.push(SimpleLine::FunctionRet {
12651278
return_data: vec![],
@@ -1280,6 +1293,7 @@ fn create_recursive_function(
12801293
else_branch: vec![SimpleLine::FunctionRet {
12811294
return_data: vec![],
12821295
}],
1296+
line_number,
12831297
},
12841298
];
12851299

@@ -1419,7 +1433,10 @@ fn replace_vars_for_unroll(
14191433
internal_vars,
14201434
);
14211435
}
1422-
Line::Assert(Boolean::Equal { left, right } | Boolean::Different { left, right }) => {
1436+
Line::Assert(
1437+
Boolean::Equal { left, right } | Boolean::Different { left, right },
1438+
_line_number,
1439+
) => {
14231440
replace_vars_for_unroll_in_expr(
14241441
left,
14251442
iterator,
@@ -1439,6 +1456,7 @@ fn replace_vars_for_unroll(
14391456
condition,
14401457
then_branch,
14411458
else_branch,
1459+
line_number: _,
14421460
} => {
14431461
match condition {
14441462
Condition::Comparison(
@@ -1523,6 +1541,7 @@ fn replace_vars_for_unroll(
15231541
function_name: _,
15241542
args,
15251543
return_data,
1544+
line_number: _,
15261545
} => {
15271546
// Function calls are not unrolled, so we don't need to change them
15281547
for arg in args {
@@ -1713,6 +1732,7 @@ fn handle_inlined_functions_helper(
17131732
function_name,
17141733
args,
17151734
return_data,
1735+
line_number: _,
17161736
} => {
17171737
if let Some(func) = inlined_functions.get(&*function_name) {
17181738
let mut inlined_lines = vec![];
@@ -1864,6 +1884,7 @@ fn handle_const_arguments_helper(
18641884
function_name,
18651885
args,
18661886
return_data: _,
1887+
line_number: _,
18671888
} => {
18681889
if let Some(func) = constant_functions.get(function_name) {
18691890
// If the function has constant arguments, we need to handle them
@@ -2055,6 +2076,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
20552076
condition,
20562077
then_branch,
20572078
else_branch,
2079+
line_number: _,
20582080
} => {
20592081
match condition {
20602082
Condition::Comparison(Boolean::Equal { left, right })
@@ -2076,7 +2098,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
20762098
replace_vars_by_const_in_expr(end, map);
20772099
replace_vars_by_const_in_lines(body, map);
20782100
}
2079-
Line::Assert(condition) => match condition {
2101+
Line::Assert(condition, _line_number) => match condition {
20802102
Boolean::Equal { left, right } | Boolean::Different { left, right } => {
20812103
replace_vars_by_const_in_expr(left, map);
20822104
replace_vars_by_const_in_expr(right, map);
@@ -2216,6 +2238,7 @@ impl SimpleLine {
22162238
condition,
22172239
then_branch,
22182240
else_branch,
2241+
line_number: _,
22192242
} => {
22202243
let then_str = then_branch
22212244
.iter()
@@ -2241,6 +2264,7 @@ impl SimpleLine {
22412264
function_name,
22422265
args,
22432266
return_data,
2267+
line_number: _,
22442268
} => {
22452269
let args_str = args
22462270
.iter()

crates/lean_compiler/src/b_compile_intermediate.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,17 @@ fn compile_lines(
288288
condition,
289289
then_branch,
290290
else_branch,
291+
line_number,
291292
} => {
292293
validate_vars_declared(&[condition], declared_vars)?;
293294

294295
let if_id = compiler.if_counter;
295296
compiler.if_counter += 1;
296297

297298
let (if_label, else_label, end_label) = (
298-
Label::if_label(if_id),
299-
Label::else_label(if_id),
300-
Label::if_else_end(if_id),
299+
Label::if_label(if_id, *line_number),
300+
Label::else_label(if_id, *line_number),
301+
Label::if_else_end(if_id, *line_number),
301302
);
302303

303304
// c: condition
@@ -425,10 +426,11 @@ fn compile_lines(
425426
function_name: callee_function_name,
426427
args,
427428
return_data,
429+
line_number,
428430
} => {
429431
let call_id = compiler.call_counter;
430432
compiler.call_counter += 1;
431-
let return_label = Label::return_from_call(call_id);
433+
let return_label = Label::return_from_call(call_id, *line_number);
432434

433435
let new_fp_pos = compiler.stack_size;
434436
compiler.stack_size += 1;

crates/lean_compiler/src/lang.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,12 @@ pub enum Line {
332332
index: Expression,
333333
value: Expression,
334334
},
335-
Assert(Boolean),
335+
Assert(Boolean, SourceLineNumber),
336336
IfCondition {
337337
condition: Condition,
338338
then_branch: Vec<Self>,
339339
else_branch: Vec<Self>,
340+
line_number: SourceLineNumber,
340341
},
341342
ForLoop {
342343
iterator: Var,
@@ -351,6 +352,7 @@ pub enum Line {
351352
function_name: String,
352353
args: Vec<Expression>,
353354
return_data: Vec<Var>,
355+
line_number: SourceLineNumber,
354356
},
355357
FunctionRet {
356358
return_data: Vec<Expression>,
@@ -446,11 +448,12 @@ impl Line {
446448
} => {
447449
format!("{array}[{index}] = {value}")
448450
}
449-
Self::Assert(condition) => format!("assert {condition}"),
451+
Self::Assert(condition, _line_number) => format!("assert {condition}"),
450452
Self::IfCondition {
451453
condition,
452454
then_branch,
453455
else_branch,
456+
line_number: _,
454457
} => {
455458
let then_str = then_branch
456459
.iter()
@@ -504,6 +507,7 @@ impl Line {
504507
function_name,
505508
args,
506509
return_data,
510+
line_number: _,
507511
} => {
508512
let args_str = args
509513
.iter()

crates/lean_compiler/src/parser/parsers/function.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::literal::VarListParser;
33
use super::statement::StatementParser;
44
use super::{Parse, ParseContext, next_inner_pair};
55
use crate::{
6+
SourceLineNumber,
67
lang::{Expression, Function, Line, SimpleExpr},
78
parser::{
89
error::{ParseResult, SemanticError},
@@ -113,6 +114,7 @@ impl Parse<Line> for FunctionCallParser {
113114
let mut return_data = Vec::new();
114115
let mut function_name = String::new();
115116
let mut args = Vec::new();
117+
let line_number = pair.line_col().0;
116118

117119
for item in pair.into_inner() {
118120
match item.as_rule() {
@@ -148,12 +150,13 @@ impl Parse<Line> for FunctionCallParser {
148150
}
149151

150152
// Handle built-in functions
151-
Self::handle_builtin_function(function_name, args, return_data)
153+
Self::handle_builtin_function(line_number, function_name, args, return_data)
152154
}
153155
}
154156

155157
impl FunctionCallParser {
156158
fn handle_builtin_function(
159+
line_number: SourceLineNumber,
157160
function_name: String,
158161
args: Vec<Expression>,
159162
return_data: Vec<String>,
@@ -249,6 +252,7 @@ impl FunctionCallParser {
249252
function_name,
250253
args,
251254
return_data,
255+
line_number,
252256
})
253257
}
254258
}

crates/lean_compiler/src/parser/parsers/statement.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub struct IfStatementParser;
7878

7979
impl Parse<Line> for IfStatementParser {
8080
fn parse(pair: ParsePair<'_>, ctx: &mut ParseContext) -> ParseResult<Line> {
81+
let line_number = pair.line_col().0;
8182
let mut inner = pair.into_inner();
8283
let condition = ConditionParser::parse(next_inner_pair(&mut inner, "if condition")?, ctx)?;
8384

@@ -104,6 +105,7 @@ impl Parse<Line> for IfStatementParser {
104105
condition,
105106
then_branch,
106107
else_branch,
108+
line_number,
107109
})
108110
}
109111
}
@@ -300,11 +302,12 @@ pub struct AssertEqParser;
300302

301303
impl Parse<Line> for AssertEqParser {
302304
fn parse(pair: ParsePair<'_>, ctx: &mut ParseContext) -> ParseResult<Line> {
305+
let line_number = pair.line_col().0;
303306
let mut inner = pair.into_inner();
304307
let left = ExpressionParser::parse(next_inner_pair(&mut inner, "left assertion")?, ctx)?;
305308
let right = ExpressionParser::parse(next_inner_pair(&mut inner, "right assertion")?, ctx)?;
306309

307-
Ok(Line::Assert(Boolean::Equal { left, right }))
310+
Ok(Line::Assert(Boolean::Equal { left, right }, line_number))
308311
}
309312
}
310313

@@ -313,10 +316,14 @@ pub struct AssertNotEqParser;
313316

314317
impl Parse<Line> for AssertNotEqParser {
315318
fn parse(pair: ParsePair<'_>, ctx: &mut ParseContext) -> ParseResult<Line> {
319+
let line_number = pair.line_col().0;
316320
let mut inner = pair.into_inner();
317321
let left = ExpressionParser::parse(next_inner_pair(&mut inner, "left assertion")?, ctx)?;
318322
let right = ExpressionParser::parse(next_inner_pair(&mut inner, "right assertion")?, ctx)?;
319323

320-
Ok(Line::Assert(Boolean::Different { left, right }))
324+
Ok(Line::Assert(
325+
Boolean::Different { left, right },
326+
line_number,
327+
))
321328
}
322329
}

0 commit comments

Comments
 (0)