Fundamentals: Expressions

Expressions are a combination of operands and operators which are evaluated according to the macro system's rules of precedence. Fundamentally, all macro command statements are expressions.

The Fundamentals: Variables topic has already introduced the assignment expression. Example:

INTEGER:#COUNTER
#COUNTER=1

The #COUNTER=1 statement is an expression. The Operands are the #COUNTER and 1 values on either side of the equal sign. The equal sign (=) is the Operator.

Expressions can include combinations of Operators. For example:

INTEGER:#COUNTER=1
#COUNTER=#COUNTER + 1

There are two Operators in this expression. The assignment operator (=) and the addition operator (+).

MCL functions can also be used as operands. The macro function TOTEL() returns an integer value that is the number of elements in the current process model file. Example:

INTEGER:#COUNTER
			
#COUNTER = TOTEL() + 1

The operands in this example are #COUNTER, TOTEL() and 1. The operators are = and +.

Operators work on more than just numbers. The following shows how the addition operator works with strings.

STRING:#SMESSAGE = "The addition operator "
#SMESSAGE = #SMESSAGE + "will concatenate strings."
PAUSE[TX=#SMESSAGE]

When this macro is run, the resulting message box will contain the text:

The addition operator will concatenate strings.

Using Element Name Expressions

For element name expressions to work properly, you must include the evaluation expression completely in quotation marks. For example:

PROF_START[EL="SPLIT+1"]

This example tells SmartCAM to start a profile on the next element following the named element "SPLIT."

Rules of Precedence

The rules of precedence are the order that operands and operators are evaluated. The precedence rules are very similar to the order of evaluation rules used in basic mathematics. Evaluation of expressions is done left to right, except where the rules of precedence apply.

The basic rules of precedence are:

  1. Parenthesis are done first. When working with nested parens the innermost is done first.
    In this example: 1 + ((2 + 3) + 4) the expression will evaluate by first handling the innermost parens, (2 + 3), then the next level of parens ((5) + 4), and finally adding 1. When there are two sets of expressions in parenthesis, at the same level, they are done left to right. So (1 + 2) + ( 3 + 4) is evaluated as (1 + 2), then (3 + 4), then (3) + (7).
  2. Unary operators (signs for positive (+1) or negative (-1) numbers).
  3. Calculation of exponentials
  4. Multiplication and division
  5. Addition and subtraction
  6. Comparison operators: =, <>, >, >=, <, <=
  7. && and || (logical AND and OR)
  8. Assignment operators (=)
  9. Branching: IF() and WHILE()

With the Rules of Precedence in mind, the following expression evaluates to: -3.

1+2^2*(2/(2-2^3/2))

If you want to experiment with precedence or see how an expression will evaluate, you can use the Calculator built into SmartCAM. It is available under Utility - Calculator.

Related Topics

Running a macro

Variables

Macro Development Fundamentals Overview

SmartCAM Automation Overview