lang: ast: replace var keyword with global
local variables are now created automatically when they are first assigned to. the global keyword can be used to declare that a name refers to a global variable instead, at which point, assigning to the name results in the assignment referencing the global variable instead of allocating a new global variable.
This commit is contained in:
134
lang/ast/global.c
Normal file
134
lang/ast/global.c
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "ctx.h"
|
||||
#include "expr/expr.h"
|
||||
#include "iterate.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <ivy/lang/ast.h>
|
||||
|
||||
struct global_parser_state {
|
||||
struct parser_state s_base;
|
||||
unsigned int s_prev_token;
|
||||
};
|
||||
|
||||
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
|
||||
{
|
||||
struct global_parser_state *state = (struct global_parser_state *)sp;
|
||||
state->s_prev_token = IVY_KW_GLOBAL;
|
||||
}
|
||||
|
||||
struct token_parse_result parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct global_parser_state *state
|
||||
= parser_get_state(ctx, struct global_parser_state);
|
||||
if (state->s_prev_token != IVY_KW_GLOBAL) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
struct expr_parser_state *ident_parse
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
expr_add_terminator(ident_parse, IVY_SYM_EQUAL);
|
||||
expr_add_terminator(ident_parse, IVY_SYM_DOT);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
struct token_parse_result parse_left_paren(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct global_parser_state *state
|
||||
= parser_get_state(ctx, struct global_parser_state);
|
||||
|
||||
if (state->s_prev_token != IVY_KW_GLOBAL) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
struct expr_parser_state *tuple_ident_parse
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
expr_add_terminator(tuple_ident_parse, IVY_SYM_EQUAL);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
struct token_parse_result parse_equal(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct global_parser_state *state
|
||||
= parser_get_state(ctx, struct global_parser_state);
|
||||
struct ivy_ast_global_node *global
|
||||
= (struct ivy_ast_global_node *)state->s_base.s_node;
|
||||
if (!global->n_left) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_SYM_EQUAL;
|
||||
|
||||
struct expr_parser_state *val_parse
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
expr_add_terminator(val_parse, IVY_SYM_DOT);
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
struct token_parse_result parse_dot(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static enum ivy_status add_child(
|
||||
struct parser_state *parent, struct ivy_ast_node *child)
|
||||
{
|
||||
struct global_parser_state *state = (struct global_parser_state *)parent;
|
||||
struct ivy_ast_global_node *global
|
||||
= (struct ivy_ast_global_node *)parent->s_node;
|
||||
|
||||
switch (state->s_prev_token) {
|
||||
case IVY_KW_GLOBAL:
|
||||
if (global->n_left) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
global->n_left = child;
|
||||
break;
|
||||
case IVY_SYM_EQUAL:
|
||||
if (global->n_val) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
global->n_val = child;
|
||||
break;
|
||||
default:
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static void collect_children(
|
||||
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
||||
{
|
||||
struct ivy_ast_global_node *global = (struct ivy_ast_global_node *)node;
|
||||
|
||||
if (global->n_left) {
|
||||
ast_node_iterator_enqueue_node(iterator, node, global->n_left);
|
||||
}
|
||||
|
||||
if (global->n_val) {
|
||||
ast_node_iterator_enqueue_node(iterator, node, global->n_val);
|
||||
}
|
||||
}
|
||||
|
||||
struct ast_node_type global_node_ops = {
|
||||
.n_add_child = add_child,
|
||||
.n_collect_children = collect_children,
|
||||
.n_state_size = sizeof(struct global_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_global_node),
|
||||
.n_init_state = init_state,
|
||||
.n_token_parsers = {
|
||||
TOK_PARSER(IDENT, parse_ident),
|
||||
},
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER(LEFT_PAREN, parse_left_paren),
|
||||
SYM_PARSER(EQUAL, parse_equal),
|
||||
SYM_PARSER(DOT, parse_dot),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user