lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Here is a utility that inlines some Lua function calls [1].  Example:

  -- input
  local y=-1
  local function square(x) return x*x end
  local function g(x) return x >= 0 and square(square(x))+1 or 1 end
  while (function() y = g(y)^(1/4) return y < 2 end)() do
    print(y)
  end

  -- output:
  local y = - 1
  while 1 do
     local __v13x = y
     local __v10 = 0 <= __v13x
     if __v10 then
        local __v12x = __v13x
        local __v14x = __v12x * __v12x
        __v10 = __v14x * __v14x + 1
     end
     local __v11 = __v10
     if not __v11 then
        __v11 = 1
     end
     y = __v11 ^ (1 / 4)
     if not (y < 2) then
       break
     end
     print (y)
  end

The utility uses Metalua for the AST manipulation.  It is somewhat
rudimentary but could be extended to do many other types of source
code optimizations.

[1] http://lua-users.org/wiki/SourceOptimizer