一個簡單的Lua解譯器

來源:互聯網
上載者:User

標籤:blog   os   io   ar   art   div   cti   sp   log   

#include "stdafx.h"#include<stdarg.h>#include<stdlib.h>#include<stdio.h>#include<string.h>    #include<math.h>#include<errno.h>extern "C"{#include "lua.h"#include "lauxlib.h"#include "lualib.h"}lua_State *L;void errormethod(const char *fmt, ...){va_list argp;va_start(argp, fmt);vfprintf(stderr, fmt, argp);va_end(argp);lua_close(L);//exit(EXIT_FAILURE);}static int l_sin(lua_State* L){//double d = lua_tonumber(L, 1);double d = luaL_checknumber(L, 1);lua_pushnumber(L, sin(d));//printf("call1 %f\n", d);return 1;} static int add(lua_State* L){int n1 = lua_tonumber(L, 1);int n2 = lua_tonumber(L, 2);int sum = n1 + n2;lua_pushnumber(L, sum);//printf("call2 %f\n", d);  return 1;}   int main(int argc, char* argv[]){char buff[256];int error;L = luaL_newstate();//建立Lua環境luaL_openlibs(L);//載入標準庫lua_pushcfunction(L, l_sin);lua_setglobal(L, "mysin");lua_pushcfunction(L, add);lua_setglobal(L, "add");while(fgets(buff, sizeof(buff), stdin) != NULL){error = luaL_loadbuffer(L, buff, strlen(buff), "line") || lua_pcall(L, 0, 0, 0);if(error){  //errormethod("%s\n", lua_tostring(L, -1));fprintf(stderr, "%s\n", lua_tostring(L, -1));lua_pop(L, -1);}  }//lua_close(L);system("pause");return 0;}

From Lua程式設計..

給我的認識是

Lua僅僅是一門嵌入式的指令碼語言

需要宿主語言去啟動它。

 

一個簡單的Lua解譯器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.