aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-23 21:37:01 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-23 21:37:01 +0100
commit7996525bfd7acfdaeebb65a63e8e6bcf9d73d7b4 (patch)
treee7b66877a79ca0c3fca997059da9bd0bc11a9973
parentba869e07df4824a13b5b90f18aa5be8a0f3cc9c3 (diff)
Some changes that were lying aroundHEADmaster
-rw-r--r--script.cpp56
-rw-r--r--script.h2
2 files changed, 57 insertions, 1 deletions
diff --git a/script.cpp b/script.cpp
index 689066f..b3b179b 100644
--- a/script.cpp
+++ b/script.cpp
@@ -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));
}
diff --git a/script.h b/script.h
index 05427ca..393bb45 100644
--- a/script.h
+++ b/script.h
@@ -27,6 +27,8 @@ bool validateExprNodeType(ExprNodeType);
struct Expression {
ExprNodeType type;
vector<Expression> args;
+ int intval;
+ string varname;
int evaluate(unordered_map<string,QueryResult>&);
};