public override int GetHashCode() { if (_isCalculatingHasCode) { return(0); } lock (_hashCodeLock) { if (_isCalculatingHasCode) { return(0); } _isCalculatingHasCode = true; int value; unchecked { value = (base.GetHashCode() * 397) ^ (LoopProperty != null ? LoopProperty.GetHashCode() : 0); } _isCalculatingHasCode = false; return(value); } }
public static void Main (string[] args) { Stopwatch swch1 = new Stopwatch (); Stopwatch swch2 = new Stopwatch (); Console.WriteLine ("Parsing sentenced program code..."); swch1.Start (); String[] lines = File.ReadAllLines (args.Length > 0 ? args[0] : "input"); String output = ""; /*foreach (String i in includes) output += "#include <" + i + ">" + Environment.NewLine;*/ String tabs = " "; String programName = ""; Dictionary<String, String> vars = new Dictionary<String, String> (); List<LoopProperty> inLoop = new List<LoopProperty> (); foreach (String s in lines) { if (s.Length == 0) continue; String ln = s; while (ln.StartsWith(" ")) ln = ln.Substring (1); while (ln.StartsWith("\t")) ln = ln.Substring (1); output += Environment.NewLine + tabs + "// " + ln + Environment.NewLine; if (ln.StartsWith ("Kérem")) { String include = ln.Split (new [] { " " }, StringSplitOptions.None) [1]; if (includes.Contains (include)) output += "#include <" + include + ">" + Environment.NewLine; else output += "// <UNKNOWN INCLUDE: \"" + include + "\"" + Environment.NewLine; continue; } if (ln.StartsWith ("Program") && !ln.StartsWith("Program vége")) { output += Environment.NewLine + "using namespace std;" + Environment.NewLine; output += "int main()" + Environment.NewLine; output += "{" + Environment.NewLine; programName = ln.Split ('"') [1]; continue; } if (ln.StartsWith ("Legyen")) { String[] lv = ln.Split (' ') [1].Split (','); foreach (String str in lv) { vars.Add (str, ln.Split (' ') [2]); output += tabs + ln.Split (' ') [2] + " " + str + ";" + Environment.NewLine; } continue; } if (ln.StartsWith ("Ki")) { if (ln.Contains ("\"")) output += tabs + "cout << \"" + ln.Split ('"') [1] + "\";" + Environment.NewLine; else output += tabs + "cout << " + ln.Split (' ') [1] + ";" + Environment.NewLine; continue; } if (ln.StartsWith ("Be")) { output += tabs + "cin >> " + ln.Split (' ') [1] + ";" + Environment.NewLine; continue; } if (ln.StartsWith ("Ha")) { if (ln.Split (' ').Last () == "vége") { tabs = tabs.Substring (4); output += tabs + "}" + Environment.NewLine; continue; } output += tabs + "if (" + ln.Split(new [] { "akkor" }, StringSplitOptions.None).First().Split(new [] { "Ha" }, StringSplitOptions.None).Last().Replace("vagy", "||").Replace("és", "&&") + ")" + Environment.NewLine + tabs + "{" + Environment.NewLine; tabs += " "; continue; } if (ln.StartsWith ("Egyébként")) { tabs = tabs.Substring (4); output += tabs + "}" + Environment.NewLine; output += tabs + "else" + Environment.NewLine; output += tabs + "{" + Environment.NewLine; tabs += " "; continue; } if (ln.StartsWith ("Ciklus")) { bool didAThing = false; foreach (LoopProperty p in inLoop) { if (p.Name == ln.Split(' ')[0]); { if (p.Type == 1 || p.Type == 2) { tabs = tabs.Substring (4); output += tabs + "}" + Environment.NewLine; } else if (p.Type == 3) { tabs = tabs.Substring (4); output += tabs + "}" + Environment.NewLine; output += tabs + "while (" + ln.Split (new [] { "amíg " }, StringSplitOptions.None) [1].Replace("vagy", "||").Replace("és", "&&") + ");" + Environment.NewLine + Environment.NewLine; } didAThing = true; inLoop.Remove (p); break; } } if (didAThing) continue; // Parse "Ciklus* amíg ch == 'i' vagy ch == 'I'" LoopProperty lp = new LoopProperty (); lp.Name = ln.Split (' ') [0]; lp.Type = 3; if (ln.Split (' ').Length > 1) { if (ln.Split (' ') [1] == "amíg") { lp.Instruction = ln.Split (new [] { "amíg " }, StringSplitOptions.None) [1]; lp.Type = 1; output += tabs + "while (" + lp.Instruction + ")" + Environment.NewLine + tabs + "{" + Environment.NewLine; } else if (ln.Contains ("-ig")) { lp.Instruction = "int cv" + (tabs.Length - 4 / 4) + " = " + ln.Split ('-') [0].Split (' ') [1] + "; cv" + (tabs.Length - 4 / 4) + " <= " + ln.Split ('-') [1].Split (' ') [1] + "; cv" + (tabs.Length - 4 / 4) + "++"; lp.Type = 2; output += tabs + "for (" + lp.Instruction + ")" + Environment.NewLine + tabs + "{" + Environment.NewLine; } } else output += tabs + "do" + Environment.NewLine + tabs + "{" + Environment.NewLine; inLoop.Add (lp); tabs += " "; continue; } foreach (String str in vars.Keys) if (ln.StartsWith (str)) { output += tabs + ln + ";" + Environment.NewLine; break; } } output += tabs + "return 0;" + Environment.NewLine; output += "}" + Environment.NewLine; File.WriteAllText (programName.Replace(" ", "_") + ".cpp", output); Console.WriteLine ("Program '{0}' parsed successfully.", programName); Console.WriteLine ("Compiling binary..."); Process pcrs = new Process(); pcrs.StartInfo = new ProcessStartInfo("g++", programName.Replace(" ", "_") + ".cpp" + " -o " + programName.Replace(" ", "_")); swch2.Start (); pcrs.Start (); while (!pcrs.HasExited) ; swch2.Stop (); swch1.Stop (); Console.WriteLine ("Compilation time: {0}ms", swch2.Elapsed.TotalMilliseconds); Console.WriteLine ("Total processing time: {0}ms", swch1.Elapsed.TotalMilliseconds); }