Designing a Language

Building a language and an interpreter for it from scratch


Project maintained by FallenDeity Hosted on GitHub Pages — Theme by mattgraham

Language Design

đź“ť Note
Currently in early stage all language design decisions will follow the tutorial and examples from here.

First Look

// This is a comment
print "Hello, World!";

Current Lox will follow syntax of C-like languages, but with some differences. It will have the following features:

Data Types

Lox will have the following data types:

Expressions

Lox will have the following expressions:

Statements

A statement’s job is to produce some effect. They are executed for their side effects such as output, input, or assignment and modification of variables.

Variables

Variables are used to store values. They are created with the var keyword.

var a = 1;
var b;  // Uninitialized variable defaults to `nil`

Control Flow

Lox will have the following control flow statements:

Functions

Functions are used to group statements together to perform some task. They are created with the fun keyword.

fun add(a, b) {
    return a + b;
}


fun main() {
    print add(1, 2);  // returns `nil` since main() doesn't return anything
}

Functions are considered first-class citizens in Lox. This means that they can be passed around like any other value.

fun add(a, b) {
    return a + b;
}

fun main() {
    var adder = add;
    print adder(1, 2);
}
fun wrapper()
{
	fun inner()
	{
		print "Hello, World!";
	}
}

Classes

There are two ways to approach objects in OOP:

Class-based

In class-based languages, classes are the blueprints for objects. They define what properties and methods an object will have. Objects are instances of classes. Instances store state in fields, and behavior is defined by methods.

Class-based

Prototype-based

In prototype-based languages, objects are the blueprints for objects. They define what properties and methods an object will have. Objects are instances of other objects.

Prototype-based

Lox Approach

Lox will follow the class-based approach.

class Person {
    init(name) {
        this.name = name;
    }

    greet() {
        print "Hello, my name is " + this.name + ".";
    }
}


var person = Person("John");
person.greet();

Inheritance in Lox

class Person {
    init(name) {
        this.name = name;
    }

    greet() {
        print "Hello, my name is " + this.name + ".";
    }
}


class Student < Person {
    init(name, id) {
        super.init(name);
        this.id = id;
    }

    greet() {
        super.greet();
        print "My student ID is " + this.id + ".";
    }
}

Standard Library

For now standard libraries in Lox will be limited to the default clock() method. I will slowly add other methods to give it basic functionality.

TODO

Look for the complete list of methods here

PyLox our Interpreter

PyLox is a tree walk interpreter written in Python. It will be used as the default interpreter for Lox.

References

Previous: Home Next: Scanner