Expression is scripting language used in PlanetCNC TNG.
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:
42 3.14 -5
'hello' 'Expr is fun'
a = 10; b = 20; a + b; // → 30
Variables live in the root scope by default.
Expr supports math, logic, and bitwise operators.
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)
-5; // -5 +5; // 5 !0; // 1 (logical NOT) !5; // 0 \~0; // -1 (bitwise NOT)
5 > 2; // 1 5 == 5; // 1 5 != 5; // 0
Short-circuiting works:
a=5; b=0; (a>10) && (b=5); // left side false → right not run b; // still 0
a=5; a << 1; // 10 a=5; a >> 1; // 2
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.
a = 0; loop(5) { a = a + 1; }; a; // 5
a = 1; while(a < 10) { a = a \* 2; }; a; // 16
sum = 0; for(i=0; i<5; i=i+1) { sum = sum + i; }; sum; // 10
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.
A special “no value”.
a = none(); a == none()(); // true
Represents Not a Number (like IEEE NaN).
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.
n = 5; fact = 1; for(i=1; i<=n; i=i+1) { fact = fact * i; }; fact; // → 120
fib1 = 1; fib2 = 1; loop(10) { temp = fib1; fib1 = fib2; fib2 = temp + fib2; }; fib1; // → 89
a = 1; if(1) { set a = 2; if(1) { set a = 3; a; // → 3 }; a; // → 2 }; a; // → 1
Expr lets you mix simple math with structured programming in a very compact syntax.
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.
Every expression or statement must end with a semicolon `;`, unless inside parentheses or braces. Examples:
a = 5; // valid if(1) 42; // requires terminating semicolon
Expr has the following value types:
Rules:
Examples:
a = 5; // root variable if(1) { set a = 10; // local variable "a" a; // 10 }; a; // 5
(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)
2 + 3 * 5; // 17 (2 + 3) * 5; // 25 2 ** 3 ** 2; // 512 -2 ** 2; // -4 (++a) ** 2; // increment first, then square
Logical `&&`, `||`, and `^^` support short-circuit evaluation (right side only evaluated if needed).
if(condition) { ... } else { ... };
* `condition` evaluates truthy (nonzero, true, nonempty string). * Can return a value (like expression). * Without `else`, returns `none()`.
loop(n) { body };
Executes body exactly `n` times (if `n` ≤ 0, body is skipped).
while(condition) { body };
Runs body while condition is true.
for(init; condition; step) { body };
Initialization, test, step like C-style `for`.
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(x²+y²)
n = 5; fact = 1; for(i=1; i<=n; i=i+1) { fact = fact * i; }; fact; // 120
fib1=1; fib2=1; loop(10) { temp=fib1; fib1=fib2; fib2=temp+fib2; }; fib1; // 89
a = 10; b = 20; max = if(a > b) { a; } else { b; }; max; // 20
a = 3; a = if(0) { 5; }; // skipped, a stays 3 a; // 3
Expr is a C-style expression language with:
It is suitable for scripting, formulas, and embedding in applications.