From 67bfdef18ab43cef9095a594415c17a888958a2a Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 6 Dec 2024 21:33:53 +0000 Subject: [PATCH] lang: ast: misc formatting and reference fixes --- lang/ast/double.c | 5 +-- lang/ast/node.c | 5 +-- lang/ast/property.c | 77 ++++++++++++++++++++++++++++++--------------- lang/ast/string.c | 19 +++++++---- 4 files changed, 70 insertions(+), 36 deletions(-) diff --git a/lang/ast/double.c b/lang/ast/double.c index 6d88dc4..80a7244 100644 --- a/lang/ast/double.c +++ b/lang/ast/double.c @@ -7,8 +7,9 @@ static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_double_node *v = (struct ivy_ast_double_node *)node; - b_string_append_cstr(str, "%s (%.2lf)", ivy_ast_node_type_to_string(node->n_type), - v->n_value->t_double); + b_string_append_cstrf( + str, "%s (%.2lf)", ivy_ast_node_type_to_string(node->n_type), + v->n_value->t_double); } struct ast_node_type double_node_ops = { diff --git a/lang/ast/node.c b/lang/ast/node.c index 6b5c346..4cdeb89 100644 --- a/lang/ast/node.c +++ b/lang/ast/node.c @@ -1,5 +1,6 @@ #include "node.h" +#include #include #include #include @@ -192,7 +193,6 @@ struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type) return node; } - void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out) { const struct ast_node_type *type_info = get_ast_node_type(node->n_type); @@ -203,7 +203,8 @@ void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out) if (type_info->n_to_string) { type_info->n_to_string(node, out); } else { - b_string_append_cstr(out, ivy_ast_node_type_to_string(node->n_type)); + b_string_append_cstr( + out, ivy_ast_node_type_to_string(node->n_type)); } } diff --git a/lang/ast/property.c b/lang/ast/property.c index 1b3afb3..5c74312 100644 --- a/lang/ast/property.c +++ b/lang/ast/property.c @@ -25,22 +25,23 @@ enum property_flags { struct property_parser_state { struct parser_state s_base; - enum property_type s_flags; + enum property_flags s_flags; struct ivy_ast_node *s_prev_node; struct ivy_token *s_ident; struct ivy_ast_node *s_get, *s_set; - /* one of either 0, PROPERTY_GET, or PROPERTY_SET, depending on which part of the - * property is currently being parsed. */ + /* one of either 0, PROPERTY_GET, or PROPERTY_SET, depending on which + * part of the property is currently being parsed. */ unsigned int s_cur_component; unsigned int s_prev; }; static void finalise_property(struct property_parser_state *state) { - struct ivy_ast_property_node *prop = (struct ivy_ast_property_node *)state->s_base.s_node; + struct ivy_ast_property_node *prop + = (struct ivy_ast_property_node *)state->s_base.s_node; if (state->s_flags & PROPERTY_GET) { prop->n_flags |= IVY_AST_PROPERTY_GET; @@ -53,7 +54,7 @@ static void finalise_property(struct property_parser_state *state) prop->n_ident = state->s_ident; prop->n_get = state->s_get; prop->n_set = state->s_set; - + state->s_ident = NULL; state->s_get = NULL; state->s_set = NULL; @@ -62,7 +63,8 @@ static void finalise_property(struct property_parser_state *state) static struct token_parse_result parse_ident( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (state->s_prev != IVY_SYM_DOLLAR) { /* the only ident we're looking for is the property name, which @@ -78,7 +80,8 @@ static struct token_parse_result parse_ident( static struct token_parse_result parse_pipe( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (state->s_prev != IVY_TOK_IDENT) { /* the only pipe we're looking for is the property @@ -103,7 +106,8 @@ static struct token_parse_result parse_pipe( static struct token_parse_result parse_left_paren( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (state->s_prev != IVY_TOK_IDENT) { /* the only left paren we're looking for comes after the @@ -127,8 +131,9 @@ static struct token_parse_result parse_left_paren( static struct token_parse_result parse_right_paren( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); - + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); + if (PROPERTY_TYPE(state->s_flags) != PROPERTY_AUTO) { /* can only use parenthesis in auto-properties. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); @@ -152,7 +157,8 @@ static struct token_parse_result parse_right_paren( static struct token_parse_result parse_equal_right_arrow( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (PROPERTY_TYPE(state->s_flags) != PROPERTY_FULL) { /* this symbol cannot be used in auto-properties. */ @@ -166,7 +172,9 @@ static struct token_parse_result parse_equal_right_arrow( state->s_prev = IVY_SYM_EQUAL_RIGHT_ANGLE; - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr->s_terminator = IVY_SYM_COMMA; expr->s_subexpr_depth = 1; @@ -176,14 +184,17 @@ static struct token_parse_result parse_equal_right_arrow( static struct token_parse_result parse_comma( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); - if (PROPERTY_TYPE(state->s_flags) == PROPERTY_FULL && state->s_prev != IVY_SYM_EQUAL_RIGHT_ANGLE) { + if (PROPERTY_TYPE(state->s_flags) == PROPERTY_FULL + && state->s_prev != IVY_SYM_EQUAL_RIGHT_ANGLE) { /* this symbol can only be used after get or set */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (state->s_cur_component != PROPERTY_GET && state->s_cur_component != PROPERTY_SET) { + if (state->s_cur_component != PROPERTY_GET + && state->s_cur_component != PROPERTY_SET) { return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } @@ -220,14 +231,16 @@ static struct token_parse_result parse_comma( static struct token_parse_result parse_dot( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (PROPERTY_TYPE(state->s_flags) != PROPERTY_FULL) { /* this symbol cannot be used in auto-properties. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (state->s_cur_component != PROPERTY_GET && state->s_cur_component != PROPERTY_SET) { + if (state->s_cur_component != PROPERTY_GET + && state->s_cur_component != PROPERTY_SET) { /* not actually parsing a getter or setter */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } @@ -264,19 +277,23 @@ static struct token_parse_result parse_dot( static struct token_parse_result parse_get( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (state->s_flags & PROPERTY_GET) { /* property already has getter defined */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (PROPERTY_TYPE(state->s_flags) == PROPERTY_FULL && state->s_prev != IVY_SYM_PIPE && state->s_prev != IVY_SYM_COMMA) { + if (PROPERTY_TYPE(state->s_flags) == PROPERTY_FULL + && state->s_prev != IVY_SYM_PIPE && state->s_prev != IVY_SYM_COMMA) { /* getter is not expected here. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (PROPERTY_TYPE(state->s_flags) == PROPERTY_AUTO && state->s_prev != IVY_SYM_LEFT_PAREN && state->s_prev != IVY_SYM_COMMA) { + if (PROPERTY_TYPE(state->s_flags) == PROPERTY_AUTO + && state->s_prev != IVY_SYM_LEFT_PAREN + && state->s_prev != IVY_SYM_COMMA) { /* getter is not expected here. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } @@ -291,19 +308,23 @@ static struct token_parse_result parse_get( static struct token_parse_result parse_set( struct ivy_parser *ctx, struct ivy_token *tok) { - struct property_parser_state *state = parser_get_state(ctx, struct property_parser_state); + struct property_parser_state *state + = parser_get_state(ctx, struct property_parser_state); if (state->s_flags & PROPERTY_SET) { /* property already has setter defined */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (PROPERTY_TYPE(state->s_flags) == PROPERTY_FULL && state->s_prev != IVY_SYM_PIPE && state->s_prev != IVY_SYM_COMMA) { + if (PROPERTY_TYPE(state->s_flags) == PROPERTY_FULL + && state->s_prev != IVY_SYM_PIPE && state->s_prev != IVY_SYM_COMMA) { /* setter is not expected here. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } - if (PROPERTY_TYPE(state->s_flags) == PROPERTY_AUTO && state->s_prev != IVY_SYM_LEFT_PAREN && state->s_prev != IVY_SYM_COMMA) { + if (PROPERTY_TYPE(state->s_flags) == PROPERTY_AUTO + && state->s_prev != IVY_SYM_LEFT_PAREN + && state->s_prev != IVY_SYM_COMMA) { /* setter is not expected here. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); } @@ -318,7 +339,8 @@ static struct token_parse_result parse_set( static enum ivy_status add_child( struct parser_state *parent, struct ivy_ast_node *child) { - struct property_parser_state *state = (struct property_parser_state *)parent; + struct property_parser_state *state + = (struct property_parser_state *)parent; if (state->s_prev_node) { return IVY_ERR_BAD_SYNTAX; @@ -353,13 +375,16 @@ static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_property_node *prop = (struct ivy_ast_property_node *)node; - b_string_append_cstrf(str, "%s (%s) [", ivy_ast_node_type_to_string(node->n_type), prop->n_ident->t_str); + b_string_append_cstrf( + str, "%s (%s) [", ivy_ast_node_type_to_string(node->n_type), + prop->n_ident->t_str); if (prop->n_flags & IVY_AST_PROPERTY_GET) { b_string_append_cstr(str, "get"); } - if ((prop->n_flags & IVY_AST_PROPERTY_GET) && prop->n_flags & IVY_AST_PROPERTY_SET) { + if ((prop->n_flags & IVY_AST_PROPERTY_GET) + && prop->n_flags & IVY_AST_PROPERTY_SET) { b_string_append_cstr(str, ", "); } diff --git a/lang/ast/string.c b/lang/ast/string.c index e1910f4..6a6fea6 100644 --- a/lang/ast/string.c +++ b/lang/ast/string.c @@ -1,20 +1,25 @@ #include "ctx.h" #include "expr/expr.h" +#include "iterate.h" #include "node.h" + #include static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_string_node *s = (struct ivy_ast_string_node *)node; - b_string_append_cstrf(str, "%s (\"%s\")", ivy_ast_node_type_to_string(node->n_type), s->n_value->t_str); + b_string_append_cstrf( + str, "%s (\"%s\")", ivy_ast_node_type_to_string(node->n_type), + s->n_value->t_str); } -struct token_parse_result parse_string( - struct ivy_parser *ctx, struct ivy_token *tok) +struct token_parse_result parse_string(struct ivy_parser *ctx, struct ivy_token *tok) { struct parser_state *state = parser_get_state_generic(ctx); - struct ivy_ast_fstring_node *parent = (struct ivy_ast_fstring_node *)state->s_node; - struct ivy_ast_string_node *child = (struct ivy_ast_string_node *)ast_node_create(IVY_AST_STRING); + struct ivy_ast_fstring_node *parent + = (struct ivy_ast_fstring_node *)state->s_node; + struct ivy_ast_string_node *child + = (struct ivy_ast_string_node *)ast_node_create(IVY_AST_STRING); if (!child) { return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0); @@ -28,7 +33,9 @@ struct token_parse_result parse_string( struct token_parse_result parse_left_brace( struct ivy_parser *ctx, struct ivy_token *tok) { - struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state(ctx, IVY_AST_EXPR, 0); + struct expr_parser_state *expr + = (struct expr_parser_state *)parser_push_state( + ctx, IVY_AST_EXPR, 0); expr->s_terminator = IVY_SYM_RIGHT_BRACE; return PARSE_RESULT(IVY_OK, 0); }