Hurl is a toy general-purpose language which gives you everything you need: a variety of error handling mechanisms! And ways to generate errors.
Let's look at a few Hurl programs to get a feel for the language.
// define a variable
let x = 10;
// define an anonymous function
func(x, y) {
x + y
};
// bind that function to a variable
let add = func(x, y) {
x + y
};
// handle a conditional
try {
(1 == 2)
} catch (true) {
print("success!");
} catch (false) {
print("failure!");
}
// define a loop
let loop = func(loop, f, vals) {
try {
f(vals);
} catch (@vals) { // stores the exception into the local var vals
return; // returns to where the exception was thrown from
} catch (true) {
loop(loop, f, vals); // do another iteration
} catch (false) {
hurl vals; // throw the vals upward
}
};
// fizz buzz
let fb = func(args) {
let x = args.1 // 1-indexed of course
let max = args.2
try {
(x == max)
} catch (true) {
hurl false; // throw false with no ability to return
} catch (false) {}
let printed = false;
try {
(x % 3)
} catch (true) {
print("fizz");
printed = true;
} catch (false) {}
try {
(x % 5)
} catch (true) {
print("buzz");
printed = true;
} catch (false) {}
try {
printed
} catch (false) {
print(x)
} catch (true) {}
args.1 = x+1
toss args // throw a value with the ability to return
hurl true
}
loop(loop, fb, 0)
So, basic grammar of this would be something like this in BNF:
expr -> literal
unary
binary