2.0 KiB
tags, references
| tags | references | |||
|---|---|---|---|---|
| api/syscall |
|
The Lua API provides functions for parsing and evaluating Lua code.
lua.parseBlock(code)
Parses a string of Lua code (a block of statements) into an abstract syntax tree (AST).
Parameters:
code: The Lua code to parse
Returns a LuaBlock object representing the parsed code.
Example:
local ast = lua.parseBlock("print('Hello')")
-- ast contains the parsed syntax tree
Note
Note
lua.parseis a deprecated alias forlua.parseBlockand is kept for backwards compatibility.
lua.parseExpression(expression)
Parses a Lua expression into an abstract syntax tree (AST).
Parameters:
expression: The Lua expression to parse
Returns a LuaExpression object representing the parsed expression.
Example:
local expr = lua.parseExpression("1 + 2 * 3")
-- expr contains the parsed expression tree
lua.evalExpression(expression)
Evaluates a Lua expression and returns its result.
Parameters:
expression: The Lua expression to evaluate
Returns the result of evaluating the expression.
Example:
local result = lua.evalExpression("1 + 2 * 3")
print(result) -- prints: 7
lua.prettyPrintBlock(block, options?)
Pretty-prints a parsed Lua block AST (as returned by #lua.parseBlock(code)) back to formatted Lua source.
The optional options table accepts indentWidth (number, default 2), quote ("double" or "single", default "double"), and trailingComma (boolean, default true). Comments are not preserved.
Example:
local formatted = lua.prettyPrintBlock(lua.parseBlock("if a then return 1 end"))
lua.prettyPrintExpression(expression, options?)
Pretty-prints a parsed Lua expression AST (as returned by #lua.parseExpression(expression)) back to formatted Lua source. Accepts the same options table as #lua.prettyPrintBlock(block, options?).
Example:
local formatted = lua.prettyPrintExpression(lua.parseExpression("{a=1,b=2}"))