OSDN Git Service

save point.
authorvisor <visor@users.sourceforge.jp>
Wed, 20 Aug 2014 15:38:50 +0000 (00:38 +0900)
committervisor <visor@users.sourceforge.jp>
Wed, 20 Aug 2014 15:38:50 +0000 (00:38 +0900)
lib/ml.cc
lib/ml.h
modules/ml-texp.cc
modules/ml-variable.cc

index 0b256a3..bd2447c 100644 (file)
--- a/lib/ml.cc
+++ b/lib/ml.cc
@@ -68,42 +68,50 @@ void  MNode::swap (MNode& b) {
 }
 
 MNode*  MNode::vectorGet (size_t pos) {
-    assert (type == MC_VECTOR);
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
     return vector->get (pos);
 }
 
 size_t  MNode::vectorSize () {
-    assert (type == MC_VECTOR);
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
     return vector->size ();
 }
 
 void  MNode::vectorPut (size_t pos, MNode* e) {
-    assert (type == MC_VECTOR);
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
     vector->put (pos, e);
 }
 
 void  MNode::vectorPush (MNode* e) {
-    assert (type == MC_VECTOR);
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
     vector->push (e);
 }
 
 MNode*  MNode::vectorPop () {
-    assert (type == MC_VECTOR);
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
     return vector->pop ();
 }
 
 void  MNode::vectorResize (size_t pos) {
-    assert (type == MC_VECTOR);
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
     vector->resize (pos);
 }
 
 MNode*  MNode::tableGet (const ustring& name) {
-    assert (type == MC_TABLE);
+    if (type != MC_TABLE)
+       throw (uErrorWrongType );
     return table->getVar (name);
 }
 
 void  MNode::tablePut (const ustring& name, MNode* e) {
-    assert (type == MC_TABLE);
+    if (type != MC_TABLE)
+       throw (uErrorWrongType );
     table->setVar (name, e);
 }
 
@@ -241,7 +249,10 @@ ustring  MNode::to_string () {
 
 ustring  MNode::vector_to_string () {
     ustring  ans;
-    assert (type == MC_VECTOR);
+
+    if (type != MC_VECTOR)
+       throw (uErrorWrongType );
+
     ans.append (CharConst ("["));
     for (MotorVector::size_type i = 0; i < vector->size (); ++ i) {
        if (i > 0)
@@ -254,7 +265,10 @@ ustring  MNode::vector_to_string () {
 
 ustring  MNode::table_to_string () {
     ustring  ans;
-    assert (type == MC_TABLE);
+
+    if (type != MC_TABLE)
+       throw (uErrorWrongType );
+
     ans.append (CharConst ("{"));
     MotorVar::iterator  b, e;
     size_t  c = 0;
index e43e48d..20f728c 100644 (file)
--- a/lib/ml.h
+++ b/lib/ml.h
@@ -184,6 +184,30 @@ inline bool  isNil (MNode* a) {
     return (a == NULL || a->isNil ());
 }
 
+inline bool  isCons (MNode* a) {
+    return (a != NULL && a->isCons ());
+}
+
+inline bool  isStr (MNode* a) {
+    return (a != NULL && a->isStr ());
+}
+
+inline bool  isSym (MNode* a) {
+    return (a != NULL && a->isSym ());
+}
+
+inline bool  isReal (MNode* a) {
+    return (a != NULL && a->isReal ());
+}
+
+inline bool  isVector (MNode* a) {
+    return (a != NULL && a->isVector ());
+}
+
+inline bool  isTable (MNode* a) {
+    return (a != NULL && a->isTable ());
+}
+
 extern bool  eq (MNode* a, MNode* b);
 extern bool  equal (MNode* a, MNode* b);
 
index 9e7ac9c..04bbf70 100644 (file)
@@ -53,7 +53,9 @@ MNode*  ml_vector_get (MNode* cell, MlEnv* mlenv) {
     if (arg)
        throw (uErrorWrongNumber);
 
-    if (! vec () || ! vec ()->isVector ()) {
+    if (isNil (vec ()))
+       return NULL;
+    if (! vec ()->isVector ()) {
        throw (uErrorWrongType);
     }
     return vec ()->vectorGet (n);
@@ -77,7 +79,9 @@ MNode*  ml_vector_size (MNode* cell, MlEnv* mlenv) {
     if (arg)
        throw (uErrorWrongNumber);
 
-    if (! vec () || ! vec ()->isVector ()) {
+    if (isNil (vec ()))
+       newMNode_num (0);
+    if (! vec ()->isVector ()) {
        throw (uErrorWrongType);
     }
     return newMNode_num (vec ()->vectorSize ());
@@ -106,10 +110,9 @@ MNode*  ml_vector_put (MNode* cell, MlEnv* mlenv) {
     nextNode (arg);
     if (arg)
        throw (uErrorWrongNumber);
-
-    if (! vec () || ! vec ()->isVector ()) {
+    if (! isVector (vec ()))
        throw (uErrorWrongType);
-    }
+
     vec ()->vectorPut (n, e ());
     return e.release ();
 }
@@ -134,10 +137,9 @@ MNode*  ml_vector_push (MNode* cell, MlEnv* mlenv) {
     nextNode (arg);
     if (arg)
        throw (uErrorWrongNumber);
-
-    if (! vec () || ! vec ()->isVector ()) {
+    if (! isVector (vec ()))
        throw (uErrorWrongType);
-    }
+
     vec ()->vectorPush (e ());
     return e.release ();
 }
@@ -159,10 +161,9 @@ MNode*  ml_vector_pop (MNode* cell, MlEnv* mlenv) {
     nextNode (arg);
     if (arg)
        throw (uErrorWrongNumber);
-
-    if (! vec () || ! vec ()->isVector ()) {
+    if (! isVector (vec ()))
        throw (uErrorWrongType);
-    }
+
     return vec ()->vectorPop ();
 }
 
@@ -186,10 +187,9 @@ MNode*  ml_vector_resize (MNode* cell, MlEnv* mlenv) {
     nextNode (arg);
     if (arg)
        throw (uErrorWrongNumber);
-
-    if (! vec () || ! vec ()->isVector ()) {
+    if (! isVector (vec ()))
        throw (uErrorWrongType);
-    }
+
     vec ()->vectorResize (n);
     return vec.release ();
 }
@@ -245,9 +245,11 @@ MNode*  ml_vector_to_list (MNode* cell, MlEnv* mlenv) {
     if (arg)
        throw (uErrorWrongNumber);
 
-    if (! e ()->isVector ()) {
+    if (isNil (e ()))
+       return NULL;
+    if (! isVector (e ()))
        throw (uErrorWrongType);
-    }
+
     MotorVector::iterator  b = e ()->vector->begin ();
     MotorVector::iterator  t = e ()->vector->end ();
     for (; b < t; ++ b) {
@@ -284,11 +286,11 @@ MNode*  ml_vector_each (MNode* cell, MlEnv* mlenv) {
 
     setParams (arg, 2, &params, kwlist, &keywords, &rest);
     vlist = eval (params[0], mlenv);
-    if (vlist () && vlist ()->isSym ()) {
+    if (isSym (vlist ())) {
        lv.push_back (vlist ()->to_string ());
-    } else if (vlist () && vlist ()->isCons ()) {
+    } else if (isCons (vlist ())) {
        a = vlist ();
-       while (a && a->isCons ()) {
+       while (isCons (a)) {
            lv.push_back (to_string (a->car ()));
            nextNode (a);
        }
@@ -304,7 +306,7 @@ MNode*  ml_vector_each (MNode* cell, MlEnv* mlenv) {
     for (i = 0; i < iu; i ++) {
        if (a) {
            MNode*  e = a->car ();
-           if (e && e->isVector ()) {
+           if (isVector (e)) {
                llv.push_back (e);
            } else {
                throw (uErrorWrongType);
@@ -368,7 +370,7 @@ MNode*  ml_table (MNode* cell, MlEnv* mlenv) {
     while (arg) {
        v = eval (arg->car (), mlenv);
        nextNode (arg);
-       if (v () && v ()->isCons ()) {
+       if (isCons (v ())) {
            ans ()->tablePut (to_string (v ()->car ()), v ()->cdr ());
        } else {
            throw (uErrorWrongType);
@@ -398,9 +400,11 @@ MNode*  ml_table_get (MNode* cell, MlEnv* mlenv) {
     if (arg)
        throw (uErrorWrongNumber);
 
-    if (! tbl () && ! tbl ()->isTable ()) {
+    if (isNil (tbl ()))
+       return NULL;
+    if (! tbl ()->isTable ())
        throw (uErrorWrongType);
-    }
+
     return tbl ()->tableGet (name);
 }
 
@@ -428,10 +432,10 @@ MNode*  ml_table_put (MNode* cell, MlEnv* mlenv) {
     if (arg)
        throw (uErrorWrongNumber);
 
-    if (! tbl () && ! tbl ()->isTable ()) {
+    if (! isTable (tbl ()))
        throw (uErrorWrongType);
-    }
     tbl ()->tablePut (name, e ());
+
     return e.release ();
 }
 
@@ -454,14 +458,17 @@ MNode*  ml_table_to_list (MNode* cell, MlEnv* mlenv) {
     if (arg)
        throw (uErrorWrongNumber);
 
-    if (! e ()->isTable ()) {
+    if (isNil (e ()))
+       return NULL;
+    if (! e ()->isTable ())
        throw (uErrorWrongType);
-    }
+
     MotorVar::iterator  b = e ()->table->begin ();
     MotorVar::iterator  t = e ()->table->end ();
     for (; b != t; ++ b) {
        ans.append (newMNode_cons (newMNode_sym (new ustring ((*b).first)), (*b).second ()));
     }
+
     return ans.release ();
 }
 
@@ -489,9 +496,8 @@ MNode*  ml_table_each (MNode* cell, MlEnv* mlenv) {
     if (vkey.size () == 0 || vval.size () == 0) {
        throw (uErrorBadArg);
     }
-    if (! tbl () || ! tbl ()->isTable () ) {
+    if (! isTable (tbl ()))
        throw (uErrorWrongType);
-    }
     
     {
        AutoLocalVariable  autoLocal (mlenv);
index 53352b0..07dbb9c 100644 (file)
@@ -115,20 +115,34 @@ MNode*  ml_setarray (MNode* cell, MlEnv* mlenv) {
                vars.push_back (to_string (a1->car ()));
                nextNode (a1);
            }
-           while (a2 && a2->isCons ()) {
-               MNode*  a3 = a2->car ();
-               n ++;
-               for (v = vars.begin (); v < vars.end (); v ++) {
-                   if (a3 && a3->isCons ()) {
-                       mlenv->setAry (*v, n, a3->car ());
-                   } else {
-                       mlenv->setAry (*v, n, NULL);
+           if (isVector (a2)) {
+               size_t  i;
+               size_t  m = a2->vectorSize ();
+               for (i = 0; i < m; ++ i) {
+                   MNode*  a3 = a2->vectorGet (i);
+                   ++ n;
+                   size_t  j = 0;
+                   for (v = vars.begin (); v < vars.end (); ++ v) {
+                       mlenv->setAry (*v, n, a3->vectorGet (j));
+                       ++ j;
                    }
-                   nextNode (a3);
                }
-               nextNode (a2);
+           } else {
+               while (a2 && a2->isCons ()) {
+                   MNode*  a3 = a2->car ();
+                   ++ n;
+                   for (v = vars.begin (); v < vars.end (); ++ v) {
+                       if (a3 && a3->isCons ()) {
+                           mlenv->setAry (*v, n, a3->car ());
+                       } else {
+                           mlenv->setAry (*v, n, NULL);
+                       }
+                       nextNode (a3);
+                   }
+                   nextNode (a2);
+               }
            }
-           for (v = vars.begin (); v < vars.end (); v ++) {
+           for (v = vars.begin (); v < vars.end (); ++ v) {
                mlenv->setArySize (*v, n);
            }
        } else {