116 lines
2.1 KiB
C
116 lines
2.1 KiB
C
/* Copyright (c) 2024 Freya Murphy */
|
|
|
|
#ifndef __LEX_H__
|
|
#define __LEX_H__
|
|
|
|
#include <mlimits.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
|
|
/// represents a non null
|
|
/// terminated string
|
|
struct string {
|
|
char *str;
|
|
uint32_t len;
|
|
uint32_t size;
|
|
bool allocated;
|
|
};
|
|
|
|
/* initalize a string */
|
|
void string_init(struct string *string);
|
|
/* free a string */
|
|
void string_free(struct string *string);
|
|
/* clone a string, leave the old one */
|
|
int string_clone(struct string *dst, const struct string *const src);
|
|
/* move a string, delete the old one */
|
|
void string_move(struct string *dst, struct string *src);
|
|
/* pushes a char onto a string */
|
|
int string_push(struct string *string, char c);
|
|
/* load a string from the bss (not allocated) */
|
|
void string_bss(struct string *string, char *src);
|
|
|
|
enum token_type {
|
|
/// has no associated
|
|
/// data
|
|
TOK_COMMA,
|
|
TOK_EQUAL,
|
|
TOK_LPAREN,
|
|
TOK_RPAREN,
|
|
TOK_EOF,
|
|
TOK_NL,
|
|
|
|
/// uses number
|
|
TOK_NUMBER,
|
|
|
|
/// uses string
|
|
TOK_REG,
|
|
TOK_IDENT,
|
|
TOK_LABEL,
|
|
TOK_STRING,
|
|
TOK_DIRECTIVE,
|
|
};
|
|
|
|
/// represents a token
|
|
/// returned from the lexer
|
|
struct token {
|
|
/// type
|
|
enum token_type type;
|
|
|
|
/// position
|
|
int x, y;
|
|
/// pos in bytes
|
|
int off;
|
|
|
|
/// data
|
|
union {
|
|
int64_t number;
|
|
struct string string;
|
|
};
|
|
};
|
|
|
|
/* frees a token*/
|
|
void token_free(struct token *token);
|
|
|
|
/// holds the data
|
|
/// for the current lexer
|
|
struct lexer {
|
|
// the currently
|
|
// open file
|
|
FILE *file;
|
|
|
|
// the last character peeked
|
|
int peek;
|
|
|
|
// the current position
|
|
int x, y;
|
|
};
|
|
|
|
/// holds a previous state of a
|
|
/// lexer, which allows rebounding
|
|
struct lexer_state {
|
|
long offset;
|
|
int peek;
|
|
int x;
|
|
int y;
|
|
};
|
|
|
|
/* initalize a lexer */
|
|
int lexer_init(const char *file, struct lexer *lexer);
|
|
|
|
/* free the lexer */
|
|
void lexer_free(struct lexer *lexer);
|
|
|
|
/* lexes the next token, returns M_ERROR on error,
|
|
* and TOK_EOF on EOF */
|
|
int lexer_next(struct lexer *lexer, struct token *token);
|
|
|
|
/* token type to string */
|
|
char *token_str(enum token_type);
|
|
|
|
/* save the state of a lexer */
|
|
void lexer_save(struct lexer *lexer, struct lexer_state *state);
|
|
|
|
/* load a different state into a lexer */
|
|
void lexer_load(struct lexer *lexer, const struct lexer_state *state);
|
|
|
|
#endif /* __LEX_H__ */
|