diff options
Diffstat (limited to 'src/ops/test.cc')
| -rw-r--r-- | src/ops/test.cc | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/ops/test.cc b/src/ops/test.cc new file mode 100644 index 0000000..b6058fc --- /dev/null +++ b/src/ops/test.cc | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <ops/express.h> | ||
| 21 | #include <mt/msmap.h> | ||
| 22 | #include <iostream> | ||
| 23 | #include <fstream> | ||
| 24 | using namespace std; | ||
| 25 | |||
| 26 | static fp_stream mout(stdout),merr(stderr); | ||
| 27 | |||
| 28 | struct tt_env : public op_env { | ||
| 29 | sspile var; | ||
| 30 | tt_env(opmap& map) : op_env(map) {} | ||
| 31 | int split(sref& first,sref& rest) throw(merror_t) { | ||
| 32 | static const charset spec("+-*%/<=>^_!~&|,?:;"); | ||
| 33 | static const charset delim("()\"\\"); | ||
| 34 | static const charset specws=spec|delim|cset_ws; | ||
| 35 | int s; | ||
| 36 | rest=rest.right_first_not_of(cset_ws); | ||
| 37 | if((s=rest.skip(cset_quotes))>0) { | ||
| 38 | first=rest.left(s); | ||
| 39 | rest=rest.right(s); | ||
| 40 | } | ||
| 41 | else if((s=rest.skip(cset_para))>0) { | ||
| 42 | first=Trim(rest.left(s),cset_para); | ||
| 43 | rest=rest.right(s); | ||
| 44 | } | ||
| 45 | else if((s=rest.skip(cset_braces))>0) { | ||
| 46 | first=rest.left(s); | ||
| 47 | rest=rest.right(s); | ||
| 48 | } | ||
| 49 | else if(spec.test(*rest)) { | ||
| 50 | first=rest.left_first_not_of(spec); | ||
| 51 | rest=rest.adv(first); | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | first=rest.left_first_of(specws); | ||
| 55 | rest=rest.adv(first); | ||
| 56 | } | ||
| 57 | return first.nempty(); | ||
| 58 | } | ||
| 59 | void term(mstream& out,expr_t* x) throw(merror_t) { | ||
| 60 | if(x->expr.empty()) { | ||
| 61 | x->status=0; | ||
| 62 | } | ||
| 63 | else if(x->expr.skip(cset_quotes)==x->expr.size()) { | ||
| 64 | out<<x->expr(1,-3); | ||
| 65 | x->status=0; | ||
| 66 | } | ||
| 67 | else if(x->expr.find_not_of(cset_real)<0) { | ||
| 68 | out<<x->expr; | ||
| 69 | x->status=0; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | for(sspile::iterator s=var.begin();s!=var.end();s++) | ||
| 73 | if(s->first==x->expr) { out<<s->second; x->status=OP_LOCAL; break; } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | #define TT_IMPL(n) \ | ||
| 79 | static void impl_##n(mstream& out,tt_env& env,const sref& L,const sref& R) | ||
| 80 | #define TT_ADDN(p,f,n,d) \ | ||
| 81 | M.add(new op_t(#n,0,p,1,2,0,(op_t::op2_t)impl_##f,d)) | ||
| 82 | |||
| 83 | TT_IMPL(assign) { | ||
| 84 | sspile& var=env.var; | ||
| 85 | for(sspile::iterator p=var.begin();p!=var.end();p++) | ||
| 86 | if(p->first==L) { p->second=R; return; } | ||
| 87 | var.push_back(sstring(L,R)); | ||
| 88 | } | ||
| 89 | |||
| 90 | TT_IMPL(incr) { | ||
| 91 | sspile& var=env.var; | ||
| 92 | for(sspile::iterator p=var.begin();p!=var.end();p++) | ||
| 93 | if(p->first==L) { p->second=itoa(atoi(p->second)+atoi(R)); break; } | ||
| 94 | } | ||
| 95 | |||
| 96 | TT_IMPL(decr) { | ||
| 97 | sspile& var=env.var; | ||
| 98 | for(sspile::iterator p=var.begin();p!=var.end();p++) | ||
| 99 | if(p->first==L) { p->second=itoa(atoi(p->second)-atoi(R)); break; } | ||
| 100 | } | ||
| 101 | |||
| 102 | inline void opAddTT(opmap& M) { | ||
| 103 | TT_ADDN(4,assign,=,"C style"); | ||
| 104 | TT_ADDN(4,assign,:=,"new var"); | ||
| 105 | TT_ADDN(4,incr,+=,"C style"); | ||
| 106 | TT_ADDN(4,decr,-=,"C style"); | ||
| 107 | } | ||
| 108 | |||
| 109 | main(int argc,char* argv[]) try | ||
| 110 | { | ||
| 111 | opmap ops; | ||
| 112 | opAddHEXP(ops); | ||
| 113 | opAddTT(ops); | ||
| 114 | tt_env env(ops); | ||
| 115 | mstring buf; | ||
| 116 | sref_t line; | ||
| 117 | expr_t expr; | ||
| 118 | if(argc>1) { | ||
| 119 | buf.load(sref(argv[1])); | ||
| 120 | sref first,rest=buf; | ||
| 121 | Splitln(first,rest); | ||
| 122 | if(first.left(2)!=sref("#!")) rest=buf; | ||
| 123 | (expr=buf).expand(env); | ||
| 124 | expr.eval(mout,env,OP_ALL); | ||
| 125 | mout<<"\n"; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | while(1) { | ||
| 129 | merr<<"ops>"; | ||
| 130 | cin.getline(line,sizeof(sref_t)); | ||
| 131 | sref first,rest(line); | ||
| 132 | Split(first,rest); | ||
| 133 | if(first==sref("exit")) { | ||
| 134 | break; | ||
| 135 | } | ||
| 136 | else if(first==sref("tree")) { | ||
| 137 | expr.trav(mout,12); | ||
| 138 | } | ||
| 139 | else if(first==sref("eval")) { | ||
| 140 | expr.eval(mout,env,OP_ALL); | ||
| 141 | mout<<"\n"; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | buf=line; | ||
| 145 | try { | ||
| 146 | (expr=buf).expand(env); | ||
| 147 | expr.eval(mout,env,OP_ALL); | ||
| 148 | mout<<"\n"; | ||
| 149 | } | ||
| 150 | catch(const merror_t& e) { | ||
| 151 | merr<<e.desc<<"\n"; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | catch(const merror_t& e) { | ||
| 159 | merr<<"ops("<<e.num<<") "<<e.desc<<"\n"; | ||
| 160 | return -1; | ||
| 161 | } | ||
