lua.generator
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.formatNumber | Converts a number into a Lua number literal that reads back as the same number. |
lua.generator.generate | Generates Lua code from an AST node. |
#lua.generator.formatNumber
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
| Name | Type | Description |
|---|---|---|
value | number | The number to format. |
Returns
| Type | Name | Description |
|---|---|---|
string | literal | The number literal. |
#lua.generator.generate
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
| Name | Type | Description |
|---|---|---|
node | lua.ast.Node | The AST node to generate code from, a chunk, a statement or an expression. |
optionsoptional | lua.GeneratorOptions | The generator options. |
Returns
| Type | Name | Description |
|---|---|---|
string | code | The generated Lua code. |