lang: ast: implement simple identifier and operator expression parsing
This commit is contained in:
@@ -11,6 +11,7 @@ extern struct ast_node_type unit_import_node_ops;
|
||||
extern struct ast_node_type class_node_ops;
|
||||
extern struct ast_node_type msgh_node_ops;
|
||||
extern struct ast_node_type selector_node_ops;
|
||||
extern struct ast_node_type expr_node_ops;
|
||||
|
||||
static const struct ast_node_type *node_ops[] = {
|
||||
[IVY_AST_UNIT] = &unit_node_ops,
|
||||
@@ -19,15 +20,10 @@ static const struct ast_node_type *node_ops[] = {
|
||||
[IVY_AST_CLASS] = &class_node_ops,
|
||||
[IVY_AST_MSGH] = &msgh_node_ops,
|
||||
[IVY_AST_SELECTOR] = &selector_node_ops,
|
||||
[IVY_AST_EXPR] = &expr_node_ops,
|
||||
};
|
||||
static const size_t nr_node_ops = sizeof node_ops / sizeof node_ops[0];
|
||||
|
||||
enum tok_expr_type {
|
||||
TOK_EXPR_NONE = 0,
|
||||
TOK_EXPR_BEGIN,
|
||||
TOK_EXPR_ANY,
|
||||
};
|
||||
|
||||
const struct ast_node_type *get_ast_node_type(enum ivy_ast_node_type type)
|
||||
{
|
||||
if (type >= nr_node_ops) {
|
||||
@@ -37,7 +33,7 @@ const struct ast_node_type *get_ast_node_type(enum ivy_ast_node_type type)
|
||||
return node_ops[type];
|
||||
}
|
||||
|
||||
static enum tok_expr_type get_tok_expr_type(struct ivy_token *tok)
|
||||
enum tok_expr_type get_tok_expr_type(struct ivy_token *tok)
|
||||
{
|
||||
switch (tok->t_type) {
|
||||
case IVY_TOK_IDENT:
|
||||
@@ -112,14 +108,45 @@ token_parse_function get_token_parser(
|
||||
return NULL;
|
||||
}
|
||||
token_parse_function generic_parser = type->n_token_parsers[tok->t_type];
|
||||
|
||||
if (!generic_parser) {
|
||||
generic_parser = type->n_token_parsers[IVY_TOK_NONE];
|
||||
}
|
||||
|
||||
token_parse_function better_parser = NULL;
|
||||
|
||||
switch (tok->t_type) {
|
||||
case IVY_TOK_KEYWORD:
|
||||
better_parser = type->n_keyword_parsers[tok->t_keyword];
|
||||
if (type->n_keyword_parsers[IVY_KW_NONE]) {
|
||||
generic_parser = type->n_keyword_parsers[IVY_KW_NONE];
|
||||
}
|
||||
break;
|
||||
case IVY_TOK_SYMBOL:
|
||||
better_parser = type->n_symbol_parsers[tok->t_symbol];
|
||||
if (type->n_symbol_parsers[IVY_SYM_NONE]) {
|
||||
generic_parser = type->n_symbol_parsers[IVY_SYM_NONE];
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (better_parser) {
|
||||
return better_parser;
|
||||
}
|
||||
|
||||
enum token_expr_type expr_type = get_tok_expr_type(tok);
|
||||
switch (expr_type) {
|
||||
case TOK_EXPR_BEGIN:
|
||||
better_parser = type->n_expr_parser.expr_begin
|
||||
? type->n_expr_parser.expr_begin
|
||||
: type->n_expr_parser.expr_all;
|
||||
break;
|
||||
case TOK_EXPR_ANY:
|
||||
better_parser = type->n_expr_parser.expr_other
|
||||
? type->n_expr_parser.expr_other
|
||||
: type->n_expr_parser.expr_all;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -223,30 +250,6 @@ const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v)
|
||||
}
|
||||
}
|
||||
|
||||
const char *ivy_ast_op_to_string(enum ivy_ast_op v)
|
||||
{
|
||||
switch (v) {
|
||||
ENUM_STR(IVY_OP_NONE);
|
||||
ENUM_STR(IVY_OP_ASSIGN);
|
||||
ENUM_STR(IVY_OP_ADD);
|
||||
ENUM_STR(IVY_OP_SUBTRACT);
|
||||
ENUM_STR(IVY_OP_MULTIPLY);
|
||||
ENUM_STR(IVY_OP_DIVIDE);
|
||||
ENUM_STR(IVY_OP_LESS_THAN);
|
||||
ENUM_STR(IVY_OP_GREATER_THAN);
|
||||
ENUM_STR(IVY_OP_EQUAL);
|
||||
ENUM_STR(IVY_OP_NOT_EQUAL);
|
||||
ENUM_STR(IVY_OP_LESS_EQUAL);
|
||||
ENUM_STR(IVY_OP_GREATER_EQUAL);
|
||||
ENUM_STR(IVY_OP_AND);
|
||||
ENUM_STR(IVY_OP_OR);
|
||||
ENUM_STR(IVY_OP_IS);
|
||||
ENUM_STR(IVY_OP_NOT);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
const char *ivy_ast_msgh_recipient_type_to_string(enum ivy_ast_msgh_recipient_type v)
|
||||
{
|
||||
switch (v) {
|
||||
|
||||
Reference in New Issue
Block a user