lua-users home
lua-l archive

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


Hi all,

compared to python, lua executable has several issues:

1. must prefix '=' to expressions, otherwise you cannot end the line.
2. readline history shows 'return ' if you prefix '=' in the previous line.
3. the previous return is not saved into '_' variable, we cannot use it as a calculator.

My patch solve above three issues, now we no longer need the prefix '=' at all, and readline history will show the expressions as is, without prefix a 'return', and the first result of previous return will always saved into _G["_"].

I hope it will be useful for someone.

Usage: cp the patch file into your lua-5.1.4/src and do command: patch -p0 <lua_c.patch


--- lua.c	2007-12-28 23:32:23.000000000 +0800
+++ lua.c.new	2008-11-24 08:56:18.000000000 +0800
@@ -184,10 +184,20 @@
   l = strlen(b);
   if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
     b[l-1] = '\0';  /* remove it */
-  if (firstline && b[0] == '=')  /* first line starts with `=' ? */
-    lua_pushfstring(L, "return %s", b+1);  /* change it to `return' */
-  else
+  if (firstline) {	/* for first line, try return it if possible. */
+    char rbuf[LUA_MAXINPUT];
+    snprintf(rbuf, LUA_MAXINPUT-1, "return %s", (b[0]=='=') ? (b+1) : b);
+    rbuf[LUA_MAXINPUT-1] = '\0';	/* ensure zero terminated */
+    int result = luaL_loadstring(L, rbuf);
+    lua_pop(L, 1);			/* ignore the output anyway */
+    if (result == LUA_ERRSYNTAX) {
+      lua_pushstring(L, b);	/* if syntax error, leave it as is */
+    } else {
+      lua_pushstring(L, rbuf);	/* if compile passed, return it */
+    }
+  } else {
     lua_pushstring(L, b);
+  }
   lua_freeline(L, b);
   return 1;
 }
@@ -207,6 +217,12 @@
     lua_insert(L, -2);  /* ...between the two lines */
     lua_concat(L, 3);  /* join them */
   }
+  /* if there is a prefixed 'return', remove it before save to readline() */
+  const char *line = lua_tostring(L, 1);
+  if (strncmp(line, "return ", 7) == 0) {
+    lua_pushstring(L, line+7);
+    lua_replace(L, 1);
+  }
   lua_saveline(L, 1);
   lua_remove(L, 1);  /* remove line */
   return status;
@@ -221,6 +237,8 @@
     if (status == 0) status = docall(L, 0, 0);
     report(L, status);
     if (status == 0 && lua_gettop(L) > 0) {  /* any result to print? */
+      lua_pushvalue(L, 1);
+      lua_setglobal(L, "_");	/* push the first return value in _G["_"] */
       lua_getglobal(L, "print");
       lua_insert(L, 1);
       if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)