Interpreter
loreline.Interpreter (Class)
Main interpreter class for Loreline scripts.
This class is responsible for executing a parsed Loreline script,
managing the runtime state, and interacting with the host application
through handler functions.
Instance Members
Starts script execution from the beginning or a specific beat.
| Name |
Type |
Default |
Description |
beatName |
String |
(optional) |
Optional name of the beat to start from. If null, execution starts from the first beat or a beat named "_" if it exists. |
Saves the current state of the interpreter.
This includes all state variables, character states, and execution stack,
allowing execution to be resumed later from the exact same point.
| Returns |
Description |
| SaveData |
A SaveData object containing the serialized state |
Restores the interpreter state from a SaveData object.
This allows resuming execution from a previously saved state.
| Name |
Type |
Description |
saveData |
SaveData |
The SaveData object containing the serialized state |
Resumes execution after restoring state.
This should be called after restore() to continue execution.
Gets a character by name.
| Name |
Type |
Description |
name |
String |
The name of the character to get |
| Returns |
Description |
| Any |
The character's fields or null if the character doesn't exist |
Gets a specific field of a character.
| Name |
Type |
Description |
character |
String |
The name of the character |
name |
String |
The name of the field to get |
| Returns |
Description |
| Any |
The field value or null if the character or field doesn't exist |
Sets a specific field on a character.
| Name |
Type |
Description |
character |
String |
The name of the character |
name |
String |
The name of the field to set |
value |
Any |
The value of the field to set |
Creates a new Loreline script interpreter.
| Name |
Type |
Default |
Description |
script |
Script |
|
The parsed script to execute |
handleDialogue |
DialogueHandler |
|
Function to call when displaying dialogue text |
handleChoice |
ChoiceHandler |
|
Function to call when presenting choices |
handleFinish |
FinishHandler |
|
Function to call when execution finishes |
options |
InterpreterOptions |
(optional) |
Additional options |
Private Members
The script being executed.
User-defined dialogue handler,
which takes care of displaying the dialogues.
User-defined choice handler,
which takes care of displaying the choices and
providing a response to the interpreter.
User-defined finish handler,
which is called when the current execution has finished.
The top level state, which is shared across the whole script execution.
Top level characters can be referenced and their state
can also be modified from anywhere in the script.
All the top level beats available, by beat name (their identifier in the script).
States associated to a specific node id. These are persistent, like the top level state,
but are only available from where they have been declared and the sub-scopes.
If some state fields already existed in a parent scope, the parent ones will be shadowed by the child ones.
Top level functions available by default in this script.
The current execution stack, which consists of scopes added on top of one another.
Each scope can have its own local beats and temporary states.
The lens instance allowing to get more information about the AST.
Tells whether access is strict or not. If set to true,
trying to read or write an undefined variable will throw an error.
Current scope associated with current execution state.
Current insertion associated with current scope or a parent scope, with current execution state.
The next scope id to assign when pushing a new scope.
Every time we reset the stack, this counter is also reset.
The next insertion id to assign when creating a new insertion.
Every time we reset the stack, this counter is also reset.
syncCallbacks: Array<Function>
List of pending callbacks that should be run synchronously.
Internal flag to know if we are currently flushing sync callbacks
(to prevent unexpected recursive flushes).
Keep track of which callback is the one that would trigger finish.
When loading saved data and failing to restore a full stack of scope,
this contains the beat to resume as fallback.
That beat will always be a top level beat.
removeCurrentInsertion(): Void
A custom instanciator to create fields objects.
resumeFromLevel(scopeLevel: Int, next: Function): Void
| Name |
Type |
scopeLevel |
Int |
next |
Function |
Serializes a scope to save data.
serializeTextTag(tag: TextTag): SaveDataTextTag
Serializes a state to save data.
| Name |
Type |
Description |
state |
RuntimeState |
The state to serialize |
Serializes all top-level characters to save data.
| Returns |
Description |
| Dynamic |
Object mapping character names to their serialized states |
Serializes all node states to save data.
| Returns |
Description |
| Dynamic |
Object mapping node IDs to their serialized states |
Serializes a beat reference for save data.
| Name |
Type |
Description |
beat |
NBeatDecl |
The beat to reference |
| Returns |
Description |
| SaveDataBeat |
The serialized beat reference |
Serializes a node reference for save data.
| Name |
Type |
Description |
node |
AstNode |
The node to reference |
| Returns |
Description |
| SaveDataNode |
The serialized node reference |
Serializes a character for save data.
Serializes fields for save data.
Only fields that have changed from their original values are included.
| Name |
Type |
Default |
Description |
fields |
Any |
|
The fields to serialize |
originalFields |
Any |
(optional) |
The original field values for comparison |
serializeValue(value: Any): Any
Serializes a value for save data.
Handles recursive serialization of objects and arrays.
| Name |
Type |
Description |
value |
Any |
The value to serialize |
| Returns |
Description |
| Any |
The serialized value |
Core function that resumes execution from a given scope.
Works by recreating the execution flow as if we had been running from the beginning.
| Name |
Type |
Description |
node |
AstNode |
The node to resume from |
scopeLevel |
Int |
The scope level to resume at |
next |
Function |
Callback to call when the node execution completes |
resumeNodeBody(node: AstNode, scopeLevel: Int, body: Array<AstNode>, next: Function): Void
Resumes execution of a node body.
| Name |
Type |
Description |
node |
AstNode |
The node containing the body |
scopeLevel |
Int |
The scope level to resume at |
body |
Array<AstNode> |
The body to execute |
next |
Function |
Callback to call when the body execution completes |
Resumes execution of a beat.
| Name |
Type |
Description |
beat |
NBeatDecl |
The beat to resume |
scopeLevel |
Int |
The scope level to resume at |
next |
Function |
Callback to call when the beat execution completes |
Resumes execution of a choice option.
| Name |
Type |
Description |
option |
NChoiceOption |
The choice option to resume |
scopeLevel |
Int |
The scope level to resume at |
next |
Function |
Callback to call when the option execution completes |
Resumes execution of a choice
Resumes execution of an if statement.
| Name |
Type |
Description |
ifStmt |
NIfStatement |
The if statement to resume |
scopeLevel |
Int |
The scope level to resume at |
next |
Function |
Callback to call when the if statement execution completes |
resumeCall(call: NCall, scopeLevel: Int, next: Function): Void
Resumes execution of a beat call.
| Name |
Type |
Description |
call |
NCall |
The call node to resume |
scopeLevel |
Int |
The scope level to resume at |
next |
Function |
Callback to call when the call execution completes |
Restores the execution stack from saved data.
| Returns |
Description |
| Bool |
True if the stack was restored successfully, false otherwise |
Finds the top-level beat to resume from if stack restoration fails.
| Returns |
Description |
| NBeatDecl |
The beat to resume from, or null if none can be found |
Restores a node from a saved reference.
| Name |
Type |
Description |
savedNode |
SaveDataNode |
The saved node reference |
savedBeatId |
NodeId |
The ID of the beat in the saved data |
beat |
NBeatDecl |
The restored beat |
| Returns |
Description |
| AstNode |
The restored node, or null if it couldn't be found |
Restores a beat from a saved reference.
| Name |
Type |
Description |
beatRef |
SaveDataBeat |
The saved beat reference |
| Returns |
Description |
| NBeatDecl |
The restored beat, or null if it couldn't be found |
Restores a state from saved data.
| Name |
Type |
Description |
state |
RuntimeState |
The state to restore into, or null to create a new one |
data |
SaveDataState |
The saved state data |
Restores characters from saved data.
| Name |
Type |
Description |
data |
Dynamic |
The saved character data |
Restores node states from saved data.
| Name |
Type |
Description |
data |
Dynamic |
The saved node state data |
Restores a character from saved data.
Restores fields from saved data.
| Name |
Type |
Description |
target |
Any |
The target object to restore into, or null to create a new one |
savedFields |
SaveDataFields |
The saved field data |
| Returns |
Description |
| Any |
The object with restored fields |
Restores a value from its saved form.
Handles recursive restoration of objects and arrays.
| Name |
Type |
Description |
value |
Any |
The saved value |
| Returns |
Description |
| Any |
The restored value |
Initializes top-level functions available to the script.
This includes built-in functions and any user-provided functions.
| Name |
Type |
Description |
functions |
FunctionsMap |
Optional map of additional functions to make available |
Wraps a callback function to control whether it executes synchronously or asynchronously.
This is crucial for managing the execution flow of the script.
| Name |
Type |
Description |
cb |
Function |
The callback to wrap |
| Returns |
Description |
| EvalNext |
An EvalNext object controlling the callback's execution |
Flushes all pending synchronous callbacks.
This ensures that all pending operations are completed before continuing.
Pops the top scope from the execution stack.
| Returns |
Description |
| Bool |
True if a scope was popped, false if the stack was already empty |
Pushes a new scope onto the execution stack.
Initializes a top-level state declaration.
Evaluates all fields and stores their values.
| Name |
Type |
Description |
state |
NStateDecl |
The state declaration to initialize |
Initializes a top-level beat declaration.
Registers the beat in the top-level beats map.
| Name |
Type |
Description |
beat |
NBeatDecl |
The beat declaration to initialize |
Initializes a top-level character declaration.
Creates a new character state and evaluates all fields.
| Name |
Type |
Description |
character |
NCharacterDecl |
The character declaration to initialize |
Initializes a state declaration within a scope.
Evaluates all fields and stores their values.
| Name |
Type |
Description |
state |
NStateDecl |
The state declaration to initialize |
scope |
RuntimeScope |
The scope in which to initialize the state |
Finishes script execution and calls the finish handler.
Transitions to a new beat, clearing the current execution stack.
| Name |
Type |
Description |
beat |
NBeatDecl |
The beat to transition to |
Evaluates a node, dispatching to the appropriate handler based on node type.
| Name |
Type |
Description |
node |
AstNode |
The node to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates a beat declaration.
Adds the beat to the current scope so it can be referenced by other nodes.
| Name |
Type |
Description |
beat |
NBeatDecl |
The beat declaration to evaluate |
next |
Function |
Callback to call when evaluation completes |
evalNodeBody(beat: NBeatDecl, node: AstNode, body: Array<AstNode>, ?insertion: RuntimeInsertion, next: Function): Void
Evaluates a node body by creating a new scope and executing each node in sequence.
| Name |
Type |
Default |
Description |
beat |
NBeatDecl |
|
The parent beat |
node |
AstNode |
|
The node containing the body |
body |
Array<AstNode> |
|
The body to execute |
insertion |
RuntimeInsertion |
(optional) |
If any, the insertion related to this evaluation |
next |
Function |
|
Callback to call when execution completes |
Evaluates a beat by executing its body.
| Name |
Type |
Description |
beat |
NBeatDecl |
The beat to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates a state declaration.
Initializes the state fields with their evaluated values.
| Name |
Type |
Description |
state |
NStateDecl |
The state declaration to evaluate |
next |
Function |
Callback to call when evaluation completes |
evalText(text: NTextStatement, next: Function): Void
Evaluates a text statement by evaluating the content and calling the dialogue handler.
| Name |
Type |
Description |
text |
NTextStatement |
The text statement to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates a dialogue statement by evaluating the content and calling the dialogue handler.
| Name |
Type |
Description |
dialogue |
NDialogueStatement |
The dialogue statement to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates a choice statement by evaluating the options and calling the choice handler.
| Name |
Type |
Description |
choice |
NChoiceStatement |
The choice statement to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates a choice option by executing its body.
| Name |
Type |
Description |
option |
NChoiceOption |
The choice option to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates an if statement by evaluating the condition and executing the appropriate branch.
| Name |
Type |
Description |
ifStmt |
NIfStatement |
The if statement to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates an assignment by resolving the target, evaluating the value, and applying the assignment.
| Name |
Type |
Description |
assign |
NAssign |
The assignment to evaluate |
next |
Function |
Callback to call when evaluation completes |
isOriginalScriptExpression(expr: NExpr): Bool
Determines whether an expression is an "original script expression" - meaning it only
depends on values directly present in the script and not on runtime state.
Original script expressions can be evaluated at parse time and safely stored as
default values, as they don't depend on dynamic context like variables or
function calls that might change during runtime.
| Name |
Type |
Description |
expr |
NExpr |
The expression to check |
| Returns |
Description |
| Bool |
True if the expression only depends on literal values in the script |
Determines if a node is a beat call.
A beat call is a special function call that executes a beat instead of a normal function.
| Name |
Type |
Default |
Description |
node |
AstNode |
|
The node to check |
scopeLevel |
Int |
-1 |
Optional scope level to search in (defaults to current scope) |
| Returns |
Description |
| Bool |
True if the node is a beat call |
Resolves a call node to a beat declaration if the call references a beat.
This allows handling beat calls differently from regular function calls.
| Name |
Type |
Default |
Description |
call |
NCall |
|
The call node to resolve |
scopeLevel |
Int |
-1 |
Optional scope level to search in (defaults to current scope) |
| Returns |
Description |
| NBeatDecl |
The beat declaration if found, null otherwise |
Evaluates a call node.
If the call references a beat, it executes the beat.
Otherwise, it evaluates it as a regular function call.
| Name |
Type |
Description |
call |
NCall |
The call node to evaluate |
next |
Function |
Callback to call when evaluation completes |
Evaluates a transition node.
Transitions cause execution to jump to a different beat,
clearing the current execution stack.
| Name |
Type |
Description |
transition |
NTransition |
The transition node to evaluate |
Evaluates a string literal into text with tags.
This handles interpolation and tag processing.
| Name |
Type |
Description |
str |
NStringLiteral |
The string literal to evaluate |
| Returns |
Description |
| AnonStruct |
Object containing the evaluated text and any tags |
Evaluates a condition expression for an if statement or choice option.
Converts the result to a boolean according to Loreline's rules.
| Name |
Type |
Description |
expr |
NExpr |
The condition expression to evaluate |
| Returns |
Description |
| Bool |
True if the condition evaluates to a truthy value |
evaluateFunctionCall(call: NCall, next: Function): Any
Evaluates a function call in an expression context.
If next is provided, the function may execute asynchronously.
If next is null, the function must execute synchronously.
| Name |
Type |
Description |
call |
NCall |
The function call node to evaluate |
next |
Function |
Optional callback for asynchronous execution |
| Returns |
Description |
| Any |
The result of the function call |
Evaluates an array literal to a runtime array.
Each element is evaluated recursively.
| Name |
Type |
Description |
expr |
Array<Dynamic> |
The array literal expression to evaluate |
| Returns |
Description |
| Any |
The resulting array |
Evaluates an object literal to a runtime object.
Each field value is evaluated recursively.
| Name |
Type |
Description |
expr |
Array<NObjectField> |
The object literal expression to evaluate |
| Returns |
Description |
| Any |
The resulting object |
Evaluates an expression to its runtime value.
This is the core expression evaluation function that handles all expression types.
| Name |
Type |
Description |
expr |
NExpr |
The expression to evaluate |
| Returns |
Description |
| Any |
The runtime value of the expression |
readAccess(access: Anonymous): Any
Reads the value from a runtime access.
This handles field access, array access, character access, and function access.
| Name |
Type |
Description |
access |
Anonymous |
The runtime access to read |
| Returns |
Description |
| Any |
The value at the access location |
writeAccess(access: Anonymous, value: Any): Void
Writes a value to a runtime access location.
This handles field access and array access.
Character and function access cannot be written to.
| Name |
Type |
Description |
access |
Anonymous |
The runtime access to write to |
value |
Any |
The value to write |
resolveAssignmentTarget(target: NExpr): Anonymous
Resolves an assignment target to a runtime access.
This is used to determine where to write a value in an assignment.
| Name |
Type |
Description |
target |
NExpr |
The target expression to resolve |
| Returns |
Description |
| Anonymous |
The runtime access to write to |
Resolves an access expression to a runtime access.
This handles finding variables, character fields, and functions in the appropriate scopes.
| Name |
Type |
Default |
Description |
access |
NAccess |
|
The access expression to resolve |
target |
NExpr |
(optional) |
Optional target object for field access |
name |
String |
|
The name to access |
| Returns |
Description |
| Anonymous |
The resolved runtime access |
getTypeName(t: Anonymous): String
Helper for getting human-readable type names in errors
| Name |
Type |
Description |
t |
Anonymous |
The type to get a name for |
| Returns |
Description |
| String |
A human-readable name for the type |
Performs a binary operation on two values.
Handles arithmetic, comparison, and logical operations.
| Name |
Type |
Description |
op |
Anonymous |
The operator to apply |
left |
Dynamic |
The left operand |
right |
Dynamic |
The right operand |
pos |
Position |
The source position for error reporting |
| Returns |
Description |
| Any |
The result of the operation |
Converts a value to its string representation.
Used for string interpolation and output.
| Name |
Type |
Description |
value |
Any |
The value to convert to a string |
| Returns |
Description |
| String |
The string representation of the value |
| Name |
Parameters |
:hxGen |
- |
:allow |
loreline.HscriptInterp |