public Define(string s, bool autoAssume) { //string firstPart = s.Substring(0, s.IndexOf("$") + 1); // int&a,b //while (s.IndexOf(',') > 0) //{ // int at = s.IndexOf(','); // s = s.Substring(0, s.IndexOf(',')) + ';' + firstPart + s.Substring(s.IndexOf(',') + 1); // // //} string[] ss = s.Split('$'); //string varName; VT varType; bool everDefined = false; for (int i = 0; i < ASTTree.variables.Count; i++) { if (ASTTree.variables[i].name == ss[1] && MISC.isVariableAvailable(i)) { everDefined = true; break; } } if (ss[1].Length > 0 && !everDefined) { varName = ss[1]; } else { throw new Exception("Can not define " + ((everDefined) ? "again " : "") + "variable with name \"" + ss[1] + "\""); } ss[0].ToLower(); varType = detectType(ss[0]); //for (int i =0; i < varName.Length; i++){ //bool isPointer = false; int pointerLevel = 0; while (varName.IndexOf('*') == 0) { pointerLevel++; varName = varName.Substring(1); } returnType = new ValueType(varType, pointerLevel); defineType = returnType; //____________________________________ if (varName.LastIndexOf("]") == varName.Length - 1 && varName.IndexOf("[") > 0) { List <string> inBr = MISC.splitByQuad(varName); // [] [] [ ] [] varName = varName.Substring(0, varName.IndexOf('[')); for (int ib = 0; ib < inBr.Count; ib++) { string inBrack = inBr[ib]; int length = 0; if (inBrack != "") { IOperation arrayLength = BinaryOperation.ParseFrom(inBrack); if (arrayLength.returnTypes() != VT.Cint) { throw new Exception("Int only can be array length parameter"); } //IOperation arrayLength = BinaryOperation.ParseFrom(inBrack); //if (arrayLength as ASTvalue == null || arrayLength.returnTypes() != VT.Cint) // throw new Exception("Incorrect array length parameters!"); //length = (int)(arrayLength as ASTvalue).getValue; //if (length < 1) // throw new Exception("Array length should be 1 and more!"); } //for (int i = 0; i < length; i++) //{ // // as default variable // ASTvariable newVar = new ASTvariable(new ValueType(varType, pointerLevel), varName + "#" + i, 0); // ASTTree.variables.Add(newVar); // MISC.pushVariable(ASTTree.variables.Count - 1); // ASTTree.tokens.Add(newVar); //} defineType = defineType.TypeOfPointerToThis(); } } //_________________________________________ ASTvariable NV = new ASTvariable(defineType, varName, pointerLevel, MISC.GetCurrentVariableAdressType()); var = NV; ASTTree.variables.Add(NV); MISC.pushVariable(ASTTree.variables.Count - 1); if (autoAssume) { MISC.defineVariable(ASTTree.variables.Count - 1); } ASTTree.tokens.Add(NV); a = NV; returnType = a.returnTypes(); b = new ASTvalue(new ValueType(VT.Cadress), (object)(ASTTree.variables.Count - 1)); }
public static IOperation ParseFrom(string s) { if (s.IndexOf('{') == 0 && s.LastIndexOf('}') == s.Length - 1) { return(new StructureDefine(MISC.getIn(s, 0))); } if (s.IndexOf('(') == 0 && s.LastIndexOf(')') == s.Length - 1) { return(BinaryOperation.ParseFrom(MISC.breakBrackets(s))); } if (s.Length > 2 && s.IndexOf("--") == s.Length - 2) { IOperation lt = ParseFrom(s.Substring(0, s.Length - 2)); return(new Assum(lt, new Diff(lt, new ASTvalue(new ValueType(VT.Cint), (object)1)))); //return new Dscr()); } if (s.Length > 2 && s.IndexOf("++") == s.Length - 2) { //return new Incr(ParseFrom(s.Substring(0, s.Length - 2))); IOperation lt = ParseFrom(s.Substring(0, s.Length - 2)); return(new Assum(lt, new Summ(lt, new ASTvalue(new ValueType(VT.Cint), (object)1)))); } if (s.IndexOf("-") == 0) { return(new Mins(ParseFrom(s.Substring(1, s.Length - 1)))); } if (s.IndexOf("!") == 0) { return(new Nega(ParseFrom(s.Substring(1, s.Length - 1)))); } if (s.IndexOf("&") == 0) { callDefined = true; ///! IOperation gettingAdressOf = ParseFrom(s.Substring(1, s.Length - 1)); return(new Adrs(gettingAdressOf)); } 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) { s = s.Insert(varType + 1, "$"); return(new Define(s, false)); } if (s.IndexOf("*") == 0) { IOperation pointTo = ParseFrom(s.Substring(1, s.Length - 1)); return(new GetValByAdress(pointTo, (pointTo).returnTypes())); throw new Exception("Invalid pointer selected!"); } try { return(new ASTFunctionCall(s)); } catch (Exception e) { if (e.Message.IndexOf("is not a function") < 0) { throw new Exception(e.Message); } } if (s.LastIndexOf("]") == s.Length - 1 && s.IndexOf("[") > 0) { //IOperation pointTo = ; //return // new GetValByAdress(new Summ(pointTo, // BinaryOperation.ParseFrom(MISC.getIn(s, s.IndexOf('[')))), // (pointTo).returnTypes()); //throw new Exception("Invalid pointer selected!"); callDefined = true; ///! string sContainBrackets = s.Substring(s.IndexOf("[")); List <string> getedBrs = MISC.splitByQuad(sContainBrackets); IOperation resOper = ParseFrom(s.Substring(0, s.IndexOf('['))); for (int o = 0; o < getedBrs.Count; o++) { IOperation currentBrsOp = BinaryOperation.ParseFrom(getedBrs[o]); resOper = new GetValByAdress(new Summ(resOper, currentBrsOp), resOper.returnTypes()); } return(resOper); } //f (s.IndexOf('(') == 0 && s.LastIndexOf(')') == s.Length - 1) //return ParseFrom(MISC.breakBrackets(s)); try { ASTvalue res = new ASTvalue(s, callDefined); callDefined = false; return(res); } catch (Exception e) { callDefined = false; if (e.Message.IndexOf("GetAddr") == 0) { int newAdress = int.Parse(e.Message.Split('_')[1]); return(new GetValByAdress(new ASTvalue(new ValueType(VT.Cadress), (object)newAdress), ASTTree.variables[newAdress].getValueType)); } throw new Exception(e.Message); } //finally //{ // calldefined = false; //} }