#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;
}
}
-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);
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 */
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 */
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 {
logSetVar (name, val, true);
#endif /* DEBUG */
} else {
- globalVar.setVar (name, val);
+ mlPool->globalVar.setVar (name, val);
#ifdef DEBUG
logSetVar (name, val);
#endif /* DEBUG */
if (pool) {
pool->setVar (name, val);
} else {
- globalVar.setVar (name, val);
+ mlPool->globalVar.setVar (name, val);
}
} else {
MNodePtr p;
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);
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);
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);
if (pool) {
return (pool->getVar (name));
} else {
- return (globalVar.getVar (name));
+ return (mlPool->globalVar.getVar (name));
}
} else {
throw (padEmpty (name) + uErrorBadName);
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);
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
}
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;
#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 << " <= ";
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 ();
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";
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 ();
}
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);
+ }
+}
+