private int DoWhile(CodeLineCollection cc) { res.AddLine(Tabul(cc[0].IndentLevel) + "do{", lm); CodeLineCollection sup = cc.ExtractSubIndentation(cc[0].LineNumber); cc = cc.Extractor(sup.Count + 1); AnalyzeCodeLines(sup); if (Regex.IsMatch(cc[0].Code, @"quando [\w\W]+", RegexOptions.IgnoreCase)) { if (cc.Count == 1) { cc[0].Code = cc[0].Code.Trim(); int indx = cc[0].Code.IndexOf(' '); res.AddLine(Tabul(cc[0].IndentLevel) + "}while(" + AnalyzeCode(cc[0].Code.Substring(indx, cc[0].Code.Length - indx).Trim(), cc[0].LineNumber) + ");", lm); } else { lm.Add("Something gone wrong at this line", cc[0].LineNumber); } } else { lm.Add("The programm cannot find the 'quando' condition", cc[0].LineNumber); } return(0); }
private int ElseAction(CodeLineCollection cc, int IndentLevel) { res.AddLine(Tabul(IndentLevel) + "else{", lm); AnalyzeCodeLines(cc); res.AddLine(Tabul(IndentLevel) + "}", lm); return(cc.Count + 1); }
//Costruttore /// <summary> /// Frammento di funzione nel codice /// </summary> /// <param name="Functions">Funzione</param> /// <param name="CodeRows">Linee di codice</param> private FunctionSection(Function Function, CodeLineCollection CodeRows) { if (Function != null && CodeRows != null) { funct = Function; codes = CodeRows; } }
private void AnalyzeCodeLines(CodeLineCollection cc) { if (cc != null && cc.Count > 0) { if (Regex.IsMatch(cc[0].Code, @"crea (vettore )?[a-z][\w]*(\s?\{[\w\W]+\})* [\w\W]+", RegexOptions.IgnoreCase)) { CreateVariable(cc[0]); AnalyzeCodeLines(cc.Extractor(1)); } else if (Regex.IsMatch(cc[0].Code, @"cambia [a-z][\w]*(\s?\{[\w\W]+\})* [\w\W]+", RegexOptions.IgnoreCase)) { ChangeVariable(cc[0]); AnalyzeCodeLines(cc.Extractor(1)); } else if (Regex.IsMatch(cc[0].Code, @"se [\W\w]+", RegexOptions.IgnoreCase)) { cc = cc.Extractor(IfAction(cc.ExtractSubIndentation(cc[0].LineNumber, true))); if (Regex.IsMatch(cc[0].Code, @"altrimenti", RegexOptions.IgnoreCase)) { AnalyzeCodeLines(cc.Extractor(ElseAction(cc.ExtractSubIndentation(cc[0].LineNumber), cc[0].IndentLevel))); } } else if (Regex.IsMatch(cc[0].Code, @"controlla [\W\w]+", RegexOptions.IgnoreCase)) { AnalyzeCodeLines(cc.Extractor(SwitchAction(cc.ExtractSubIndentation(cc[0].LineNumber, true)))); } else if (Regex.IsMatch(cc[0].Code, @"ripeti quando [\w\W]+", RegexOptions.IgnoreCase)) { AnalyzeCodeLines(cc.Extractor(WhileAction(cc.ExtractSubIndentation(cc[0].LineNumber, true)))); } else if (Regex.IsMatch(cc[0].Code, @"ripeti [\w\W]+ volte", RegexOptions.IgnoreCase)) { AnalyzeCodeLines(cc.Extractor(ForAction(cc.ExtractSubIndentation(cc[0].LineNumber, true)))); } else if (Regex.IsMatch(cc[0].Code, @"ripeti", RegexOptions.IgnoreCase)) { CodeLineCollection sup = cc.ExtractSubIndentation(cc[0].LineNumber, true); sup.Add(cc.Extractor(sup.Count)[0]); AnalyzeCodeLines(cc.Extractor(DoWhile(sup))); } else if (Regex.IsMatch(cc[0].Code, @"ritorna [\w\W]+", RegexOptions.IgnoreCase)) { ReturnAction(cc[0]); AnalyzeCodeLines(cc.Extractor(1)); } else if (Regex.IsMatch(cc[0].Code, @"[\w\W]+", RegexOptions.IgnoreCase)) { FunctionAction(cc[0]); AnalyzeCodeLines(cc.Extractor(1)); } else { lm.Add("Nothing is possible to translate at this line", cc[0].LineNumber); } } }
//Metodi statici public static FunctionSection ExtractSection(string RawCode, int LineNumber, LogManager lm) { Function f = Function.GetFunctionByCode(RawCode, lm, LineNumber); RawCode = CodeElaborator.RemoveFunctionSign(RawCode, lm); CodeLineCollection cc = CodeLineCollection.ExtractCollection(RawCode, LineNumber, lm); return(new FunctionSection(f, cc)); }
private int SwitchAction(CodeLineCollection cc) { cc[0].Code = cc[0].Code.Trim(); int indx = cc[0].Code.IndexOf(' ') + 1; string name = cc[0].Code.Substring(indx, cc[0].Code.Length - indx); CaseAction(cc.Extractor(1), name); return(cc.Count); }
//Metodi statici public static CodeLineCollection ExtractCollection(string RawCode, int LineNumber, LogManager lm) { CodeLineCollection cc = new CodeLineCollection(); string[] line = RawCode.Split('\n'); for (int i = 0; i < line.Length; i++) { cc.Add(CodeLine.LineExtractor(line[i], LineNumber + i, lm)); } return(cc); }
private int IfAction(CodeLineCollection cc) { cc[0].Code = cc[0].Code.Trim(); int indx = cc[0].Code.IndexOf(' ') + 1; res.AddLine(Tabul(cc[0].IndentLevel) + "if(" + AnalyzeCode(cc[0].Code.Substring(indx, cc[0].Code.Length - indx).Trim(), cc[0].LineNumber) + "){", lm); AnalyzeCodeLines(cc.Extractor(1)); res.AddLine(Tabul(cc[0].IndentLevel) + "}", lm); return(cc.Count); }
private int WhileAction(CodeLineCollection cc) { cc[0].Code = cc[0].Code.Trim(); int indx = Regex.Match(cc[0].Code, "ripeti quando ", RegexOptions.IgnoreCase).Length; res.AddLine(Tabul(cc[0].IndentLevel) + "while(" + AnalyzeCode(cc[0].Code.Substring(indx, cc[0].Code.Length - indx).Trim(), cc[0].LineNumber) + "){", lm); AnalyzeCodeLines(cc.Extractor(1)); res.AddLine(Tabul(cc[0].IndentLevel) + "}", lm); return(cc.Count); }
//Metodi di estrazione /// <summary> /// Permette di estrarre una sottocollezione dalla collezione stessa /// </summary> /// <param name="start">indice di partenza</param> /// <param name="lenght">numero di elementi da estrarre</param> /// <returns>Collezione estratta</returns> public CodeLineCollection Extractor(int start, int lenght) { if (start > 0 && lenght > 0 && start + lenght <= this.Count) { CodeLineCollection cc = new CodeLineCollection(); for (int i = start; i < start + lenght; i++) { cc.Add(this[i]); } return(cc); } else { return(new CodeLineCollection()); } }
private void CaseAction(CodeLineCollection cc, string varName) { if (cc != null && cc.Count > 0) { cc[0].Code = cc[0].Code.Trim(); if (Regex.IsMatch(cc[0].Code, @"se [\W\w]+", RegexOptions.IgnoreCase)) { int indx = cc[0].Code.IndexOf(' ') + 1; res.AddLine(Tabul(cc[0].IndentLevel) + "if(" + AnalyzeCode(varName + " " + cc[0].Code.Substring(indx, cc[0].Code.Length - indx).Trim(), cc[0].LineNumber) + "){", lm); CodeLineCollection sup = cc.ExtractSubIndentation(cc[0].LineNumber); AnalyzeCodeLines(sup); res.AddLine(Tabul(cc[0].IndentLevel) + "}else ", lm); sup = cc.Extractor(sup.Count + 1); if (sup == null || sup.Count <= 0) { lm.Add("The translator cannot find the 'Altrimenti' tag"); } CaseAction(sup, varName); } else if (Regex.IsMatch(cc[0].Code, @"altrimenti", RegexOptions.IgnoreCase)) { int indx = cc[0].Code.IndexOf(' ') + 1; res.AddLine(Tabul(cc[0].IndentLevel) + "{", lm); CodeLineCollection sup = cc.ExtractSubIndentation(cc[0].LineNumber); AnalyzeCodeLines(sup); res.AddLine(Tabul(cc[0].IndentLevel) + "}", lm); if (cc.Extractor(sup.Count + 1).Count > 0) { lm.Add("The 'altrimenti' must be the last tag in the switch"); } } else { lm.Add("Cannot translate the line in the switch", cc[0].LineNumber); } } }
private int ForAction(CodeLineCollection cc) { cc[0].Code = cc[0].Code.Trim(); int indx = Regex.Match(cc[0].Code, @"ripeti ", RegexOptions.IgnoreCase).Length; int lnght = Regex.Match(cc[0].Code, @" volte ", RegexOptions.IgnoreCase).Index; string name = "indice"; if (Regex.IsMatch(cc[0].Code, @"ripeti [\w\W]+ volte [a-zA-Z][\w]*", RegexOptions.IgnoreCase)) { int indx2 = Regex.Match(cc[0].Code, @"ripeti [\w\W]+ volte ", RegexOptions.IgnoreCase).Length; name = cc[0].Code.Substring(indx2, cc[0].Code.Length - indx2); } res.AddLine(Tabul(cc[0].IndentLevel) + "for( Variable " + name + " = new (\"" + name + "\", 0) ; " + name + " < " + AnalyzeCode(cc[0].Code.Substring(indx, lnght - indx).Trim(), cc[0].LineNumber) + " ; " + name + " += (new Variable(\"_\",1)) ){", lm); vc.Add(new Variable(name, "")); AnalyzeCodeLines(cc.Extractor(1)); vc.Remove(new Variable(name, "")); res.AddLine(Tabul(cc[0].IndentLevel) + "{", lm); return(cc.Count); }
/// <summary> /// Estrae tutte le linee con indentazione maggiore a quella indicata /// </summary> /// <param name="LineNumber">Numero di linea da cui partire (non compresa)</param> /// <returns>Lista a indentazione maggiore</returns> public CodeLineCollection ExtractSubIndentation(int LineNumber, bool comprendi = false) { int indx = FindLine(LineNumber), i = 0; if (indx > -1) { CodeLineCollection clc = new CodeLineCollection(); i = indx + 1; if (comprendi) { clc.Add(this[indx]); } while (i < this.Count && this[indx].IndentLevel < this[i].IndentLevel) { clc.Add(this[i]); i++; } return(clc); } else { return(null); } }