2024-09-09 16:41:49 +00:00
|
|
|
/* Copyright (c) 2024 Freya Murphy */
|
|
|
|
|
|
|
|
#ifndef __LEX_H__
|
|
|
|
#define __LEX_H__
|
|
|
|
|
|
|
|
#include <mlimits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
/// represents a non null
|
|
|
|
/// terminated string
|
|
|
|
struct string {
|
|
|
|
char *str;
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t size;
|
|
|
|
bool allocated;
|
2024-09-09 16:41:49 +00:00
|
|
|
};
|
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
/* 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);
|
2024-09-13 15:11:18 +00:00
|
|
|
|
2024-09-09 16:41:49 +00:00
|
|
|
enum token_type {
|
2024-10-04 23:41:10 +00:00
|
|
|
/// has no associated
|
|
|
|
/// data
|
2024-09-09 16:41:49 +00:00
|
|
|
TOK_COMMA,
|
|
|
|
TOK_EQUAL,
|
|
|
|
TOK_LPAREN,
|
|
|
|
TOK_RPAREN,
|
|
|
|
TOK_EOF,
|
|
|
|
TOK_NL,
|
2024-10-04 23:41:10 +00:00
|
|
|
|
|
|
|
/// uses number
|
|
|
|
TOK_NUMBER,
|
|
|
|
|
|
|
|
/// uses string
|
|
|
|
TOK_REG,
|
|
|
|
TOK_IDENT,
|
|
|
|
TOK_LABEL,
|
|
|
|
TOK_STRING,
|
2024-09-09 16:41:49 +00:00
|
|
|
TOK_DIRECTIVE,
|
|
|
|
};
|
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
/// represents a token
|
|
|
|
/// returned from the lexer
|
2024-09-09 16:41:49 +00:00
|
|
|
struct token {
|
2024-10-04 23:41:10 +00:00
|
|
|
/// type
|
2024-09-09 16:41:49 +00:00
|
|
|
enum token_type type;
|
2024-10-04 23:41:10 +00:00
|
|
|
|
|
|
|
/// position
|
|
|
|
int x, y;
|
|
|
|
/// pos in bytes
|
|
|
|
int off;
|
|
|
|
|
|
|
|
/// data
|
2024-09-09 16:41:49 +00:00
|
|
|
union {
|
|
|
|
int64_t number;
|
2024-10-04 23:41:10 +00:00
|
|
|
struct string string;
|
2024-09-09 16:41:49 +00:00
|
|
|
};
|
2024-10-04 23:41:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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;
|
2024-09-09 16:41:49 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* initalize a lexer */
|
|
|
|
int lexer_init(const char *file, struct lexer *lexer);
|
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
/* free the lexer */
|
|
|
|
void lexer_free(struct lexer *lexer);
|
2024-09-09 16:41:49 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2024-09-13 15:11:18 +00:00
|
|
|
/* 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);
|
|
|
|
|
2024-09-09 16:41:49 +00:00
|
|
|
#endif /* __LEX_H__ */
|