Skip to content

Commit

Permalink
AdventureSample - support IF/THEN
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Apr 23, 2018
1 parent a1fab6a commit 5419e9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 17 additions & 1 deletion projects/AdventureSample/src/GWBas2CS/BasicVisitor.cs
Expand Up @@ -85,7 +85,11 @@ public void Go(string name, int dest)

public void IfThen(BasicExpression cond, BasicStatement ifTrue)
{
throw new NotImplementedException();
ifTrue.Accept(this);
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);
}

public void Input(string prompt, BasicExpression v)
Expand Down Expand Up @@ -753,6 +757,11 @@ public void AddComment(int number, SyntaxTrivia comment)
this.Get(number).Add(comment);
}

public void AddIfThen(int number, SyntaxNode cond)
{
this.Get(number).Wrap(n => this.generator.IfStatement(cond, n));
}

public void AddGoto(int number, int destination)
{
var label = SyntaxFactory.IdentifierName(Label(destination));
Expand Down Expand Up @@ -884,6 +893,13 @@ public void Add(SyntaxTrivia comment)
}
}

public void Wrap(Func<IEnumerable<SyntaxNode>, SyntaxNode> wrap)
{
SyntaxNode wrapped = wrap(this.nodes);
this.nodes.Clear();
this.nodes.Add(wrapped);
}

public IEnumerable<SyntaxNode> Nodes(ISet<int> references)
{
if (references.Contains(this.number))
Expand Down
8 changes: 8 additions & 0 deletions projects/AdventureSample/test/GWBas2CS.Test/ExampleProgram.cs
Expand Up @@ -31,6 +31,7 @@ 140 CLS
170 GOSUB 2020
180 GOSUB 2030
190 INPUT A
200 IF A=1 THEN 190
1000 GOTO 20
2000 CLS
2010 RETURN
Expand Down Expand Up @@ -190,7 +191,14 @@ private int Main()
return 2;
}
L190:
;
A_n = (INPUT_n(""""));
if ((((A_n.CompareTo(1)) == (0)) ? (-1) : (0)) != (0))
{
goto L190;
}
goto L20;
return 2;
}
Expand Down

0 comments on commit 5419e9d

Please sign in to comment.