lua-users home
lua-l archive

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


Hi
Thanks a lot. Referring your links I could do a small test program. However I am not able to get the output of lua function in C code. Here is my example code. Please let me know, how to collect the lua function output in C code.  After I call lua_loadstring, I want to print function output in Code.., please let me know how to do it...





#include <lua5.2/lauxlib.h>
#include <lua5.2/lualib.h>

int main (void) {
    char *data[8] = {
                "function fact (n)",
                "if n==0 then",
                "return 1",
                "else",
                "return n * (fact(n-1))",
                "end",
                "end"   /*,

                "print (\"Factorial: \",fact(4))" */

                };
    char buffer[512];
    int error;
    int index = 0;
    int offset = 0;
    lua_State *L = luaL_newstate();   /* opens Lua */
    luaopen_base(L);             /* opens the basic library */
    luaopen_string(L);           /* opens the string lib. */
    luaopen_math(L);             /* opens the math lib. */


    while(index < 9)
    {
        offset += sprintf((buffer+offset), "%s\n", data[index]);
        index++;
    }

    buffer[offset] = 0;

    //printf("string = %s", buffer);
    luaL_dostring(L, buffer);

    lua_close(L);
    return 0;
}


Thanks
Austin



On Mon, Jun 23, 2014 at 6:59 PM, Choonster TheMage <choonster.2010@gmail.com> wrote:
On 23 June 2014 22:54, Austin Einter <austin.einter@gmail.com> wrote:
 
Dear All
I am quite new to Lua environment.

We have very simple requirement. Before we start using Lua , we need to check if it is the right script language for our requirement.

I have a bunch of code as below

int count = 5
count = count * 2
return count

The above syntax is what I have thought of, I can change syntax if required

My whole project is in C. Now I need to evaluate above block and get the value.

Can we use Lua to determine the value of above block and return the value to C code.

I do not want to have script.lua and execute (same I can do with shell etc) by dofile load.

I have above string, I want to feed this string to Lua (or equivalent of above string that Lua understands) and can Lua evaluate count value and return to C code.

Can somebody guide me if Lua is right choice.
 
Also , at any point of time in same machine I might have 1000s of programs running and each program might be loading Lua Libs etc, will there be any performance constraints..

Thanks
Austin

The simplest way to execute a string as a chunk of Lua code from C is probably luaL_dostring:

If you need more control, you can load the chunk with luaL_loadstring, luaL_loadbuffer, lua_load, etc. before you execute it.

Any values returned from the chunk are left on the stack, as described in lua_call:

As for performance, I can't really help you there.

Regards,
Choonster