lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am Fr., 20. Jan. 2023 um 09:17 Uhr schrieb Ziyao <ziyao@disroot.org>:
> [1]: https://github.com/ziyao233/code/blob/main/Lua/Calculator.lua

... but Lua is an interpreter language and has interpreter "on board".
If you can ask your calculater user to use Lua writing style for the
calculator entry, like "4 + 5", "4 - 5", "4*5 ", "4/5" (even "4 ^5"
works), then you can do this MUCH easier with Lua (see figure 28.4 in
Roberto's book "Programming in Lua", "Calling a Lua function from C",
pcCmd would be the user Entry , e. g. "4+5"):

void RunLuaCommand( lua_State* L, char* pcCmd){
  char acTmp[100];
  sprintf( acTmp, "A=%s; print( A)", pcCmd);
  int status= luaL_loadbufferx( L, acTmp, strlen( acTmp), pcCmd, "t");
  if( status== LUA_OK)
    status= lua_pcall( L, 0, 0, 0);
  if( status != LUA_OK){
    const char* pc= lua_string( L, -1);
    printf( "Error: %s", pc);
  }
}

(I did not test it, I hope not too many errors :) ).