asm: implement an asm parser and emitter
This commit is contained in:
66
asm/parse/atom.c
Normal file
66
asm/parse/atom.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "parse.h"
|
||||
#include <ivy/ident.h>
|
||||
#include <ivy/asm/bin.h>
|
||||
#include <ivy/asm/assembler.h>
|
||||
#include <ivy/asm/lex.h>
|
||||
|
||||
struct atom_parser_state {
|
||||
struct parser_state s_base;
|
||||
unsigned int s_prev_token;
|
||||
|
||||
struct ivy_asm_token *s_name;
|
||||
};
|
||||
|
||||
static enum ivy_status init_state(
|
||||
struct ivy_asm_parser *ctx, struct parser_state *s)
|
||||
{
|
||||
struct atom_parser_state *state = (struct atom_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 atom_parser_state *state = (struct atom_parser_state *)asm_parser_get_state(ctx);
|
||||
if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (state->s_name) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
state->s_name = tok;
|
||||
state->s_prev_token = IVY_ASM_TOK_IDENT;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_right_paren(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct atom_parser_state *state = (struct atom_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
if (state->s_prev_token != IVY_ASM_TOK_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (!state->s_name) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
asm_parser_pop_state(ctx, state->s_name);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
struct parser_state_type atom_parser_state_type = {
|
||||
.n_init_state = init_state,
|
||||
.n_state_size = sizeof(struct atom_parser_state),
|
||||
.n_token_parsers = {
|
||||
TOK_PARSER(IDENT, parse_ident),
|
||||
},
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER(RIGHT_PAREN, parse_right_paren),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user