Home
Consulting
Advisories
Software
Articles
Contact

Aphid Syntax

Introduction | Try It Online | Syntax | Types | Derivatives | Download

Strings

String literals can be written using either single quotes or double quotes. The backslash character (\) is used for escape sequences.

x = 'foobar\r\n';

Numbers

Number literals can be written in decimal, hexadecimal, or binary form.

x = 1;
x = 3.14159265359;
x = 0xDEADBEEF;
x = 0b1000;

Booleans

Boolean values can be specified using the true or false keywords.

x = true;
x = false;

Lists

Lists are comprised of comma (,) delimited elements enclosed in square brackets ([ ]).

x = [];
x = [ 5, 2, 3, 0 ];

Objects

Objects are comprised of comma (,) delimited name-value pairs enclosed in braces ({ }). Names and values are separated by a colon (:).

widget = {
    name: 'My Widget',
    location: { x: 10, y: 20 }
};

Functions

Functions can be called using standard C-style syntax.

x = add(10, y);

To declare a function, use the function operator (@).

foo = @(x) {
    /* do something */
};

When no parameters are specified, the parentheses can be omitted.

foo = @{
    /* do something */
};

A value can be returned from a function by using the ret keyword.

foo = @{
    ret 'Hello';
};

If a function body is comprised of a single return statement, it can be written as a lambda expression.

add = @(x, y) x + y;

// is the same as

add = @(x, y) {
    ret x + y;
};

The function operator (@) can also be used to perform partial function application.

add = @(x, y) x + y;
add10 = @add(10);

The pipeline operator (|>) can be used as an alternative to the typical C-style call syntax. This is generally preferrable to nested calls.

square = @(x) x * x;
5 |> square |> print;

Partial function application can be utilized to pipeline functions that accept more than one argument.

add = @(x, y) x + y;
square = @(x) x * x;
5 |> square |> @add(3) |> print;

When pipelining to a function or partial function expression, the pipeline operator (|>) can be omitted.

add = @(x, y) x + y;
5 @(x) x * x @add(3) |> print;

Control Structures

The if and else keywords can be used to write conditional statements.

if (x == 1) {
    print('x is 1');
} else {
    print('x is not 1');
}

Curly braces can be omitted if the condition body is a single statement.

if (x == 1)
    print('x is 1');
else
    print('x is not 1');

The for keyword can be used to create a typical for loop.

l = [ 1, 2, 3 ];

for (x = 0; x < l.count(); x++) {
    print(l[x]);
}

The for keyword can also be used in conjunction with in to create a foreach loop.

for (x in [ 'a', 'b', 'c' ]) {
    print(x);
}

The while keyword can be used to create while loops.

x = 0;

while (x < 5) {
    x++;
    print(x);
}

Switch statements can be declared with the switch keyword. Rather than using the typical case/break, Aphid uses a syntax closer to the other control structures. A default case can be specified with the default keyword.

x = 5;

switch (x) {
    1: {
        print('Is 1');
    }

    2: {
        print('Is 2');
    }

    default: {
        print('Is not 1 or 2');
    }
}

If a case body consists of a single expression, the curly braces can be omitted.

x = 5;

switch (x) {
    1: print('Is 1');
    2: print('Is 2');
    default: print('Is not 1 or 2');
}


Copyright © 2018 AutoSec Tools LLC