What are some techniques for code generation?

前端 未结 8 1108
抹茶落季
抹茶落季 2021-01-06 13:05

I\'m generating C++ code, and it seems like it\'s going to get very messy, even my simple generating classes already have tons of special cases. Here is the code as it stan

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 13:22

    I agree with S.Lott, that you should write out an example of what you want to generate.

    Solving a problem with code generation should be less complicated than without.

    This is because your total program has to deal with a lot of input information, and if a subset of that information changes very seldom, like once a week, the code generator only has to condition on that subset. The generated code conditions on the remaining input that changes more frequently. It's a divide-and-conquer strategy. Another name for it is "partial evaluation".

    Generated code should also run a lot faster because it's less general.

    In your specific case, there's no harm in doing the code generation in 2 (or more) passes. Like on pass 1 you generate declarations. On pass 2 you generate process code. Alternatively you could generate two output streams, and concatenate them at the end.

    Hope that helps. Sorry if I'm just saying what's obvious.

提交回复
热议问题