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),
|
||||
},
|
||||
};
|
||||
0
asm/parse/block.c
Normal file
0
asm/parse/block.c
Normal file
0
asm/parse/class.c
Normal file
0
asm/parse/class.c
Normal file
247
asm/parse/constpool.c
Normal file
247
asm/parse/constpool.c
Normal file
@@ -0,0 +1,247 @@
|
||||
#include "parse.h"
|
||||
#include <ivy/ident.h>
|
||||
#include <ivy/selector.h>
|
||||
#include <ivy/asm/bin.h>
|
||||
#include <ivy/asm/assembler.h>
|
||||
|
||||
struct constpool_parser_state {
|
||||
struct parser_state s_base;
|
||||
unsigned int s_prev_token;
|
||||
|
||||
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;
|
||||
unsigned int s_current_index;
|
||||
};
|
||||
|
||||
static enum ivy_status parse_linefeed(struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
struct ivy_bin_constpool_table_entry entry = {0};
|
||||
|
||||
state->s_prev_token = IVY_ASM_TOK_LINEFEED;
|
||||
|
||||
if (!state->s_current_index_set) {
|
||||
/* no index set */
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
struct ivy_asm_token *tmp = NULL;
|
||||
|
||||
switch (state->s_current_pval_type) {
|
||||
case IVY_ASM_PVAL_IDENT:
|
||||
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_IDENT);
|
||||
entry.e_ex_handle = b_i32_htob(ivy_assembler_put_pval(ctx->p_assembler, IVY_ASM_PVAL_IDENT, state->s_current_index, state->s_base.s_previous_value));
|
||||
ivy_ident_destroy(state->s_base.s_previous_value);
|
||||
break;
|
||||
case IVY_ASM_PVAL_ATOM:
|
||||
tmp = state->s_base.s_previous_value;
|
||||
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_ATOM);
|
||||
entry.e_ex_handle = b_i32_htob(ivy_assembler_put_pval(ctx->p_assembler, IVY_ASM_PVAL_ATOM, state->s_current_index, tmp->t_str));
|
||||
ivy_asm_token_destroy(tmp);
|
||||
break;
|
||||
case IVY_ASM_PVAL_SINT:
|
||||
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_INT);
|
||||
entry.e_int = b_i32_htob(state->s_current_pval_val.tok->t_int.v);
|
||||
break;
|
||||
case IVY_ASM_PVAL_UINT:
|
||||
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_UINT);
|
||||
entry.e_int = b_i32_htob(state->s_current_pval_val.tok->t_int.uv);
|
||||
break;
|
||||
case IVY_ASM_PVAL_SELECTOR:
|
||||
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_SELECTOR);
|
||||
entry.e_ex_handle = b_i32_htob(ivy_assembler_put_pval(ctx->p_assembler, IVY_ASM_PVAL_SELECTOR, state->s_current_index, state->s_base.s_previous_value));
|
||||
ivy_selector_destroy(state->s_base.s_previous_value);
|
||||
break;
|
||||
case IVY_ASM_PVAL_STRING:
|
||||
tmp = state->s_current_pval_val.tok;
|
||||
entry.e_type = b_i32_htob(IVY_CONSTPOOL_TABLE_STRING);
|
||||
entry.e_ex_handle = b_i32_htob(ivy_assembler_put_pval(ctx->p_assembler, IVY_ASM_PVAL_STRING, state->s_current_index, tmp->t_str));
|
||||
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)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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) {
|
||||
state->s_current_index = tok->t_int.uv;
|
||||
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)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_atom(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_kw_ident(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_selector(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
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)
|
||||
{
|
||||
struct constpool_parser_state *state = (struct constpool_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
switch (state->s_prev_token) {
|
||||
case IVY_ASM_KW_SELECTOR:
|
||||
asm_parser_push_state(ctx, ASM_PARSER_SELECTOR);
|
||||
break;
|
||||
case IVY_ASM_KW_IDENT:
|
||||
asm_parser_push_state(ctx, ASM_PARSER_IDENT);
|
||||
break;
|
||||
case IVY_ASM_KW_ATOM:
|
||||
asm_parser_push_state(ctx, ASM_PARSER_ATOM);
|
||||
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),
|
||||
}
|
||||
};
|
||||
86
asm/parse/ident.c
Normal file
86
asm/parse/ident.c
Normal 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),
|
||||
},
|
||||
};
|
||||
0
asm/parse/import.c
Normal file
0
asm/parse/import.c
Normal file
172
asm/parse/parse.c
Normal file
172
asm/parse/parse.c
Normal file
@@ -0,0 +1,172 @@
|
||||
#include <ivy/asm/parse.h>
|
||||
#include <ivy/asm/lex.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "parse.h"
|
||||
|
||||
extern struct parser_state_type unit_parser_state_type;
|
||||
extern struct parser_state_type constpool_parser_state_type;
|
||||
extern struct parser_state_type ident_parser_state_type;
|
||||
extern struct parser_state_type selector_parser_state_type;
|
||||
extern struct parser_state_type atom_parser_state_type;
|
||||
|
||||
static const struct parser_state_type *parser_state_types[] = {
|
||||
[ASM_PARSER_UNIT] = &unit_parser_state_type,
|
||||
[ASM_PARSER_CONSTPOOL] = &constpool_parser_state_type,
|
||||
[ASM_PARSER_IDENT] = &ident_parser_state_type,
|
||||
[ASM_PARSER_SELECTOR] = &selector_parser_state_type,
|
||||
[ASM_PARSER_ATOM] = &atom_parser_state_type,
|
||||
};
|
||||
static const size_t nr_parser_state_types = sizeof parser_state_types / sizeof parser_state_types[0];
|
||||
|
||||
enum ivy_status ivy_asm_parser_create(struct ivy_asm_parser **out)
|
||||
{
|
||||
struct ivy_asm_parser *parser = malloc(sizeof *parser);
|
||||
|
||||
if (!parser) {
|
||||
return IVY_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(parser, 0x0, sizeof *parser);
|
||||
|
||||
asm_parser_push_state(parser, ASM_PARSER_UNIT);
|
||||
|
||||
*out = parser;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
void ivy_asm_parser_destroy(struct ivy_asm_parser *p)
|
||||
{
|
||||
free(p);
|
||||
}
|
||||
|
||||
void ivy_asm_parser_set_assembler(
|
||||
struct ivy_asm_parser *p, struct ivy_assembler *as)
|
||||
{
|
||||
p->p_assembler = as;
|
||||
}
|
||||
|
||||
static token_parse_function get_token_parser(struct parser_state *state, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct parser_state_type *type = state->s_type;
|
||||
if (!type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
token_parse_function specific_parser = NULL;
|
||||
token_parse_function specific_fallback_parser = NULL;
|
||||
token_parse_function token_parser = type->n_token_parsers[__TOK_PARSER_INDEX(tok->t_type)];
|
||||
token_parse_function token_fallback_parser = type->n_token_parsers[__TOK_PARSER_FALLBACK_INDEX];
|
||||
|
||||
switch (tok->t_type) {
|
||||
case IVY_ASM_TOK_SYMBOL:
|
||||
specific_parser = type->n_symbol_parsers[__SYM_PARSER_INDEX(tok->t_symbol)];
|
||||
specific_fallback_parser = type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX];
|
||||
break;
|
||||
case IVY_ASM_TOK_KEYWORD:
|
||||
specific_parser = type->n_keyword_parsers[__KW_PARSER_INDEX(tok->t_keyword)];
|
||||
specific_fallback_parser = type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (specific_parser)
|
||||
return specific_parser;
|
||||
if (specific_fallback_parser)
|
||||
return specific_fallback_parser;
|
||||
if (token_parser)
|
||||
return token_parser;
|
||||
if (token_fallback_parser)
|
||||
return token_fallback_parser;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
enum ivy_status ivy_asm_parser_push_token(
|
||||
struct ivy_asm_parser *p, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct parser_state *state = asm_parser_get_state(p);
|
||||
if (!state) {
|
||||
return IVY_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
token_parse_function f = get_token_parser(state, tok);
|
||||
if (!f) {
|
||||
if (tok->t_type == IVY_ASM_TOK_LINEFEED) {
|
||||
ivy_asm_token_destroy(tok);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
return f(p, tok);
|
||||
}
|
||||
|
||||
static struct parser_state_type *get_parser_state_type(enum parser_state_type_id type)
|
||||
{
|
||||
if (type < 0 || type >= nr_parser_state_types) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return parser_state_types[type];
|
||||
}
|
||||
|
||||
struct parser_state *asm_parser_push_state(struct ivy_asm_parser *parser, enum parser_state_type_id type)
|
||||
{
|
||||
struct parser_state_type *type_info = get_parser_state_type(type);
|
||||
|
||||
if (!type_info) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct parser_state *state = malloc(type_info->n_state_size);
|
||||
|
||||
if (!state) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(state, 0x0, type_info->n_state_size);
|
||||
|
||||
state->s_type = type_info;
|
||||
|
||||
if (type_info->n_init_state) {
|
||||
type_info->n_init_state(parser, state);
|
||||
}
|
||||
|
||||
b_queue_push_back(&parser->p_state, &state->s_entry);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
void asm_parser_pop_state(struct ivy_asm_parser *parser, void *ret)
|
||||
{
|
||||
b_queue_entry *last = b_queue_pop_back(&parser->p_state);
|
||||
if (!last) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct parser_state *state = b_unbox(struct parser_state, last, s_entry);
|
||||
|
||||
if (state->s_type->n_finish_state) {
|
||||
state->s_type->n_finish_state(parser, state);
|
||||
}
|
||||
|
||||
free(state);
|
||||
|
||||
state = asm_parser_get_state(parser);
|
||||
if (state) {
|
||||
state->s_previous_value = ret;
|
||||
}
|
||||
}
|
||||
|
||||
struct parser_state *asm_parser_get_state(struct ivy_asm_parser *parser)
|
||||
{
|
||||
b_queue_entry *last = b_queue_last(&parser->p_state);
|
||||
|
||||
if (!last) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return b_unbox(struct parser_state, last, s_entry);
|
||||
}
|
||||
68
asm/parse/parse.h
Normal file
68
asm/parse/parse.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef _PARSE_PARSE_H_
|
||||
#define _PARSE_PARSE_H_
|
||||
|
||||
#include <ivy/asm/lex.h>
|
||||
#include <blue/core/queue.h>
|
||||
|
||||
#define __TOK_PARSER_INDEX(x) ((x)-__IVY_ASM_TOK_INDEX_BASE)
|
||||
#define __SYM_PARSER_INDEX(x) ((x)-__IVY_ASM_SYM_INDEX_BASE)
|
||||
#define __KW_PARSER_INDEX(x) ((x)-__IVY_ASM_KW_INDEX_BASE)
|
||||
|
||||
#define __TOK_PARSER_FALLBACK_INDEX IVY_ASM_TOK_NONE
|
||||
#define __SYM_PARSER_FALLBACK_INDEX IVY_ASM_SYM_NONE
|
||||
#define __KW_PARSER_FALLBACK_INDEX IVY_ASM_KW_NONE
|
||||
|
||||
#define TOK_PARSER(id, func) [__TOK_PARSER_INDEX(IVY_ASM_TOK_##id)] = func
|
||||
#define SYM_PARSER(id, func) [__SYM_PARSER_INDEX(IVY_ASM_SYM_##id)] = func
|
||||
#define KW_PARSER(id, func) [__KW_PARSER_INDEX(IVY_ASM_KW_##id)] = func
|
||||
|
||||
#define TOK_PARSER_FALLBACK(func) [__TOK_PARSER_FALLBACK_INDEX] = func
|
||||
#define SYM_PARSER_FALLBACK(func) [__SYM_PARSER_FALLBACK_INDEX] = func
|
||||
#define KW_PARSER_FALLBACK(func) [__KW_PARSER_FALLBACK_INDEX] = func
|
||||
|
||||
struct ivy_asm_parser;
|
||||
|
||||
enum parser_state_type_id {
|
||||
ASM_PARSER_NONE = 0,
|
||||
ASM_PARSER_UNIT,
|
||||
ASM_PARSER_CLASS,
|
||||
ASM_PARSER_CONSTPOOL,
|
||||
ASM_PARSER_IMPORT,
|
||||
ASM_PARSER_BLOCK,
|
||||
ASM_PARSER_SELECTOR,
|
||||
ASM_PARSER_IDENT,
|
||||
ASM_PARSER_ATOM,
|
||||
};
|
||||
|
||||
typedef enum ivy_status (*token_parse_function)(
|
||||
struct ivy_asm_parser *, struct ivy_asm_token *);
|
||||
|
||||
struct parser_state_type {
|
||||
size_t n_state_size;
|
||||
|
||||
void (*n_init_state)(struct ivy_asm_parser *, struct parser_state *);
|
||||
void (*n_finish_state)(struct ivy_asm_parser *, struct parser_state *);
|
||||
enum ivy_status (*n_add_child)(
|
||||
struct parser_state *, struct ivy_ast_node *);
|
||||
|
||||
token_parse_function n_token_parsers[__TOK_PARSER_INDEX(__IVY_ASM_TOK_INDEX_LIMIT)];
|
||||
token_parse_function n_keyword_parsers[__KW_PARSER_INDEX(__IVY_ASM_KW_INDEX_LIMIT)];
|
||||
token_parse_function n_symbol_parsers[__SYM_PARSER_INDEX(__IVY_ASM_SYM_INDEX_LIMIT)];
|
||||
};
|
||||
|
||||
struct parser_state {
|
||||
b_queue_entry s_entry;
|
||||
const struct parser_state_type *s_type;
|
||||
void *s_previous_value;
|
||||
};
|
||||
|
||||
struct ivy_asm_parser {
|
||||
struct ivy_assembler *p_assembler;
|
||||
b_queue p_state;
|
||||
};
|
||||
|
||||
extern struct parser_state *asm_parser_push_state(struct ivy_asm_parser *parser, enum parser_state_type_id type);
|
||||
extern void asm_parser_pop_state(struct ivy_asm_parser *parser, void *ret);
|
||||
extern struct parser_state *asm_parser_get_state(struct ivy_asm_parser *parser);
|
||||
|
||||
#endif
|
||||
157
asm/parse/selector.c
Normal file
157
asm/parse/selector.c
Normal file
@@ -0,0 +1,157 @@
|
||||
#include "parse.h"
|
||||
#include <ivy/selector.h>
|
||||
#include <ivy/asm/bin.h>
|
||||
#include <ivy/asm/assembler.h>
|
||||
#include <ivy/asm/lex.h>
|
||||
|
||||
struct selector_parser_state {
|
||||
struct parser_state s_base;
|
||||
unsigned int s_prev_token;
|
||||
|
||||
unsigned int s_paren_depth;
|
||||
struct ivy_asm_token *s_prev_label;
|
||||
struct ivy_selector *s_sel;
|
||||
};
|
||||
|
||||
static enum ivy_status init_state(
|
||||
struct ivy_asm_parser *ctx, struct parser_state *s)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)s;
|
||||
state->s_prev_token = IVY_ASM_SYM_LEFT_PAREN;
|
||||
return ivy_selector_create(&state->s_sel);
|
||||
}
|
||||
|
||||
static enum ivy_status parse_ident(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)asm_parser_get_state(ctx);
|
||||
if (state->s_prev_token == IVY_ASM_TOK_LABEL && state->s_prev_label) {
|
||||
ivy_selector_add_arg(state->s_sel, state->s_prev_label->t_str, tok->t_str);
|
||||
ivy_asm_token_destroy(tok);
|
||||
ivy_asm_token_destroy(state->s_prev_label);
|
||||
state->s_prev_label = tok;
|
||||
state->s_prev_token = IVY_ASM_TOK_LABEL;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN && state->s_prev_token != IVY_ASM_SYM_HYPHEN && state->s_prev_token != IVY_ASM_SYM_PLUS) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
ivy_selector_set_name(state->s_sel, tok->t_str);
|
||||
ivy_asm_token_destroy(tok);
|
||||
state->s_prev_token = IVY_ASM_TOK_IDENT;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_label(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)asm_parser_get_state(ctx);
|
||||
if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN
|
||||
&& state->s_prev_token != IVY_ASM_TOK_LABEL
|
||||
&& state->s_prev_token != IVY_ASM_SYM_HYPHEN
|
||||
&& state->s_prev_token != IVY_ASM_SYM_PLUS) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (state->s_prev_label) {
|
||||
ivy_selector_add_arg(state->s_sel, state->s_prev_label->t_str, NULL);
|
||||
ivy_asm_token_destroy(state->s_prev_label);
|
||||
state->s_prev_label = NULL;
|
||||
}
|
||||
|
||||
state->s_prev_label = tok;
|
||||
state->s_prev_token = IVY_ASM_TOK_LABEL;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_hyphen(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
ivy_selector_set_recipient(state->s_sel, IVY_SEL_OBJECT);
|
||||
state->s_prev_token = IVY_ASM_SYM_HYPHEN;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_plus(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
ivy_selector_set_recipient(state->s_sel, IVY_SEL_CLASS);
|
||||
state->s_prev_token = IVY_ASM_SYM_PLUS;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_left_paren(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
if (state->s_prev_token != IVY_ASM_TOK_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (state->s_paren_depth > 0) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
state->s_paren_depth++;
|
||||
state->s_prev_token = IVY_ASM_SYM_LEFT_PAREN;
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_right_paren(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)asm_parser_get_state(ctx);
|
||||
|
||||
if (state->s_prev_token != IVY_ASM_TOK_IDENT && state->s_prev_token != IVY_ASM_TOK_LABEL && state->s_prev_token != IVY_ASM_SYM_RIGHT_PAREN) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (state->s_prev_label) {
|
||||
ivy_selector_add_arg(state->s_sel, state->s_prev_label->t_str, NULL);
|
||||
ivy_asm_token_destroy(state->s_prev_label);
|
||||
state->s_prev_label = NULL;
|
||||
}
|
||||
|
||||
if (state->s_paren_depth > 0) {
|
||||
state->s_paren_depth--;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
asm_parser_pop_state(ctx, state->s_sel);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
struct parser_state_type selector_parser_state_type = {
|
||||
.n_init_state = init_state,
|
||||
.n_state_size = sizeof(struct selector_parser_state),
|
||||
.n_token_parsers = {
|
||||
TOK_PARSER(IDENT, parse_ident),
|
||||
TOK_PARSER(LABEL, parse_label),
|
||||
},
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER(HYPHEN, parse_hyphen),
|
||||
SYM_PARSER(PLUS, parse_plus),
|
||||
SYM_PARSER(LEFT_PAREN, parse_left_paren),
|
||||
SYM_PARSER(RIGHT_PAREN, parse_right_paren),
|
||||
},
|
||||
};
|
||||
42
asm/parse/unit.c
Normal file
42
asm/parse/unit.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "parse.h"
|
||||
|
||||
struct unit_parser_state {
|
||||
struct parser_state s_base;
|
||||
};
|
||||
|
||||
static enum ivy_status parse_constpool(struct ivy_asm_parser* ctx, struct ivy_asm_token* tok)
|
||||
{
|
||||
asm_parser_push_state(ctx, ASM_PARSER_CONSTPOOL);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_import(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
asm_parser_push_state(ctx, ASM_PARSER_IMPORT);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_block(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
asm_parser_push_state(ctx, ASM_PARSER_BLOCK);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_class(
|
||||
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
||||
{
|
||||
asm_parser_push_state(ctx, ASM_PARSER_CLASS);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
struct parser_state_type unit_parser_state_type = {
|
||||
.n_state_size = sizeof(struct unit_parser_state),
|
||||
.n_keyword_parsers = {
|
||||
KW_PARSER(CONSTPOOL, parse_constpool),
|
||||
KW_PARSER(IMPORT, parse_import),
|
||||
KW_PARSER(CLASS, parse_class),
|
||||
KW_PARSER(BLOCK, parse_block),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user