- Sudah familier dengan codeblocks.
- Tahu cara melinking library di codeblocks.
Yang akan kita buat adalah sebuah aplikasi interpreter LUA sederhana. Step-step nya adalah sebagai berikut:
1. Download library LUA.
Download dari sini saja, atau bisa cari di devpaks.org bagian scripting.2. Buat program console biasa dengan codeblocks.
3. Linking library dari poin no 1 kedalam project.
4. Hapus isi main.cpp dan copy-paste kode dibawah ini.
extern "C"
{
#include <lualib.h>
#include <lauxlib.h>
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
lua_State *L = luaL_newstate(); // opens Lua
luaL_openlibs(L); // opens the standard libraries
while (1)
{
char temp[64];
cout << "input: ";
std::cin.getline(temp, 64);
string input(temp);
if( input == "exit" ) break;
int error = 0;
error = luaL_loadbuffer(L, input.c_str(), input.length(), "line");
if( error != 0 )
{
cout << "luaL_loadbuffer error" << endl;
}
error = lua_pcall(L, 0, 0, 0);
if( error != 0 )
{
cout << "lua_pcall error" << endl;
}
if (error)
{
string out(lua_tostring(L, -1));
cout << out << endl;
// pop error message from the stack
lua_pop(L, 1);
}
}
lua_close(L);
return 0;
}
5. Kompile, dan run.
Seharusnya tidak ada masalah ketika kompile. Ketika run masukan input:print('Hello World!');Code barusan adalah codenya LUA, yang akan diproses oleh aplikasi C++. Output yang dihasilkan adalah:Hello World!
Tidak ada komentar:
Posting Komentar