koi-reference-compiler

This is the reference compiler implementation for the programming language Koi. This is a very simple compiler that does no optimization, and because of this remains very easy to understand. The compiler is implemented using the delegation pattern in that a tree representing the program is built up out of subclasses of the SyntaxNode class, each of which knows how to compile it’s own specific instruction and nothing more.

Example

Take, for example, a very simple (and incorrect!) program that just pushes an integer onto the stack. The Abstract Syntax Tree (AST) for such a program might look like this:

<IntegerLiteral "1">

In order to compile this program we might create an instance method on the IntegerLiteral class that looks like this:

class IntegerLiteral < SyntaxNode
  def compile
    return [PUSH_INT, self.text_value.to_i]
  end
end

This method simply produces the opcode PUSH_INT and provides the integer value of the text_value of the syntax node, in this case “1”. This is obviously a minimal case, but it demonstrates the principal in a straight-forward way.

Abstract Syntax Tree Format

The compiler expects the AST to be passed to it in the form of a nested hash which represents the program structure. The AST hash for the program:

test = 1

would look like so:

{
  :name => "Block",
  :text_value => "test = 1",
  :offset => 0,
  :elements => [
    {
      :name => "Statement",
      :text_value => "test = 1",
      :offset => 0,
      :elements => [
        {
          :name => "Assignment",
          :text_value => "test = 1",
          :offset => 0,
          :elements => [
            {
              :name => "Identifier",
              :text_value => "test",
              :offset => 0,
              :elements => nil
            },
            {
              :name => "AssignmentOperator",
              :text_value => "+",
              :offset => 5,
              :elements => nil
            },
            {
              :name => "Expression",
              :text_value => "1",
              :offset => 7,
              :elements => [
                {
                  :name => "IntegerLiteral",
                  :text_value => "1",
                  :offset => 7,
                  :elements => nil
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Compilation Target

This compiler produces opcodes for KoiVM virtual machines. For the reference implementation of a Koi Virtual Machine please have a look at: koi-vm-ruby

Installation

This compiler is normally installed as part of Koi’s default toolchain. However if you would like to install it on it’s own you can do so by installing the gem like so:

gem install koi-reference-compiler

Usage

require 'rubygems'
require 'koi-reference-compiler'

include KoiReferenceCompiler

bytecode = Compiler.compile( hash_representing_abstract_syntax_tree )

Author & Credits

Author

Aaron Gough

Copyright © 2010 Aaron Gough (thingsaaronmade.com), released under the MIT license