Implement the initialization and freeing of lexer.

This commit is contained in:
Danila Fedorin 2018-02-10 14:21:04 -08:00
parent 10652048ab
commit 2db840288d
2 changed files with 78 additions and 0 deletions

View File

@ -17,7 +17,34 @@ struct lexer {
eval_config config; 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 struct lexer lexer;
typedef enum lexer_token lexer_token;
/** /**
* Initializes the given lexer, * Initializes the given lexer,

View File

@ -1 +1,52 @@
#include "lexer.h" #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;
}