Giter Site home page Giter Site logo

compiler-examples's Introduction

Example Compilers in Ruby

This repository contains compilers, written in Ruby, for several languages. They are intended to show people how to use the RLTK and RCGTK projects.

Brainfuck

The Brainfuck compiler is presented as-is, without any tutorial material. In the future a README will be added explaining the code and structure of the compiler.

Kazoo

The Kazoo compiler is organized into an eight-step tutorial that discusses each aspect of the project in detail. It covers many of the aspects of using both the RLTK and RCGTK projects. The tutorial will show you how to use RLTK/RCGTK to build a lexer, parser, AST nodes, and compiler for a toy language called Kazoo. The tutorial is based on the LLVM Kaleidoscope tutorial, but has been modified to:

  1. Be in Ruby
  2. Use a lexer and parser generator and
  3. Use a language that I call Kazoo, which is really just a cleaned up and simplified version of the Kaleidoscope language used in the LLVM tutorial (as opposed to the Kaleidoscope language from the 90′s).

The Kazoo toy language is a procedural language that allows you to define functions, use conditionals, and perform basic mathematical operations. Over the course of the tutorial we’ll extend Kazoo to support the if/then/else construct, for loops, JIT compilation, and a simple command line interface to the JIT.

Because we want to keep things simple the only datatype in Kazoo is a 64-bit floating point type (a C double or a Ruby float). As such, all values are implicitly double precision and the language doesn’t require type declarations. This gives the language a very nice and simple syntax. For example, the following example computes Fibonacci numbers:

def fib(x)
  if x < 3 then
    1
  else
    fib(x-1) + fib(x-2)

The tutorial is organized as follows:

Before starting this tutorial you should know about regular expressions, the basic ideas behind lexing and parsing, and be able to read context-free grammar (CFG) definitions. By the end of this tutorial we will have written 372 lines of source code and have a JIT compiler for a Turing complete language.

News

This repository contains the example and tutorial material that used to be included in the RLTK project. It is now presnted here, independent of the RLTK/RCGTK gems.

compiler-examples's People

Contributors

chriswailes avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

badstreams

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.