mips/masm/parse.h

84 lines
1.3 KiB
C
Raw Normal View History

/* Copyright (c) 2024 Freya Murphy */
#ifndef __PARSE_H__
#define __PARSE_H__
#include "lex.h"
#include <mlimits.h>
#include <mips.h>
#include <stdint.h>
2024-09-11 16:06:09 +00:00
///
/// reference
///
enum reference_type {
REF_NONE,
REF_OFFESET,
REF_TARGET,
};
struct reference {
enum reference_type type;
/// symbol name
char name[MAX_LEX_LENGTH];
/// integer addend
int64_t addend;
};
struct const_expr {
char name[MAX_LEX_LENGTH];
uint32_t value;
};
2024-09-11 16:06:09 +00:00
struct ins_expr {
/// pesudo instructions can return
/// more than one instruction
size_t ins_len;
struct mips_instruction ins[2];
/// instructions can reference symbols.
/// instruction `n` will be paried with reference `n`
struct reference ref[2];
};
enum expr_type {
EXPR_DIRECTIVE,
EXPR_CONSTANT,
2024-09-11 16:06:09 +00:00
EXPR_INS,
EXPR_LABEL,
};
struct expr {
enum expr_type type;
union {
// directive
2024-09-11 16:06:09 +00:00
struct mips_directive directive;
// constant
struct const_expr constant;
2024-09-11 16:06:09 +00:00
// instruction
struct ins_expr ins;
// label
char label[MAX_LEX_LENGTH];
2024-09-10 00:48:08 +00:00
};
};
struct parser {
struct lexer *lexer;
struct token peek;
};
/* get the next expression in the parser */
int parser_next(struct parser *parser, struct expr *expr);
/* initalize the base parser */
int parser_init(struct lexer *lexer, struct parser *parser);
/* free the base parser */
void parser_free(struct parser *parser);
#endif /* __PARSE_H__ */