Skip to content

Commit

Permalink
AdventureSample - support LEN
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Apr 24, 2018
1 parent 7a04b08 commit b952d05
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
20 changes: 19 additions & 1 deletion projects/AdventureSample/src/GWBas2CS/BasicVisitor.cs
Expand Up @@ -416,7 +416,11 @@ public void Operator(string name, BasicExpression[] operands)
{ {
operands[0].Accept(this); operands[0].Accept(this);
SyntaxNode x = this.Value; SyntaxNode x = this.Value;
if (operands.Length == 2) if (operands.Length == 1)
{
this.Value = this.Unary(name, x);
}
else if (operands.Length == 2)
{ {
operands[1].Accept(this); operands[1].Accept(this);
SyntaxNode y = this.Value; SyntaxNode y = this.Value;
Expand All @@ -438,6 +442,15 @@ private SyntaxNode Cast(SyntaxNode node)
return this.generator.CastExpression(this.generator.TypeExpression(SpecialType.System_Int32), node); return this.generator.CastExpression(this.generator.TypeExpression(SpecialType.System_Int32), node);
} }


private SyntaxNode Unary(string name, SyntaxNode x)
{
switch (name)
{
case "Len": return this.Len(x);
default: throw new NotSupportedException("Operator:" + name);
}
}

private SyntaxNode Binary(string name, SyntaxNode x, SyntaxNode y) private SyntaxNode Binary(string name, SyntaxNode x, SyntaxNode y)
{ {
switch (name) switch (name)
Expand Down Expand Up @@ -465,6 +478,11 @@ private SyntaxNode Cond(Func<SyntaxNode, SyntaxNode, SyntaxNode> cond, SyntaxNod
var neg1 = this.generator.LiteralExpression(-1); var neg1 = this.generator.LiteralExpression(-1);
return this.generator.ConditionalExpression(cond(call, zero), neg1, zero); return this.generator.ConditionalExpression(cond(call, zero), neg1, zero);
} }

private SyntaxNode Len(SyntaxNode x)
{
return this.generator.MemberAccessExpression(x, "Length");
}
} }


private sealed class Variables private sealed class Variables
Expand Down
48 changes: 48 additions & 0 deletions projects/AdventureSample/test/GWBas2CS.Test/LenExpressions.cs
@@ -0,0 +1,48 @@
// <copyright file="LenExpressions.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace GWBas2CS.Test
{
using FluentAssertions;
using Xunit;

public sealed class LenExpressions
{
[Fact]
public void OneStringVar()
{
const string Input = @"10 A=LEN(A$)";
const string Expected = @"*
private int Main()
{
this.Init();
A_n = (A_s.Length);
return 2;
}
*";

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

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

[Fact]
public void OneStringExpr()
{
const string Input = @"10 A=LEN(A$+""x"")";
const string Expected = @"*
private int Main()
{
this.Init();
A_n = (((A_s) + (""x"")).Length);
return 2;
}
*";

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

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

0 comments on commit b952d05

Please sign in to comment.