What are some code generation methods?

I am generating C ++ code and it looks like it will be very confusing, even my simple generation classes already have many special cases. Here is the code as it is now: http://github.com/alex/alex-s-language/tree/local%2Fcpp-generation/alexs_lang/cpp .

+4
c ++ python code-generation
Nov 16 '08 at 22:58
source share
8 answers

I wrote Cog in part to generate C ++ code from an XML data schema. It allows you to use Python code embedded in C ++ source files to generate a C ++ source.

+9
Nov 16 '08 at 23:06
source share

One method I used to generate the code was not to worry about formatting in the code generator. Then, as the next step after generating the code, run it indent to format it enough for you to read (and more importantly, debug) This.

+7
Nov 16 '08 at 23:12
source share

See Tool for creating test cases .

It is not clear what the problem is.

If you ask the question: "How do I handle all special cases in my generator classes?" then here are some tips. If your question is something else, update your question.

  • Use the template generator. Mako , for example, will make your life easier.

    Write an example of your result. Replace the spare parts with ${thing} . Since you started with what worked, it's easy to turn it into a template.

  • When creating code in another language, you need to have all the class definitions in another language, designed for flexible assembly. You want to create as little fresh new code as possible. You want to tweak and tweak a little, but you don't want to generate a lot of stuff from scratch.

  • Special cases are best treated with conventional polymorphism. Individual subclasses of a general superclass may implement various exceptions and special cases. In fact, difficult situations are handled well by the Strategy design template.

    Essentially, you have Python classes that represent real-world objects. These classes have attributes that can be entered into the C ++ template to generate the C ++ version of these objects.

+4
Nov 16 '08 at 23:23
source share

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

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

This is because your general program needs to deal with a lot of input data, and if a subset of this information changes very rarely, for example, once a week, the code generator should only install this subset. Generated code conditions on the remaining input, which changes more often. This is a strategy of separation and conquest. Another name for him is "partial assessment".

Generated code should also run much faster because it is less general.

In your particular case, there is no harm in creating code in 2 (or more) passes. As in step 1, you generate ads. In step 2, you generate the process code. Alternatively, you can generate two output streams and concatenate them at the end.

Hope this helps. Sorry if I just say that is obvious.

+1
Nov 21 '08 at 14:32
source share

as Ned suggested, Cog is a great tool for writing a template. For example, I had to write AOP-style event system support for specific classes so that it would work as follows:

  • you declare a method for the class
  • for each method, the event should be fired when called, passing the method arguments as event parameters

So, I made a special python declarator function that I would call in the cog scope, which would generate declarations and template definitions for each method and event. At the end of the cog area, the user places a block of code for a function that hides the implementation and is called by the AOP wrapper, something like this:

 class MyFoo { public: /*[[[cog import myAOPDeclarators AOP = myAOPDeclarators.AOP AOP.declareAOPInterceptorMethod( 'invokeSomeStuff' , '(int param1, std::string param2)' ) ]]]*/ //AOP wrapper void invokeSomeStuff_ImplementationAOP(int param1, std::string param2); void invokeSomeStuff(int param1, std::string param2) { sendAOPPreEvent( param1 , param2 , "invokeSomeStuff" ); invokeSomeStuff_ImplementationAOP( param1 , param2); } void invokeSomeStuff_ImplementationAOP(int param1, std::string param2) //[[[end]]] { // ...invokeSomeStuff implementation, not automatically generated } 

The best guide I can give you for generating code; make your generated code as readable as your manuscript written in . This makes the use of code transparency transparent (even more transparent than the code template, buy YMMV). Of course, as Greg suggested, you can indent after that, so it makes no sense to spend time mixing the indentation when creating the code if the tool can continue to process the source files anyway

+1
Mar 04 2018-12-12T00:
source share

I have a code generation system, and one of the best options that I took with me is to put most of the resulting program in non-generated code, for example. library / runtime. Using templates also works well. Complex templating systems can be difficult to work with manually, but you don’t work with them manually, so use this.

0
Nov 16 '08 at 23:25
source share

In fact, it will be just recursion directly, except that I need to pull out all the function declarations and put them in another place, as well as the fact that for all function calls I need to build a vector of all arguments, and then pass it to the function since C ++ has no syntax for vectors.

0
Nov 16 '08 at 23:41
source share

I was also looking for something like this and found this question. I was not very happy with cog, so in the end I wrote my own, similar, but it adds some (imo) very necessary functions.

https://github.com/icholy/swapm

0
Oct 19 '12 at 19:37
source share



All Articles