aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez>2005-03-14 07:22:02 +0000
committerantirez <antirez>2005-03-14 07:22:02 +0000
commit39b905ab9927ee895865b0c292f85542dce1a9c4 (patch)
tree4944c44b461f5b58f506ece8929376103fe420be
parent0234b2d8533d89ecfdacaf3783fd758d86131689 (diff)
downloadjimtcl-39b905ab9927ee895865b0c292f85542dce1a9c4.zip
jimtcl-39b905ab9927ee895865b0c292f85542dce1a9c4.tar.gz
jimtcl-39b905ab9927ee895865b0c292f85542dce1a9c4.tar.bz2
[lmap] command.
-rw-r--r--ChangeLog4
-rw-r--r--TODO2
-rw-r--r--jim.c51
3 files changed, 47 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index e0453e0..296833d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2005-03-13 18:43 antirez
+
+ * ChangeLog, jim.h: Max nesting depth modified to 10000
+
2005-03-13 16:53 antirez
* ChangeLog, jim.c: [string first]. Tests still not added, until
diff --git a/TODO b/TODO
index bcf879d..2667ed3 100644
--- a/TODO
+++ b/TODO
@@ -15,7 +15,7 @@ CORE COMMANDS
are not lazy. Math functions are not present but probably will never
be added as expr functions, but as Tcl commands, like [sin], [cos] and
so on.
-- onleave commnad, executing something as soon as the current procedure
+- onleave command, executing something as soon as the current procedure
returns. With no arguments it returns the script set, with one appends
the onleave script. There should be a way to reset.
diff --git a/jim.c b/jim.c
index f566034..4b54201 100644
--- a/jim.c
+++ b/jim.c
@@ -1,7 +1,7 @@
/* Jim - A small embeddable Tcl interpreter
* Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
*
- * $Id: jim.c,v 1.100 2005/03/13 15:53:51 antirez Exp $
+ * $Id: jim.c,v 1.101 2005/03/14 07:22:02 antirez Exp $
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -8225,17 +8225,22 @@ out:
return JIM_OK;
}
-/* [foreach] */
-static int Jim_ForeachCoreCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
+/* foreach + lmap implementation. */
+static int JimForeachMapHelper(Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv, int doMap)
{
int result = JIM_ERR, i, nbrOfLists, *listsIdx, *listsEnd;
int nbrOfLoops = 0;
- Jim_Obj *emptyStr, *script;
+ Jim_Obj *emptyStr, *script, *mapRes = NULL;
+
if (argc < 4 || argc % 2 != 0) {
Jim_WrongNumArgs(interp, 1, argv, "varList list ?varList list ...? script");
return JIM_ERR;
}
+ if (doMap) {
+ mapRes = Jim_NewListObj(interp, NULL, 0);
+ Jim_IncrRefCount(mapRes);
+ }
emptyStr = Jim_NewEmptyStringObj(interp);
Jim_IncrRefCount(emptyStr);
script = argv[argc-1]; /* Last argument is a script */
@@ -8285,21 +8290,48 @@ static int Jim_ForeachCoreCommand(Jim_Interp *interp, int argc,
}
}
switch (result = Jim_EvalObj(interp, script)) {
- case JIM_OK: case JIM_CONTINUE: break;
- case JIM_BREAK: goto out; break;
- default: goto err;
+ case JIM_OK:
+ if (doMap)
+ Jim_ListAppendElement(interp, mapRes, interp->result);
+ break;
+ case JIM_CONTINUE:
+ break;
+ case JIM_BREAK:
+ goto out;
+ break;
+ default:
+ goto err;
}
}
out:
result = JIM_OK;
- Jim_SetEmptyResult(interp);
+ if (doMap)
+ Jim_SetResult(interp, mapRes);
+ else
+ Jim_SetEmptyResult(interp);
err:
+ if (doMap)
+ Jim_DecrRefCount(interp, mapRes);
Jim_DecrRefCount(interp, emptyStr);
Jim_Free(listsIdx);
Jim_Free(listsEnd);
return result;
}
+/* [foreach] */
+static int Jim_ForeachCoreCommand(Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv)
+{
+ return JimForeachMapHelper(interp, argc, argv, 0);
+}
+
+/* [lmap] */
+static int Jim_LmapCoreCommand(Jim_Interp *interp, int argc,
+ Jim_Obj *const *argv)
+{
+ return JimForeachMapHelper(interp, argc, argv, 1);
+}
+
/* [if] */
static int Jim_IfCoreCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
@@ -9833,6 +9865,7 @@ static struct {
{"while", Jim_WhileCoreCommand},
{"for", Jim_ForCoreCommand},
{"foreach", Jim_ForeachCoreCommand},
+ {"lmap", Jim_LmapCoreCommand},
{"if", Jim_IfCoreCommand},
{"switch", Jim_SwitchCoreCommand},
{"list", Jim_ListCoreCommand},