Parrot Virtual Machine/Building A Compiler

HLL Compilers
Now that we know NQP and PGE, we have the tools that we need to start writing a compiler for our favorite language. Let's review the steps to creating a compiler:


 * 1) Write Grammar using PGE
 * 2) Write Actions using NQP
 * 3) Actions create PAST tree
 * 4) PCT converts PAST into PIR
 * 5) Parrot converts PIR into bytecode.

After step 5, if we have done everything correctly, we should have a working compiler. Of course, there is a lot of work to do before we reach step 5. Lucky for us, however, steps 4 and 5 are automated by the build process, so we only need to worry about 1, 2, and 3. In the Tutorials section we are going to have several language-building tutorials that you can follow.

Creating a PAST Tree
PAST nodes are objects that your NQP Action methods must create. PCT automatically inserts these nodes into the tree once you create them. PAST nodes are complex objects with an array and a hash component. The array stores a list of references to the children nodes, and the hash stores information about the node itself. Creating the past tree means that we create nodes in lower-rules, and insert them into the array for higher rules. As we go, we set the necessary options for each rule, so that PCT can generate the appropriate code.

There is a reference for the various types of PAST nodes available in the appendix.

PAST::Node is the base class from which the other PAST node types are derived. The other types are PAST::Stmts, PAST::Val, PAST::Var, PAST::Op, and PAST::Block.

PAST::Op
 * PAST::Op nodes are operations that need to be performed.

PAST::Val
 * PAST::VAL nodes are constant values like integers or strings

PAST::Var
 * PAST::Var nodes are variables

PAST::Block
 * PAST::Block nodes are used for lexically scoping variables and defining subroutines. They keep other nodes together.

PAST::Stmts
 * PAST::Stmts nodes are just groups of other nodes, and perform no task besides basic organization.

Once you have created the necessary node, you insert it into the parse tree with the keyword.

The Match Object
The Match object is a special object that contains a hash. The match object hash contains information about the matched rule. Each element in the hash is named after one of the sub-rules in the rule. For instance, if we have the following rule:

rule sentence { }

Then the match rule would have entries "adjective", "noun", "adverb", and "verb". The value of the hash for each of these keys are the values that were returned from the subrules using the  command, if any. So, we would have the following fields:

$ $  $  $

We would then use the values of each of these to produce and  a node for the   rule. If each of these fields return PAST nodes of their own, we should push those

Examples
We are going to show some very small examples here to demonstrate the basic compiler construction method. More advanced tutorials are provided in the Tutorials section.