gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2012年02月27日 11:48:45 +0100
committerFrancesco Abbate <francesco.bbt@gmail.com>2012年02月27日 11:48:45 +0100
commite4bce46b5857244f84a756589b2c92c32386bb73 (patch)
tree04eab3ea305ac33328e46fc06e3707c3a4da4808
parentb276952b8a8432a00ea54e1d1dfeb69495900083 (diff)
downloadgsl-shell-e4bce46b5857244f84a756589b2c92c32386bb73.tar.gz
Optimize code for interactive shell's input line treatment
Diffstat
-rw-r--r--gsl-shell-jit.c 51
1 files changed, 31 insertions, 20 deletions
diff --git a/gsl-shell-jit.c b/gsl-shell-jit.c
index 576a98cf..bea6611a 100644
--- a/gsl-shell-jit.c
+++ b/gsl-shell-jit.c
@@ -308,10 +308,8 @@ static int pushline (lua_State *L, int firstline) {
that the value is returned as a result of the evaluation.
If the value is not an expression leave the stack as before and
returns a non zero value. */
-static int yield_expr(lua_State* L, int index)
+static int yield_expr(lua_State* L, int index, const char* line, size_t len)
{
- size_t len;
- const char *line = lua_tolstring(L, index, &len);
const char *p;
int status;
@@ -340,34 +338,47 @@ static int yield_expr(lua_State* L, int index)
static int loadline(lua_State *L)
{
int status;
+ size_t len;
+ const char *line;
+
lua_settop(L, 0);
if (!pushline(L, 1))
return -1; /* no input */
- if (strcmp (lua_tostring(L, 1), "exit") == 0)
+ line = lua_tolstring(L, 1, &len);
+
+ if (strcmp (line, "exit") == 0)
return -1;
/* try to load the string as an expression */
- if (yield_expr(L, 1) == 0)
+ if (yield_expr(L, 1, line, len) == 0)
return 0;
- for (;;) { /* repeat until gets a complete line */
- status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
- if (!incomplete(L, status)) break; /* cannot try to add lines? */
- if (!pushline(L, 0)) /* no more input? */
- return -1;
- lua_pushliteral(L, "\n"); /* add a new line... */
- lua_insert(L, -2); /* ...between the two lines */
- lua_concat(L, 3); /* join them */
- }
+ /* try to load it as a simple Lua chunk */
+ status = luaL_loadbuffer(L, line, len, "=stdin");
- /* even if previous "load" was successfull we try to load the string as
- an expression */
- if (yield_expr(L, 1) == 0)
+ if (incomplete(L, status))
{
- /* remove old eval function */
- lua_remove(L, 1);
- return 0;
+ for (;;) { /* repeat until gets a complete line */
+ if (!pushline(L, 0)) /* no more input? */
+ return -1;
+ lua_pushliteral(L, "\n"); /* add a new line... */
+ lua_insert(L, -2); /* ...between the two lines */
+ lua_concat(L, 3); /* join them */
+
+ line = lua_tolstring(L, 1, &len);
+ status = luaL_loadbuffer(L, line, len, "=stdin");
+ if (!incomplete(L, status)) break; /* cannot try to add lines? */
+ }
+
+ /* even if previous "load" was successfull we try to load the string as
+ an expression */
+ if (yield_expr(L, 1, line, len) == 0)
+ {
+ /* remove old eval function */
+ lua_remove(L, 1);
+ return 0;
+ }
}
my_saveline(L, 1);
generated by cgit v1.2.3 (git 2.25.1) at 2025年10月04日 06:27:55 +0000

AltStyle によって変換されたページ (->オリジナル) /