public CycleFor(string parseCondition, CommandOrder actions) { condition = new Equal(BinaryOperation.ParseFrom(parseCondition), new ASTvalue(new ValueType(VT.Cboolean), (object)true)); this.actions = actions; GlobalOperatorNumber = ++MISC.GlobalOperatorNumber; FindIterateVars(); }
public CycleWhile(string parseCondition, string parseActions, bool doFirst) { condition = new Equal(BinaryOperation.ParseFrom(parseCondition), new ASTvalue(new ValueType(VT.Cboolean), (object)true)); MISC.GoDeep("WHILE"); actions = new CommandOrder(parseActions, ';'); MISC.GoBack(); this.doFirst = doFirst; }
//public ICommand[] ParseCommand(String S) //{ // // here we get a 1 string between ;...; // // it can be cycle or simple operation // int p1 = MISC.IndexOfOnLevel0(S, "{", 0), // p2 = MISC.IndexOfOnLevel0(S, "}", 0); // #region 0Zone and array assume // if (p1 == 0 && p2 == S.Length - 1) // { // return new ICommand[] { new OperatorZone(MISC.getIn(S, S.IndexOf('{'))) }; // } // if ((S.IndexOf("{") < 0 || (MISC.IndexOfOnLevel0(S, "=", 0) > 0)) // && S.ToLower().IndexOf("if") != 0/* && S.ToLower().IndexOf("else") != 0*/) // { // IOperation newBO = BinaryOperation.ParseFrom(S); // if ((newBO as Assum) != null && (newBO as Assum).requiredUpdate != "none") // { // string needUpdate = (newBO as Assum).requiredUpdate; // if (needUpdate.IndexOf("structdefineinfor") == 0) // { // string nam = (newBO as Assum).GetAssumableName; // if (nam == "-") // throw new Exception("What are you doing!?"); // List<IOperation> values = (newBO as Assum).GetStructDefine(); // List<ICommand> res = new List<ICommand>(); // for (int i = 0; i < values.Count; i++) // res.Add(new Assum(BinaryOperation.ParseFrom(nam + "[" + i + "]"), values[i])); // return res.ToArray(); // } // } // else // return new ICommand[] { newBO }; // } // #endregion // #region Cycles // //try to parse while cycles // if (S.ToLower().IndexOf("for") == 0) // { // string parseCondition = MISC.getIn(S, S.IndexOf('(')), // parseAction = MISC.getIn(S, S.IndexOf('{')); // string[] conditionParts = parseCondition.Split(';'); // if (conditionParts.Length != 3) // throw new Exception("Invalid count of FOR-cycle condition parts"); // // first one - is simple commands of initialization // MISC.GoDeep("FOR"); // if (conditionParts[0].Length > 0) // this.MergeWith(new CommandOrder(conditionParts[0], ',')); // included // if (conditionParts[1].Length <= 0) // conditionParts[1] = "true"; // // parse commands // CommandOrder actions = new CommandOrder(parseAction, ';'); // if (conditionParts[2].Length > 0) // actions.MergeWith(new CommandOrder(conditionParts[2], ',')); // ICommand[] res = new ICommand[] { new CycleFor(conditionParts[1], actions) }; // MISC.GoBack(); // return res; // } // if (S.ToLower().IndexOf("while") == 0) // return new ICommand[] { new CycleWhile(MISC.getIn(S, S.IndexOf('(')), MISC.getIn(S, S.IndexOf('{')), false) }; // if (S.ToLower().IndexOf("do") == 0) // return new ICommand[] { new CycleWhile(MISC.getIn(S, S.IndexOf('(')), MISC.getIn(S, S.IndexOf('{')), true) }; // #endregion // #region Operators // if (S.ToLower().IndexOf("if") == 0) // { // int indexOfConditionRightBrakket = MISC.IndexOfOnLevel0(S, ")", 0); // if (S.IndexOf("{") - 1 == indexOfConditionRightBrakket) // { // int pos1 = MISC.IndexOfOnLevel0(S, "}", 0), // pos2 = MISC.IndexOfOnLevel0(S, "}", pos1 + 1), // posElse = MISC.IndexOfOnLevel0(S, "}else{", 0); // if (pos2 < pos1) // return new ICommand[] { new OperatorIf(MISC.getIn(S, S.IndexOf('(')), MISC.getIn(S, S.IndexOf('{')), "") }; // else // return new ICommand[] { new OperatorIf(MISC.getIn(S, S.IndexOf('(')), MISC.getIn(S, S.IndexOf('{')), MISC.getIn(S, S.LastIndexOf("{"))) }; // } // else // { // int indexElse = MISC.IndexOfOnLevel0(S, "else", 0); // if (indexElse < 0) // return new ICommand[] { new OperatorIf(MISC.getIn(S, S.IndexOf('(')), S.Substring(indexOfConditionRightBrakket + 1), "") }; // else // return new ICommand[]{new OperatorIf(MISC.getIn(S, S.IndexOf('(')), // S.Substring(indexOfConditionRightBrakket + 1, indexElse - indexOfConditionRightBrakket - 1), // S.Substring(indexElse + 4))}; // } // } // #endregion // throw new Exception("Can not parse a command\t " +MISC.StringFirstLetters(S, 20, true)); // return null; //} public void MergeWith(CommandOrder another) { for (int i = 0; i < another.commands.Count; i++) { commands.Add(another.commands[i]); } // merge with }
void ClearTree() { funcs = new List <ASTFunction>(); tokens = new List <IASTtoken>(); variables = new List <ASTvariable>(); MISC.ClearStack(); GlobalVars = new CommandOrder(); }
void ClearTree() { funcs = new List <ASTFunction>(); tokens = new List <IASTtoken>(); variables = new List <ASTvariable>(); MISC.ClearStack(); GlobalVars = new CommandOrder(); GlobalVarsVars = new List <ASTvariable>(); MISC.ResetAdressing(); MISC.LLVMtmpNumber = 0; MISC.GlobalOperatorNumber = 0; }
public static string ToLLVMVariative(int depth, int GlobalOperatorNumber, string type, bool doBeforeWhile, Equal condition, CommandOrder actions, List <ASTvariable> iterateVars) { LLVM.AddToCode(";" + type + "\n"); // if (!doBeforeWhile) { LLVM.AddToCode(String.Format("{0}br label %{2}cond{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type)); LLVM.AddToCode(String.Format("{0}{2}cond{1}:\n", MISC.tabsLLVM(depth - 1), GlobalOperatorNumber, type)); // reload all variables in it foreach (ASTvariable vari in iterateVars) { vari.reloadedTimes++; LLVM.AddToCode(String.Format("{0}{1} = load {2}, {3} {4}\n", MISC.tabsLLVM(depth), vari.ToLLVM(), vari.returnTypes().ToLLVM(), vari.returnTypes().TypeOfPointerToThis().ToLLVM(), MISC.RemoveCall(vari.ToLLVM()))); } string condLine = condition.getTrueEqual().ToLLVM(depth); LLVM.AddToCode(String.Format("{0}br i1 {3}, label %{2}action{1}, label %{2}cont{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type, condLine)); LLVM.AddToCode(String.Format("{0}{2}action{1}:\n", MISC.tabsLLVM(depth - 1), GlobalOperatorNumber, type)); LLVM.AddToCode(actions.ToLLVM(depth)); LLVM.AddToCode(String.Format("{0}br label %{2}cond{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type)); LLVM.AddToCode(String.Format("{0}{2}cont{1}:", MISC.tabsLLVM(depth - 1), GlobalOperatorNumber, type)); } else { LLVM.AddToCode(String.Format("{0}br label %{2}action{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type)); LLVM.AddToCode(String.Format("{0}{2}action{1}:\n", MISC.tabsLLVM(depth - 1), GlobalOperatorNumber, type)); LLVM.AddToCode(actions.ToLLVM(depth)); LLVM.AddToCode(String.Format("{0}br label %{2}cond{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type)); LLVM.AddToCode(String.Format("{0}{2}cond{1}:\n", MISC.tabsLLVM(depth - 1), GlobalOperatorNumber, type)); string condLine = condition.getTrueEqual().ToLLVM(depth); LLVM.AddToCode(String.Format("{0}br i1 {3}, label %{2}action{1}, label %{2}cont{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type, condLine)); LLVM.AddToCode(String.Format("{0}br label %{2}cond{1}\n", MISC.tabsLLVM(depth), GlobalOperatorNumber, type)); LLVM.AddToCode(String.Format("{0}{2}cont{1}:", MISC.tabsLLVM(depth - 1), GlobalOperatorNumber, type)); } return(""); }
public ICommand ParseCommand2(String S) { int s1 = MISC.IndexOfOnLevel0(S, "(", 0), s2 = MISC.IndexOfOnLevel0(S, ")", 0), p1 = MISC.IndexOfOnLevel0(S, "{", 0), p2 = MISC.IndexOfOnLevel0(S, "}", 0); if (s2 < s1 || p2 < p1) { throw new Exception("Command contains incorrect brackets:\t" + MISC.StringFirstLetters(S, 20, true)); } if (p1 == 0 && p2 == S.Length - 1) { MISC.GoDeep("Block"); CommandOrder co = new CommandOrder(MISC.getIn(S, 0), ';'); MISC.GoBack(); return(co); } // Binary operation usuall if ((p1 < 0 || (MISC.IndexOfOnLevel0(S, "=", 0) > 0)) && S.ToLower().IndexOf("if") != 0 && S.ToLower().IndexOf("for") != 0) { IOperation newBO = BinaryOperation.ParseFrom(S); if ((newBO as Assum) != null && (newBO as Assum).requiredUpdate != "none") { string needUpdate = (newBO as Assum).requiredUpdate; if (needUpdate.IndexOf("structdefineinfor") == 0) { string nam = (newBO as Assum).GetAssumableName; if (nam == "-") { throw new Exception("What are you doing!?"); } List <IOperation> values = (newBO as Assum).GetStructDefine(); List <ICommand> res = new List <ICommand>(); for (int i = 0; i < values.Count; i++) { res.Add(new Assum(BinaryOperation.ParseFrom(nam + "[" + i + "]"), values[i])); } return(new CommandOrder(res.ToArray())); } } else { return(newBO); } } // _______________________ if (s1 > 0) { string operatorFind = S.Remove(s1); // simple if if (operatorFind == "if") { // we gonna parse IF from this shit! string conditionParse = MISC.getIn(S, 2), firstActionParse = S.Substring(s2 + 1); int indElse = MISC.IndexOfOnLevel0(firstActionParse, "else", 0); if (indElse > 0) { string secondActionParse = firstActionParse.Substring(indElse + 4); firstActionParse = firstActionParse.Substring(0, indElse); return(new OperatorIf(conditionParse, firstActionParse, secondActionParse)); } return(new OperatorIf(conditionParse, firstActionParse, "")); } // simple while if (operatorFind == "while") { string conditionParse = MISC.getIn(S, 5), iterationParse = S.Substring(s2 + 1); return(new CycleWhile(conditionParse, iterationParse, false)); } // reverse while if (p1 == 2 && S.Remove(p1) == "do") { int whilePos = MISC.IndexOfOnLevel0(S, "while", 0); if (whilePos < -1) { throw new Exception("No while, but used \"do\"\t " + MISC.StringFirstLetters(S, 20, true)); } string iterationParse = MISC.getIn(S, 2), conditionParse = MISC.getIn(S, s1); return(new CycleWhile(conditionParse, iterationParse, true)); } // FOR mazafaka if (operatorFind == "for") { string partsParse = MISC.getIn(S, 3), allOther = S.Substring(s2 + 1); string[] spp = partsParse.Split(';'); if (spp.Length != 3) { throw new Exception("Invalid count of FOR-cycle condition parts\t " + MISC.StringFirstLetters(S, 20, true)); } MISC.GoDeep("FOR"); this.MergeWith(new CommandOrder(spp[0], ',')); if (spp[1] == "") { spp[1] = "true"; // condition } CommandOrder actions = new CommandOrder(allOther, ';'); actions.MergeWith(new CommandOrder(spp[2], ',')); CycleFor cf = new CycleFor(spp[1], actions); MISC.GoBack(); return(cf); } throw new Exception("Can not parse a command\t " + MISC.StringFirstLetters(S, 20, true)); } return(new CommandOrder()); }
public CycleWhile(string parseCondition, CommandOrder actions, bool doFirst) { condition = new Equal(BinaryOperation.ParseFrom(parseCondition), new ASTvalue(new ValueType(VT.Cboolean), (object)true)); this.actions = actions; this.doFirst = doFirst; }
// 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)); }