dreamworkAPI
Ctrl K
GitHub
libraryDocs / lua / generator

lua.generator

librarysharedmenusource

Lua code generator, turns AST from lua.parser ( or a hand-made one ) back into Lua code.

Parentheses are added where operator priorities require them, so hand-made ASTs do not need Paren nodes.

Comments are not part of the AST and are not generated.

Functions

lua.generator.formatNumberConverts a number into a Lua number literal that reads back as the same number.
lua.generator.generateGenerates Lua code from an AST node.

#lua.generator.formatNumber

sharedmenunodiscardsource
function lua.generator.formatNumber(value: number): string

Converts a number into a Lua number literal that reads back as the same number.

inf, -inf and nan become (1/0), (-1/0) and (0/0).

Parameters
NameTypeDescription
valuenumberThe number to format.
Returns
TypeNameDescription
stringliteralThe number literal.

#lua.generator.generate

sharedmenunodiscardsource
function lua.generator.generate(node: lua.ast.Node, options?: lua.GeneratorOptions): string

Generates Lua code from an AST node.

By default the code is formatted with one statement per line and indentation, options.compact removes all optional whitespace, options.lines places statements on their original lines ( line numbers in errors stay the same as in the parsed code ).

Parameters
NameTypeDescription
nodelua.ast.NodeThe AST node to generate code from, a chunk, a statement or an expression.
optionsoptionallua.GeneratorOptionsThe generator options.
Returns
TypeNameDescription
stringcodeThe generated Lua code.
Generated from the dreamwork meta branch.