From 5c5a2d236cd6f79522cd461ab75fb0b758385046 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 23 Nov 2024 10:15:40 +0000 Subject: [PATCH] lang: ast: add stub parser functions --- lang/ast.c | 106 ++++++++++++++++++++++++++++++++++++ lang/ast.h | 19 +++++++ lang/include/ivy/lang/ast.h | 30 ++++++++-- 3 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 lang/ast.h diff --git a/lang/ast.c b/lang/ast.c index e69de29..1cf9846 100644 --- a/lang/ast.c +++ b/lang/ast.c @@ -0,0 +1,106 @@ +#include "ast.h" + +#include +#include +#include + +enum ivy_status ivy_parser_create(struct ivy_parser **parser) +{ + struct ivy_parser *out = malloc(sizeof *out); + if (!out) { + return IVY_ERR_NO_MEMORY; + } + + memset(out, 0x0, sizeof *out); + + *parser = out; + return IVY_OK; +} + +void ivy_parser_destroy(struct ivy_parser *parser) +{ + free(parser); +} + +enum ivy_status ivy_parser_get_status(struct ivy_parser *parser) +{ + return parser->p_status; +} + +struct ivy_ast_node *ivy_parser_read(struct ivy_parser *lex) +{ + return NULL; +} + +void ivy_ast_node_destroy(struct ivy_ast_node *node) +{ +} + +#define ENUM_STR(x) \ + case x: \ + return #x + +const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v) +{ + switch (v) { + ENUM_STR(IVY_AST_NONE); + ENUM_STR(IVY_AST_UNIT); + ENUM_STR(IVY_AST_OP); + ENUM_STR(IVY_AST_MSG); + ENUM_STR(IVY_AST_CLASS); + ENUM_STR(IVY_AST_MSGH); + ENUM_STR(IVY_AST_PROPERTY); + ENUM_STR(IVY_AST_LAMBDA); + ENUM_STR(IVY_AST_UNIT_PACKAGE); + ENUM_STR(IVY_AST_UNIT_IMPORT); + ENUM_STR(IVY_AST_INT); + ENUM_STR(IVY_AST_DOUBLE); + ENUM_STR(IVY_AST_STRING); + ENUM_STR(IVY_AST_FSTRING); + ENUM_STR(IVY_AST_ATOM); + ENUM_STR(IVY_AST_IDENT); + ENUM_STR(IVY_AST_FOR_LOOP); + ENUM_STR(IVY_AST_WHILE_LOOP); + ENUM_STR(IVY_AST_COND_GROUP); + ENUM_STR(IVY_AST_COND); + ENUM_STR(IVY_AST_TUPLE); + ENUM_STR(IVY_AST_DO); + default: + return ""; + } +} + +const char *ivy_ast_op_to_string(enum ivy_ast_op v) +{ + switch (v) { + ENUM_STR(IVY_OP_NONE); + ENUM_STR(IVY_OP_ASSIGN); + ENUM_STR(IVY_OP_ADD); + ENUM_STR(IVY_OP_SUBTRACT); + ENUM_STR(IVY_OP_MULTIPLY); + ENUM_STR(IVY_OP_DIVIDE); + ENUM_STR(IVY_OP_LESS_THAN); + ENUM_STR(IVY_OP_GREATER_THAN); + ENUM_STR(IVY_OP_EQUAL); + ENUM_STR(IVY_OP_NOT_EQUAL); + ENUM_STR(IVY_OP_LESS_EQUAL); + ENUM_STR(IVY_OP_GREATER_EQUAL); + ENUM_STR(IVY_OP_AND); + ENUM_STR(IVY_OP_OR); + ENUM_STR(IVY_OP_IS); + ENUM_STR(IVY_OP_NOT); + default: + return ""; + } +} + +const char *ivy_ast_msgh_recipient_type_to_string(enum ivy_ast_msgh_recipient_type v) +{ + switch (v) { + ENUM_STR(IVY_AST_MSGH_NONE); + ENUM_STR(IVY_AST_MSGH_OBJECT); + ENUM_STR(IVY_AST_MSGH_CLASS); + default: + return ""; + } +} diff --git a/lang/ast.h b/lang/ast.h new file mode 100644 index 0000000..7a628c3 --- /dev/null +++ b/lang/ast.h @@ -0,0 +1,19 @@ +#ifndef _AST_H_ +#define _AST_H_ + +#include +#include + +struct ivy_parser_state { + b_queue_entry s_entry; + struct ivy_ast_node *s_parent; +}; + +struct ivy_parser { + enum ivy_status p_status; + b_queue p_state; + b_queue p_token_queue; + b_queue p_node_queue; +}; + +#endif diff --git a/lang/include/ivy/lang/ast.h b/lang/include/ivy/lang/ast.h index 44792e2..7d25184 100644 --- a/lang/include/ivy/lang/ast.h +++ b/lang/include/ivy/lang/ast.h @@ -2,8 +2,11 @@ #define IVY_LANG_AST_H_ #include +#include +#include struct ivy_token; +struct ivy_parser; enum ivy_ast_node_type { IVY_AST_NONE = 0x00, @@ -67,7 +70,7 @@ struct ivy_ast_unit_node { struct ivy_ast_op_node { struct ivy_ast_node n_base; enum ivy_ast_op n_op; - struct ivy_ast_node *n_left; // NULL for unary operators. + struct ivy_ast_node *n_left; // NULL for unary operators. struct ivy_ast_node *n_right; }; @@ -111,13 +114,14 @@ struct ivy_ast_property_node { struct ivy_token *n_ident; /* one of either: * a) a lambda. the lambda is executed to get the property value; or, - * b) a constant value. the constant is returned as the property value. - * c) NULL. the property is write-only. + * b) a constant value. the constant is returned as the property + * value. c) NULL. the property is write-only. */ struct ivy_ast_node *n_get; /* one of either: - * a) a lambda. the lambda is executed with the provided value as a parameter to set the property value; or, - * b) NULL. the property is read-only. + * a) a lambda. the lambda is executed with the provided value as a + * parameter to set the property value; or, b) NULL. the property is + * read-only. */ struct ivy_ast_node *n_set; }; @@ -220,4 +224,18 @@ struct ivy_ast_do_node { b_queue n_members; }; -#endif \ No newline at end of file +IVY_API enum ivy_status ivy_parser_create(struct ivy_parser **parser); +IVY_API void ivy_parser_destroy(struct ivy_parser *parser); + +IVY_API enum ivy_status ivy_parser_get_status(struct ivy_parser *parser); + +IVY_API void ivy_parser_push_token(struct ivy_parser *lex, struct ivy_token *tok); + +IVY_API void ivy_ast_node_destroy(struct ivy_ast_node *node); + +IVY_API const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v); +IVY_API const char *ivy_ast_op_to_string(enum ivy_ast_op v); +IVY_API const char *ivy_ast_msgh_recipient_type_to_string( + enum ivy_ast_msgh_recipient_type v); + +#endif