From 2db840288d43ed44481306b203bb7d27325a3a56 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 10 Feb 2018 14:21:04 -0800 Subject: [PATCH] Implement the initialization and freeing of lexer. --- include/lexer.h | 27 ++++++++++++++++++++++++++ src/lexer.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/include/lexer.h b/include/lexer.h index f816ce7..702ba5e 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -17,7 +17,34 @@ struct lexer { eval_config config; }; +/** + * The various tokens used by the lexer + * in order to tag meaningful sequences + * of characters. + */ +enum lexer_token { + TOKEN_CHAR = 0, + TOKEN_ID, + TOKEN_TRUE, + TOKEN_FALSE, + TOKEN_NUM, + TOKEN_STR, + TOKEN_CHAR_LIT, + TOKEN_KW_FUN, + TOKEN_KW_IF, + TOKEN_KW_ELSE, + TOKEN_KW_WHILE, + TOKEN_KW_DO, + TOKEN_KW_FOR, + TOKEN_KW_RETURN, + TOKEN_OP_INFIX, + TOKEN_OP_POSTFIX, + TOKEN_OP_PREFIX, + TOKEN_LAST +}; + typedef struct lexer lexer; +typedef enum lexer_token lexer_token; /** * Initializes the given lexer, diff --git a/src/lexer.c b/src/lexer.c index 723ab1d..26765f2 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1 +1,52 @@ #include "lexer.h" + +libab_result lexer_init(lexer* lexer) { + size_t i; + libab_result result = LIBAB_SUCCESS; + const char* words[] = { + ".", + "[a-zA-Z][a-zA-Z0-9_]*", + "true", + "false", + "[0-9]+(\\.[0-9]*)", + "\"[^\"]*\"", + "'[^']'", + "fun", + "if", + "else", + "while", + "do", + "for", + "return" + }; + lexer_token tokens[] = { + TOKEN_CHAR, + TOKEN_ID, + TOKEN_TRUE, + TOKEN_FALSE, + TOKEN_NUM, + TOKEN_STR, + TOKEN_CHAR_LIT, + TOKEN_KW_FUN, + TOKEN_KW_IF, + TOKEN_KW_ELSE, + TOKEN_KW_WHILE, + TOKEN_KW_DO, + TOKEN_KW_FOR, + TOKEN_KW_RETURN + }; + const size_t count = sizeof(tokens)/sizeof(lexer_token); + + eval_config_init(&lexer->config); + for(i = 0; i < count && result == LIBAB_SUCCESS; i++) { + result = + (eval_config_add(&lexer->config, words[i], tokens[i]) == LIBLEX_SUCCESS) + ? LIBAB_SUCCESS : LIBAB_MALLOC; + } + + return result; +} +libab_result lexer_free(lexer* lexer) { + return (eval_config_free(&lexer->config) == LIBLEX_SUCCESS) ? + LIBAB_SUCCESS : LIBAB_MALLOC; +}