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

start(?beatName: String): Void

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.

save(): SaveData

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

restore(saveData: SaveData): Void

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

resume(): Void

Resumes execution after restoring state. This should be called after restore() to continue execution.


getCharacter(name: String): Any

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

getCharacterField(character: String, name: String): Any

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

setCharacterField(character: String, name: String, value: Any): Void

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

new(script: Script, handleDialogue: DialogueHandler, handleChoice: ChoiceHandler, handleFinish: FinishHandler, ?options: InterpreterOptions): Void

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

script: Script

The script being executed.


handleDialogue: DialogueHandler

User-defined dialogue handler, which takes care of displaying the dialogues.


handleChoice: ChoiceHandler

User-defined choice handler, which takes care of displaying the choices and providing a response to the interpreter.


handleFinish: FinishHandler

User-defined finish handler, which is called when the current execution has finished.


topLevelState: RuntimeState

The top level state, which is shared across the whole script execution.


topLevelCharacters: Map

Top level characters can be referenced and their state can also be modified from anywhere in the script.


topLevelBeats: Map

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.


topLevelFunctions: Map

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.


lens: Lens

The lens instance allowing to get more information about the AST.


strictAccess: Bool

Tells whether access is strict or not. If set to true, trying to read or write an undefined variable will throw an error.


currentScope: RuntimeScope

Current scope associated with current execution state.


currentInsertion: RuntimeInsertion

Current insertion associated with current scope or a parent scope, with current execution state.


nextScopeId: Int

The next scope id to assign when pushing a new scope. Every time we reset the stack, this counter is also reset.


nextInsertionId: Int

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.


flushing: Bool

Internal flag to know if we are currently flushing sync callbacks (to prevent unexpected recursive flushes).


finishTrigger: EvalNext

Keep track of which callback is the one that would trigger finish.


beatToResume: NBeatDecl

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.


debug(expr: Dynamic): Dynamic
Name Type
expr Dynamic
Returns
Dynamic

removeCurrentInsertion(): Void

customCreateFields(interpreter: Interpreter, type: String, node: Node): Any

A custom instanciator to create fields objects.

Name Type
interpreter Interpreter
type String
node Node
Returns
Any

resumeFromLevel(scopeLevel: Int, next: Function): Void
Name Type
scopeLevel Int
next Function

serializeScope(scope: RuntimeScope, insertions: Dynamic): SaveDataScope

Serializes a scope to save data.

Name Type Description
scope RuntimeScope The scope to serialize
insertions Dynamic
Returns Description
SaveDataScope The serialized scope data

serializeInsertion(insertion: RuntimeInsertion, insertions: Dynamic): Null<Int>
Name Type
insertion RuntimeInsertion
insertions Dynamic
Returns
Null<Int>

serializeChoiceOption(option: ChoiceOption, insertions: Dynamic): SaveDataChoiceOption
Name Type
option ChoiceOption
insertions Dynamic
Returns
SaveDataChoiceOption

serializeTextTag(tag: TextTag): SaveDataTextTag
Name Type
tag TextTag
Returns
SaveDataTextTag

serializeState(state: RuntimeState): SaveDataState

Serializes a state to save data.

Name Type Description
state RuntimeState The state to serialize
Returns Description
SaveDataState The serialized state data

serializeCharacters(): Dynamic

Serializes all top-level characters to save data.

Returns Description
Dynamic Object mapping character names to their serialized states

serializeNodeStates(): Dynamic

Serializes all node states to save data.

Returns Description
Dynamic Object mapping node IDs to their serialized states

serializeBeatReference(beat: NBeatDecl): SaveDataBeat

Serializes a beat reference for save data.

Name Type Description
beat NBeatDecl The beat to reference
Returns Description
SaveDataBeat The serialized beat reference

serializeNodeReference(node: AstNode): SaveDataNode

Serializes a node reference for save data.

Name Type Description
node AstNode The node to reference
Returns Description
SaveDataNode The serialized node reference

serializeCharacter(character: RuntimeCharacter): SaveDataCharacter

Serializes a character for save data.

Name Type Description
character RuntimeCharacter The character to serialize
Returns Description
SaveDataCharacter The serialized character data

serializeFields(fields: Any, ?originalFields: Any): SaveDataFields

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
Returns Description
SaveDataFields The serialized fields data

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

resumeNode(node: AstNode, scopeLevel: Int, next: Function): Void

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

resumeBeatRun(beat: NBeatDecl, scopeLevel: Int, next: Function): Void

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

resumeChoiceOption(option: NChoiceOption, scopeLevel: Int, next: Function): Void

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

resumeChoice(choice: NChoiceStatement, scopeLevel: Int, next: Function): Void

Resumes execution of a choice

Name Type
choice NChoiceStatement
scopeLevel Int
next Function

resumeIf(ifStmt: NIfStatement, scopeLevel: Int, next: Function): Void

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

restoreStack(savedStack: Array<SaveDataScope>): Bool

Restores the execution stack from saved data.

Name Type Description
savedStack Array<SaveDataScope> The saved stack data
Returns Description
Bool True if the stack was restored successfully, false otherwise

restoreBeatToResume(savedStack: Array<SaveDataScope>): NBeatDecl

Finds the top-level beat to resume from if stack restoration fails.

Name Type Description
savedStack Array<SaveDataScope> The saved stack data
Returns Description
NBeatDecl The beat to resume from, or null if none can be found

restoreNode(savedNode: SaveDataNode, savedBeatId: NodeId, beat: NBeatDecl): AstNode

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

restoreBeat(beatRef: SaveDataBeat): NBeatDecl

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

restoreState(state: RuntimeState, data: SaveDataState): RuntimeState

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
Returns Description
RuntimeState The restored state

restoreCharacters(data: Dynamic): Void

Restores characters from saved data.

Name Type Description
data Dynamic The saved character data

restoreNodeStates(data: Dynamic): Void

Restores node states from saved data.

Name Type Description
data Dynamic The saved node state data

restoreCharacter(character: RuntimeCharacter, data: SaveDataCharacter): RuntimeCharacter

Restores a character from saved data.

Name Type Description
character RuntimeCharacter The character to restore into, or null to create a new one
data SaveDataCharacter The saved character data
Returns Description
RuntimeCharacter The restored character

restoreFields(target: Any, savedFields: SaveDataFields): Any

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

restoreValue(value: Any): Any

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

random(): Float
Returns
Float

initializeTopLevelFunctions(functions: FunctionsMap): Void

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

initializeTopLevelFunction(func: NFunctionDecl): Void
Name Type
func NFunctionDecl

wrapNext(cb: Function): EvalNext

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

flush(): Void

Flushes all pending synchronous callbacks. This ensures that all pending operations are completed before continuing.


pop(): Bool

Pops the top scope from the execution stack.

Returns Description
Bool True if a scope was popped, false if the stack was already empty

push(scope: RuntimeScope): Void

Pushes a new scope onto the execution stack.

Name Type Description
scope RuntimeScope The scope to push

initializeTopLevelState(state: NStateDecl): Void

Initializes a top-level state declaration. Evaluates all fields and stores their values.

Name Type Description
state NStateDecl The state declaration to initialize

initializeTopLevelBeat(beat: NBeatDecl): Void

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

initializeTopLevelCharacter(character: NCharacterDecl): Void

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

initializeState(state: NStateDecl, scope: RuntimeScope): Void

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

finish(): Void

Finishes script execution and calls the finish handler.


transitionToBeat(beat: NBeatDecl): Void

Transitions to a new beat, clearing the current execution stack.

Name Type Description
beat NBeatDecl The beat to transition to

evalNode(node: AstNode, next: Function): Void

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

evalBeatDecl(beat: NBeatDecl, next: Function): Void

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

evalBeatRun(beat: NBeatDecl, next: Function): Void

Evaluates a beat by executing its body.

Name Type Description
beat NBeatDecl The beat to evaluate
next Function Callback to call when evaluation completes

evalStateDecl(state: NStateDecl, next: Function): Void

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

evalDialogue(dialogue: NDialogueStatement, next: Function): Void

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

evalChoice(choice: NChoiceStatement, next: Function): Void

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

evalChoiceOptionsAndInsertions(beat: NBeatDecl, choice: NChoiceStatement, result: Array<ChoiceOption>, next: Function): Void
Name Type
beat NBeatDecl
choice NChoiceStatement
result Array<ChoiceOption>
next Function

evalInsertion(insertion: RuntimeInsertion, next: Function): Void
Name Type
insertion RuntimeInsertion
next Function

evalChoiceOption(option: NChoiceOption, next: Function): Void

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

evalIf(ifStmt: NIfStatement, next: Function): Void

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

evalAssignment(assign: NAssign, next: Function): Void

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

isBeatCall(node: AstNode, ?scopeLevel: Int = -1): Bool

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

resolveBeatFromCall(call: NCall, ?scopeLevel: Int = -1): NBeatDecl

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

evalCall(call: NCall, next: Function): Void

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

evalTransition(transition: NTransition): Void

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

evaluateString(str: NStringLiteral): AnonStruct

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

stripStringIndent(content: String): String
Name Type
content String
Returns
String

stripStringComments(content: String): String
Name Type
content String
Returns
String

evaluateCondition(expr: NExpr): Bool

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

evaluateArrayLiteral(expr: Array<Dynamic>): Any

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

evaluateObjectLiteral(expr: Array<NObjectField>): Any

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

evaluateExpression(expr: NExpr): Any

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

resolveAccess(access: NAccess, ?target: NExpr, name: String): Anonymous

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

performOperation(op: Anonymous, left: Dynamic, right: Dynamic, pos: Position): Any

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

valueToString(value: Any): String

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

printLoreline(node: Node): String
Name Type
node Node
Returns
String

Metadata

Name Parameters
:hxGen -
:allow loreline.HscriptInterp