The Ox Programming Language
Tokenization
Expression Parsing
Operator Reference
Statement Reference
Reference Implementation
Extending the Language
Tokenization
Character Encoding
Whitespace
Comments
Identifiers
Character Encoding
The language requires a character encoding that is a superset of ASCII,
otherwise it is character encoding agnostic.
Whitespace
Tabs (HT), newlines (LF) and spaces (SP) and comments are considered whitespace.
Line comments
When a # character is found outside of a string, and the next character
is not *, the rest of the line is considered a comment.
Example:
# This is a line comment
Block comments
When a # character is found outside of a string, and the next character
is *, a comment is started that continues until the sequence
*# is found.
Block comments may span multiple lines and may be nested.
Example:
#* This is a block comment.
#* Block comments may be nested *#
and span multiple lines. *#
Identifiers
Identifiers may consist of the characters
A–Z, a–z,
0–9, _, and characters with an ordinal value of
128 or higher. The first character may not be an ASCII digit.
Identifiers are case sensitive, abc and ABC are two distinct
identifiers.
Keywords
A keyword is an identifier that is reserved by the language and cannot
be used in program declarations. The following identifiers are keywords:
break, case, catch, const,
continue, default, do, else, exit,
false, finally, for, if, include,
loop, null, return, struct, switch,
true, try, var, while, with.
Expression Parsing
Numbers
Strings
Lists
Maps
struct
funct
Numbers
Strings
Lists
A list is instantiated by putting zero or more expressions inside brackets.
Usage
[[expression[, expression]...]]
expression: An expression.
Maps
A map is instantiated by putting zero or more pair expressions inside braces.
Usage
{[pair_expression[,
pair_expression]...]}
pair_expression: expression => expression
expression: An expression.
struct (expression)
Instantiates a structure.
Usage
struct { [statement...] }
statement: A statement to execute.
Remarks
Any symbols defined directly in the structure body are added to the structure.
See also
struct (statement).
funct (expression)
Instantiates a function.
Usage
funct (parameter_list) { [statement...] }
funct (parameter_list) return_value
parameter_list: [parameter[, parameter]...[, default_parameter]...]
parameter_list: default_parameter[, default_parameter]...
parameter: standard_parameter
parameter: reference_parameter
standard_parameter: parameter_name
reference_parameter: ¶meter_name
default_parameter: parameter_name = default_argument
default_argument: Default argument if the argument is omitted, an expression.
parameter_name: Unique parameter identifier.
statement: Any statement.
return_value: Function return value, an expression.
Remarks
To instantiate and return a map with the lambda form, the map definition must be
wrapped in a subexpression:
funct(b, c) ({b => c})
See also
funct (statement).
Operator Reference
Precedence and Associativity
()
[]
{}
.
++
--
$
*
/
+
-
?:
=
$= *= /=
+= -= <<= >>= &=
^= |= &&= ||= ??=
,
Precedence and Associativity
From highest to lowest precedence.
| Operator |
Type |
Associativity |
| () [] {} . |
Binary | Left |
| ++ -- |
Unary postfix |
Left |
| ! ~ + - ++ -- |
Unary prefix |
Right |
| $ |
Binary |
Left |
| * / % |
Binary |
Left |
| + - |
Binary |
Left |
| << >> |
Binary |
Left |
| & |
Binary |
Left |
| ^ |
Binary |
Left |
| | |
Binary |
Left |
| <=> |
Binary |
Left |
| < <= > >= |
Binary |
Left |
| == != |
Binary |
Left |
| && |
Binary |
Left |
| || |
Binary |
Left |
| ?? |
Binary |
Left |
| ?: |
Ternary |
Right |
| = += -= *= /= |
Binary |
Right |
| , |
Binary |
Left |
()
Function call operator.
Usage
lhs(rhs)
lhs: An expression evaluating to a function.
rhs: [argument[, argument]...].
argument: Any expression or the token & followed by an
l-value expression to pass a reference.
Operation
lhs is evaluated, then each argument of rhs is evaluated
in order. lhs is called with the supplied arguments and the function's
return value is returned.
[]
List access operator.
Usage
lhs[rhs]
lhs: An expression evaluating to a list.
rhs: An expression evaluating to a number.
Operation
lhs is evaluated, then rhs. The element in the list lhs
with the index rhs is returned.
If lhs is an l-value, the result is also an l-value.
See also
Lists.
{}
Map access operator.
Usage
lhs{rhs}
lhs: An expression evaluating to a map.
rhs: An expression.
Operation
lhs is evaluated, then rhs. The value in the map lhs with
the key rhs is returned.
If lhs is an l-value, the result is also an l-value.
See also
Maps.
.
Dot operator.
Usage
lhs . rhs
lhs: An expression evaluating to a structure.
rhs: An identifier.
Operation
lhs is evaluated. The value of the symbol rhs in the symbol table
of the structure lhs is returned.
If the symbol is mutable, the result is an l-value.
++
Increment operator.
Usage
operand++
++operand
operand: An l-value expression evaluating to a number.
Operation
Postfix: operand is evaluated and its value is incremented by 1. The
result is the previous value.
Prefix: operand is evaluated and its value is incremented by 1. The
result is the new value.
--
Decrement operator.
Usage
operand--
--operand
operand: An l-value expression evaluating to a number.
Operation
Postfix: operand is evaluated and its value is decremented by 1. The
result is the previous value.
Prefix: operand is evaluated and its value is decremented by 1. The
result is the new value.
$
Concatenation operator.
Usage
lhs $ rhs
lhs: An expression evaluating to a string.
rhs: An expression evaluating to a string.
Operation
lhs is evaluated, then rhs. A string consisting of lhs with
rhs appended is returned.
*
Multiplication.
Usage
lhs * rhs
lhs: An expression evaluating to a number.
rhs: An expression evaluating to a number.
Operation
lhs is evaluated, then rhs. The product of
lhs × rhs is returned.
/
Division operator.
Usage
lhs / rhs
lhs: An expression evaluating to a number.
rhs: An expression evaluating to a number.
Operation
lhs is evaluated, then rhs. The quotient of
lhs ÷ rhs is returned.
%
Modulo operator.
Usage
lhs % rhs
lhs: An expression evaluating to a number.
rhs: An expression evaluating to a number.
Operation
lhs is evaluated, then rhs. The remainder of
lhs ÷ rhs is returned.
+
Addition operator.
Usage
+ rhs
lhs + rhs
lhs: An expression evaluating to a number.
rhs: An expression evaluating to a number.
Operation
Prefix: rhs is evaluated and the sum of 0 + rhs is returned.
Infix: lhs is evaluated, then rhs. The sum of
lhs + rhs is returned.
-
Subtraction operator.
Usage
- rhs
lhs - rhs
lhs: An expression evaluating to a number.
rhs: An expression evaluating to a number.
Operation
Prefix: rhs is evaluated and the difference of 0 − rhs
is returned.
Infix: lhs is evaluated, then rhs. The difference of
lhs − rhs, is returned.
??
Null coalescing operator.
Usage
lhs ?? rhs
lhs: An expression.
rhs: An expression.
Operation
If lhs evaluates to null, rhs is evaluated and returned.
Otherwise, lhs is returned.
?:
Ternary conditional operator.
Usage
condition ? then : else
condition: An expression evaluating to a boolean.
then: An expression.
else: An expression.
Operation
If condition evaluates to true, then is evaluated and
returned. Otherwise, else is evaluated and returned.
=
Assignment operator.
Usage
lhs = rhs
lhs: An l-value expression.
rhs: An expression.
Operation
lhs is evaluated, then rhs. The value of rhs is assigned
to lhs. The result is rhs.
$= *= /=
+= -= <<= >>= &=
^= |= &&= ||= ??=
Compound assignment operators.
Usage
lhs $= rhs
lhs *= rhs
lhs /= rhs
lhs += rhs
lhs -= rhs
lhs <<= rhs
lhs >>= rhs
lhs &= rhs
lhs ^= rhs
lhs |= rhs
lhs &&= rhs
lhs ||= rhs
lhs ??= rhs
lhs: An l-value expression.
rhs: An expression.
Operation
The compound assignment operation (lhs op=
rhs) is equivalent to (lhs =
lhs op (rhs)). For example,
(a *= b + 5) is equivalent to (a = a * (b + 5)).
,
Comma operator.
Usage
lhs, rhs
lhs: An expression.
rhs: An expression.
Operation
lhs is evaluated first, then rhs. The result is rhs.
Remarks
To prevent synctactic ambiguities, the comma operator can only be used in
subexpressions. For example, to use the comma operator in a lambda definition,
wrap the expression in a subexpression:
funct a(f, g) { ... }
a(funct(b) (c(b), d() + b), e);
Statement Reference
break
const
continue
do
exit
for
funct
if
include
loop
pragma
return
struct
switch
throw
try
var
while
with
break
Exits the innermost loop.
Usage
break;
See also
continue,
do,
exit,
for,
loop,
return,
throw,
try.
while.
const
Defines one or more constants in the current scope.
Usage
const name = expression[,
name = expression]...;
name: Name of constant to define, an identifier. A symbol with the name
must not already exist in the current scope.
expression: An expression to evaluate and assign to the constant.
See also
funct,
struct,
var.
continue
Ends the current iteration of the innermost loop.
Usage
continue;
See also
break,
do,
for,
loop,
while.
do
Post-conditional loop.
Usage
do statement while
(condition);
statement: A statement to execute while condition evaluates to
true.
condition: An expression evaluating to a boolean.
See also
break,
continue,
for,
loop,
while.
exit
Emits an exit signal to cause the thread to gracefully terminate.
Usage
exit;
See also
break,
return,
throw,
try.
for
Iterates over a number range or the elements of a collection.
Usage
for [iterator] (first,
last[, step]) body
for [iterator[, iterator]]
(collection) body
iterator: Identifier of number or element being iterated. If two
iterators are specified, collection must evaluate to a map. The first
iterator is then the element key and the second iterator the element value. If
collection evaluates to a map and only one iterator is specified, the
iterator is the element key.
first: An expression, the first number in the range.
last: An expression, the last number in the range.
step: An expression, the amount to increase the iterator after each
iteration. Defaults to 1.
collection: An expression that evaluates to a list or map.
body: A statement to execute each iteration in a new scope.
See also
break,
continue,
do,
loop,
while.
funct (statement)
Defines a constant and binds it to a function.
Usage
funct name(parameter_list) { [statement...] }
funct name(parameter_list) return_value;
name: Name of constant, a nonexistent identifier in the current scope.
parameter_list: [parameter[, parameter]...[, default_parameter]...]
parameter_list: default_parameter[, default_parameter]...
parameter: standard_parameter
parameter: reference_parameter
standard_parameter: parameter_name
reference_parameter: ¶meter_name
default_parameter: parameter_name = default_argument
default_argument: Default argument if the argument is omitted, an expression.
parameter_name: Unique parameter identifier.
statement: Any statement.
return_value: Function return value, an expression.
Remarks
To instantiate and return a map with the lambda form, the map definition must be
wrapped in a subexpression:
funct a(b, c) ({b => c});
Reference implementation
The funct statement creates a named function. The name is
displayed in backtraces.
See also
const,
funct (expression).
if
Conditionally executes a statement.
Usage
if (condition) then
[else else]
condition: An expression that evaluates to a boolean.
then: A statement.
else: A statement
condition is evaluated. If true, then is executed.
Otherwise, else is executed.
Example
if (a != b)
{
print("a does not equal b");
break;
}
else
print("a equals b");
See also
switch.
include
Compiles a source file and executes it in the current scope.
Usage
include pathname;
pathname: Pathname of file to include, a string.
Example
struct incfile {
include "incfile";
}
incfile.a();
incfile:
funct a() { print("incfile"); }
loop
Unconditional loop.
Usage
loop statement
statement: A statement to repeatedly execute.
Example
loop
{
a();
if (b())
break;
c();
}
See also
break,
continue,
do,
for,
while.
pragma
Implementation-defined statement.
Usage
pragma type [token...];
type: An identifier.
token: Any token other than semicolon.
An implementation shall ignore any pragma types it does not recognize.
Reference implementation
The following pragmas are recognized:
assert (condition[,
message]) - Asserts that condition, a boolean
expression, evaluates to true. message, a string expression,
is a message to include in the diagnostic if the assertion fails. The pragma has
no effect unless debugging is enabled.
pause
[(condition)] - Pauses the thread
unconditionally or conditionally if the boolean expression condition
evaluates to true. The pragma has no effect unless debugging is
enabled.
return
Returns from the current function.
Usage
return [value];
value: A value to return to the caller. Defaults to null.
See also
break,
exit,
throw,
try.
struct (statement)
Defines a constant and binds it to a structure.
Usage
struct name { [statement...] }
name: Constant name, a nonexistent identifier in the scope.
statement: A statement to execute.
Remarks
Any symbols defined directly in the structure body are added to the structure.
Even though the defined symbol is immutable, the structure itself is not. The
mutability of a symbol in a structure is determined solely by its definition.
Example
struct foo
{
var x = "foo";
funct bar(s) x = s;
}
print(foo.x);
foo.bar("bar");
print(foo.x);
See also
const,
struct (expression).
with.
switch
Selectively executes a statement.
Usage
switch (control) case_branch...
[default_branch]
switch (control) default_branch
control: An expression.
case_branch: case (case[,
case]...) then.
case: An expression.
then: A statement.
default_branch: default else.
else: A statement.
control is evaluated, then each case is evaluated, in
order, until a value that equals control is found. If a match is found,
the then of the branch is executed. If a match is not found, else
is executed.
Reference implementation
If no cases are defined, a warning diagnostic is generated.
Example
switch (TRUE)
case (a > b) {
c();
return;
}
case (d && e, d || f)
g(f);
default
h();
See also
if,
throw
Emits a throw signal.
Usage
throw [payload];
payload: A value to associate with the signal. Defaults to null.
Remarks
The signal is propagated until it is absorbed by a try/catch
statement. If the signal is not absorbed, an error occurs.
See also
break,
exit,
return,
try.
try
Intercepts a signal.
Usage
try try_body catch
([symbol]) catch_body
[finally finally_body]
try try_body finally finally_body
try_body: A statement to execute.
symbol: Identifier of an immutable symbol to define in the scope of
catch_body that contains the throw payload.
catch_body: A statement to execute if a throw signal is emitted
by try_body.
finally_body: A statement to execute after try_body or
catch_body.
Remarks
If a throw signal is emitted by try_body, and catch_body
is defined, the signal is absorbed.
If a break, continue, exit, return or
throw signal is emitted from try_body or catch_body,
finally_body is executed before the signal is propagated.
finally_body must not emit a new signal, for example by executing a
return statement.
Example
try
may_throw();
catch (e)
{
log(e);
throw e;
}
finally
cleanup();
See also
break,
continue,
exit,
return,
throw,
var
Defines one or more variables in the current scope.
Usage
var name[ = expression][,
name[ = expression]]...;
name: Name of variable to define, an identifier. A symbol with the name
must not already exist in the current scope.
expression: An expression to evaluate and assign to the variable.
Defaults to null.
See also
const,
while
Pre-conditional loop.
Usage
while (condition) statement;
condition: An expression evaluating to a boolean.
statement: A statement to execute while condition evaluates to
true.
See also
break,
continue,
do,
for,
loop.
with
Executes a statement in the scope of a structure.
Usage
with (expression) statement
expression: An expression that evaluates to a structure.
statement: A statement to execute.
The semantics of the optional second use of with depends on the
implementation.
Remarks
Any symbols defined directly in the with body are added to the
structure.
Example
struct foo
{
var bar = 1;
}
with (foo)
{
++bar;
print(string(bar));
const baz = "baz";
}
print(foo.baz);
Reference implementation
If expression evaluates to a handle, the handle's callback function is
called with HANDLE_ACTION_WITH_BEGIN. If the callback function returns
false, a runtime error occurs. After statement has been executed,
the handle's callback function is called with HANDLE_ACTION_WITH_END.
See the library development documentation for more information.
See also
struct (statement),
Reference Implementation
Command Line
Debugger
Command Line
To see the usage and options for the interpreter, please see the ox manual:
$ man ox
Debugger
Using the Interactive Debugger
To debug a program, the interpreter must be started with either the command line
option -d or -p.
When a thread is paused it means it is currently being debugged.
A thread can be paused with either a pause statement or by
starting the interpreter with the -p command line option.
Only one thread can be paused at a time. If multiple threads are being debugged,
threads will take turn in pausing on a "first come first served" basis. All
threads are suspended while the debugger is active.
Modes
Continue: Normal execution. This is the default debugger mode for each
thread unless the -p option is specified.
Pause: Pause at the next statement or substatement. This corresponds to
the command "step into".
Step Over: Step over the next statement, then pause at the following
statement.
Step Out: Step out of the current substatement, then pause at the
following statement.
See also
Command Line,
pause.
Extending the Language
An implementation may extend the language as long as any program that is valid
according to this specification executes correctly. New keywords may not be
added since this could clash with a program identifier.