From c39633e7aceeb72cfe3a3963c399eeba4d9c4838 Mon Sep 17 00:00:00 2001 From: visor Date: Sat, 11 Apr 2015 16:37:50 +0900 Subject: [PATCH] dotable wiki command, eval wiki inline function. --- wiki/wikicmd.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- wiki/wikicmd.h | 1 + wiki/wikiline.cc | 38 +++++++++++++++++++++++++++++++++++ wiki/wikiline.h | 1 + 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/wiki/wikicmd.cc b/wiki/wikicmd.cc index 6849164..fee9331 100644 --- a/wiki/wikicmd.cc +++ b/wiki/wikicmd.cc @@ -243,7 +243,7 @@ void wc_dovector (WikiLine* wl, WikiFormat* wiki) { } do_linevec (wl->block, wiki); } - for (it = 0; it < iu; ++ it) { + for (it = 0; it < iu; ++ it) { // 書き戻す wiki->mlenv->setVar (lv[it], lvv[it] ()); } } @@ -251,6 +251,65 @@ void wc_dovector (WikiLine* wl, WikiFormat* wiki) { /* ============================================================ */ /*DOC: +===$dotable=== + $dotable:''VARKEY'',''VARVAL'':''TABLEVAR'' + ... + $enddotable + +テーブル''TABLEVAR''の各要素のキーと値ををそれぞれ変数''VAR1'', ''VAR2''に代入しながら,$enddotableまでのブロックを繰り返し実行する。 + +*/ +//#WIKICMD $dotable $enddotable wc_dotable +void wc_dotable (WikiLine* wl, WikiFormat* wiki) { + WikiMotor motor (wl->begin, wl->end, wiki); + WikiMotorObjVec objv; + WikiMotorObjVecVec args; + ustring vkey, vval; + MNodePtr tbl; + ustring s; + +#ifdef WIKIREPEATPROTECT + if (wiki->protectMode && ! wl->fsuper) { +#ifdef DEBUG + std::cerr << "(wiki):(protected)\n"; +#endif /* DEBUG */ + return; + } +#endif + +#ifdef DEBUG + std::cerr << "(wiki):" << ustring (wl->begin0, wl->end) << "\n"; +#endif /* DEBUG */ + motor.compile (objv); + objv.splitCharA (':', args); + if (args.size () != 2) { + wiki->errorMsg.append (CharConst ("$dovector: wrong number of parameters.\n")); + return; + } + WikiMotorObjVecVec v; + args[0]->splitCharA (',', v); + if (0 < v.size ()) + vkey = clipWhite (v[0]->textOut (wiki)); + if (1 < v.size ()) + vval = clipWhite (v[1]->textOut (wiki)); + s = clipWhite (args[1]->textOut (wiki)); + tbl = wiki->getVar (s); + if (! isTable (tbl ())) { + wiki->errorMsg.append (CharConst ("$dovector: bad value type.\n")); + return; + } + + MotorVar::iterator b = tbl ()->table->begin (); + MotorVar::iterator t = tbl ()->table->end (); + for (; b != t; ++ b) { + wiki->mlenv->setVar (vkey, newMNode_str (new ustring ((*b).first))); // テーブルのキーは、文字列 + wiki->mlenv->setVar (vval, (*b).second ()); + do_linevec (wl->block, wiki); + } +} + +/* ============================================================ */ +/*DOC: ===$block=== $block:''EXPR'' ... diff --git a/wiki/wikicmd.h b/wiki/wikicmd.h index e02fd69..d6e3012 100644 --- a/wiki/wikicmd.h +++ b/wiki/wikicmd.h @@ -10,6 +10,7 @@ class WikiFormat; void wc_repeat (WikiLine* wl, WikiFormat* wiki); void wc_doarray (WikiLine* wl, WikiFormat* wiki); void wc_dovector (WikiLine* wl, WikiFormat* wiki); +void wc_dotable (WikiLine* wl, WikiFormat* wiki); void wc_block (WikiLine* wl, WikiFormat* wiki); void wc_setvar (WikiLine* wl, WikiFormat* wiki); void wc_eval (WikiLine* wl, WikiFormat* wiki); diff --git a/wiki/wikiline.cc b/wiki/wikiline.cc index 788acb6..a6ff1aa 100644 --- a/wiki/wikiline.cc +++ b/wiki/wikiline.cc @@ -281,6 +281,44 @@ bool wl_table (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki return true; } +/*DOC: +===Lispファンクション実行=== + [[eval:FUNC]] + +*/ +//#WIKILINE eval wl_eval +bool wl_eval (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki) { +#ifdef WIKIEVALPROTECT + if (wiki->protectMode && ! wl->fsuper) { + return false; + } +#endif + + MotorTexp ml (wiki->mlenv); + ustring sexp = arg2->textOut (wiki); + MNodePtr v; + + if (! sexp.empty ()) { + ml.scan (sexp); + if (ml.top.isCons ()) { + MNode* arg = ml.top.cdr (); + if (arg && arg->isCons ()) { + try { + v = eval (arg->car (), wiki->mlenv); + MotorOutputWiki o (out); + o.outamp (to_string (v ())); + } catch (ustring& msg) { + if (wiki->mlenv->currentCell ()) { + wiki->errorMsg.append (wiki->mlenv->currentCell ()->dump_string_short ()).append (CharConst (": ")); + } + wiki->errorMsg.append (msg).append (uLF); + } + } + } + } + return true; +} + /* ============================================================ */ /*DOC: ===強調=== diff --git a/wiki/wikiline.h b/wiki/wikiline.h index 327056a..3ea4af7 100644 --- a/wiki/wikiline.h +++ b/wiki/wikiline.h @@ -13,6 +13,7 @@ bool wl_car (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki); bool wl_cdr (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki); bool wl_join (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki); bool wl_table (WikiMotorObjVecVec* args, WikiMotorObjVec& out, WikiFormat* wiki); +bool wl_eval (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki); bool wl_italic (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki); bool wl_bold (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki); bool wl_bolditalic (WikiMotorObjVec* arg2, WikiMotorObjVec& out, WikiFormat* wiki); -- 2.11.0