Hi,

this is a small change to the macro language, to accept syntax like this:

arr[] = "zero"
arr[] = "one"
:

Currently this is just an error. Now the key will be chosen as the
currently max numeric key in arr + 1. So holes in the numeric key
range are not filled.

Bert
---

 source/interpret.c |   88 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 55 insertions(+), 33 deletions(-)

diff --quilt old/source/interpret.c new/source/interpret.c
--- old/source/interpret.c
+++ new/source/interpret.c
@@ -2518,36 +2518,59 @@ static int arrayAssign(void)
     DISASM_RT(PC-2, 1);
     STACKDUMP(nDim, 3);
 
-    if (nDim > 0) {
-        POP(srcValue)
+    POP(srcValue)
 
+    if (nDim > 0) {
         errNum = makeArrayKeyFromArgs(nDim, &keyString, 0);
         if (errNum != STAT_OK) {
             return(errNum);
         }
-        
-        POP(dstArray)
+    }
 
-        if (dstArray.tag != ARRAY_TAG && dstArray.tag != NO_TAG) {
-            return(execError("cannot assign array element of non-array", 
NULL));
-        }
-        if (srcValue.tag == ARRAY_TAG) {
-            DataValue arrayCopyValue;
-            
-            errNum = ArrayCopy(&arrayCopyValue, &srcValue);
-            srcValue = arrayCopyValue;
-            if (errNum != STAT_OK) {
-                return(errNum);
+    POP(dstArray)
+
+    if (dstArray.tag != ARRAY_TAG && dstArray.tag != NO_TAG) {
+        return(execError("cannot assign array element of non-array", NULL));
+    }
+
+    /* array[] = expr: search max+1 numeric index */
+    if (nDim == 0) {
+        SparseArrayEntry *iter;
+        int maxKey = -1;
+
+        iter = arrayIterateFirst(&dstArray);
+        while (iter) {
+            int thisKey;
+
+            if (StringToNum(iter->key, &thisKey)) {
+                /* even if the rbTree is sorted, just check it to be sure */
+                if (thisKey > maxKey)
+                    maxKey = thisKey;
             }
+
+            iter = arrayIterateNext(iter);
         }
-        if (ArrayInsert(&dstArray, keyString, &srcValue)) {
-            return(STAT_OK);
-        }
-        else {
-            return(execError("array member allocation failure", NULL));
+
+        keyString = AllocString(TYPE_INT_STR_SIZE(int));
+        sprintf(keyString, "%d", maxKey + 1);
+    }
+
+    if (srcValue.tag == ARRAY_TAG) {
+        DataValue arrayCopyValue;
+
+        errNum = ArrayCopy(&arrayCopyValue, &srcValue);
+        srcValue = arrayCopyValue;
+        if (errNum != STAT_OK) {
+            return(errNum);
         }
     }
-    return(execError("empty operator []", NULL));
+
+    if (ArrayInsert(&dstArray, keyString, &srcValue)) {
+        return(STAT_OK);
+    }
+    else {
+        return(execError("array member allocation failure", NULL));
+    }
 }
 
 /*
@@ -2577,29 +2600,28 @@ static int arrayRefAndAssignSetup(void)
         POP(moveExpr)
     }
     
+    PEEK(srcArray, nDim)
+    if (srcArray.tag != ARRAY_TAG) {
+        return(execError("operator [] on non-array", NULL));
+    }
+
     if (nDim > 0) {
         errNum = makeArrayKeyFromArgs(nDim, &keyString, 1);
         if (errNum != STAT_OK) {
             return(errNum);
         }
 
-        PEEK(srcArray, nDim)
-        if (srcArray.tag == ARRAY_TAG) {
-            if (!ArrayGet(&srcArray, keyString, &valueItem)) {
-                return(execError("referenced array value not in array: %s", 
keyString));
-            }
-            PUSH(valueItem)
-            if (binaryOp) {
-                PUSH(moveExpr)
-            }
-            return(STAT_OK);
+        if (!ArrayGet(&srcArray, keyString, &valueItem)) {
+            return(execError("referenced array value not in array: %s", 
keyString));
         }
-        else {
-            return(execError("operator [] on non-array", NULL));
+        PUSH(valueItem)
+        if (binaryOp) {
+            PUSH(moveExpr)
         }
+        return(STAT_OK);
     }
     else {
-        return(execError("array[] not an lvalue", NULL));
+        return(execError("assign to uninitialized array[] lvalue", NULL));
     }
 }
 
-- 
NEdit Develop mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/develop

Reply via email to