Skip to content

Latest commit

 

History

History
120 lines (110 loc) · 7.99 KB

lua.md

File metadata and controls

120 lines (110 loc) · 7.99 KB

Qhun Transpiler -> LUA target

This transpiler target is called lua and can be used to transpile Typescript into LUA. The emitted lua output is compatible with LUA version 5.1. I am sure that newer LUA version are supported as well, but i havn't tested it.

What is supported?

What you should keep in mind when writing typescript code:

  1. Lua does not know about the difference between the let and const statement. Same as javascript ES5. These are just hints at compilertime and will be lost at runtime.
  2. The keywords null and undefined will both be transpiled to nil. So a test null === undefined will transpiled into a truethy expression: nil == nil!
  3. Use the array destructing pattern for achiving multireturn like results. See this example:
// myTypescriptFile.ts
const [a, b, c, , e] = myMultiReturnFunction()

Will be transpiled to:

-- theGeneratedLuaFile.lua
local a, b, c, _, e = myMultiReturnFunction()
  1. To support multireturn the tuple or array literal is used to achive this. When returning an array literal, the transpiler will use a multireturn. If a variable is returned, no matter the type, it will not be a multireturn. Example:
// this function will return multiple values
function myFunc(): [number, string] {
    return [1, "test"]
}
// this function will NOT return multiple values
function myOtherFunc(): [number, string] {
    const a = [1, "test"];
    return a;
}

Will be transpiled to:

-- this function will return multiple values
local function myFunc()
    return 1, "test"
end
-- this function will NOT return multiple values
local function myOtherFunc()
    local a = {1, "test"}
    return a
end
  1. Lua has no type reference to variables. But there is some kind of static reflection added at compiling time. Currently the following reflections are available via Bitop flag in the qhun-transpiler.json config entry staticReflection:
Number Result
0 No reflection at all
1 Class constructor signature analysis is written to constructor.__staticReflection
2 Class name is written to constructor.__name
4 Class namespace or folder path is written to constructor.__namespace

Usage: Sum up the required reflection options and put the result in the json file. E.g: 1 (constructor signature) + 4 (class namespace) = 5 The number 5 will enable both selected features. In this example no class name is written into the transpiled file.

Config block

Each target has a config block section in the qhun-transpiler.json file. So LUA does does. This block is currently empty and is reversed for future releases to configure the transpiling process.