2024-09-09 16:41:49 +00:00
|
|
|
/* 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;
|
|
|
|
};
|
|
|
|
|
2024-09-09 16:41:49 +00:00
|
|
|
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];
|
|
|
|
};
|
|
|
|
|
2024-09-09 16:41:49 +00:00
|
|
|
enum expr_type {
|
|
|
|
EXPR_DIRECTIVE,
|
|
|
|
EXPR_CONSTANT,
|
2024-09-11 16:06:09 +00:00
|
|
|
EXPR_INS,
|
2024-09-09 16:41:49 +00:00
|
|
|
EXPR_LABEL,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct expr {
|
|
|
|
enum expr_type type;
|
|
|
|
union {
|
|
|
|
// directive
|
2024-09-11 16:06:09 +00:00
|
|
|
struct mips_directive directive;
|
2024-09-09 16:41:49 +00:00
|
|
|
// 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
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-09-09 16:41:49 +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__ */
|