Statements

Statement Sequences

Statement sequences are separated by semicolons. The value of a statement sequence is the value of the last expression in the sequence. An optional trailing semicolon in a statement sequence is ignored by WebL.

If Statement

If statements specify the conditional execution of guarded commands. The boolean expression preceding an expression is called its guard. The guards are evaluated in sequence of occurrence until one evaluates to true, whereafter its associated expression is evaluated. If no guard is satisfied, the statement sequence following the symbol else is executed, if there is one. The value of an if statement is the value of the associated expression whose guard evaluated to true.

Syntax:

IfStat = if SS then SS [ ElseStat ] end

ElseStat = else SS | elsif SS then SS [ ElseStat ]

 

Example:

if ch >= 'a' and ch <= 'z' then ReadIdent()

elsif ch >= '0' and ch <= '9' then ReadNumber()

elsif ch == '\'' or ch == '"' then ReadString()

else ReadSpecial()

end

 

While Statement

While statements specify the repeated execution of an expression while a boolean expression (its guard) yields true. The guard is checked before every execution of the expression. The value of a while statement is nil.

Syntax:

WhileStat = while SS do SS end

 

Example:

while x > 0 do x = x div 2; k = k + 1 end

 

Repeat Statement

Repeat statements specify the repeated execution of an expression until a boolean expression (its guard) yields true. The guard is checked after every execution of the expression. The value of a repeat statement is nil.

Syntax:

RepeatStat = repeat SS until SS end

 

Example:

repeat x = x * 2 until x > k end

 

Try Statement

Execution of an expression may terminate in a failure or exception. We say that the expression has thrown an exception. Exceptions are implemented with objects in WebL. The throw function accepts any object to throw as an exception. The try expression is used to trap a failed expression, or more commonly said, to catch the exception object. In case no exception occurs, the try statement simply executes a statement sequence and returns its value. In the case of an exception occurring in the statement sequence, a sequence of guarded expressions is evaluated. The guards are evaluated in sequence until one evaluates to true, whereafter the associated expression is evaluated and returned as value. In case no guard evaluates to true, the exception is automatically re-thrown (and may be caught by an enclosing try statement. The exception object is automatically assigned to a programmer-specified exception variable. (WebL will automatically declare the exception variable in a fresh context.)

By definition any WebL object can be thrown as an exception. By convention though, most exception objects consist of a string-valued field msg that describes the exception, and a string valued field type that is used for identifying the exception type. In some cases, the exception object contains fields that give more specific information on what went wrong, for example the file and line number where it occurred.

Syntax:

CatchStat = try SS catch Ident { on E do SS } end

 

Examples:

try

p = GetURL("http://www.yahoo.com")

catch E

on E.statuscode == 404 do

PrintLn("page not found")

on E.type == "HttpError" do

PrintLn("connection error")

on true do

nil // catch everything else

end

 

Throw ([. type="OutOfMemory", msg="No space left" .])

 

Every Statement

The every statement enumerates the elements of sets, lists, strings, objects and piece-sets. (Piece-sets will be introduced later.) Set elements are enumerated in an undefined sequence. List elements are enumerated from left to right. Enumerating a string gives the individual characters of the string from left to right. Enumerating an object gives the field names of the objects.

While enumerating, each element is assigned in turn to the loop variable and the body of the every statement executed. The value of the every statement is nil.

Syntax:

EveryStat = every Ident in E do SS end

 

Example:

every x in [1, 2, 3, 4] do

PrintLn("X has the value ", x)

end

Lock Statement

The lock statement is used to prevent race conditions in muli-threaded programs. The statement locks an object, executes a statement sequence, and unlocks the object. The lock on the object can only be held by a single thread at any specific time. In case a thread tries to aquire a lock on an object held by another thread, the thread is suspended until the lock is released.

Syntax:

LockStat = lock SS do SS end

 

Example:

var counter = [.

val = 0,

inc = meth(s, i)

lock s do

s.val = s.val + 1

end

end

.]

 

Begin Statement

The begin statement allows the programmer to introduce a new statement sequence in a sub-expression. This is sometimes useful to open a fresh context in which temporary variables can be declared.

Syntax:

BeginStat = begin SS end

 

Example:

x + begin var s = a * b; s * s end

 

Return Statement

The return statement returns the value of a function or method call. The return token is optionally followed by an expression that evaluates to the value returned (otherwise nil is returned by default). Note that the return statement can be used to return a value early. Contrast the WebL convention of the last expression of a function or method calculating the return value, which allows returning a value only at the end of the function or method. Also note that it is a runtime exception to execute a return statement outside of a function or method body.

Syntax:

ReturnStat = return [E]

 

Example:

var F = fun(s)

if s == nil then

return ""

end;

ToString(s)

end;


Up Previous Next