ltcmelo / psychec

A compiler frontend for the C programming language
BSD 3-Clause "New" or "Revised" License
536 stars 39 forks source link

Emit C code from user-defined AST? #87

Open SamuelMarks opened 2 years ago

SamuelMarks commented 2 years ago

In Python I can do this:

class_reduction_v2 = ClassDef(
    name="ReductionV2",
    bases=[],
    keywords=[],
    body=[
        Expr(value=set_value("foo")),
        Assign(targets=[Name(id="AUTO", ctx=Store())], value=set_value("auto")),
        Assign(targets=[Name(id="NONE", ctx=Store())], value=set_value("none")),
        Assign(targets=[Name(id="SUM", ctx=Store())], value=set_value("sum")),
        Assign(
            targets=[Name(id="SUM_OVER_BATCH_SIZE", ctx=Store())],
            value=set_value("sum_over_batch_size"),
        ),
    ],
    decorator_list=[],
)

Which produces:

class ReductionV2:
    """ foo """
    AUTO = 'auto'
    NONE = 'none'
    SUM = 'sum'
    SUM_OVER_BATCH_SIZE = 'sum_over_batch_size'

Looks like neither LLVM LibTooling nor LLVM libclang have support for this: https://discourse.llvm.org/t/create-and-emit-ast-for-struct-clang-recorddecl/61383

Is this something you would consider adding to Psyche-C? 🥇

ltcmelo commented 2 years ago

Hi @SamuelMarks . I wouldn't implement such a feature myself, but I'd be willing to accept it as a contribution (if the implementation doesn't cause side effects for the main use of psyche-c's AST).