PowerBLua is a little contribution of mine to the
PowerBASIC community.
It's a wrapper for Lua, a powerful light-weight
programming language designed for extending applications.
At the moment, only a subset of API is implemented, but thankful to the Lua
flexibility, is already more than enough to do something useful with the language.
' ===================================================================
' This sample show how to register/export PowerBASIC functions to Lua
' ===================================================================
' Include the necessary declarations
#INCLUDE "PowerBLua.INC"
' This is the function exported to Lua
FUNCTION MinMax CDECL (BYVAL pLuaHandle AS DWORD) AS DWORD
DIM NumPar AS LONG
DIM i AS LONG
DIM Num AS DOUBLE
DIM MaxNum AS DOUBLE
DIM MinNum AS DOUBLE
' Get the number of parameters passed on the Virtual Stack
' by the calling Lua code
NumPar = Lua_GetTop(pLuaHandle)
' Pop all parameters from the VS and evaluate them
FOR i = 1 TO NumPar
' Peek the specified VS element
Num = Lua_ToNumber(pLuaHandle, i)
MaxNum = MAX(Num, MaxNum)
IF MinNum = 0 THEN
MinNum = Num
ELSE
MinNum = MIN(Num, MinNum)
END IF
NEXT i
' Push the results on the VS
Lua_PushNumber(pLuaHandle, MinNum)
Lua_PushNumber(pLuaHandle, MaxNum)
' Set number of returned parameters
FUNCTION = 2
END FUNCTION
FUNCTION PBMAIN
DIM LuaHandle AS DWORD
' Create a Lua state
LuaHandle = Lua_Open()
' Load the base Lua library (needed for 'print')
LuaOpen_Base(LuaHandle)
' Register the function to be called from Lua as PBMinMax
PBLua_Register(LuaHandle, "PBMinMax", CODEPTR(MinMax))
' Execute the Lua script from a file
PBLua_DoFile(LuaHandle, "minmax.lua")
' Release the Lua state
Lua_Close(LuaHandle)
PRINT "PB : Finished! Press any key"
WAITKEY$
END FUNCTION
Lua source: minmax.lua
-- Show how to call a function registered in the embedding application
-- PBMinMax is a PowerBASIC function
Min, Max = PBMinMax(42, 2, 17, 33, 15, 1.5)
print("Lua: Min= ", Min, ", Max= ", Max, "\n")