78 lines
2.8 KiB
C++
78 lines
2.8 KiB
C++
/*=============================================================================
|
|
Copyright (c) 2001-2014 Joel de Guzman
|
|
Copyright (c) 2013-2014 Agustin Berge
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
=============================================================================*/
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// A Calculator example demonstrating generation of AST. The AST,
|
|
// once created, is traversed, 1) To print its contents and
|
|
// 2) To evaluate the result.
|
|
//
|
|
// [ JDG April 28, 2008 ] For BoostCon 2008
|
|
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
|
|
// [ JDG January 9, 2013 ] Spirit X3
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "grammar.hpp"
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// Main program
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
int
|
|
main()
|
|
{
|
|
std::cout << "/////////////////////////////////////////////////////////\n\n";
|
|
std::cout << "Expression parser...\n\n";
|
|
std::cout << "/////////////////////////////////////////////////////////\n\n";
|
|
std::cout << "Type an expression...or [q or Q] to quit\n\n";
|
|
|
|
typedef std::string::const_iterator iterator_type;
|
|
typedef client::ast::program ast_program;
|
|
typedef client::ast::printer ast_print;
|
|
typedef client::ast::eval ast_eval;
|
|
|
|
std::string str;
|
|
while (std::getline(std::cin, str))
|
|
{
|
|
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
|
|
break;
|
|
|
|
auto& calc = client::calculator; // Our grammar
|
|
ast_program program; // Our program (AST)
|
|
ast_print print; // Prints the program
|
|
ast_eval eval; // Evaluates the program
|
|
|
|
iterator_type iter = str.begin();
|
|
iterator_type end = str.end();
|
|
boost::spirit::x3::ascii::space_type space;
|
|
bool r = phrase_parse(iter, end, calc, space, program);
|
|
|
|
if (r && iter == end)
|
|
{
|
|
std::cout << "-------------------------\n";
|
|
std::cout << "Parsing succeeded\n";
|
|
print(program);
|
|
std::cout << "\nResult: " << eval(program) << std::endl;
|
|
std::cout << "-------------------------\n";
|
|
}
|
|
else
|
|
{
|
|
std::string rest(iter, end);
|
|
std::cout << "-------------------------\n";
|
|
std::cout << "Parsing failed\n";
|
|
std::cout << "stopped at: \"" << rest << "\"\n";
|
|
std::cout << "-------------------------\n";
|
|
}
|
|
}
|
|
|
|
std::cout << "Bye... :-) \n\n";
|
|
return 0;
|
|
}
|