[ Download Now | Download Statistics | Browse Source | SourceForge | Blog | asimjalis(nospam)gmail.com ]

MDef: The Generic Code Generation Framework

Hello World Example

Here is an example of MDef.

Overview 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.

When To Use MDef

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.

How To Run MDef

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

To use this in code:

mx MyMacro A B C

Or just:

MyMacro A B C

You can define a bunch of macros in a file (macro.lib) and then include it in many places as:

minc macro.lib

This is just replaced with the contents of the file.


Copyright © 2003, 2004 Asim Jalis.


To give feedback on this project, or if you found this useful, please send me e-mail.

Copyright © 2003, 2004, Asim Jalis (asimjalis(nospam)gmail.com). All rights reserved.

SourceForge.net Logo