2024-12-03 13:26:55 +00:00
|
|
|
#ifndef _AST_EXPR_EXPR_H_
|
|
|
|
|
#define _AST_EXPR_EXPR_H_
|
|
|
|
|
|
|
|
|
|
#include "../ctx.h"
|
|
|
|
|
#include "../node.h"
|
|
|
|
|
|
|
|
|
|
#include <blue/core/queue.h>
|
|
|
|
|
|
2024-12-07 21:28:25 +00:00
|
|
|
#define EXPR_TERMINATOR_MAX 8
|
|
|
|
|
|
2024-12-03 13:26:55 +00:00
|
|
|
struct ivy_ast_node;
|
|
|
|
|
struct ivy_ast_msg_node;
|
|
|
|
|
|
|
|
|
|
enum expr_type {
|
|
|
|
|
EXPR_TYPE_NONE = 0,
|
|
|
|
|
/* if-else, while/for, match, etc (any expression that ends with an
|
|
|
|
|
* 'end' keyword */
|
|
|
|
|
EXPR_TYPE_STMT,
|
|
|
|
|
/* arithmetic and message-sending expressions (any expression that ends
|
|
|
|
|
* implicitly or with an expression separator */
|
|
|
|
|
EXPR_TYPE_ARITH,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum expr_subtype {
|
|
|
|
|
EXPR_SUBTYPE_NONE = 0,
|
|
|
|
|
/* generic parenthesis-enclosed arithmetic expressions */
|
|
|
|
|
EXPR_SUBTYPE_PAREN,
|
2024-12-03 21:57:44 +00:00
|
|
|
/* any kind of message */
|
|
|
|
|
EXPR_SUBTYPE_MSG,
|
2024-12-03 13:26:55 +00:00
|
|
|
/* keyword messages */
|
|
|
|
|
EXPR_SUBTYPE_KEYWORD_MSG,
|
|
|
|
|
/* expression delimited by labels */
|
|
|
|
|
EXPR_SUBTYPE_KEYWORD_ARG,
|
|
|
|
|
/* complex messages */
|
|
|
|
|
EXPR_SUBTYPE_COMPLEX_MSG,
|
2025-04-23 10:53:34 +01:00
|
|
|
/* expression delimited by commas */
|
|
|
|
|
EXPR_SUBTYPE_COMPLEX_ARG,
|
2024-12-03 21:57:44 +00:00
|
|
|
/* message cascade operation */
|
|
|
|
|
EXPR_SUBTYPE_CASCADE,
|
2024-12-03 13:26:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum expr_component {
|
|
|
|
|
EXPR_CMP_NONE = 0,
|
|
|
|
|
EXPR_CMP_OPERATOR,
|
|
|
|
|
EXPR_CMP_OPERAND,
|
|
|
|
|
EXPR_CMP_MSG,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct expr_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
|
|
|
|
enum expr_type s_type;
|
2024-12-03 21:57:44 +00:00
|
|
|
enum expr_subtype s_sub_type;
|
|
|
|
|
|
2024-12-06 10:01:56 +00:00
|
|
|
/* this is a return statement (prefixed with a caret) */
|
|
|
|
|
bool s_return;
|
|
|
|
|
|
2024-12-03 13:26:55 +00:00
|
|
|
/* for arithmetic expressions, this records whether the previous
|
|
|
|
|
* component (either a token or parenthesised group of tokens) is an
|
|
|
|
|
* operator, operand, or message */
|
|
|
|
|
enum expr_component s_prev_component;
|
|
|
|
|
/* the token type/keyword type/symbol type of the last token that was
|
|
|
|
|
* encountered */
|
|
|
|
|
unsigned int s_prev_token;
|
|
|
|
|
/* if this is an arithmetic expression, this variable is the depth
|
|
|
|
|
* of parentheses that this sub-expression is at */
|
|
|
|
|
unsigned int s_paren_depth;
|
|
|
|
|
|
2024-12-03 21:57:44 +00:00
|
|
|
/* if this expression is parsing a sub-component of a larger expression,
|
|
|
|
|
* the depth of the expression is recorded here. */
|
|
|
|
|
unsigned int s_subexpr_depth;
|
|
|
|
|
|
2024-12-07 21:28:25 +00:00
|
|
|
/* the expression will be terminated when any token in this list
|
|
|
|
|
* is encountered. the token that terminated the expression will
|
|
|
|
|
* not be consumed. */
|
|
|
|
|
unsigned short s_terminators[EXPR_TERMINATOR_MAX];
|
|
|
|
|
unsigned short s_nr_terminators;
|
2024-12-03 13:26:55 +00:00
|
|
|
|
|
|
|
|
b_queue s_output_queue;
|
|
|
|
|
b_queue s_operator_stack;
|
|
|
|
|
|
|
|
|
|
/* these variables are for keyword-message expressions */
|
|
|
|
|
struct ivy_ast_node *s_recipient;
|
|
|
|
|
struct ivy_ast_msg_node *s_msg;
|
|
|
|
|
b_queue s_labels;
|
2024-12-03 21:57:44 +00:00
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
/* for keyword-messages, this is a list of arg values. */
|
|
|
|
|
b_queue s_args;
|
|
|
|
|
/* for cascade operators, this is a list of messages to send to the recipient. */
|
|
|
|
|
b_queue s_cascade_msg;
|
|
|
|
|
};
|
2024-12-03 13:26:55 +00:00
|
|
|
};
|
|
|
|
|
|
2024-12-04 16:35:19 +00:00
|
|
|
/* general functions */
|
|
|
|
|
|
2024-12-07 21:28:25 +00:00
|
|
|
extern void expr_add_terminator(struct expr_parser_state *state, unsigned short tok);
|
2025-01-16 13:15:48 +00:00
|
|
|
extern void expr_copy_terminators(
|
|
|
|
|
const struct expr_parser_state *src, struct expr_parser_state *dest);
|
|
|
|
|
extern bool expr_terminates_at_token(
|
|
|
|
|
struct expr_parser_state *state, unsigned short tok);
|
2024-12-07 21:28:25 +00:00
|
|
|
|
2024-12-04 16:35:19 +00:00
|
|
|
extern struct token_parse_result expr_finalise(
|
|
|
|
|
struct ivy_parser *ctx, struct expr_parser_state *state,
|
|
|
|
|
enum ivy_operator_precedence min_precedence, struct ivy_ast_node **expr);
|
|
|
|
|
extern struct token_parse_result expr_finalise_and_return(
|
|
|
|
|
struct ivy_parser *ctx, struct expr_parser_state *state);
|
|
|
|
|
|
|
|
|
|
/* arithmetic parser callbacks */
|
|
|
|
|
|
2024-12-03 13:26:55 +00:00
|
|
|
extern void arith_push_operator(
|
|
|
|
|
struct expr_parser_state *state, struct ivy_ast_node *node);
|
2024-12-04 22:22:25 +00:00
|
|
|
extern enum ivy_status arith_push_operand(
|
2024-12-03 13:26:55 +00:00
|
|
|
struct expr_parser_state *state, struct ivy_token *tok);
|
|
|
|
|
|
|
|
|
|
extern enum ivy_status arith_add_child(
|
|
|
|
|
struct parser_state *parent, struct ivy_ast_node *child);
|
|
|
|
|
|
|
|
|
|
extern struct token_parse_result arith_parse_ident(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 13:46:58 +00:00
|
|
|
extern struct token_parse_result arith_parse_fstring(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-03 13:26:55 +00:00
|
|
|
extern struct token_parse_result arith_parse_operand(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_operator(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_label(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_left_paren(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_right_paren(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 13:46:58 +00:00
|
|
|
extern struct token_parse_result arith_parse_left_brace(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_right_brace(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 22:26:06 +00:00
|
|
|
extern struct token_parse_result arith_parse_left_bracket(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_right_bracket(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-03 21:57:44 +00:00
|
|
|
extern struct token_parse_result arith_parse_semicolon(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-03 13:26:55 +00:00
|
|
|
extern struct token_parse_result arith_parse_dot(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 13:22:33 +00:00
|
|
|
extern struct token_parse_result arith_parse_bang(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 10:01:56 +00:00
|
|
|
extern struct token_parse_result arith_parse_caret(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-04 22:22:25 +00:00
|
|
|
extern struct token_parse_result arith_parse_comma(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_equal_right_angle(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result arith_parse_in(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2025-04-10 13:04:12 +01:00
|
|
|
extern struct token_parse_result arith_parse_is(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 10:02:31 +00:00
|
|
|
extern struct token_parse_result arith_parse_do(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-03 13:26:55 +00:00
|
|
|
|
2024-12-04 16:35:19 +00:00
|
|
|
/* statement parser callbacks */
|
|
|
|
|
|
2025-01-16 13:15:48 +00:00
|
|
|
extern struct token_parse_result stmt_parse_try(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-06 10:02:31 +00:00
|
|
|
extern struct token_parse_result stmt_parse_for(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-05 19:29:21 +00:00
|
|
|
extern struct token_parse_result stmt_parse_while(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-04 22:22:25 +00:00
|
|
|
extern struct token_parse_result stmt_parse_match(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-04 16:35:19 +00:00
|
|
|
extern struct token_parse_result stmt_parse_if(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result stmt_parse_then(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
struct token_parse_result stmt_parse_else(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
struct token_parse_result stmt_parse_end(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2025-09-08 15:55:12 +01:00
|
|
|
extern struct token_parse_result stmt_parse_break(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
|
|
|
|
extern struct token_parse_result stmt_parse_continue(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok);
|
2024-12-04 16:35:19 +00:00
|
|
|
|
2024-12-03 13:26:55 +00:00
|
|
|
#endif
|