Refinements to Piggy

While I now have Piggy producing a p/invoke header for a Clang-C header file, there are several improvements that I’ve made or will make soon.

In order to have code blocks and text blocks recognized as a single token by the Antlr-generated lexer and parser, I needed to make new delimiters for code and text blocks. C# code is now contained in {{ … }}; Text is now contained in [[ …. ]].

Antlr implements a means to allow user code to be inserted into parsers via a header option. Following by example, I’m going to generate a class to contain all code blocks that will be JIT compiled by the tool. The vars[] dictionary will be removed because the user will be able to add code with a header option in the spec file. Note, separate compilation and referenced assemblies do not work yet–I am getting load assembly errors.

Since a symbol table is a basic requirement for code generation, I’m going to be adding a symbol table to Piggy. In order to not re-invent the wheel, I’ve ported Parr’s Symbol Table into C#. However, it seems that it may need changes for enums.

The syntax for passes and templates is now changed. Templates are just parenthesized expressions, with the keyword template no longer used. Passes use curly braces to enclose all the templates for the pass: pass ID { template* }.

SWIG allows a user include other SWIG input files via a %include directive. Follow by example, I’m going to be adding an include mechanism for Piggy specs. The reason is that it’s a little much to always supply the full pass/template patterns for any conversion. Instead, you should be able to load the base conversion rules, then specialize that. I haven’t worked all this out, but I will very soon.

A useful feature in C# is string interpolation. In Piggy, C# interpolation is possible for attribute values, e.g., in the pattern ( SrcRange=$”{Templates.limit}” … ). which gets the string value limit that is part of the Templates class (where all code blocks go), currently a Regex pattern to match on clang-c. The result is an extremely powerful method of changing the pattern matcher based upon user code block values!!


Posted in Tip