static int loadline(Lua.lua_State L) { int status; Lua.lua_settop(L, 0); if (pushline(L, 1) == 0) { return(-1); /* no input */ } for (; ;) { /* repeat until gets a complete line */ Lua.CharPtr line = Lua.lua_tostring(L, 1); Lua.WriteLog(line.ToString()); status = Lua.luaL_loadbuffer(L, Lua.lua_tostring(L, 1), Lua.lua_strlen(L, 1), "=stdin"); if (incomplete(L, status) == 0) { break; /* cannot try to add lines? */ } if (pushline(L, 0) == 0) /* no more input? */ { return(-1); } Lua.lua_pushliteral(L, "\n"); /* add a new line... */ Lua.lua_insert(L, -2); /* ...between the two lines */ Lua.lua_concat(L, 3); /* join them */ } Lua.lua_saveline(L, 1); Lua.lua_remove(L, 1); /* remove line */ return(status); }
static void dotty(Lua.lua_State L) { int status; Lua.CharPtr oldprogname = progname; progname = null; while ((status = loadline(L)) != -1) { if (status == 0) { status = docall(L, 0, 0); } report(L, status); if (status == 0 && Lua.lua_gettop(L) > 0) { /* any result to print? */ Lua.lua_getglobal(L, "print"); Lua.lua_insert(L, 1); if (Lua.lua_pcall(L, Lua.lua_gettop(L) - 1, 0, 0) != 0) { l_message(progname, Lua.lua_pushfstring(L, "error calling " + Lua.LUA_QL("print").ToString() + " (%s)", Lua.lua_tostring(L, -1))); } } } Lua.lua_settop(L, 0); /* clear stack */ Lua.fputs("\n", Lua.stdout); Lua.fflush(Lua.stdout); progname = oldprogname; }
static int pushline(Lua.lua_State L, int firstline) { Lua.CharPtr buffer = new char[Lua.LUA_MAXINPUT]; Lua.CharPtr b = new Lua.CharPtr(buffer); int l; Lua.CharPtr prmt = get_prompt(L, firstline); if (!Lua.lua_readline(L, b, prmt)) { return(0); /* no input */ } l = Lua.strlen(b); if (l > 0 && b[l - 1] == '\n') /* line ends with newline? */ { b[l - 1] = '\0'; /* remove it */ } if ((firstline != 0) && (b[0] == '=')) /* first line starts with `=' ? */ { Lua.lua_pushfstring(L, "return %s", b + 1); /* change it to `return' */ } else { Lua.lua_pushstring(L, b); } Lua.lua_freeline(L, b); return(1); }
//#define S(x) (int)(x),SS(x) private static void PrintHeader(Lua.Proto f) { Lua.CharPtr s = f.source != null?Lua.getstr(f.source) : "=?"; if (s[0] == '@' || s[0] == '=') { s = s.next(); } else if (s[0] == Lua.LUA_SIGNATURE[0]) { s = "(bstring)"; } else { s = "(string)"; } Lua.printf("\n%s <%s:%d,%d> (%d Instruction%s at %p)\n", (f.linedefined == 0)?"main":"function", s, f.linedefined, f.lastlinedefined, (int)(f.sizecode), SS(f.sizecode), VOID(f)); Lua.printf("%d%s param%s, %d slot%s, %d upvalue%s, ", (int)(f.numparams), (f.is_vararg != 0) ? "+" : "", SS(f.numparams), (int)(f.maxstacksize), SS(f.maxstacksize), (int)f.sizeupvalues, SS(f.sizeupvalues)); Lua.printf("%d local%s, %d constant%s, %d function%s\n", (int)(f.sizelocvars), SS(f.sizelocvars), (int)f.sizek, SS(f.sizek), (int)f.sizep, SS(f.sizep)); }
public static void Main(string[] args) { //Console.WriteLine("Hello World!"); // TODO: Implement Functionality Here //Console.WriteLine("atof() = " + KopiLua.Lua.atof("12.34")); #if !TEST_LUA Lua.CharPtr[] argv; argv = new Lua.CharPtr[args.Length + 1]; argv[0] = new Lua.CharPtr("lua.exe"); for (int i = 0; i < args.Length; ++i) { argv[i + 1] = args[i]; } Lua.main(argv.Length, argv); #else Lua.CharPtr[] argv; argv = new Lua.CharPtr[] { "lua.exe", "array.lua" }; //ok // argv = new Lua.CharPtr[] {"lua.exe", "loop.lua"}; //ok // argv = new Lua.CharPtr[] {"lua.exe", "save.lua"}; //ok // argv = new Lua.CharPtr[] {"lua.exe", "sort.lua"}; //ok // argv = new Lua.CharPtr[] {"lua.exe", "split.lua"}; //ok // argv = new Lua.CharPtr[] {"lua.exe", "teste.lua"}; //ok // argv = new Lua.CharPtr[] {"lua.exe", "type.lua"}; //ok Lua.main(argv.Length, argv); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); #endif }
static void l_message(Lua.CharPtr pname, Lua.CharPtr msg) { if (pname != null) { Lua.fprintf(Lua.stderr, "%s: ", pname); } Lua.fprintf(Lua.stderr, "%s\n", msg); Lua.fflush(Lua.stderr); }
static int dostring(Lua.lua_State L, Lua.CharPtr s, Lua.CharPtr name) { int status = Lua.luaL_loadbuffer(L, s, (uint)Lua.strlen(s), name); if (status == Lua.LUA_OK) { status = docall(L, 0, 1); } return(report(L, status)); }
static int dofile(Lua.lua_State L, Lua.CharPtr name) { int status = Lua.luaL_loadfile(L, name); if (status == Lua.LUA_OK) { status = docall(L, 0, 1); } return(report(L, status)); }
static int handle_luainit(Lua.lua_State L) { Lua.CharPtr init = Lua.getenv(Lua.LUA_INIT); if (init == null) { return(0); /* status OK */ } if (init[0] == '@') { return(dofile(L, init + 1)); } return(dostring(L, init, "=" + Lua.LUA_INIT)); }
private static int marklen = mark.chars.Length - 1; //FIXME:??? static int incomplete(Lua.lua_State L, int status) { if (status == Lua.LUA_ERRSYNTAX) { uint lmsg; Lua.CharPtr msg = Lua.lua_tolstring(L, -1, out lmsg); if (lmsg >= marklen && Lua.strcmp(msg + lmsg - marklen, mark) == 0) { Lua.lua_pop(L, 1); return(1); } } return(0); /* else... */ }
static int report(Lua.lua_State L, int status) { if ((status != 0) && !Lua.lua_isnil(L, -1)) { Lua.CharPtr msg = Lua.lua_tostring(L, -1); if (msg == null) { msg = "(error object is not a string)"; } l_message(progname, msg); Lua.lua_pop(L, 1); } return(status); }
static int incomplete(Lua.lua_State L, int status) { if (status == Lua.LUA_ERRSYNTAX) { uint lmsg; Lua.CharPtr msg = Lua.lua_tolstring(L, -1, out lmsg); Lua.CharPtr tp = msg + lmsg - (Lua.strlen(Lua.LUA_QL("<eof>"))); if (Lua.strstr(msg, Lua.LUA_QL("<eof>")) == tp) { Lua.lua_pop(L, 1); return(1); } } return(0); /* else... */ }
static int traceback(Lua.lua_State L) { Lua.CharPtr msg = Lua.lua_tostring(L, 1); if (msg != null) { Lua.luaL_traceback(L, L, msg, 1); } else if (!Lua.lua_isnoneornil(L, 1)) /* is there an error object? */ { if (Lua.luaL_callmeta(L, 1, "__tostring") == 0) /* try its 'tostring' metamethod */ { Lua.lua_pushliteral(L, "(no error message)"); } } return(1); }
static int report(Lua.lua_State L, int status) { if ((status != Lua.LUA_OK) && !Lua.lua_isnil(L, -1)) { Lua.CharPtr msg = Lua.lua_tostring(L, -1); if (msg == null) { msg = "(error object is not a string)"; } l_message(progname, msg); Lua.lua_pop(L, 1); /* force a complete garbage collection in case of errors */ Lua.lua_gc(L, Lua.LUA_GCCOLLECT, 0); } return(status); }
static int pmain(Lua.lua_State L) { Smain s = (Smain)Lua.lua_touserdata(L, 1); int argc = s.argc; string[] argv = s.argv; Lua.Proto f; int i; if (Lua.lua_checkstack(L, argc) == 0) { fatal("too many input files"); } for (i = 0; i < argc; i++) { Lua.CharPtr filename = (Lua.strcmp(argv[i], "-") == 0) ? null : argv[i]; if (Lua.luaL_loadfile(L, filename) != 0) { fatal(Lua.lua_tostring(L, -1)); } } f = combine(L, argc); if (listing != 0) { Lua.luaU_print(f, (listing > 1)?1:0); } if (dumping != 0) { Stream D = (output == null) ? Lua.stdout : Lua.fopen(output, "wb"); if (D == null) { cannot("open"); } Lua.lua_lock(L); Lua.luaU_dump(L, f, writer, D, stripping); Lua.lua_unlock(L); if (Lua.ferror(D) != 0) { cannot("write"); } if (Lua.fclose(D) != 0) { cannot("close"); } } return(0); }
public static void PrintString(Lua.TString ts) { Lua.CharPtr s = Lua.getstr(ts); uint i, n = ts.tsv.len; Lua.printf("%c", '"'); for (i = 0; i < n; i++) { int c = (int)(byte)s[i]; switch (c) { case '"': Lua.printf("\\\""); break; case '\\': Lua.printf("\\\\"); break; case '\a': Lua.printf("\\a"); break; case '\b': Lua.printf("\\b"); break; case '\f': Lua.printf("\\f"); break; case '\n': Lua.printf("\\n"); break; case '\r': Lua.printf("\\r"); break; case '\t': Lua.printf("\\t"); break; case '\v': Lua.printf("\\v"); break; default: if (Lua.isprint(c)) { Lua.printf("%c", c); } else { Lua.printf("\\%03d", c); } break; //FIXME:added } } Lua.printf("%c", '"'); }
static int Main_luac(string[] args) { // prepend the exe name to the arg list as it's done in C // so that we don't have to change any of the args indexing // code above List <string> newargs = new List <string>(args); newargs.Insert(0, Assembly.GetExecutingAssembly().Location); args = (string[])newargs.ToArray(); Lua.lua_State L; int argc = args.Length; //FIXME:added Lua.CharPtr[] argv = new Lua.CharPtr[args.Length]; //FIXME:added for (int kk = 0; kk < argv.Length; ++kk) { argv[kk] = args[kk]; } int i = doargs(argc, args); newargs.RemoveRange(0, i); argc -= i; args = (string[])newargs.ToArray(); if (argc <= 0) { usage("no input files given"); } L = Lua.luaL_newstate(); if (L == null) { fatal("cannot create state: not enough memory"); } Lua.lua_pushcfunction(L, pmain); Lua.lua_pushinteger(L, argc); Lua.lua_pushlightuserdata(L, argv); if (Lua.lua_pcall(L, 2, 0, 0) != Lua.LUA_OK) { fatal(Lua.lua_tostring(L, -1)); } Lua.lua_close(L); return(Lua.EXIT_SUCCESS); }
private static void PrintConstant(Lua.Proto f, int i) { /*const*/ TValue o = f.k[i]; switch (Lua.ttype(o)) { case Lua.LUA_TNIL: Lua.printf("nil"); break; case Lua.LUA_TBOOLEAN: Lua.printf(Lua.bvalue(o) != 0 ? "true" : "false"); break; case Lua.LUA_TNUMFLT: { Lua.CharPtr buff = new Lua.CharPtr(new char[100]); Lua.sprintf(buff, Lua.LUA_NUMBER_FMT, Lua.fltvalue(o)); Lua.printf("%s", buff); if (buff[Lua.strspn(buff, "-0123456789")] == '\0') { Lua.printf(".0"); } break; } case Lua.LUA_TNUMINT: Lua.printf(Lua.LUA_INTEGER_FMT, Lua.ivalue(o)); break; case Lua.LUA_TSHRSTR: case Lua.LUA_TLNGSTR: PrintString(Lua.tsvalue(o)); break; default: /* cannot happen */ Lua.printf("? type=%d", Lua.ttype(o)); break; } }
static void usage(Lua.CharPtr message) { if (message[0] == '-') { Lua.fprintf(Lua.stderr, "%s: unrecognized option " + Lua.LUA_QS + "\n", progname, message); } else { Lua.fprintf(Lua.stderr, "%s: %s\n", progname, message); } Lua.fprintf(Lua.stderr, "usage: %s [options] [filenames].\n" + "Available options are:\n" + " - process stdin\n" + " -l list\n" + " -o name output to file " + Lua.LUA_QL("name") + " (default is \"%s\")\n" + " -p parse only\n" + " -s strip debug information\n" + " -v show version information\n" + " -- stop handling options\n", progname, Output); Environment.Exit(Lua.EXIT_FAILURE); }
//#define IS(s) (strcmp(argv[i],s)==0) static int doargs(int argc, string[] argv) { int i; int version = 0; if ((argv.Length > 0) && (argv[0] != "")) { progname = argv[0]; } for (i = 1; i < argc; i++) { if (argv[i][0] != '-') /* end of options; keep it */ { break; } else if (Lua.strcmp(argv[i], "--") == 0) /* end of options; skip it */ { ++i; if (version != 0) { ++version; } break; } else if (Lua.strcmp(argv[i], "-") == 0) /* end of options; use stdin */ { break; } else if (Lua.strcmp(argv[i], "-l") == 0) /* list */ { ++listing; } else if (Lua.strcmp(argv[i], "-o") == 0) /* output file */ { output = argv[++i]; if (output == null || (output[0] == 0)) { usage(Lua.LUA_QL("-o") + " needs argument"); } if (Lua.strcmp(argv[i], "-") == 0) { output = null; } } else if (Lua.strcmp(argv[i], "-p") == 0) /* parse only */ { dumping = 0; } else if (Lua.strcmp(argv[i], "-s") == 0) /* strip debug information */ { stripping = 1; } else if (Lua.strcmp(argv[i], "-v") == 0) /* show version */ { ++version; } else /* unknown option */ { usage(argv[i]); } } if (i == argc && ((listing != 0) || (dumping == 0))) { dumping = 0; argv[--i] = Output.ToString(); } if (version != 0) { Lua.printf("%s %s\n", Lua.LUA_RELEASE, Lua.LUA_COPYRIGHT); if (version == argc - 1) { Environment.Exit(Lua.EXIT_SUCCESS); } } return(i); }
static int pmain(Lua.lua_State L) { Smain s = (Smain)Lua.lua_touserdata(L, 1); string[] argv = s.argv; int script; int has_i = 0, has_v = 0, has_e = 0; globalL = L; if ((argv.Length > 0) && (argv[0] != "")) { progname = argv[0]; } Lua.lua_gc(L, Lua.LUA_GCSTOP, 0); /* stop collector during initialization */ Lua.luaL_openlibs(L); /* open libraries */ Lua.lua_gc(L, Lua.LUA_GCRESTART, 0); s.status = handle_luainit(L); if (s.status != 0) { return(0); } script = collectargs(argv, ref has_i, ref has_v, ref has_e); if (script < 0) { /* invalid args? */ print_usage(); s.status = 1; return(0); } if (has_v != 0) { print_version(); } s.status = runargs(L, argv, (script > 0) ? script : s.argc); if (s.status != 0) { return(0); } if (script != 0) { s.status = handle_script(L, argv, script); } if (s.status != 0) { return(0); } if (has_i != 0) { dotty(L); } else if ((script == 0) && (has_e == 0) && (has_v == 0)) { if (Lua.lua_stdin_is_tty() != 0) { print_version(); dotty(L); } else { dofile(L, null); /* executes stdin as a file */ } } return(0); }
public static string dolua_(string message) { if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>" + message); } if (L_ == null) { L_ = Lua.luaL_newstate(); Lua.luaL_openlibs(L_); } if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>2"); } string errorMessage = null; bool printResult = true; int status = Lua.luaL_loadbuffer(L_, message, (uint)Lua.strlen(message), "=stdin"); if (status == 0) { if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>3"); } status = docall_(L_, 0, printResult ? Lua.LUA_MULTRET : 0); } if ((status != 0) && !Lua.lua_isnil(L_, -1)) { if (DEBUG_) { Lua.fprintf(Lua.stdout, "%s\n", "==============>4"); } Lua.CharPtr msg = Lua.lua_tostring(L_, -1); if (msg == null) { msg = "(error object is not a string)"; } errorMessage = msg.ToString(); Lua.lua_pop(L_, 1); /* force a complete garbage collection in case of errors */ Lua.lua_gc(L_, Lua.LUA_GCCOLLECT, 0); } if (printResult) { //see Lua.LUA_MULTRET if (status == 0 && Lua.lua_gettop(L_) > 0) /* any result to print? */ { Lua.luaL_checkstack(L_, Lua.LUA_MINSTACK, "too many results to print"); Lua.lua_getglobal(L_, "print"); Lua.lua_insert(L_, 1); if (Lua.lua_pcall(L_, Lua.lua_gettop(L_) - 1, 0, 0) != 0) { l_message_(progname, Lua.lua_pushfstring(L_, "error calling " + Lua.LUA_QL("print").ToString() + " (%s)", Lua.lua_tostring(L_, -1))); } } } return(errorMessage); }
static int writer(Lua.lua_State L, Lua.CharPtr p, uint size, object u) { //UNUSED(L); return(((Lua.fwrite(p, (int)size, 1, (Stream)u) != 1) && (size != 0)) ? 1 : 0); }
static Lua.CharPtr progname = PROGNAME; /* actual program name */ static void fatal(Lua.CharPtr message) { Lua.fprintf(Lua.stderr, "%s: %s\n", progname, message); Environment.Exit(Lua.EXIT_FAILURE); }
static int dolibrary(Lua.lua_State L, Lua.CharPtr name) { Lua.lua_getglobal(L, "require"); Lua.lua_pushstring(L, name); return(report(L, docall(L, 1, 1))); }
} /* get line */ //FIXME: no Lua_MAXINPUT private static void lua_saveline(Lua.lua_State L, Lua.CharPtr line) /*(void)L; (void)line;*/ }
//#endif /* } */ //#endif /* } */ /* ** lua_readline defines how to show a prompt and then read a line from ** the standard input. ** lua_saveline defines how to "save" a read line in a "history". ** lua_freeline defines how to free a line read by lua_readline. */ //#if !defined(lua_readline) /* { */ //#if defined(LUA_USE_READLINE) /* { */ //#include <readline/readline.h> //#include <readline/history.h> //#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) //#define lua_saveline(L,line) ((void)L, add_history(line)) //#define lua_freeline(L,b) ((void)L, free(b)) //#else /* }{ */ private static int lua_readline(Lua.lua_State L, Lua.CharPtr b, Lua.CharPtr p) { /*(void)L,*/ Lua.fputs(p, Lua.stdout); Lua.fflush(Lua.stdout); /* show prompt */ return((Lua.fgets(b /*, LUA_MAXINPUT*/, Lua.stdin) != null) ? 1 : 0); } /* get line */ //FIXME: no Lua_MAXINPUT
static int dofile(Lua.lua_State L, Lua.CharPtr name) { int status = (Lua.luaL_loadfile(L, name) != 0) || (docall(L, 0, 1) != 0) ? 1 : 0; return(report(L, status)); }
static void cannot(Lua.CharPtr what) { Lua.fprintf(Lua.stderr, "%s: cannot %s %s: %s\n", progname, what, output, Lua.strerror(Lua.errno())); Environment.Exit(Lua.EXIT_FAILURE); }
static int dostring(Lua.lua_State L, Lua.CharPtr s, Lua.CharPtr name) { int status = (Lua.luaL_loadbuffer(L, s, (uint)Lua.strlen(s), name) != 0) || (docall(L, 0, 1) != 0) ? 1 : 0; return(report(L, status)); }