Expressions

Function:

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

Note

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

See also

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.

Note

The function block SMC_ReadNCFile2 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 technical reasons, jump markers (x in L!x) must not contain any local variables.

See also

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

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

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

To do this, the interface SMC_NC_IFunction has to be implemented, and a global instance of the corresponding POU has to be accessible to SMC_NCTokenParser by means of the SMC_NC_GFunctionTable structure.

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.

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.

See also