Relua.Script uses the Visitor pattern to make AST transformations easy. Each node type (see Types section below) can have its own global function with the same name as the type. If such a function exists when the script is loaded, it will be used by the enter(node)
builtin function.
The first function that is ran is always the Block
visitor (because parsing a file returns a Block
). A simple implementation might look like this:
function Block(node)
node.Statements:iter(function(i, stat)
enter(stat)
end)
end
An implementation like this will remove all global top level assignments, alongside renaming all function calls to print
.
function FunctionCall(node)
node.Function = astexpr("print")
end
function Block(node)
node.Statements:reverse_iter(function(i, stat)
if (asttype(stat) == "Assignment") then
node.Statements:remove(i)
else
enter(stat)
end
end)
end
Runs the appropriate type function based on the type of node
.
Returns a new AST node of type nodetype
. See Types section below.
Note that lists cannot be instantiated.
Parses the string in expr
and returns an expression node.
Parses the string in stat
and returns a statement node.
Parses the file at path
and returns its Block
node.
Returns the type name of node
.
Note: lists are 1-indexed.
Available Properties and Functions:
list.count
- getter (
list[idx]
) - setter (
list[idx] = val
) list:add(elem)
list:insert(idx, elem)
list:remove(idx)
list:clear()
list:iter(function(index, entry) ... end)
list:reverse_iter(function(index, entry) ... end)
Name
(string)
No fields.
No fields.
Value
(bool)
Type
(string)Negate
(not
)Invert
(-
)Length
(#
)
Expression
(expression node)
Type
(string)Add
(+
)Subtract
(-
)Multiply
(*
)Divide
(/
)Power
(^
)Modulo
(%
)Concat
(..
)GreaterThan
(>
)GreaterOrEqual
(>=
)LessThan
(<
)LessOrEqual
(<=
)Equal
(==
)NotEqual
(~=
)And
(and
)Or
(or
)
Left
(expression node)Right
(expression node)
Value
(string)
Value
(number)
Value
(number)
Table
(expression node)Index
(expression node)
Function
(expression node)Arguments
(list of expression nodes)ForceTruncateReturnValues
(bool)
Entries
(list ofTableConstructorEntry
nodes)
Key
(expression node)Value
(expression node)ExplicitKey
(bool)
No fields.
Expressions
(list of expression nodes)
Statements
(list of statement nodes)TopLevel
(bool)
Condition
(expression node)Block
(Block
node)
MainIf
(ConditionalBlock
node)ElseIfs
(list ofConditionalBlock
nodes)Else
(Block
node)
Condition
(expression node)Block
(Block
node)
Condition
(expression node)Block
(Block
node)
ArgumentNames
(list of strings)Block
(Block
node)AcceptsVarargs
(bool)ImplicitSelf
(bool)
IsLocal
(bool)Targets
(list of assignable nodes)Values
(list of expression nodes)
VariableName
(string)StartPoint
(expression node)EndPoint
(expression node)Step
(expression node)
VariableNames
(list of strings)Iterator
(expression node)