public static string eval(string expr, Hashtable h) { Regex re_sep = new Regex(@"(==|!=|&&|\|\||[./+*\(\)-]| )"); Regex re_int = new Regex(@"^[0-9]+$"); Regex re_var = new Regex(@"^\$?[a-zA-Z_][0-9a-zA-Z_]*$"); Regex re_float = new Regex(@"^\-?\d+\.?\d*$"); eval_list i=new eval_list(); eval_list s=new eval_list(); try { // Tokenize string[] tokens=re_sep.Split(expr); for(int n=0;n<tokens.Length;n++) { string token=tokens[n]; if(token==".") { i.enqueue("DOT",token); } else if(token=="+") { i.enqueue("PLUS",token); } else if(token=="-") { i.enqueue("MINUS",token); } else if(token=="(") { i.enqueue("LP",token); } else if(token==")") { i.enqueue("RP",token); } else if(token=="==") { i.enqueue("EQUAL",token); } else if(token=="!=") { i.enqueue("NOT_EQUAL",token); } else if(token=="and" || token=="&&") { i.enqueue("AND",token); } else if(token=="or" || token=="||") { i.enqueue("OR",token); } else if(re_int.Match(token).Success) { i.enqueue("STR",token); } else if(re_var.Match(token).Success) { Object o=h[token]; if (o is bool && !(bool)o) o = ""; i.enqueue("STR",(o==null) ? "" : o.ToString() ); } else if(token.StartsWith("\"") || token.StartsWith("'")) { string term=token.Substring(0,1); token=token.Substring(1,token.Length-1); while(!token.EndsWith(term)) token+=tokens[++n]; i.enqueue("STR",token.Substring(0,token.Length-1)); } } // print("input: ",tokens); // print("input: ",i.l); // print("*************************************"); // Shift Reduce loop while(i.l.Count>0 || s.l.Count>1) { //print("input: ",i.l); //print("stack: ",s.l); //print(); // REDUCE if(s[2][0]=="STR" && s[1][0]=="PLUS" && s[0][0]=="STR") { int r = ToInt(s[2][1]) + ToInt(s[0][1]); s.replace(3,"STR", r.ToString()); } else if(s[2][0]=="STR" && s[1][0]=="MINUS" && s[0][0]=="STR") { int r = ToInt(s[2][1]) - ToInt(s[0][1]); s.replace(3,"STR", r.ToString()); } else if(s[2][0]=="STR" && s[1][0]=="DOT" && s[0][0]=="STR") { s.replace(3,"STR", s[2][1] + s[0][1]); } else if(s[2][0]=="STR" && s[1][0]=="EQUAL" && s[0][0]=="STR") { s.replace(3,"STR", (s[2][1]==s[0][1]) ? "True" : ""); } else if(s[2][0]=="STR" && s[1][0]=="NOT_EQUAL" && s[0][0]=="STR") { s.replace(3,"STR", (s[2][1]!=s[0][1]) ? "True" : ""); } else if(s[2][0]=="STR" && s[1][0]=="AND" && s[0][0]=="STR") { s.replace(3,"STR", (s[2][1].Length>0 && s[0][1].Length>0) ? "True" : ""); } else if(s[2][0]=="STR" && s[1][0]=="OR" && s[0][0]=="STR") { s.replace(3,"STR", (s[2][1].Length>0 || s[0][1].Length>0) ? "True" : ""); } else if(s[2][0]=="LP" && s[1][0]=="STR" && s[0][0]=="RP") { s.replace(3,"STR", s[1][1] ); // SHIFT } else { s.push(i.pop()); } } // print("input: ",i.l); // print("stack: ",s.l); // print("eval",expr,s[0][1]); return s[0][1]; } catch { QU.rwe("Error evaluating : <br/><pre>" + expr); return "ERROR"; } }
public static string eval(string expr, Hashtable h) { Regex re_sep = new Regex(@"(==|!=|&&|\|\||[./+*\(\)-]| )"); Regex re_int = new Regex(@"^[0-9]+$"); Regex re_var = new Regex(@"^\$?[a-zA-Z_][0-9a-zA-Z_]*$"); Regex re_float = new Regex(@"^\-?\d+\.?\d*$"); eval_list i = new eval_list(); eval_list s = new eval_list(); try { // Tokenize string[] tokens = re_sep.Split(expr); for (int n = 0; n < tokens.Length; n++) { string token = tokens[n]; if (token == ".") { i.enqueue("DOT", token); } else if (token == "+") { i.enqueue("PLUS", token); } else if (token == "-") { i.enqueue("MINUS", token); } else if (token == "(") { i.enqueue("LP", token); } else if (token == ")") { i.enqueue("RP", token); } else if (token == "==") { i.enqueue("EQUAL", token); } else if (token == "!=") { i.enqueue("NOT_EQUAL", token); } else if (token == "and" || token == "&&") { i.enqueue("AND", token); } else if (token == "or" || token == "||") { i.enqueue("OR", token); } else if (re_int.Match(token).Success) { i.enqueue("STR", token); } else if (re_var.Match(token).Success) { Object o = h[token]; if (o is bool && !(bool)o) { o = ""; } i.enqueue("STR", (o == null) ? "" : o.ToString()); } else if (token.StartsWith("\"") || token.StartsWith("'")) { string term = token.Substring(0, 1); token = token.Substring(1, token.Length - 1); while (!token.EndsWith(term)) { token += tokens[++n]; } i.enqueue("STR", token.Substring(0, token.Length - 1)); } } // print("input: ",tokens); // print("input: ",i.l); // print("*************************************"); // Shift Reduce loop while (i.l.Count > 0 || s.l.Count > 1) { //print("input: ",i.l); //print("stack: ",s.l); //print(); // REDUCE if (s[2][0] == "STR" && s[1][0] == "PLUS" && s[0][0] == "STR") { int r = ToInt(s[2][1]) + ToInt(s[0][1]); s.replace(3, "STR", r.ToString()); } else if (s[2][0] == "STR" && s[1][0] == "MINUS" && s[0][0] == "STR") { int r = ToInt(s[2][1]) - ToInt(s[0][1]); s.replace(3, "STR", r.ToString()); } else if (s[2][0] == "STR" && s[1][0] == "DOT" && s[0][0] == "STR") { s.replace(3, "STR", s[2][1] + s[0][1]); } else if (s[2][0] == "STR" && s[1][0] == "EQUAL" && s[0][0] == "STR") { s.replace(3, "STR", (s[2][1] == s[0][1]) ? "True" : ""); } else if (s[2][0] == "STR" && s[1][0] == "NOT_EQUAL" && s[0][0] == "STR") { s.replace(3, "STR", (s[2][1] != s[0][1]) ? "True" : ""); } else if (s[2][0] == "STR" && s[1][0] == "AND" && s[0][0] == "STR") { s.replace(3, "STR", (s[2][1].Length > 0 && s[0][1].Length > 0) ? "True" : ""); } else if (s[2][0] == "STR" && s[1][0] == "OR" && s[0][0] == "STR") { s.replace(3, "STR", (s[2][1].Length > 0 || s[0][1].Length > 0) ? "True" : ""); } else if (s[2][0] == "LP" && s[1][0] == "STR" && s[0][0] == "RP") { s.replace(3, "STR", s[1][1]); // SHIFT } else { s.push(i.pop()); } } // print("input: ",i.l); // print("stack: ",s.l); // print("eval",expr,s[0][1]); return(s[0][1]); } catch { QU.rwe("Error evaluating : <br/><pre>" + expr); return("ERROR"); } }