W Wrapl, The Programming Language

Expressions

The table below summarizes the different expression forms in Wrapl. The following notation is used in the table; idi denotes an idenitifer, expri denotes an expression and decli denotes a declaration.

Basic Expressions

NIL

Evaluates to the constant Std.Object.Nil, used to denote the absence of any other value.

BACK

Fails to produce any value, causing the current expression to backtrack.

. expr

Returns the value of expr. If expr is a reference, then . expr returns the current value of expr without any reference.

? expr

Returns the type of expr.

expr1 == expr2

Returns expr2 if expr1 and expr2 are the same object, fails otherwise.

expr1 ~== expr2

Returns expr2 if expr1 and expr2 are not the same object, fails otherwise.

Structure Expressions

[expr1, ..., exprk]

Returns a list (instance of Std.List.T) with elements expr1, expr2, ..., exprk

{exprk1 IS exprv1, ..., exprkk IS exprvk}

Returns a table (instance of Std.Table.T) with pairs (exprki, exprvi). Any value exprvj is omitted, the value NIL is used.

ALL expr

Returns a list of all the values produced by expr.

UNIQ expr

Returns a table with the (unique) values produced by expr as keys.

METH :method(signature) IS expr

where signature is of the form [@|= expr1], ..., [@|= exprk].

Adds expr (which should be an invokable object) to :method corresponding to signature, where @ type denotes a type match and = value denotes a value match, using Std.Symbol.Set. Returns expr.

The old keyword can still be used instead of .

Assignment Expressions

expr1 <- expr2

Assigns the value of expr2 to the reference produced by expr1. If expr1 does not evalute to a reference then Wrapl.Loader.IncorrectAssignMessageT message is sent. Within the evaluation of expr2 the special variable $ can be used to refer to the current value of expr1.

expr1 -> expr2

Assigns the value of expr1 to the reference produced by expr2. If expr1 does not evalute to a reference then Wrapl.Loader.IncorrectAssignMessageT message is sent.

Function Expressions

<id1[+][| expr], ..., idk[+|*]> expr

Creates a new function object with parameters id1, ..., idk and body expr. If a parameter is followed by + it is passed by reference, other it is passed by value. A * after the final parameter indicates that it is a variadic parameter, any extra arguments are collected into a Wrapl.Loader.VariadicT and passed in this parameter.

<> VAR expr

Creates a new Std.Function.VariableT object that returns the variable produced by expr. This is equivalent to a call to Std.Function.VariableNew with expr.

<> DEF expr

Creates a new Std.Function.ConstantT function object that returns the value produced by expr. This is equivalent to a call to Std.Function.ConstantNew with expr.

TO :method(signature) VAR | DEF ] expr

where signature is of the form [id1[+]] [@|= expr1], ..., [idk[+|*]] [@|= exprk].

Equivalent to METH :method(signature') IS func (see here) where signature' is the type/value information in signature (i.e. ignoring any identifiers and pass-by-reference markers) and func is the function: <id1[+], ..., idk[+]> [ VAR | DEF ] expr (creating unique id's for those omitted). Returns func.

The old keyword can still be used instead of .

Function Invocation Expressions

expr0(expr1, ..., exprk)

Calls the function expr0 with arguments expr1, ..., exprk. If the function fails, exprk is resumed and the function is called again. If exprk fails, exprk-1 is resumed, and so on.

expr0{expr1, ..., exprk}

Calls the function expr0 with arguments expr1, ..., exprk. If the function fails, each expr1, expr2, ..., exprk is resumed and the function is called again.

Function Control Expressions

RET expr

Returns expr from the current function.

FAIL

Exits the current function without producing any value (failing).

SUSP expr

Suspends the current function producing the value expr. If the function in resumed, evaluation continues with SUSP expr producing the value of expr.

Conditional Expressions

expr0 [=> expr1] [// expr2]

If expr0 succeeds, returns the values produced by expr1 otherwise returns the values produced by expr2. If the failure clause (// expr2) is omitted and expr0 fails, the entire expression fails. If the success clause (=> expr1) is omitted and expr0 succeeds, the entire expression fails.

Scope Expressions

({decl;|expr;})

Creates a new scope for the evaluation of each declaration decl and each expression expr. The block expression produces the value(s) of the last expression in the block. A block expression can contain a RECV expression for handling message.

WITH id1 <- expr1, ..., idk <- exprk DO expr

For each i = 1, ..., k: binds idi to the value of expri and produces the values produced by expr. If expri produces an assignable reference, then idi will be assignable.

expr1.expr2

Normally, an expression of the form id1.id2 denotes the variable/constant id2 imported from the module id1. However, if id1 is not a constant then a runtime call to :"." is made. Thus expr1.expr2 is equivalent to :"."(expr1, expr2) except when expr2 is an identifier, in which case expr1.id2 is equivalent to :"."(expr1, "id2").

Loop Expressions

REP expr

Repeatedly evaluates expr.

EVERY expr0 DO expr1

For each value produced by expr0 evaluates expr1.

STEP

Causes the current loop to start its next iteration.

EXIT expr

Exits the current loop with the value(s) of expr. Note that expr is evaluated in the context directly outside the loop, in particular this affects STEP, EXIT and SEND expressions.

WHILE expr and UNTIL expr

WHILE expr is equivalent to expr // EXIT NIL.

UNTIL expr is equivalent to expr => EXIT NIL.

Generator Control Expressions

For each value produced by expr1, produces all the values of expr2. If expr1 fails then the entire expression fails, thus expr1 & expr2 behaves like a shortcut logical and expression which returns it's second operand if it succeeds.

expr1 \ expr2

For each value V produced by expr1, produces V for each value produced by expr2. Within the evaluation of expr2 the special variable $ can be used to refer to the value produced by expr1, thus expr1 \ expr2 can be used to mean "such that".

expr1 | expr2

Produces all the values produced by expr1 followed by the values produced by expr2. Since expr1 | expr2 will produce a value if either expr1 or expr2 succeeds, it behaves like a shortcut logical or expression.

expr1 ! expr2

Causes expr1 and expr2 to produce values in parallel: for each value produced by expr1 the next value produced by expr2 is returned. The expression fails when either expr1 or expr2 fails.

expr1 # expr2 # ... # exprk

Generates the values produced by expr1, expr2, ..., exprk in an interleaved fashion; i.e. the first value produced by expr1 is produced, followed the first from expr2 and so on until the first value from exprk is produced. Then the second value from expr1 is produced, then the second from expr2 and so on. The expression fails when any of expr1, expr2, ..., exprk fail.

expr1 OF expr2

Produces the first expr1 values produced by expr2.

expr1 SKIP expr2

Produces all but the first expr1 values produced by expr2.

@ expr

Generate all the values of expr repeatedly. If expr fails to produce any values, then the entire expression fails.

SUM expr

Returns the sum of the values produced by expr. Fails if expr fails to produce any values.

PROD expr

Returns the product of the values produced by expr. Fails if expr fails to produce any values.

COUNT expr

Returns the number of values produced by expr. Returns 0 if expr fails to produce any values.

Iterator Expressions

DO expr

Returns an instance of Std.Function.IteratorT which produces the values produced by expr.

USE expr

Returns the next value produced by expr which should be an instance of Std.Function.IteratorT.

Message Handling Expressions

RECV id DO expr

Catches messages sent in the current block. The message sent is assigned to id and value(s) produced by expr is returned as the value of the block. Note that RECV expressions receive all messages, it is therefore necessary to resend unhandled messages using SEND.

SEND expr

Sends expr as a message. The first enclosing (at runtime) RECV expr will receive the message.

Precompiled Expressions

`expr`

Produces the value of expr as evaluated at compile time. If expr fails or sends msg at compile time, then `expr` is equivalent to BACK or SEND msg respectively. Since expr is evaluted at compile time, it may only use constants and global variables.