Extended AST visitors with optional return type.

This commit is contained in:
Patrick Lühne 2017-06-04 21:59:19 +02:00
parent ab71e8eb0a
commit b4c8ce3dc4
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF

View File

@ -15,11 +15,11 @@ namespace ast
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: refactor // TODO: refactor
template<class T> template<class T, class ReturnType = void>
struct RecursiveFormulaVisitor struct RecursiveFormulaVisitor
{ {
template <class... Arguments> template <class... Arguments>
void visit(And &and_, Formula &formula, Arguments &&... arguments) ReturnType visit(And &and_, Formula &formula, Arguments &&... arguments)
{ {
for (auto &argument : and_.arguments) for (auto &argument : and_.arguments)
argument.accept(*this, argument, std::forward<Arguments>(arguments)...); argument.accept(*this, argument, std::forward<Arguments>(arguments)...);
@ -28,7 +28,7 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Biconditional &biconditional, Formula &formula, Arguments &&... arguments) ReturnType visit(Biconditional &biconditional, Formula &formula, Arguments &&... arguments)
{ {
biconditional.left.accept(*this, biconditional.left, std::forward<Arguments>(arguments)...); biconditional.left.accept(*this, biconditional.left, std::forward<Arguments>(arguments)...);
biconditional.right.accept(*this, biconditional.right, std::forward<Arguments>(arguments)...); biconditional.right.accept(*this, biconditional.right, std::forward<Arguments>(arguments)...);
@ -37,19 +37,19 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Boolean &boolean, Formula &formula, Arguments &&... arguments) ReturnType visit(Boolean &boolean, Formula &formula, Arguments &&... arguments)
{ {
return T::accept(boolean, formula, std::forward<Arguments>(arguments)...); return T::accept(boolean, formula, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Comparison &comparison, Formula &formula, Arguments &&... arguments) ReturnType visit(Comparison &comparison, Formula &formula, Arguments &&... arguments)
{ {
return T::accept(comparison, formula, std::forward<Arguments>(arguments)...); return T::accept(comparison, formula, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Exists &exists, Formula &formula, Arguments &&... arguments) ReturnType visit(Exists &exists, Formula &formula, Arguments &&... arguments)
{ {
exists.argument.accept(*this, exists.argument, std::forward<Arguments>(arguments)...); exists.argument.accept(*this, exists.argument, std::forward<Arguments>(arguments)...);
@ -57,7 +57,7 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(ForAll &forAll, Formula &formula, Arguments &&... arguments) ReturnType visit(ForAll &forAll, Formula &formula, Arguments &&... arguments)
{ {
forAll.argument.accept(*this, forAll.argument, std::forward<Arguments>(arguments)...); forAll.argument.accept(*this, forAll.argument, std::forward<Arguments>(arguments)...);
@ -65,7 +65,7 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Implies &implies, Formula &formula, Arguments &&... arguments) ReturnType visit(Implies &implies, Formula &formula, Arguments &&... arguments)
{ {
implies.antecedent.accept(*this, implies.antecedent, std::forward<Arguments>(arguments)...); implies.antecedent.accept(*this, implies.antecedent, std::forward<Arguments>(arguments)...);
implies.consequent.accept(*this, implies.consequent, std::forward<Arguments>(arguments)...); implies.consequent.accept(*this, implies.consequent, std::forward<Arguments>(arguments)...);
@ -74,13 +74,13 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(In &in, Formula &formula, Arguments &&... arguments) ReturnType visit(In &in, Formula &formula, Arguments &&... arguments)
{ {
return T::accept(in, formula, std::forward<Arguments>(arguments)...); return T::accept(in, formula, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Not &not_, Formula &formula, Arguments &&... arguments) ReturnType visit(Not &not_, Formula &formula, Arguments &&... arguments)
{ {
not_.argument.accept(*this, not_.argument, std::forward<Arguments>(arguments)...); not_.argument.accept(*this, not_.argument, std::forward<Arguments>(arguments)...);
@ -88,7 +88,7 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Or &or_, Formula &formula, Arguments &&... arguments) ReturnType visit(Or &or_, Formula &formula, Arguments &&... arguments)
{ {
for (auto &argument : or_.arguments) for (auto &argument : or_.arguments)
argument.accept(*this, argument, std::forward<Arguments>(arguments)...); argument.accept(*this, argument, std::forward<Arguments>(arguments)...);
@ -97,7 +97,7 @@ struct RecursiveFormulaVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Predicate &predicate, Formula &formula, Arguments &&... arguments) ReturnType visit(Predicate &predicate, Formula &formula, Arguments &&... arguments)
{ {
return T::accept(predicate, formula, std::forward<Arguments>(arguments)...); return T::accept(predicate, formula, std::forward<Arguments>(arguments)...);
} }
@ -105,12 +105,12 @@ struct RecursiveFormulaVisitor
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T> template<class T, class ReturnType = void>
struct RecursiveTermVisitor struct RecursiveTermVisitor
{ {
// TODO: return type is incorrect // TODO: return type is incorrect
template <class... Arguments> template <class... Arguments>
void visit(BinaryOperation &binaryOperation, Term &term, Arguments &&... arguments) ReturnType visit(BinaryOperation &binaryOperation, Term &term, Arguments &&... arguments)
{ {
binaryOperation.left.accept(*this, binaryOperation.left, std::forward<Arguments>(arguments)...); binaryOperation.left.accept(*this, binaryOperation.left, std::forward<Arguments>(arguments)...);
binaryOperation.right.accept(*this, binaryOperation.left, std::forward<Arguments>(arguments)...); binaryOperation.right.accept(*this, binaryOperation.left, std::forward<Arguments>(arguments)...);
@ -119,19 +119,19 @@ struct RecursiveTermVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Boolean &boolean, Term &term, Arguments &&... arguments) ReturnType visit(Boolean &boolean, Term &term, Arguments &&... arguments)
{ {
return T::accept(boolean, term, std::forward<Arguments>(arguments)...); return T::accept(boolean, term, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Constant &constant, Term &term, Arguments &&... arguments) ReturnType visit(Constant &constant, Term &term, Arguments &&... arguments)
{ {
return T::accept(constant, term, std::forward<Arguments>(arguments)...); return T::accept(constant, term, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Function &function, Term &term, Arguments &&... arguments) ReturnType visit(Function &function, Term &term, Arguments &&... arguments)
{ {
for (auto &argument : function.arguments) for (auto &argument : function.arguments)
argument.accept(*this, argument, std::forward<Arguments>(arguments)...); argument.accept(*this, argument, std::forward<Arguments>(arguments)...);
@ -140,13 +140,13 @@ struct RecursiveTermVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(Integer &integer, Term &term, Arguments &&... arguments) ReturnType visit(Integer &integer, Term &term, Arguments &&... arguments)
{ {
return T::accept(integer, term, std::forward<Arguments>(arguments)...); return T::accept(integer, term, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Interval &interval, Term &term, Arguments &&... arguments) ReturnType visit(Interval &interval, Term &term, Arguments &&... arguments)
{ {
interval.from.accept(*this, interval.from, std::forward<Arguments>(arguments)...); interval.from.accept(*this, interval.from, std::forward<Arguments>(arguments)...);
interval.to.accept(*this, interval.to, std::forward<Arguments>(arguments)...); interval.to.accept(*this, interval.to, std::forward<Arguments>(arguments)...);
@ -155,19 +155,19 @@ struct RecursiveTermVisitor
} }
template <class... Arguments> template <class... Arguments>
void visit(SpecialInteger &specialInteger, Term &term, Arguments &&... arguments) ReturnType visit(SpecialInteger &specialInteger, Term &term, Arguments &&... arguments)
{ {
return T::accept(specialInteger, term, std::forward<Arguments>(arguments)...); return T::accept(specialInteger, term, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(String &string, Term &term, Arguments &&... arguments) ReturnType visit(String &string, Term &term, Arguments &&... arguments)
{ {
return T::accept(string, term, std::forward<Arguments>(arguments)...); return T::accept(string, term, std::forward<Arguments>(arguments)...);
} }
template <class... Arguments> template <class... Arguments>
void visit(Variable &variable, Term &term, Arguments &&... arguments) ReturnType visit(Variable &variable, Term &term, Arguments &&... arguments)
{ {
return T::accept(variable, term, std::forward<Arguments>(arguments)...); return T::accept(variable, term, std::forward<Arguments>(arguments)...);
} }