asm: implement an asm parser and emitter

This commit is contained in:
2024-12-13 12:26:01 +00:00
parent 0a8d913fdd
commit 5fe1a21978
19 changed files with 1106 additions and 49 deletions

86
asm/parse/ident.c Normal file
View File

@@ -0,0 +1,86 @@
#include "parse.h"
#include <ivy/ident.h>
#include <ivy/asm/bin.h>
#include <ivy/asm/assembler.h>
#include <ivy/asm/lex.h>
struct ident_parser_state {
struct parser_state s_base;
unsigned int s_prev_token;
b_queue s_parts;
};
static enum ivy_status init_state(
struct ivy_asm_parser *ctx, struct parser_state *s)
{
struct ident_parser_state *state = (struct ident_parser_state *)s;
state->s_prev_token = IVY_ASM_SYM_LEFT_PAREN;
return IVY_OK;
}
static enum ivy_status parse_ident(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
{
struct ident_parser_state *state = (struct ident_parser_state *)asm_parser_get_state(ctx);
if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN && state->s_prev_token != IVY_ASM_SYM_DOT) {
return IVY_ERR_BAD_SYNTAX;
}
b_queue_push_back(&state->s_parts, &tok->t_entry);
state->s_prev_token = IVY_ASM_TOK_IDENT;
return IVY_OK;
}
static enum ivy_status parse_dot(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
{
struct ident_parser_state *state = (struct ident_parser_state *)asm_parser_get_state(ctx);
if (state->s_prev_token != IVY_ASM_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
}
state->s_prev_token = IVY_ASM_SYM_DOT;
return IVY_OK;
}
static enum ivy_status parse_right_paren(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
{
struct ident_parser_state *state = (struct ident_parser_state *)asm_parser_get_state(ctx);
if (state->s_prev_token != IVY_ASM_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
}
struct ivy_ident *ident = ivy_ident_create();
b_queue_iterator it = {0};
b_queue_iterator_begin(&state->s_parts, &it);
while (b_queue_iterator_is_valid(&it)) {
struct ivy_asm_token *tok = b_unbox(struct ivy_asm_token, it.entry, t_entry);
b_queue_iterator_erase(&it);
ivy_ident_add_part(ident, tok->t_str);
ivy_asm_token_destroy(tok);
}
asm_parser_pop_state(ctx, ident);
return IVY_OK;
}
struct parser_state_type ident_parser_state_type = {
.n_init_state = init_state,
.n_state_size = sizeof(struct ident_parser_state),
.n_token_parsers = {
TOK_PARSER(IDENT, parse_ident),
},
.n_symbol_parsers = {
SYM_PARSER(DOT, parse_dot),
SYM_PARSER(RIGHT_PAREN, parse_right_paren),
},
};