lua_load hook (was On the subject of Lua's grammar... --[[ ]])

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

lua_load hook (was On the subject of Lua's grammar... --[[ ]])

André de Leiradella
Hi All,

luareader (http://www.geocities.com/andre_leiradella/#luareader) uses the
lua_load hook to load chunks of data from a stream and pre-process the lua
source code to change statements on the "@id", "@id(...)" and "@@id(...)"
forms into something else, making it easier (at least for me) to write OOP
code with lua (with the help of some metatables also):

Button = class(Control)

function Button:new(x, y, width, height, caption)
    @@new(x, y, width, height) -- translates to inherited(self, "new", x, y,
width, height)
    @caption = caption -- translates to self.caption = caption
end

If someone is willing to write a C-like preprocessor for Lua maybe luareader
can be helpfull.

By the way I have a newer version of lua_reader which is able to read source
and pre-compiled lua files from files or memory. The bytes, be in a file or
memory, can be gzipped or bzipped and can be grouped into a tar file that
can be automatically iterated to execute Lua code. It comes with a different
version of bin2c:

usage: bin2c [-help] [-?] [-quiet] [-input input_file] [-output output_file]
          [-name lua_file_name] [-function function_name]
          [-trace trace_function_name] [-error error_function_name]
          [-compressed]* [-bunzip2]* [-gunzip]* [-tar iterator_function]*
          [-classpp field_format method_format inherited_method_format]*

which automatically generates C code that calls luareader to execute Lua
code from memory in tar, tar.bz2, tar.gz, or plain code, pre-compiled or
not, with the option of pre-processing it with the OOP module.

If anyone is interested in luareader please let me know, I can release it
with the same license Lua has in one or two weeks.

Regards,

Andre de Leiradella
http://www.geocities.com/andre_leiradella/

--------------- Mensagem Original ---------------
Message: 15
Date: Thu, 27 Nov 2003 15:12:03 -0200
From: Luiz Henrique de Figueiredo <[hidden email]>
Subject: Re: Re[2]: On the subject of Lua's grammar... --[[ ]]
To: [hidden email]
Message-ID: <200311271712.PAA04195@...>

>RH> How about Lua sending the text through some preprocessor first?
>
>I think some sort of hook function might be neat, that is called
>before the lua code is actually compiled, and may alter the source
>code. With such a thing, C style #defines could be implemented, but
>also more complex things like a scheme-style hygienic macro
>preprocessor. I have no need for that currently, but it would be cool
>to have. Might pose some interesting problems, though...

A primitive form of hook already exists because lua_load, which is the
*only*
way to compile Lua code, calls a user function to get blocks of data. In
this
function, you can do as you please. I'm not saying that it's easy, I'm only
saying that the mechanism is already in place.
--lhf


Reply | Threaded
Open this post in threaded view
|

Re: lua_load hook (was On the subject of Lua's grammar... --[[ ]])

Luiz Henrique de Figueiredo
>If someone is willing to write a C-like preprocessor for Lua maybe luareader
>can be helpfull.

Not exactly this, but here is the old Lua preprocessor implemented in Lua...
--lhf

-- implementation of Lua 3.x preprocessor

local function abort(name,line,why)
 error(name..":"..line..": "..why.."\n",0)
end

local function eval(s,name,line)
 local f,e=loadstring("return "..s,"=$if")
 if f==nil then abort(name,line,e) end
 return f()
end

local function scan(IN,OUT,name,ln,fp,pp,p)
 local e=nil				-- no `else' seen
 p=pp and p				-- ensure consistency of print flags
 while 1 do
  local s=IN:read()
  if s==nil then
   if fp then return else abort(name,ln,"unexpected end of file") end
  end
  ln=ln+1
  local i,j,c=string.find(s,"%$%s*(%w+)")
  if not i then				-- ordinary line
   if not p then s="" end		-- preserve line numbers
   OUT:write(s,"\n")
  else					-- preprocessor line
   OUT:write("\n")			-- preserve line numbers
   local t=string.sub(s,j+1,-1)
   if c=="debug" then			-- do nothing
   elseif c=="nodebug" then		-- do nothing
   elseif c=="endinput" then return 0
   elseif c=="if" then
    local v=eval(t,name,ln)
    ln=scan(IN,OUT,name,ln,nil,p,v)
    if ln==0 then return 0 end
   elseif c=="ifnot" then
    local v=eval(t,name,ln)
    ln=scan(IN,OUT,name,ln,nil,p,not v)
    if ln==0 then return 0 end
   elseif c=="end" then
    if fp then
     abort(name,ln,"unmatched `endif'")
    else
     return ln
    end
   elseif c=="else" then
    if fp then
     abort(name,ln,"unmatched `else'")
    elseif e then
     abort(name,ln,"extraneous `else'")
    else
     p=pp and not p			-- invert print permission
     e=1				-- `else' seen
    end
   else
    abort(name,ln,"unknown pragma `"..c.."'")
   end
  end
 end
end

function pploadfile(s)
 local IN,e=io.open(s,"r")	assert(IN,e)
 local t=os.tmpname()		assert(t)
 local OUT,e=io.open(t,"w")	assert(OUT,e)
 scan(IN,OUT,s,0,1,1,1)
 io.close(OUT)
 local a,b=loadfile(t,s)
 os.remove(t)
 if a==nil then
  a=string.gsub(b,".-:([0-9]+): ",function (x) b=x return "" end)
  a=string.gsub(a,"\n","")
  abort(s,b,a)
 end
 return a,b
end

-- an example
-- pploadfile("i")()

Reply | Threaded
Open this post in threaded view
|

Re: lua_load hook (was On the subject of Lua's grammar... --[[ ]])

Roberto Ierusalimschy
In reply to this post by André de Leiradella
BTW, Lua 5.1 removes the restriction that you cannot call Lua from a
reader. In particular it provides the "load" function in Lua, so it will
be possible to write such hooks in Lua itself.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: lua_load hook (was On the subject of Lua's grammar... --[[ ]])

Daniel Silverstone
On Fri, 2003-11-28 at 13:19, Roberto Ierusalimschy wrote:
> BTW, Lua 5.1 removes the restriction that you cannot call Lua from a
> reader. In particular it provides the "load" function in Lua, so it will
> be possible to write such hooks in Lua itself.

And here I was, sitting and thinking "It'd be so much cooler if...." and
you go and tell me it'll be done in 5.1

You guys are excellent.

D.

-- 
Daniel Silverstone                       http://www.digital-scurf.org/
Hostmaster, Webmaster, and Chief Code Wibbler: Digital-Scurf Unlimited
GPG Public key available from keyring.debian.org       KeyId: 20687895



Reply | Threaded
Open this post in threaded view
|

Re: lua_load hook (was On the subject of Lua's grammar... --[[ ]])

Adam D. Moss
In reply to this post by Roberto Ierusalimschy
Roberto Ierusalimschy wrote:
BTW, Lua 5.1 removes the restriction that you cannot call Lua from a
reader. In particular it provides the "load" function in Lua, so it will
be possible to write such hooks in Lua itself.

Wow!  That's really cool.  These are all nice goodies (though I
still hunger most for the incremental GC! :)).  Really looking
forward to 5.1!

--Adam
--
Adam D. Moss   . ,,^^   [hidden email]   http://www.foxbox.org/   co:3
Consume Less, Live More

Reply | Threaded
Open this post in threaded view
|

Re: lua_load hook (was On the subject of Lua's grammar... --[[ ]])

André de Leiradella
In reply to this post by André de Leiradella
Hey, I can't wait to make a Lua luareader! It'll make it much easier to
write pre-processors to filter Lua source code, as lhf already showed in
another message!

Andre de Leiradella
http://www.geocities.com/andre_leiradella/

--------------- Mensagem Original ---------------
Message: 8
Date: Fri, 28 Nov 2003 11:19:21 -0200
From: Roberto Ierusalimschy <[hidden email]>
Subject: Re: lua_load hook (was On the subject of Lua's grammar...
        --[[ ]])
To: Lua list <[hidden email]>
Message-ID: <200311281319.hASDJLh32760@...>
Content-Type: text/plain; charset="us-ascii"

BTW, Lua 5.1 removes the restriction that you cannot call Lua from a
reader. In particular it provides the "load" function in Lua, so it will
be possible to write such hooks in Lua itself.

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re[2]: lua_load hook (was On the subject of Lua's grammar... --[[ ]])

Gunnar Zötl
In reply to this post by Roberto Ierusalimschy
RI> BTW, Lua 5.1 removes the restriction that you cannot call Lua from a
RI> reader. In particular it provides the "load" function in Lua, so it will
RI> be possible to write such hooks in Lua itself.

great news :-)

Gunnar