From 0d53e2fe4e59a37851970c35b5ff3d1f2dfb5935 Mon Sep 17 00:00:00 2001 From: visor Date: Sat, 16 Jan 2010 18:09:18 +0900 Subject: [PATCH] add lisp object reader/writer function. --- lib/ml.cc | 9 ++++++--- lib/ml.h | 3 +-- modules/ml-string.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ modules/ml-string.h | 2 ++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/ml.cc b/lib/ml.cc index 89c9842..80bb8e5 100644 --- a/lib/ml.cc +++ b/lib/ml.cc @@ -474,7 +474,8 @@ bool MotorSexp::scanSexp (int& linenum, uiterator& b, uiterator& e, MNode*& cel if (qcdr) newMNodeCdr (cell); x = new MNode; - mlenv->push_linenum (x, linenum); + if (mlenv) + mlenv->push_linenum (x, linenum); if (! scanCar (linenum, b, e, x)) { status = S_MISSING_PAREN; } @@ -497,7 +498,8 @@ bool MotorSexp::scanSexp (int& linenum, uiterator& b, uiterator& e, MNode*& cel status = S_MISSING_PAREN; Ep1: if (l.isCons ()) { - mlenv->push_linenum (l.cdr (), n); + if (mlenv) + mlenv->push_linenum (l.cdr (), n); cell->set_car (l.cdr ()); } } @@ -508,7 +510,8 @@ bool MotorSexp::scanSexp (int& linenum, uiterator& b, uiterator& e, MNode*& cel newMNodeCdr (cell); x = new MNode; cell->set_car (x); - mlenv->push_linenum (x, linenum); + if (mlenv) + mlenv->push_linenum (x, linenum); scanQuote (linenum, b, e, x); break; case YYREN: diff --git a/lib/ml.h b/lib/ml.h index ed749ce..b7de3dd 100644 --- a/lib/ml.h +++ b/lib/ml.h @@ -37,8 +37,7 @@ class MNode { cons.cdr = NULL; refcount = 0; }; - MNode (bool v) { - }; + MNode (bool v) {}; ~MNode () { fdelete (); }; diff --git a/modules/ml-string.cc b/modules/ml-string.cc index 24437af..40cf830 100644 --- a/modules/ml-string.cc +++ b/modules/ml-string.cc @@ -602,3 +602,48 @@ MNode* ml_random_key (MNode* cell, MlEnv* mlenv) { return newMNode_str (new ustring (randomKey ())); } + +/*DOC: +===to-string=== + (to-string OBJECT) -> STRING + +*/ +//#AFUNC to-string ml_to_string +//#WIKIFUNC to-string +MNode* ml_to_string (MNode* cell, MlEnv* mlenv) { + MNode* arg = cell->cdr (); + ustring text; + + if (! arg) + throw (uErrorWrongNumber); + text = eval_str (arg->car (), mlenv); + nextNode (arg); + if (arg) + throw (uErrorWrongNumber); + + return newMNode_str (new ustring (text)); +} + +/*DOC: +===to-lisp=== + (to-lisp STRING) -> OBJECT + +*/ +//#AFUNC to-lisp ml_to_lisp +//#WIKIFUNC to-lisp +MNode* ml_to_lisp (MNode* cell, MlEnv* mlenv) { + MNode* arg = cell->cdr (); + ustring text; + MotorSexp ml (NULL); + + if (! arg) + throw (uErrorWrongNumber); + text = eval_str (arg->car (), mlenv); + nextNode (arg); + if (arg) + throw (uErrorWrongNumber); + + ml.scan (text); + + return mlenv->retval = ml.top.cdr (); +} diff --git a/modules/ml-string.h b/modules/ml-string.h index 2ed1101..d07ed96 100644 --- a/modules/ml-string.h +++ b/modules/ml-string.h @@ -22,5 +22,7 @@ MNode* ml_pad0 (MNode* cell, MlEnv* mlenv); MNode* ml_ellipsis (MNode* cell, MlEnv* mlenv); MNode* ml_string_format (MNode* cell, MlEnv* mlenv); MNode* ml_random_key (MNode* cell, MlEnv* mlenv); +MNode* ml_to_string (MNode* cell, MlEnv* mlenv); +MNode* ml_to_lisp (MNode* cell, MlEnv* mlenv); #endif /* ML_STRING_H */ -- 2.11.0