JGrok/QL syntax guide

under construction

The essentials

Statements and expressions. A QL program consists of a sequence of statements and/or expressions. Statements are executed for their side effects, typically changing the value of a variable, printing something to the screen, etc. Expressions typically produce no side effects; instead, they are evaluated and the results of evaluation are printed to standard output (which is typically the screen, unless the output has been redirected).

It is acceptable, although not always necessary, to place semicolons at the end of every statement and expression. In most cases, QL can determine where one statement ends and a different one begins. In cases where there is ambiguity, semicolons are required. For example:

foo = {1,2,3} X {4} + {4} X {5,6};
bar = {"hello"} X {"world"};
This piece of code defines two relations and then appears to print the transitive closure of foo, followed by bar. This is not the case, however. Because the expressions on the last two lines are not separated, they will be treated as one expression, and this code will instead print the union of foo and bar. To print transitive closure of foo first and bar second, a semicolon must be placed at the end of the third line.

For clarity reasons, and to avoid potential confusion, it is recommended that you place semicolons at the end of every expression or statement.

Compound statements. A sequence of statements and expressions can be treated as a single compound statement when surrounded by curly braces.

Variables and types. There is no need to declare variables in QL. A variable is automatically created when it is first assigned to. All variables have a type, which is automatically determined based on the value assigned to a variable; the type of a variable may change throughout its life, based on the values assigned to it.

Parameters. QL scripts can take command-line parameters. The parameters can be accessed through the variables $0, $1, $2, and so on. $0 holds the name of the QL script being executed, $1 holds the first parameter, $2 holds the second parameter, and so on.

Flow control statements

If statement. The if statement in QL has the general form of:

if ( expression )

The else part of the statement is optional, and the statement can be abbreviated to:

if ( expression )

The expression in brackets must evaluate to a boolean (true or false). Unlike many other languages, QL does not automatically map integers and strings to booleans; providing a non-boolean expression in a boolean context generates an error. The conditional expression controls the execution of a single expression or statement. The statement controlled can be a simple or a compound statement.

For statement. The for loop in QL looks as follows:

for variable in set statement_or_expression

During execution, the loop variable will take on all the values in the set in turn. The body of the loop (a single expression or statement) will be executed once for every member in the set, as it appeared before the loop began executing. This means that adding or deleting items from the set in the body of the loop does not affect the number of times the loop is executed.