lua-users home
lua-l archive

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


David Manura wrote:
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

Ugh, I love the concept and in fact want to add a similar sort of functionality built into my own dialect of Lua, but looking at that code, there's some un-optimized stuff IMO (unless I'm wrong):

if __v10 then
   local __v12x = __v13x
   local __v14x = __v12x * __v12x
   __v10 = __v14x * __v14x + 1
end


Becomes:

if __v10 then

   __v10 = __v13x^4 + 1

end