diff options
author | kdx <kdx@42l.fr> | 2023-03-14 02:13:13 +0100 |
---|---|---|
committer | kdx <kdx@42l.fr> | 2023-03-14 02:13:13 +0100 |
commit | 7b888be4de737b652b3b4ab78229482fcb77bd03 (patch) | |
tree | 1c7a14c881e7613db7478a950e017c2ef3ab09df | |
parent | 915ddb4243856339a4f95493271052523d150633 (diff) | |
download | golem-7b888be4de737b652b3b4ab78229482fcb77bd03.tar.gz |
basic lexing
-rwxr-xr-x | build.sh | 2 | ||||
-rw-r--r-- | lexer.c | 60 | ||||
-rw-r--r-- | main.c | 7 | ||||
-rw-r--r-- | slice.c | 5 | ||||
-rw-r--r-- | token.c | 13 | ||||
-rw-r--r-- | token.h | 1 |
6 files changed, 80 insertions, 8 deletions
@@ -1,2 +1,2 @@ #!/bin/sh -gcc -std=c99 -Wall -Wextra -o golem *.c +gcc -g -O0 -std=c99 -Wall -Wextra -o golem *.c @@ -1,12 +1,70 @@ #include "lexer.h" #include "slice.h" #include "token.h" +#include <ctype.h> +#include <stdio.h> #include <stdlib.h> +static unsigned int +simple_token(char c) +{ + switch (c) { + case '(': return TOK_OPEN_PAREN; + case ')': return TOK_CLOS_PAREN; + case '{': return TOK_OPEN_CURL; + case '}': return TOK_CLOS_CURL; + default: return TOK_NONE; + } +} + +static Slice +slice_number(Slice slice) +{ + for (int i = slice.begin; i < slice.end; i++) + if (!isdigit(slice.str[i])) + return slice_sub(slice, slice.begin, i); + return slice; +} + Token * lexer(Slice slice) { Token *toks = NULL; - token_append(&toks, token_create(slice, TOK_STRING)); + int i = slice.begin; + while (i < slice.end) { + // Skip whitespaces. + if (isspace(slice.str[i])) { + i += 1; + continue; + } + + // Ignore comments. + if (slice.str[i] == '/' && slice.str[i + 1] == '/') { + while (i < slice.end && slice.str[i] != '\n') + i += 1; + continue; + } + + // Single character tokens. + if (simple_token(slice.str[i]) != TOK_NONE) { + token_append(&toks, + token_create(slice_sub(slice, i, i + 1), + simple_token(slice.str[i]))); + i += 1; + continue; + } + + // Number token. + if (isdigit(slice.str[i])) { + Slice number; + number = slice_number(slice_sub(slice, i, slice.end)); + token_append(&toks, token_create(number, TOK_NUMBER)); + i = number.end; + continue; + } + + printf("skipping '%c'\n", slice.str[i]); + i += 1; + } return toks; } @@ -21,11 +21,12 @@ main(int argc, char **argv) Slice slice = slice_from_str(drain(file)); fclose(file); - slice_print(slice); - if (slice.str != NULL) - free(slice.str); Token *tokens = lexer(slice); + token_print(tokens); token_destroy(tokens); + + if (slice.str != NULL) + free(slice.str); return 0; } @@ -6,9 +6,8 @@ Slice slice_print(Slice slice) { putchar('"'); - for (int i = slice.begin; i < slice.end; i++) { + for (int i = slice.begin; i < slice.end; i++) putchar(slice.str[i]); - } putchar('"'); putchar('\n'); return slice; @@ -25,5 +24,5 @@ slice_from_str(char *str) Slice slice_sub(Slice src, int begin, int end) { - return (Slice){ src.str, src.begin + begin, src.begin + end }; + return (Slice){ src.str, begin, end }; } @@ -1,4 +1,5 @@ #include "token.h" +#include "slice.h" #include <stdlib.h> #include <stdio.h> @@ -40,3 +41,15 @@ token_destroy(Token *token) token = next; } } + +Token * +token_print(Token *token) +{ + Token *tok = token; + while (token != NULL) { + printf("type: %2u slice: ", token->type); + slice_print(token->slice); + token = token->next; + } + return tok; +} @@ -23,3 +23,4 @@ struct Token { Token *token_create(Slice slice, unsigned int type); Token *token_append(Token **list, Token *elem); void token_destroy(Token *token); +Token *token_print(Token *token); |