Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Type checking tool for Lua scripts  (Read 1643 times)

mifki

  • Bay Watcher
  • works secretly...
    • View Profile
    • mifki
Type checking tool for Lua scripts
« on: June 09, 2016, 09:56:19 pm »

I'm working on a tool to check Lua scripts against DF data structures. I need it to track changes between DF versions and update DF Remote scripts accordingly. Maybe it will be useful for other DFHack scripts too.

https://github.com/mifki/df-luacheck

I've just started yesterday, so a lot of things is missing, including support for require(), internal dfhack functions, and more.

Code: [Select]
function test(unitid)
    local ret = {}

    for i,unit in ipairs(df.global.world.units.active) do
        if unit.civ_id == df.global.ui.civ_id and unit.flags1.tame and not unit.flags1.dead and not unit.flags1.forest then
            local work = (unit.profession == df.profession.TRAINED_WAR or unit.profession == df.profession.TRAINED_HUNT)
            local geld = unit.flags3.geld
           
            -- ...
        end
    end

    return ret
end

test(1)
---------------------------
6: ERROR value TRAINED_HUNT does not exist in enum df.profession
6: ERROR type of operand df.profession.TRAINED_HUNT is unknown, assuming the result is bool
7: ERROR field geld does not exist in unit.flags3 of type df.unit_flags3
7: ERROR type of expression is unknown unit.flags3.geld

Code: [Select]
function test(bldid)
    local bld
   
    if bldid then
        bld = df.building.find(bldid)
    else
        bld = df.global.world.selected_bld
    end   
   
    bld.owner_id = df.global.world.units.active[0]
end

test(1)

---------------------------
7: ERROR field selected_bld does not exist in df.global.world of type df.world
7: ERROR type of expression is unknown df.global.world.selected_bld
10: ERROR assigning df.global.world.units.active[0] of type df.unit to bld.owner_id of type number

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: Type checking tool for Lua scripts
« Reply #1 on: June 10, 2016, 04:49:16 am »

Oh cool. I was thinking that we lack tools for lua.

Have you looked at strict lua. It's one include and takes care of most(?) global nil references. Though its runtime thing not like this nice static checking.

mifki

  • Bay Watcher
  • works secretly...
    • View Profile
    • mifki
Re: Type checking tool for Lua scripts
« Reply #2 on: June 21, 2016, 06:15:37 pm »

Making good progress here. Some language constructions may still not be supported, but it's now able to check DF Remote code and some of the dfhack plugins. For example, here we see one of them needs to be updated for field names changes, and warnings should be fixed, too.

Code: [Select]
ERROR makeown.lua:151 field anon_1 does not exist in hf of type df.historical_figure
ERROR makeown.lua:151 field anon_2 does not exist in unit.relations of type df.unit.T_relations
ERROR makeown.lua:152 field anon_2 does not exist in hf of type df.historical_figure
ERROR makeown.lua:152 field anon_3 does not exist in unit.relations of type df.unit.T_relations
WARN  makeown.lua:87 assignment to global/unknown var event
WARN  makeown.lua:256 assignment to global/unknown var nemesis_link