#include <cstdlib>

#include <libwccl/values/strset.h>
#include <libwccl/parser/Parser.h>
#include <antlr/MismatchedTokenException.hpp>

// ----------------------------------------------------------------------------

/**
 * @desc It's simple command line tester for testing predicates
 */

int main()
{
	std::string str_in;
	Corpus2::Tagset tagset;
	Wccl::Parser parser(tagset);

	boost::shared_ptr<const Wccl::Bool> retVal;
	boost::shared_ptr<ANTLRParserResult<Wccl::Bool> > retOp;
	boost::shared_ptr<Corpus2::Sentence> sentence;
	Wccl::SentenceContext sc(sentence);

	if (system("clear")) {
		//
	}

	std::cerr << "Simple command line tester for testing bool operators"
						<< std::endl;

	while (1) {
		std::cerr << "Enter a bool operator expression: ";

		getline(std::cin, str_in);

		if (str_in == "clear" || str_in == "cls") {
			if (system("clear")) {
				//
			}
		}
		else if (str_in == "exit" || str_in == "quit") {
			break;
		}
		else {
			try {
				retOp = parser.parsePredicate(str_in);

				if (retOp.get()) {
					Wccl::FunExecContext cx(sc, retOp->variables);

					if ((retVal = retOp->op->apply(cx)).get()) {
						std::cerr << "Parsed expression: " << retVal->to_raw_string()
											<< std::endl;
					}
					else {
						std::cerr << "Problem while parsing -- "
											<< "haven't got StrSet object in boost::shared_ptr!"
											<< std::endl;
					}
				}
				else {
					std::cerr << "Problem while parsing -- "
										<< "haven't got Function<Wccl::StrSet> object in "
										<< "boost::shared_ptr!" << std::endl;
				}
			}
			catch (antlr::MismatchedTokenException &e) {
				std::cerr << e.getMessage() << std::endl;
			}
			catch (Wccl::InvalidVariableName &e) {
				std::cerr << "Wccl::InvalidVariableName" << std::endl;
			}
			catch (Wccl::VariableTypeMismatch &e) {
				std::cerr << "Wccl::VariableTypeMismatch" << std::endl;
			}
			catch (...) {
				std::cerr << "[N] Syntax error!" << std::endl;
			}
		}
	}

  return 0;
}