Standard Library
Built-in Functions
In LuneScript, the following Lua-compatible or specialized built-in functions are available.
| Function Name | Description |
|---|---|
print(...) |
Outputs arguments to the standard output. |
type(val) |
Returns the type name of the value as a string (e.g., "int", "str", "table", "nil"). |
tostring(val) |
Converts the value to a string. |
tonumber(val, base) |
Converts a string to a number. The base (2-36) can be specified with base. Returns
nil on failure. |
error(msg) |
Raises an error. |
assert(cond, msg) |
Raises an error if the condition is false. |
_load(code, name) |
Loads a Lua code string and returns an executable function. name is the chunk name.
|
unwrap val [default] |
Converts a Nilable type to a Non-nilable type. If default is provided, it returns the
default value if the input is nil. |
String Operations (str type methods)
The string type str can use functions from Lua's string library as methods.
The format operator % is also available.
let text = "hello world";
print( text.upper() ); // HELLO WORLD
print( text.sub( 1, 5 ) ); // hello (LuneScript/Lua indexing starts at 1)
print( #text ); // 11 (Get length with #text)
print( text.find( "world", 1, true ) );
let formatted = "Value: %d" ( 123 ); // Format
print( formatted );
Using Lua Standard Libraries
Lua standard libraries (math, os, io, string,
table) are available by making a module declaration.
LuneScript does not have built-in features that directly wrap these, but they can be used by defining them
as follows.
Example of Math Library
module Math require "math" {
pub static let pi: real;
pub static fn sin( x:real ): real;
pub static fn cos( x:real ): real;
}
print( Math.sin( Math.pi ) );
IO (Input/Output)
File operations use Lua's io library. Depending on the environment, you may need a
module declaration, or the built-in io variable may already be available.
// Example of File Reading
if! let mut file = io.open( "test.txt", "r" ) {
if! let txt = file.read( "*a" ) {
print( txt );
}
file.close();
}
Available IO features may vary depending on the transcompilation target environment (Lua, Go).