lang: replace stack-based parser with a recursive parser
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#include "ctx.h"
|
||||
|
||||
#include <ivy/lang/ast.h>
|
||||
#include <ivy/lang/operator.h>
|
||||
|
||||
const struct ivy_operator *get_operator_from_token(struct ivy_token *tok)
|
||||
{
|
||||
switch (tok->t_type) {
|
||||
case IVY_TOK_SYMBOL:
|
||||
return ivy_operator_get_by_token(tok->t_symbol);
|
||||
case IVY_TOK_KEYWORD:
|
||||
return ivy_operator_get_by_token(tok->t_keyword);
|
||||
default:
|
||||
return ivy_operator_get_by_token(tok->t_type);
|
||||
}
|
||||
}
|
||||
|
||||
struct ivy_ast_node *create_operator_node_from_token(struct ivy_token *tok)
|
||||
{
|
||||
const struct ivy_operator *op = get_operator_from_token(tok);
|
||||
if (!op) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ivy_ast_op_node *op_node = create_node(IVY_AST_OP);
|
||||
op_node->n_op = op;
|
||||
ivy_ast_node_set_bounds_from_token(generic_node(op_node), tok);
|
||||
return generic_node(op_node);
|
||||
}
|
||||
|
||||
const struct ivy_operator *get_operator_from_node(struct ivy_ast_node *n)
|
||||
{
|
||||
switch (n->n_type) {
|
||||
case IVY_AST_OP: {
|
||||
struct ivy_ast_op_node *op = (struct ivy_ast_op_node *)n;
|
||||
return op->n_op;
|
||||
}
|
||||
case IVY_AST_MSG:
|
||||
return ivy_operator_get_by_id(IVY_OP_MSG);
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user