OSDN Git Service

fix for FreeBSD 11.1.
[hmh/hhml.git] / lib / mlenv.cc
index d7507b8..605689c 100644 (file)
@@ -5,15 +5,16 @@
 #include "util_string.h"
 #include "config.h"
 #include "motorconst.h"
+#include "motorvar.h"
 #include <boost/ptr_container/ptr_vector.hpp>
 #include <boost/unordered_map.hpp>
 
-void  MlEnv::setStartTime () {
+void  MlPool::setStartTime () {
     starttime = now ();
     limittime = starttime + cDEFAULTTIMELIMIT;
 }
 
-bool  MlEnv::qtimeup () {
+bool  MlPool::qtimeup (std::ostream* log) {
     if (now () > limittime) {
        *log << "timeup\n";
        return true;
@@ -22,24 +23,72 @@ bool  MlEnv::qtimeup () {
     }
 }
 
-void  MlEnv::inclIncCount () {
+void  MlPool::inclIncCount () {
     if (includeCount == kIncludeMax)
        throw (uErrorInclNest);
     includeCount ++;
 }
 
-bool  MlEnv::inclIncCount_nothrow () {
+bool  MlPool::inclIncCount_nothrow () {
     if (includeCount == kIncludeMax)
        return false;
     includeCount ++;
     return true;
 }
 
-void  MlEnv::declIncCount () {
+void  MlPool::declIncCount () {
     assert (includeCount > 0);
     includeCount --;
 }
 
+bool  MlEnv::searchMTable (const ustring& name, FTableVal*& ans) {
+    FTable::iterator  it;
+    int  i;
+
+    if (mlFTable->mtable && (it = mlFTable->mtable->find (name)) != mlFTable->mtable->end ()) {
+       ans = it->second;
+       if (name[0] == '$') {
+           for (i = mlFTable->mstack.size () - 1; i >= 0; i --) {
+               if (mlFTable->mstack[i].mfunc->mlid == ans->mlid) {
+                   // nested error.
+                   throw (name + ": forbidden.");
+                   return false;
+               }
+           }
+       }
+       return true;
+    }
+    return false;
+}
+
+bool  MlEnv::searchSTable (const ustring& name, FTableVal*& ans, MLFunc*& mobj) {
+    int  i;
+    MlFTable::MStackVal*  t;
+    FTable::iterator  it;
+
+    for (i = mlFTable->mstack.size () - 1; i >= 0; i --) {
+       t = &mlFTable->mstack[i];
+       if (t->mobj && t->mfunc->stable && (it = t->mfunc->stable->find (name)) != t->mfunc->stable->end ()) {
+           ans = it->second;
+           mobj = t->mobj;
+           return true;
+       }
+    }
+
+    return false;
+}
+
+bool  MlEnv::searchFTable (const ustring& name, FTableVal*& ans) {
+    FTable::iterator  it;
+
+    if (mlFTable->ftable && (it = mlFTable->ftable->find (name)) != mlFTable->ftable->end ()) {
+       ans = it->second;
+       return true;
+    } else {
+       return false;
+    }
+}
+
 bool  MlEnv::validName (const ustring& name) {
     umatch  m;
     static uregex  re_var (rMOTORVAR);
@@ -51,7 +100,7 @@ void  MlEnv::setGlobalAry (const ustring& name, size_t i, MNode* val) {
     ustring  a (name);
     a.append (uUScore);
     a.append (to_ustring (i));
-    globalVar.setVar (a, val);
+    mlPool->globalVar.setVar (a, val);
 #ifdef DEBUG
     logSetVar (a, val);
 #endif /* DEBUG */
@@ -62,7 +111,7 @@ void  MlEnv::setGlobalArySize (const ustring& name, size_t n) {
     MNode*  val;
     a.append (CharConst ("_n"));
     val = newMNode_num (n);
-    globalVar.setVar (a, val);
+    mlPool->globalVar.setVar (a, val);
 #ifdef DEBUG
     logSetVar (a, val);
 #endif /* DEBUG */
@@ -93,14 +142,14 @@ MNode*  MlEnv::getGlobalAry (const ustring& name, size_t i) {
     ustring  a (name);
     a.append (uUScore);
     a.append (to_ustring (i));
-    return globalVar.getVar (a);
+    return mlPool->globalVar.getVar (a);
 }
 
 size_t  MlEnv::getGlobalArySize (const ustring& name) {
     MNode*  v;
     ustring  a (name);
     a.append (CharConst ("_n"));
-    v = globalVar.getVar (a);
+    v = mlPool->globalVar.getVar (a);
     if (v && v->isReal ()) {
        return (size_t)v->real;
     } else {
@@ -139,7 +188,7 @@ void  MlEnv::setVar (const ustring& name, MNode* val) {
            logSetVar (name, val, true);
 #endif /* DEBUG */
        } else {
-           globalVar.setVar (name, val);
+           mlPool->globalVar.setVar (name, val);
 #ifdef DEBUG
            logSetVar (name, val);
 #endif /* DEBUG */
@@ -161,7 +210,7 @@ void  MlEnv::setVar_nolog (const ustring& name, MNode* val) {
        if (pool) {
            pool->setVar (name, val);
        } else {
-           globalVar.setVar (name, val);
+           mlPool->globalVar.setVar (name, val);
        }
     } else {
        MNodePtr  p;
@@ -185,7 +234,7 @@ void  MlEnv::setVar2 (const ustring& name, MNode* val) {
 void  MlEnv::setAry (const ustring& name, size_t i, MNode* val) {
     MotorVar*  pool;
 
-    if (validName (name) && i >= 0) {
+    if (validName (name)) {
        pool = findLocal (name);
        if (pool) {
            setLocalAry (pool, name, i, val);
@@ -233,9 +282,14 @@ void  MlEnv::setAry (const ustring& name, MNode* list) {
                    setLocalAry (pool, name, n, list->car ());
                    nextNode (list);
                }
+           } else if (list->isVector ()) {
+               MotorVector::iterator  b = list->vector->begin ();
+               MotorVector::iterator  e = list->vector->end ();
+               for (; b < e; ++ b) {
+                   ++ n;
+                   setLocalAry (pool, name, n, (*b) ());
+               }
            } else {
-//             n ++;
-//             setLocalAry (pool, name, n, list);
                throw (ustring (CharConst ("setting a scalar value to an array.")));
            }
            setLocalArySize (pool, name, n);
@@ -247,9 +301,14 @@ void  MlEnv::setAry (const ustring& name, MNode* list) {
                    setGlobalAry (name, n, list->car ());
                    nextNode (list);
                }
+           } else if (list->isVector ()) {
+               MotorVector::iterator  b = list->vector->begin ();
+               MotorVector::iterator  e = list->vector->end ();
+               for (; b < e; ++ b) {
+                   ++ n;
+                   setGlobalAry (name, n, (*b) ());
+               }
            } else {
-//             n ++;
-//             setGlobalAry (name, n, list);
                throw (ustring (CharConst ("setting a scalar value to an array.")));
            }
            setGlobalArySize (name, n);
@@ -270,7 +329,7 @@ MNode*  MlEnv::getVar (const ustring& name) {
        if (pool) {
            return (pool->getVar (name));
        } else {
-           return (globalVar.getVar (name));
+           return (mlPool->globalVar.getVar (name));
        }
     } else {
        throw (padEmpty (name) + uErrorBadName);
@@ -290,13 +349,11 @@ MNode*  MlEnv::getAry (const ustring& name, size_t i) {
     MotorVar*  pool;
 
     if (validName (name)) {
-       if (i >= 0) {
-           pool = findLocal (name);
-           if (pool) {
-               return getLocalAry (pool, name, i);
-           } else {
-               return getGlobalAry (name, i);
-           }
+       pool = findLocal (name);
+       if (pool) {
+           return getLocalAry (pool, name, i);
+       } else {
+           return getGlobalAry (name, i);
        }
     } else {
        throw (padEmpty (name) + uErrorBadName);
@@ -330,11 +387,11 @@ size_t  MlEnv::getArySize (const ustring& name) {
 
 void  MlEnv::beginLocal () {
     MotorVar*  v = new MotorVar;
-    localVar.push_back (v);
+    mlPool->localVar.push_back (v);
 }
 
 void  MlEnv::setLocalVar (const ustring& name, MNode* val) {
-    MotorVar*  pool = &localVar.back ();
+    MotorVar*  pool = &mlPool->localVar.back ();
     if (validName (name)) {
        pool->setVar (name, val);
 #ifdef DEBUG
@@ -350,21 +407,21 @@ void  MlEnv::setLocalVar (const ustring& name, MNode* val) {
 }
 
 void  MlEnv::defineLocalVar (const ustring& name) {
-    MotorVar*  pool = &localVar.back ();
+    MotorVar*  pool = &mlPool->localVar.back ();
     if (validName (name)) {
        pool->setVar (name, NULL);
     }
 }
 
 void  MlEnv::endLocal () {
-    localVar.pop_back ();
+    mlPool->localVar.pop_back ();
 }
 
 MotorVar*  MlEnv::findLocal (const ustring& name) {
     boost::ptr_vector<MotorVar>::reverse_iterator  t;
     MotorVar::iterator  it;
 
-    for (t = localVar.rbegin (); t != localVar.rend (); t ++) {
+    for (t = mlPool->localVar.rbegin (); t != mlPool->localVar.rend (); t ++) {
        it = t->find (name);
        if (it != t->end ()) {
            return &*t;
@@ -375,9 +432,12 @@ MotorVar*  MlEnv::findLocal (const ustring& name) {
 
 #ifdef DEBUG
 void  MlEnv::logSetVar (const ustring& name, MNode* val, bool flocal) {
-    if (! log)
+    if (! log || mlPool->nolog)
        return;
-    *log << "  [";
+    *log << "        ";
+    for (int i = 0; i < nestCount; i ++)
+       *log << "|  ";
+    *log << "[";
     if (flocal)
        *log << "*";
     *log <<  name << " <= ";
@@ -391,7 +451,9 @@ void  MlEnv::logSetVar (const ustring& name, MNode* val, bool flocal) {
 void  MlEnv::logSetVarError (const ustring& name, MNode* val) {
     if (! log)
        return;
-    *log << "  error: [";
+    for (int i = 0; i < nestCount; i ++)
+       *log << "   ";
+    *log << "  error: [";
     *log <<  name << " <= ";
     if (val)
        *log << val->dump_string_short ();
@@ -401,7 +463,9 @@ void  MlEnv::logSetVarError (const ustring& name, MNode* val) {
 void  MlEnv::logSetAryError (const ustring& name, size_t i, MNode* val) {
     if (! log)
        return;
-    *log << "  error: [" <<  name << uUScore << i << " <= ";
+    for (int i = 0; i < nestCount; i ++)
+       *log << "   ";
+    *log << "  error: [" <<  name << uUScore << i << " <= ";
     if (val)
        *log << val->dump_string_short ();
     *log << "]\n";
@@ -410,34 +474,45 @@ void  MlEnv::logSetAryError (const ustring& name, size_t i, MNode* val) {
 void  MlEnv::logSetArySizeError (const ustring& name, size_t n) {
     if (! log)
        return;
-    *log << "  [" <<  name << "_n" << " <= " << n << "]\n";
+    *log << "        ";
+    for (int i = 0; i < nestCount; i ++)
+       *log << "|  ";
+    *log << "[" <<  name << "_n" << " <= " << n << "]\n";
 }
 #endif
 
 void  MlEnv::push_linenum (MNode* c, int ln) {
-    linenum.insert (std::pair<MNode*, int> (c, ln));
+    mlPool->linenum.insert (std::pair<MNode*, int> (c, ln));
 }
 
 void  MlEnv::logLinenum (MNode* c) {
     boost::unordered_map<MNode*, int>::iterator  i;
 
-    i = linenum.find (c);
-    if (i == linenum.end ()) {
+    i = mlPool->linenum.find (c);
+    if (i == mlPool->linenum.end ()) {
        *log << "<none>: ";
     } else {
-       *log << i->second << ": ";
+       if (i->second < 10)
+           *log << " ";
+       if (i->second < 100)
+           *log << " ";
+       if (i->second < 1000)
+           *log << " ";
+       *log << i->second << ":   ";
     }
 }
 
 void  MlEnv::logSexp (MNode* c) {
     int  i;
-    if (log && c && c->isCons ()) {
-       for (i = 0; i < includeCount; i ++)
-           *log << ":";
+
+    if (!mlPool->nolog && log && c && c->isCons ()) {
+//     for (i = 0; i < mlPool->includeCount; i ++)
+//         *log << ":";
        logLinenum (c);
-//     *log << c->dump_string_short () << "\n";
+       for (i = 0; i < nestCount; i ++)
+           *log << "|  ";
        if (c->car () && c->car ()->isSym ()
-           && (match (*c->car ()->sym, CharConst ("defun")) || matchHead (*c->car ()->sym, CharConst ("defun-")))
+           && (match (*ptr_symbol (c->car ()), CharConst ("defun")) || matchHead (*ptr_symbol (c->car ()), CharConst ("defun-")))
            && c->cdr () && c->cdr ()->isCons ()
            && c->cdr ()->cdr () && c->cdr ()->cdr ()->isCons ()) {
            *log << "(" << c->car ()->dump_string ();
@@ -458,9 +533,17 @@ void  MlEnv::logSexp (MNode* c) {
 }
 
 void  MlEnv::setMStack (MLFunc* mobj) {
-    MStackVal*  t = &mstack.back ();
+    MlFTable::MStackVal*  t = &mlFTable->mstack.back ();
 
     assert (t->mobj == NULL);
     t->mobj = mobj;
 }
 
+void  MlEnv::execDatastoreFunc () {
+    int  i;
+
+    for (i = 0; i < datastoreFuncStack.size (); i ++) {
+       datastoreFuncStack[i].second (datastoreFuncStack[i].first);
+    }
+}
+