Permalink
Browse files

AdventureSample - support multiple statements on a line

  • Loading branch information...
bobbymcr committed Apr 22, 2018
1 parent 7037cf1 commit cd7652bc16a11cc083309cc3f6b98a9ee1cf7657
@@ -518,39 +518,39 @@ public Lines()
this.references = new HashSet<int>();
}
public void Add(int line, SyntaxNode node)
public void Add(int number, SyntaxNode node)
{
this.statements.Add(line, new Line(line, node, null));
Line line;
if (!this.statements.TryGetValue(number, out line))
{
line = new Line(number);
this.statements.Add(number, line);
}
line.Add(node);
}
public void AddComment(int line, SyntaxTrivia comment)
public void AddComment(int number, SyntaxTrivia comment)
{
this.statements.Add(line, new Line(line, null, comment));
this.Add(number, SyntaxFactory.EmptyStatement().WithTrailingTrivia(comment));
}
public void AddGoto(int line, int destination)
public void AddGoto(int number, int destination)
{
var label = SyntaxFactory.IdentifierName(Label(destination));
var gotoStatement = SyntaxFactory.GotoStatement(SyntaxKind.GotoStatement, label);
this.Add(line, gotoStatement);
this.Add(number, gotoStatement);
this.references.Add(destination);
}
public IEnumerable<SyntaxNode> Statements()
{
SyntaxTrivia? previous = null;
foreach (Line line in this.statements.Values)
{
SyntaxTrivia? next = line.Comment;
if (next == null)
foreach (SyntaxNode node in line.Nodes(this.references))
{
foreach (SyntaxNode node in line.Nodes(this.references, previous))
{
yield return node;
}
yield return node;
}
previous = next;
}
}
@@ -562,41 +562,26 @@ private static string Label(int number)
private sealed class Line
{
private readonly int number;
private readonly SyntaxNode node;
private readonly SyntaxTrivia? comment;
private readonly List<SyntaxNode> nodes;
public Line(int number, SyntaxNode node, SyntaxTrivia? comment)
public Line(int number)
{
this.number = number;
this.node = node;
this.comment = comment;
this.nodes = new List<SyntaxNode>();
}
public SyntaxTrivia? Comment => this.comment;
public void Add(SyntaxNode node) => this.nodes.Add(node);
public IEnumerable<SyntaxNode> Nodes(ISet<int> references, SyntaxTrivia? previous)
public IEnumerable<SyntaxNode> Nodes(ISet<int> references)
{
SyntaxNode first = null;
SyntaxNode second = null;
if (references.Contains(this.number))
{
first = SyntaxFactory.LabeledStatement(Label(this.number), SyntaxFactory.EmptyStatement());
second = this.node;
}
else
{
first = this.node;
}
if (previous.HasValue)
{
first = first.WithLeadingTrivia(previous.Value);
yield return SyntaxFactory.LabeledStatement(Label(this.number), SyntaxFactory.EmptyStatement());
}
yield return first;
if (second != null)
foreach (SyntaxNode node in this.nodes)
{
yield return second;
yield return node;
}
}
}
@@ -87,7 +87,7 @@ private void CLS()
private bool Main()
{
this.Init();
// My first BASIC program
; // My first BASIC program
L20:
;
PRINT(""HELLO, WORLD!"");
@@ -0,0 +1,71 @@
// <copyright file="MultipleStatements.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>
namespace GWBas2CS.Test
{
using FluentAssertions;
using Xunit;
public sealed class MultipleStatements
{
[Fact]
public void TwoOnOneLine()
{
const string Input = @"10 PRINT ""HELLO, WORLD!"" : CLS";
const string Expected = @"*
private bool Main()
{
this.Init();
PRINT(""HELLO, WORLD!"");
CLS();
return false;
}
*";
string actual = Test.Translate("MyProg", Input);
actual.Should().Match(Expected);
}
[Fact]
public void ThreeOnOneLine()
{
const string Input = @"10 PRINT ""HELLO, WORLD!"" : CLS : CLS";
const string Expected = @"*
private bool Main()
{
this.Init();
PRINT(""HELLO, WORLD!"");
CLS();
CLS();
return false;
}
*";
string actual = Test.Translate("MyProg", Input);
actual.Should().Match(Expected);
}
[Fact]
public void ThreeOnOneLineCommentEnd()
{
const string Input = @"10 PRINT ""HELLO, WORLD!"" : CLS : REM A comment";
const string Expected = @"*
private bool Main()
{
this.Init();
PRINT(""HELLO, WORLD!"");
CLS();
; // A comment
return false;
}
*";
string actual = Test.Translate("MyProg", Input);
actual.Should().Match(Expected);
}
}
}

0 comments on commit cd7652b

Please sign in to comment.