#include <iostream>
#include <string>
#include <stdexcept>
#include <cctype>
#include <map>
#include <cstdlib>
#include <cmath>
#include <ctime>
struct parse_error : std::runtime_error
{
parse_error(std::string const& what)
: runtime_error(what)
{}
};
typedef double calculation_type;
std::map<std::string, calculation_type> constants;
std::map<std::string, calculation_type (*) ()> nullary_functions;
std::map<std::string, calculation_type (*) (calculation_type)> unary_functions;
std::map<std::string, calculation_type (*) (calculation_type, calculation_type)> binary_functions;
// unary-operator = '+' | '-' | '#'
// identifier = {A-Za-z_} {A-Za-z0-9_}*
// function-call = identifier [primary-expression] [primary-expression]
// group = '(' sum ')' | '[' sum ']' | '{' sum '}' | '|' sum '|'
// primary-expression = unary-operator primary-expression | double | function-call | group
// infix-function-call = primary-expression [':' identifier ':' primary-expression]*
// root = infix-function-call ['#' root]
// power = root ['^' power]
// product = power [('*' | '/' | '%') power]*
// sum = product [('+' | '-') product]*
calculation_type parse_sum(char const*& input);
calculation_type parse_primary_expression(char const*& input);
void skip_spaces(char const*& input)
{
while(std::isspace(*input))
++input;
}
double parse_double(char const*& input)
{
return std::strtod(input, const_cast<char**>(&input));
}
std::string parse_identifier(char const*& input)
{
skip_spaces(input);
std::string identifier(1, *input++);
while(std::isalnum(*input) || *input == '_')
identifier += *input++;
return identifier;
}
calculation_type parse_function_call(char const*& input)
{
std::string const name = parse_identifier(input);
{
auto const iter = constants.find(name);
if(iter != constants.end())
return iter->second;
}
{
auto const iter = nullary_functions.find(name);
if(iter != nullary_functions.end())
return iter->second();
}
{
auto const iter = unary_functions.find(name);
if(iter != unary_functions.end())
{
calculation_type const argument = parse_primary_expression(input);
return iter->second(argument);
}
}
{
auto iter = binary_functions.find(name);
if(iter != binary_functions.end())
{
calculation_type const arg1 = parse_primary_expression(input);
calculation_type const arg2 = parse_primary_expression(input);
return iter->second(arg1, arg2);
}
}
throw parse_error("call to unknown function '" + name + "'");
}
calculation_type parse_group(char const*& input)
{
char const delim = *input == '(' ? ')' : *input == '[' ? ']' : *input == '{' ? '}' : '|';
calculation_type const value = parse_sum(++input);
if(*input++ != delim)
throw parse_error("terminating '" + std::string(1, delim) + "' missing");
return value;
}
calculation_type parse_primary_expression(char const*& input)
{
skip_spaces(input);
if(*input == '+')
return parse_primary_expression(++input);
if(*input == '-')
return -parse_primary_expression(++input);
if(*input == '#')
return std::sqrt(parse_primary_expression(++input));
if(std::isdigit(*input) || *input == '.')
return parse_double(input);
if(std::isalpha(*input) || *input == '_')
return parse_function_call(input);
if(*input == '(' || *input == '[' || *input == '{')
return parse_group(input);
if(*input == '|')
{
calculation_type const value = parse_group(input);
return std::abs(value);
}
throw parse_error("expected value here: " + std::string(input));
}
calculation_type parse_infix_function_call(char const*& input)
{
calculation_type result = parse_primary_expression(input);
for(;;)
{
skip_spaces(input);
if(*input != ':')
break;
std::string const name = parse_identifier(++input);
skip_spaces(input);
if(*input != ':')
throw parse_error("missing terminating ':' at infix call to function '" + name + "'");
auto iter = binary_functions.find(name);
if(iter != binary_functions.end())
result = iter->second(result, parse_primary_expression(++input));
else
throw parse_error("infix call to unknown function '" + name + "'");
}
return result;
}
calculation_type parse_root(char const*& input)
{
calculation_type root = parse_infix_function_call(input);
skip_spaces(input);
if(*input == '#')
root = std::pow(parse_root(++input), 1 / root);
return root;
}
calculation_type parse_power(char const*& input)
{
calculation_type power = parse_root(input);
skip_spaces(input);
if(*input == '^')
power = std::pow(power, parse_power(++input));
return power;
}
calculation_type parse_product(char const*& input)
{
calculation_type product = parse_power(input);
for(;;)
{
skip_spaces(input);
if(*input == '*')
product *= parse_power(++input);
else if(*input == '/')
product /= parse_power(++input);
else if(*input == '%')
product = std::fmod(product, parse_power(++input));
else break;
}
return product;
}
calculation_type parse_sum(char const*& input)
{
calculation_type sum = parse_product(input);
for(;;)
{
skip_spaces(input);
if(*input == '+')
sum += parse_product(++input);
else if(*input == '-')
sum -= parse_product(++input);
else break;
}
return sum;
}
calculation_type eval(char const* input)
{
calculation_type result = parse_sum(input);
skip_spaces(input);
if(!*input)
return result;
throw parse_error("unexpected character here: " + std::string(input));
}
calculation_type squared(calculation_type v)
{
return v * v;
}
calculation_type randint()
{
return std::rand();
}
int main()
{
std::srand(static_cast<unsigned>(std::time(0)));
constants["pi"] = 3.14159265;
constants["e"] = 2.71828183;
nullary_functions["randint"] = randint;
unary_functions["sin"] = std::sin;
unary_functions["cos"] = std::cos;
unary_functions["tan"] = std::tan;
unary_functions["squared"] = squared;
binary_functions["pow"] = std::pow;
for(std::string line; std::getline(std::cin, line) && line != "";)
{
try
{
std::cout << eval(line.c_str()) << '\n';
}
catch(parse_error const& e)
{
std::cout << "parse error: " << e.what() << '\n';
}
}
}