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:
2025-09-08 15:50:50 +01:00
parent 5ea544692b
commit 84e52b1649
5 changed files with 39 additions and 41 deletions

View File

@@ -101,10 +101,10 @@ static struct token_parse_result parse_symbol(
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
static struct token_parse_result parse_var( static struct token_parse_result parse_global(
struct ivy_parser *ctx, struct ivy_token *tok) struct ivy_parser *ctx, struct ivy_token *tok)
{ {
parser_push_state(ctx, IVY_AST_VAR, 0); parser_push_state(ctx, IVY_AST_GLOBAL, 0);
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }
@@ -157,7 +157,7 @@ struct ast_node_type block_node_ops = {
.n_state_size = sizeof(struct block_parser_state), .n_state_size = sizeof(struct block_parser_state),
.n_node_size = sizeof(struct ivy_ast_block_node), .n_node_size = sizeof(struct ivy_ast_block_node),
.n_keyword_parsers = { .n_keyword_parsers = {
KW_PARSER(VAR, parse_var), KW_PARSER(GLOBAL, parse_global),
KW_PARSER(END, parse_end), KW_PARSER(END, parse_end),
KW_PARSER(ELSE, parse_else), KW_PARSER(ELSE, parse_else),
KW_PARSER(ELIF, parse_else), /* same behaviour as ELSE */ KW_PARSER(ELIF, parse_else), /* same behaviour as ELSE */

View File

@@ -5,22 +5,22 @@
#include <ivy/lang/ast.h> #include <ivy/lang/ast.h>
struct var_parser_state { struct global_parser_state {
struct parser_state s_base; struct parser_state s_base;
unsigned int s_prev_token; unsigned int s_prev_token;
}; };
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg) static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
{ {
struct var_parser_state *state = (struct var_parser_state *)sp; struct global_parser_state *state = (struct global_parser_state *)sp;
state->s_prev_token = IVY_KW_VAR; state->s_prev_token = IVY_KW_GLOBAL;
} }
struct token_parse_result parse_ident(struct ivy_parser *ctx, struct ivy_token *tok) struct token_parse_result parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct var_parser_state *state struct global_parser_state *state
= parser_get_state(ctx, struct var_parser_state); = parser_get_state(ctx, struct global_parser_state);
if (state->s_prev_token != IVY_KW_VAR) { if (state->s_prev_token != IVY_KW_GLOBAL) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
@@ -35,10 +35,10 @@ struct token_parse_result parse_ident(struct ivy_parser *ctx, struct ivy_token *
struct token_parse_result parse_left_paren( struct token_parse_result parse_left_paren(
struct ivy_parser *ctx, struct ivy_token *tok) struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct var_parser_state *state struct global_parser_state *state
= parser_get_state(ctx, struct var_parser_state); = parser_get_state(ctx, struct global_parser_state);
if (state->s_prev_token != IVY_KW_VAR) { if (state->s_prev_token != IVY_KW_GLOBAL) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
@@ -51,11 +51,11 @@ struct token_parse_result parse_left_paren(
struct token_parse_result parse_equal(struct ivy_parser *ctx, struct ivy_token *tok) struct token_parse_result parse_equal(struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct var_parser_state *state struct global_parser_state *state
= parser_get_state(ctx, struct var_parser_state); = parser_get_state(ctx, struct global_parser_state);
struct ivy_ast_var_node *var struct ivy_ast_global_node *global
= (struct ivy_ast_var_node *)state->s_base.s_node; = (struct ivy_ast_global_node *)state->s_base.s_node;
if (!var->n_left) { if (!global->n_left) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
} }
@@ -77,23 +77,24 @@ struct token_parse_result parse_dot(struct ivy_parser *ctx, struct ivy_token *to
static enum ivy_status add_child( static enum ivy_status add_child(
struct parser_state *parent, struct ivy_ast_node *child) struct parser_state *parent, struct ivy_ast_node *child)
{ {
struct var_parser_state *state = (struct var_parser_state *)parent; struct global_parser_state *state = (struct global_parser_state *)parent;
struct ivy_ast_var_node *var = (struct ivy_ast_var_node *)parent->s_node; struct ivy_ast_global_node *global
= (struct ivy_ast_global_node *)parent->s_node;
switch (state->s_prev_token) { switch (state->s_prev_token) {
case IVY_KW_VAR: case IVY_KW_GLOBAL:
if (var->n_left) { if (global->n_left) {
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
} }
var->n_left = child; global->n_left = child;
break; break;
case IVY_SYM_EQUAL: case IVY_SYM_EQUAL:
if (var->n_val) { if (global->n_val) {
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
} }
var->n_val = child; global->n_val = child;
break; break;
default: default:
return IVY_ERR_BAD_SYNTAX; return IVY_ERR_BAD_SYNTAX;
@@ -105,22 +106,22 @@ static enum ivy_status add_child(
static void collect_children( static void collect_children(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
{ {
struct ivy_ast_var_node *var = (struct ivy_ast_var_node *)node; struct ivy_ast_global_node *global = (struct ivy_ast_global_node *)node;
if (var->n_left) { if (global->n_left) {
ast_node_iterator_enqueue_node(iterator, node, var->n_left); ast_node_iterator_enqueue_node(iterator, node, global->n_left);
} }
if (var->n_val) { if (global->n_val) {
ast_node_iterator_enqueue_node(iterator, node, var->n_val); ast_node_iterator_enqueue_node(iterator, node, global->n_val);
} }
} }
struct ast_node_type var_node_ops = { struct ast_node_type global_node_ops = {
.n_add_child = add_child, .n_add_child = add_child,
.n_collect_children = collect_children, .n_collect_children = collect_children,
.n_state_size = sizeof(struct var_parser_state), .n_state_size = sizeof(struct global_parser_state),
.n_node_size = sizeof(struct ivy_ast_var_node), .n_node_size = sizeof(struct ivy_ast_global_node),
.n_init_state = init_state, .n_init_state = init_state,
.n_token_parsers = { .n_token_parsers = {
TOK_PARSER(IDENT, parse_ident), TOK_PARSER(IDENT, parse_ident),

View File

@@ -173,9 +173,6 @@ struct ast_node_type lambda_node_ops = {
SYM_PARSER(PIPE, parse_pipe), SYM_PARSER(PIPE, parse_pipe),
SYM_PARSER(COLON, parse_colon), SYM_PARSER(COLON, parse_colon),
}, },
.n_keyword_parsers = {
KW_PARSER(VAR, parse_expr_begin),
},
.n_expr_parser = { .n_expr_parser = {
.expr_begin = parse_expr_begin, .expr_begin = parse_expr_begin,
}, },

View File

@@ -16,7 +16,7 @@ extern struct ast_node_type expr_node_ops;
extern struct ast_node_type block_node_ops; extern struct ast_node_type block_node_ops;
extern struct ast_node_type msg_node_ops; extern struct ast_node_type msg_node_ops;
extern struct ast_node_type op_node_ops; extern struct ast_node_type op_node_ops;
extern struct ast_node_type var_node_ops; extern struct ast_node_type global_node_ops;
extern struct ast_node_type ident_node_ops; extern struct ast_node_type ident_node_ops;
extern struct ast_node_type int_node_ops; extern struct ast_node_type int_node_ops;
extern struct ast_node_type double_node_ops; extern struct ast_node_type double_node_ops;
@@ -52,7 +52,7 @@ static const struct ast_node_type *node_ops[] = {
[IVY_AST_BLOCK] = &block_node_ops, [IVY_AST_BLOCK] = &block_node_ops,
[IVY_AST_MSG] = &msg_node_ops, [IVY_AST_MSG] = &msg_node_ops,
[IVY_AST_OP] = &op_node_ops, [IVY_AST_OP] = &op_node_ops,
[IVY_AST_VAR] = &var_node_ops, [IVY_AST_GLOBAL] = &global_node_ops,
[IVY_AST_IDENT] = &ident_node_ops, [IVY_AST_IDENT] = &ident_node_ops,
[IVY_AST_INT] = &int_node_ops, [IVY_AST_INT] = &int_node_ops,
[IVY_AST_DOUBLE] = &double_node_ops, [IVY_AST_DOUBLE] = &double_node_ops,
@@ -349,7 +349,7 @@ const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v)
ENUM_STR(IVY_AST_UNIT_IMPORT); ENUM_STR(IVY_AST_UNIT_IMPORT);
ENUM_STR(IVY_AST_DISCARD); ENUM_STR(IVY_AST_DISCARD);
ENUM_STR(IVY_AST_INT); ENUM_STR(IVY_AST_INT);
ENUM_STR(IVY_AST_VAR); ENUM_STR(IVY_AST_GLOBAL);
ENUM_STR(IVY_AST_DOUBLE); ENUM_STR(IVY_AST_DOUBLE);
ENUM_STR(IVY_AST_STRING); ENUM_STR(IVY_AST_STRING);
ENUM_STR(IVY_AST_FSTRING); ENUM_STR(IVY_AST_FSTRING);

View File

@@ -19,10 +19,10 @@ static struct token_parse_result parse_use_keyword(
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }
static struct token_parse_result parse_var_keyword( static struct token_parse_result parse_global_keyword(
struct ivy_parser *ctx, struct ivy_token *tok) struct ivy_parser *ctx, struct ivy_token *tok)
{ {
parser_push_state(ctx, IVY_AST_VAR, 0); parser_push_state(ctx, IVY_AST_GLOBAL, 0);
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }
@@ -75,7 +75,7 @@ struct ast_node_type unit_node_ops = {
KW_PARSER(PACKAGE, parse_package_keyword), KW_PARSER(PACKAGE, parse_package_keyword),
KW_PARSER(CLASS, parse_class_keyword), KW_PARSER(CLASS, parse_class_keyword),
KW_PARSER(USE, parse_use_keyword), KW_PARSER(USE, parse_use_keyword),
KW_PARSER(VAR, parse_var_keyword), KW_PARSER(GLOBAL, parse_global_keyword),
}, },
.n_symbol_parsers = { .n_symbol_parsers = {
SYM_PARSER(DOT, parse_dot), SYM_PARSER(DOT, parse_dot),