So how is your life? Did you ever wonder how it would look through two gin and tonics, and a peanut butter pita? Like this. Wait, one downed and one mixed, that doesn't really make two does it? Forgive me if this deteriorates though.

Ok, now before hitting the big subject of code gen, let me share with you the best tasting sandwich on this very underrated earth. First thing, you will have to travel to Winnipeg, to pick up 'Winnipeg Harvest' bread. Now this is a great grain laden bread, however I seriously doubt any of the grain was actually harvested in Winnipeg. Having spent years there, I know it to be a shit hole. And while you can meet some good buddies in such a place, you can not grow shit for wheat there. That has to come from out here -- rural Manitoba, home of the best wheat in rural Manitoba. So two slices of this in the toaster. Next, avocado, need I say more, other than that screw movies, thank you California -- first for avocado, second for everything else. Cut around the seed, top to bottom, twist, et voila. To get the seed out of side number two, whack the knife in it and twist. Cut in long slices, about four or five per half.

Now the foreign part. And yes, every kick ass recipe has a foreign part, anyone who thinks less is just arrogant and probably lonely (unless they are Turkish, where in fact you can find almost all the best food in the world, weird thing). So, the foreign part. Baba Ghanouj (pronounced: ee'tit, don sa'it). Oh la la. Sooo fricking tasty. Now, to get my exact sandwich, you would also have to buy that around here -- but you might as well just drop by and have one of mine if you are that close, so lets look around. I'm not sure if recipes are jealously guarded in the Middle East too, but that would be your best option if you have a friend there. A tip to getting the recipe - don't try to use the "but we are bringing you democracy" speech as a form of persuasion. Baba Ghanouj rises far above any transient political squabbles the bottom two percents of our populations may be having at any given moment. Besides, I can vote in Canada, and trust me, Baba Ghanouj is well well above that experience. Maybe just hunt through a good supermarket. Spread it on the toast, buttered of course, and add the slices of avocado.

The Coup de Gras (that's 'cut the lawn' for all you Anglos), is a generous squeeze of lemon. The larger surface area of thinly chopped lettuce will help absorb more this precious liquid if you like, or mix with parsley if you are that kind of person. A bit of pepper (um, obviously hand ground, or hammer ground if the grinder is down), maybe a few leaves of cilantro.

How good is it? Put it this way: I owe Jenn deHann a gigantic Calgary Flames face tattoo favour, during the playoffs no less, yet if I could find a way to deliver this work of art to Alberta still warm, I will consider myself even. As a human, you will find this sandwich alone worth the price of admission - by all means do not die without trying it, and until then watch out for large busses.

Ok, five paragraphs and still no code gen. Pissed off? Ok, here we are. I've used CodeGen for a few years now. It started innocently enough, just rolling my own on the weekends. Pretty soon it became a regular habit at my job. Ok I'm shitting you, it isn't that big of deal, but at the right time it is nice. Here is an example, swf parsing. It is something big and hard, made small and not quite as hard -- which generally indicates satisfaction or cold water. Swf parsing isn't so complicated these days, however it still involves a fucking lot of typing. Code gen (not to be confused with 'Coder Jenn') can save your life here. If you like ASP, you will love CodeSmith. However if you are like most reasonable people, you hate ASP (and all those other dirty server side html sewer spewers). However you might still tolerate it if it saves a bit of typing.

Six paragraphs and still no code gen. The trick to code gen is to realize that all those things you learned about 'good practices' need not apply. You just care about the text that is generated, and you probably won't even save the abomination that shat it out. Many will tell you that generated code should only be complete (eg. never be edited). These people are both full of themselves and full of crap, both at once, leaking from each end I suppose. Code gen hath many levels, and level one is cranking out shit you'd rather not type. So rule number one of code gen is, formally: "if it works once, loop it to death". Rule two is: "generate all you possibly can, because typing is for secretaries". And yes I have a chip on my shoulder with secretaries, because they can type without looking. Rule three is: "the generated code should always parse, unless you are being clever and throwing errors as markers for the compiler to catch". Rule four in code gen is: "Rules are for pussies". I guess at this point you have to decide where you fall here. Rule five is "generated code should only be complete (eg. never be edited)". Clearly the pussy delineation happened around four.

Seven paragraphs, you may be starting to think this is a blog gen experiment. Did I mention Baba Ghanouj? Yes? Do you have any hobbies? Ok, on with your handy dandy (partial) swf parser/generator. You can cover a lot of ground with a list of stuff and some stuff transforming code. That may sound technical, but really it isn't any more complicated than that mail merge stuff in Word -- except that it involves programming, and doesn't expect the compiler to get warm and fuzzy towards a corporation after seeing its own name in print. So for the list, lets look only at the actions, as that is shorter. Each action tag has a name and number. They also can have properties/arguments - for example if you jump, you generally have to jump somewhere. In swf's case, this is an offset, and it is an integer. So in our list we will have the format:

TagID,Name,Arg1,Arg1Type,Arg2,Arg2Type...

for example:

...
0x9D,If,BranchOffset,uint
0x99,Jump,BranchOffset,uint
0x9E,Call
0x1C,GetVariable
0x1D,SetVariable
0x9A,GetURL2,SendVarsMethod,UrlSendMethod,LoadTarget,LoadTarget,LoadVariables,bool
...

Now here is the meat of the conversation. With this list, you can not only generate a bunch of classes that represent the actions (like a swfActionDom, if you have the currently trendy 'Dom' suffix fetish) -- you can also generate a parser, and a generator. Heck, even a player if you like. Well, at least a pretty good skeleton of these. The dom will be the classes and properties, along with code folding, doc comments, version attributes etc. The other two usually boil down to a giant switch statement in a recursive method. Swf actions are pretty flat, but say for the C# dom you have namespaces with classes with methods with statements with expressions etc. The workings of this can all be generated with a good typed list.

Ok, for the rest, I can only point to examples, as explaining it without more context is futile anyway. Personally I tend to generate code then leave it around for a while. It gets regenerated until I've stopped adjusting things. Once the missing peices start getting filled in the code gen is useless, as regenerating would overwrite stuff. Of course with a 'compiler generator' like antlr you would probably never do this (hand edit), but code gen should be treated as a big step below that imo. Anyway, I say this because this means there may be little (or big) adjustments that were later made to this generated code, by hand. Eg. this code isn't necessarily all correct and final, it was the starting point. I would have posted the C# dom stuff as it was more interesting, but I didn't save it (in fact I overwrote it with this swf gen code, oh well, can't be precious with crappy ASP code).

As for time, it took about one hour to go through the swf spec and make the list. Then some time to make the code by hand for one or maybe two items in the list. Being that this is about the tenth time doing this kind of thing, it was maybe 15 mins with a lot of copy and paste, but it can take more of course. This gets the desired structure, and insures everything makes sense/compiles. Translating into ASP/C# is something I really hate doing, but it doesn't take much time. Actually the coding part is fun enough, it is all the wiggly little formatting garbage, and lack of sensible error messages I hate. Oh and typing <% %> drives me batty. Just create variable names from items in the list, and insert them where they belong. If you know nothing about asp don't worry, it is they type of thing you can just shuffle around sample code and get to work.

For some reason AgentSmith (err, CodeSmith) compiles this code every time you open it, which is slow enough to be annoying. Anyway, that is the long part, the actual generation of course is in bullet time. Not even time to bite into a sandwich. Of course there are other uses for code gen - notably property and collection generation. These also work better on lists. And there are less noteable uses, like suicide notes for refactored classes.

Here are some examples. If you just skipped down here, you missed the really good dirty joke above.

List of swf actions
transformed by:
ActionsDom asp code
ActionsParser asp code
ActionsGen asp code
creates:
Dom output
Parser output
generator output

Sample property generator

Cheers,
Robin

posted on Thursday, June 03, 2004 2:47 PM
Feedback
  • # re: CodeGen is your buddy, I'm just your friend
    Burak KALAYCI
    Posted @ 6/3/2004 3:42 PM
    That food link made me hungry! Are you planning a visit to Turkey?

    Best regards,
    Burak

  • # re: CodeGen is your buddy, I'm just your friend
    Robin Debreuil
    Posted @ 6/3/2004 4:37 PM
    I am determined to go back to Turkey at one point, first opportunity or excuse. If you are up for it one day, I'd like nothing better than to spend an afternoon in an Istanbul cafe, eating said food and talking about nothing in paticular.

    Until then, I'm watching out for large busses : ).


  • # re: CodeGen is your buddy, I'm just your friend
    stacey
    Posted @ 6/3/2004 5:00 PM
    IF you ever manage a way to transport that sandwich hot and toasty to Alberta, count me in. I'll take two, and a smile & a water, cos they are free.

    I went through Winnipeg. It could potentially be the asshole of the universe ( thank you, Kurt Vonnegut, eternally, for that wonderful phrase). If it isn't, then Smith Falls in Ontario is its biggest competition in the Canada division.



  • # re: CodeGen is your buddy, I'm just your friend
    Robin Debreuil
    Posted @ 6/4/2004 4:27 AM
    You do realize that if Winnipeg is in fact the asshole of the universe, and you passed through it, umm...

    My theory is that the universe is full of assholes. I think this can actually account for most of the missing dark matter. I often see the earth having a giant butt crack in it, but the only way I can place the hoolio so far north is if earth is bent over rinsing it's face in the toilet. This is an appealing theory -- certainly it would explain a lot of other things, but in spite of the city being small dirty and emarrassing, 90% of the universes mass? It just seems too significant.

    But how about that game! Maybe you guys can just warm these things up on the flames, I'll send them frozen. I totally enjoyed that...




  • # re: CodeGen is your buddy, I'm just your friend
    Nik Khilnani
    Posted @ 6/11/2004 11:16 AM
    very hungry over here....

    thanks for publishing ure findings n studies..
    nik

  • # re: CodeGen is your buddy, I'm just your friend
    veejay
    Posted @ 6/15/2004 8:58 AM
    3 large eggplant
    ¾ cup tahini (sesame seed paste)
    ½ cup lemon juice
    2 teaspoons minced or pressed garlic
    1 teaspoon cumin
    Pinch of cayenne
    1 teaspoon salt

    Garnish with one of the following, if desired:
    6 or 8 chopped black olives
    Thin layer of olive oil
    Chopped parsley



  • # Using Code Generation to Create a Parser
    ThoughtChain
    Posted @ 8/30/2004 2:37 PM


Blog Stats

  • Posts - 121
  • Stories - 1
  • Comments - 1441
  • Trackbacks - 47

.Net Blogs

01101 Blogs

Flash Blogs

Graphics

People