Git Product home page Git Product logo

alpha's Introduction

Alpha

Reference compiler for the Alpha programming language. Inspired by Andrew W. Appel's Modern Compiler Implementation in ML.

What is Alpha?

  • Alpha is an garbage collected, imperative-styled programming language with features known from object oriented and functional languages.
  • Language constructs such as if/else, while, for and function calls work as expected.
  • Functions are first-class, meaning that they can be parsed around just as regular variables
  • Lambda functions capture their lexically enclosing variables. These two features allows a continuation-passing style approach as seen below.
fun sum(int n, fun(int) -> int f) -> int {
    if(n == 0) {
        return f(0);
    }
    else {
        return sum(n-1, (int k) -> int {
            return f(k) + n;
        });
    }
}

fun identity(int n) -> int {
    return n;
}

fun main() -> int {
    int n = sum(10, identity);
    return n;
}

Or, using a more procedural style, by summing the values of an array:

fun sum([int] a, int n) -> int {
    int total = 0;
    for(int i = 0; i < n; ++i) {
        total += a[i];
    }
    return total;
}

fun main() -> int {
    var a = int[10];
    for(int i = 0; i < 10; ++i) {
        a[i] = i+1;
    }
    return sum(a, 10);
}
  • Floating point computation is achieved using the float datatype. The following example shows a simple approximation of pi using Leibniz formula for pi:
fun calcPi(float n) -> float {
    float pi = 1.0;
    for (float i = 3.0; i <= n; i += 4.0) {
        pi -= 1.0 / i;
        pi += 1.0 / (i + 2.0);
    }
    return pi * 4.0;
}

fun main() -> float {
    return calcPi(1000.0);
}
  • Interface- and class declaration syntax is shamelessly stolen from Java. The following shows how an interface is implemented, and class extension is provided using the extends keyword.
interface BinOp {
    public fun apply(int a, int b) -> int;
}

class Plus implements BinOp {
    public fun apply(int a, int b) -> int {
        return a + b;
    }
}

fun main() -> int {
    BinOp plus = Plus();
    return plus.apply(1, 3);
}

What is [not yet] implemented?

Frontend

  • Switch statements
  • Exceptions

Backend

  • Various optimizations. The first passes to be written are
    • Constant propagation and constant folding
    • Common subexpression elimination
    • Tail recursion
    • Loop-invariant code motion

alpha's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.