diff options
| author | Johnny Richard <johnny@johnnyrichard.com> | 2025-04-14 23:22:57 +0200 |
|---|---|---|
| committer | Johnny Richard <johnny@johnnyrichard.com> | 2025-04-14 23:22:57 +0200 |
| commit | b3bd068f614a46580ee3e5688dd9cfd40694d75b (patch) | |
| tree | 45183f99a8373527caa03ff473c4e40fe07cd459 /src/lexer.c | |
| parent | 63104d34e1c1772131f6366f825e67f38d027dba (diff) | |
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src/lexer.c')
| -rw-r--r-- | src/lexer.c | 84 |
1 files changed, 37 insertions, 47 deletions
diff --git a/src/lexer.c b/src/lexer.c index 19265c8..9258ec3 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -14,40 +14,40 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include "lexer.h" +#include "array.h" +#include "string_view.h" +#include "utils.h" #include <assert.h> +#include <ctype.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <stdbool.h> -#include <ctype.h> -#include "utils.h" -#include "array.h" -#include "string_view.h" -#include "lexer.h" void -lexer_init(lexer_t *lexer, char *file_name) +lexer_init(lexer_t* lexer, char* file_name) { assert(lexer); - char *program = read_file_contents(file_name); + char* program = read_file_contents(file_name); if (program == NULL) { fprintf(stderr, "Unable to read file <%s>\n", file_name); exit(EXIT_FAILURE); } lexer->file_name = file_name; - lexer->loc = (lex_loc_t) { 0 }; + lexer->loc = (lex_loc_t){ 0 }; lexer->source = string_view_from_cstr(program); } bool -lexer_is_eof(lexer_t *lexer) +lexer_is_eof(lexer_t* lexer) { return !(lexer->loc.offset < lexer->source.size); } char -lexer_current_char(lexer_t *lexer) +lexer_current_char(lexer_t* lexer) { return lexer->source.chars[lexer->loc.offset]; } @@ -59,7 +59,7 @@ _isspace(char c) } char -lexer_next_char(lexer_t *lexer) +lexer_next_char(lexer_t* lexer) { assert(lexer->loc.offset < lexer->source.size); char previous_char = lexer_current_char(lexer); @@ -73,10 +73,10 @@ lexer_next_char(lexer_t *lexer) } void -lexer_next_token(lexer_t *lexer, token_t *token) +lexer_next_token(lexer_t* lexer, token_t* token) { if (lexer_is_eof(lexer)) { - *token = (token_t) { .kind = TOKEN_EOF }; + *token = (token_t){ .kind = TOKEN_EOF }; return; } @@ -88,14 +88,16 @@ lexer_next_token(lexer_t *lexer, token_t *token) } if (lexer_is_eof(lexer)) { - *token = (token_t) { .kind = TOKEN_EOF }; + *token = (token_t){ .kind = TOKEN_EOF }; return; } if (c == '\n') { token->kind = TOKEN_EOS; token->loc = lexer->loc; - token->value = (string_view_t) { .size = 1, .chars = lexer->source.chars + lexer->loc.offset }; + token->value = + (string_view_t){ .size = 1, + .chars = lexer->source.chars + lexer->loc.offset }; lexer_next_char(lexer); return; } @@ -223,46 +225,34 @@ lexer_next_token(lexer_t *lexer, token_t *token) if (c == ':') { token->kind = TOKEN_COLON; - token->value = (string_view_t) { .size = 1, .chars = lexer->source.chars + lexer->loc.offset}; + token->value = + (string_view_t){ .size = 1, + .chars = lexer->source.chars + lexer->loc.offset }; token->loc = lexer->loc; lexer_next_char(lexer); return; } } -static char *token_to_cstr_table[] = { - [TOKEN_KW_PUSH] = "push", - [TOKEN_KW_DUP] = "dup", - [TOKEN_KW_COPY] = "copy", - [TOKEN_KW_SWAP] = "swap", - [TOKEN_KW_DROP] = "drop", - [TOKEN_KW_SLIDE] = "slide", - [TOKEN_KW_ADD] = "add", - [TOKEN_KW_SUB] = "sub", - [TOKEN_KW_MUL] = "mul", - [TOKEN_KW_DIV] = "div", - [TOKEN_KW_MOD] = "mod", - [TOKEN_KW_STORE] = "store", - [TOKEN_KW_LOAD] = "load", - [TOKEN_KW_CALL] = "call", - [TOKEN_KW_RET] = "ret", - [TOKEN_KW_JMP] = "jmp", - [TOKEN_KW_JMPZ] = "jz", - [TOKEN_KW_JMPN] = "jn", - [TOKEN_KW_PRINTI] = "printi", - [TOKEN_KW_PRINTC] = "printc", - [TOKEN_KW_READI] = "readi", - [TOKEN_KW_READC] = "readc", - [TOKEN_KW_END] = "end", - [TOKEN_IDENT] = "identifier", - [TOKEN_EOS] = "<eos>", - [TOKEN_NUMBER] = "number", - [TOKEN_COLON] = ":", - [TOKEN_UNKOWN] = "<unkown>", +static char* token_to_cstr_table[] = { + [TOKEN_KW_PUSH] = "push", [TOKEN_KW_DUP] = "dup", + [TOKEN_KW_COPY] = "copy", [TOKEN_KW_SWAP] = "swap", + [TOKEN_KW_DROP] = "drop", [TOKEN_KW_SLIDE] = "slide", + [TOKEN_KW_ADD] = "add", [TOKEN_KW_SUB] = "sub", + [TOKEN_KW_MUL] = "mul", [TOKEN_KW_DIV] = "div", + [TOKEN_KW_MOD] = "mod", [TOKEN_KW_STORE] = "store", + [TOKEN_KW_LOAD] = "load", [TOKEN_KW_CALL] = "call", + [TOKEN_KW_RET] = "ret", [TOKEN_KW_JMP] = "jmp", + [TOKEN_KW_JMPZ] = "jz", [TOKEN_KW_JMPN] = "jn", + [TOKEN_KW_PRINTI] = "printi", [TOKEN_KW_PRINTC] = "printc", + [TOKEN_KW_READI] = "readi", [TOKEN_KW_READC] = "readc", + [TOKEN_KW_END] = "end", [TOKEN_IDENT] = "identifier", + [TOKEN_EOS] = "<eos>", [TOKEN_NUMBER] = "number", + [TOKEN_COLON] = ":", [TOKEN_UNKOWN] = "<unkown>", [TOKEN_EOF] = "<eof>", }; -char * +char* token_to_cstr(token_kind_t kind) { return token_to_cstr_table[kind]; |
