For LScript's C-like syntax, the following keywords and operators are used:
if else while pragma
var break switch case
library const continue return
True/true False/false Nil/nil =
+ - * /
== != <= >=
+= -= *= /=
++ -- || &&
! % & ^
<< >> |= ^=
&=
LScript also allows you to use AREXX-like keywords in constructing
your scripts:
begin end then do
to select when by
Examples of this alternate syntax can be found in the LScript
example script file2.ls.
LScript provides several different pragma options to the script
programmer. Each of these pragma directives is documented in
the following sections.
Version indicators can be either literal floating-point values, or literal
string values. Only major and minor values, separated by a period ('.'), will
be considered by the interpreter.
Consider the following example:
In the case of while, the condition upon which the control
loop is based is once again evaluated, and program execution
continues from there.
In the case of a for loop, the post-loop code is evaluated,
and the condition upon which the control loop is based is re-
evaluated. Program execution continues from that point.
Comments In LScript
You can embed comments into your LScripts using standard
C/C++ syntax. Single lines can be commented with the C++-
standard double-slash:
jitter(1.5); // make it uniformly ugly
Alternatively, multiple lines can be commented using the C-
standard '/*' '*/' token pair:
/*
multiple
lines
of
comments
*/
Using Pragma Directives
A pragma directive is an instruction you give to a compiler or
interpreter to modify or affect its behavior. In LScript, each
pragma directive applies only to the LScript in which it
appears.Warnings
The warnings pragma directive instructs LScript to bypass the
display of run-time warning messages.
#pragma warnings
Warning messages could be generated when a loss of data or an
unusual programming circumstance is detected by the
interpreter during script execution. For instance, assigning
the return values of a user-defined procedure to an array
whose size is less than the count of return arguments is
always guaranteed to generate a warning message during an
LScript execution.Automatic error handling
The normal practice for the LScript interpreter is to provide the
return value of all Modeler-specific commands and functions to
the script. This allows the script to determine if a command
failed, and take actions that it deems appropriate. However,
there may be situations where the script programmer does not
care to add the extra code required to manage these potential
error conditions. In this case, the script programmer can
specify the autoerror pragma. This pragma directive instructs
LScript to handle these error conditions automatically, issuing an
error message to the user, and terminating the script.
#pragma autoerror
Parallel process execution
LScript has the ability to launch external Microsoft(r) Windows(tm) or
console processes during a script's execution (see the example
LScript batch.ls for an illustration of this capability).
The default behavior of the LScript engine is to execute
these processes in a synchronous fashion--halting execution of
the LScript until the external process has completed.
However, there may be times when a script programmer wishes to
interact with an external process. In this case, the script
programmer needs to direct LScript to launch external processes
asynchronously, causing them to operate in parallel with the
LScript. The asyncspawn pragma directive is used in this
situation.
#pragma asyncspawn
Loading all library procedures
As mentioned previously, LScript will selectively compile user-defined
procedures that appear in a library file only if they
meet the criteria of satisfying an unresolved external
reference in the main script. However, due to LScript's single-pass
nature, LScript cannot detect dependencies of library
procedures upon other library procedures. Although it is
recommended that you avoid constructing your libraries with
internal dependencies, the loadall pragma directive gives you
the ability to alter LScript's default library-handling behavior.
This directive causes LScript to compile all procedures that
reside in a library into the execution space of your primary
script.
#pragma loadall
It needs to be noted that this pragma can appear in both the
primary LScript, as well as within each library file. If
this pragma is included in the primary LScript, then all
libraries that the script declares dependencies upon will have
all their user-defined procedures compiled into the script’s
execution space. However, if this pragma only appears at the
beginning of a library file, then only those procedures that
appear in that particular library will be loaded and compiled.
Other libraries will be handled in the default manner (i.e.,
only procedures satisfying unresolved references are compiled)
unless they too specify this pragma. This allows library
developers to indicate an internal dependency of which the
main script need not be aware.
Versioning Scripts
The LScript engine provides for allowing scripts to specify
minimum versions of the LScript plug-in to be used to execute the script. This
is useful for scripts that have taken advantage of newer features that would
cause the script to fail to compile on earlier versions of the plug-in.
#pragma version 1.5 // need at least v1.5 of LScript
Enforcing Variable Declaration
LScript allows you to simply use variables and arrays whereever and whenever
you need them without having to specify them in a previous 'var' statement
(and in the case of arrays, without having had to first explicitly declare an
upper limit). However, there may be times when you wish to add more structure
to your script development by requiring all variables and arrays to be
explicitly declared. The strict pragma switches the LScript engine
into a behavior where variables and arrays are required to be explicitly
declared or sized in a var statement.
#pragma strict
Control Structures
Control structures in LScript provide the script programmer the
facilities to design structured programs. These control
structures are documented in the following sections.if/else
The if operator allows the LScript to make decisions about
what should be executed. This operator supports two different
styles of syntax. The first is identical to C:
if((f.eof() == false) && (size(tokens) > 1) &&
(tokens[1] == "Plugin"))
info(tokens[1]," ",tokens[2]);
The second style is similar to AREXX:
if f.eof() == false and
size(tokens) > 1 and
tokens[1] == "Plugin" then
info(tokens[1]," ",tokens[2]));
If operators that host more than one executable statement must
house those statements inside a block control, identical in
structure to that used by user-defined procedures.
if(file == nil)
{
error("Cannot create scene file ",sceneFile);
return;
}
or, alternately,
if file == nil then
begin
error("Cannot create scene file ",sceneFile);
return;
end
If operators can facilitate "either/or" situations through the
use of an else clause. The else clause follows the executable
statement (or block control) for the if operator:
if(lensFlare[index])
{
file.writeln("LensFlare 1");
...
}
else
file.writeln("LensFlare 0");
while
The while operator provides looping capabilities within LScript.
It will execute a statement, or sequence of statements if they
are inclosed within a block control, as long as a condition
evaluates to a boolean true value. This control takes the
form:
while ( /boolean expression/ )
/statement/
As with if, LScript also allows the script programmer to use an
alternate syntax for while that is similar to AREXX:
while /boolean expression/ do
begin
...
end
There are two LScript commands that allow the script programmer to
effect the flow of looping controls: break and continue. Each
of these commands are discussed in sections found later in
this document.for
Similar to while, the for operator also provides looping
control within LScript. However, while provides limited control
of the looping construct, performing only the evaluation of
the conditional expression. The for operator differs in that
it provides for pre-loop initialization and post-loop code
execution in addition to the evaluation of the looping
condition. The C version of for takes the form:
for ( /init/ ; /expr/ ; /post/ )
/statement/
LScript also allows this simpler, AREXX-like form of the for
operator:
for /var/ = /expr/ to /expr/ [ by /expr/ ] do
begin
...
end
In either format, any variables used by the for loop operator
must have been previously declared before being used.
switch/select
LScript offers the script programmer a structured selection
mechanism, identical to its C counterpart. The switch
operator provides for the selection of code execution based
upon the numeric value of an expression:
switch ( /expression/ )
{
case /number/ :
...
break;
default:
...
break;
}
Again, LScript offers an AREXX-like version of this selection
mechanism through the use of the select operator. However,
unlike the switch operator, whose selection criteria is
limited solely to numeric values, the select control allows
whole expressions to be evaluated in determining the code to
execute:
select
when /expression/ then do
...
end
end
inline select
The inline select operator (?:) allows you to select one of
two alternatives based on a condition. This "shorthand" form
of the select operator is identical to that provided by C, and
takes the form:
/boolean expression/ ? /true select/ : /false select/
This operator can be use just about anyplace an expression can
appear in LScript. For example,
main
{
var j = 1;
...
info("result is ",(j == 1 ? "yes" : "no"));
}
break
The break command, mentioned earlier, will prematurely
terminate a looping control in LScript. Should either a while or
for operator encounter a break within their domain, they will
immediately cycle to the next LScript statement following the end
of their loop control.
while(j)
{
if(j == 10)
break; // line 4
++j;
}
info("j == ", j); // line 9
In the preceding example, upon encountering the break in line
#4, LScript would cause the while loop to terminate, and would set
its internal program counter to the executable statement on
line #9 (info()).continue
continue is another command within LScript that can effect the
flow of execution of a looping operator. Whereas break caused
program execution to continue with the next LScript code line
following the loop, the continue command will cause a loop
operator to behave as though it had reached the end of its
control loop. The continue command is very similar to a goto
command with the target being the top of the control loop.
while(1)
continue; // uh oh! an infinite loop!
Next Section
Previous Section
Table of Contents
Index
Errata
© 1996 Virtual Visions, Inc.
© 1997 NewTek, Inc.