Table of Contents

About

Expression is scripting language used in PlanetCNC TNG.

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

Numbers

42
3.14
-5 

Strings

'hello'
'Expr is fun' 

Booleans

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

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

a = none();
a == none()();  // true

nan()

Represents Not a Number (like IEEE 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

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

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:

Rules:


3. Variables and Scope

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


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


7. Special Values in Control Flow


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:

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