This chapter is a reference guide to the JavaScript statements. These statements contain the basic control flow operators for the javaScript language. All of the basic building blocks of JavaScript are contained in its statements.
All JavaScript statements are lexical structures, which are understood only by the javaScript parser. They are not understood by the system outside of the javaScript parser, and they are not available nor are they understood by Lisp. All JavaScript statements have meaning only at parse time.
All JavaScript special forms are structures, which are understood only by the JavaScript compiler. Unlike Function objects, the JavaScript special forms are invalid outside of compilation, at run time.
JavaScript supports user defined global symbols, allowing the user to assign any valid JavaScript object to the newly created global symbol. User defined global symbols behave just like their built-in cousins; however, JavaScript even allows the user to redefine any of the built-in JavaScript special form symbols.
The Analytic Information Server dialect of javaScript is case sensitive.
javaScript supports the basic binary and unary arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), protected division (#), and modulus (%).
Statement
operand1 binaryOperator operand2
Arguments | Explanation |
---|---|
Operand1 | The first operand. |
binaryOperator | The binary operator. Must be one of: +, -, *, /, #, %. |
Operand2 | The second operand. |
RETURN | The result of the binary operation. |
Use the binary operator statement to perform basic arithmetic operations on two numeric values, or to concatenate two string values.
These simple examples demonstrates the effect of the various binary operators.
a = 1 + 20;
//Returns
21
b = 30 - 5;
//Returns 25
c = 2 * 2.1;
//Returns 4.2
d = 100 / 10;
//Returns 10
e = 10 % 4;
//Returns 2 (the remainder)
The binary operator statement performs basic arithmetic operations on two numeric values, or concatenates two string values.
On a divsion by zero, the protected divide operator returns the highest possible number in the system if the sign of the numerator is negative or the lowest possible number in the system if the sign of the numerator is positive.
javaScript supports the basic unary arithmetic operations: increment
(++), decrement (--), and negation (-).
Syntax: unaryOperator operand1
Statement
unaryOperator operand1
operand1 incrementOperator
operand1 decrementOperator
Arguments | Explanation |
---|---|
unaryOperator | The unary operator. Must be one of: ++, --, -. |
Operand1 | The first operand. |
RETURN | The result of the unary operation. |
Use the unary operator statement to perform basic arithmetic operations on a single numeric value.
These simple examples demonstrates the effect of the various unary operators.
var a = 100;
var b = 100;
var c = 10;
var d = 10;
++a; //Returns 101
--b; //Returns 99
c++; //Returns 11
d--; //Returns 9
-a; //Returns -101
The unary operator statement performs basic arithmetic operations on a single numeric value.
javaScript assigns a value to a variable with the = (equal) operator. Another class of assignment operators perform math operations on the variable and saves the result in the same variable. They are +=, -=, *=, /=and %=. The += operator is shorthand for a=a + exp.
Statement
variable assignmentOperator expression
Arguments | Explanation |
---|---|
variable | The variable to be assigned a new value. |
assignmentOperator | The assignment Operator. Must be one of: =, +=, -=, *=, /=, %=. |
expression | If the assignmentOperator is (=), the expression may return any value. If the assignmentOperator is (+=, -=, *=, /=, or %=), then the expression must return a numeric value. |
RETURN | If the assignmentOperator is (=), the variable is set to the value of expression. If the assignmentOperator is (+=, -=, *=, /=, or %=), then the arithmetic operation is performed on the original variable and the expression. |
Use the assignment operator statement to assign a new value to a javaScript variable.
This simple example demonstrates the effect of the simple assignment operator.
a = 3.14; //Returns 3.14
This examples demonstrate the effects of the arithmetic assignment operators.
var a = 100;
var b = 100;
var c = 10;
var d = 10;
a += 10; //Returns 110
b -= 10; //Returns 90
c *= 10; //Returns 100
The assignment operator statement assigns a new value to a javaScript variable.
The child function declaration creates a new Lambda object and assigns it to the specified persistent variable name childName of the parent Lambda parent. The child function declaration always returns the newly created object identifier of the new Lambda. The child Lambda is invoked using the parent.childName() syntax form.
Statement
child parent childName ([type] arg..) { cvardec pvardec vardec exp }
Arguments | Explanation |
---|---|
child |
Mandatory keyword. |
parent |
Name of the parent class |
childName |
The name of the child Lambda (function) |
([type] arg...) |
A list of type hints (optional) and arguments separated by commas. If there are no arguments the parenthesis are still required. Note: The optional type hints can be one of the following: char, bool, int, float, or obj |
cvardec |
Optional declaration. If present, must be followed by a persistent class variable declaration. See cvar. |
pvardec |
Optional declaration. If present, must be followed by a persistent variable declaration. See pvar. |
vardec |
Optional declaration. If present, must be followed by a local variable declaration. See var. |
exp |
The javaScript statements that form the Lambda |
RETURN | An Lambda object. |
Use the child statement to create a new javaScript Lambda with a child relationship to a parent Lambda.
This simple example demonstrates the effect of the simple assignment operator.
function foo(x) { pvar y; y = x return (y); } child foo sum (x) { var temp ; temp = x + y; return ( temp ); }
We can invoke the parent and the child Lambda as follows:
foo(10); //Returns 10 foo.sum(3); //Returns 13 compareEQ(foo.Cv, foo.sum.Cv); //Returns true compareEQ(foo.Pv, foo.sum.Pv); //Returns true
The child statement creates a new javaScript Lambda with a child relationship to a parent Lambda. A Child Lambda inherits pvar and cvar structures from its parent Lambda.
The class statement declares a class and names all of its members. The class statement can also use to sub-class, i.e., create a class that inherits from another class and extend it.
Statement
class className {member1 ; member2 ... }
class className extends parentClass {member1 ; member2 ... }
Arguments | Explanation |
---|---|
class |
Mandatory Keyword |
className |
The name of this class |
extends |
Optional Keyword. If present, it must be followed by an existing parent class. |
{member1; member2; ...} |
The names of the class members, where each name is separated by a semicolon |
RETURN | An class name. |
Use the class statement to create a new class or to extend and existing class through in heritance.
This simple example demonstrates the effect of the simple assignment operator.
class employee {Name; Address; Salary } emp = new ('employee' , 'Name' ,"Tom Jones" , 'Address', "200 Main Street, Small Town, USA" ) display(emp.Name)
This simple example demonstrates the effect of the simple assignment operator.
class employee {Name; Address; Salary } class manager extends 'employee' {Perks } emp = new ('employee' , 'Name' ,"Tom Jones" , 'Address', "200 Main Street, Small Town, USA" ) mgr = new('manager', 'Name', "John Smith", 'Perks', "Special Parking Space")
The class statement creates a new class or to extend and existing class through in heritance.
The comparison operators are ==(equal) , !=(not equal) ,< (less
than) , <=(less than or equal) , > (greater than), >=(greater than
or equal) . The resulting value is a Boolean value of true if the
relational operator results in a true comparison otherwise the resulting
value is false. javaScript performs type checking and type
conversions, therefore the operands need not be the same type.
Syntax: (obj1 operator obj2)
obj1 |
First object to be compared |
operator |
Must be ==(equal), !=(not equal) ,< (less than) , <=(less than or equal) , > (greater than), >=(greater than or equal) . |
obj2 |
Second object to be compared |
Returns |
Returns true if the comparison is true, false if the comparison is false |
Example1
var a = true; //Returns True (a == true ); //Returns True ( 2 < 3) ; //Returns True ( 2 > 3) ; //Returns False
A conditional expression is a shorthand version of the if-else statement.
Besides having a shorter syntax, conditional expression always return a
value, unlike the if statement.
Syntax: condition ? val1 : val2
condition |
A Boolean expression |
? |
Mandatory operator |
val1 |
The value to be returned if condition is true. |
: |
Mandatory operator |
val2 |
The value to be returned if condition is false. |
Returns |
If the condition is true, val1 is returned, otherwise val2 is returned |
Example1
a = 1; X = (a >= 1) ? 1 : -1 ); // X will be set to 1
The cvar statement declares a persistent class variable within an
Lambda. This means that the persistent class variable will have a scope for
the life of the Lambda object. The class variable is created and initialized
when the Lambda is instantiated and re-initialized each time the Lambda is
invoked. Although javaScript is a dynamically typed language, the cvar
allows the programmer to optionally provide hints to the compiler by
specifying a type for variable declarations. The supported types are: char,
bool, int, float, and obj. Specifying a type of obj is equivalent to
specifying no type information.
Syntax: cvar varName
Syntax: cvar varName=init
Syntax: cvar type varName
Syntax: cvar type varName=init
cvar |
Mandatory keyword |
type | obj char bool int float obj text string symbol bitvec fltvec pcdvec stc dir dic matrix nummat vec bitvec numvec intvec objvec |
cvarName |
The name of the persistent class variable |
=init |
Optional argument. If present, the compiler will assign the cvarName to the value in init. |
When To Use
Unlike a temporary variable (var) that is declared inside a function and which is only "alive" while the function is currently active, a cvar (persistent variable) is accessible, as long the Lambda object instance is active. The contents of the entire cvar structure is in LambdaName.Cv, where LambdaName is the name of the instance of the Lambda. The contents of a single cvar variable is LambdaName.Cv.memberName;
A persistent class variable (cvar) is analogous to a static variable in a C or C++ programming environment. It is created and initialized when the Lambda is instantiated and it is persistent, meaning the memory is not released as long as the Lambda is in memory. However, the cvar differs from the pvar in that it is always re-initialized on entry to the Lambda that encapsulates it.
Example1
function squareIt(x) { cvar float xx; var y; xx = x * x; y = xx; return (xx); } var result, int; result = squareIt(10); writeln(result); //displays "100" writeln(squareIt.Cv.xx); //displays "100" writeln(squareIt.y); //error !Cannot find name: y in the PV structure of Lambda squareIt!
Example2
function foo (x) { cvar z; z = x; return(z); } var z; result = foo(7); display(result); //displays "7" display(z); //displays #void (accesses the contents of the global variable) display(foo.Cv.z); //displays "7"
Notes and Hints
The scope of variables in nested Lambdas depends upon the following compilation rules: Nested Lambdas do not share either their arguments or their temporary variables. The argument and the temporary variables are always limited in scope to the immediate Lambda object. A nested Lambda shares the persistent variables (pvar) and the persistent class variables (cvar) of its parents. When a variable reference is encountered, the javaScript compiler first searches the local Lambda's temporary variables (var), then the persistent variable structure (pvar), then the persistent class structure (cvar), and finally the global variables.
Also note: Any variable that is used without explicit declaration is automatically declared as a global variable. (This is true even if it is used inside a function definition).
The for statement repeats a set of javaScript statements until the
specified condition is false.
Syntax: for ( initexp; cond; increxp ) { exp ... }
for |
Mandatory keyword |
Initexp; |
Initializer expression. Usually resets an index to be used to determine loop control |
cond; |
An expression that returns a Boolean value. Usually tests the loop index to see if has reached a max number. |
Increxp |
Increment expression. Usually increments (or decrements) the loop index. |
{expr...} |
A set of javaScript statements |
Example1
var int a; for (a = 1; a < 10; ++a ) { display(" ", a)}
This will display the following line in the console screen:
1 2 3 4 5 6 7 8 9
The friend statement creates a new Lambda object and assigns it to
the specified persistent variable name friendName of the parent Lambda
parent. The friend function declaration always returns
the newly created Lambda object. The new friend Lambda is invoked by using the
parent.friendName () syntax.
Syntax: friend parent friendName ([type] arg..) { cvardec pvardec vardec exp
}
friend |
Mandatory keyword. |
parent |
Name of the parent class |
friendName |
The name of the friend Lambda (function) |
([type] arg...) |
A list of type hints (optional) and arguments separated by commas. If there are no arguments the parenthesis are still required. Note: The optional type hints can be one of the following: char, bool, int, float, or obj |
cvardec |
Optional declaration. If present, must be followed by a persistent class variable declaration. See cvar. |
pvardec |
Optional declaration. If present, must be followed by a persistent variable declaration. See pvar. |
vardec |
Optional declaration. If present, must be followed by a local variable declaration. See var. |
exp |
The javaScript statements that form the Lambda |
Returns |
Lambda object identifier |
Example1
function foo(x) {We can invoke the parent and friend Lambdas as follows:
foo(10); //Returns 10 foo.sum(3); //Returns 28 compareEQ(foo.Pv, foo.sum.Cv); //Returns true compareEQ(foo.Pv, foo.sum.Pv); //Returns false
Notes and Hints
A friend Lambda has a different pvar structure than its parent Lambda, but the cvar structure of a friend Lambda is the pvar structure of its parent Lambda.
A javaScript function compiles into a first class Lambda object. It has
the same footprint and is treated like any Lambda Object created by any
compiler in the LambdaClient development environment.
Syntax: function name ([type] arg..) { cvardec pvardec vardec exp }
Syntax: function ([type] arg..) { cvardec pvardec vardec exp }
function |
Mandatory keyword. |
name |
(Optional) The name of the Lambda (function) |
([type] arg...) |
A list of type hints (optional) and arguments separated by commas. If there are no arguments the parenthesis are still required. Note: The optional type hints can be one of the following: char, bool, int, float, or obj |
cvardec |
Optional Argument. If present, must be followed by a persistent variable declaration. See cvar. |
pvardec |
Optional Argument. If present, must be followed by a persistent variable declaration. See pvar. |
vardec |
Optional Argument. If present, must be followed by a local variable declaration. See var. |
exp |
The javaScript statements that form the Lambda |
Returns |
Lambda object identifier |
Note: A function with no name will generate an unnamed function.
When To Use
The function statement defines the name of an Lambda, its arguments, and the statements that comprise the Lambda.
Example1
function foo (x) { writeln( "The value of x is " , x); }
To invoke:
foo(29);
Example2
function sayHello ( ) { writeln( "Hello World"); }
To invoke:
sayHello();
javaScript functions may be called by name or by Object Identifier. The
function to be invoked must always have a set of parenthesis following the
function name. If the function requires arguments, the arguments are passed
as a comma separated list inside the parenthesis.
Syntax: name ()
Syntax: name (arg ...)
Syntax: Lambda.child ()
Syntax: Lambda.friend ( arg ...)
Syntax: class.method ()
Syntax: class.method ( arg ...)
name |
The name of the function to be invoked. |
class.method |
Alternate form of function name. Must be a method of a specified class |
(arg...) |
A list of arguments separated by commas. If there are no arguments the parenthesis are still required. |
Returns |
Lambda object identifier |
Example1
foo (x) { display( "The value of x is " , x); }
To invoke:
foo (1998);
Example2
function foo(x) { pvar y; y = x; return (y); } child foo sum (x) { var temp; temp = x + y; return ( temp ); }
We can invoke the parent and the child Lambda as follows:
foo(10); //Returns 10 foo.sum(3); //Returns 13
The if statement selects one of two expressions to evaluate based upon the value of a {test} expression. If the {cond} evaluates to True, then the expressions in the {thenClause} is evaluated.
If the else keyword is present and the {test} evaluates to false, then the expressions in the {elseClause} is evaluated.
The {test} and {thenClause} expressions are mandatory. The {else} expression is optional. The braces {} are also mandatory.
The {elseClause} may also be an if statement therefore
creating a limitless number of conditions and conditional paths.
Syntax: if (test) { trueClause... }
Syntax: if (test) { trueClause... } else { elseClause... }
Syntax: if (test) { trueClause... } else if (test2) { exp ... }
test |
An expression that returns a Boolean value. Must be enclosed in parenthesis. |
thenClause |
The expression to be executed if the cond clause returned True |
else |
Optional Keyword |
elseClause |
Optional expression. If the else keyword is present the elseClause must be present. The expression to be executed if the cond clause returned False |
else if |
If present, must be a valid if statement. |
Returns |
Returns the value of the expression evaluated. If no else clause is specified, the returned value is the result of the condition expression. |
When To Use
The if statement is used whenever it is necessary to perform actions based on a condition
Example1
j=1if ( j < 10 ) { k = 100;} else { k = -100;} //sets k to 100
Example2
j = 2; if ( j < 10 ) { k = 100;} else if ( j == 0) {k = 10;} else if ( j == 1 ) {k = 100;} else if ( j == 2 ) {k = 1000;} //sets k to 1000
The logical operators are && (and) , || (or) , and !
(not) . The resulting value from a logical operation is a Boolean value.
The && operator returns true if both
operands are true, the || operator returns true
if one or both operands are true,
and the ! operator returns true if the operand is false,
otherwise the ! operator returns false. javaScript performs
type checking and type conversions, therefore if the operands are not
Boolean, the operands are converted to Boolean.
Syntax: (obj1 && obj2 )
Syntax: (obj1 || obj2 )
obj1 |
First object to be compared |
operator |
Must be && (and) , || (or) |
obj2 |
Second object to be compared |
Returns |
The && operator returns true if both operands are true, otherwise it returns false. The || operator returns true if one or both operands are true otherwise it returns false. |
Syntax: (!obj1)
! |
Logical not operator |
obj1 |
Object to be negated |
Returns |
Returns true if the operand is false, otherwise the ! operator returns false. |
Example1
var a = true, k; if ( 2 < 3) && (a == true) {k = 1;} else {k = 2;} // sets k to 1 if ( 2 > 3) || (a == true) {k = 1;} else {k = 2;} // sets k to 1 if ( 2 > 3) && (!a) {k = 1;} else {k = 2;}// sets k to 2
The method statement defines a method to javaScript class.
Syntax: method className methodName([type] arg...) { expr }
method |
Mandatory keyword |
className |
Name of the parent class of the method |
methodName |
Name of this method. |
([type] arg...) |
A list of type hints (optional) and arguments separated by commas. If there are no arguments the parenthesis are still required. Note: The optional type hints can be one of the following: char, bool, int, float, or obj |
{expr} |
A set of javaScript statements |
Example1
class employee {Name; Address; Salary } method employee updateSalary( this, newSalary) { this.Salary = newSalary; } emp = new ('employee', 'Name', "Tom Jones", 'Address', "200 Main Street, Small Town, USA" ) emp.updateSalary(45000); writeln (emp.Name, "'s new salary is ", emp.Salary);Note: In a method declaration the receiving object (this) is declared, but is not passed in the function invocation.
emp.updateSalary(45000); // This is correct emp.updateSalary(emp,45000); // This is incorrect
The new function creates an instance of an object. The Lambda Information Server definition of an object is very specific. It is a memory resident container to store data, which is of variable length or is too large to fit in small fixed containers. The Analytic Information Server object Heap manager supports automated object resizing, garbage collection, and anti-fragmentation algorithms so that the user may concentrate on the analysis and modeling of data rather than on memory management.
The javaScript compiler supports all of the Analytic Information Server objects. Each object is covered in detail in its own chapter in the Lambda Information Server Programmer's Guide.
Syntax: new ('objectType' ) Syntax: new ('String' , value) Syntax: new ('Symbol' , value) Syntax: new ('Vector' , vecSubType, size) Syntax: new ('Vector', vecSubType, size, value) Syntax: new ('Structure', key, value) Syntax: new ('Structure', key, value ... . cdr ) Syntax: new ('Dictionary', key, value) Syntax: new ('Directory', key, value) Syntax: new ('ObjectRepository', filename, 'key', code, 'buffer', count)
new |
Mandatory keyword |
objectType |
Must be 'String', 'Symbol', 'Vector', 'Structure' , 'Dictionary', 'Directory', 'ObjectRepository' |
value |
Optional Argument. If present, must be a constant of type objectType. |
vecSubType |
Optional argument, but if present must only be used with objectType, Vector. Valid vecSubtypes are 'normal' 'bit' 'integer' 'float' 'number' 'small' 'object' 'pcode'. If omitted, the default is normal vector. |
size |
Optional argument, but if present must only be used with objectType, Vector. It must be an integer representing the number of items the Vector will store. |
key, value |
Optional arguments to Structure, Dictionary, and Directory, but must appear in pairs. There may be an arbitrary number of key, value pairs. |
. cdr |
Optional Argument to Structure only. If present, it must be a constant that will be assigned to the cdr (tail) of a structure. |
filename |
If objectType is ObjectRepository, filename is a mandatory field which names the database archive file to be associated with the ObjectRepository. If no such file exists, a new database archive file will be created. |
'clear' |
If objectType is ObjectRepository, 'clear' is an optional keyword. If present, the database archive file will be cleared immediately before any further processing. If no such file exists, a new database archive file will be created. |
'key', code |
If objectType is ObjectRepository, 'key' is an optional keyword. If present and it must followed by a number. See ObjectRepository in the Programmer's Guide for more detail. |
'buffer',count |
If objectType is ObjectRepository, 'buffer' is an optional keyword. If the key word 'buffer' is present, the numeric buffered object count must follow. See ObjectRepository in the Programmer's Guide for more detail. |
Returns |
The new function will return an object identifier for the type specified in objectType . |
Example1
javaScript Syntax | Lisp Syntax |
new ('String', "John") | (new String: "John") |
new ('Symbol' , "Donald") | (new Symbol: "Donald") |
new ('Vector', 3, 11, 12, 99) | (new Vector: 3 11 12 99) |
new ('Vector' , 'bit', 5, 1 ) | (new Vector: bit: 5 1) |
new ('Structure', 'X', 22, 'Y', 34, ' .', 3) | (new Structure: X: 22 Y: 34 . 3) |
new ('Dictionary' , 'Name', "John Doe", 'Age', 30) | (new Dictionary: Name: "John Doe" Age: 30) |
new ('Directory' , 1, "New Year's Day", 2, "Valentine's Day" ) | (new Directory: 1 "New Year's Day" 2 "Valentine's Day" ) |
new ( 'ObjectRepository', "myarchive.odb") | (new ObjectRepository: "myarchive.odb") |
The orphan statement creates a new Lambda object and assigns it to
the specified persistent variable name orphanName of the parent Lambda
parent. The orphan function declaration always returns
the newly created Lambda object. The new orphan Lambda is invoked by using the
parent.orphanName () syntax form.
Syntax: orphan parent orphanName ([type] arg..) { cvardec pvardec vardec exp
}
orphan |
Mandatory keyword. |
parent |
Name of the parent class |
orphanName |
The name of the orphan Lambda (function) |
([type] arg...) |
A list of type hints (optional) and arguments separated by commas. If there are no arguments the parenthesis are still required. Note: The optional type hints can be one of the following: char, bool, int, float, or obj |
cvardec |
Optional declaration. If present, must be followed by a persistent class variable declaration. See cvar. |
pvardec |
Optional declaration. If present, must be followed by a persistent variable declaration. See pvar. |
vardec |
Optional declaration. If present, must be followed by a local variable declaration. See var. |
exp |
The javaScript statements that form the Lambda |
Returns |
Lambda object identifier |
Example1
function foo(x) { pvar y = 5; pvar d =30; var temp ; temp = x + d + y; return ( temp ); } orphan foo sum (x) { pvar d = 20 ; cvar e = 100 ; var temp ; temp = x + d + y; return ( temp ); }
We can invoke the parent and orphan Lambdas as follows:
y = 10; //declare a global variable foo(10); //Returns 45 foo.sum(10); //Returns 40 (uses the value of global y) compareEQ(foo.Cv, foo.sum.Cv); //Returns false compareEQ(foo.Pv, foo.sum.Pv); //Returns false
Notes and Hints
An orphan Lambda is a member of its parent's pvar structure. Therefore, the parent knows about the orphan. However, the orphan Lambda has a different pvar structure than its parent Lambda, and a different cvar structure than its parent Lambda. Therefore, an orphan Lambda knows nothing of its parent Lambda.
The pvar statement declares a persistent variable within an Lambda. This means that the persistent class variable will have a scope for the life of the Lambda object. The persistent variable is created and initialized when the Lambda is instantiated and not re-initialized each time the Lambda is invoked. Although javaScript is a dynamically typed language, the pvar allows the programmer to optionally provide hints to the compiler by specifying a type for variable declarations. The supported types are: char, bool, int, float, and obj. Specifying a type of obj is equivalent to specifying no type information.
Note: If type hints are supplied, the programmer must assure that no other types are saved in the variable.Syntax: pvar varName Syntax: pvar varName = init Syntax: pvar type varName Syntax: pvar type varName = init
pvar |
Mandatory keyword |
type | obj char bool int float obj text string symbol bitvec fltvec pcdvec stc dir dic matrix nummat vec bitvec numvec intvec objvec |
pvarName |
The name of the persistent variable |
=init |
Optional argument. If present, the compiler will assign the pvarName to the value in init. |
When To Use
Unlike a var statement that is declared inside a function, the variables are only "alive" while the function is currently active. A pvar (persistent variable) is accessible, as long the Lambda object instance is active. The contents of the pvar structure is in LambdaName.Pv, where LambdaName is the name of the instance of the Lambda.
A persistent variable (pvar) is analogous to a static variable in a C or C++ programming environment. . It created and initialized when the Lambda is instantiated.
Unlike temporary variables (var), which are always allocated and initialized upon entry to a function and released upon exit, a pvar is initialized once and the memory allocated is fixed until the application ends.
Example1
function squareIt (x) { pvar xx; var y; xx = x * x; y = xx; return (xx); } var result; result = squareIt (10); writeln(result); //displays "100" writeln(squareIt.xx); //displays "100" writeln(squareIt.y); //error !Cannot find name: y in the PV structure of Lambda squareIt!
Notes and Hints
The scope of variables in nested Lambdas depends upon the following compilation rules: Nested Lambdas do not share either their arguments or their temporary variables. The argument and the temporary variables are always limited in scope to the immediate Lambda object. A nested Lambda shares the persistent variables (pvar) and the persistent class variables (cvar) of its parents. When a variable reference is encountered, the javaScript compiler first searches the local Lambda's temporary variables (var), then the persistent variable structure (pvar), then the persistent class structure (cvar), and finally the global variables.
Also note: Any variable that is used without explicit declaration is automatically declared as a global variable. (This is true even if it is used inside a function definition).
The reg statement declares a register variable inside a javaScript program. If a reg statement appears outside a function definition, the variable declaration generates an error message.
The reg statement declares a register variable within an Lambda. This means that the register variable will have a scope for this invocation of the Lambda object. The register variable is created and initialized each time the Lambda is invoked. Although javaScript is a dynamically typed language, the reg allows the programmer to optionally provide hints to the compiler by specifying a type for register declarations. The supported types are: int and float. Specifying a type of int is equivalent to specifying no type information.
Note: If type hints are supplied, the programmer must assure that no other types are saved in the variable.reg |
Mandatory keyword |
type | int float |
varName |
The name of the register variable |
=init |
Optional argument. If present, the compiler will assign the varName to the value in init. |
When To Use
It is necessary to explicitly declare register variables inside a function using the reg statement. The register variable structure, LambdaName.Rv, (where LambdaName is the name of the instance of the Lambda) is accessible.
Register variables are always allocated and initialized upon entry to a function and released upon exit.
Example1
function zoo (x) { reg int m; m = x; writeln(m); return (m); } var m = 99; result = zoo(7); //displays "7" (accesses the contents of the local variable) writeln(m); //displays "99" (accesses the contents of the global variable) writeln(zoo.m); //error--!Cannot find name: m in the PV structure of Lambda zoo!
An arbitrary number of javaScript statements may be grouped logically
by embedding the statements in braces { exp ...}. A
statement block such as this may be placed in any position where a
single statement is required. Multiple statements in javaScript may be
written on a single line as long as a semicolon terminates each
statement.
Syntax: {exp ...}
exp ... |
Any number of expressions to be evaluated |
When To Use
A statement block may be placed in any position where a single statement is required.
Example1
j = 1; if ( j < 10 ) { k = 100; writeln(k);} else { k = -100; writeln(k);}
The var statement declares a temporary variable inside a javaScript program. The location of the var statement determines the scope the variable. If a var statement appears inside a function definition, the variable is considered local to that function only. If a var statement appears outside a function definition, the variable has global scope.
The var statement declares a temporary variable within an Lambda. This means that the temporary variable will have a scope for this invocation of the Lambda object. The temporary variable is created and initialized each time the Lambda is invoked. Although javaScript is a dynamically typed language, the var allows the programmer to optionally provide hints to the compiler by specifying a type for variable declarations. The supported types are: char, bool, int, float, and obj. Specifying a type of obj is equivalent to specifying no type information.
Note: If type hints are supplied, the programmer must assure that no other types are saved in the variable.var |
Mandatory keyword |
type | obj char bool int float obj text string symbol bitvec fltvec pcdvec stc dir dic matrix nummat vec bitvec numvec intvec objvec |
varName |
The name of the temporary variable |
=init |
Optional argument. If present, the compiler will assign the varName to the value in init. |
When To Use
Since global variables are automatically declared in the LambdaClient environment, it is unnecessary declare global variables. However, it is necessary to explicitly declare temporary variables inside a function using the var statement. The temporary variable structure, LambdaName.Tv, (where LambdaName is the name of the instance of the Lambda) is accessible, as long the Lambda object instance is active.
Temporary variables are always allocated and initialized upon entry to a function and released upon exit.
Example1
function zoo (x) { var m; m = x; writeln(m); return (m); } var m = 99; result = zoo(7); //displays "7" (accesses the contents of the local variable) writeln(m); //displays "99" (accesses the contents of the global variable) writeln(zoo.m); //error--!Cannot find name: m in the PV structure of Lambda zoo!
Notes and Hints
The scope of variables in nested Lambdas depends upon the following compilation rules: Nested Lambdas do not share either their arguments or their temporary variables. The argument and the temporary variables are always limited in scope to the immediate Lambda object. A nested Lambda shares the persistent variables (pvar) and the persistent class variables (cvar) of its parents. When a variable reference is encountered, the javaScript compiler first searches the local Lambda's temporary variables (var), then the persistent variable structure (pvar), then the persistent class structure (cvar), and finally the global variables.
Also note: Any variable that is used without explicit declaration is automatically declared as a global variable. (This is true even if it is used inside a function definition).
The while statement repeats a set of javaScript statements
until its condition is false.
Syntax: while (test) { expr }
while |
Mandatory keyword |
(test) |
An expression that returns a Boolean value |
{expr} |
A set of javaScript statements |
Example1
var int a = 1; while (a < 10) { display (" ", a) ++a }
This will display the following line in the console screen:
1 2 3 4 5 6 7 8 9
The return statement exits the current function passes a
single value to the caller.
Syntax: return ( value )
value |
Any expression that returns a value. May be embedded in parenthesis |
Returns |
Immediately evaluates the expression, returns the value, and resume execution of the caller function |
When To Use
The return statement is used to leave the current function and return to the caller with a return value.
Example1
function foo (x) { var m; m = x * 2; return (m); // the value of m is returned to the caller } result = foo(7); //result will receive the value of 14