解析Lua解釋器運(yùn)行方法學(xué)習(xí)教程
Lua解釋器運(yùn)行方法學(xué)習(xí)教程是本文要介紹的內(nèi)容,主要是來了解LUA解釋器的使用方法,關(guān)于解釋器如何來運(yùn)行,那么一起來看內(nèi)容詳解。
1、直接運(yùn)行l(wèi)ua.exe,輸入lua語句,比如print("hello world")
2、將print("hello world")寫進(jìn)一個(gè)名為hello.lua的文件里,假設(shè)放在D盤,然后可以打開lua.exe后運(yùn)行dofile("d:/hello.lua")--(之前寫成了DoFile,事實(shí)證明得小寫的dofile),lua解釋器就會(huì)執(zhí)行這個(gè)文件內(nèi)的指令。
3、使用命令提示符,即直接運(yùn)行cmd
- D:\LUA>lua hello.lua
- hello world
- D:\>lua
- > print("hello")
- hello
- D:\>lua d:/lua/hello.lua
- hello world
簡易解釋器代碼(C++)
- #include <stdio.h>
- #include <string.h>
- #include "lua.hpp"
- #include "luaconf.h"
- int main (void)
- {
- char buff[256];
- int error;
- lua_State *L = lua_open();
- luaopen_base(L);
- luaopen_table(L);
- luaopen_string(L);
- luaopen_math(L);
- 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);
- }
- }
- lua_close(L);
- return 0;
- }
小結(jié):解析Lua解釋器運(yùn)行方法學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!