History

Lua 5.3.0-work2

Lua Team committed 12 years ago

Commit ad386d91 parent b83a40a60 files · +3280 −2672Browse files

Showing the changes to src/lua.c (1 of 60 files). Show all

Unified · Split

src/lua.c+69−31
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.c,v 1.206 2012/09/29 20:07:06 roberto Exp $
2+** $Id: lua.c,v 1.210 2014/02/26 15:27:56 roberto Exp $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -31,28 +31,38 @@ · 25 unmodified lines
3131 #define LUA_MAXINPUT 512
3232 #endif
3333
34−#if !defined(LUA_INIT)
35−#define LUA_INIT "LUA_INIT"
34+#if !defined(LUA_INIT_VAR)
35+#define LUA_INIT_VAR "LUA_INIT"
3636 #endif
3737
38−#define LUA_INITVERSION \
39− LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
38+#define LUA_INITVARVERSION \
39+ LUA_INIT_VAR "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
4040
4141
4242 /*
4343 ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
4444 ** is, whether we're running lua interactively).
4545 */
46−#if defined(LUA_USE_ISATTY)
46+#if !defined(lua_stdin_is_tty) /* { */
47+
48+#if defined(LUA_USE_POSIX) /* { */
49+
4750 #include <unistd.h>
4851 #define lua_stdin_is_tty() isatty(0)
49−#elif defined(LUA_WIN)
52+
53+#elif defined(LUA_WIN) /* }{ */
54+
5055 #include <io.h>
51−#include <stdio.h>
5256 #define lua_stdin_is_tty() _isatty(_fileno(stdin))
53−#else
57+
58+#else /* }{ */
59+
60+/* ANSI definition */
5461 #define lua_stdin_is_tty() 1 /* assume stdin is a tty */
55−#endif
62+
63+#endif /* } */
64+
65+#endif /* } */
5666
5767
5868 /*
@@ -61,9 +71,10 @@ · 2 unmodified lines
6171 ** lua_saveline defines how to "save" a read line in a "history".
6272 ** lua_freeline defines how to free a line read by lua_readline.
6373 */
64−#if defined(LUA_USE_READLINE)
74+#if !defined(lua_readline) /* { */
75+
76+#if defined(LUA_USE_READLINE) /* { */
6577
66−#include <stdio.h>
6778 #include <readline/readline.h>
6879 #include <readline/history.h>
6980 #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
@@ -72,7 +83,7 @@ · 2 unmodified lines
7283 add_history(lua_tostring(L, idx)); /* add it to history */
7384 #define lua_freeline(L,b) ((void)L, free(b))
7485
75−#elif !defined(lua_readline)
86+#else /* }{ */
7687
7788 #define lua_readline(L,b,p) \
7889 ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \
@@ -80,7 +91,9 @@ · 1 unmodified line
8091 #define lua_saveline(L,idx) { (void)L; (void)idx; }
8192 #define lua_freeline(L,b) { (void)L; (void)b; }
8293
83−#endif
94+#endif /* } */
95+
96+#endif /* } */
8497
8598
8699
@@ -257,20 +270,21 @@ static int incomplete (lua_State *L, int status) { · 170 unmodified lines
257270 }
258271
259272
273+/* prompt the user, read a line, and push it into the Lua stack */
260274 static int pushline (lua_State *L, int firstline) {
261275 char buffer[LUA_MAXINPUT];
262276 char *b = buffer;
263277 size_t l;
264278 const char *prmt = get_prompt(L, firstline);
265279 int readstatus = lua_readline(L, b, prmt);
266− lua_pop(L, 1); /* remove result from 'get_prompt' */
267280 if (readstatus == 0)
268− return 0; /* no input */
281+ return 0; /* no input (prompt will be popped by caller) */
282+ lua_pop(L, 1); /* remove prompt */
269283 l = strlen(b);
270284 if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
271285 b[l-1] = '\0'; /* remove it */
272− if (firstline && b[0] == '=') /* first line starts with `=' ? */
273− lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
286+ if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */
287+ lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
274288 else
275289 lua_pushstring(L, b);
276290 lua_freeline(L, b);
@@ -278,24 +292,48 @@ static int pushline (lua_State *L, int firstline) { · 1 unmodified line
278292 }
279293
280294
281−static int loadline (lua_State *L) {
295+/* try to compile line on the stack as 'return <line>'; on return, stack
296+ has either compiled chunk or original line (if compilation failed) */
297+static int addreturn (lua_State *L) {
282298 int status;
283− lua_settop(L, 0);
284− if (!pushline(L, 1))
285− return -1; /* no input */
299+ size_t len; const char *line;
300+ lua_pushliteral(L, "return ");
301+ lua_pushvalue(L, -2); /* duplicate line */
302+ lua_concat(L, 2); /* new line is "return ..." */
303+ line = lua_tolstring(L, -1, &len);
304+ if ((status = luaL_loadbuffer(L, line, len, "=stdin")) == LUA_OK)
305+ lua_remove(L, -3); /* remove original line */
306+ else
307+ lua_pop(L, 2); /* remove result from 'luaL_loadbuffer' and new line */
308+ return status;
309+}
310+
311+
312+/* read multiple lines until a complete line */
313+static int multiline (lua_State *L) {
286314 for (;;) { /* repeat until gets a complete line */
287− size_t l;
288− const char *line = lua_tolstring(L, 1, &l);
289− status = luaL_loadbuffer(L, line, l, "=stdin");
290− if (!incomplete(L, status)) break; /* cannot try to add lines? */
291− if (!pushline(L, 0)) /* no more input? */
292− return -1;
293− lua_pushliteral(L, "\n"); /* add a new line... */
315+ size_t len;
316+ const char *line = lua_tolstring(L, 1, &len); /* get what it has */
317+ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
318+ if (!incomplete(L, status) || !pushline(L, 0))
319+ return status; /* cannot/should not try to add continuation line */
320+ lua_pushliteral(L, "\n"); /* add newline... */
294321 lua_insert(L, -2); /* ...between the two lines */
295322 lua_concat(L, 3); /* join them */
296323 }
324+}
325+
326+
327+static int loadline (lua_State *L) {
328+ int status;
329+ lua_settop(L, 0);
330+ if (!pushline(L, 1))
331+ return -1; /* no input */
332+ if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
333+ status = multiline(L); /* try as command, maybe with continuation lines */
297334 lua_saveline(L, 1);
298335 lua_remove(L, 1); /* remove line */
336+ lua_assert(lua_gettop(L) == 1);
299337 return status;
300338 }
301339
@@ -421,10 +459,10 @@ static int runargs (lua_State *L, char **argv, int n) { · 119 unmodified lines
421459
422460
423461 static int handle_luainit (lua_State *L) {
424− const char *name = "=" LUA_INITVERSION;
462+ const char *name = "=" LUA_INITVARVERSION;
425463 const char *init = getenv(name + 1);
426464 if (init == NULL) {
427− name = "=" LUA_INIT;
465+ name = "=" LUA_INIT_VAR;
428466 init = getenv(name + 1); /* try alternative name */
429467 }
430468 if (init == NULL) return LUA_OK;