Skip to content

Commit d8f02b8

Browse files
authored
feat(cli): Introduce --ast-lowered flag
Changes the behavior of the `--ast` flag to always emit the pre-lowered AST while introducing a `--ast-lowered` that matches the current `--ast` output. Still not perfect in that one can not adjust at what stage to emit the AST (i.e. similar to the participant trait) but good enough for now to peek at the AST before ANY lowering step was executed.
1 parent 69603ab commit d8f02b8

23 files changed

+109
-38
lines changed

compiler/plc_ast/src/ast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ impl Debug for Pou {
269269
.field("variable_blocks", &self.variable_blocks)
270270
.field("pou_type", &self.kind)
271271
.field("return_type", &self.return_type)
272-
.field("interfaces", &self.interfaces);
272+
.field("interfaces", &self.interfaces)
273+
.field("properties", &self.properties);
274+
273275
if !self.generics.is_empty() {
274276
str.field("generics", &self.generics);
275277
}

compiler/plc_driver/src/cli.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ pub struct CompileParameters {
2727
global = true,
2828
help = "Emit AST (Abstract Syntax Tree) as output"
2929
)]
30-
pub output_ast: bool,
30+
pub print_ast: bool,
31+
32+
#[clap(
33+
long = "ast-lowered",
34+
group = "format",
35+
global = true,
36+
help = "Emit lowered AST (Abstract Syntax Tree) as output"
37+
)]
38+
pub print_ast_lowered: bool,
3139

3240
#[clap(long = "version", group = "format", global = true)]
3341
pub build_info: bool,
@@ -673,7 +681,8 @@ mod cli_tests {
673681
fn valid_output_formats() {
674682
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--ir")).unwrap();
675683
assert!(parameters.output_ir);
676-
assert!(!parameters.output_ast);
684+
assert!(!parameters.print_ast);
685+
assert!(!parameters.print_ast_lowered);
677686
assert!(!parameters.output_bit_code);
678687
assert!(!parameters.output_obj_code);
679688
assert!(!parameters.output_pic_obj);
@@ -682,7 +691,18 @@ mod cli_tests {
682691

683692
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--ast")).unwrap();
684693
assert!(!parameters.output_ir);
685-
assert!(parameters.output_ast);
694+
assert!(parameters.print_ast);
695+
assert!(!parameters.print_ast_lowered);
696+
assert!(!parameters.output_bit_code);
697+
assert!(!parameters.output_obj_code);
698+
assert!(!parameters.output_pic_obj);
699+
assert!(!parameters.output_shared_obj);
700+
assert!(!parameters.output_reloc_code);
701+
702+
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--ast-lowered")).unwrap();
703+
assert!(!parameters.output_ir);
704+
assert!(!parameters.print_ast);
705+
assert!(parameters.print_ast_lowered);
686706
assert!(!parameters.output_bit_code);
687707
assert!(!parameters.output_obj_code);
688708
assert!(!parameters.output_pic_obj);
@@ -691,7 +711,8 @@ mod cli_tests {
691711

692712
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--bc")).unwrap();
693713
assert!(!parameters.output_ir);
694-
assert!(!parameters.output_ast);
714+
assert!(!parameters.print_ast);
715+
assert!(!parameters.print_ast_lowered);
695716
assert!(parameters.output_bit_code);
696717
assert!(!parameters.output_obj_code);
697718
assert!(!parameters.output_pic_obj);
@@ -700,7 +721,8 @@ mod cli_tests {
700721

701722
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--static")).unwrap();
702723
assert!(!parameters.output_ir);
703-
assert!(!parameters.output_ast);
724+
assert!(!parameters.print_ast);
725+
assert!(!parameters.print_ast_lowered);
704726
assert!(!parameters.output_bit_code);
705727
assert!(parameters.output_obj_code);
706728
assert!(!parameters.output_pic_obj);
@@ -709,7 +731,8 @@ mod cli_tests {
709731

710732
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--pic")).unwrap();
711733
assert!(!parameters.output_ir);
712-
assert!(!parameters.output_ast);
734+
assert!(!parameters.print_ast);
735+
assert!(!parameters.print_ast_lowered);
713736
assert!(!parameters.output_bit_code);
714737
assert!(!parameters.output_obj_code);
715738
assert!(parameters.output_pic_obj);
@@ -718,7 +741,8 @@ mod cli_tests {
718741

719742
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--shared")).unwrap();
720743
assert!(!parameters.output_ir);
721-
assert!(!parameters.output_ast);
744+
assert!(!parameters.print_ast);
745+
assert!(!parameters.print_ast_lowered);
722746
assert!(!parameters.output_bit_code);
723747
assert!(!parameters.output_obj_code);
724748
assert!(!parameters.output_pic_obj);
@@ -727,7 +751,8 @@ mod cli_tests {
727751

728752
let parameters = CompileParameters::parse(vec_of_strings!("input.st", "--relocatable")).unwrap();
729753
assert!(!parameters.output_ir);
730-
assert!(!parameters.output_ast);
754+
assert!(!parameters.print_ast);
755+
assert!(!parameters.print_ast_lowered);
731756
assert!(!parameters.output_bit_code);
732757
assert!(!parameters.output_obj_code);
733758
assert!(!parameters.output_pic_obj);
@@ -736,7 +761,8 @@ mod cli_tests {
736761

737762
let parameters = CompileParameters::parse(vec_of_strings!("input.st")).unwrap();
738763
assert!(!parameters.output_ir);
739-
assert!(!parameters.output_ast);
764+
assert!(!parameters.print_ast);
765+
assert!(!parameters.print_ast_lowered);
740766
assert!(!parameters.output_bit_code);
741767
assert!(!parameters.output_obj_code);
742768
assert!(!parameters.output_pic_obj);

compiler/plc_driver/src/pipelines.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,26 @@ impl<T: SourceContainer> Pipeline for BuildPipeline<T> {
299299

300300
self.initialize_thread_pool();
301301

302+
// 1. Parse
302303
let parsed_project = self.parse()?;
304+
305+
if self.compile_parameters.as_ref().is_some_and(|opt| opt.print_ast) {
306+
println!("{:#?}", parsed_project.units);
307+
return Ok(());
308+
}
309+
310+
// 2. Index
303311
let indexed_project = self.index(parsed_project)?;
312+
313+
// 3. Resolve
304314
let annotated_project = self.annotate(indexed_project)?;
305315

306-
//TODO : this is post lowering, we might want to control this
307-
if let Some(CompileParameters { output_ast: true, .. }) = self.compile_parameters {
316+
if self.compile_parameters.as_ref().is_some_and(|opt| opt.print_ast_lowered) {
308317
println!("{:#?}", annotated_project.units);
309318
return Ok(());
310319
}
311320

312-
// 5. Validate
313-
//TODO: this goes into a participant
321+
// 4. Validate
314322
annotated_project.validate(&self.context, &mut self.diagnostician)?;
315323

316324
//TODO: probably not needed, should be a participant anyway
@@ -323,13 +331,12 @@ impl<T: SourceContainer> Pipeline for BuildPipeline<T> {
323331
annotated_project.generate_hardware_information(format, location)?;
324332
}
325333

326-
// 5 : Codegen
327-
if !self.compile_parameters.as_ref().map(CompileParameters::is_check).unwrap_or_default() {
328-
let context = CodegenContext::create();
329-
self.generate(&context, annotated_project)?;
334+
// 5. Codegen
335+
if self.compile_parameters.as_ref().is_some_and(CompileParameters::is_check) {
336+
return Ok(());
330337
}
331338

332-
Ok(())
339+
self.generate(&CodegenContext::create(), annotated_project)
333340
}
334341

335342
fn parse(&mut self) -> Result<ParsedProject, Diagnostic> {

compiler/plc_lowering/src/inheritance.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ mod units_tests {
295295
pou_type: FunctionBlock,
296296
return_type: None,
297297
interfaces: [],
298+
properties: [],
298299
}
299300
"###);
300301
}
@@ -2065,6 +2066,7 @@ mod units_tests {
20652066
pou_type: FunctionBlock,
20662067
return_type: None,
20672068
interfaces: [],
2069+
properties: [],
20682070
}
20692071
"###);
20702072
}
@@ -2095,7 +2097,7 @@ mod units_tests {
20952097

20962098
let (_, project) = parse_and_annotate("test", vec![src]).unwrap();
20972099
let unit = &project.units[0].get_unit().pous[3];
2098-
assert_debug_snapshot!(unit, @r#"
2100+
assert_debug_snapshot!(unit, @r###"
20992101
POU {
21002102
name: "child.foo",
21012103
variable_blocks: [
@@ -2146,8 +2148,9 @@ mod units_tests {
21462148
},
21472149
return_type: None,
21482150
interfaces: [],
2151+
properties: [],
21492152
}
2150-
"#);
2153+
"###);
21512154
}
21522155

21532156
#[test]

compiler/plc_xml/src/xml_parser/snapshots/plc_xml__xml_parser__control__tests__jump_and_label_converted_to_ast.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CompilationUnit {
2929
pou_type: Program,
3030
return_type: None,
3131
interfaces: [],
32+
properties: [],
3233
},
3334
],
3435
implementations: [

compiler/plc_xml/src/xml_parser/snapshots/plc_xml__xml_parser__control__tests__negated_jump_ast.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CompilationUnit {
2929
pou_type: Program,
3030
return_type: None,
3131
interfaces: [],
32+
properties: [],
3233
},
3334
],
3435
implementations: [

compiler/plc_xml/src/xml_parser/snapshots/plc_xml__xml_parser__control__tests__unconnected_jump_generated_as_empty_statement.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CompilationUnit {
2929
pou_type: Program,
3030
return_type: None,
3131
interfaces: [],
32+
properties: [],
3233
},
3334
],
3435
implementations: [

compiler/plc_xml/src/xml_parser/snapshots/plc_xml__xml_parser__control__tests__unnamed_controls.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CompilationUnit {
1212
pou_type: Program,
1313
return_type: None,
1414
interfaces: [],
15+
properties: [],
1516
},
1617
],
1718
implementations: [

src/lowering/property.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ mod tests {
457457
},
458458
),
459459
interfaces: [],
460+
properties: [],
460461
}
461462
"###);
462463

@@ -525,6 +526,7 @@ mod tests {
525526
},
526527
return_type: None,
527528
interfaces: [],
529+
properties: [],
528530
}
529531
"###);
530532
}
@@ -803,6 +805,7 @@ mod tests {
803805
},
804806
),
805807
interfaces: [],
808+
properties: [],
806809
},
807810
POU {
808811
name: "foo.__set_bar",
@@ -833,6 +836,7 @@ mod tests {
833836
},
834837
return_type: None,
835838
interfaces: [],
839+
properties: [],
836840
},
837841
]
838842
"###);

src/lowering/snapshots/rusty__lowering__calls__tests__function_wirh_array_of_string_return-3.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ CompilationUnit {
3333
},
3434
),
3535
interfaces: [],
36+
properties: [],
3637
},
3738
POU {
3839
name: "main",
3940
variable_blocks: [],
4041
pou_type: Function,
4142
return_type: None,
4243
interfaces: [],
44+
properties: [],
4345
},
4446
],
4547
implementations: [

0 commit comments

Comments
 (0)