min.c 800 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * min.c -- a minimal Lua interpreter
  3. * loads stdin only with minimal error handling.
  4. * no interaction, and no standard library, only a "print" function.
  5. */
  6. #include <stdio.h>
  7. #include "lua.h"
  8. #include "lauxlib.h"
  9. static int print(lua_State *L)
  10. {
  11. int n=lua_gettop(L);
  12. int i;
  13. for (i=1; i<=n; i++)
  14. {
  15. if (i>1) printf("\t");
  16. if (lua_isstring(L,i))
  17. printf("%s",lua_tostring(L,i));
  18. else if (lua_isnil(L,i))
  19. printf("%s","nil");
  20. else if (lua_isboolean(L,i))
  21. printf("%s",lua_toboolean(L,i) ? "true" : "false");
  22. else
  23. printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i));
  24. }
  25. printf("\n");
  26. return 0;
  27. }
  28. int main(void)
  29. {
  30. lua_State *L=lua_open();
  31. lua_register(L,"print",print);
  32. if (luaL_dofile(L,NULL)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1));
  33. lua_close(L);
  34. return 0;
  35. }