From f5fc07765303055922e1dfcc35f2f2f620c1e66b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 14 Jan 2020 07:54:59 +0100 Subject: add parser... --- src/main.c | 23 +++-------------------- src/parser.c | 39 +++++++++++++++++++++++++++++++++++++++ src/tokenizer.c | 12 +++++++++++- 3 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 src/parser.c (limited to 'src') diff --git a/src/main.c b/src/main.c index 6653ba2..04932d3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,7 @@ -#include "../include/tokenizer.h" +#include "../include/parser.h" #include -#include 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 + +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; +} -- cgit v1.2.3