core/parser/expression.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Avisynth v2.5. Copyright 2002 Ben Rudiak-Gould et al. | ||
| 2 | // http://avisynth.nl | ||
| 3 | |||
| 4 | // This program is free software; you can redistribute it and/or modify | ||
| 5 | // it under the terms of the GNU General Public License as published by | ||
| 6 | // the Free Software Foundation; either version 2 of the License, or | ||
| 7 | // (at your option) any later version. | ||
| 8 | // | ||
| 9 | // This program is distributed in the hope that it will be useful, | ||
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | // GNU General Public License for more details. | ||
| 13 | // | ||
| 14 | // You should have received a copy of the GNU General Public License | ||
| 15 | // along with this program; if not, write to the Free Software | ||
| 16 | // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit | ||
| 17 | // http://www.gnu.org/copyleft/gpl.html . | ||
| 18 | // | ||
| 19 | // Linking Avisynth statically or dynamically with other modules is making a | ||
| 20 | // combined work based on Avisynth. Thus, the terms and conditions of the GNU | ||
| 21 | // General Public License cover the whole combination. | ||
| 22 | // | ||
| 23 | // As a special exception, the copyright holders of Avisynth give you | ||
| 24 | // permission to link Avisynth with independent modules that communicate with | ||
| 25 | // Avisynth solely through the interfaces defined in avisynth.h, regardless of the license | ||
| 26 | // terms of these independent modules, and to copy and distribute the | ||
| 27 | // resulting combined work under terms of your choice, provided that | ||
| 28 | // every copy of the combined work is accompanied by a complete copy of | ||
| 29 | // the source code of Avisynth (the version of Avisynth used to produce the | ||
| 30 | // combined work), being distributed under the terms of the GNU General | ||
| 31 | // Public License plus this exception. An independent module is a module | ||
| 32 | // which is not derived from or based on Avisynth, such as 3rd-party filters, | ||
| 33 | // import and export plugins, or graphical user interfaces. | ||
| 34 | |||
| 35 | #ifndef __Expression_H__ | ||
| 36 | #define __Expression_H__ | ||
| 37 | |||
| 38 | #include <avisynth.h> | ||
| 39 | #include "../function.h" | ||
| 40 | |||
| 41 | #include <vector> | ||
| 42 | #include <atomic> | ||
| 43 | |||
| 44 | /******************************************************************** | ||
| 45 | ********************************************************************/ | ||
| 46 | |||
| 47 | |||
| 48 | struct ReturnExprException | ||
| 49 | { | ||
| 50 | AVSValue value; | ||
| 51 | }; | ||
| 52 | |||
| 53 | /**** Base Classes ****/ | ||
| 54 | |||
| 55 | class Expression { | ||
| 56 | public: | ||
| 57 | 18 | Expression() : refcnt(0) {} | |
| 58 | virtual AVSValue Evaluate(IScriptEnvironment* env) = 0; | ||
| 59 | ✗ | virtual const char* GetLvalue() { return 0; } | |
| 60 | 18 | virtual ~Expression() {} | |
| 61 | |||
| 62 | private: | ||
| 63 | friend class PExpression; | ||
| 64 | std::atomic<int> refcnt; | ||
| 65 | 105 | void AddRef() { ++refcnt; } | |
| 66 |
3/4✓ Branch 3 → 4 taken 18 times.
✓ Branch 3 → 6 taken 87 times.
✓ Branch 4 → 5 taken 18 times.
✗ Branch 4 → 6 not taken.
|
105 | void Release() { if (--refcnt <= 0) delete this; } |
| 67 | }; | ||
| 68 | |||
| 69 | class PExpression | ||
| 70 | { | ||
| 71 | public: | ||
| 72 | 132 | PExpression() { Init(0); } | |
| 73 | 18 | PExpression(Expression* p) { Init(p); } | |
| 74 | 72 | PExpression(const PExpression& p) { Init(p.e); } | |
| 75 | 6 | PExpression& operator=(Expression* p) { Set(p); return *this; } | |
| 76 | 15 | PExpression& operator=(const PExpression& p) { Set(p.e); return *this; } | |
| 77 | ✗ | int operator!() const { return !e; } | |
| 78 | 156 | operator void*() const { return e; } | |
| 79 | 18 | Expression* operator->() const { return e; } | |
| 80 | 222 | ~PExpression() { Release(); } | |
| 81 | |||
| 82 | private: | ||
| 83 | Expression* e; | ||
| 84 |
2/2✓ Branch 2 → 3 taken 84 times.
✓ Branch 2 → 4 taken 138 times.
|
222 | void Init(Expression* p) { e=p; if (e) e->AddRef(); } |
| 85 |
3/4✓ Branch 2 → 3 taken 21 times.
✗ Branch 2 → 4 not taken.
✓ Branch 4 → 5 taken 9 times.
✓ Branch 4 → 6 taken 12 times.
|
21 | void Set(Expression* p) { if (p) p->AddRef(); if (e) e->Release(); e=p; } |
| 86 |
2/2✓ Branch 2 → 3 taken 96 times.
✓ Branch 2 → 4 taken 126 times.
|
222 | void Release() { if (e) e->Release(); } |
| 87 | }; | ||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | /**** Object classes ****/ | ||
| 96 | |||
| 97 | class ExpRootBlock : public Expression | ||
| 98 | { | ||
| 99 | public: | ||
| 100 |
1/2✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 5 not taken.
|
6 | ExpRootBlock(const PExpression& e) : exp(e) {} |
| 101 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 102 | |||
| 103 | private: | ||
| 104 | const PExpression exp; | ||
| 105 | }; | ||
| 106 | |||
| 107 | class ExpConstant : public Expression | ||
| 108 | { | ||
| 109 | public: | ||
| 110 | ExpConstant(std::vector<AVSValue>* v) : val(v->data(), (int)(v->size())) {} // array of AVSValue* | ||
| 111 | ✗ | ExpConstant(AVSValue v) : val(v) {} | |
| 112 |
1/2✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 5 not taken.
|
2 | ExpConstant(int i) : val(i) {} |
| 113 | ✗ | ExpConstant(int64_t i) : val(i) {} | |
| 114 | ExpConstant(float f) : val(f) {} | ||
| 115 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
|
1 | ExpConstant(double f) : val(f) {} |
| 116 | ✗ | ExpConstant(const char* s) : val(s) {} | |
| 117 | 3 | virtual AVSValue Evaluate(IScriptEnvironment* env) { | |
| 118 | AVS_UNUSED(env); | ||
| 119 | 3 | return val; } | |
| 120 | |||
| 121 | private: | ||
| 122 | friend class ExpNegative; | ||
| 123 | const AVSValue val; | ||
| 124 | }; | ||
| 125 | |||
| 126 | |||
| 127 | class ExpSequence : public Expression | ||
| 128 | { | ||
| 129 | public: | ||
| 130 | ✗ | ExpSequence(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 131 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 132 | private: | ||
| 133 | const PExpression a, b; | ||
| 134 | }; | ||
| 135 | |||
| 136 | |||
| 137 | class ExpExceptionTranslator : public Expression | ||
| 138 | { | ||
| 139 | public: | ||
| 140 |
1/2✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 5 not taken.
|
6 | ExpExceptionTranslator(const PExpression& _exp) : exp(_exp) {} |
| 141 | AVSValue Evaluate(IScriptEnvironment* env); | ||
| 142 | |||
| 143 | private: | ||
| 144 | const PExpression exp; | ||
| 145 | void TrapEval(AVSValue&, unsigned &excode, IScriptEnvironment*); | ||
| 146 | }; | ||
| 147 | |||
| 148 | |||
| 149 | class ExpTryCatch : public ExpExceptionTranslator | ||
| 150 | { | ||
| 151 | public: | ||
| 152 | ✗ | ExpTryCatch(const PExpression& _try_block, const char* _id, const PExpression& _catch_block) | |
| 153 | ✗ | : ExpExceptionTranslator(_try_block), id(_id), catch_block(_catch_block) {} | |
| 154 | AVSValue Evaluate(IScriptEnvironment* env); | ||
| 155 | |||
| 156 | private: | ||
| 157 | const char* const id; | ||
| 158 | const PExpression catch_block; | ||
| 159 | }; | ||
| 160 | |||
| 161 | class ExpLine : public ExpExceptionTranslator | ||
| 162 | { | ||
| 163 | public: | ||
| 164 | 6 | ExpLine(const PExpression& _exp, const char* _filename, int _line) | |
| 165 | 6 | : ExpExceptionTranslator(_exp), filename(_filename), line(_line) {} | |
| 166 | AVSValue Evaluate(IScriptEnvironment* env); | ||
| 167 | |||
| 168 | private: | ||
| 169 | const char* const filename; | ||
| 170 | const int line; | ||
| 171 | }; | ||
| 172 | |||
| 173 | |||
| 174 | class ExpBlockConditional : public Expression | ||
| 175 | { | ||
| 176 | public: | ||
| 177 | ✗ | ExpBlockConditional(const PExpression& _If, const PExpression& _Then, const PExpression& _Else) | |
| 178 | ✗ | : If(_If), Then(_Then), Else(_Else) {} | |
| 179 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 180 | |||
| 181 | private: | ||
| 182 | const PExpression If, Then, Else; | ||
| 183 | }; | ||
| 184 | |||
| 185 | |||
| 186 | class ExpWhileLoop : public Expression | ||
| 187 | { | ||
| 188 | public: | ||
| 189 | ✗ | ExpWhileLoop(const PExpression& _condition, const PExpression& _body) | |
| 190 | ✗ | : condition(_condition), body(_body) {} | |
| 191 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 192 | |||
| 193 | private: | ||
| 194 | const PExpression condition, body; | ||
| 195 | }; | ||
| 196 | |||
| 197 | |||
| 198 | class ExpForLoop : public Expression | ||
| 199 | { | ||
| 200 | public: | ||
| 201 | ✗ | ExpForLoop(const char* const _id, const PExpression& _init, const PExpression& _limit, | |
| 202 | const PExpression& _step, const PExpression& _body) | ||
| 203 | ✗ | : id(_id), init(_init), limit(_limit), step(_step), body(_body) {} | |
| 204 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 205 | |||
| 206 | private: | ||
| 207 | const char* const id; | ||
| 208 | const PExpression init, limit, step, body; | ||
| 209 | }; | ||
| 210 | |||
| 211 | class ExpBreak : public Expression | ||
| 212 | { | ||
| 213 | public: | ||
| 214 | ✗ | ExpBreak() {} | |
| 215 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 216 | }; | ||
| 217 | |||
| 218 | class ExpContinue : public Expression | ||
| 219 | { | ||
| 220 | public: | ||
| 221 | ✗ | ExpContinue() {} | |
| 222 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 223 | }; | ||
| 224 | |||
| 225 | class ExpConditional : public Expression | ||
| 226 | { | ||
| 227 | public: | ||
| 228 | ✗ | ExpConditional(const PExpression& _If, const PExpression& _Then, const PExpression& _Else) | |
| 229 | ✗ | : If(_If), Then(_Then), Else(_Else) {} | |
| 230 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 231 | |||
| 232 | private: | ||
| 233 | const PExpression If, Then, Else; | ||
| 234 | }; | ||
| 235 | |||
| 236 | class ExpReturn : public Expression | ||
| 237 | { | ||
| 238 | public: | ||
| 239 | ✗ | ExpReturn(PExpression value) : value(value) {} | |
| 240 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 241 | |||
| 242 | private: | ||
| 243 | const PExpression value; | ||
| 244 | }; | ||
| 245 | |||
| 246 | |||
| 247 | |||
| 248 | /**** Operator classes ****/ | ||
| 249 | |||
| 250 | class ExpOr : public Expression | ||
| 251 | { | ||
| 252 | public: | ||
| 253 | ✗ | ExpOr(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 254 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 255 | |||
| 256 | private: | ||
| 257 | const PExpression a, b; | ||
| 258 | }; | ||
| 259 | |||
| 260 | |||
| 261 | class ExpAnd : public Expression | ||
| 262 | { | ||
| 263 | public: | ||
| 264 | ✗ | ExpAnd(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 265 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 266 | |||
| 267 | private: | ||
| 268 | const PExpression a, b; | ||
| 269 | }; | ||
| 270 | |||
| 271 | |||
| 272 | class ExpEqual : public Expression | ||
| 273 | { | ||
| 274 | public: | ||
| 275 | ✗ | ExpEqual(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 276 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 277 | |||
| 278 | private: | ||
| 279 | const PExpression a, b; | ||
| 280 | }; | ||
| 281 | |||
| 282 | |||
| 283 | class ExpLess : public Expression | ||
| 284 | { | ||
| 285 | public: | ||
| 286 | ✗ | ExpLess(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 287 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 288 | |||
| 289 | private: | ||
| 290 | const PExpression a, b; | ||
| 291 | }; | ||
| 292 | |||
| 293 | |||
| 294 | class ExpPlus : public Expression | ||
| 295 | { | ||
| 296 | public: | ||
| 297 | ✗ | ExpPlus(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 298 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 299 | |||
| 300 | private: | ||
| 301 | const PExpression a, b; | ||
| 302 | }; | ||
| 303 | |||
| 304 | |||
| 305 | class ExpDoublePlus : public Expression | ||
| 306 | { | ||
| 307 | public: | ||
| 308 | ✗ | ExpDoublePlus(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 309 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 310 | |||
| 311 | private: | ||
| 312 | const PExpression a, b; | ||
| 313 | }; | ||
| 314 | |||
| 315 | |||
| 316 | class ExpMinus : public Expression | ||
| 317 | { | ||
| 318 | public: | ||
| 319 | ExpMinus(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | ||
| 320 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 321 | |||
| 322 | private: | ||
| 323 | const PExpression a, b; | ||
| 324 | }; | ||
| 325 | |||
| 326 | |||
| 327 | class ExpMult : public Expression | ||
| 328 | { | ||
| 329 | public: | ||
| 330 | ✗ | ExpMult(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 331 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 332 | |||
| 333 | private: | ||
| 334 | const PExpression a, b; | ||
| 335 | }; | ||
| 336 | |||
| 337 | |||
| 338 | class ExpDiv : public Expression | ||
| 339 | { | ||
| 340 | public: | ||
| 341 | ✗ | ExpDiv(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 342 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 343 | |||
| 344 | private: | ||
| 345 | const PExpression a, b; | ||
| 346 | }; | ||
| 347 | |||
| 348 | |||
| 349 | class ExpMod : public Expression | ||
| 350 | { | ||
| 351 | public: | ||
| 352 | ✗ | ExpMod(const PExpression& _a, const PExpression& _b) : a(_a), b(_b) {} | |
| 353 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 354 | |||
| 355 | private: | ||
| 356 | const PExpression a, b; | ||
| 357 | }; | ||
| 358 | |||
| 359 | |||
| 360 | class ExpNegate : public Expression | ||
| 361 | { | ||
| 362 | public: | ||
| 363 | ✗ | ExpNegate(const PExpression& _e) : e(_e) {} | |
| 364 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 365 | |||
| 366 | private: | ||
| 367 | const PExpression e; | ||
| 368 | }; | ||
| 369 | |||
| 370 | |||
| 371 | class ExpNot : public Expression | ||
| 372 | { | ||
| 373 | public: | ||
| 374 | ✗ | ExpNot(const PExpression& _e) : e(_e) {} | |
| 375 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 376 | |||
| 377 | private: | ||
| 378 | const PExpression e; | ||
| 379 | }; | ||
| 380 | |||
| 381 | |||
| 382 | class ExpVariableReference : public Expression | ||
| 383 | { | ||
| 384 | public: | ||
| 385 | 3 | ExpVariableReference(const char* _name) : name(_name) {} | |
| 386 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 387 | |||
| 388 | ✗ | virtual const char* GetLvalue() { return name; } | |
| 389 | |||
| 390 | private: | ||
| 391 | const char* const name; | ||
| 392 | }; | ||
| 393 | |||
| 394 | |||
| 395 | class ExpAssignment : public Expression | ||
| 396 | { | ||
| 397 | public: | ||
| 398 | ✗ | ExpAssignment(const char* _lhs, const PExpression& _rhs) : lhs(_lhs), rhs(_rhs), withret(false) {} | |
| 399 | ✗ | ExpAssignment(const char* _lhs, const PExpression& _rhs, bool wr) : lhs(_lhs), rhs(_rhs), withret(wr) {} | |
| 400 | |||
| 401 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 402 | |||
| 403 | private: | ||
| 404 | const char* const lhs; | ||
| 405 | PExpression rhs; | ||
| 406 | bool withret; | ||
| 407 | }; | ||
| 408 | |||
| 409 | |||
| 410 | class ExpGlobalAssignment : public Expression | ||
| 411 | { | ||
| 412 | public: | ||
| 413 | ✗ | ExpGlobalAssignment(const char* _lhs, const PExpression& _rhs) : lhs(_lhs), rhs(_rhs) {} | |
| 414 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 415 | |||
| 416 | private: | ||
| 417 | const char* const lhs; | ||
| 418 | PExpression rhs; | ||
| 419 | }; | ||
| 420 | |||
| 421 | |||
| 422 | class ExpFunctionCall : public Expression | ||
| 423 | { | ||
| 424 | public: | ||
| 425 | ExpFunctionCall( const char* _name, const PExpression& _func, PExpression* _arg_exprs, | ||
| 426 | const char** _arg_expr_names, int _arg_expr_count, bool _oop_notation ); | ||
| 427 | ~ExpFunctionCall(void); | ||
| 428 | |||
| 429 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 430 | |||
| 431 | private: | ||
| 432 | const char* const name; | ||
| 433 | PExpression func; | ||
| 434 | PExpression* arg_exprs; | ||
| 435 | const char** arg_expr_names; | ||
| 436 | const int arg_expr_count; | ||
| 437 | const bool oop_notation; | ||
| 438 | }; | ||
| 439 | |||
| 440 | |||
| 441 | class ExpLegacyFunctionDefinition : public Expression { | ||
| 442 | public: | ||
| 443 | ✗ | virtual AVSValue Evaluate(IScriptEnvironment* env) { return AVSValue(); } | |
| 444 | }; | ||
| 445 | |||
| 446 | |||
| 447 | class ExpFunctionWrapper : public Expression | ||
| 448 | { | ||
| 449 | public: | ||
| 450 | ExpFunctionWrapper(const char* name); | ||
| 451 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 452 | private: | ||
| 453 | PFunction func; | ||
| 454 | const char* const name; | ||
| 455 | }; | ||
| 456 | |||
| 457 | |||
| 458 | class ExpFunctionDefinition : public Expression | ||
| 459 | { | ||
| 460 | public: | ||
| 461 | ExpFunctionDefinition(const PExpression& _body, | ||
| 462 | const char* _name, const char* _param_types, | ||
| 463 | const bool* _param_floats, const char** _param_names, int param_count, | ||
| 464 | const char** _var_names, int _var_count, | ||
| 465 | const char* filename, int line); | ||
| 466 | ✗ | ~ExpFunctionDefinition() { | |
| 467 | ✗ | delete[]param_floats; | |
| 468 | ✗ | delete[]param_names; | |
| 469 | ✗ | delete[]var_names; | |
| 470 | ✗ | } | |
| 471 | |||
| 472 | virtual AVSValue Evaluate(IScriptEnvironment* env); | ||
| 473 | |||
| 474 | //private: | ||
| 475 | const PExpression body; | ||
| 476 | const char* name; | ||
| 477 | const char* param_types; | ||
| 478 | bool* param_floats; | ||
| 479 | const char** param_names; | ||
| 480 | int var_count; | ||
| 481 | const char** var_names; | ||
| 482 | |||
| 483 | const char* filename; | ||
| 484 | int line; | ||
| 485 | }; | ||
| 486 | |||
| 487 | |||
| 488 | class FunctionInstance : public IFunction | ||
| 489 | { | ||
| 490 | public: | ||
| 491 | FunctionInstance(ExpFunctionDefinition* pdef, IScriptEnvironment* env); | ||
| 492 | virtual ~FunctionInstance(); | ||
| 493 | virtual const char* ToString(IScriptEnvironment* env); | ||
| 494 | ✗ | virtual const char* GetLegacyName() { return nullptr; } | |
| 495 | ✗ | virtual const Function* GetDefinition() { return &data; } | |
| 496 | ✗ | virtual CaptureVars GetCaptures() { | |
| 497 | ✗ | CaptureVars ret = { pdef->var_count, pdef->var_names, var_data }; | |
| 498 | ✗ | return ret; | |
| 499 | } | ||
| 500 | AVSValue Execute(const AVSValue& args, IScriptEnvironment* env); | ||
| 501 | static AVSValue Execute_(AVSValue args, void* user_data, IScriptEnvironment* env); | ||
| 502 | |||
| 503 | private: | ||
| 504 | Function data; | ||
| 505 | ExpFunctionDefinition* pdef; | ||
| 506 | PExpression pdef_ref; | ||
| 507 | AVSValue *var_data; | ||
| 508 | }; | ||
| 509 | |||
| 510 | |||
| 511 | #endif // __Expression_H_ | ||
| 512 |