Skip to main content

Expressions

Function:

In version 4.4.0.0 and higher, it is possible to use mathematical, Boolean, and string expressions. The SMC_ReadNCFile2 and SMC_NCInterpreter function blocks have to be used for this (instead of SMC_ReadNCFile and SMC_NCDecoder).

Tip

Expressions work in the online decoder only (not in the CNC editor).

In principle, expressions can be used in G-code in two positions:

  • As values of G words (one G word consists of an address and a value, for example "G1")

  • As transfer parameters for subprogram calls

Syntax – General

An expression can consist of the following elements:

  • Numeric and string literals

  • Global and local variables

  • Infix operators and functions

  • Commas and brackets

In the same way, parentheses (if not used for identifying comments) and braces can be used for structuring of expressions. This means that the expression (1 + 2} * 3 is permitted.

Tip

The SMC_ReadNCFile2 function block has a new mode (bParenthesesAsComments input) where parentheses are not comments. Instead, multiline comments are opened and closed with "(*" and "*)", respectively. In this new mode, both braces and parentheses can be used for expressions.

A space after the G address is required only if SMC_ReadNCFile2 would not recognize it as an independent token (example: X abs{-2} instead of Xabs{-2}.).

In contrast with ST, single-position functions do not necessarily have to be called with parentheses (example: sin 3).

When parsing, each partial expression is assigned one of the three types: BOOL, LREAL, or STRING. Accordingly, each Infix operator and each function expects a specific sequence of argument types for which noncompliance (wrong type, too few, or too many arguments) returns an error.

Restrictions:

  • Block numbers have to be numeric literals.

  • For strictly technical reasons, jump labels (x in L!x) must not contain any local variables.

For more information, see: Jump, Using Variables

Examples

N01 G36 O$var$ D concat{'prefix_', concat{$var$, '_suffix'}}
N02 G1 X$var$ + sin{pi + 3 * #locvar}
N03 G20 L0 K NOT {myfun{$var$, expt{2, #locvar}} XOR myfun{0, 0}}

Supported operators and functions

Table 54. Infix operators

Character

Type

Arguments

Precedence

MOD

LREAL

LREAL, LREAL

14

*

LREAL

LREAL,LREAL

13

/

LREAL

LREAL, LREAL

13

+

LREAL

LREAL, LREAL

12

-

LREAL

LREAL, LREAL

12

=

BOOL

BOOL, BOOL

10

=

BOOL

LREAL, LREAL

10

=

BOOL

STRING, STRING

10

<>

BOOL

BOOL, BOOL

10

<>

BOOL

LREAL, LREAL

10

<>

BOOL

STRING, STRING

10

>

BOOL

LREAL, LREAL

10

<

BOOL

LREAL, LREAL

10

>=

BOOL

LREAL, LREAL

10

<=

BOOL

LREAL, LREAL

10

AND

BOOL

BOOL, BOOL

6

XOR

BOOL

BOOL, BOOL

5

OR

BOOL

BOOL, BOOL

4



Table 55. Functions

Character

Type

Arguments

-

LREAL

LREAL

ABS

LREAL

LREAL

MAX

LREAL

LREAL, LREAL

MIN

LREAL

LREAL, LREAL

NOT

BOOL

BOOL

TRUE

BOOL

FALSE

BOOL

SIN

LREAL

LREAL

COS

LREAL

LREAL

TAN

LREAL

LREAL

ASIN

LREAL

LREAL

ACOS

LREAL

LREAL

ATAN

LREAL

LREAL

EXP

LREAL

LREAL

LN

LREAL

LREAL

SQRT

LREAL

LREAL

EXPT

LREAL

LREAL, LREAL

FLOOR

LREAL

LREAL

CEIL

LREAL

LREAL

PI

LREAL

LEN

LREAL

STRING

CONCAT

STRING

STRING, STRING



Defining your own functions

It is possible to add your own functions or overwrite an existing implementation. (When parsing the G-code, functions are searched for first in the user functions.)

The SMC_NC_IFunction interface must be implemented and a global instance of the corresponding POU must be transferred to SMC_ReadNCFile2 or SMC_ReadNCFromStream via the structure SMC_NC_GFunctionTable.

The enumeration SMC_GVar_Type is used for the return type and argument types. The entry contained there T_OTHER can be used as a placeholder for a type. When parsing, the system checks that all arguments that correspond to a T_OTHER in the signature have the same type. The type does not matter.

For more information, see: User-Specific G-Code Functions

Error Handling

When possible, the exact position of the defective token and its length are issued in syntax errors. The error position is listed in SMC_ReadNCFile2.errorPos.