diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-12-23 21:37:01 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-12-23 21:37:01 +0100 |
commit | 7996525bfd7acfdaeebb65a63e8e6bcf9d73d7b4 (patch) | |
tree | e7b66877a79ca0c3fca997059da9bd0bc11a9973 | |
parent | ba869e07df4824a13b5b90f18aa5be8a0f3cc9c3 (diff) |
-rw-r--r-- | script.cpp | 56 | ||||
-rw-r--r-- | script.h | 2 |
2 files changed, 57 insertions, 1 deletions
@@ -1,4 +1,5 @@ #include "script.h" +#include <functional> bool validateExprNodeType(ExprNodeType type){ return type>=EFN_ADD&&type<=EFN_INVALID; @@ -41,8 +42,61 @@ pair<ExprFunction,int> parseExprFunction(const string &s,int start=0){ }*/ +function<int(int, int)> exprnode_functions[]{ + [](int a,int b){return a + b; }, + [](int a,int b){return a - b; }, + [](int a,int b){return a * b; }, + [](int a,int b){return a / b; }, + [](int a,int b){return a % b; }, + [](int a,int b){return a == b; }, + [](int a,int b){return a != b; }, + [](int a,int b){return a < b; }, + [](int a,int b){return a > b; }, + [](int a,int b){return a <= b; }, + [](int a,int b){return a >= b; }, + [](int a,int b){return a && b; }, + [](int a,int b){return a || b; }, + [](int _,int b){return !b; }, + [](int a,int b){return max(a,b);}, + [](int a,int b){return min(a,b);}, +}; + + + int Expression::evaluate(unordered_map<string,QueryResult> &vars){ - ; + if(type<0||type>=EFN_INVALID)return -42; + if((type==EFN_NUMBER||type==EFN_VARIABLE)&&args.size()!=0)return -43; + if(type==EFN_NUMBER)return intval; + if(type==EFN_VARIABLE)return vars[varname].res; + + if((type==EFN_MAX||type==EFN_MIN)&&args.size()==0)return -44; + if(type==EFN_MAX){ + int maxval=args[0].evaluate(vars),maxat=0,i,v; + for(i=1;i<args.size();i++){ + v=args[i].evaluate(vars); + if(v>maxval){ + maxval=v; + maxat=i; + } + } + return maxval; + } + if(type==EFN_MIN){ + int minval=args[0].evaluate(vars),minat=0,i,v; + for(i=1;i<args.size();i++){ + v=args[i].evaluate(vars); + if(v>minval){ + minval=v; + minat=i; + } + } + return minval; + } + + if(args.size()!=1)return -45; + if(type==EFN_NOT)return exprnode_functions[EFN_NOT](0,args[0].evaluate(vars)); + if(args.size()!=2)return -46; + return exprnode_functions[type](args[0].evaluate(vars),args[1].evaluate(vars)); } @@ -27,6 +27,8 @@ bool validateExprNodeType(ExprNodeType); struct Expression { ExprNodeType type; vector<Expression> args; + int intval; + string varname; int evaluate(unordered_map<string,QueryResult>&); }; |