Skip to content

Commit 1d77ff3

Browse files
authored
Issue/86: Add line numbers to labels (#87)
* add line numbers to loop labels * add line numbers to condition labels
1 parent 8b07e2b commit 1d77ff3

File tree

6 files changed

+98
-33
lines changed

6 files changed

+98
-33
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 34 additions & 6 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 {
@@ -477,6 +482,7 @@ fn simplify_lines(
477482
body,
478483
rev,
479484
unroll,
485+
line_number,
480486
} => {
481487
if *unroll {
482488
let (internal_variables, _) = find_variable_usage(body);
@@ -534,7 +540,7 @@ fn simplify_lines(
534540
const_malloc.counter = loop_const_malloc.counter;
535541
array_manager.valid = valid_aux_vars_in_array_manager_before; // restore the valid aux vars
536542

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

540546
// Find variables used inside loop but defined outside
@@ -574,6 +580,7 @@ fn simplify_lines(
574580
// Create recursive function body
575581
let recursive_func = create_recursive_function(
576582
func_name.clone(),
583+
*line_number,
577584
func_args,
578585
iterator.clone(),
579586
end_simplified,
@@ -590,12 +597,14 @@ fn simplify_lines(
590597
function_name: func_name,
591598
args: call_args,
592599
return_data: vec![],
600+
line_number: *line_number,
593601
});
594602
}
595603
Line::FunctionCall {
596604
function_name,
597605
args,
598606
return_data,
607+
line_number,
599608
} => {
600609
let simplified_args = args
601610
.iter()
@@ -605,6 +614,7 @@ fn simplify_lines(
605614
function_name: function_name.clone(),
606615
args: simplified_args,
607616
return_data: return_data.clone(),
617+
line_number: *line_number,
608618
});
609619
}
610620
Line::FunctionRet { return_data } => {
@@ -872,6 +882,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
872882
condition,
873883
then_branch,
874884
else_branch,
885+
line_number: _,
875886
} => {
876887
on_new_condition(condition, &internal_vars, &mut external_vars);
877888

@@ -894,7 +905,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
894905
}
895906
internal_vars.extend(return_data.iter().cloned());
896907
}
897-
Line::Assert(condition) => {
908+
Line::Assert(condition, _line_number) => {
898909
on_new_condition(
899910
&Condition::Comparison(condition.clone()),
900911
&internal_vars,
@@ -941,6 +952,7 @@ pub fn find_variable_usage(lines: &[Line]) -> (BTreeSet<Var>, BTreeSet<Var>) {
941952
body,
942953
rev: _,
943954
unroll: _,
955+
line_number: _,
944956
} => {
945957
let (body_internal, body_external) = find_variable_usage(body);
946958
internal_vars.extend(body_internal);
@@ -1040,6 +1052,7 @@ pub fn inline_lines(
10401052
condition,
10411053
then_branch,
10421054
else_branch,
1055+
line_number: _,
10431056
} => {
10441057
inline_condition(condition);
10451058

@@ -1058,7 +1071,7 @@ pub fn inline_lines(
10581071
inline_internal_var(return_var);
10591072
}
10601073
}
1061-
Line::Assert(condition) => {
1074+
Line::Assert(condition, _line_number) => {
10621075
inline_comparison(condition);
10631076
}
10641077
Line::FunctionRet { return_data } => {
@@ -1112,6 +1125,7 @@ pub fn inline_lines(
11121125
body,
11131126
rev: _,
11141127
unroll: _,
1128+
line_number: _,
11151129
} => {
11161130
inline_lines(body, args, res, inlining_count);
11171131
inline_internal_var(iterator);
@@ -1234,6 +1248,7 @@ fn handle_array_assignment(
12341248

12351249
fn create_recursive_function(
12361250
name: String,
1251+
line_number: SourceLineNumber,
12371252
args: Vec<Var>,
12381253
iterator: Var,
12391254
end: SimpleExpr,
@@ -1257,6 +1272,7 @@ fn create_recursive_function(
12571272
function_name: name.clone(),
12581273
args: recursive_args,
12591274
return_data: vec![],
1275+
line_number,
12601276
});
12611277
body.push(SimpleLine::FunctionRet {
12621278
return_data: vec![],
@@ -1277,6 +1293,7 @@ fn create_recursive_function(
12771293
else_branch: vec![SimpleLine::FunctionRet {
12781294
return_data: vec![],
12791295
}],
1296+
line_number,
12801297
},
12811298
];
12821299

@@ -1416,7 +1433,10 @@ fn replace_vars_for_unroll(
14161433
internal_vars,
14171434
);
14181435
}
1419-
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+
) => {
14201440
replace_vars_for_unroll_in_expr(
14211441
left,
14221442
iterator,
@@ -1436,6 +1456,7 @@ fn replace_vars_for_unroll(
14361456
condition,
14371457
then_branch,
14381458
else_branch,
1459+
line_number: _,
14391460
} => {
14401461
match condition {
14411462
Condition::Comparison(
@@ -1489,6 +1510,7 @@ fn replace_vars_for_unroll(
14891510
body,
14901511
rev: _,
14911512
unroll: _,
1513+
line_number: _,
14921514
} => {
14931515
assert!(other_iterator != iterator);
14941516
*other_iterator =
@@ -1519,6 +1541,7 @@ fn replace_vars_for_unroll(
15191541
function_name: _,
15201542
args,
15211543
return_data,
1544+
line_number: _,
15221545
} => {
15231546
// Function calls are not unrolled, so we don't need to change them
15241547
for arg in args {
@@ -1709,6 +1732,7 @@ fn handle_inlined_functions_helper(
17091732
function_name,
17101733
args,
17111734
return_data,
1735+
line_number: _,
17121736
} => {
17131737
if let Some(func) = inlined_functions.get(&*function_name) {
17141738
let mut inlined_lines = vec![];
@@ -1860,6 +1884,7 @@ fn handle_const_arguments_helper(
18601884
function_name,
18611885
args,
18621886
return_data: _,
1887+
line_number: _,
18631888
} => {
18641889
if let Some(func) = constant_functions.get(function_name) {
18651890
// If the function has constant arguments, we need to handle them
@@ -2051,6 +2076,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
20512076
condition,
20522077
then_branch,
20532078
else_branch,
2079+
line_number: _,
20542080
} => {
20552081
match condition {
20562082
Condition::Comparison(Boolean::Equal { left, right })
@@ -2072,7 +2098,7 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
20722098
replace_vars_by_const_in_expr(end, map);
20732099
replace_vars_by_const_in_lines(body, map);
20742100
}
2075-
Line::Assert(condition) => match condition {
2101+
Line::Assert(condition, _line_number) => match condition {
20762102
Boolean::Equal { left, right } | Boolean::Different { left, right } => {
20772103
replace_vars_by_const_in_expr(left, map);
20782104
replace_vars_by_const_in_expr(right, map);
@@ -2212,6 +2238,7 @@ impl SimpleLine {
22122238
condition,
22132239
then_branch,
22142240
else_branch,
2241+
line_number: _,
22152242
} => {
22162243
let then_str = then_branch
22172244
.iter()
@@ -2237,6 +2264,7 @@ impl SimpleLine {
22372264
function_name,
22382265
args,
22392266
return_data,
2267+
line_number: _,
22402268
} => {
22412269
let args_str = args
22422270
.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: 8 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,
@@ -345,11 +346,13 @@ pub enum Line {
345346
body: Vec<Self>,
346347
rev: bool,
347348
unroll: bool,
349+
line_number: SourceLineNumber,
348350
},
349351
FunctionCall {
350352
function_name: String,
351353
args: Vec<Expression>,
352354
return_data: Vec<Var>,
355+
line_number: SourceLineNumber,
353356
},
354357
FunctionRet {
355358
return_data: Vec<Expression>,
@@ -445,11 +448,12 @@ impl Line {
445448
} => {
446449
format!("{array}[{index}] = {value}")
447450
}
448-
Self::Assert(condition) => format!("assert {condition}"),
451+
Self::Assert(condition, _line_number) => format!("assert {condition}"),
449452
Self::IfCondition {
450453
condition,
451454
then_branch,
452455
else_branch,
456+
line_number: _,
453457
} => {
454458
let then_str = then_branch
455459
.iter()
@@ -481,6 +485,7 @@ impl Line {
481485
body,
482486
rev,
483487
unroll,
488+
line_number: _,
484489
} => {
485490
let body_str = body
486491
.iter()
@@ -502,6 +507,7 @@ impl Line {
502507
function_name,
503508
args,
504509
return_data,
510+
line_number: _,
505511
} => {
506512
let args_str = args
507513
.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
}

0 commit comments

Comments
 (0)