Product Documentation
OCEAN Reference
Product Version IC23.1, September 2023

13


Language Constructs

There are three types of SKILL language constructs:

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

g_condition

Any Virtuoso SKILL language expression

g_thenExpression

Any SKILL expression

g_elseExpression

Any SKILL expression

Value Returned

g_result

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

g_condition

Any SKILL expression

g_expr1

Any SKILL expression

Value Returned

g_result

Returns the value of the last expression of the sequence g_expr1 … if g_condition evaluates to nil.

nil

Returns nil if g_condition evaluates to non-nil.

Examples

Prints "x is negative" as a side effect.

x = -123
unless( x >= 0 println( "x is negative" ) -x ) 
=> 123

Returns nil.

unless( x < 0 println( "x is positive ") x) 
=> nil

when

when(
g_condition 
g_expr1 … 
) 
=> g_result / nil

Description

Evaluates a condition.

If the result is non-nil, evaluates the sequence of expressions and returns the value of the last expression. Otherwise, returns nil.

Arguments

g_condition

Any SKILL expression

g_expr1

Any SKILL expression

Value Returned

g_result

Returns the value of the last expression of the sequence g_expr1 … if g_condition evaluates to non-nil.

nil

Returns nil if the g_condition expression evaluates to 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

Returns 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.

Everything that can be done with a for loop can also be done with a while loop.

Arguments

s_loopVar

Name of the local loop variable that must not be changed inside the loop.

x_initialValue

Integer expression setting the initial value for the local loop variable.

x_finalValue

Integer expression giving final limit value for the loop.

g_expr1

Expression to evaluate inside loop.

g_expr2

Additional expressions to evaluate inside loop.

Value Returned

t

This construct always returns t.

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.

The first syntax form,

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.

The second syntax form,

foreach( (s_formalVar1…s_formalVarN) g_exprList1g_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.

The third syntax form,

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

s_formalVar

Name of the variable.

g_exprList

Expression whose value is a list of elements to assign to the formal variable s_formalVar.

g_expr1

g_expr2

Expressions to execute.

g_exprTable

Association table whose elements are to be processed.

Value Returned

l_valueList

Returns the value of the second argument, g_exprList.

o_valueTable

Returns the value of g_exprTable.

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.

Everything that can be done with a for loop can also be done with a while loop.

Arguments

g_condition

Any SKILL expression

g_expr1

Any SKILL expression

Value Returned

t

Always returns t.

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

g_selectionExpr

An expression whose value is evaluated and tested for equality against the comparators in each clause. When a match is found, the rest of the clause is evaluated.

l_clause1

An expression whose first element is an atom or list of atoms to be compared against the value of g_selectionExpr. The remainder of the l_clause is evaluated if a match is found.

l_clause2

Zero or more clauses of the same form as l_clause1.

Value Returned

g_result

Returns the value of the last expression evaluated in the matched clause.

nil

Returns nil if there is no match.

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"

Sets path 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

l_clause1

Each clause must be of the form (g_condition g_expr1…). When g_condition evaluates to non-nil, all the succeeding expressions are evaluated.

Value Returned

g_result

Returns the value of the last expression of the satisfied clause.

nil

Returns nil if no clause is satisfied.

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
 ⠀
X