From 297db5a5318395d97f2320ed8840b3a35ad3a45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 16 Oct 2017 18:23:03 +0200 Subject: [PATCH] supplemental commit for BefunCompile [d12d26bb] --- BefunGen/AST/TextFungeParser.cs | 183 +++++++++++++++++++++++++++++++- BefunGen/Commandline/Runner.cs | 2 +- 2 files changed, 182 insertions(+), 3 deletions(-) diff --git a/BefunGen/AST/TextFungeParser.cs b/BefunGen/AST/TextFungeParser.cs index 5a5bdd7..cd6f0a2 100644 --- a/BefunGen/AST/TextFungeParser.cs +++ b/BefunGen/AST/TextFungeParser.cs @@ -217,8 +217,11 @@ public static string ExtractDisplayFromTFFormat(string sourcecode) var displayBuilder = new StringBuilder(); var inDisplayDefinition = false; - foreach (var line in lines) + var first = true; + foreach (var rawline in lines) { + var line = rawline.Trim(); + if (line.StartsWith("///")) { string content = line.Substring(3); @@ -231,10 +234,12 @@ public static string ExtractDisplayFromTFFormat(string sourcecode) } else { - if (displayBuilder.Length == 0) + if (first) displayBuilder.Append(content); else displayBuilder.Append("\n" + content); + + first = false; } } else @@ -243,6 +248,7 @@ public static string ExtractDisplayFromTFFormat(string sourcecode) { inDisplayDefinition = true; displayBuilder = new StringBuilder(); + first = true; } } } @@ -254,6 +260,179 @@ public static string ExtractDisplayFromTFFormat(string sourcecode) return string.Empty; } + public static bool UpdateDisplayInTFFormat(ref string sourcecode, string displayvalue) + { + if (string.IsNullOrWhiteSpace(displayvalue)) return RemoveDisplayInTFFormat(ref sourcecode); + + var inputLines = Regex.Split(sourcecode, @"\r?\n"); + var displayLines = Regex.Split(displayvalue, @"\r?\n"); + + #region Patch existing + { + StringBuilder output = new StringBuilder(); + + bool replaced = false; + bool inDisplayDefinition = false; + string displayindent = ""; + foreach (var rawline in inputLines) + { + var line = rawline.TrimStart(); + + if (line.StartsWith("///")) + { + string content = line.Substring(3); + + if (inDisplayDefinition) + { + if (content.Trim() == "") + { + // skip + inDisplayDefinition = false; + output.AppendLine(rawline); + continue; + } + else + { + // skip + continue; + } + } + else + { + if (content.Trim() == "" && !replaced) + { + // add display + output.AppendLine(rawline); + displayindent = line.Substring(0, line.IndexOf("///")); + foreach (var dl in displayLines) output.AppendLine(displayindent + "///" + dl); + inDisplayDefinition = true; + replaced = true; + } + else + { + // output + output.AppendLine(rawline); + } + } + } + else + { + // output + inDisplayDefinition = false; + output.AppendLine(rawline); + } + } + + if (replaced) + { + StringBuilderToStringWithoutDanglingNewline(output); + return true; + } + } + #endregion + + #region Create new + { + StringBuilder output = new StringBuilder(); + + bool replaced = false; + string displayindent = ""; + foreach (var rawline in inputLines) + { + var line = rawline.TrimStart(); + + if (line.Trim().ToLower().StartsWith("program ") && !replaced) + { + // add display + displayindent = rawline.Substring(0, rawline.IndexOf("program")); + output.AppendLine(displayindent + "///"); + foreach (var dl in displayLines) output.AppendLine(displayindent + "///" + dl); + output.AppendLine(displayindent + "///"); + replaced = true; + } + + // output + output.AppendLine(rawline); + } + + if (replaced) + { + StringBuilderToStringWithoutDanglingNewline(output); + return true; + } + } + #endregion + + return false; + } + + private static bool RemoveDisplayInTFFormat(ref string sourcecode) + { + var inputLines = Regex.Split(sourcecode, @"\r?\n"); + + StringBuilder output = new StringBuilder(); + + bool inDisplayDefinition = false; + foreach (var rawline in inputLines) + { + var line = rawline.TrimStart(); + + if (line.StartsWith("///")) + { + string content = line.Substring(3); + + if (inDisplayDefinition) + { + if (content.Trim() == "") + { + // skip + inDisplayDefinition = false; + continue; + } + else + { + // skip + continue; + } + } + else + { + if (content.Trim() == "") + { + // skip + inDisplayDefinition = true; + } + else + { + // output + output.AppendLine(rawline); + } + } + } + else + { + // output + inDisplayDefinition = false; + output.AppendLine(rawline); + } + } + + sourcecode = StringBuilderToStringWithoutDanglingNewline(output); + + return true; + } + + private static string StringBuilderToStringWithoutDanglingNewline(StringBuilder b) + { + var str = b.ToString(); + + if (str.EndsWith("\r\n")) return str.Substring(0, str.Length - 2); + + if (str.EndsWith("\n")) return str.Substring(0, str.Length - 1); + + return str; + } + public string GetGrammarDefinition() { return Resources.TextFunge_grm; diff --git a/BefunGen/Commandline/Runner.cs b/BefunGen/Commandline/Runner.cs index 7739188..25cbd19 100644 --- a/BefunGen/Commandline/Runner.cs +++ b/BefunGen/Commandline/Runner.cs @@ -62,7 +62,7 @@ private int RunDirect() var prog = parser.GenerateAst(inputCode); var env = new RunnerEnvironment(); - prog.RunDirect(env, ""); + prog.RunDirect(env, TextFungeParser.ExtractDisplayFromTFFormat(inputCode)); return 0; }