13
Language Constructs
There are three types of SKILL language constructs:
-
Conditional statements
Conditional statements test for a condition and perform operations when that condition is found. These statements areif,unless, andwhen. -
Selection statements
A selection statement allows a list of elements, each with a corresponding operation. A variable can then be compared to the list of elements. If the variable matches one of the elements, the corresponding operation is performed. These statements includefor,foreach, andwhile. -
Iterative statements
Iterative statements repeat an operation as long as a certain condition is met. These statements includecaseandcond.
This chapter contains information on the following statements
if
if( g_condition g_thenExpression [ g_elseExpression ] ) => g_result if( g_condition then g_thenExpr1 ... [ else g_elseExpr1 ... ] ) => g_result
Evaluates g_condition, typically a relational expression, and runs g_thenExpression if the condition is true (that is, its value is non-nil); otherwise, runs g_elseExpression.
The value returned by if is the value of the corresponding expression evaluated.
Additionally, if also can also be used with the keywords then and else to group sequences of expressions for conditional execution. If the g_condition is true, the sequence of expressions between then and else (or the end of the if form) is evaluated, with the value of the last expression evaluated returned as the value of the form.
Arguments
Value Returned
|
Returns the value of g_thenExpression if g_condition has a non-nil value. The value of g_elseExpression is returned otherwise. |
Examples
Returns 0 because x is less than 5.
x = 2
if(( x > 5) 1 0)
=> 0
Prints the string npn and returns the result of print.
a ="npn"
if(( a == "npn" ) print( a ) ) "npn"
=>nil
Returns "non-nil" because x was not nil. If x was nil, "nil" would be returned.
x = 5
if( x "non-nil" "nil" )
=> "non-nil"
Returns 1 because x is greater than 5.
x = 7
if(( x > 5) then 1 else 0 )
=> 1
if( (x > 5) then println("x is greater than 5") x + 1 else print("x is less ") x - 1)
x is greater than 5 ; Printed if x was 7.
=> 8 ; Returned 8 if x was 7.
unless
unless(g_condition g_expr1… ) =>g_result/ nil
Description
Evaluates a condition. If the result is true (non-nil), it returns nil; otherwise it evaluates the body expressions in sequence and returns the value of the last expression.
The semantics of this function can be read literally as “unless the condition is true, evaluate the body expressions in sequence.”
Arguments
Value Returned
|
Returns the value of the last expression of the sequence g_expr1 … if g_condition evaluates to |
|
Examples
Prints "x is negative" as a side effect.
x = -123
unless( x >= 0 println( "x is negative" ) -x )
=> 123
unless( x < 0 println( "x is positive ") x)
=>nil
when
when(g_condition g_expr1… ) =>g_result/ nil
Description
If the result is non-nil, evaluates the sequence of expressions and returns the value of the last expression. Otherwise, returns nil.
Arguments
Value Returned
|
Returns the value of the last expression of the sequence g_expr1 … if g_condition evaluates to non-nil. |
|
Example
x = -123
when( x < 0 println( "x is negative" ) -x )
=> 123
Prints "x is negative" as a side effect.
when( x >= 0 println( "x is positive" ) x)
=>nil
for
for(s_loopVar x_initialValue x_finalValue g_expr1[g_expr2… ] ) => t
Description
Evaluates the sequence g_expr1 g_expr2 … for each loop variable value, beginning with x_initialValue and ending with x_finalValue.
First evaluates the initial and final values, which set the initial value and final limit for the local loop variable named s_loopVar. Both x_initialValue and x_finalValue must be integer expressions. During each iteration, the sequence of expressions g_expr1 g_expr2 … is evaluated and the loop variable is then incremented by one. If the loop variable is still less than or equal to the final limit, another iteration is performed. The loop ends when the loop variable reaches a value greater than the limit. The loop variable must not be changed inside the loop. It is local to the for loop and would not retain any meaningful value upon exit from the for loop.
Arguments
|
Name of the local loop variable that must not be changed inside the loop. |
|
|
Integer expression setting the initial value for the local loop variable. |
|
Value Returned
Example
sum = 0
for( i 1 10
sum = sum + i
printf( "%d" sum ))
=> t
Prints 10 numbers and returns t.
sum = 0
for( i 1 5
sum = sum + i
println( sum )
)
=> t
Prints the value of sum with a carriage return for each pass through the loop:
1
3
6
10
15
foreach
foreach(s_formalVar g_exprList g_expr1[g_expr2… ] ) =>l_valueList
foreach( (s_formalVar1…s_formalVarN)g_exprList1… g_exprListN g_expr1[g_expr2 …] ) =>l_valueList
foreach(s_formalVar g_exprTable g_expr1[g_expr2… ] ) =>o_valueTable
Description
Evaluates one or more expressions for each element of a list of values.
foreach( s_formalVar g_exprList g_expr1 [g_expr2 …] )
=>l_valueList
evaluates g_exprList, which returns a list l_valueList. It then assigns the first element from l_valueList to the formal variable s_formalVar and processes the expressions g_expr1 g_expr2 … in sequence. The function then assigns the second element from l_valueList and repeats the process until l_valueList is exhausted.
foreach( (s_formalVar1…s_formalVarN) g_exprList1… g_exprListN g_expr1 [g_expr2 …] )=> l_valueList
can iterate over multiple lists to perform vector operations. Instead of a single formal variable, the first argument is a list of formal variables followed by a corresponding number of expressions for value lists and the expressions to be evaluated.
foreach( s_formalVar g_exprTable g_expr1 [g_expr2 …])
=>o_valueTable
can be used to process the elements of an association table. In this case, s_formalVar is assigned each key of the association table one by one, and the body expressions are evaluated each iteration. The syntax for association table processing is provided in this syntax statement.
Arguments
|
Expression whose value is a list of elements to assign to the formal variable s_formalVar. |
|
|
g_expr2 … |
|
Value Returned
Example
foreach( x '( 1 2 3 4 ) println( x ) )
1
2
3
4
=> ( 1 2 3 4 )
Prints the numbers 1 through 4 and returns the second argument to foreach.
foreach( key myTable printf( "%L : %L" key myTable[key] ) )
Accesses an association table and prints each key and its associated data.
( foreach ( x y ) '( 1 2 3 ) '( 4 5 6 ) ( println x+y ) )
5
7
9
=> ( 1 2 3 )
Uses foreach with more than one loop variable.
Errors and Warnings
The error messages from foreach might at times appear cryptic because some foreach forms get expanded to call the mapping functions mapc, mapcar, mapcan, and so forth.
while
while(g_condition g_expr1… ) =>t
Description
Repeatedly evaluates g_condition and the sequence of expressions g_expr1 … if the condition is true.
This process is repeated until g_condition evaluates to false (nil). As this form always returns t, it is principally used for its side effects.
Arguments
Value Returned
Example
i = 0
while( (i <= 10) printf("%d" i++) )
=> t
Prints the digits 0 through 10.
case
case(g_selectionExpr l_clause1[l_clause2… ] ) =>g_result/ nil
Description
Evaluates the selection expression, matches the resulting selector values sequentially against comparators defined in clauses, and runs the expressions in the matching clause.
Each l_clause is a list of the form (g_comparator g_expr1 [g_expr2…] ), where a comparator is either an atom (that is, a scalar) of any data type or a list of atoms. Comparators are always treated as constants and are never evaluated. The g_selectionExpr expression is evaluated and the resulting selector value is matched sequentially against comparators defined in l_clause1 l_clause2…. A match occurs when either the selector is equal to the comparator or the selector is equal to one of the elements in the list given as the comparator. If a match is found, the expressions in that clause and that clause only (that is, the first match) are run. The value of case is then the value of the last expression evaluated (that is, the last expression in the clause selected). If there is no match, case returns nil.
The symbol t has special meaning as a comparator: it matches anything. It is typically used in the last clause to serve as a default case when no match is found with other clauses.
Arguments
Value Returned
|
Returns the value of the last expression evaluated in the matched clause. |
|
Example
cornersType = "min"
type = case( cornersType
("min" path("./min"))
("typ" path("./typ"))
("max" path("./max"))
(t println("you have not chosen an appropriate
corner")))
=> path is set to "./min"
cond
cond(l_clause1… ) =>g_result/ nil
Description
Examines conditional clauses from left to right until either a clause is satisfied or there are no more clauses remaining.
This command is useful when there is more than one test condition, but only the statements of one test are to be carried out. Each clause is of the form ( g_condition g_expr1… ). The cond function examines a clause by evaluating the condition associated with the clause. The clause is satisfied if g_condition evaluates to non-nil, in which case expressions in the rest of the clause are evaluated from left to right, and the value returned by the last expression in the clause is returned as the value of the cond form. If g_condition evaluates to nil, however, cond skips the rest of the clause and moves on to the next clause.
Arguments
|
Each clause must be of the form |
Value Returned
|
Returns the value of the last expression of the satisfied clause. |
|
Example
procedure( test(x)
cond(((null x) (println "Arg is null"))
((numberp x) (println "Arg is a number"))
((stringp x) (println "Arg is a string"))
(t (println "Arg is an unknown type")))
)
test(nil)
=>nil; Prints "Arg is null".
test( 5 )
=>nil; Prints "Arg is a number".
test( ’sym )
=>nil; Prints "Arg is an unknown type".
Tests each of the arguments according to the conditions specified with cond.
Return to top