public virtual CalResult Operate() { ExeTimes++; if (this.OnBeginOperator != null) { OnBeginOperator(); } if (Params > 0 && !(this is FunSign) && RightVal == null) { throw new ExpressErrorException(this.SignName + "缺少右值!"); } if (Params > 1 && LeftVal == null) { throw new ExpressErrorException(this.SignName + "缺少左值!"); } if ((this is FunSign)) { FunSign fs = (FunSign)this; if (fs.HasArrayParam()) { return(CollectOperate()); } } //CommFun.Assert(Check()); if ((LeftVal == null || LeftVal.Results == null) && (RightVal == null || RightVal.Results == null)) { return(SingOperate()); } if (LeftVal != null && LeftVal.Results != null && RightVal != null && RightVal.Results != null && LeftVal.Results.Length != RightVal.Results.Length ) { throw new ExpressErrorException("无法操作,左右集合维数不相同。"); } return(CollectOperate()); }
/// <summary> /// 分解过程,每次分解一行代码 /// </summary> /// <param name="express"></param> /// <returns></returns> private IExpressPart[] ResolveCalStep(string express, int line) { if (string.IsNullOrWhiteSpace(express)) { return(null); } //express = new Regex(@";;{1,}").Replace(express, ";"); //这里有问题,比如((5+3)/2) //express = TrimBrackets(express); List <IExpressPart> arry = new List <IExpressPart>(); int ifcount = 0; int bracket = 0; int pointStart = 0; int elsePosit = 0; int thenPosit = 0; int modelID = 0; IExpressPart ep = null; int expressLen = express.Length; var expressCharArray = express.ToArray(); for (int i = 0; i <= expressLen; i++) { if (i == expressLen || (bracket == 0 && i > 0 && !IsCanGroup(expressCharArray[i - 1], expressCharArray[i])) ) { string es = express.Substring(pointStart, i - pointStart); es = es.TrimBrackets(); //检查是否是保留字 //if (CalSignFactory.IsProtectWord(es)) //{ // throw new ExpressErrorException(es + "是保留字!"); //} if (i < expressLen && es[0] == '\'' && expressCharArray[i] == '\'' && ifcount == 0) { i++; StringSign cexp = new StringSign(es.Trim('\''), ++modelID); cexp.StartIndex = pointStart; cexp.EndIndex = i; cexp.CodeLine = line; arry.Add(cexp); } else if (es[0] == '\'' && expressCharArray[i] != '\'' && ifcount == 0) { throw new ExpressErrorException("字符串分析错误"); } else if (es.Equals("if", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 0) { ep = new ConditionSign(); modelID += 2; ep.CodeLine = line; ep.ModeID = modelID; ep.StartIndex = pointStart; ep.EndIndex = i; arry.Add(ep); } ifcount++; } //else if (es.ToLower() == "then") else if (es.Equals("then", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 1) { //先加上左边的条件 if (i - 6 - ep.EndIndex <= 0) { throw new ExpressErrorException("if缺少条件表达式!", line, ep.EndIndex, i, ep); } CalExpress cexp = new CalExpress(express.Substring(ep.EndIndex + 1, i - 6 - ep.EndIndex).TrimBrackets(), ep.ModeID - 1); cexp.StartIndex = ep.EndIndex + 1; cexp.EndIndex = i - 4; cexp.CodeLine = line; arry.Add(cexp); thenPosit = i - 4; } } //else if (es.ToLower() == "else") else if (es.Equals("else", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 0) { throw new ExpressErrorException("表达式错误,else缺少if条件!", line, elsePosit, i, ep); } if (thenPosit == 0) { throw new ExpressErrorException("表达式错误,else前要有then!", line, thenPosit, i, ep); } if (ifcount == 1) { elsePosit = i - 4; } } //else if (es.ToLower() == "end") else if (es.Equals("end", StringComparison.OrdinalIgnoreCase)) { if (ifcount == 1 && thenPosit == 0) { throw new ExpressErrorException("if 表达式错误,缺少then!", line, i, i + 3, ep); } ifcount--; if (ifcount == 0) { if (elsePosit > 0) { modelID += 2; ElseSign elseSign = new ElseSign(); elseSign.StartIndex = elsePosit; elseSign.EndIndex = elsePosit + 4; elseSign.ModeID = modelID; elseSign.CodeLine = line; arry.Add(elseSign); //左边为真条件 if (elsePosit - thenPosit - 6 <= 0) { throw new ExpressErrorException("if else 表达式中缺少条件为真的表达式!", line, thenPosit - 1, thenPosit, ep); } CalExpress cexp0 = new CalExpress(express.Substring(thenPosit + 5, elsePosit - thenPosit - 6).Trim().TrimBrackets(), modelID - 1); cexp0.StartIndex = thenPosit + 5; cexp0.EndIndex = elsePosit - thenPosit - 6; cexp0.CodeLine = line; arry.Add(cexp0); if (i - elsePosit - 9 <= 0) { throw new ExpressErrorException("if else 表达式中缺少条件为假的表达式!", line, elsePosit + 4, elsePosit + 5, ep); } //右边为假条件 CalExpress cexp = new CalExpress(express.Substring(elsePosit + 5, i - elsePosit - 9).Trim().TrimBrackets(), ++modelID); cexp.StartIndex = elsePosit + 5; cexp.EndIndex = i - 4; cexp.CodeLine = line; arry.Add(cexp); } else { //加上右边的操作 if (i - thenPosit - 9 <= 0) { throw new ExpressErrorException("if表达式中缺少条件表达式!", line, thenPosit + 4, thenPosit + 5, ep); } //右边条件 CalExpress cexp = new CalExpress(express.Substring(thenPosit + 5, i - thenPosit - 9).TrimBrackets(), ++modelID); cexp.StartIndex = thenPosit + 5; cexp.EndIndex = i - 4; arry.Add(cexp); } } } else if (!string.IsNullOrWhiteSpace(es) && es != ";" && ifcount == 0) { CalSign cs; if (CalSignFactory.TryCreateSign(es, this.CalCurrent, out cs)) { cs.StartIndex = pointStart; cs.EndIndex = i; if (cs is FunSign) { FunSign fs = cs as FunSign; arry.Add(new CalExpress(fs.FunName, ++modelID)); fs.ModeID = ++modelID; arry.Add(fs); if (!string.IsNullOrWhiteSpace(fs.ParamString)) { arry.Add(new CalExpress(fs.ParamString, ++modelID)); } } else { cs.ModeID = ++modelID; arry.Add(cs); } } else { CalExpress cexp = new CalExpress(es, ++modelID); cexp.StartIndex = pointStart; cexp.EndIndex = i; arry.Add(cexp); } } pointStart = i; } if (i < expressLen) { if (expressCharArray[i] == '(') { bracket++; } else if (expressCharArray[i] == ')') { bracket--; } } if (i == expressLen && bracket > 0) { throw new ExpressErrorException("表达式错误,缺少右)!", line, expressLen, expressLen + 1, ep); } if (i == expressLen && ifcount > 0) { throw new ExpressErrorException("if表达式错误!", line, expressLen, expressLen + 1, ep); } } return(arry.OrderBy(c => c.ModeID).ToArray()); }
private static CalSign CreateCalSign(string signExp, CalCurrent pool) { if (string.Equals(signExp, "and", StringComparison.OrdinalIgnoreCase)) { return(new AndSign()); } else if (string.Equals(signExp, "not", StringComparison.OrdinalIgnoreCase)) { return(new NotSign()); } else if (string.Equals(signExp, "or", StringComparison.OrdinalIgnoreCase)) { return(new OrSign()); } else if (string.Equals(signExp, ":", StringComparison.OrdinalIgnoreCase)) { return(new SetValueSign(pool)); } else if (string.Equals(signExp, ":=", StringComparison.OrdinalIgnoreCase)) { return(new OutputValueSign()); } else if (string.Equals(signExp, "=", StringComparison.OrdinalIgnoreCase)) { return(new EqSign()); } else if (string.Equals(signExp, ">", StringComparison.OrdinalIgnoreCase)) { return(new Bigersign()); } else if (string.Equals(signExp, ">=", StringComparison.OrdinalIgnoreCase)) { return(new BigerEqSign()); } else if (string.Equals(signExp, "<", StringComparison.OrdinalIgnoreCase)) { return(new SmallerSign()); } else if (string.Equals(signExp, "<=", StringComparison.OrdinalIgnoreCase)) { return(new SmallerEqSign()); } else if (string.Equals(signExp, "+", StringComparison.OrdinalIgnoreCase)) { return(new PlusSign()); } else if (string.Equals(signExp, "-", StringComparison.OrdinalIgnoreCase)) { return(new MinusSign()); } else if (string.Equals(signExp, "*", StringComparison.OrdinalIgnoreCase)) { return(new MultiplieSign()); } else if (string.Equals(signExp, "/", StringComparison.OrdinalIgnoreCase)) { return(new DividedSign()); } else if (string.Equals(signExp, "mod", StringComparison.OrdinalIgnoreCase)) { return(new ModSign()); } else if (string.Equals(signExp, ",", StringComparison.OrdinalIgnoreCase)) { return(new BeanSign()); } else { Match mc = Comm.MatchFunctionExpress(signExp); //Match mc = new Regex(@"^([A-z]{1}[A-z0-9]*)\((.*)\)$").Match(signExp); if (mc.Success) { if (Comm.BrackIsMatched(mc.Groups[2].Value, 0, mc.Groups[2].Length)) { FunSign fs = CreateFunSign(mc.Groups[1].Value, pool); if (fs == null) { throw new ExpressErrorException("不存在的函数" + mc.Groups[1].Value + "!"); } fs.ParamString = mc.Groups[2].Value; return(fs); } } //mc = Comm.MatchValNameExpress(signExp); if (Comm.IsValName(signExp) && !IsProtectWord(signExp)) { FunSign fs = CreateFunSign(signExp, pool); if (fs != null) { return(fs); } VarSign vs = new VarSign(signExp, pool); return(vs); } return(null); } }