GENERATE/PREV.gifGENERATE/NEXT.gif

Expressions

MAXScript is an expression-based language, which means that every construct in the language is an expression and yields a result, including constructs that other languages consider statements. This both simplifies the syntax of the language and allows you to build very expressive code. Anywhere you can write an >SP<expr> expression, you can write any construct in MAXScript.

A good example is the if construct. In statement-based languages, there is usually one syntax for if statements and a separate one for conditional expressions. In MAXScript, they are one and the same. For example, the following two lines of code are both valid, and their meaning should be obvious:

if a > b then print c else print d

x = if a > b then c else d

You could even write the following line, containing a nested if expression and a nested assignment, which is an expression itself:

x = if (if a > b then c else d) < e then f else (g = 23)

Another example is the block-expression, denoted <block_expr>. It contains a series of <expr> expressions enclosed in parentheses:

(

print a

print b

if a > b then print "the big a"

)

Each expression in the block is separated by a line break, or you can also write several expressions on one line using a ‘;' semicolon to separate them:

(print a; print b; if a > b then print "the big a")

Block-expressions like this are themselves <expr> expressions. They evaluate their component expressions in sequence and yield as their value the value of the last expression in the block. To create classic block-structured statements, you might write:

if a > b then

(

print c

x = sqrt (c)

)

else

(

print d

x = sin (e)

)

Or, you could use a nested block-expression to compute a comparison operand:

if a > (y = sin b; sqrt(y * z)) then print c

At its simplest, a MAXScript program is made up of <expr> expressions. An <expr> is defined to be any of the following:

<simple_expr>

<assignment>

<if_expr>

<while_loop>

<do_loop>

<for_loop>

<loop_exit>

<variable_decls>

<function_def>

<function_return>

<context_expr>

<max_command>

<utility_def>

These expression types constitute the main constructs in MAXScript. They are described in more detail in the following topics:

Simple Expressions

Control Constructs

Language Constructs

Function Definition

Pathnames

Context Expressions