aboutsummaryrefslogtreecommitdiff
path: root/script.cpp
blob: b3b179bb9dca34d7edb831ffc20513dfa5a3dbd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "script.h"
#include <functional>

bool validateExprNodeType(ExprNodeType type){
	return type>=EFN_ADD&&type<=EFN_INVALID;
}

/*//returns parsed func and length of func
pair<ExprFunction,int> 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<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));
}


ScriptLine::~ScriptLine(void){
	if(querystub)delete querystub;
	if(destvar)delete destvar;
	if(expr)delete expr;
	if(block)delete block;
}

vector<QueryResult> ScriptLine::execute(map<string,Table> &tables,unordered_map<string,QueryResult> &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<QueryResult> Script::execute(map<string,Table> &tables){
	unordered_map<string,QueryResult> vars;
	return execute(tables,vars);
}
vector<QueryResult> Script::execute(map<string,Table> &tables,unordered_map<string,QueryResult> &vars){
	;
}


Query* resolveQueryStub(const string &stub,map<string,Table> &tables,unordered_map<string,QueryResult> &vars){
	;
}