Skip to content

Commit

Permalink
BOOST_SPIRIT_NO_PREDEFINED_TERMINALS and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hmenke committed Oct 31, 2019
1 parent fc5b394 commit 013f28c
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 119 deletions.
3 changes: 0 additions & 3 deletions src/qi/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

#pragma once

#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>

#include <list>
#include <string>

namespace matheval {

namespace qi = boost::spirit::qi;

namespace ast {

struct nil {};
Expand Down
1 change: 1 addition & 0 deletions src/qi/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "ast.hpp"

#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
#include <boost/spirit/include/qi.hpp>

#include <iostream>
Expand Down
147 changes: 93 additions & 54 deletions src/qi/parser_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "parser.hpp"

#include <boost/spirit/include/phoenix.hpp>
#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
#include <boost/spirit/include/qi.hpp>

#include <cmath>
Expand All @@ -26,54 +27,92 @@ namespace parser {

template <typename Iterator>
grammar<Iterator>::grammar() : grammar::base_type(expression) {
constant.add("e", boost::math::constants::e<double>())(
"epsilon", std::numeric_limits<double>::epsilon())(
"phi", boost::math::constants::phi<double>())(
"pi", boost::math::constants::pi<double>());
ufunc.add("abs", static_cast<double (*)(double)>(&std::abs))(
"acos", static_cast<double (*)(double)>(&std::acos))(
"asin", static_cast<double (*)(double)>(&std::asin))(
"atan", static_cast<double (*)(double)>(&std::atan))(
"ceil", static_cast<double (*)(double)>(&std::ceil))(
"cos", static_cast<double (*)(double)>(&std::cos))(
"cosh", static_cast<double (*)(double)>(&std::cosh))(
"deg", static_cast<double (*)(double)>(&math::deg))(
"exp", static_cast<double (*)(double)>(&std::exp))(
"floor", static_cast<double (*)(double)>(&std::floor))(
"isinf", static_cast<double (*)(double)>(&math::isinf))(
"isnan", static_cast<double (*)(double)>(&math::isnan))(
"log", static_cast<double (*)(double)>(&std::log))(
"log10", static_cast<double (*)(double)>(&std::log10))(
"rad", static_cast<double (*)(double)>(&math::rad))(
"sgn", static_cast<double (*)(double)>(&math::sgn))(
"sin", static_cast<double (*)(double)>(&std::sin))(
"sinh", static_cast<double (*)(double)>(&std::sinh))(
"sqrt", static_cast<double (*)(double)>(&std::sqrt))(
"tan", static_cast<double (*)(double)>(&std::tan))(
"tanh", static_cast<double (*)(double)>(&std::tanh));
bfunc.add("atan2", static_cast<double (*)(double, double)>(&std::atan2))(
"pow", static_cast<double (*)(double, double)>(&std::pow));
unary_op.add("+", static_cast<double (*)(double)>(&math::plus))(
"-", static_cast<double (*)(double)>(&math::minus))(
"!", static_cast<double (*)(double)>(&math::unary_not));
additive_op.add("+", static_cast<double (*)(double, double)>(&math::plus))(
"-", static_cast<double (*)(double, double)>(&math::minus));
multiplicative_op.add("*", static_cast<double (*)(double, double)>(&math::multiplies))(
"/", static_cast<double (*)(double, double)>(&math::divides))(
"%", static_cast<double (*)(double, double)>(&std::fmod));
logical_op.add("&&",
static_cast<double (*)(double, double)>(&math::logical_and))(
"||", static_cast<double (*)(double, double)>(&math::logical_or));
relational_op.add("<", static_cast<double (*)(double, double)>(&math::less))(
"<=", static_cast<double (*)(double, double)>(&math::less_equals))(
">", static_cast<double (*)(double, double)>(&math::greater))(
">=", static_cast<double (*)(double, double)>(&math::greater_equals));
equality_op.add("==", static_cast<double (*)(double, double)>(&math::equals))(
"!=", static_cast<double (*)(double, double)>(&math::not_equals));
power.add("**", static_cast<double (*)(double, double)>(&std::pow));
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;

qi::alnum_type alnum;
qi::alpha_type alpha;
qi::double_type double_;
qi::lexeme_type lexeme;
qi::raw_type raw;

// clang-format off

constant.add
("e" , boost::math::constants::e<double>())
("epsilon", std::numeric_limits<double>::epsilon())
("phi" , boost::math::constants::phi<double>())
("pi" , boost::math::constants::pi<double>())
;

ufunc.add
("abs" , static_cast<double (*)(double)>(&std::abs))
("acos" , static_cast<double (*)(double)>(&std::acos))
("asin" , static_cast<double (*)(double)>(&std::asin))
("atan" , static_cast<double (*)(double)>(&std::atan))
("ceil" , static_cast<double (*)(double)>(&std::ceil))
("cos" , static_cast<double (*)(double)>(&std::cos))
("cosh" , static_cast<double (*)(double)>(&std::cosh))
("deg" , static_cast<double (*)(double)>(&math::deg))
("exp" , static_cast<double (*)(double)>(&std::exp))
("floor", static_cast<double (*)(double)>(&std::floor))
("isinf", static_cast<double (*)(double)>(&math::isinf))
("isnan", static_cast<double (*)(double)>(&math::isnan))
("log" , static_cast<double (*)(double)>(&std::log))
("log10", static_cast<double (*)(double)>(&std::log10))
("rad" , static_cast<double (*)(double)>(&math::rad))
("sgn" , static_cast<double (*)(double)>(&math::sgn))
("sin" , static_cast<double (*)(double)>(&std::sin))
("sinh" , static_cast<double (*)(double)>(&std::sinh))
("sqrt" , static_cast<double (*)(double)>(&std::sqrt))
("tan" , static_cast<double (*)(double)>(&std::tan))
("tanh" , static_cast<double (*)(double)>(&std::tanh))
;

bfunc.add
("atan2", static_cast<double (*)(double, double)>(&std::atan2))
("pow" , static_cast<double (*)(double, double)>(&std::pow))
;

unary_op.add
("+", static_cast<double (*)(double)>(&math::plus))
("-", static_cast<double (*)(double)>(&math::minus))
("!", static_cast<double (*)(double)>(&math::unary_not))
;

additive_op.add
("+", static_cast<double (*)(double, double)>(&math::plus))
("-", static_cast<double (*)(double, double)>(&math::minus))
;

multiplicative_op.add
("*", static_cast<double (*)(double, double)>(&math::multiplies))
("/", static_cast<double (*)(double, double)>(&math::divides))
("%", static_cast<double (*)(double, double)>(&std::fmod))
;

logical_op.add
("&&", static_cast<double (*)(double, double)>(&math::logical_and))
("||", static_cast<double (*)(double, double)>(&math::logical_or))
;

relational_op.add
("<" , static_cast<double (*)(double, double)>(&math::less))
("<=", static_cast<double (*)(double, double)>(&math::less_equals))
(">" , static_cast<double (*)(double, double)>(&math::greater))
(">=", static_cast<double (*)(double, double)>(&math::greater_equals))
;

equality_op.add
("==", static_cast<double (*)(double, double)>(&math::equals))
("!=", static_cast<double (*)(double, double)>(&math::not_equals))
;

power.add
("**", static_cast<double (*)(double, double)>(&std::pow))
;

expression =
logical.alias()
;
Expand All @@ -97,25 +136,25 @@ grammar<Iterator>::grammar() : grammar::base_type(expression) {
multiplicative =
factor >> *(multiplicative_op > factor)
;

factor =
primary >> *( power > factor )
;

unary =
ufunc > '(' > expression > ')'
;

binary =
bfunc > '(' > expression > ',' > expression > ')'
;

variable =
qi::raw[qi::lexeme[qi::alpha >> *(qi::alnum | '_')]]
raw[lexeme[alpha >> *(alnum | '_')]]
;

primary =
qi::double_
double_
| ('(' > expression > ')')
| (unary_op > primary)
| binary
Expand Down Expand Up @@ -143,8 +182,8 @@ grammar<Iterator>::grammar() : grammar::base_type(expression) {
// error_handler_function(error_handler<Iterator>())(
// "Error! Expecting ", qi::_4, qi::_3));
qi::on_error<qi::fail>(
expression, boost::phoenix::bind(boost::phoenix::ref(err_handler),
qi::_3, qi::_2, qi::_4));
expression,
boost::phoenix::bind(boost::phoenix::ref(err_handler), _3, _2, _4));
}

} // namespace parser
Expand Down
Loading

0 comments on commit 013f28c

Please sign in to comment.