Skip to content

Commit

Permalink
supplemental commit for BefunCompile [d12d26bb]
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescher committed Oct 16, 2017
1 parent d6d9ea2 commit 297db5a
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 3 deletions.
183 changes: 181 additions & 2 deletions BefunGen/AST/TextFungeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -243,6 +248,7 @@ public static string ExtractDisplayFromTFFormat(string sourcecode)
{
inDisplayDefinition = true;
displayBuilder = new StringBuilder();
first = true;
}
}
}
Expand All @@ -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 <DISPLAY>
{
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() == "</DISPLAY>")
{
// skip
inDisplayDefinition = false;
output.AppendLine(rawline);
continue;
}
else
{
// skip
continue;
}
}
else
{
if (content.Trim() == "<DISPLAY>" && !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 <DISPLAY>
{
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 + "///<DISPLAY>");
foreach (var dl in displayLines) output.AppendLine(displayindent + "///" + dl);
output.AppendLine(displayindent + "///</DISPLAY>");
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() == "</DISPLAY>")
{
// skip
inDisplayDefinition = false;
continue;
}
else
{
// skip
continue;
}
}
else
{
if (content.Trim() == "<DISPLAY>")
{
// 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;
Expand Down
2 changes: 1 addition & 1 deletion BefunGen/Commandline/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 297db5a

Please sign in to comment.