/*for (int i = 0; i < s.Length + 1 - symbols.Length; i++) * { * if (s[i] == leftBracket) * nowLevel++; * if (s[i] == rightBracket) * nowLevel--; * if (nowLevel == 0 && s.Substring(i, symbols.Length) == symbols) * pos = i; * }*/ public static IOperation ParseFrom(string s) { try { string left = "", right = ""; if (s.IndexOf("return") == 0) { return(new Ret(ParseFrom(s.Substring(6)))); } //___________ int varType = Math.Max((s.IndexOf("int") >= 0) ? 2 : -1, Math.Max((s.IndexOf("double") >= 0) ? 5 : -1, Math.Max((s.IndexOf("char") >= 0) ? 3 : -1, Math.Max((s.IndexOf("string") >= 0) ? 5 : -1, (s.IndexOf("bool") >= 0) ? 3 : -1)))); if (varType > 0 && MISC.IndexOfOnLevel0(s, ",", 0) > 0) { s = s.Insert(varType + 1, "$"); string firstPart = s.Substring(0, s.IndexOf("$")); // int&a,b int at; do { at = MISC.IndexOfOnLevel0(s, ",", 0); if (at >= 0) { s = s.Substring(0, at) + ';' + firstPart + s.Substring(at + 1); } } while (at >= 0); s = s.Remove(s.IndexOf('$'), 1); throw new Exception("#MDS:" + s); } //_________________ if (s.IndexOf('{') == 0 && s.LastIndexOf('}') == s.Length - 1) { return(MonoOperation.ParseFrom(s)); } if (!onLevel(s, "==", 0) && !onLevel(s, "!=", 0) && onLevel(s, "=", 0)) { MISC.separate(s, "=", ref left, ref right, lastIndex); IOperation rightOperation = ParseFrom(right); if (left.Length > 0 && ("+-*/").IndexOf(left[left.Length - 1]) >= 0) { IOperation leftMinusOne = MonoOperation.ParseFrom(left.Remove(left.Length - 1)); switch (left[left.Length - 1]) { case '+': return(new Assum(leftMinusOne, new Summ(leftMinusOne, rightOperation))); case '-': return(new Assum(leftMinusOne, new Diff(leftMinusOne, rightOperation))); case '*': return(new Assum(leftMinusOne, new Mult(leftMinusOne, rightOperation))); case '/': return(new Assum(leftMinusOne, new Qout(leftMinusOne, rightOperation))); default: break; } } IOperation leftOperation = MonoOperation.ParseFrom(left); if ((rightOperation as StructureDefine) == null) { return(new Assum(leftOperation, rightOperation)); } else { Assum needUPdatedAssum = new Assum(leftOperation, rightOperation); needUPdatedAssum.requiredUpdate = "structdefineinfor"; return(needUPdatedAssum); } } if (onLevel(s, "==", 0)) { MISC.separate(s, "==", ref left, ref right, lastIndex); return(new Equal(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "!=", 0)) { MISC.separate(s, "!=", ref left, ref right, lastIndex); return(new Uneq(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "<=", 0)) { MISC.separate(s, "<=", ref left, ref right, lastIndex); return(new LsEq(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, ">=", 0)) { MISC.separate(s, ">=", ref left, ref right, lastIndex); return(new MrEq(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "<", 0)) { MISC.separate(s, "<", ref left, ref right, lastIndex); return(new Less(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, ">", 0)) { MISC.separate(s, ">", ref left, ref right, lastIndex); return(new More(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "+", 0)) { MISC.separate(s, "+", ref left, ref right, lastIndex); return(new Summ(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "-", 0)) { MISC.separate(s, "-", ref left, ref right, lastIndex); if (left.Length == 0) { return(MonoOperation.ParseFrom("-" + right)); } return(new Diff(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "*", 0) && s.IndexOf("*") > 0) { MISC.separate(s, "*", ref left, ref right, lastIndex); // can be initialization of pointer! if (new ValueType(VT.Cunknown) == Define.detectType(left)) { return(new Mult(ParseFrom(left), ParseFrom(right))); } } if (onLevel(s, "/", 0)) { MISC.separate(s, "/", ref left, ref right, lastIndex); return(new Qout(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "||", 0)) { MISC.separate(s, "||", ref left, ref right, lastIndex); return(new ORS(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "|", 0)) { MISC.separate(s, "|", ref left, ref right, lastIndex); return(new OR(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "&&", 0)) { MISC.separate(s, "&&", ref left, ref right, lastIndex); return(new ANDS(ParseFrom(left), ParseFrom(right))); } if (onLevel(s, "&", 0) && s.IndexOf("&") > 0) { MISC.separate(s, "&", ref left, ref right, lastIndex); return(new AND(ParseFrom(left), ParseFrom(right))); } if (s.IndexOf('(') == 0 && s.LastIndexOf(')') == s.Length - 1) { return(ParseFrom(MISC.breakBrackets(s))); } try { return(UniqOperation.ParseFrom(s)); } catch (Exception e) { return(MonoOperation.ParseFrom(s)); } } catch (Exception e) { if (e.Message.IndexOf("#MDS:") != 0) { throw new Exception("Can not parse \"" + s + "\"\n" + e.Message); } else { throw new Exception(e.Message); } } }
// public ASTFunction(string S) { //TypeConvertion tpcv = new TypeConvertion("IIBDDBDIBIDBCCB", 2); string s = S.Substring(0, S.IndexOf('(')); //tpcvString = ""; List <ValueType> vtList = new List <ValueType>(); int varType = Math.Max((s.IndexOf("int") >= 0) ? 2 : -1, Math.Max((s.IndexOf("double") >= 0) ? 5 : -1, Math.Max((s.IndexOf("char") >= 0) ? 3 : -1, Math.Max((s.IndexOf("string") >= 0) ? 5 : -1, Math.Max((s.IndexOf("bool") >= 0) ? 3 : -1, (s.IndexOf("void") >= 0) ? 3 : -1))))); if (varType >= 0) { varType++; string[] type_name = new string[] { s.Substring(0, varType), s.Substring(varType, s.Length - varType) };//s.Split(s[varType + 1]); name = type_name[1]; int returnPointerLevel = 0; while (name[0] == '*') { returnPointerLevel++; name = name.Substring(1); } if (name.Length == 0) { throw new Exception("Invalid function name!"); } // ! retType = new ValueType(Define.detectType(type_name[0]), returnPointerLevel); // try to parse signature and actions List <string> vars = MISC.splitBy(MISC.getIn(S, S.IndexOf('(')), ','); input = new List <Define>(); MISC.GoDeep("FDEFINED"); for (int i = 0; i < vars.Count; i++) { input.Add((Define)MonoOperation.ParseFrom(vars[i])); vtList.Add((input[input.Count - 1] as Define).returnTypes()); //tpcvString += vars[i][0].ToString().ToUpper(); } //tpcvString += returnTypes().ToString()[1].ToString().ToUpper(); tpcv = new TypeConvertion(vtList, retType); // check name uniq! //bool foundFunc = false; for (int i = 0; i < ASTTree.funcs.Count; i++) { if (ASTTree.funcs[i].actions.CommandCount > 0 && MISC.CompareFunctionSignature(ASTTree.funcs[i], this)) { throw new Exception("Can not redefine a function \"" + name + " : " + this.getArgsString + "\"!"); } } if (S.IndexOf('{') >= 0) { try { MISC.GoDeep("FUNCTION$" + name + "$" + returnTypes()); string actionCode = MISC.getIn(S, S.IndexOf('{')); actions = new CommandOrder(actionCode, ';'); MISC.GoBack(); } catch (Exception e) { throw new Exception("Problem in function \"" + name + "\"\n" + e.Message); } } else { actions = new CommandOrder(); } MISC.GoBack(); return; } // check contain of Return function throw new Exception("Can not parse a function\t " + MISC.StringFirstLetters(S, 20, true)); }