Skip to content
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

Construct Header, Source files from an AST-like structure instead of Jinja templating #13

Merged
merged 52 commits into from Jun 13, 2023

Conversation

buckbaskin
Copy link
Owner

@buckbaskin buckbaskin commented May 7, 2023

FormaK aims to combine symbolic modeling for fast, efficient system modelling
with code generation to create performant code that is easy to use.

This design provides an extension to the fifth of the Five Keys
"C++ interfaces to support a variety of model uses" by reworking how C++
generation is done for easier extensions. After the Calibration design, a lot
of the code templates looked like:

        StateAndVariance
        ExtendedKalmanFilter::process_model(
            double dt,
            const StateAndVariance& input
            // clang-format off
{% if enable_calibration %}
            // clang-format on
            ,
            const Calibration& input_calibration
            // clang-format off
{% endif %}  // clang-format on
            // clang-format off
{% if enable_control %}
            // clang-format on
            ,
            const Control& input_control
            // clang-format off
{% endif %}  // clang-format on
        ) {

Instead of relying on increasingly intricate Jinja templating and managing
formatting via flagging clang-format on and off, I instead opted for another
approach: generate the code from an AST that approximated the Python AST. The
reason to go with something that approximates the Python AST is to have an
inspiration and a guide from an AST that has accumulated experience.

Afterwards, the code can look like:

        args = [
            Arg("double", "dt"),
            Arg("const StateAndVariance&", "input_state"),
        ]
        if enable_calibration:
            args.append(Arg("const Calibration&", "input_calibration"))
        if enable_control:
            args.append(Arg("const Control&", "input_control"))
        return FunctionDeclaration(
            "StateAndVariance",
            "process_model",
            args=args,
            modifier="",
        )

This approach isn't necessarily shorter, but it allows for replacing Jinja
templating with manipulating Python structures (primarily lists) in code. It
also generates cleaner code without droppings for clang-formatting

@buckbaskin buckbaskin self-assigned this May 7, 2023
@buckbaskin buckbaskin merged commit c068ed6 into master Jun 13, 2023
3 of 5 checks passed
@buckbaskin buckbaskin added the enhancement New feature or request label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant