lang: ast: misc formatting and reference fixes

This commit is contained in:
2024-12-06 21:33:53 +00:00
parent bd377b00f4
commit 67bfdef18a
4 changed files with 70 additions and 36 deletions

View File

@@ -7,7 +7,8 @@ static void to_string(struct ivy_ast_node *node, b_string *str)
{ {
struct ivy_ast_double_node *v = (struct ivy_ast_double_node *)node; 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), b_string_append_cstrf(
str, "%s (%.2lf)", ivy_ast_node_type_to_string(node->n_type),
v->n_value->t_double); v->n_value->t_double);
} }

View File

@@ -1,5 +1,6 @@
#include "node.h" #include "node.h"
#include <blue/object/string.h>
#include <ivy/lang/ast.h> #include <ivy/lang/ast.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -192,7 +193,6 @@ struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type)
return node; return node;
} }
void ivy_ast_node_to_string(struct ivy_ast_node *node, struct b_string *out) 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); 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) { if (type_info->n_to_string) {
type_info->n_to_string(node, out); type_info->n_to_string(node, out);
} else { } 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));
} }
} }

View File

@@ -25,22 +25,23 @@ enum property_flags {
struct property_parser_state { struct property_parser_state {
struct parser_state s_base; struct parser_state s_base;
enum property_type s_flags; enum property_flags s_flags;
struct ivy_ast_node *s_prev_node; struct ivy_ast_node *s_prev_node;
struct ivy_token *s_ident; struct ivy_token *s_ident;
struct ivy_ast_node *s_get, *s_set; struct ivy_ast_node *s_get, *s_set;
/* one of either 0, PROPERTY_GET, or PROPERTY_SET, depending on which part of the /* one of either 0, PROPERTY_GET, or PROPERTY_SET, depending on which
* property is currently being parsed. */ * part of the property is currently being parsed. */
unsigned int s_cur_component; unsigned int s_cur_component;
unsigned int s_prev; unsigned int s_prev;
}; };
static void finalise_property(struct property_parser_state *state) 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) { if (state->s_flags & PROPERTY_GET) {
prop->n_flags |= IVY_AST_PROPERTY_GET; prop->n_flags |= IVY_AST_PROPERTY_GET;
@@ -62,7 +63,8 @@ static void finalise_property(struct property_parser_state *state)
static struct token_parse_result parse_ident( static struct token_parse_result parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (state->s_prev != IVY_SYM_DOLLAR) {
/* the only ident we're looking for is the property name, which /* 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( static struct token_parse_result parse_pipe(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (state->s_prev != IVY_TOK_IDENT) {
/* the only pipe we're looking for is the property /* 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( static struct token_parse_result parse_left_paren(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (state->s_prev != IVY_TOK_IDENT) {
/* the only left paren we're looking for comes after the /* the only left paren we're looking for comes after the
@@ -127,7 +131,8 @@ static struct token_parse_result parse_left_paren(
static struct token_parse_result parse_right_paren( static struct token_parse_result parse_right_paren(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (PROPERTY_TYPE(state->s_flags) != PROPERTY_AUTO) {
/* can only use parenthesis in auto-properties. */ /* can only use parenthesis in auto-properties. */
@@ -152,7 +157,8 @@ static struct token_parse_result parse_right_paren(
static struct token_parse_result parse_equal_right_arrow( static struct token_parse_result parse_equal_right_arrow(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (PROPERTY_TYPE(state->s_flags) != PROPERTY_FULL) {
/* this symbol cannot be used in auto-properties. */ /* 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; 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_terminator = IVY_SYM_COMMA;
expr->s_subexpr_depth = 1; 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( static struct token_parse_result parse_comma(
struct ivy_parser *ctx, struct ivy_token *tok) 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 */ /* this symbol can only be used after get or set */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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); 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( static struct token_parse_result parse_dot(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (PROPERTY_TYPE(state->s_flags) != PROPERTY_FULL) {
/* this symbol cannot be used in auto-properties. */ /* this symbol cannot be used in auto-properties. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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 */ /* not actually parsing a getter or setter */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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( static struct token_parse_result parse_get(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (state->s_flags & PROPERTY_GET) {
/* property already has getter defined */ /* property already has getter defined */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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. */ /* getter is not expected here. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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. */ /* getter is not expected here. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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( static struct token_parse_result parse_set(
struct ivy_parser *ctx, struct ivy_token *tok) 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) { if (state->s_flags & PROPERTY_SET) {
/* property already has setter defined */ /* property already has setter defined */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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. */ /* setter is not expected here. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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. */ /* setter is not expected here. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); 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( 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 property_parser_state *state = (struct property_parser_state *)parent; struct property_parser_state *state
= (struct property_parser_state *)parent;
if (state->s_prev_node) { if (state->s_prev_node) {
return IVY_ERR_BAD_SYNTAX; 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; 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) { if (prop->n_flags & IVY_AST_PROPERTY_GET) {
b_string_append_cstr(str, "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, ", "); b_string_append_cstr(str, ", ");
} }

View File

@@ -1,20 +1,25 @@
#include "ctx.h" #include "ctx.h"
#include "expr/expr.h" #include "expr/expr.h"
#include "iterate.h"
#include "node.h" #include "node.h"
#include <blue/object/string.h> #include <blue/object/string.h>
static void to_string(struct ivy_ast_node *node, b_string *str) static void to_string(struct ivy_ast_node *node, b_string *str)
{ {
struct ivy_ast_string_node *s = (struct ivy_ast_string_node *)node; 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 token_parse_result parse_string(struct ivy_parser *ctx, struct ivy_token *tok)
struct ivy_parser *ctx, struct ivy_token *tok)
{ {
struct parser_state *state = parser_get_state_generic(ctx); 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_fstring_node *parent
struct ivy_ast_string_node *child = (struct ivy_ast_string_node *)ast_node_create(IVY_AST_STRING); = (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) { if (!child) {
return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0); 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 token_parse_result parse_left_brace(
struct ivy_parser *ctx, struct ivy_token *tok) 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; expr->s_terminator = IVY_SYM_RIGHT_BRACE;
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }