What exactly should the function return? Since it has a string as an argument should the string just be changed, chopped or whatever?
(BTW thanks for calling EGSL as an important project ... )
What exactly should the function return? Since it has a string as an argument should the string just be changed, chopped or whatever?
(BTW thanks for calling EGSL as an important project ... )
Best regards,
Cybermonkey
Thank you for your answer.
I would like a function that returns a boolean value. The idea of the function is : "Is the string valid". Here is my Lua function : IsFEN.
Last edited by Roland Chastain; 30-10-2014 at 09:21 PM. Reason: Added a link to a Lua script
Ok, just for completely understanding: Do you want to execute a Lua script from your Pascal program and return the boolean value to the Pascal program or do you want to implement that function in Pascal and execute it from a Lua script and return the result to the Lua script?
Best regards,
Cybermonkey
Ok, I see. That's a bit more complicated than the other way but it's possible. I am using the Lua52 headers which can be found on: http://lua-users.org/wiki/LuaInFreePascal
I'll post a simple example where two integers are added and the sum is returned to the Pascal program, it shouldn't be that hard to adapt it to a string/boolean function.
First the Lua script:
That's fairly simple. Now the Pascal program (console only). After compiling one has to specify the Lua script as a parameter on the console.Code:-- add two numbers function add (x,y) return (x+y) end
The result should be 15 of course.Code:program callLua; {$mode objfpc}{$H+} uses sysutils,lua52; var L : plua_state; script:string; sum:integer; function luaadd (x,y:integer):integer ; var sum:integer; begin //* the function name */ lua_getglobal(L, 'add'); //* the first argument */ lua_pushinteger(L, x); //* the second argument */ lua_pushinteger(L, y); //* call the function with 2 arguments, return 1 result */ lua_call(L, 2, 1); //* get the result */ sum := (lua_tointeger(L, -1)); lua_pop(L, 1); result:= sum; end; begin if (ParamCount > 0) then begin script := pChar(ParamStr(1)); L:=luaL_newstate(); luaL_openlibs(L); if (luaL_dofile(L,pChar( script)))<>0 then begin //show if an error occured writeln(lua_tostring(L, -1)); end; sum:=luaadd(12,3 ); lua_close(L); writeln (sum); end; end.
Best regards,
Cybermonkey
Thank you very much Cybermonkey!
Very useful example. By modifying it, I could easily make what I needed.
Here is my own example (including an Lua script).
Last edited by Roland Chastain; 01-11-2014 at 01:55 PM.
Hello Cybermonkey!
Here is a new version of your example, using the LUA53.PAS unit from the Lua4Lazarus package.
https://github.com/malcome/Lua4Lazarus
http://forum.lazarus.freepascal.org/...c,27268.0.html
The Lua script uses a new feature of Lua 5.3 (euclidian division).
Best regards.
Roland
Bookmarks