Sidebar

Home



Expressions V4


Tutorials

How-To

Reference

  Lexical basics
  Type system
  Variables and assignment
  Operators
  Expression rules
  Control flow
  Functions
  Built-in functions
   None and NaN
   Arithmetic
   Algebra
   Logarithmic and Exponential
   Trigonometric
   Rounding and Centering
   Strings
   Output, Formatting, Clipboard, and Errors
   Dialog Functions
   Platform and Paths
  Methods and properties
  Built-in methods
   Common Value Methods
   Number Methods
   String Basic Methods
   String Slice Methods
   String Parsing Methods
   String Formatting Methods
   String Regex Methods
   String Trim and Case Methods
  Objects
  Built-in objects
   Global objects
    settings
    controller
    session
    python
   Collections
    array
    map
    bytes
   File Format I/O
    image_data
    image_stream
   Utils
    crypto
    timer
   Comm
    serial
   Dialogs
    file_open
    file_save
    msg_ok
    msg_ok_cancel
    msg_yes_no
    msg_password
   GUI objects
    window
    panel
    group
    splitpanel
    label
    textbutton
    drawablebutton
    togglebutton
    togglegroup
    textinput
    textedit
    numinput
    slider
    combobox
    listbox
    progressbar
    led
    separator
    menu
    image
    snake
  Classes and user-defined objects
  Include system
  Error model
  Execution model and sessions
  Host integration
  Limits and performance
  Formal reference
  Glossary

Cookbook

exprv4:reference:host_integration

Host Integration

Expr is a language runtime embedded in the host application.

The core language defines parsing, expressions, values, variables, functions, classes, objects, control flow, errors, and evaluation behavior. The host decides where Expr is used, which session is attached, which output stream receives text, which built-in objects are available, and which compatibility options are enabled for a specific context.

Most user scripts should use normal Expr syntax. Host integration features are for specific application contexts such as G-code expression evaluation, custom dialogs, probing scripts, ATC scripts, file import helpers, and interactive MDI use.


Integration Responsibilities

Expr is responsible for language semantics:

  • tokenizing and parsing Expr source
  • evaluating expressions and statements
  • applying scope and assignment rules
  • storing session variables, user functions, and user classes
  • reporting parse and runtime errors
  • calling built-in functions, object methods, and callbacks

The host application is responsible for application policy:

  • where a script can be executed
  • which session name is used
  • which output sink is attached
  • which built-in objects and host services are exposed
  • which include paths are allowed
  • which compatibility options are enabled
  • whether a script is trusted enough to access files, settings, Python, serial ports, dialogs, or machine-related services

Expr should not be treated as a standalone sandbox. If a host exposes side-effect operations, scripts can use those operations.


Common Host Contexts

Expr can be used in several different host contexts.

Context Typical behavior
Interactive or MDI evaluation The host can use a named session so variables, functions, and classes persist between commands.
G-code expression evaluation The host can enable G-code compatibility options and connect Expr variables to G-code parameters.
Custom dialogs and forms Scripts can construct GUI objects and react to callbacks.
ATC, probing, and automation scripts Scripts can use Expr as an alternative to longer G-code script sequences.
File import, export, and processing helpers Scripts can use file format objects, dialogs, arrays, maps, and data buffers.
Background or asynchronous work Host objects such as timers, serial monitors, or Python tasks can invoke callbacks later.

The available objects and the persistence of variables depend on the context selected by the host.


Runtime Compatibility Options

Compatibility options are selected by the host for a specific evaluation context. They are not recommended for normal Expr source files unless that context requires them.

Wrapper Markers

Wrapper markers are host or G-code embedding syntax, not core Expr syntax.

A host context may allow Expr text to be embedded inside wrapper markers such as {! ... !}. The host extracts or marks the Expr portion before evaluation.

Do not use wrapper markers in ordinary Expr include files or standalone Expr scripts.

Hash Variables

Hash variables are a G-code compatibility option.

When Expr is called from G-code expression evaluation, the host can enable hash-variable syntax so Expr variables and G-code parameters refer to the same values in that context.

Examples of compatibility forms include:

#1
#name
#<name>

Use normal Expr identifiers in normal scripts:

speed = 1200;
depth = -1.5;

See Lexical basics.

Named Operators

The host can enable named operator compatibility for older or embedded contexts.

Examples include `AND`, `OR`, `XOR`, `NOT`, `DIV`, `MOD`, `EQ`, `NE`, `GE`, `LE`, `GT`, and `LT`.

Normal Expr scripts should use the standard symbolic operators described in Operators.

Modifiable Comments

//= is a host-editable comment form when modifiable-comment compatibility is enabled.

It is not ordinary comment syntax for normal Expr scripts. Use // and /* ... */ for normal comments.

See Lexical basics.

Include Files Use Strict Syntax

Included files are parsed with default Expr parsing options.

They do not inherit caller compatibility options such as G-code hash variables, named operators, wrapper markers, or modifiable comments.

This keeps shared include files predictable and portable between host contexts.

See Include system.


Sessions Selected by the Host

Each evaluation runs in an Expr session. The host chooses the session name, or it can evaluate without relying on persistent session state.

A host can:

  • reuse a named session for interactive workflows
  • create an isolated session for a job or background task
  • use a temporary session for one-shot evaluation
  • clear or delete sessions when state is no longer needed

Root variables, user-defined functions, and user-defined classes persist only when the same named session is reused.

// first evaluation in a persistent host session
part_count = 1;
 
// later evaluation in the same host session
part_count += 1;
print(part_count);

The global `session` object exposes session inspection and management for contexts where the host allows it.

See Execution model and sessions and session.


Output and Printing

Functions such as `print()` and `printf()` write to the output sink selected by the host.

Depending on context, output may appear in a console, message/log panel, debug output, script result view, or another host-defined destination.

`enable_print(false)` can suppress print output for the current evaluation context when available:

enable_print(false);
print('hidden');
enable_print(true);
print('visible');

See Output, Formatting, Clipboard, and Errors.


Host Paths and File Access

Path handling is host-defined.

The `path()` function returns the profile path. `path(relativePath)` resolves profile-relative paths that start with `.` and source-relative paths that do not.

profile = path();
script = path('./scripts/probe.expr');

Other objects that load or save files also depend on host path policy and platform behavior. Examples include file dialogs, image data, image streams, byte buffers, Python files, and crypto file operations.

Use `path(…)` or host file dialogs when scripts should be portable between machines.

See Platform and Paths, Include system, and Dialogs.


Host-Provided Functions and Objects

Some built-in functions and objects are pure language helpers. Others are bridges to host services.

Important host-facing groups include:

Area Reference Examples
Output, clipboard, dialogs, platform, paths Output, Formatting, Clipboard, and Errors, Dialog Functions, Platform and Paths `print()`, `printf()`, `enable_print()`, `to_clipboard()`, `from_clipboard()`, `msgdlg_ok()`, `platform()`, `path()`
Global objects Global objects `settings`, `controller`, `session`, `python`
Dialog objects Dialogs `file_open()`, `file_save()`, `msg_ok()`, `msg_ok_cancel()`, `msg_yes_no()`, `msg_password()`
GUI objects GUI objects `window()`, `panel()`, `textinput()`, `numinput()`, `image()`, `splitpanel()`
File format I/O File Format I/O `image_data()`, `image_stream()`
Collections and buffers Collections `array()`, `map()`, `bytes()`
Utilities Utils `crypto()`, `timer(callback)`
Communication Comm `serial()`

A host build or context can expose additional application-specific globals. Scripts that depend on such globals are host-specific.


G-code Integration Notes

When Expr is used from G-code, the host can evaluate Expr expressions as part of G-code parsing or script execution.

In this mode, the host may:

  • enable hash-variable compatibility
  • map Expr variables to G-code parameters
  • enable wrapper marker handling in G-code text
  • provide G-code or machine-related objects
  • choose a session strategy for the current program or command

This does not change normal Expr file syntax. Shared Expr include files should use strict Expr syntax and normal variable names.


Callbacks and Asynchronous Host Objects

Some host objects store callable Expr references and invoke them later.

Examples include:

  • `timer(callback)`
  • serial monitor callbacks
  • GUI event callbacks
  • asynchronous Python task handling, where supported by the host object

A callback runs in a new evaluation context connected to the relevant session selected by the object or host.

If callback state must survive after the original evaluation ends, keep it in the object instance or in a named session.

class Counter()
{
    Count = 0;
 
    function OnTimer(n)
    {
        Count += 1;
        print('timer call ', Count);
    }
}
 
counter = Counter();
t = timer(counter.OnTimer);
t.start(1000, 3);

Callback errors are reported through the object or host error path. A host object may stop the operation after a callback error.

See Execution model and sessions, timer, and serial.


Security and Trust Boundary

Expr is not a security sandbox.

A script can only do what the host exposes, but exposed objects may allow side effects such as:

  • reading or writing files
  • modifying application settings
  • running Python code
  • opening dialogs or windows
  • accessing serial ports
  • using clipboard data
  • starting timers or background work

Host-side recommendations:

  • Treat Expr scripts as trusted configuration or trusted automation code.
  • Restrict include roots to trusted folders.
  • Avoid exposing sensitive host objects to untrusted script text.
  • Log script source, context, and errors when traceability matters.
  • Prefer strict Expr syntax for shared script libraries.

Portability Guidelines

For scripts intended to be reused in different contexts:

  • Use normal Expr identifiers instead of hash variables.
  • Use symbolic operators instead of named operator compatibility.
  • Put reusable code in include files that use strict Expr syntax.
  • Avoid hard-coded absolute paths.
  • Use `path(…)` or file dialogs for host-resolved paths.
  • Keep host-specific code near the script entry point.
  • Document required host objects such as `settings`, `python`, `serial`, or GUI objects.

Previous: Execution model and sessions

Next: Limits and performance

exprv4/reference/host_integration.txt · Last modified: by 127.0.0.1

Page Tools