Permalink
Browse files

ExpressionSample - use Visitor pattern for BasicExpression.ToString

  • Loading branch information...
bobbymcr committed Apr 15, 2018
1 parent 2f84030 commit 8ce466c2ff2b14081ec92b6c0ef187e2f177c696
@@ -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" />
@@ -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);
}
}
}
@@ -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();
}
}
}
}
@@ -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);
}
}
}
@@ -4,7 +4,7 @@
namespace GWExpr
{
internal enum BasicType
public enum BasicType
{
None = 0,
Num,
@@ -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);
}
}
}
@@ -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);
}
}
@@ -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.