README file of the MetaC Compiler "metacc"
What is MetaC?
MetaC is a metalanguage that is based on the C language. Programs written in MetaC have two parts: a meta-program and a regular program. The meta-program is executed at compile-time and the regular program provides the code for execution at run-time.
With MetaC you can analyze and modify the regular program.
Getting started:
To get started, first read this file, then go to the examples directory and take a look and compile the provided MetaC programs. First file to start with is hello.mc (01.mc) and follow the sequence of the dot-mc files.
To build a MetaC program, you have to go through the following steps:
- Compile the MetaC program:
$ metacc -c example.mc
This will produce a file example.i
- Compile the generated C source code with a normal C compiler:
$ cc example.i -o example
- Execute the program:
$ ./example
Some MetaC files are based on the 1999 version of the C standard. As
support for this standard is incomplete up to now, you must manually
enable it with "-std=c99". Additionally, certain extensions of the GNU
compiler collection are supported. These must be enabled with the
"-xgnu" flag on the command line. "metacc" provides even more options
that can be set on the command line. For a full list of available
switches and arguments type:
$ metacc -help
During the execution of metacc the meta-program is run. After this all meta-functions and meta variables are stripped off the source code and the source code is printed to the standard output, if you didn't provide the -c flag or set explicitly an output file with the -o flag.
The MetaC language:
The core of the language is based on two concepts:
- Special meta-data-types for referencing certain source code structures,
based on their syntax.
- A methodology to define code-structure-patterns that can be used to
search for certain code structures in the sources and to instantiate
new code.
The following meta-data-types are available:
- zed: The keyword zed stands for the letter "Z" that is the name of the
set of whole numbers. It is the type of a variable that can hold an
integer value that can be used for calculations within meta-programs
and to instantiate integer-literals of arbitrary type.
- real: The same as zed, but for floating point values.
- strg: Refers to a string literal, including the enclosing quotation
marks.
- ident: Holds a lexical token of type identifier. I.e. it is a
character sequence that can be used to give variables a name. In
accordance to the C standard an identifier may use any combination of
letters (A-Z, a-z), numbers (0-9), and the underscore (_), provided
that the first character is not a number.
- expr: Refers to an expression. I.e. binary expression such as (+,|),
unary expressions (++,--), postfix expressions (function calls,
operator ->), and so on.
- stmt: Refers to a statement. Valid statements are e.g. for-, while-,
do-, expression-, if-, switch-statement.
- type: Encapsulates the type of an object or function. E.g. int *,
void *(*)(int, float, struct st *).
- symb: Refers to the definition of a variable and (indirectly) to its uses.
- func: Is a special form of symb that is a symbol that is known to be
function that is defined in the current translation-unit.
- scope: A metavariable of type scope refers the area of code in which a
certain symb is visible. Additionally, it provides access to all
definitions visible in this area.
Derived data-types (i.e. functions, arrays, pointers, and structs) can be constructed, too. Union types are unavailable, as the meta-data-types have unspecified layout and size in memory. Arrays are enhanced in a way that they can be resized dynamically, by accessing the element after the last valid element. The newly added element is initialized with a null-equivalent value for all meta-data types. Pointers are implemented as references to objects or to objects within arrays as base reference plus offset. In consequence pointer are valid after a resizing an array with a possibly required relocation of the array-data in memory.
The dynamic semantics of statements of meta-programs written in MetaC are equivalent to their logical base. I.e. an if-statement in a MetaC meta-program has the same runtime behavior as an if-statement in C. The semantics of C sources within MetaC source code is unchanged.
Feedback and bugs:
Please read the file FEEDBACK concerning these topics.
