diff options
author | antirez <antirez> | 2005-03-14 07:22:02 +0000 |
---|---|---|
committer | antirez <antirez> | 2005-03-14 07:22:02 +0000 |
commit | 39b905ab9927ee895865b0c292f85542dce1a9c4 (patch) | |
tree | 4944c44b461f5b58f506ece8929376103fe420be /jim.c | |
parent | 0234b2d8533d89ecfdacaf3783fd758d86131689 (diff) | |
download | jimtcl-39b905ab9927ee895865b0c292f85542dce1a9c4.zip jimtcl-39b905ab9927ee895865b0c292f85542dce1a9c4.tar.gz jimtcl-39b905ab9927ee895865b0c292f85542dce1a9c4.tar.bz2 |
[lmap] command.
Diffstat (limited to 'jim.c')
-rw-r--r-- | jim.c | 51 |
1 files changed, 42 insertions, 9 deletions
@@ -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}, |