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.)
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.
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 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.
|
`abc` 2 |
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.
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).
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 "=".
var x; // Defines the variable x.
var a, b, c; // Defines three variables a, b, c.
var name = "John"; // Defines a variable called name
var // Define and initialize several
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.
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:
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.