Printer
A code printer that converts AST nodes back into formatted Loreline source code. Handles indentation, newlines, and pretty-printing of all node types.
Instance Members
enableComments: BoolSet to false to ignore comments.
clear(): VoidResets the printer to its initial state.
indent(): VoidIncreases the current indentation level by one.
unindent(): VoidDecreases the current indentation level by one.
Writes a string to the output buffer, handling indentation if at start of line.
| Name | Type | Description |
|---|---|---|
s |
String | String to write |
| Returns | Description |
|---|---|
| Printer | This printer instance for chaining |
writeln(): PrinterWrites a line break to the output.
| Returns | Description |
|---|---|
| Printer | This printer instance for chaining |
newline(): PrinterAdds a newline to the output, limiting consecutive blank lines to 1.
| Returns | Description |
|---|---|
| Printer | This printer instance for chaining |
tab(): PrinterAdds indentation at the current level to the output.
| Returns | Description |
|---|---|
| Printer | This printer instance for chaining |
line(): PrinterAlias for newline().
| Returns | Description |
|---|---|
| Printer | This printer instance for chaining |
toString(): StringReturns the current content of the output buffer.
| Returns | Description |
|---|---|
| String | Generated source code |
Main entry point for printing an AST node to source code.
| Name | Type | Description |
|---|---|---|
node |
Node | Root node to print |
| Returns | Description |
|---|---|
| String | Generated source code as string |
Creates a new code printer with customizable formatting options.
| Name | Type | Default | Description |
|---|---|---|---|
indent |
String | " " |
String used for each level of indentation (default: 4 spaces) |
newline |
String | "\n" |
String used for line breaks (default: \n) |
Private Members
Dispatches a node to its appropriate printing function based on type.
| Name | Type | Default | Description |
|---|---|---|---|
node |
Node | Node to print | |
sameLine |
Bool | false |
Prints any leading comments attached to a node.
| Name | Type | Description |
|---|---|---|
node |
AstNode | Node with potential comments |
Prints any trailing comments attached to a node.
| Name | Type | Description |
|---|---|---|
node |
AstNode | Node with potential comments |
printImport(imp: NImportStatement): VoidPrints an import statement.
| Name | Type | Description |
|---|---|---|
imp |
NImportStatement | Import statement node |
Prints a complete Loreline script node.
| Name | Type | Description |
|---|---|---|
script |
Script | Script node to print |
printStateDecl(state: NStateDecl): VoidPrints a state declaration node.
| Name | Type | Description |
|---|---|---|
state |
NStateDecl | State declaration to print |
printCharacterDecl(char: NCharacterDecl): VoidPrints a character declaration node.
| Name | Type | Description |
|---|---|---|
char |
NCharacterDecl | Character declaration to print |
Prints a beat declaration node.
| Name | Type | Description |
|---|---|---|
beat |
NBeatDecl | Beat declaration to print |
printFunctionDecl(func: NFunctionDecl): VoidPrints a function declaration node.
| Name | Type | Description |
|---|---|---|
func |
NFunctionDecl | Function declaration to print |
printTextStatement(text: NTextStatement): VoidPrints a text statement node.
| Name | Type | Description |
|---|---|---|
text |
NTextStatement | Text statement to print |
printDialogueStatement(dialogue: NDialogueStatement): VoidPrints a dialogue statement node.
| Name | Type | Description |
|---|---|---|
dialogue |
NDialogueStatement | Dialogue statement to print |
printChoiceStatement(choice: NChoiceStatement): VoidPrints a choice statement node.
| Name | Type | Description |
|---|---|---|
choice |
NChoiceStatement | Choice statement to print |
printImportStatement(imp: NImportStatement): VoidPrints an import statement node.
| Name | Type | Description |
|---|---|---|
imp |
NImportStatement | Import statement to print |
printChoiceOption(option: NChoiceOption): VoidPrints a choice option node.
| Name | Type | Description |
|---|---|---|
option |
NChoiceOption | Choice option to print |
printIfStatement(ifStmt: NIfStatement, ?isElseIf: Bool = false): VoidPrints an if statement node, handling both simple if and if-else structures.
| Name | Type | Default | Description |
|---|---|---|---|
ifStmt |
NIfStatement | If statement to print | |
isElseIf |
Bool | false |
Whether this is part of an else-if chain |
| Name | Type |
|---|---|
node |
AstNode |
| Returns |
|---|
| Bool |
printTransition(trans: NTransition, sameLine: Bool): VoidPrints a transition node.
| Name | Type | Description |
|---|---|---|
trans |
NTransition | Transition to print |
sameLine |
Bool |
printInsertion(insert: NInsertion): VoidPrints an insertion node.
| Name | Type | Description |
|---|---|---|
insert |
NInsertion | Insertion to print |
printStringLiteral(str: NStringLiteral): VoidPrints a string literal node, handling both plain strings and strings with quotes.
| Name | Type | Description |
|---|---|---|
str |
NStringLiteral | String literal to print |
Prints a literal value node (numbers, booleans, arrays, objects).
| Name | Type | Description |
|---|---|---|
lit |
NLiteral | Literal node to print |
Prints a field access expression (object.field).
| Name | Type | Description |
|---|---|---|
access |
NAccess | Field access node to print |
printArrayAccess(access: NArrayAccess): VoidPrints an array access expression (array[index]).
| Name | Type | Description |
|---|---|---|
access |
NArrayAccess | Array access node to print |
Prints a function call expression.
| Name | Type | Description |
|---|---|---|
call |
NCall | Function call node to print |
Prints a binary operation expression (a + b, a && b, etc). Handles operator precedence with parentheses when needed.
| Name | Type | Default | Description |
|---|---|---|---|
binary |
NBinary | Binary operation node to print | |
skipParen |
Bool | false |
Prints a unary operation expression (!x, -x).
| Name | Type | Description |
|---|---|---|
unary |
NUnary | Unary operation node to print |
Prints an assignment expression (a = b, a += b).
| Name | Type | Description |
|---|---|---|
assign |
NAssign | Assignment node to print |
Prints an in-line expression optionally wrapped in parentheses.
| Name | Type | Description |
|---|---|---|
expr |
NExpr | Expression to wrap in parentheses |
parens |
Bool |
getOperator(op: Anonymous): StringConverts a token type to its corresponding operator string representation.
| Name | Type | Description |
|---|---|---|
op |
Anonymous | Token type to convert |
| Returns | Description |
|---|---|
| String | String representation of the operator |