Giter VIP home page Giter VIP logo

compiler-in-rust's Introduction

A compiler that emits bytecode and a VM that could decode and execute said bytecode.

This is written in Rust by following the books from Thorsten Ball on writing a compiler in Go.
The compiler is built from scratch and it includes a lexer (tokenizer), a parser, a compiler and a vm.

This project is an extension to a previously written interpreter project.
Except for the tree walking evaluation part, both lexer and parser have been ported from that project.


Features

  • The language supports int, string and boolean data types.
  • Supports composite data types: Arrays and HashMaps.
  • Supports common operators like +, -, ==, !=, <, > etc.
  • Supports let, return and while statements.
  • Supports assignments, if/else expressions and function expressions.
  • Supports higher order functions and closures.
  • Have a range of built-in functions such as len, print, push, sleep etc.
  • Supports indexing on arrays, strings and HashMaps.
  • Supports Range indexing on arrays and strings.

Usage

  • Download the asset file relevant to your platform from the latest release.
  • Extract the zip and run the executable to start the REPL.
  • Run the executable with relative filepath as an argument to execute a script file.

Example Code

let map = fn(arr, callback) {
    let new_arr = [];
    let i = 0;

    while (i < len(arr)) {
        let mapped = callback(arr[i]);
        push(new_arr, mapped);
        i = i + 1;
    }

    return new_arr;
}

let add_1 = fn(x) {
    return x + 1;
}

let arr = [10, 20, 30, 40, 50];
map(arr, add_1);

More code examples can be found in tests/testfiles directory.

Methodology

  • The lexer does the tokenization of the code input.
  • The tokens will be fed in to the parser, which forwards the tokens as it parses the program statements one by one.
  • To parse expressions, pratt parsing is used (recursive decent parsing).
  • For each statement/expression parsed, the parser will create a corresponding AST node to be later evaluated.
  • Once the parsing is finished, the compiler will walk through the AST node recursively and emit the instructions and index the constants.
  • The VM will then run the program using the emitted instructions and the constant index.

compiler-in-rust's People

Contributors

panduladw avatar

Watchers

 avatar

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.