Permalink
Browse files

AdventureSample - support LEN

  • Loading branch information...
bobbymcr committed Apr 24, 2018
1 parent 7a04b08 commit b952d05a89ebee2d61b8278f8d647b4d76c76d52
@@ -416,7 +416,11 @@ public void Operator(string name, BasicExpression[] operands)
{
operands[0].Accept(this);
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);
SyntaxNode y = this.Value;
@@ -438,6 +442,15 @@ private SyntaxNode Cast(SyntaxNode 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)
{
switch (name)
@@ -465,6 +478,11 @@ private SyntaxNode Cond(Func<SyntaxNode, SyntaxNode, SyntaxNode> cond, SyntaxNod
var neg1 = this.generator.LiteralExpression(-1);
return this.generator.ConditionalExpression(cond(call, zero), neg1, zero);
}
private SyntaxNode Len(SyntaxNode x)
{
return this.generator.MemberAccessExpression(x, "Length");
}
}
private sealed class Variables
@@ -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.