Basic Terminology

Expressions

WebL programs consist of sequences of expressions separated by semicolons. Running a WebL program involves evaluating the expressions in sequence. Each expression either evaluates to a value (or result) or causes an exception that causes the program evaluation to terminate at that point. (We say that the expression throws an exception. More details about exceptions can be found in the sections See Try Statement and See Exceptions.)

The value of one expression is typically used by other expressions in the program. We also define the value of a sequence of expressions to be the value that the last expression in the sequence evaluated to. If no special steps are taken by the programmer, the results of the remainder of the single expressions in an expression sequence are lost.

Running or executing a WebL program involves several integrated steps:1

The program source text is parsed and checked for syntax errors. If syntax errors are detected, the execution of the program is terminated.

A representation of the program in the form of an abstract syntax tree (AST) is constructed in memory. The AST consists of a sequence of expressions.

The in-memory sequence of expressions are executed in turn. Side effects of the computation might be printing of results on the console, or communicating over the Internet. The value of the expression sequence (the last expression executed) is discarded.

Value Types

Each value has an associated value type or type. The type determines how the value can be further used by expressions. For example, it is only possible to multiply two values that have a numerical type. WebL is a dynamically typed programming language. This means that at the point where values are used by expressions, they are checked to be of the correct type as expected by the expression. If they are not, an exception is thrown.

The defined value types of WebL are: nil, boolean, int, real, char, string, fun, meth, set, list, object, page, piece, pieceset, tag. (See See Dynamic Types for more details.)

Constants

Constants are simple expressions that evaluate to themselves. They are the simplest WebL expressions. WebL allows nil, boolean, integer, real, character, and string constants. See Constant examples lists examples of constant expressions, what they evaluate to, and the resulting value type.

Constant examples

Expression

Value

Value Type

nil

nil

nil

true

true

bool

false

false

bool

21

21

int

1.41

1.41

real

'a'

'a'

char

"abc"

"abc"

string

`abc`2

"abc"

string

Operators

Operators combine expressions into more complicated expressions. Evaluating an expression involves evaluating the operands (constituent expressions), performing some computation on the resulting values, and returning a result. Examples include numerical, boolean and service combinator operators. The evaluation sequence of operands is typically left to right.

Operator examples

Expression

Value

Value Type

true or false

true

bool

2 + 2

4

int

1.2 * 2

2.4

real

"abc" + "def"

"abcdef"

string

Statements

WebL uses typical imperative program language constructs like while, if, and try statements. These statements are expressions in WebL, which means that they also evaluate to a value (often to the value nil).

Examples:

while x > 0 do x = x - 1 end

 

if x > y then y else x end

 

if x = 1 then y = x * 2

elsif x = 2 then y = x * 7

else y = 1

end

 

every s in "Hello World" do

PrintLn(s)

end

 

repeat x = x * 2 until x > y end

 

Variables and Scoping

A context is a set of variables and associated value bindings. Expression evaluation is performed in a context which specifies the values of the variables that appear in the expression. A context can be created in several ways. The most common way is by the programmer, who defines the variables and their values explicitly (in the current context) using variable declarations. After declaration, a variable can be assigned arbitrary values with the assignment expression "=".

Examples:

var x; // Defines the variable x.

var a, b, c; // Defines three variables a, b, c.

var name = "John"; // Defines a variable called name

// and assigns it a value.

var // Define and initialize several

x = 1, // variables.

y = 2,

z = 3 * x;

x = y * 2; // Assignment expression.

 

A variable's value is set to nil when no initializer is specified. A variable must be declared before it is used for the first time, and should be declared only once in any given context. Variable declarations are expressions that evaluate to the value the variable is set to. Assignment expressions evaluate to the value that is assigned.

It is important to note that

var x = x + 1

 

is equivalent to

var x = nil;

x = x + 1

 

Both of these programs lead to a runtime exception because 1 and nil are not type compatible under the plus operator. This definition of variable declaration allows the introduction of self-recursive functions.

WebL uses lexical scoping for variables. This allows contexts to be nested in each other according to the syntactic structure of the program. Nested contexts are automatically created at points where sequences of statements can be used, for example inside while, repeat, and if statements. A fresh context can be created explicitly with the begin statement. A variable can be used in a specific context at all positions syntactically following the place where it was declared.

Variable resolution is done by searching for a binding from inner (nested) contexts to outer contexts. This allows variables in inner contexts to override variables with the same name in outer contexts. For example, in the following program the variable sq is visible only inside the body of the while statement, and the variable i is visible only inside the context defined by the begin statement:

var sum = 0;

Print("The sum of the squares between 0 and 100 is ");

begin

var i = 0;

while i <= 100 do

var sq = i * i;

sum = sum + sq;

i = i + 1

end;

end;

PrintLn(sum)

Constructors

WebL also supports lists of values, sets of values, and objects with fields. Constructors perform the creation of these types of values from simpler values. See Constructor expressions shows that lists are constructed by square brackets, sets by curly braces, and objects by square brackets and a period token. More information about these value types is given in the section See Dynamic Types. Note that constructors consist of sub-expressions that are evaluated during value construction.

Constructor expressions

Expression

Value

Value Type

[ 1, 2, 2 + 1 ]

[1, 2, 3]

list

{ 1, 2, 1 + 1 }

{ 1, 2 }

set

[. a = 1+1, b=2 .]

[. a=2, b=2 .]

object


1. Note that WebL programs need not be compiled explicitly -- compilation is performed automatically just before the program is executed. WebL's execution model is similar to that of most scripting languages.

2. Back-quoted string constants differ from regular string constants in that escape sequences contained in the string are not expanded.


Up Previous Next