Skip to content

feat: add --ast CLI argument to emit the AST to stdout #1256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2024

Conversation

nturley-copia
Copy link
Contributor

@nturley-copia nturley-copia commented Jul 8, 2024

Implements #1003

If the --ast is found, then after Resolve / Annotate, it prints the AST to stdout and then terminates.

Ideally this would be another output format that gets written to disk as JSON or something instead of emitted over stdout, but this is a start.

This just uses the default Rust Debug formatting.

For example:

target\debug\plc.exe examples/hello_world.st --ast

produced

[
    (
        CompilationUnit {
            global_vars: [],
            units: [
                POU {
                    name: "puts",
                    variable_blocks: [
                        VariableBlock {
                            variables: [
                                Variable {
                                    name: "text",
                                    data_type: DataTypeReference {
                                        referenced_type: "STRING",
                                    },
                                },
                            ],
                            variable_block_type: Input(
                                ByRef,
                            ),
                        },
                    ],
                    pou_type: Function,
                    return_type: Some(
                        DataTypeReference {
                            referenced_type: "DINT",
                        },
                    ),
                },
                POU {
                    name: "main",
                    variable_blocks: [],
                    pou_type: Function,
                    return_type: Some(
                        DataTypeReference {
                            referenced_type: "DINT",
                        },
                    ),
                },
            ],
            implementations: [
                Implementation {
                    name: "puts",
                    type_name: "puts",
                    linkage: External,
                    pou_type: Function,
                    statements: [],
                    location: SourceLocation {
                        span: Range(
                            TextLocation {
                                line: 5,
                                column: 0,
                                offset: 75,
                            }..TextLocation {
                                line: 5,
                                column: 12,
                                offset: 87,
                            },
                        ),
                        file: Some(
                            "examples\\hello_world.st",
                        ),
                    },
                    name_location: SourceLocation {
                        span: Range(
                            TextLocation {
                                line: 1,
                                column: 9,
                                offset: 20,
                            }..TextLocation {
                                line: 1,
                                column: 13,
                                offset: 24,
                            },
                        ),
                        file: Some(
                            "examples\\hello_world.st",
                        ),
                    },
                    overriding: false,
                    generic: false,
                    access: None,
                },
                Implementation {
                    name: "main",
                    type_name: "main",
                    linkage: Internal,
                    pou_type: Function,
                    statements: [
                        CallStatement {
                            operator: ReferenceExpr {
                                kind: Member(
                                    Identifier {
                                        name: "puts",
                                    },
                                ),
                                base: None,
                            },
                            parameters: Some(
                                LiteralString {
                                    value: "hello, world!\n",
                                    is_wide: false,
                                },
                            ),
                        },
                    ],
                    location: SourceLocation {
                        span: Range(
                            TextLocation {
                                line: 8,
                                column: 4,
                                offset: 114,
                            }..TextLocation {
                                line: 9,
                                column: 12,
                                offset: 151,
                            },
                        ),
                        file: Some(
                            "examples\\hello_world.st",
                        ),
                    },
                    name_location: SourceLocation {
                        span: Range(
                            TextLocation {
                                line: 7,
                                column: 9,
                                offset: 98,
                            }..TextLocation {
                                line: 7,
                                column: 13,
                                offset: 102,
                            },
                        ),
                        file: Some(
                            "examples\\hello_world.st",
                        ),
                    },
                    overriding: false,
                    generic: false,
                    access: None,
                },
            ],
            user_types: [],
            file_name: "examples\\hello_world.st",
        },
        {
            Datatype(
                "puts",
            ),
            Datatype(
                "STRING",
            ),
            Datatype(
                "main",
            ),
            Datatype(
                "__auto_pointer_to_STRING",
            ),
            Datatype(
                "DINT",
            ),
            Call(
                "puts",
            ),
            Datatype(
                "__STRING_14",
            ),
        },
        StringLiterals {
            utf08: {
                "hello, world!\n",
            },
            utf16: {},
        },
    ),
]

@volsa volsa linked an issue Jul 8, 2024 that may be closed by this pull request
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 91.17%. Comparing base (be78df6) to head (6d419be).
Report is 40 commits behind head on master.

Files Patch % Lines
compiler/plc_driver/src/lib.rs 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1256      +/-   ##
==========================================
+ Coverage   91.11%   91.17%   +0.06%     
==========================================
  Files         153      155       +2     
  Lines       44816    45497     +681     
==========================================
+ Hits        40833    41481     +648     
- Misses       3983     4016      +33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@volsa volsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@volsa volsa merged commit 4aea520 into PLC-lang:master Jul 9, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--ast flag
2 participants