Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Visitor pattern for Walkers and Re-writers #110

Open
nathanwinder opened this issue Oct 15, 2018 · 0 comments
Open

Implement Visitor pattern for Walkers and Re-writers #110

nathanwinder opened this issue Oct 15, 2018 · 0 comments

Comments

@nathanwinder
Copy link

I still need to think through some of this design, particularly the VisitChildren and RewriteChildren but I wanted to get this on your radar.

        public interface IVisitor
	{
		void DefaultVisit(IAcceptVisitor obj);
	}

	public interface IVisitor<TResult>
	{
		TResult DefaultVisit(IAcceptVisitor obj);
	}

	public interface IAcceptVisitor
	{
		void Accept(IVisitor visitor);

		TResult Accept<TResult>(IVisitor<TResult> visitor);

		void VisitChildren(IVisitor visitor);

	}

	public interface IAcceptRewriter<T,TResult>
	{
		T RewriteChildren(IVisitor<TResult> visitor);
	}

	public interface IPersonVisitor<TResult> : IVisitor<TResult>
	{
		TResult VisitPerson(Person node);
	}

	public interface IPersonVisitor : IVisitor
	{
		void VisitPerson(Person node);
	}

	public partial class Person
	{
		private readonly Person mother;
		private readonly Person father;
	}

	public partial class Person : IAcceptVisitor, IAcceptRewriter<Person, object>
	{
		[DebuggerStepThrough]
		public void Accept(IVisitor visitor)
		{
			if (visitor is IPersonVisitor personVisitor)
			{
				personVisitor.VisitPerson(this);
			}
			else
			{
				visitor.DefaultVisit(this);
			}
		}

		[DebuggerStepThrough]
		public T Accept<T>(IVisitor<T> visitor)
		{
			if (visitor is IPersonVisitor<T> personVisitor)
			{
				return personVisitor.VisitPerson(this);
			}
			else
			{
				return visitor.DefaultVisit(this);
			}
		}

		[DebuggerStepThrough]
		public void VisitChildren(IVisitor visitor)
		{
			mother.Accept(visitor);
			father.Accept(visitor);
		}

		[DebuggerStepThrough]
		public Person RewriteChildren(IVisitor<object> visitor)
		{
			var mother = (Person)this.mother.Accept(visitor);
			var father = (Person)this.father.Accept(visitor);
			return With(mother, father);
		}
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant