2020-10-28 17:09:44 +01:00
|
|
|
#include "parse.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "ast/ast.h"
|
|
|
|
|
|
|
|
#define UNREACHABLE() __builtin_unreachable()
|
|
|
|
#define ARR_SIZE(Arr) (sizeof(Arr) / sizeof(*Arr))
|
|
|
|
|
2020-10-30 18:31:15 +01:00
|
|
|
static const struct {
|
|
|
|
const char *op;
|
2020-10-30 19:33:35 +01:00
|
|
|
const enum op_kind kind;
|
2020-10-30 18:31:15 +01:00
|
|
|
const int prio;
|
|
|
|
const enum { ASSOC_LEFT, ASSOC_RIGHT, ASSOC_NONE } assoc;
|
|
|
|
const enum { OP_INFIX, OP_PREFIX, OP_POSTFIX } fix;
|
|
|
|
} ops[] = {
|
2020-10-30 19:55:19 +01:00
|
|
|
# define BINOP(Op, Kind, Prio, Assoc, Fix) \
|
|
|
|
[Kind] = { #Op, Kind, Prio, Assoc, Fix },
|
|
|
|
# define PREOP(Op, Kind, Prio, Assoc, Fix) \
|
|
|
|
[Kind] = { #Op, Kind, Prio, Assoc, Fix },
|
|
|
|
# define POSTOP(Op, Kind, Prio, Assoc, Fix) \
|
|
|
|
[Kind] = { #Op, Kind, Prio, Assoc, Fix },
|
2020-10-30 18:31:15 +01:00
|
|
|
#include "operators.inc"
|
|
|
|
};
|
|
|
|
|
2020-10-28 17:09:44 +01:00
|
|
|
static struct ast_node *climbing_parse_internal(const char **input, int prec);
|
|
|
|
static struct ast_node *parse_operand(const char **input);
|
|
|
|
|
|
|
|
static void eat_char(const char **input)
|
|
|
|
{
|
|
|
|
*input += 1; // Skip this character
|
|
|
|
}
|
|
|
|
|
|
|
|
static void skip_whitespace(const char **input)
|
|
|
|
{
|
|
|
|
while (*input[0] && isspace(*input[0]))
|
|
|
|
eat_char(input);
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:20:24 +01:00
|
|
|
static enum op_kind char_to_binop(char c)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
2020-10-30 18:31:15 +01:00
|
|
|
if (ops[i].fix == OP_INFIX && c == ops[i].op[0])
|
2020-10-28 17:09:44 +01:00
|
|
|
return ops[i].kind;
|
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:44:49 +01:00
|
|
|
static enum op_kind char_to_prefix(char c)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
2020-10-30 19:44:49 +01:00
|
|
|
if (ops[i].fix == OP_PREFIX && c == ops[i].op[0])
|
|
|
|
return ops[i].kind;
|
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum op_kind char_to_postfix(char c)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
|
|
|
if (ops[i].fix == OP_POSTFIX && c == ops[i].op[0])
|
2020-10-28 17:09:44 +01:00
|
|
|
return ops[i].kind;
|
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_binop(char c)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
2020-10-30 18:31:15 +01:00
|
|
|
if (ops[i].fix == OP_INFIX && c == ops[i].op[0])
|
2020-10-28 17:09:44 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:56:36 +01:00
|
|
|
static bool is_prefix(char c)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
2020-10-30 18:56:36 +01:00
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
|
|
|
if (ops[i].fix == OP_PREFIX && c == ops[i].op[0])
|
|
|
|
return true;
|
2020-10-28 17:09:44 +01:00
|
|
|
|
2020-10-30 18:56:36 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_postfix(char c)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
|
|
|
if (ops[i].fix == OP_POSTFIX && c == ops[i].op[0])
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:33:35 +01:00
|
|
|
static bool prec_between(enum op_kind op, int min, int max)
|
2020-10-30 18:56:36 +01:00
|
|
|
{
|
2020-10-28 17:09:44 +01:00
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
2020-10-30 19:33:35 +01:00
|
|
|
if (op == ops[i].kind)
|
2020-10-28 17:09:44 +01:00
|
|
|
return min <= ops[i].prio && ops[i].prio <= max;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:33:35 +01:00
|
|
|
static int right_prec(enum op_kind op)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
2020-10-30 19:33:35 +01:00
|
|
|
if (op == ops[i].kind)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
if (ops[i].assoc == ASSOC_RIGHT)
|
|
|
|
return ops[i].prio;
|
|
|
|
return ops[i].prio + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT_MIN;
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:33:35 +01:00
|
|
|
static int next_prec(enum op_kind op)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < ARR_SIZE(ops); ++i)
|
2020-10-30 19:33:35 +01:00
|
|
|
if (op == ops[i].kind)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
if (ops[i].assoc != ASSOC_LEFT)
|
|
|
|
return ops[i].prio - 1;
|
|
|
|
return ops[i].prio;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple climbing parser, see `operators.inc` for more details.
|
|
|
|
*
|
|
|
|
* Whitespace is ignored in the input string, only serving to delimit numbers.
|
|
|
|
*
|
|
|
|
* The input shall consist of a single expression, having a trailing
|
|
|
|
* expression in the input results in an error.
|
|
|
|
*/
|
|
|
|
struct ast_node *climbing_parse(const char *input)
|
|
|
|
{
|
|
|
|
if (input == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
struct ast_node *ast = climbing_parse_internal(&input, 0);
|
|
|
|
|
|
|
|
if (ast == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Make sure there is no trailing character, except whitespace
|
|
|
|
skip_whitespace(&input);
|
|
|
|
if (input[0] != '\0')
|
|
|
|
{
|
|
|
|
destroy_ast(ast);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:33:35 +01:00
|
|
|
static bool update_op(enum op_kind *op, const char **input)
|
|
|
|
{
|
|
|
|
skip_whitespace(input);
|
|
|
|
|
|
|
|
char c = *input[0];
|
|
|
|
if (is_binop(c))
|
|
|
|
{
|
|
|
|
*op = char_to_binop(c);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (is_postfix(c))
|
|
|
|
{
|
2020-10-30 19:44:49 +01:00
|
|
|
*op = char_to_postfix(c);
|
2020-10-30 19:33:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:09:44 +01:00
|
|
|
static struct ast_node *climbing_parse_internal(const char **input, int prec)
|
|
|
|
{
|
|
|
|
prec = prec;
|
|
|
|
struct ast_node *ast = parse_operand(input);
|
|
|
|
|
|
|
|
int r = INT_MAX;
|
2020-10-30 19:33:35 +01:00
|
|
|
enum op_kind op; // Used in the next loop
|
|
|
|
while (update_op(&op, input) // Initialise the operator
|
|
|
|
&& prec_between(op, prec, r) // Use newly initialized operator
|
|
|
|
&& ast)
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
|
|
|
const char c = *input[0];
|
|
|
|
eat_char(input);
|
|
|
|
if (is_binop(c))
|
|
|
|
{
|
2020-10-30 19:33:35 +01:00
|
|
|
struct ast_node *rhs =
|
|
|
|
climbing_parse_internal(input, right_prec(op));
|
2020-10-28 17:09:44 +01:00
|
|
|
if (!rhs)
|
|
|
|
{
|
|
|
|
destroy_ast(ast);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct ast_node *tree = make_binop(op, ast, rhs);
|
|
|
|
|
|
|
|
if (!tree)
|
|
|
|
destroy_ast(ast); // Error case
|
|
|
|
ast = tree;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-30 19:33:35 +01:00
|
|
|
struct ast_node *tree = make_unop(op, ast);
|
2020-10-28 17:09:44 +01:00
|
|
|
if (!tree)
|
|
|
|
destroy_ast(ast); // Error case
|
|
|
|
ast = tree;
|
|
|
|
}
|
2020-10-30 19:33:35 +01:00
|
|
|
r = next_prec(op);
|
2020-10-28 17:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool my_atoi(const char **input, int *val)
|
|
|
|
{
|
|
|
|
if (!isdigit(*input[0]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*val = 0; // Initialize its value
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*val *= 10;
|
|
|
|
*val += *input[0] - '0';
|
|
|
|
*input += 1;
|
|
|
|
} while (isdigit(*input[0]));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_node *parse_operand(const char **input)
|
|
|
|
{
|
|
|
|
skip_whitespace(input); // Whitespace is not significant
|
|
|
|
struct ast_node *ast = NULL;
|
|
|
|
|
|
|
|
int val = 0;
|
2020-10-30 18:56:36 +01:00
|
|
|
if (is_prefix(*input[0]))
|
2020-10-28 17:09:44 +01:00
|
|
|
{
|
2020-10-30 19:44:49 +01:00
|
|
|
enum op_kind op = char_to_prefix(*input[0]);
|
|
|
|
// Remove the operator
|
2020-10-28 17:09:44 +01:00
|
|
|
eat_char(input);
|
|
|
|
|
2020-10-30 19:33:35 +01:00
|
|
|
ast = climbing_parse_internal(input, next_prec(op));
|
2020-10-28 17:09:44 +01:00
|
|
|
|
|
|
|
if (!ast)
|
|
|
|
return NULL;
|
|
|
|
struct ast_node *tree = make_unop(op, ast);
|
|
|
|
if (!tree)
|
|
|
|
destroy_ast(ast);
|
|
|
|
ast = tree;
|
|
|
|
}
|
|
|
|
else if (my_atoi(input, &val))
|
|
|
|
ast = make_num(val);
|
|
|
|
else if (*input[0] == '(')
|
|
|
|
{
|
|
|
|
// Remove the parenthesis
|
|
|
|
eat_char(input);
|
|
|
|
ast = climbing_parse_internal(input, 0);
|
|
|
|
// Check that we have our closing parenthesis
|
|
|
|
skip_whitespace(input);
|
|
|
|
if (*input[0] != ')')
|
|
|
|
{
|
|
|
|
destroy_ast(ast);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Remove the parenthesis
|
|
|
|
eat_char(input);
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|