Here is an example of MDef.
MDef (Macro Definition Compiler) is a framework that allows you to extend any language with new constructs, and to eliminate repetition, and to write specialized code generators. You can extract any commonly occurring pattern and reuse it over and over again. You code at a higher level and your productivity multiplies. Instead of thinking at the level of stacks and memory addresses, you think in terms of code layout and code structures.
MDef is based on the idea of a generic code generator that you can use to generate any code that follows a simpler pattern.
This is not an academic theory but a real tool that you can use in your projects today. You can use it for most common languages such as Perl, Java, C#, C, C++, Python, Ruby, Bash/Sh, VB, and other languages.
The basic idea of metaprogramming or code generation is that instead of writing programs, you write programs that write programs.
The basic idea of MDef is to systematize cut-and-paste. It lets you define code templates which you can then expand to generate specific code.
The fun begins when you define templates that define other templates. See the accompanying example files for examples of this.
Instead of telling the code what to do you just show it what to do and then let it fill in the blanks.
Another way to look at this. In a way this is Code Oriented Programming. Instead of dealing with objects and data structures, you start thinking in terms of code blobs and you throw them around.
You must have Perl installed on your machine. To run the program type:
mdef.pl file1 file2 file > final.c
The program outputs to standard output which you can redirect and save before the compilation step. The file final.c can be final.cs, or final.pl, depending on what programming language you are generating. It will contain the generated source code, which you will need to compile before using it.
Now it makes sense that since this is a template based system instead of giving you a lot of talk on how to make this work, I just give you a template of the templates.
Define a macro template as follows:
mdef MyMacro arg1 arg2 arg3 code arg1 mend
To use this in code:
mx MyMacro A B C
MyMacro A B C
You can define a bunch of macros in a file (macro.lib) and then include it in many places as:
This is just replaced with the contents of the file.
To give feedback on this project, or if you found this useful, please send me e-mail.