diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 23 | ||||
-rw-r--r-- | src/parser.c | 39 | ||||
-rw-r--r-- | src/tokenizer.c | 12 |
3 files changed, 53 insertions, 21 deletions
@@ -1,9 +1,7 @@ -#include "../include/tokenizer.h" +#include "../include/parser.h" #include <string.h> -#include <stdio.h> int main() { - TslTokenizer tokenizer; const char *code = "value1 = 1\n" "value2 = true\n" @@ -22,20 +20,5 @@ int main() { "\n" "str = value9[\"hello\"]\n" "value9[\"sayHello\"]()"; - tsl_tokenizer_init(&tokenizer, code, strlen(code)); - - for(;;) { - TslToken token = tsl_tokenizer_next(&tokenizer); - if(token == TSL_TOKEN_END_OF_FILE) { - break; - } else if(token == TSL_TOKEN_IDENTIFIER) { - printf("identifier: %.*s\n", (int)tokenizer.identifier.size, tokenizer.identifier.data); - } else if(token == TSL_TOKEN_NUM) { - printf("num: %ld\n", tokenizer.number_value); - } else { - return 1; - } - } - - return 0; -}
\ No newline at end of file + return tsl_parse(code, strlen(code)); +} diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..35a4abb --- /dev/null +++ b/src/parser.c @@ -0,0 +1,39 @@ +#include "../include/parser.h" +#include <stdio.h> + +typedef struct { + TslTokenizer tokenizer; +} TslParser; + +static void tsl_parser_init(TslParser *self, const char *code, size_t code_size) { + tsl_tokenizer_init(&self->tokenizer, code, code_size); +} + +static int tsl_parser_parse_rhs(TslParser *self) { + return 0; +} + +static int tsl_parser_parse(TslParser *self) { + for(;;) { + TslToken token = tsl_tokenizer_next(&self->tokenizer); + if(token == TSL_TOKEN_IDENTIFIER) { + TslStringView identifier = self->tokenizer.identifier; + if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_EQUAL)) { + return -1; + } + tsl_parser_parse_rhs(self); + } else if(token == TSL_TOKEN_END_OF_FILE) { + break; + } else { + fprintf(stderr, "Error: Expected identifier, got TODO\n"); + return -1; + } + } + return 0; +} + +int tsl_parse(const char *code, size_t code_size) { + TslParser parser; + tsl_parser_init(&parser, code, code_size); + return tsl_parser_parse(&parser); +} diff --git a/src/tokenizer.c b/src/tokenizer.c index 1ab34db..97673f5 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -76,7 +76,8 @@ const int64_t num_multipliers[] = { /* TODO: Check if the number if too large to fit into the result */ static int64_t string_to_int(TslStringView *str) { int64_t num = 0; - for(size_t i = 0; i < str->size; ++i) { + size_t i = 0; + for(; i < str->size; ++i) { char digit = str->data[str->size - 1 - i] - '0'; num += digit * num_multipliers[i]; } @@ -146,3 +147,12 @@ TslToken tsl_tokenizer_next(TslTokenizer *self) { return TSL_TOKEN_UNEXPECTED_SYMBOL; } } + +int tsl_tokenizer_accept(TslTokenizer *self, TslToken expected_token) { + TslToken actual_token = tsl_tokenizer_next(self); + if(actual_token != expected_token) { + fprintf(stderr, "Error: Expected TODO, got TODO\n"); + return 0; + } + return 1; +} |