Skip to content

Commit

Permalink
AdventureSample - handle FOR/NEXT with many preceding stmts on same line
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed May 3, 2018
1 parent 8fee31c commit 30ba55f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
14 changes: 13 additions & 1 deletion projects/AdventureSample/src/GWBas2CS/BasicVisitor.cs
Expand Up @@ -1169,6 +1169,7 @@ public IEnumerable<SyntaxNode> Main()

public void For(int number, SyntaxNode vx, SyntaxNode sx, SyntaxNode ex, SyntaxNode sp)
{
this.Get(number).Add(null);
this.loops.Push(number, vx, sx, ex, sp);
}

Expand All @@ -1183,7 +1184,18 @@ public void Next(int end, SyntaxNode vx)
{
break;
}
else if (n >= start)
else if (n == start)
{
IEnumerable<SyntaxNode> nodes = null;
Line line = this.statements[n];
line.Reduce(ns =>
{
nodes = ns;
return Enumerable.Empty<SyntaxNode>();
});
body.AddRange(nodes);
}
else if (n > start)
{
body.AddRange(this.statements[n].Nodes());
this.statements.Remove(n);
Expand Down
28 changes: 28 additions & 0 deletions projects/AdventureSample/test/GWBas2CS.Test/ForNextStatements.cs
Expand Up @@ -9,6 +9,34 @@ namespace GWBas2CS.Test

public sealed class ForNextStatements
{
[Fact]
public void ManyStatementsOneLine()
{
const string Input =
@"10 CLS : FOR I=1 TO 5
20 A=I*2
30 NEXT I";
const string Expected = @"*
private int Main()
{
this.Init();
CLS();
I_n = (1);
while ((I_n) <= (5))
{
A_n = ((I_n) * (2));
I_n = ((I_n) + (1));
}
return 2;
}
*";

string actual = Test.Translate("MyProg", Input);

actual.Should().Match(Expected);
}

[Fact]
public void TwoNested()
{
Expand Down

0 comments on commit 30ba55f

Please sign in to comment.