Skip to content

Instantly share code, notes, and snippets.

@marcmo
Created April 4, 2011 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcmo/901479 to your computer and use it in GitHub Desktop.
Save marcmo/901479 to your computer and use it in GitHub Desktop.
support for interpretation of a lua file
module LuaIntegration where
import qualified Scripting.Lua as Lua
lua_noerrors = 0
lua_yield = 1
lua_errrun = 2
lua_errsyntax = 3
lua_errmem = 4
dofile :: Lua.LuaState -> String -> IO Int
dofile s name = do
res <- Lua.loadfile s name
if (res == lua_noerrors)
then print $ "loaded file correctly:" ++ name
else reportError $ "error while loading file: " ++ name
let handlePcall x
| x == lua_noerrors = print $ "executed file correctly:" ++ name
| x == lua_errrun = reportError "run-error"
| x == lua_errsyntax = reportError "syntax-error"
| x == lua_errmem = reportError "memory-allocation-error"
| otherwise = reportError ("unknown error(code " ++ show x ++ ") when executing file:" ++ name)
Lua.pcall s 0 0 0 >>= handlePcall
return res
where reportError desc = do
stackDump s
err <- Lua.tostring s (-1)
Lua.pop s 1 -- remove error message
error $ desc ++ " - " ++ err
stackDump :: Lua.LuaState -> IO ()
stackDump s = do
print "stackdump:"
top <- Lua.gettop s
doLevel 1 top
where doLevel n top
| n == top = print "end"
| otherwise = do
t <- Lua.ltype s n
case t of
Lua.TSTRING -> Lua.tostring s n >>= showLevel n
Lua.TBOOLEAN -> Lua.toboolean s n >>= showLevel n
Lua.TNUMBER -> Lua.tonumber s n >>= showLevel n
_ -> Lua.typename s t >>= showLevel n
showLevel n xs = do
let ss = concat $ replicate n "--->"
print $ ss ++ "level " ++ show n ++ ", " ++ show xs
mkdir -p tmp
ghc -i.. -threaded simplelua.hs -o tmp/testThreadedLua -outputdir tmp
[1 of 2] Compiling LuaIntegration ( LuaIntegration.hs, tmp/LuaIntegration.o )
[2 of 2] Compiling Main ( simplelua.hs, tmp/Main.o )
Linking tmp/testThreadedLua ...
/usr/local/lib/hslua-0.3/ghc-7.0.3/libHShslua-0.3.a(loslib.o): In function `os_tmpname':
loslib.c:(.text+0x35): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
./tmp/testThreadedLua
Hello from Lua
"stackdump:"
"end"
testThreadedLua: error while loading file: script_sample.lua - [string "script_sample.lua"]:4: unexpected symbol near '�'
mkdir -p tmp
ghc -i.. simplelua.hs -o tmp/test -outputdir tmp
[1 of 2] Compiling LuaIntegration ( LuaIntegration.hs, tmp/LuaIntegration.o )
[2 of 2] Compiling Main ( simplelua.hs, tmp/Main.o )
Linking tmp/test ...
/usr/local/lib/hslua-0.3/ghc-7.0.3/libHShslua-0.3.a(loslib.o): In function `os_tmpname':
loslib.c:(.text+0x35): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
./tmp/test
Hello from Lua
"loaded file correctly:script_sample.lua"
hello from lua-script
"executed file correctly:script_sample.lua"
import qualified Scripting.Lua as Lua
import LuaIntegration
main = do
l <- Lua.newstate
Lua.openlibs l
Lua.callproc l "print" "Hello from Lua"
dofile l "script_sample.lua"
Lua.close l
print("hello from lua-script")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment