Skip to content

compile

ffredyk edited this page Jul 23, 2026 · 1 revision

compile

Category

Code Execution

Arity

Unarycompile <string>.

compile <string>

Description

Compiles a string containing SQ# source code into a Code value at runtime. The resulting code can be stored in a variable and executed later with call or spawn. This is runtime compilation — useful for dynamic code generation, user input evaluation, or config-driven behavior.

How It Works

The source string is passed through the SQ# lexer and parser, producing a compiled Code object. If the string contains syntax errors, compilation fails with a diagnostic error. The compiled code captures no lexical closure — it runs in the calling context's scope.

Usage

Basic compile

_code = compile "hint str _this;";
"hello" call _code;            // hints "hello"

Dynamic math

_expr = compile _userInput;    // e.g., "2 + 3 * 4"
_result = call _expr;          // 14

Store and reuse

_myFunc = compile "(_this select 0) * (_this select 1)";
_result = [5, 3] call _myFunc; // 15
_result = [10, 2] call _myFunc; // 20

Build code from parts

_op = "+";
_src = f"(_this select 0) {_op} (_this select 1)";
_calc = compile _src;
_result = [10, 5] call _calc;  // 15

Spawn compiled code

_code = compile "sleep 2; systemChat 'done';";
_handle = spawn _code;

Caveats

  • Compilation has a cost — cache compiled code when reused.
  • No lexical closure — code runs in its call site's variable scope.
  • Syntax errors occur at compile time, not call time.
  • Dynamic code from untrusted input is a security risk — validate carefully.

Thread Safety

Compilation is CPU-only with no side effects. Safe from any scheduler. The resulting Code value is immutable.

See Also

  • call — execute compiled code synchronously
  • spawn — execute compiled code asynchronously
  • execVM — compile and spawn file
  • callUnscheduled — execute outside scheduler

SQ# Wiki

Home

Engine Docs

Migration

Commands

Value Constructors

Arithmetic

Comparison

Logic

Array

String

Math

Random

Type & Introspection

HashMap

Code Execution

Concurrency

Scheduler

Thread Safety

Error

Output

Time

Multiplayer

Compiler

Clone this wiki locally