Expressions

MAXScript is an expression-based language. Every construct in the language is an expression and yields a result. This includes constructs that other languages consider as statements. The simplified syntax of the language allows you to build very expressive code. Anywhere you can write an <expr> expression, and you can write any construct in MAXScript.

A good example of this is the if construct. In statement-based languages, there is usually one syntax for if statements and another for conditional expressions. In MAXScript, a single syntax is used for both cases.

FOR EXAMPLE,

The following two lines of code are both valid and their meaning is obvious:

``` if a > b then print c else print d x = if a > b then c else d

```

You can also write the following line with a nested if expression and a nested assignment, which itself is an expression:

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

```

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

FOR EXAMPLE,

``` a = 2 b = 1 ( print a print b if a > b then print "the big a" )

```

Each expression in the block is separated by a line break. You can also write several expressions in one line using a ";" (semicolon) to separate them.

FOR EXAMPLE,

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

```

Block-expressions are themselves <expr> expressions. They evaluate their component expressions in sequence and their value is the value of the last expression in the block.

To create classic block-structured statements:

YOU MIGHT WRITE

``` c = 3 d = 4 if a > b then ( print c x = sqrt (c) ) else ( print d x = sin (e) )

```

OR

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 as any of the following:

<simple_expr>
<variable_decls>
<assignment>
<context_expr>
<if_expr>
<while_loop>
<do_loop>
<for_loop>
<loop_exit>
<case_expr>
<try_expr>
<function_def>
<function_return>
<struct_def>
<max_command>
<utility_def>
<rollout_def>
<tool_def>
<rcmenu_def>
<plugin_def>

These expressions constitute the main constructs in MAXScript.