Skip to content

Commit

Permalink
ExpressionSample - use Visitor pattern for BasicExpression.ToString
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Apr 15, 2018
1 parent 2f84030 commit 8ce466c
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 18 deletions.
3 changes: 0 additions & 3 deletions projects/ExpressionSample/GWExpr.Test/GWExpr.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Remove="ve-BF3F.tmp" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.2" />
Expand Down
5 changes: 2 additions & 3 deletions projects/ExpressionSample/GWExpr/BasicArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public static BasicArray Str(string name, IEnumerable<BasicExpression> subs)
return new BasicArray(BasicType.Str, name, subs);
}

public override string ToString()
public override void Accept(IVisitor visit)
{
var list = string.Join<BasicExpression>(", ", this.subs);
return this.type + "Arr(" + this.name + ", " + list + ")";
visit.Array(this.type, this.name, this.subs);
}
}
}
80 changes: 80 additions & 0 deletions projects/ExpressionSample/GWExpr/BasicExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,92 @@

namespace GWExpr
{
using System.Text;

public abstract class BasicExpression
{
protected BasicExpression()
{
}

public static BasicExpression FromString(string input) => Expr.FromString(input);

public abstract void Accept(IVisitor visit);

public override string ToString()
{
ExpressionString str = new ExpressionString();
this.Accept(str);
return str.ToString();
}

private sealed class ExpressionString : IVisitor
{
private readonly StringBuilder sb;

public ExpressionString()
{
this.sb = new StringBuilder();
}

public void Literal(BasicType type, object o)
{
this.sb.Append(type.ToString()).Append("L(");

string s = o.ToString();
if (type == BasicType.Str)
{
s = "\"" + s + "\"";
}

this.sb.Append(s);

this.sb.Append(")");
}

public void Variable(BasicType type, string name)
{
this.sb.Append(type.ToString()).Append("V(");

this.sb.Append(name);

this.sb.Append(")");
}

public void Array(BasicType type, string name, BasicExpression[] subs)
{
this.sb.Append(type.ToString()).Append("Arr(");

this.sb.Append(name);

foreach (BasicExpression sub in subs)
{
this.sb.Append(", ");
sub.Accept(this);
}

this.sb.Append(")");
}

public void Operator(string name, BasicExpression[] operands)
{
this.sb.Append(name).Append("(");

foreach (BasicExpression op in operands)
{
op.Accept(this);
this.sb.Append(", ");
}

this.sb.Remove(this.sb.Length - 2, 2);

this.sb.Append(")");
}

public override string ToString()
{
return this.sb.ToString();
}
}
}
}
10 changes: 2 additions & 8 deletions projects/ExpressionSample/GWExpr/BasicLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ private BasicLiteral(BasicType type, object o)

public static BasicLiteral Str(string s) => new BasicLiteral(BasicType.Str, s);

public override string ToString()
public override void Accept(IVisitor visit)
{
string s = this.o.ToString();
if (this.type == BasicType.Str)
{
s = "\"" + s + "\"";
}

return this.type + "L(" + s + ")";
visit.Literal(this.type, this.o);
}
}
}
2 changes: 1 addition & 1 deletion projects/ExpressionSample/GWExpr/BasicType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GWExpr
{
internal enum BasicType
public enum BasicType
{
None = 0,
Num,
Expand Down
5 changes: 4 additions & 1 deletion projects/ExpressionSample/GWExpr/BasicVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ private BasicVariable(BasicType type, string name)

public static BasicVariable Str(string name) => new BasicVariable(BasicType.Str, name);

public override string ToString() => this.type + "V(" + this.name + ")";
public override void Accept(IVisitor visit)
{
visit.Variable(this.type, this.name);
}
}
}
17 changes: 17 additions & 0 deletions projects/ExpressionSample/GWExpr/IVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <copyright file="IVisitor.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace GWExpr
{
public interface IVisitor
{
void Literal(BasicType type, object o);

void Variable(BasicType type, string name);

void Array(BasicType type, string name, BasicExpression[] subs);

void Operator(string name, BasicExpression[] operands);
}
}
4 changes: 2 additions & 2 deletions projects/ExpressionSample/GWExpr/OperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public static OperatorExpression Ternary(string name, BasicExpression x, BasicEx
return new OperatorExpression(name, new BasicExpression[] { x, y, z });
}

public override string ToString()
public override void Accept(IVisitor visit)
{
return this.name + "(" + string.Join<BasicExpression>(", ", this.operands) + ")";
visit.Operator(this.name, this.operands);
}
}
}

0 comments on commit 8ce466c

Please sign in to comment.