bug fix.
/*DOC:
===json-read===
- (json-read TEXT [#array-is-list | :array-is-cons SYMBOL |#function-symbol]) -> LIST
+ (json-read TEXT [#array-is-list | :array-is-cons SYMBOL | #function-symbol | #texp]) -> LIST
*list mode
scalar_value → Atom
array → List
vector->put (pos, e);
}
+MNode* MNode::vectorDel (size_t pos) {
+ if (type != MC_VECTOR)
+ throw (uErrorWrongType );
+ MNodePtr ans;
+ size_t n = vector->size ();
+ size_t to = pos + 1;
+ ans = vector->get (pos);
+ if (pos < n) {
+ for (; to < n; ++ pos, ++ to) {
+ vector->put (pos, vector->get (to));
+ }
+ vector->resize (pos);
+ }
+ return ans.release ();
+}
+
MNode* MNode::vectorDel (size_t from, size_t to) {
if (type != MC_VECTOR)
throw (uErrorWrongType );
- MNode* ans = vector->get (from);
+ MNodePtr ans;
size_t n = vector->size ();
+ size_t pos;
++ to;
- if (from < to) {
+ ans = newMNode_vector ();
+ for (pos = from; pos < to; ++ pos) {
+ ans ()->vectorPush (vector->get (pos));
+ }
+ if (from < n && from < to) {
for (; to < n; ++ from, ++ to) {
vector->put (from, vector->get (to));
}
vector->resize (from);
}
- return ans;
+ return ans.release ();
+}
+
+void MNode::vectorUnshift (MNode* e) {
+ if (type != MC_VECTOR)
+ throw (uErrorWrongType );
+ vector->unshift (e);
+}
+
+MNode* MNode::vectorShift () {
+ if (type != MC_VECTOR)
+ throw (uErrorWrongType );
+ return vector->shift ();
}
void MNode::vectorPush (MNode* e) {
case MNode::MC_TABLE:
return 0.;
case MNode::MC_STR:
- return atof (str->c_str ());
+ return boost::lexical_cast<double> (*str);
+ case MNode::MC_SYM:
+ return boost::lexical_cast<double> (*sym);
+ case MNode::MC_DOUBLE:
+ return real;
+ default:
+ assert (0);
+ }
+}
+
+int MNode::to_int () {
+ switch (type) {
+ case MNode::MC_NIL:
+ return 0;
+ case MNode::MC_CONS:
+ case MNode::MC_VECTOR:
+ case MNode::MC_TABLE:
+ return 0;
+ case MNode::MC_STR:
+ return boost::lexical_cast<int> (*str);
+ case MNode::MC_SYM:
+ return boost::lexical_cast<int> (*sym);
+ case MNode::MC_DOUBLE:
+ return real;
+ default:
+ assert (0);
+ }
+}
+
+int64_t MNode::to_int64 () {
+ switch (type) {
+ case MNode::MC_NIL:
+ return 0;
+ case MNode::MC_CONS:
+ case MNode::MC_VECTOR:
+ case MNode::MC_TABLE:
+ return 0;
+ case MNode::MC_STR:
+ return boost::lexical_cast<int64_t> (*str);
case MNode::MC_SYM:
- return atof (sym->c_str ());
+ return boost::lexical_cast<int64_t> (*sym);
case MNode::MC_DOUBLE:
return real;
default:
MNode* vectorGet (size_t pos);
size_t vectorSize ();
void vectorPut (size_t pos, MNode* e);
+ MNode* vectorDel (size_t pos);
MNode* vectorDel (size_t from, size_t to);
+ void vectorUnshift (MNode* e);
+ MNode* vectorShift ();
void vectorPush (MNode* e);
MNode* vectorPop ();
void vectorResize (size_t pos);
void tablePut (const ustring& name, MNode* e);
MNode* tableDel (const ustring& name);
double to_double ();
- int to_int () {
- return (int)to_double ();
- };
- int64_t to_int64 () {
- return (int64_t)to_double ();
- };
+ int to_int ();
+ int64_t to_int64 ();
bool to_bool ();
ustring sym_to_string ();
ustring cons_to_texp (bool fcdr = false);
if (ml.top.isCons ()) {
try {
progn_ex (&ml.top, mlenv);
- }
- catch (ustring& msg) {
+ } catch (ustring& msg) {
logMessage (msg);
mlenv->currentCell = NULL;
+ } catch (boost::bad_lexical_cast tmsg) {
+ logMessage (uErrorBadValueN);
+ mlenv->currentCell = NULL;
}
}
} else {
} catch (ustring& msg) {
logMessage (msg);
mlenv->currentCell = NULL;
+ } catch (boost::bad_lexical_cast tmsg) {
+ logMessage (uErrorBadValueN);
+ mlenv->currentCell = NULL;
}
}
} catch (ustring& msg) {
logMessage (msg);
mlenv->currentCell = NULL;
+ } catch (boost::bad_lexical_cast tmsg) {
+ logMessage (uErrorBadValueN);
+ mlenv->currentCell = NULL;
}
}
at (pos) = val;
}
+void MotorVector::unshift (MNode* val) {
+ size_t n = size ();
+ if (n > 0) {
+ push (get (n - 1));
+ for (size_t i = n - 1; i > 0; -- i) {
+ put (i, get (i - 1));
+ }
+ put (0, val);
+ } else {
+ push (val);
+ }
+}
+
+MNode* MotorVector::shift () {
+ size_t n = size ();
+ if (n > 0) {
+ MNode* ans = get (0);
+ for (size_t i = 1; i < n; ++ i) {
+ put (i - 1, get (i));
+ }
+ pop ();
+ return ans;
+ } else {
+ return NULL;
+ }
+}
virtual void push (MNode* val);
virtual MNode* pop ();
- virtual MNode* get (size_type pos); // 1-base
+ virtual MNode* get (size_type pos); // 0-base
virtual void put (size_type pos, MNode* val);
+ virtual void unshift (MNode* val);
+ virtual MNode* shift ();
};
#endif /* MOTORVAR_H */
ustring uErrorInclNest (CharConst ("deeply nested."));
ustring uErrorBadName (CharConst (": bad name."));
ustring uErrorBadParam (CharConst (": bad parameter."));
+ustring uErrorBadValueN (CharConst ("bad value."));
ustring uErrorBadValue (CharConst (": bad value."));
ustring uErrorNotFound (CharConst (": not found."));
ustring uErrorMissingDatastore (CharConst ("missing datastore parameter."));
extern ustring uErrorInclNest;
extern ustring uErrorBadName;
extern ustring uErrorBadParam;
+extern ustring uErrorBadValueN;
extern ustring uErrorBadValue;
extern ustring uErrorNotFound;
extern ustring uErrorMissingDatastore;
env.doMotor ();
}
catch (ustring& msg) {
- std::cerr << msg << "\n";
+ std::cerr << msg << uLF;
goto Ex1;
}
- catch (boost::bad_lexical_cast msg) {
- std::cerr << "bad values\n";
+ catch (boost::bad_lexical_cast tmsg) {
+ std::cerr << uErrorBadValueN << uLF;
goto Ex1;
}
MNode* ml_vector_del (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
MNodePtr vec;
- int from, to;
+ MNodePtr fromobj, toobj;
if (! arg)
throw (uErrorWrongNumber);
vec = eval (arg->car (), mlenv);
nextNodeNonNil (arg);
- from = eval_int (arg->car (), mlenv);
+ fromobj = eval (arg->car (), mlenv);
nextNode (arg);
if (arg) {
- to = eval_int (arg->car (), mlenv);
+ toobj = eval (arg->car (), mlenv);
nextNode (arg);
+ }
+ if (arg)
+ throw (uErrorWrongNumber);
+
+ if (! isVector (vec ()))
+ throw (uErrorWrongType);
+
+ if (isNil (toobj ())) {
+ return vec ()->vectorDel (to_int (fromobj ()));
} else {
- to = from;
+ return vec ()->vectorDel (to_int (fromobj ()), to_int (toobj ()));
}
+}
+
+/*DOC:
+===vector-unshift===
+ (vector-unshift VECTOR VALUE) -> VALUE
+
+*/
+//#AFUNC vector-unshift ml_vector_unshift
+//#WIKIFUNC vector-unshift
+MNode* ml_vector_unshift (MNode* cell, MlEnv* mlenv) {
+ MNode* arg = cell->cdr ();
+ MNodePtr vec;
+ MNodePtr e;
+
+ if (! arg)
+ throw (uErrorWrongNumber);
+ vec = eval (arg->car (), mlenv);
+ nextNodeNonNil (arg);
+ e = eval (arg->car (), mlenv);
+ nextNode (arg);
+ if (arg)
+ throw (uErrorWrongNumber);
+
+ if (! isVector (vec ()))
+ throw (uErrorWrongType);
+
+ vec ()->vectorUnshift (e ());
+ return e.release ();
+}
+
+/*DOC:
+===vector-shift===
+ (vector-shift VECTOR) -> VALUE
+
+*/
+//#AFUNC vector-shift ml_vector_shift
+//#WIKIFUNC vector-shift
+MNode* ml_vector_shift (MNode* cell, MlEnv* mlenv) {
+ MNode* arg = cell->cdr ();
+ MNodePtr vec;
+
+ if (! arg)
+ throw (uErrorWrongNumber);
+ vec = eval (arg->car (), mlenv);
+ nextNode (arg);
if (arg)
throw (uErrorWrongNumber);
if (! isVector (vec ()))
throw (uErrorWrongType);
- return vec ()->vectorDel (from, to);
+ return vec ()->vectorShift ();
}
/*DOC:
MNode* ml_vector_size (MNode* cell, MlEnv* mlenv);
MNode* ml_vector_put (MNode* cell, MlEnv* mlenv);
MNode* ml_vector_del (MNode* cell, MlEnv* mlenv);
+MNode* ml_vector_unshift (MNode* cell, MlEnv* mlenv);
+MNode* ml_vector_shift (MNode* cell, MlEnv* mlenv);
MNode* ml_vector_push (MNode* cell, MlEnv* mlenv);
MNode* ml_vector_pop (MNode* cell, MlEnv* mlenv);
MNode* ml_vector_append (MNode* cell, MlEnv* mlenv);