User Tools

Site Tools


expr:about

About

Expression is scripting language used in PlanetCNC TNG.

  • Expression is used with scripts, e.g. Expr.txt. Such files contain script code which is evaluated by PlanetCNC TNG expression evaluator.
  • It can be very practical for PLC type applications where periodical evaluation is necessary or for program events.
  • Expr.txt file can contain and use vast array of expression functions, parameters and other commands supported by PlanetCNC TNG.
  • Expr.txt file is located in the profile folder of TNG software.
  • Profile folder can contain multiple Expr.txt files. Specific Expr file naming convention is available for better distinction. e.g.: Expr_Semaphore
  • If multiple expr files use same function, then function will be called first from the first file and only then with the next file.
  • Local variables(e.g. name convention of local variables: red_light, global variables: _red_light ) have scope only within the parent Expr file.
  • Each Expr file contains functions that are marked with symbol #.
  • Each function begins with symbol # and ends with the next # symbol.
  • Each expr file can use built-in functions that are related with program events

Expr V4 Language Tutorial

Note: This tutorial is for Expr version V4.

Expr is a lightweight expression and scripting language for calculations, logic, and control flow. It looks a little like C, Python, and math languages mixed together.

This guide covers the basics:

  • Values and Variables
  • Expressions and Operators
  • Control Flow (`if`, `loop`, `while`, `for`)
  • Functions (math & user expressions)
  • Special Values (`none`, `nan`)
  • Scoping (`set` keyword)
  • Putting it all together

Values and Variables

Numbers

42
3.14
-5 

Strings

'hello'
'Expr is fun' 

Booleans

  • Numeric: `0` = false, non-zero = true
  • String: `“true”` (case-insensitive) = true, anything else = false

Variables

a = 10;
b = 20;
a + b;    // → 30 

Variables live in the root scope by default.


Expressions and Operators

Expr supports math, logic, and bitwise operators.

Arithmetic

2 + 3 \* 5;      // 17 (multiplication before addition)
(2 + 3) \* 5;    // 25
100 / 10 / 2;   // 5  (left associative)
2**3**2;        // 512 (power is right associative) 

Unary

-5;      // -5
+5;      // 5
!0;      // 1 (logical NOT)
!5;      // 0
\~0;      // -1 (bitwise NOT) 

Comparisons

5 > 2;    // 1
5 == 5;   // 1
5 != 5;   // 0 

Logical operators

  • `&&` (AND)
  • `||` (OR)
  • `^^` (XOR)

Short-circuiting works:

a=5; b=0;
(a>10) && (b=5);  // left side false → right not run
b;                // still 0 

Bitwise operators

  • `&`, `|`, `^`
  • `«`, `»`
a=5; a << 1;   // 10
a=5; a >> 1;   // 2

Control Flow

If

a = 5;
if(a > 3) 
{
  'greater';
} 
else 
{
  'less';
}; 

If used as an expression:

t = if(a > 0) 
{ 
  'positive'; 
} 
else 
{ 
  'negative'; 
}; 

Every `if` (and similar keywords) must end with `;` when used as a statement.

Loop (counted loop)

a = 0;
loop(5) { a = a + 1; };
a;   // 5 

While

a = 1;
while(a < 10) 
{
  a = a \* 2;
};
a;   // 16 

For

sum = 0;
for(i=0; i<5; i=i+1) 
{
  sum = sum + i;
};
sum;   // 10 

Functions

Math functions are built in:

pi();        // 3.14159...
sin(pi()/2); // 1
cos(0);      // 1
tan(pi()/4); // 1

Also logarithms, square roots, hyperbolic functions, etc.


Special Values

none()

A special “no value”.

  • Returned by empty `if`, `loop`, `while`, `for`
  • Evaluates as false in conditions
  • Cannot be used in arithmetic/string ops
a = none();
a == none()();  // true

nan()

Represents Not a Number (like IEEE NaN).

  • Propagates through math
  • `nan() != nan()`

Scoping with `set`

Normally, assignments (`=`) affect variables in the root scope or the nearest existing scope.

Use `set` to create a variable in the current local scope:

a = 5;
if(1) 
{
  set a = 10;   // shadows root 'a'
  a;            // → 10
};
a;              // → 5 (root unchanged)

Scopes apply to blocks: `{ … }`, `if`, `loop`, `while`, `for`.

Variables defined with `set` disappear when the block ends.


Putting it all together

Factorial

n = 5;
fact = 1;
for(i=1; i<=n; i=i+1) 
{
  fact = fact * i;
};
fact;   // → 120 

Fibonacci

fib1 = 1; fib2 = 1;
loop(10) 
{
  temp = fib1;
  fib1 = fib2;
  fib2 = temp + fib2;
};
fib1;   // → 89 

Nested scopes

a = 1;
if(1) 
{
  set a = 2;
  if(1) 
  {
    set a = 3;
    a;     // → 3
  };
  a;       // → 2
};
a;         // → 1 

Summary

  • Statements end with `;`
  • Blocks use `{ … }`
  • Operators: math, logical, bitwise, comparisons, power
  • Keywords: `if`, `loop`, `while`, `for`, `break`, `continue`, `set`
  • Special values: `none()`, `nan()`
  • Scope: `set` creates variables visible only in current block and children
  • Assignments (`=`) modify the nearest variable if it exists, otherwise root

Expr lets you mix simple math with structured programming in a very compact syntax.


Expr Language Reference

This document defines the syntax, semantics, and behavior of the Expr scripting language. It contains everything needed for both humans and AI systems to learn and correctly write programs in Expr.


1. Lexical Rules

Tokens

  • Identifiers: start with a letter or underscore, followed by letters, digits, or underscores.
  • Numbers: integer (`42`), floating point (`3.14`), scientific (`1e-6`).
  • Strings: single-quoted `'text'`.
  • Booleans: `true` / `false` (case-insensitive).
  • Keywords: `if`, `else`, `loop`, `while`, `for`, `break`, `continue`, `set`, `none`, `nan`, etc.
  • Operators: `+ - * / % ** ! ~ & | ^ << >> && || ^^ ? : = ++ --`.
  • Separators: `;` terminates expressions. `{}` group statements. `()` group expressions or call functions.

Semicolon Rule

Every expression or statement must end with a semicolon `;`, unless inside parentheses or braces. Examples:

a = 5;     // valid
if(1) 42;  // requires terminating semicolon 

2. Values

Expr has the following value types:

  • Number: 64-bit floating-point (`double`).
  • String: sequences of characters.
  • Boolean: `true` / `false`.
  • None: `none()` — represents “no value”.
  • NaN: `nan()` — mathematical not-a-number.

Rules:

  • `none()` evaluates as false in conditions, but cannot be used in arithmetic.
  • `nan()` propagates through math; comparisons with it are false (`nan() != nan()`).

3. Variables and Scope

  • By default, variables are global (root scope).
  • A variable can be declared in a local scope with `set`.
  • Local variables shadow outer variables, but root remains unchanged.
  • Assignment into `none()` is skipped (special case for keyword returns).

Examples:

a = 5;              // root variable
if(1) 
{
  set a = 10;       // local variable "a"
  a;                // 10
};
a;                  // 5 

4. Operators

Precedence and Associativity

(from highest to lowest):

1. **Postfix**: `a++ a--`
2. **Prefix**: `++a --a + - ! ~`
3. **Power**: `**` (right-associative)
4. **Multiply/Divide/Modulo**: `* / %`
5. **Add/Subtract**: `+ -`
6. **Shift**: `<< >>`
7. **Comparison**: `< <= > >=`
8. **Equality**: `== !=`
9. **Bitwise AND**: `&`
10. **Bitwise XOR**: `^`
11. **Bitwise OR**: `|`
12. **Logical AND**: `&&`
13. **Logical XOR**: `^^`
14. **Logical OR**: `||`
15. **Ternary**: `? :` (right-associative)
16. **Assignment**: `=` (right-associative)

Examples

2 + 3 * 5;        // 17
(2 + 3) * 5;      // 25
2 ** 3 ** 2;      // 512
-2 ** 2;          // -4
(++a) ** 2;       // increment first, then square

Short-circuit

Logical `&&`, `||`, and `^^` support short-circuit evaluation (right side only evaluated if needed).


5. Control Flow

If

if(condition) { ... } else { ... };

* `condition` evaluates truthy (nonzero, true, nonempty string). * Can return a value (like expression). * Without `else`, returns `none()`.

Loop (counted)

loop(n) { body };

Executes body exactly `n` times (if `n` ≤ 0, body is skipped).

While

while(condition) { body };

Runs body while condition is true.

For

for(init; condition; step) { body };

Initialization, test, step like C-style `for`.

Break & Continue

  • `break;` exits the current loop.
  • `continue;` skips to next iteration.

6. Functions

Built-in Functions

Math

pi()      // 3.14159...
sin(x) cos(x) tan(x)
asin(x) acos(x) atan(x) atan2(y,x)
sqrt(x) log(x) log2(x) log10(x)
exp(x) expm1(x) 

Hyperbolic

sinh(x) cosh(x) tanh(x)
asinh(x) acosh(x) atanh(x) 

Conversions

rad2deg(x) deg2rad(x) 

Other

hypot(x,y)  // sqrt(+) 

Domains and Errors

  • `asin`, `acos` require `-1 ⇐ x ⇐ 1`.
  • `sqrt` requires `x >= 0`.
  • `log` requires `x > 0`.
  • Division by zero throws error.
  • `pow(0, negative)` throws error.

7. Special Values in Control Flow

  • `if(false)` returns `none()`.
  • Assigning `none()` to a variable can be skipped (`a = if(0) { 5; };` leaves `a` unchanged).
  • `none()` used in expressions causes operator error, except in assignment.

8. Examples

Factorial

n = 5; fact = 1;
for(i=1; i<=n; i=i+1) 
{
  fact = fact * i;
};
fact;   // 120

Fibonacci

fib1=1; fib2=1;
loop(10) 
{
  temp=fib1;
  fib1=fib2;
  fib2=temp+fib2;
};
fib1;   // 89

Max of Two Numbers

a = 10; b = 20;
max = if(a > b) { a; } else { b; };
max;   // 20

Using none()

a = 3;
a = if(0) { 5; };  // skipped, a stays 3
a;  // 3

Summary

Expr is a C-style expression language with:

  • Numbers, strings, booleans, `none`, `nan`
  • Strong operator precedence & short-circuit logic
  • Variables with root/local scopes
  • Control flow (`if`, `loop`, `while`, `for`)
  • Functions for math and conversion
  • Special handling of `none()`

It is suitable for scripting, formulas, and embedding in applications.

expr/about.txt · Last modified: by andrej

Page Tools