private List <Element> construct(string code) { List <string> toks = CompilerCore.AdcancedSplit(code.Trim(' ', '\t', '\r')); //整理代码,中缀运算符改后缀 List <Element> restok = new List <Element>(); Stack <string> elestack = new Stack <string>(); foreach (string data in toks) { if (data.Length == 0) { continue; } Element tmpEle; if (data.Contains("(") && data.Contains(")")) //是函数 { tmpEle = new FunctionElement(data, Element.Function); restok.Add(tmpEle); } else if (data.Contains("[") && data.Contains("]")) //是数组 { tmpEle = new Element(data, Element.Array); restok.Add(tmpEle); } else if (data[0] >= '0' && data[0] <= '9') //是数字 { tmpEle = new Element(data, Element.Number); restok.Add(tmpEle); } else if (data[0] >= 'a' && data[0] <= 'z' || data[0] >= 'A' && data[0] <= 'Z') //是一个变量 { tmpEle = new Element(data, Element.Variable); restok.Add(tmpEle); } else if (data[0] == '"') //是字符串 { tmpEle = new Element(data, Element.String); restok.Add(tmpEle); } else //是符号 { if (elestack.Count == 0 || data == "(") { elestack.Push(data); } else { if (data == ")") { while (elestack.Peek() != "(" && elestack.Count > 0) { restok.Add(new Element(elestack.Pop(), Element.Symbol)); } if (elestack.Count == 0) { throw new SyntaxErrorException("syntax error,the number of '(' and ')' don't match!"); } elestack.Pop(); } else { if (CompilerCore.operator_priority[data] >= CompilerCore.operator_priority[elestack.Peek()]) { elestack.Push(data); } else { if (elestack.Peek() == "(") { elestack.Push(data); } else { while (CompilerCore.operator_priority[data] < CompilerCore.operator_priority[elestack.Peek()] && elestack.Count > 0) { if (elestack.Peek() == "(") { elestack.Pop(); break; } restok.Add(new Element(elestack.Pop(), Element.Symbol)); } elestack.Push(data); } } } } } } //将栈中的剩余运算符按序插入到表达式后面 while (elestack.Count > 0) { if (elestack.Peek() == "(") { elestack.Pop(); continue; } restok.Add(new Element(elestack.Pop(), Element.Symbol)); } return(restok); }