public List <string> WHILE(List <string> Lines) { List <string> prog = new List <string>(Lines); string openclose = ""; int count = 0; string[] args = Lines[0].Substring(Lines[0].IndexOf("(") + 1, Lines[0].LastIndexOf(")") - 1 - Lines[0].IndexOf("(")).Split(';'); while (Cond.IsTrue("(" + args[0] + ")")) { //Task.Delay(10); if (prog[count].IndexOf(Refer.Do) > -1) { openclose += "{"; } //prog.RemoveAt(0); count++; if (prog[count].IndexOf(Refer.Do) > -1) { openclose += "{"; } List <string> _prog = new List <string>(); foreach (string s in prog) { _prog.Add(s); } while ((prog[count].IndexOf(Refer.Then) == -1 && prog[count].IndexOf(Refer.Do) == -1) && openclose != "{") { if (_prog[count].IndexOf(Refer.End) > -1) { _prog[count] = _prog[count].Replace(Refer.End, " "); openclose += "}"; openclose = openclose.Replace("{}", ""); if (openclose == "") { break; } } if (prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { prog[0] }); } Ex.Execute(_prog[count]); _prog.RemoveAt(count); if (_prog.LongCount() - count == 1) { break; } } if (prog[count].IndexOf(Refer.If) > -1 && prog[count].IndexOf(Refer.Then) > -1) { List <string> pro = new List <string>(); for (int i = 0; count + i < prog.LongCount(); i++) { pro.Add(prog[i + count]); } pro = IF.IF(pro); if (pro[0].IndexOf(Refer.Return) > -1) { return(new List <string> { pro[0] }); } for (int j = 0; j < pro.LongCount(); j++) { prog.RemoveAt(count); prog.Add(pro[j]); } } if (prog[count].IndexOf(Refer.For) > -1 && prog[count].IndexOf(Refer.Do) > -1) { List <string> pro = new List <string>(); for (int i = 0; count + i < prog.LongCount(); i++) { pro.Add(prog[i + count]); } pro = FOR.FOR(pro); if (pro[0].IndexOf(Refer.Return) > -1) { return(new List <string> { pro[0] }); } for (int j = 0; j < pro.LongCount(); j++) { prog.RemoveAt(count); prog.Add(pro[j]); } } if (prog[count].IndexOf(Refer.While) > -1 && prog[count].IndexOf(Refer.Do) > -1) { List <string> pro = new List <string>(); for (int i = 0; count + i < prog.LongCount(); i++) { pro.Add(prog[i + count]); } pro = WHILE(pro); if (pro[0].IndexOf(Refer.Return) > -1) { return(new List <string> { pro[0] }); } for (int j = 0; j < pro.LongCount(); j++) { prog.RemoveAt(count); prog.Add(pro[j]); } } count = 0; } for (; count > 0; count--) { prog.RemoveAt(0); } return(prog); }
// recursive if func public List <string> IF(List <string> prog) { string openclose = ""; if (Cond.IsTrue(prog[0])) { if (prog[0].IndexOf(Refer.Then) > -1 || prog[0].IndexOf(Refer.Else) > -1 || prog[0].IndexOf(Refer.Do) > -1) { openclose += "{"; } prog.RemoveAt(0); if (prog[0].IndexOf(Refer.Then) > -1 || prog[0].IndexOf(Refer.Else) > -1 || prog[0].IndexOf(Refer.Do) > -1) { openclose += "{"; } while (prog[0] != Refer.Else && (prog[0].IndexOf(Refer.If) == -1 || prog[0].IndexOf(Refer.While) == -1 || prog[0].IndexOf(Refer.For) == -1) && openclose != "") { if (prog[0].IndexOf(Refer.End) > -1) { openclose += "}"; openclose = openclose.Replace("{}", ""); if (openclose == "") { break; } } if (prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { prog[0] }); } Ex.Execute(prog[0]); prog.RemoveAt(0); if (prog.LongCount() == 0) { return(prog); } } if (prog[0].IndexOf(Refer.Else) > -1) { while (prog[0].IndexOf(Refer.End) < 0 && prog[0].IndexOf(Refer.Then) < 0 && prog[0].IndexOf(Refer.Do) < 0) { prog.RemoveAt(0); if (prog.LongCount() == 0) { return(prog); } } } if (prog[0].IndexOf(Refer.If) > -1 && prog[0].IndexOf(Refer.Then) > -1) { prog = IF(prog); } if (prog[0].IndexOf(Refer.For) > -1 && prog[0].IndexOf(Refer.Do) > -1) { prog = FOR.FOR(prog); } if (prog[0].IndexOf(Refer.While) > -1 && prog[0].IndexOf(Refer.Do) > -1) { prog = WHILE.WHILE(prog); } } else { prog.RemoveAt(0); while (prog[0] != Refer.Else && prog[0].IndexOf(Refer.Then) < 0 && prog[0].IndexOf(Refer.Do) < 0) { prog.RemoveAt(0); if (prog.LongCount() == 0) { return(prog); } } if (prog[0].IndexOf(Refer.Else) > -1) { while (prog[0].IndexOf(Refer.End) < 0 && prog[0].IndexOf(Refer.Then) < 0 && prog[0].IndexOf(Refer.Do) < 0) { if (prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { prog[0] }); } Ex.Execute(prog[0]); prog.RemoveAt(0); if (prog.LongCount() == 0) { return(prog); } } } if (prog[0].IndexOf(Refer.If) > -1 && prog[0].IndexOf(Refer.Then) > -1) { prog = IF(prog); if (prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { prog[0] }); } } if (prog[0].IndexOf(Refer.For) > -1 && prog[0].IndexOf(Refer.Do) > -1) { prog = FOR.FOR(prog); if (prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { prog[0] }); } } if (prog[0].IndexOf(Refer.While) > -1 && prog[0].IndexOf(Refer.Do) > -1) { prog = WHILE.WHILE(prog); if (prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { prog[0] }); } } } return(prog); }
public List <string> FOR(List <string> Lines) { List <string> _prog = new List <string>(); string openclose = ""; int count = 0; while (!(Lines[0].IndexOf(Refer.For) > -1) && (Lines[0].IndexOf(Refer.Do) > -1) && Lines.LongCount() > 0) { Lines.RemoveAt(0); } string[] args = Lines[0].Substring(Lines[0].IndexOf("(") + 1, Lines[0].LastIndexOf(")") - 1 - Lines[0].IndexOf("(")).Split(';'); if ((int)(args.LongCount()) != 3) { ExM.Get(Refer.Print).DynamicInvoke(Refer.ErrorHead + Refer.ErrorFor + "" + Refer.Do); return(null); } //prog.RemoveAt(0); if (Vars.Replace(args[0]) == args[0]) { Variables.VariablesStruct v = new Variables.VariablesStruct(); v.Type = "Local"; v.Name = args[0]; v.Value = args[1]; Vars.VarAdd(v); } for (; Cond.IsTrue("(" + args[0].Replace(" ", "") + " != " + args[2].Replace(" ", "") + ")");) { Task.Delay(10); if (Lines[count].IndexOf(Refer.End) > -1) { openclose += "{"; } //prog.RemoveAt(0); count++; if (Lines[count].IndexOf(Refer.Do) > -1 && Lines[count].IndexOf(Refer.Then) > -1) { openclose += "{"; } _prog = new List <string>(); foreach (string s in Lines) { _prog.Add(s); } while ((_prog[count].IndexOf(Refer.Then) == -1 && _prog[count].IndexOf(Refer.Do) == -1) && openclose != "{") { if (_prog[count].IndexOf(Refer.End) > -1) { _prog[count] = _prog[count].Replace(Refer.End, " "); openclose += "}"; openclose = openclose.Replace("{}", ""); if (openclose == "") { break; } } _prog[count] = _prog[count].Replace(Refer.Else, "").Replace(Refer.If, "").Replace(Refer.For, "").Replace(Refer.While, "").Replace(Refer.Then, "").Replace(Refer.Do, ""); if (_prog[0].IndexOf(Refer.Return) > -1) { return(new List <string> { _prog[0] }); } Ex.Execute(_prog[count]); count++; //_prog.RemoveAt(count); if (_prog.LongCount() - count == 1)// 1 for the "end" line { //Vars.VarRem(args[0]); //count = 0; break; } } if (_prog[count].IndexOf(Refer.If) > -1 && _prog[count].IndexOf(Refer.Then) > -1) { List <string> pro = new List <string>(); for (int i = 0; count + i < _prog.LongCount(); i++) { pro.Add(_prog[i + count]); } pro = IF.IF(pro); if (pro[0].IndexOf(Refer.Return) > -1) { return(new List <string> { pro[0] }); } for (int j = 0; j < pro.LongCount(); j++) { count++; } //prog.RemoveAt(count); prog.Add(pro[j]); } if (_prog[count].IndexOf(Refer.For) > -1 && _prog[count].IndexOf(Refer.Do) > -1) { List <string> pro = new List <string>(); for (int i = 0; count + i < _prog.LongCount(); i++) { pro.Add(Lines[i + count]); } pro = FOR(pro); if (pro[0].IndexOf(Refer.Return) > -1) { return(new List <string> { pro[0] }); } for (int j = 0; j < pro.LongCount(); j++) { count++; } } if (_prog[count].IndexOf(Refer.While) > -1 && _prog[count].IndexOf(Refer.Do) > -1) { List <string> pro = new List <string>(); for (int i = 0; count + i < _prog.LongCount(); i++) { pro.Add(Lines[i + count]); } pro = WHILE.WHILE(pro); if (pro[0].IndexOf(Refer.Return) > -1) { return(new List <string> { pro[0] }); } for (int j = 0; j < pro.LongCount(); j++) { count++; } } count = 0; //Ex.Execute(_prog[count]); if (Convert.ToInt64(args[1]) < Convert.ToInt64(args[2])) { Ex.Execute(args[0] + "++;"); } else { if (Convert.ToInt64(args[1]) > Convert.ToInt64(args[2])) { Ex.Execute(args[0] + "--;"); } } } if (_prog.LongCount() > 0) { _prog.RemoveAt(0); } Vars.VarRem(args[0]); return(_prog); }