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);
}
static struct token_parse_result parse_var(
static struct token_parse_result parse_global(
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);
}
@@ -157,7 +157,7 @@ struct ast_node_type block_node_ops = {
.n_state_size = sizeof(struct block_parser_state),
.n_node_size = sizeof(struct ivy_ast_block_node),
.n_keyword_parsers = {
KW_PARSER(VAR, parse_var),
KW_PARSER(GLOBAL, parse_global),
KW_PARSER(END, parse_end),
KW_PARSER(ELSE, parse_else),
KW_PARSER(ELIF, parse_else), /* same behaviour as ELSE */