← SPL reference

Sample scripts

Self-contained examples you can paste into a .spl file or the REPL. Run with python someProgrammingLanguage/main.py yourfile.spl from the project root (paths may need adjusting).

Hello world

print.string("Hello, SPL");

Variables and arithmetic

a.setVar(10);
b.setVar(3);
sum.setVar(math.add(a, b));
print.number(sum);

Conditionals

n.setVar(7);
test.ifTrue(test.isGreater(n, 5)):
    print.string("n is greater than 5");
end;
test.else():
    print.string("n is not greater than 5");
end;

Loop

test.forLoop(1, 5, i):
    print.number(i);
end;

Function

functionDefine.double(x):
    return.number(math.multiply(x, 2));
end;

y.setVar(function.double(21));
print.number(y);

List

list.createList([1, 2, 3], nums);
first.setVar(list.get(0, nums));
print.number(first);

Try / except

error.try():
    q.setVar(math.div(1, 0));
end;
error.except(error.DivisionByZeroError):
    print.string("Caught division by zero");
end;

Function reference and list.sort

function.ref holds a function by name; function.call invokes it. Sort modes: alpha, numeric, ascii, utf-8, utf-16, utf-32.

functionDefine.double(x):
    return.number(math.multiply(x, 2));
end;
fn.setVar(function.ref(double));
print.number(function.call(fn, 4));
list.createList([3, 1, 2], xs);
list.sort(xs, "numeric", 0);
print.list(xs);

Minimal class (classDefine)

Put this in a .spl under lib/ or adjust use. See Classes & OOP for inheritance and visibility.

classDefine.Point():
x.setVar(0);
x.public();
constructorDefine():
this.x = 0;
end;
methodDefine.getX():
return.number(this.x);
end;
end;
class.createObj(p, Point);
print.number(p.getX());

Using another .spl library

The dotted path must resolve to a file on disk (see interpreter library resolution). Example if your working directory is the repo root:

use someProgrammingLanguage.lib.newMath;
print.number(function.power(2, 10));