diff options
author | Freya Murphy <freya@freyacat.org> | 2024-10-04 19:41:10 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-10-04 19:41:10 -0400 |
commit | 1c11a13ff33873bcc79d4597d31cd252d5c6c1ae (patch) | |
tree | a4321b97f5ad69d1a9b9d06dd629a4dc532758b0 /masm/lex.h | |
parent | update msim usage (diff) | |
download | mips-1c11a13ff33873bcc79d4597d31cd252d5c6c1ae.tar.gz mips-1c11a13ff33873bcc79d4597d31cd252d5c6c1ae.tar.bz2 mips-1c11a13ff33873bcc79d4597d31cd252d5c6c1ae.zip |
refactor masm to add codegen step
Diffstat (limited to 'masm/lex.h')
-rw-r--r-- | masm/lex.h | 86 |
1 files changed, 67 insertions, 19 deletions
@@ -7,41 +7,89 @@ #include <stdio.h> #include <stdint.h> -struct lexer { - FILE *file; - int peek; - int x; - int y; +/// represents a non null +/// terminated string +struct string { + char *str; + uint32_t len; + uint32_t size; + bool allocated; }; -struct lexer_state { - long offset; - int peek; - int x; - int y; -}; +/* 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 { - TOK_IDENT, - TOK_REG, - TOK_LABEL, - TOK_STRING, + /// has no associated + /// data TOK_COMMA, TOK_EQUAL, TOK_LPAREN, TOK_RPAREN, - TOK_NUMBER, 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; - char text[MAX_LEX_LENGTH]; + 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; }; @@ -49,8 +97,8 @@ struct token { /* initalize a lexer */ int lexer_init(const char *file, struct lexer *lexer); -/* free the lxer */ -int lexer_free(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 */ |