Permalink
Browse files

ExpressionSample - implement visitor pattern for statements

  • Loading branch information...
bobbymcr committed Apr 20, 2018
1 parent fda9e66 commit 2e83ae0a9f05b74b8e5275f9d54743d6c56e3b02
@@ -17,6 +17,9 @@ public AssignmentStatement(BasicExpression left, BasicExpression right)
this.right = right;
}
public override string ToString() => "Assign(" + this.left + ", " + this.right + ")";
public override void Accept(IStatementVisitor visit)
{
visit.Assign(this.left, this.right);
}
}
}
@@ -4,12 +4,105 @@
namespace GWParse.Statements
{
using System.Text;
using GWParse.Expressions;
public abstract class BasicStatement
{
protected BasicStatement()
{
}
public static BasicStatement FromString(string input) => Stmt.FromString(input);
public abstract void Accept(IStatementVisitor visit);
public override string ToString()
{
StatementString str = new StatementString();
this.Accept(str);
return str.ToString();
}
private sealed class StatementString : IStatementVisitor
{
private readonly StringBuilder sb;
public StatementString()
{
this.sb = new StringBuilder();
}
public void Assign(BasicExpression left, BasicExpression right)
{
this.sb.Append("Assign(").Append(left).Append(", ").Append(right).Append(")");
}
public void For(BasicExpression v, BasicExpression start, BasicExpression end, BasicExpression step)
{
this.sb.Append("For(").Append(v).Append(", ").Append(start);
this.sb.Append(", ").Append(end).Append(", ").Append(step).Append(")");
}
public void Go(string name, int dest)
{
this.sb.Append(name).Append("(").Append(dest).Append(")");
}
public void IfThen(BasicExpression cond, BasicStatement ifTrue)
{
this.sb.Append("If(").Append(cond).Append(", ").Append(ifTrue).Append(")");
}
public void Input(string prompt, BasicExpression v)
{
this.sb.Append("Input(\"").Append(prompt).Append("\", ").Append(v).Append(")");
}
public void Many(string name, BasicExpression[] list)
{
this.sb.Append(name).Append("(");
foreach (BasicExpression expr in list)
{
this.sb.Append(expr.ToString());
this.sb.Append(", ");
}
if (list.Length > 0)
{
this.sb.Remove(this.sb.Length - 2, 2);
}
this.sb.Append(")");
}
public void Remark(string text)
{
this.sb.Append("Rem(\"");
foreach (char c in text)
{
switch (c)
{
case '"':
case '\\':
this.sb.Append('\\');
break;
}
this.sb.Append(c);
}
this.sb.Append("\")");
}
public void Void(string name)
{
this.sb.Append(name).Append("()");
}
public override string ToString() => this.sb.ToString();
}
}
}
@@ -21,9 +21,9 @@ public ForStatement(BasicExpression v, BasicExpression start, BasicExpression en
this.step = step;
}
public override string ToString()
public override void Accept(IStatementVisitor visit)
{
return "For(" + this.v + ", " + this.start + ", " + this.end + ", " + this.step + ")";
visit.For(this.v, this.start, this.end, this.step);
}
}
}
@@ -15,6 +15,9 @@ public GoStatement(string name, int dest)
this.dest = dest;
}
public override string ToString() => this.name + "(" + this.dest + ")";
public override void Accept(IStatementVisitor visit)
{
visit.Go(this.name, this.dest);
}
}
}
@@ -0,0 +1,27 @@
// <copyright file="IStatementVisitor.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>
namespace GWParse.Statements
{
using GWParse.Expressions;
public interface IStatementVisitor
{
void Assign(BasicExpression left, BasicExpression right);
void For(BasicExpression v, BasicExpression start, BasicExpression end, BasicExpression step);
void Go(string name, int dest);
void IfThen(BasicExpression cond, BasicStatement ifTrue);
void Input(string prompt, BasicExpression v);
void Many(string name, BasicExpression[] list);
void Remark(string text);
void Void(string name);
}
}
@@ -17,6 +17,9 @@ public IfThenStatement(BasicExpression cond, BasicStatement ifTrue)
this.ifTrue = ifTrue;
}
public override string ToString() => "If(" + this.cond + ", " + this.ifTrue + ")";
public override void Accept(IStatementVisitor visit)
{
visit.IfThen(this.cond, this.ifTrue);
}
}
}
@@ -17,6 +17,9 @@ public InputStatement(string prompt, BasicExpression v)
this.v = v;
}
public override string ToString() => "Input(\"" + this.prompt + "\", " + this.v + ")";
public override void Accept(IStatementVisitor visit)
{
visit.Input(this.prompt, this.v);
}
}
}
@@ -19,9 +19,9 @@ public ManyStatement(string name, IEnumerable<BasicExpression> list)
this.list = list.ToArray();
}
public override string ToString()
public override void Accept(IStatementVisitor visit)
{
return this.name + "(" + string.Join<BasicExpression>(", ", this.list) + ")";
visit.Many(this.name, this.list);
}
}
}
@@ -15,25 +15,9 @@ public RemarkStatement(string text)
this.text = text;
}
public override string ToString() => "Rem(\"" + this.Escape() + "\")";
private string Escape()
public override void Accept(IStatementVisitor visit)
{
StringBuilder sb = new StringBuilder();
foreach (char c in this.text)
{
switch (c)
{
case '"':
case '\\':
sb.Append('\\');
break;
}
sb.Append(c);
}
return sb.ToString();
visit.Remark(this.text);
}
}
}
@@ -13,6 +13,9 @@ public VoidStatement(string name)
this.name = name;
}
public override string ToString() => this.name + "()";
public override void Accept(IStatementVisitor visit)
{
visit.Void(this.name);
}
}
}

0 comments on commit 2e83ae0

Please sign in to comment.