diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index 89ee70fbcbe96397a7ea327408a73f9c192dbf42..054fb849cb1dbef58703d08c2f0d62c64e6bf298 100644 --- a/libwccl/CMakeLists.txt +++ b/libwccl/CMakeLists.txt @@ -35,6 +35,7 @@ SET(libwccl_STAT_SRC ops/functions/bool/iterations/rightlook.cpp ops/functions/bool/predicate.cpp ops/functions/bool/predicates/and.cpp + ops/functions/bool/predicates/debug.cpp ops/functions/bool/predicates/isinside.cpp ops/functions/bool/predicates/isoutside.cpp ops/functions/bool/predicates/logicalpredicate.cpp diff --git a/libwccl/ops/functions/bool/predicates/debug.cpp b/libwccl/ops/functions/bool/predicates/debug.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f96c09dd63c8a921a2ebda499768bad40f9b4716 --- /dev/null +++ b/libwccl/ops/functions/bool/predicates/debug.cpp @@ -0,0 +1,23 @@ +#include <libwccl/ops/functions/bool/predicates/debug.h> +#include <libwccl/ops/formatters.h> + +namespace Wccl { + +DebugPrint::BaseRetValPtr DebugPrint::apply_internal(const FunExecContext& context) const +{ + const boost::shared_ptr<const Value>& v = expr_->apply_internal(context); + std::cerr << v->to_raw_string() << "\n"; + return Predicate::True(context); +} + +std::string DebugPrint::to_string(const Corpus2::Tagset& tagset) const +{ + return UnaryFunctionFormatter::to_string(tagset, *this, *expr_); +} + +std::ostream& DebugPrint::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" << *expr_ << ")"; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/debug.h b/libwccl/ops/functions/bool/predicates/debug.h new file mode 100644 index 0000000000000000000000000000000000000000..f42613729d5c534296e317c723abc15362e698b5 --- /dev/null +++ b/libwccl/ops/functions/bool/predicates/debug.h @@ -0,0 +1,58 @@ +#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_DEBUG_H +#define LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_DEBUG_H + +#include <libwccl/ops/functions/bool/predicate.h> +#include <libwccl/values/value.h> + +namespace Wccl { + +/** + * Diagnostic predicate that prints the value + */ +class DebugPrint : public Predicate +{ +public: + typedef boost::shared_ptr<FunctionBase> FunctionPtr; + + DebugPrint(const FunctionPtr& expr) + : expr_(expr) + { + BOOST_ASSERT(expr_); + } + + /** + * @returns String representation of the function in the form of: + * "inside(arg_expr_string)" + */ + std::string to_string(const Corpus2::Tagset& tagset) const; + + /** + * @returns Name of the function: "debug" + */ + std::string raw_name() const { + return "debug"; + } + +protected: + const FunctionPtr expr_; + + /** + * Outputs the string value of the returned value + * @returns True + */ + BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "debug(expr)" + * @note This version doesn't require a tagset, but may be incomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& os) const; +}; + + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_DEBUG_H diff --git a/libwccl/parser/grammar.g b/libwccl/parser/grammar.g index 3bf431b40164450c604aef919453827f59c32142..4892a65a5248f0df9b72372250ef9fde794682a2 100644 --- a/libwccl/parser/grammar.g +++ b/libwccl/parser/grammar.g @@ -26,6 +26,7 @@ header { #include <libwccl/ops/functions/conditional.h> #include <libwccl/ops/functions/bool/varsetter.h> + #include <libwccl/ops/functions/bool/predicates/debug.h> #include <libwccl/ops/functions/bool/predicates/or.h> #include <libwccl/ops/functions/bool/predicates/nor.h> #include <libwccl/ops/functions/bool/predicates/and.h> @@ -999,6 +1000,8 @@ bool_operator | ret = bool_agreement [tagset, vars] // | ret = bool_phrase [tagset, vars] + // debug operators + | ret = debug_print_operator [tagset, vars] // | LPAREN ret = bool_operator [tagset, vars] RPAREN ; @@ -1239,6 +1242,46 @@ inter_operator RPAREN ; +// ---------------------------------------------------------------------------- +// Debug printing: +debug_print_operator + [const Corpus2::Tagset& tagset, Variables& vars] + returns [boost::shared_ptr<Function<Bool> > ret] +{ + boost::shared_ptr<FunctionBase> v; +} + : "debug" LPAREN + ( + (position_operator [tagset, vars]) => + ( + v = position_operator [tagset, vars] { + ret.reset(new DebugPrint(v)); + } + ) + | + (symset_operator [tagset, vars]) => + ( + v = symset_operator [tagset, vars] { + ret.reset(new DebugPrint(v)); + } + ) + | + (strset_operator [tagset, vars]) => + ( + v = strset_operator [tagset, vars] { + ret.reset(new DebugPrint(v)); + } + ) + | + ( + v = bool_operator [tagset, vars] { + ret.reset(new DebugPrint(v)); + } + ) + ) + RPAREN +; + // ---------------------------------------------------------------------------- // Iterations: bool_iteration