diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/ops/basic.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/ops/basic.cc')
| -rw-r--r-- | src/ops/basic.cc | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/src/ops/basic.cc b/src/ops/basic.cc new file mode 100644 index 0000000..b30e351 --- /dev/null +++ b/src/ops/basic.cc | |||
| @@ -0,0 +1,269 @@ | |||
| 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/operator.h> | ||
| 21 | #include <math.h> | ||
| 22 | |||
| 23 | OP1_IMPL(exp) { out<<exp(atof(a)); } | ||
| 24 | OP1_IMPL(ln) { out<<log(atof(a)); } | ||
| 25 | OP1_IMPL(abs) { out<<fabs(atof(a)); } | ||
| 26 | OP1_IMPL(int) { out<<int(atof(a)); } | ||
| 27 | |||
| 28 | OP1_IMPL(minus) { out<<-atof(a); } | ||
| 29 | OP2_IMPL(minus) { out<<atof(L)-atof(R); } | ||
| 30 | OP1_IMPL(plus) { out<<atof(a); } | ||
| 31 | OP2_IMPL(plus) { out<<atof(L)+atof(R); } | ||
| 32 | OP2_IMPL(mul) { out<<atof(L)*atof(R); } | ||
| 33 | OP2_IMPL(divide) { out<<atof(L)/atof(R); } | ||
| 34 | OP2_IMPL(mod) { | ||
| 35 | double a=atof(L),b=atof(R); | ||
| 36 | a-=b*int(a/b); | ||
| 37 | if(a<0) a+=fabs(b); | ||
| 38 | out<<a; | ||
| 39 | } | ||
| 40 | OP2_IMPL(imod) { out<<(atoi(L)%atoi(R)); } | ||
| 41 | OP2_IMPL(div) { out<<(atoi(L)/atoi(R)); } | ||
| 42 | OP2_IMPL(pow) { out<<pow(atof(L),atof(R)); } | ||
| 43 | |||
| 44 | OP2_IMPL(eq) {out<<(L==R); } | ||
| 45 | OP2_IMPL(ne) {out<<(L!=R);} | ||
| 46 | OP2_IMPL(eqn) {out<<(L.compare(R,cset_lcase)==0);} | ||
| 47 | OP2_IMPL(nen) {out<<(L.compare(R,cset_lcase)!=0);} | ||
| 48 | OP2_IMPL(eqf) {out<<(atof(L)==atof(R));} | ||
| 49 | OP2_IMPL(nef) {out<<(atof(L)!=atof(R));} | ||
| 50 | |||
| 51 | OP1_IMPL(not) {out<<!atob(a);} | ||
| 52 | OP2_IMPL(and) {out<<(atob(L)&atob(R)); } | ||
| 53 | OP2_IMPL(or) {out<<(atob(L)|atob(R)); } | ||
| 54 | OP2_IMPL(xor) {out<<(atob(L)^atob(R)); } | ||
| 55 | |||
| 56 | OP2_IMPL(shiftleft) {out<<(atoi(L)<<atoi(R));} | ||
| 57 | OP2_IMPL(shiftright) {out<<(atoi(L)>>atoi(R));} | ||
| 58 | |||
| 59 | OP1_IMPL(bitnot) {out<<~atoi(a);} | ||
| 60 | OP2_IMPL(bitand) {out<<(atoi(L)&atoi(R)); } | ||
| 61 | OP2_IMPL(bitor) {out<<(atoi(L)|atoi(R)); } | ||
| 62 | OP2_IMPL(bitxor) {out<<(atoi(L)^atoi(R)); } | ||
| 63 | |||
| 64 | OP2_IMPL(SP) {out<<L<<" "<<R;} | ||
| 65 | |||
| 66 | OP2_IMPL(comma) {out<<R;} | ||
| 67 | |||
| 68 | OP1_IMPL(semi) {} | ||
| 69 | OP2_IMPL(semi) {} | ||
| 70 | |||
| 71 | void opAddBasic(opmap& M) | ||
| 72 | { | ||
| 73 | OP_ADD(32,exp, | ||
| 74 | "C style\n" | ||
| 75 | "Natural exponential function\n" | ||
| 76 | "Syntax: exp (real)\n" | ||
| 77 | "Returns: (real)\n" | ||
| 78 | ); | ||
| 79 | OP_ADD(32,ln, | ||
| 80 | "C style\n" | ||
| 81 | "Natural logarithm function\n" | ||
| 82 | "Syntax: ln (real)\n" | ||
| 83 | "Returns: (real)\n" | ||
| 84 | ); | ||
| 85 | OP_ADD(32,abs, | ||
| 86 | "C style\n" | ||
| 87 | "Absolute value\n" | ||
| 88 | "Syntax: abs (real)\n" | ||
| 89 | "Returns: (real)\n" | ||
| 90 | ); | ||
| 91 | OP_ADD(32,int, | ||
| 92 | "C style\n" | ||
| 93 | "Integer truncation\n" | ||
| 94 | "Syntax: int (real)\n" | ||
| 95 | "Returns: (int)\n" | ||
| 96 | "\n" | ||
| 97 | "Note that truncation is just as when converting to int, not rounding\n" | ||
| 98 | ); | ||
| 99 | |||
| 100 | OP_ADDN(30,bitnot,~, | ||
| 101 | "C style\n" | ||
| 102 | "Bitwise NOT\n" | ||
| 103 | "Syntax: ~ (int)\n" | ||
| 104 | "Returns: (int)\n" | ||
| 105 | ); | ||
| 106 | OP_ADDN(30,not,!, | ||
| 107 | "C style\n" | ||
| 108 | "Logical NOT\n" | ||
| 109 | "Syntax: ! (int)\n" | ||
| 110 | "Returns: (int)\n" | ||
| 111 | ); | ||
| 112 | OP_ADD2(30,24,plus,+, | ||
| 113 | "C style\n" | ||
| 114 | "Addition\n" | ||
| 115 | "Syntax: (real) + (real)\n" | ||
| 116 | "Returns: (real)\n" | ||
| 117 | ); | ||
| 118 | OP_ADD2(30,24,minus,-, | ||
| 119 | "C style\n" | ||
| 120 | "Subtraction\n" | ||
| 121 | "Syntax: (real) - (real)\n" | ||
| 122 | "Returns: (real)\n" | ||
| 123 | ); | ||
| 124 | |||
| 125 | OP_ADDN(27,pow,**, | ||
| 126 | "Power\n" | ||
| 127 | "Syntax: (real) a ** (real) b\n" | ||
| 128 | "Returns: (real) a to the power of b\n" | ||
| 129 | ); | ||
| 130 | |||
| 131 | OP_ADDN(26,mul,*, | ||
| 132 | "C style\n" | ||
| 133 | "Multiplication\n" | ||
| 134 | "Syntax: (real) * (real)\n" | ||
| 135 | "Returns: (real)\n" | ||
| 136 | ); | ||
| 137 | OP_ADDN(26,divide,/, | ||
| 138 | "C style\n" | ||
| 139 | "Division\n" | ||
| 140 | "Syntax: (real) / (real)\n" | ||
| 141 | "Returns: (real)\n" | ||
| 142 | "\n" | ||
| 143 | "Note computerese; same precedence as *\n" | ||
| 144 | ); | ||
| 145 | OP_ADDN(26,imod,%, | ||
| 146 | "C style\n" | ||
| 147 | "Integer modulus\n" | ||
| 148 | "Syntax: (int) % (int)\n" | ||
| 149 | "Returns: (int)\n" | ||
| 150 | ); | ||
| 151 | |||
| 152 | OP_ADD(25,div, | ||
| 153 | "Integer division\n" | ||
| 154 | "Syntax: (int) div (int)\n" | ||
| 155 | "Returns: (int)\n" | ||
| 156 | "\n" | ||
| 157 | "Note that this is the same as integer / in C, but with lower precedence\n" | ||
| 158 | ); | ||
| 159 | // plus/minus 24 | ||
| 160 | OP_ADD(23,mod, | ||
| 161 | "General moulus\n" | ||
| 162 | "Syntax: (real) mod (real)\n" | ||
| 163 | "Returns: (real)\n" | ||
| 164 | "\n" | ||
| 165 | "Modulus on ring; negative vals are mapped onto base\n" | ||
| 166 | ); | ||
| 167 | |||
| 168 | OP_ADDN(22,shiftleft,<<, | ||
| 169 | "C style\n" | ||
| 170 | "Shift left\n" | ||
| 171 | "Syntax: (int) << (int)\n" | ||
| 172 | "Returns: (int)\n" | ||
| 173 | ); | ||
| 174 | OP_ADDN(22,shiftright,>>, | ||
| 175 | "C style\n" | ||
| 176 | "Shift right\n" | ||
| 177 | "Syntax: (int) >> (int)\n" | ||
| 178 | "Returns: (int)\n" | ||
| 179 | ); | ||
| 180 | |||
| 181 | OP_ADD(18,eq, | ||
| 182 | "Lexicographic equal\n" | ||
| 183 | "Syntax: (string) eq (string)\n" | ||
| 184 | "Returns: (int)\n" | ||
| 185 | ); | ||
| 186 | OP_ADD(18,eqn, | ||
| 187 | "Lexicographic equal, case insensitive\n" | ||
| 188 | "Syntax: (string) eqn (string)\n" | ||
| 189 | "Returns: (int)\n" | ||
| 190 | ); | ||
| 191 | OP_ADDN(18,eqf,==, | ||
| 192 | "Arithmetic equal\n" | ||
| 193 | "Syntax: (real) == (real)\n" | ||
| 194 | "Returns: (int)\n" | ||
| 195 | ); | ||
| 196 | OP_ADD(18,ne, | ||
| 197 | "Lexicographic not equal\n" | ||
| 198 | "Syntax: (string) ne (string)\n" | ||
| 199 | "Returns: (int)\n" | ||
| 200 | ); | ||
| 201 | OP_ADD(18,nen, | ||
| 202 | "Lexicographic not equal, case insensitive\n" | ||
| 203 | "Syntax: (string) nen (string)\n" | ||
| 204 | "Returns: (int)\n" | ||
| 205 | ); | ||
| 206 | OP_ADDN(18,nef,!=, | ||
| 207 | "Arithmetic not equal\n" | ||
| 208 | "Syntax: (real) != (real)\n" | ||
| 209 | "Returns: (int)\n" | ||
| 210 | ); | ||
| 211 | |||
| 212 | OP_ADDN(16,bitand,&, | ||
| 213 | "C style\n" | ||
| 214 | "Bitwise AND\n" | ||
| 215 | "Syntax: (int) & (int)\n" | ||
| 216 | "Returns: (int)\n" | ||
| 217 | ); | ||
| 218 | OP_ADDN(14,bitxor,^, | ||
| 219 | "C style\n" | ||
| 220 | "Bitwise XOR\n" | ||
| 221 | "Syntax: (int) ^ (int)\n" | ||
| 222 | "Returns: (int)\n" | ||
| 223 | ); | ||
| 224 | OP_ADDN(12,bitor,|, | ||
| 225 | "C style\n" | ||
| 226 | "Bitwise OR\n" | ||
| 227 | "Syntax: (int) | (int)\n" | ||
| 228 | "Returns: (int)\n" | ||
| 229 | ); | ||
| 230 | |||
| 231 | OP_ADDN(10,and,&&, | ||
| 232 | "C style\n" | ||
| 233 | "Logical AND\n" | ||
| 234 | "Syntax: (int) && (int)\n" | ||
| 235 | "Returns: (int)\n" | ||
| 236 | ); | ||
| 237 | OP_ADDN(9,xor,^^, | ||
| 238 | "Logical XOR\n" | ||
| 239 | "Syntax: (int) ^^ (int)\n" | ||
| 240 | "Returns: (int)\n" | ||
| 241 | ); | ||
| 242 | OP_ADDN(8,or,||, | ||
| 243 | "C style\n" | ||
| 244 | "Logical OR\n" | ||
| 245 | "Syntax: (int) || (int)\n" | ||
| 246 | "Returns: (int)\n" | ||
| 247 | ); | ||
| 248 | |||
| 249 | /* | ||
| 250 | OP_ADD(3,SP, | ||
| 251 | "Whitespace padding\n" | ||
| 252 | "Syntax: a ws OR ws a OR a ws b\n" | ||
| 253 | "Returns: a b\n" | ||
| 254 | ); | ||
| 255 | */ | ||
| 256 | |||
| 257 | M.add(new op_t(",",2,impl_comma, | ||
| 258 | "C style\n" | ||
| 259 | "Separate expression\n" | ||
| 260 | "Syntax: a , b\n" | ||
| 261 | "Returns: b\n" | ||
| 262 | )); | ||
| 263 | OP_ADD2(1,1,semi,;, | ||
| 264 | "C style\n" | ||
| 265 | "End expression\n" | ||
| 266 | "Syntax: a ; OR a ; b\n" | ||
| 267 | "Returns: nothing\n" | ||
| 268 | ); | ||
| 269 | } | ||
