From f5fc07765303055922e1dfcc35f2f2f620c1e66b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 14 Jan 2020 07:54:59 +0100 Subject: add parser... --- Makefile | 10 +++++++--- include/parser.h | 8 ++++++++ include/tokenizer.h | 1 + src/main.c | 23 +++-------------------- src/parser.c | 39 +++++++++++++++++++++++++++++++++++++++ src/tokenizer.c | 12 +++++++++++- 6 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 include/parser.h create mode 100644 src/parser.c diff --git a/Makefile b/Makefile index 9176922..8b6cb84 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,14 @@ -CFLAGS=-Wall -Wextra -g3 +CFLAGS = -Wall -Wextra -g3 -ansi -pedantic +OBJ = main.o tokenizer.o parser.o -all: main.o tokenizer.o - cc -o tsl main.o tokenizer.o -fPIE +all: $(OBJ) + cc -o tsl $(OBJ) -fPIE main.o: src/main.c include/tokenizer.h cc -c src/main.c $(CFLAGS) tokenizer.o: src/tokenizer.c include/tokenizer.h cc -c src/tokenizer.c $(CFLAGS) + +parser.o: src/parser.c include/parser.h + cc -c src/parser.c $(CFLAGS) diff --git a/include/parser.h b/include/parser.h new file mode 100644 index 0000000..3523127 --- /dev/null +++ b/include/parser.h @@ -0,0 +1,8 @@ +#ifndef TSL_PARSER_H +#define TSL_PARSER_H + +#include "tokenizer.h" + +int tsl_parse(const char *code, size_t code_size); + +#endif /* TSL_PARSER_H */ diff --git a/include/tokenizer.h b/include/tokenizer.h index decdae4..a1d0932 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -32,6 +32,7 @@ typedef struct { void tsl_tokenizer_init(TslTokenizer *self, const char *code, size_t code_size); TslToken tsl_tokenizer_next(TslTokenizer *self); +int tsl_tokenizer_accept(TslTokenizer *self, TslToken expected_token); #endif /* TSL_TOKENIZER_H */ 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