7 ctype_(std::use_facet<std::ctype<char>>(input.getloc())),
14 if (c ==
'\a')
return R
"('\a')";
15 if (c ==
'\b')
return R
"('\b')";
16 if (c ==
'\f')
return R
"('\f')";
17 if (c ==
'\n')
return R
"('\n')";
18 if (c ==
'\r')
return R
"('\r')";
19 if (c ==
'\t')
return R
"('\t')";
20 if (c ==
'\v')
return R
"('\v')";
21 if (c ==
'\'')
return R
"('\'')";
22 if (c ==
'\\')
return R
"('\\')";
25 return std::string{
"\'"} + std::string(1,c) +
"\'";
27 std::ostringstream stream{};
28 stream <<
"'\\x" << std::hex;
31 stream << (std::char_traits<char>::to_int_type(c) & 0xFF) <<
'\'';
84 if (c ==
'+' or c ==
'-' or c ==
'*' or c ==
'/' or c ==
'%' or c ==
'(' or c ==
')' or c ==
'=') {
89 if (c < '0' or c >
'9') {
93 while (c >=
'0' and c <=
'9') {
101 throw parse_error{
"unterminated number: expected digit after the decimal point"};
102 if (c < '0' or c >
'9') {
104 throw parse_error{
"syntax error: expected digit after decimal point, got " +
charify(c)};
106 while (c >=
'0' and c <=
'9') {
112 if (c ==
'e' or c ==
'E') {
115 throw parse_error{
"unterminated number: expected digit in the exponent"};
116 if (c ==
'-' or c ==
'+') {
119 throw parse_error{
"unterminated number: expected digit after sign in the exponent"};
121 if (c < '0' or c >
'9') {
125 while (c >=
'0' and c <=
'9') {
137 std::istringstream stream{token};
139 return (stream >> result);
154 throw parse_error{
"syntax error: expected IDENTIFIER, but got " + name};
157 throw parse_error{
"syntax error: expected =, but got " + token};
159 throw parse_error{
"syntax error: expected additive-exprssion in assignment"};
169 throw parse_error{
"syntax error: expected an additive-expression"};
180 if (k !=
'+' and k !=
'-') {
186 throw parse_error{
"syntax error: unterminated expression. Expected a multiplicative-expression after " + token};
202 if (k !=
'*' and k !=
'/') {
208 throw parse_error{
"syntax error: unterminated expression. Expected a unary-expression after " + token};
211 else if (right == 0.0)
229 }
else if (k ==
'+') {
248 throw parse_error{
"syntax error: EOF when expecting ')'"};
250 throw parse_error{
"syntax error: expected ')', but got " + token};
255 throw parse_error{
"Invalid numeric literal: " + token};
261 throw parse_error{
"syntax error: expected a primary, but got " + token};
273 for (output <<
"> "; std::getline(input, line); output <<
"> ") {
274 std::istringstream input{std::move(line)};
281 output << ex.what() <<
'\n';
282 }
catch(std::exception
const& ex) {
283 output <<
"exception: " << ex.what() <<
'\n';
bool get_primary(double &result)
void parse_loop(std::istream &input, std::ostream &output)
bool isprint(char c) const
void set_variable(std::string name, double value)
parser(std::istream &input)
void get_identifier(std::string &identifier)
std::string charify(char c)
void push_back(std::string const &token, kind k)
bool get_mul_expr(double &result)
bool isalpha(char c) const
kind get_token(std::string &token)
std::string token_
One token push-back.
bool get_number(std::string const &token, double &result)
bool get_expr(double &result)
bool get_add_expr(double &result)
std::istream & input_
Share the input stream.
bool get_unary(double &result)
kind kind_
The kind of token that was pushed back.
double get_variable(std::string const &name)
bool isalnum(char c) const