lua-users home
lua-l archive

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


You can have a look at the lua.c.

----- Original Message ----- 
From: "Edward Mitchell" <emitchell@ieee.org>
To: <lua@bazar2.conectiva.com.br>
Sent: Tuesday, October 17, 2006 4:39 PM
Subject: How to read thru a script file from C a line at a time.


> Is there a way I can pass line by line of a script to Lua.  I've tried the 
> example interpreter in Listing 24.1 of PIL2 but if the syntax element 
> continues over more than one line, then an error is reported.  Lua is 
> supposed to detect when the line it has read does not form a complete chunk 
> and ask for more - p4 of PIL2.  Is there a way I can do this from my own 
> loop to read a line at a time.  My current read thru the script file looks 
> like:
> 
>    ...
>    while(templateFile) {
>        templateFile.getline(buff, BUFSIZ);
>        // ?? debug echo to the console
>        fprintf(stdout, "%s\n", buff);
>        iError = luaL_loadbuffer(L, buff, strlen(buff), "Line")
>            || lua_pcall(L, 0, 0, 0);
>        if(iError) {
>            // echo the error message
>            fprintf(stdout, "%s\n", lua_tostring(L, -1));
>            // quit on any error
>            break;
>        }
>    }
>    lua_close(L);
> 
> Doing it this way the line numbers are incorrect in the error messages as 
> well but that's easy to fix.  But I'd really like to read the file myself, 
> line by line if it's possible.
> 
> Ed
> --
> Edward E.L. Mitchell
> Phone: (239)415-7039
> 6707 Daniel Court
> Fort Myers, FL 33908 
> 
> 
> 
>