GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||
GeezerVison is showing moonscript.org — MoonScript Compiler API MoonScript 0.8.0 - Compiler API
|
MoonScript
|
This module contains an assortment of functions for loading and compiling MoonScript code from within Lua.
The module provides load, loadfile, loadstring functions, which are
analogous to the similarly named Lua functions. The major difference is that
they load MoonScript code instead of Lua code.
MoonScript
|
All of these functions can take an optional last argument, a table of options:
implicitly_return_root — Setting this to false makes it so the file
does not implicitly return its last statement.correlate — Setting this to true generates Lua code where the line
numbers match the line numbers of the MoonScript source as closely as
possible, the same as the --correlate flag of moonc.MoonScript
|
One more useful function is provided: to_lua. This function takes a string of
MoonScript code and returns the compiled Lua result along with the line mapping
table. If there are any errors then nil and the error message are returned.
MoonScript
|
Similar to the load* functions from above, to_lua can take an optional
final argument of a table of options.
The second return value of to_lua is useful if you want to perform line
number reversal. It’s a table where the key is a Lua line number and the value
is a character offset from the original MoonScript source.
If you need finer grained control over the compilation process you can use the raw parse and compile modules.
Parsing converts a string of MoonScript into an abstract syntax tree. Compiling converts an abstract syntax tree into a Lua code string.
Knowledge of this API may be useful for creating tools to aid the generation of Lua code from MoonScript code. For example, you could build a macro system by analyzing and manipulating the abstract syntax tree. Be warned though, the format of the abstract syntax tree is undocumented and may change in the future.
Here is a quick example of how you would compile a MoonScript string to a Lua
String (This is effectively the same as the to_lua function described above):
MoonScript
|
On a parse failure parse.string returns nil and a multi-line error string
of the form <message> at line <n>, column <m>: followed by the offending
source line and a ^ position marker.