From b9835f11e31b7e021da6b0831eac659425735ba2 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sun, 24 Jan 2010 12:02:01 +1000 Subject: New features Add unset -nocomplain and tests Implement lrepeat --- TODO | 1 - jim.c | 68 ++++++++++++++++++++++++++++++++++------- tests/misc.test | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 12 deletions(-) diff --git a/TODO b/TODO index ffd26cf..fc8dfc8 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ CORE LANGUAGE FEATURES - lrepeat -- unset -nocomplain - parse foo($bar) into special tokens so that interpolation/parsing is not required every time diff --git a/jim.c b/jim.c index e4c3a91..fc7e8ab 100644 --- a/jim.c +++ b/jim.c @@ -3536,10 +3536,12 @@ int Jim_UnsetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int flags) if (err == JIM_DICT_SUGAR) if (JimDictSugarSet(interp, nameObjPtr, NULL) == JIM_OK) return JIM_OK; - Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); - Jim_AppendStrings(interp, Jim_GetResult(interp), - "can't unset \"", nameObjPtr->bytes, - "\": no such variable", NULL); + if (flags & JIM_ERRMSG) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "can't unset \"", nameObjPtr->bytes, + "\": no such variable", NULL); + } return JIM_ERR; /* var not found */ } varPtr = nameObjPtr->internalRep.varValue.varPtr; @@ -9319,19 +9321,34 @@ static int Jim_SetCoreCommand(Jim_Interp *interp, int argc, return JIM_OK; } -/* [unset] */ +/* [unset] + * + * unset ?-nocomplain? ?--? ?varName ...? + */ static int Jim_UnsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { - int i; + int i = 1; + int complain = 1; - if (argc < 2) { - Jim_WrongNumArgs(interp, 1, argv, "varName ?varName ...?"); - return JIM_ERR; + while (i < argc) { + if (Jim_CompareStringImmediate(interp, argv[i], "--")) { + i++; + break; + } + if (Jim_CompareStringImmediate(interp, argv[i], "-nocomplain")) { + complain = 0; + i++; + continue; + } + break; } - for (i = 1; i < argc; i++) { - if (Jim_UnsetVariable(interp, argv[i], JIM_ERRMSG) != JIM_OK) + + while (i < argc) { + if (Jim_UnsetVariable(interp, argv[i], complain ? JIM_ERRMSG : JIM_NONE) != JIM_OK && complain) { return JIM_ERR; + } + i++; } return JIM_OK; } @@ -11807,6 +11824,33 @@ static int Jim_LrangeCoreCommand(Jim_Interp *interp, int argc, return JIM_OK; } +/* [lrepeat] */ +static int Jim_LrepeatCoreCommand(Jim_Interp *interp, int argc, + Jim_Obj *const *argv) +{ + Jim_Obj *objPtr; + long count; + + if (argc < 3 || Jim_GetLong(interp, argv[1], &count) != JIM_OK || count <= 0) { + Jim_WrongNumArgs(interp, 1, argv, "positiveCount value ?value ...?"); + return JIM_ERR; + } + + argc -= 2; + argv += 2; + + objPtr = Jim_NewListObj(interp, argv, argc); + while (--count) { + int i; + for (i = 0; i < argc; i++) { + ListAppendElement(objPtr, argv[i]); + } + } + + Jim_SetResult(interp, objPtr); + return JIM_OK; +} + /* [env] */ static int Jim_EnvCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) @@ -12034,6 +12078,7 @@ static const struct { {"scan", Jim_ScanCoreCommand}, {"error", Jim_ErrorCoreCommand}, {"lrange", Jim_LrangeCoreCommand}, + {"lrepeat", Jim_LrepeatCoreCommand}, {"env", Jim_EnvCoreCommand}, {"source", Jim_SourceCoreCommand}, {"lreverse", Jim_LreverseCoreCommand}, @@ -12123,6 +12168,7 @@ int Jim_GetEnum(Jim_Interp *interp, Jim_Obj *objPtr, int i, count = 0; *indexPtr = -1; + for (entryPtr = tablePtr, i = 0; *entryPtr != NULL; entryPtr++, i++) { if (Jim_CompareStringImmediate(interp, objPtr, *entryPtr)) { *indexPtr = i; diff --git a/tests/misc.test b/tests/misc.test index 53dce1c..c5d45e5 100644 --- a/tests/misc.test +++ b/tests/misc.test @@ -31,4 +31,98 @@ test io-1.1 "Read last line with no newline" { list $lines } {2} +section "unset" + +set g1 1 +set g2 2 +array set g3 {4 5 6 7} + +proc test_unset {} { + test unset-1.1 "Simple var" { + set g4 4 + list [catch {unset g4; info exists g4} msg] $msg + } {0 0} + + test unset-1.2 "Simple var" { + list [catch {unset g4; info exists g4} msg] $msg + } {1 {can't unset "g4": no such variable}} + + test unset-1.3 "Simple var" { + list [catch {unset g2; info exists g2} msg] $msg + } {1 {can't unset "g2": no such variable}} + + test unset-1.4 "Global via global" { + global g1 + list [catch {unset g1; info exists g1} msg] $msg + } {0 0} + + test unset-1.5 "Global error" { + list [catch {unset ::g2; info exists ::g2} msg] $msg + } {0 0} + + test unset-1.6 "Global array" { + list [catch {unset ::g3; info exists ::g3} msg] $msg + } {0 0} + + test unset-1.7 "Simple var -nocomplain" { + list [catch {unset -nocomplain g2; info exists g2} msg] $msg + } {0 0} + + test unset-1.8 "Simple var --" { + list [catch {unset -- g2; info exists g2} msg] $msg + } {1 {can't unset "g2": no such variable}} + + test unset-1.9 "Simple var -nocomplain --" { + set g2 1 + list [catch {unset -nocomplain -- g2; info exists g2} msg] $msg + } {0 0} + + test unset-1.10 "Var named -nocomplain with --" { + set -nocomplain 1 + list [catch {unset -- -nocomplain; info exists -nocomplain} msg] $msg + } {0 0} + + test unset-1.11 "Unset no args" { + list [catch {unset} msg] $msg + } {0 {}} +} + +test_unset + +section "lrepeat" + +test lrepeat-1.1 "Basic tests" { + lrepeat 1 a +} {a} + +test lrepeat-1.2 "Basic tests" { + lrepeat 1 a b +} {a b} + +test lrepeat-1.3 "Basic tests" { + lrepeat 2 a b +} {a b a b} + +test lrepeat-1.4 "Basic tests" { + lrepeat 2 a +} {a a} + +test lrepeat-1.5 "Errors" { + catch {lrepeat} +} {1} + +test lrepeat-1.6 "Errors" { + catch {lrepeat 1} +} {1} + +test lrepeat-1.7 "Errors" { + catch {lrepeat 0 a b} +} {1} + +test lrepeat-1.8 "Errors" { + catch {lrepeat -10 a} +} {1} + +section "unset" + testreport -- cgit v1.1