03. Cross-compiling between Lua versions
This time I will change the schedule a little and explain LuneScript cross-compilation.
cross compilation
"Cross-compiling" here means "cross-compiling" between versions of Lua.
When LuneScript transcompiles to Lua, it will transcompile according to the Lua version running LuneScript.
For example, the following controls are performed.
- When running on Lua5.2, bit operations use the bit32 library.
- When running in Lua5.3, bitwise operations make use of Lua's built-in operators.
Cross-compiling will transcompile the emitted Lua code to the specified Lua version instead of the Lua version running LuneScript.
This allows you to do things like:
"Run LuneScript with Lua5.3 and transcompile to Lua5.1"
how to use
To use it, just specify the version with the -ol option as follows:
$ lnsc src.lns lua -ol 51
The above example will transcompile src.lns for lua5.1 and print the result to stdout.
The values to be specified for the -ol option are as follows.
option | version | |
---|---|---|
51 | Lua5.1 | |
52 | Lua5.2 | |
53 | Lua5.3 |
The -ol option can be specified with save and lua.
Lua5.1 support for LuneScript
Until now, LuneScript required Lua5.2 and Lua5.3 to run, but with the above cross-compile support, Lua5.1 can now run.
Since LuneScript itself is self-hosting developed with LuneScript, Lua5.1 is supported by cross-compiling LuneScript itself.
Developing with LuneScript has the advantage of being able to "absorb Lua version differences to some extent" in addition to "increasing productivity".
However, it can only be absorbed to a certain extent.
Lua5.1 has limitations that are explained in the next section.
Limitations of Lua5.1
Transpiling LuneScript code to Lua5.1 has the following limitations:
It's more of a Lua5.1 limitation than a LuneScript limitation. .
- Class destructor not available.
- Bit operations cannot be used.
- %g is not available for character classes such as
find()
. - If you specify %s, %q in the
string.format()
format, you cannot specify anything other than a character string.
In the above, regarding destructors and bit operations, an error will be output during compilation, but regarding %g, %s, %q, an error will not be output during compilation, so be careful.
However, regarding %s and %q, the following pattern avoids errors by adding tostring()
during transcompiling.
// @lnsFront: ok
print( string.format( "%d,%s,%s,%s", 1, 1.0, {}, [] ) );
As above, the format format using a literal string looks at the %s and the corresponding data type, and appends tostring()
as follows if it's not a str.
print( string.format( "%d,%s,%s,%s", 1, tostring( 1.0), tostring( {}), tostring( {}) ) )
This conversion is only possible when using literal strings.
For example, the following cannot be converted.
// @lnsFront: ok
let formatTxt = "%d,%s,%s,%s";
print( string.format( formatTxt, 1, 1.0, {}, [] ) );
summary
LuneScript supports cross-compilation between versions of Lua.
This supports running on Lua5.1, Lua5.2 and Lua5.3.
However, Lua5.1 has limitations.
Next time, I will explain the class.