[<<][staapl][>>][..]
Sun May 17 10:04:27 CEST 2009
removed front page stuff again..
I keep on doing this.. Why? Anyways, the new introduction says almost
the same as this, but with examples.
<h2>Basic Elements</h2>
<p>
Metaprogramming is about manipulating programs as data. A key
element here is the representation of programs. Being a compiler,
Staapl has two languages to deal with: a high-level input language
and a low-level output language. Both languages are
<a href="http://en.wikipedia.org/wiki/Concatenative_programming_language">concatenative</a>.
<ul>
<li> Output language programs are represented as lists of
instructions
<code>[op]</code>.
<li> Input language programs are represented as output language
program transformer functions <code>[op]->[op]</code>.
</ul>
<p> A meaning can be attached to high-level concatenative source
code using the following compilation algorithm:
<ol>
<li> Read the tokens from the input program source, and
associate each of them with an <code>[op]->[op]</code>
transformer.
<li> Compose all <code>[op]->[op]</code> transformers using
function composition, in the order their tokens appear in the
input program source.
<li> Apply this function to the empty program <code>[]</code> to
obtain the final output program.
</ol>
From this one could infer that each <code>[op]->[op]</code>
function appends a small machine code fragment to the code
accumulator, essentially behaving as
an <a href="http://en.wikipedia.org/wiki/Assembly_language#Macros">assembler
macro</a>. However, the function is free to modify the accumulated code in
its entirety, performing any optimization it sees fit.
<p> Using this representation the task of building code generators can
be split in these parts
<ul>
<li> High level: Composing generators/transformers is expressed
using concatenation. I.e. the Scheme form <code>(macro: 123
+)</code> creates a composite code transformer, built from the
code transformers <code>123</code> and <code>+</code>.
The <code>macro:</code> form behaves as quasiquote, facilitating
template programming. I.e. the previous transformer is
equivalent to <code>(let ((x 123)) (macro: ',x +))</code>.
<li> Low level: Creating language primitives as
<code>[op]</code> processors, and possibly defining a machine
instruction set <code>op</code> that has the double function of
representing target semantics and serving as a representation of
compile time data.
</ul>
<h2>Macro Forth</h2>
<p> By interpreting the code list <code>[op]</code> as a stack and
adding an <code>op</code> instruction <code>(QW value)</code> that
represents loading of a literal value on the
<a href="http://en.wikipedia.org/wiki/Forth_programming_language#The_stacks">parameter
stack</a> at run time, it is possible to implement Forth with eager
partial evaluation.
<p> I.e. the function associated to the token word <code>+</code>
would normally only compile machine code that pops 2 numbers from
the run-time stack, adds them and pushes the result to the stack.
Instead it could be made to inspect the code it is passed for any
instructions that load literal values on the runtime stack, remove
those, perform the addition at compile time, and generate code for a
constant instead.
<p> Note that the basic structure of <code>[op]</code> lists
and <code>[op]->[op]</code> transformers is more general than stack
languages. In fact, it could be used to implement partially
evaluating macro languages for any kind of state machine.
[Reply][About]
[<<][staapl][>>][..]