#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) {
char buff[256];
int error;
lua_State *L = lua_open(); /* opens Lua */
luaopen_base(L); /* opens the basic library */
luaopen_table(L); /* opens the table library */
luaopen_io(L); /* opens the I/O library */
luaopen_string(L); /* opens the string lib. */
luaopen_math(L); /* opens the math lib. */
while (fgets(buff, sizeof(buff), stdin) != NULL) {
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
}
lua_close(L);
return 0;
}
luaL_loadbuffer
最新推荐文章于 2026-04-04 02:42:52 发布
本文介绍了一个简单的Lua脚本解释器实现过程。该解释器使用C语言编写,集成了Lua的基本库、表格库、输入输出库、字符串库、数学库等。通过不断读取标准输入的数据并执行Lua代码,能够即时反馈错误信息。
1万+

被折叠的 条评论
为什么被折叠?



