Programming Language Development 101 - The Language

programming-languages

Let's start by taking a look at the language we're going to be developing.

We'll call the language plod (expanded from pld, short for Programming Language Development). It will be a dynamically-typed language that takes inspiration from JavaScript, PHP and Go. Here's an example script that calculates the nth Fibonacci number recursively.

func fib(n) {
    if n < 2 {
        return n
    }

    return fib(n - 1) + fib(n - 2)
}

var result = fib(30)

println("fib(30) = {}", result)

Functions are defined using the func keyword followed by an identifier. Parameters are defined inside of parentheses.

Control structures don't require parentheses around the expressions and semi-colons are optional.

Variables are created using the var keyword followed by an identifier.

String literals are constructed by wrapping text in "". Functions are called by suffixing an identifier with ().

A complete dump of the language can be seen below with some comments.

// Function definitions.
func foo() {

}

// Variable declaration with initial value, a string literal.
var name = "Ryan";

// Mathematical operators.
1 + 2;
3 - 4;
5 * 6;
7 / 8;

// Boolean operators.
true && true;
true || false;

// Array literals.
[1, 2, 3];

// Function calls.
foo();

// Structural definitions.
struct Person {
    name,
    email,
    age
}

// Adding methods to structures.
impl Person {
    func hello() {
        return this.name
    }
}

// Instantiating a structure.
var Ryan = Person { name: "Ryan", email: "ryan@ryangjchandler.co.uk", age: 100 };

You might also enjoy...

Found this article useful? Share on Twitter!