#include "script.h" #include bool validateExprNodeType(ExprNodeType type){ return type>=EFN_ADD&&type<=EFN_INVALID; } /*//returns parsed func and length of func pair parseExprFunction(const string &s,int start=0){ switch(s[start]){ case '+': return {EFN_ADD,1}; case '-': return {EFN_SUBTRACT,1}; case '*': return {EFN_MULTIPLY,1}; case '/': return {EFN_DIVIDE,1}; case '%': return {EFN_MODULO,1}; case '=': if(s[start+1]=='=')return {EFN_EQUAL,2}; else return {EFN_INVALID,0}; case '!': if(s[start+1]=='=')return {EFN_UNEQUAL,2}; else return {EFN_NOT,1}; case '<': if(s[start+1]=='=')return {EFN_LESSEQUAL,2}; else return {EFN_LESS,1}; case '>': if(s[start+1]=='=')return {EFN_GREATEREQUAL,2}; else return {EFN_GREATER,1}; case '&': if(s[start+1]=='&')return {EFN_AND,2}; else return {EFN_INVALID,0}; case '|': if(s[start+1]=='|')return {EFN_OR,2}; else return {EFN_INVALID,0}; default: return {EFN_INVALID,0}; } }*/ function 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 &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;imaxval){ maxval=v; maxat=i; } } return maxval; } if(type==EFN_MIN){ int minval=args[0].evaluate(vars),minat=0,i,v; for(i=1;iminval){ 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)); } ScriptLine::~ScriptLine(void){ if(querystub)delete querystub; if(destvar)delete destvar; if(expr)delete expr; if(block)delete block; } vector ScriptLine::execute(map &tables,unordered_map &vars){ if(type==SLT_QUERYSTUB&&querystub){ Query *qu=resolveQueryStub(*querystub,tables,vars); QueryResult res=qu->execute(tables); delete qu; if(destvar)vars[*destvar]=res; return {res}; } else if(type==SLT_IF&&expr&&block){ if(expr->evaluate(vars))return block->execute(tables,vars); } else if(type==SLT_WHILE&&expr&&block){ while(expr->evaluate(vars))return block->execute(tables,vars); } else if(type==SLT_ASSIGNMENT&&destvar&&expr){ vars[*destvar]=QueryResult(expr->evaluate(vars)); return {}; } return {}; } vector Script::execute(map &tables){ unordered_map vars; return execute(tables,vars); } vector Script::execute(map &tables,unordered_map &vars){ ; } Query* resolveQueryStub(const string &stub,map &tables,unordered_map &vars){ ; }