2024-12-13 12:26:01 +00:00
|
|
|
#include "parse.h"
|
2025-05-15 12:09:39 +01:00
|
|
|
|
|
|
|
|
#include <ivy/asm/assembler.h>
|
|
|
|
|
#include <ivy/asm/bin.h>
|
2024-12-13 12:26:01 +00:00
|
|
|
#include <ivy/ident.h>
|
|
|
|
|
#include <ivy/selector.h>
|
|
|
|
|
|
|
|
|
|
struct constpool_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
|
|
|
|
unsigned int s_prev_token;
|
2025-05-15 12:09:39 +01:00
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
enum ivy_assembler_pval_type s_current_pval_type;
|
|
|
|
|
|
|
|
|
|
union {
|
|
|
|
|
struct ivy_selector *sel;
|
|
|
|
|
struct ivy_ident *ident;
|
|
|
|
|
struct ivy_asm_token *tok;
|
|
|
|
|
} s_current_pval_val;
|
|
|
|
|
|
|
|
|
|
bool s_current_index_set;
|
2024-12-13 17:20:58 +00:00
|
|
|
size_t s_current_index;
|
2024-12-13 12:26:01 +00:00
|
|
|
};
|
|
|
|
|
|
2025-05-15 12:09:39 +01:00
|
|
|
static enum ivy_status parse_linefeed(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
2024-12-13 12:26:01 +00:00
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
state->s_prev_token = IVY_ASM_TOK_LINEFEED;
|
2025-05-15 12:09:39 +01:00
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
if (!state->s_current_index_set) {
|
|
|
|
|
/* no index set */
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 17:20:58 +00:00
|
|
|
enum ivy_status status = IVY_OK;
|
|
|
|
|
ivy_extended_data_key key = IVY_EX_DATA_KEY_NULL;
|
2024-12-13 12:26:01 +00:00
|
|
|
struct ivy_asm_token *tmp = NULL;
|
|
|
|
|
|
|
|
|
|
switch (state->s_current_pval_type) {
|
|
|
|
|
case IVY_ASM_PVAL_IDENT:
|
2024-12-13 17:20:58 +00:00
|
|
|
status = ivy_assembler_put_pval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_PVAL_IDENT,
|
2025-05-15 12:09:39 +01:00
|
|
|
state->s_current_index, state->s_base.s_previous_value,
|
|
|
|
|
&key);
|
2024-12-13 17:20:58 +00:00
|
|
|
if (status != IVY_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
ivy_ident_destroy(state->s_base.s_previous_value);
|
|
|
|
|
break;
|
|
|
|
|
case IVY_ASM_PVAL_ATOM:
|
|
|
|
|
tmp = state->s_base.s_previous_value;
|
2024-12-13 17:20:58 +00:00
|
|
|
status = ivy_assembler_put_pval(
|
2025-05-15 12:09:39 +01:00
|
|
|
ctx->p_assembler, IVY_ASM_PVAL_ATOM,
|
|
|
|
|
state->s_current_index, tmp->t_str, &key);
|
2024-12-13 17:20:58 +00:00
|
|
|
if (status != IVY_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
ivy_asm_token_destroy(tmp);
|
|
|
|
|
break;
|
|
|
|
|
case IVY_ASM_PVAL_SINT:
|
2025-05-16 16:56:40 +01:00
|
|
|
status = ivy_assembler_put_pval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_PVAL_SINT, state->s_current_index,
|
|
|
|
|
&state->s_current_pval_val.tok->t_int.v, &key);
|
|
|
|
|
if (status != IVY_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
break;
|
|
|
|
|
case IVY_ASM_PVAL_UINT:
|
2025-05-16 16:56:40 +01:00
|
|
|
status = ivy_assembler_put_pval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_PVAL_UINT, state->s_current_index,
|
|
|
|
|
&state->s_current_pval_val.tok->t_int.uv, &key);
|
|
|
|
|
if (status != IVY_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
break;
|
|
|
|
|
case IVY_ASM_PVAL_SELECTOR:
|
2024-12-13 17:20:58 +00:00
|
|
|
status = ivy_assembler_put_pval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_PVAL_SELECTOR,
|
2025-05-15 12:09:39 +01:00
|
|
|
state->s_current_index, state->s_base.s_previous_value,
|
|
|
|
|
&key);
|
2024-12-13 17:20:58 +00:00
|
|
|
if (status != IVY_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
ivy_selector_destroy(state->s_base.s_previous_value);
|
|
|
|
|
break;
|
|
|
|
|
case IVY_ASM_PVAL_STRING:
|
|
|
|
|
tmp = state->s_current_pval_val.tok;
|
2024-12-13 17:20:58 +00:00
|
|
|
status = ivy_assembler_put_pval(
|
2025-05-15 12:09:39 +01:00
|
|
|
ctx->p_assembler, IVY_ASM_PVAL_STRING,
|
|
|
|
|
state->s_current_index, tmp->t_str, &key);
|
2024-12-13 17:20:58 +00:00
|
|
|
if (status != IVY_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
ivy_asm_token_destroy(tmp);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_current_index = 0;
|
|
|
|
|
state->s_current_index_set = false;
|
|
|
|
|
state->s_current_pval_type = IVY_ASM_PVAL_NONE;
|
|
|
|
|
state->s_base.s_previous_value = NULL;
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_int(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
2024-12-13 12:26:01 +00:00
|
|
|
|
|
|
|
|
if (state->s_prev_token == IVY_ASM_SYM_COLON) {
|
|
|
|
|
/* this is a const value. */
|
|
|
|
|
state->s_current_pval_type = IVY_ASM_PVAL_UINT;
|
|
|
|
|
state->s_current_pval_val.tok = tok;
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_INT;
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!state->s_current_index_set) {
|
2025-05-15 12:09:39 +01:00
|
|
|
state->s_current_index
|
|
|
|
|
= tok->t_int.sign ? tok->t_int.v : tok->t_int.uv;
|
2024-12-13 12:26:01 +00:00
|
|
|
state->s_current_index_set = true;
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_INT;
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* not sure what this is but we aren't expecting it. */
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_colon(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
2024-12-13 12:26:01 +00:00
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_INT) {
|
|
|
|
|
/* not expected at this time */
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_prev_token = IVY_ASM_SYM_COLON;
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_string(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
if (state->s_prev_token != IVY_ASM_SYM_COLON) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_current_pval_type = IVY_ASM_PVAL_STRING;
|
|
|
|
|
state->s_current_pval_val.tok = tok;
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_STRING;
|
2025-05-15 12:09:39 +01:00
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_atom(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
if (state->s_prev_token != IVY_ASM_SYM_COLON) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_current_pval_type = IVY_ASM_PVAL_ATOM;
|
|
|
|
|
state->s_current_pval_val.tok = tok;
|
|
|
|
|
state->s_prev_token = IVY_ASM_KW_ATOM;
|
2025-05-15 12:09:39 +01:00
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_kw_ident(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
if (state->s_prev_token != IVY_ASM_SYM_COLON) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_current_pval_type = IVY_ASM_PVAL_IDENT;
|
|
|
|
|
state->s_current_pval_val.tok = tok;
|
|
|
|
|
state->s_prev_token = IVY_ASM_KW_IDENT;
|
2025-05-15 12:09:39 +01:00
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_selector(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
if (state->s_prev_token != IVY_ASM_SYM_COLON) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_current_pval_type = IVY_ASM_PVAL_SELECTOR;
|
|
|
|
|
state->s_prev_token = IVY_ASM_KW_SELECTOR;
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_end(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
if (state->s_current_index_set) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
asm_parser_pop_state(ctx, NULL);
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_left_paren(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2025-05-15 12:09:39 +01:00
|
|
|
struct constpool_parser_state *state
|
|
|
|
|
= (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
switch (state->s_prev_token) {
|
|
|
|
|
case IVY_ASM_KW_SELECTOR:
|
2024-12-14 20:26:04 +00:00
|
|
|
asm_parser_push_state(ctx, ASM_PARSER_SELECTOR, NULL);
|
2024-12-13 12:26:01 +00:00
|
|
|
break;
|
|
|
|
|
case IVY_ASM_KW_IDENT:
|
2024-12-14 20:26:04 +00:00
|
|
|
asm_parser_push_state(ctx, ASM_PARSER_IDENT, NULL);
|
2024-12-13 12:26:01 +00:00
|
|
|
break;
|
|
|
|
|
case IVY_ASM_KW_ATOM:
|
2024-12-14 20:26:04 +00:00
|
|
|
asm_parser_push_state(ctx, ASM_PARSER_ATOM, NULL);
|
2024-12-13 12:26:01 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void init_state(struct ivy_asm_parser *ctx, struct parser_state *state)
|
|
|
|
|
{
|
|
|
|
|
ivy_assembler_begin_scope(ctx->p_assembler, IVY_ASM_SCOPE_CONSTPOOL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void finish_state(struct ivy_asm_parser *ctx, struct parser_state *state)
|
|
|
|
|
{
|
|
|
|
|
ivy_assembler_end_scope(ctx->p_assembler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct parser_state_type constpool_parser_state_type = {
|
|
|
|
|
.n_init_state = init_state,
|
|
|
|
|
.n_finish_state = finish_state,
|
|
|
|
|
.n_state_size = sizeof(struct constpool_parser_state),
|
|
|
|
|
.n_token_parsers = {
|
|
|
|
|
TOK_PARSER(INT, parse_int),
|
|
|
|
|
TOK_PARSER(STRING, parse_string),
|
|
|
|
|
TOK_PARSER(LINEFEED, parse_linefeed),
|
|
|
|
|
},
|
|
|
|
|
.n_symbol_parsers = {
|
|
|
|
|
SYM_PARSER(COLON, parse_colon),
|
|
|
|
|
SYM_PARSER(LEFT_PAREN, parse_left_paren),
|
|
|
|
|
},
|
|
|
|
|
.n_keyword_parsers = {
|
|
|
|
|
KW_PARSER(IDENT, parse_kw_ident),
|
|
|
|
|
KW_PARSER(SELECTOR, parse_selector),
|
|
|
|
|
KW_PARSER(ATOM, parse_atom),
|
|
|
|
|
KW_PARSER(END, parse_end),
|
|
|
|
|
}
|
2025-05-15 12:09:39 +01:00
|
|
|
};
|