Permalink
Browse files

AdventureSample - handle IF/THEN with many preceding stmts on same line

  • Loading branch information...
bobbymcr committed May 3, 2018
1 parent e05659d commit 8fee31c8792dbb3ae42489ed615b836ec7b162c3
@@ -105,6 +105,7 @@ public void Go(string name, int dest)
public void IfThen(BasicExpression cond, BasicStatement[] ifTrue)
{
this.lines.If(this.lineNumber);
foreach (BasicStatement stmt in ifTrue)
{
stmt.Accept(this);
@@ -113,7 +114,7 @@ public void IfThen(BasicExpression cond, BasicStatement[] ifTrue)
ExpressionNode expr = new ExpressionNode(this.generator, this.vars, this.methods);
cond.Accept(expr);
var nz = this.generator.ValueNotEqualsExpression(expr.Value, this.generator.LiteralExpression(0));
this.lines.AddIfThen(this.lineNumber, nz);
this.lines.Then(this.lineNumber, nz);
}
public void Input(string prompt, BasicExpression v)
@@ -1073,9 +1074,14 @@ public void AddComment(int number, SyntaxTrivia comment)
this.Get(number).Add(comment);
}
public void AddIfThen(int number, SyntaxNode cond)
public void If(int number)
{
this.Get(number).Wrap(n => this.generator.IfStatement(cond, n));
this.Get(number).Add(null);
}
public void Then(int number, SyntaxNode cond)
{
this.Get(number).Reduce(n => new SyntaxNode[] { this.generator.IfStatement(cond, n) });
}
public void AddGoto(int number, int destination)
@@ -1269,11 +1275,20 @@ public void Add(SyntaxTrivia comment)
}
}
public void Wrap(Func<IEnumerable<SyntaxNode>, SyntaxNode> wrap)
public void Reduce(Func<IEnumerable<SyntaxNode>, IEnumerable<SyntaxNode>> op)
{
SyntaxNode wrapped = wrap(this.nodes);
int i = 0;
while (this.nodes[i++] != null)
{
}
List<SyntaxNode> start = new List<SyntaxNode>(this.nodes.Take(i - 1));
List<SyntaxNode> end = new List<SyntaxNode>(this.nodes.Skip(i));
this.nodes.Clear();
this.nodes.Add(wrapped);
this.nodes.AddRange(start);
IEnumerable<SyntaxNode> reduced = op(end);
this.nodes.AddRange(reduced);
}
public IEnumerable<SyntaxNode> Nodes(ISet<int> references = null)
@@ -0,0 +1,39 @@
// <copyright file="IfThenStatements.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>
namespace GWBas2CS.Test
{
using FluentAssertions;
using Xunit;
public sealed class IfThenStatements
{
[Fact]
public void ManyStatementsWithTrailingIfThen()
{
const string Input = @"100 PRINT : CM$="""" : INPUT ""WHAT NOW"";CM$ : IF CM$="""" THEN 100";
const string Expected = @"*
private int Main()
{
this.Init();
L100:
;
PRINT("""");
CM_s = ("""");
CM_s = (INPUT_s(""WHAT NOW""));
if ((((CM_s.CompareTo("""")) == (0)) ? (-1) : (0)) != (0))
{
goto L100;
}
return 2;
}
*";
string actual = Test.Translate("MyProg", Input);
actual.Should().Match(Expected);
}
}
}

0 comments on commit 8fee31c

Please sign in to comment.