Register reserved operators as tokens.

This commit is contained in:
Danila Fedorin 2018-03-16 23:09:11 -07:00
parent d25c1a379a
commit 27acfb0be7
5 changed files with 55 additions and 3 deletions

View File

@ -58,6 +58,7 @@ enum libab_lexer_token_e {
TOKEN_OP_INFIX, TOKEN_OP_INFIX,
TOKEN_OP_PREFIX, TOKEN_OP_PREFIX,
TOKEN_OP_POSTFIX, TOKEN_OP_POSTFIX,
TOKEN_OP_RESERVED,
TOKEN_KW_IF, TOKEN_KW_IF,
TOKEN_KW_ELSE, TOKEN_KW_ELSE,
TOKEN_KW_WHILE, TOKEN_KW_WHILE,

View File

@ -37,5 +37,17 @@ typedef struct libab_reserved_operator_s libab_reserved_operator;
* @return the reserved operator, if it is found. * @return the reserved operator, if it is found.
*/ */
const libab_reserved_operator* libab_find_reserved_operator(const char* name); const libab_reserved_operator* libab_find_reserved_operator(const char* name);
/**
* Registers the existing reserved operators into the given lexer.
* @param lexer the lexer to register into.
* @return the result of the registration.
*/
libab_result libab_register_reserved_operators(libab_lexer* lexer);
/**
* Remove the existing reserved operators from the given lexer.
* @param lexer the lexer to remove from.
* @return the result of the removal.
*/
libab_result libab_remove_reserved_operators(libab_lexer* lexer);
#endif #endif

View File

@ -43,6 +43,10 @@ libab_result libab_lexer_init(libab_lexer* lexer) {
eval_config_add(&lexer->config, words[i], tokens[i])); eval_config_add(&lexer->config, words[i], tokens[i]));
} }
if(result != LIBAB_SUCCESS) {
eval_config_free(&lexer->config);
}
return result; return result;
} }

View File

@ -1,11 +1,25 @@
#include "libabacus.h" #include "libabacus.h"
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
#include "reserved.h"
libab_result libab_init(libab* ab) { libab_result libab_init(libab* ab) {
libab_result result;
libab_table_init(&ab->table); libab_table_init(&ab->table);
libab_parser_init(&ab->parser, &ab->table); libab_parser_init(&ab->parser, &ab->table);
return libab_lexer_init(&ab->lexer); result = libab_lexer_init(&ab->lexer);
if(result == LIBAB_SUCCESS) {
result = libab_register_reserved_operators(&ab->lexer);
}
if(result != LIBAB_SUCCESS) {
libab_table_free(&ab->table);
libab_parser_free(&ab->parser);
libab_lexer_free(&ab->lexer);
}
return result;
} }
void _sanitize(char* to, const char* from, size_t buffer_size) { void _sanitize(char* to, const char* from, size_t buffer_size) {

View File

@ -1,5 +1,6 @@
#include "reserved.h" #include "reserved.h"
#include "string.h" #include "string.h"
#include "util.h"
static const libab_reserved_operator libab_reserved_operators[] = { static const libab_reserved_operator libab_reserved_operators[] = {
{ {
@ -8,10 +9,10 @@ static const libab_reserved_operator libab_reserved_operators[] = {
1 /* Right associative, a = b = 6 should be a = (b = 6) */ 1 /* Right associative, a = b = 6 should be a = (b = 6) */
} }
}; };
static const size_t element_count =
sizeof(libab_reserved_operators) / sizeof(libab_reserved_operator);
const libab_reserved_operator* libab_find_reserved_operator(const char* name) { const libab_reserved_operator* libab_find_reserved_operator(const char* name) {
static const size_t element_count =
sizeof(libab_reserved_operators) / sizeof(libab_reserved_operator);
size_t i; size_t i;
for(i = 0; i < element_count; i++) { for(i = 0; i < element_count; i++) {
if(strcmp(name, libab_reserved_operators[i].op) == 0) if(strcmp(name, libab_reserved_operators[i].op) == 0)
@ -19,3 +20,23 @@ const libab_reserved_operator* libab_find_reserved_operator(const char* name) {
} }
return NULL; return NULL;
} }
libab_result libab_register_reserved_operators(libab_lexer* lexer) {
libab_result result = LIBAB_SUCCESS;
size_t i;
for(i = 0; i < element_count && result == LIBAB_SUCCESS; i++) {
result = libab_convert_lex_result(eval_config_add(&lexer->config,
libab_reserved_operators[i].op, TOKEN_OP_RESERVED));
}
return result;
}
libab_result libab_remove_reserved_operators(libab_lexer* lexer) {
libab_result result = LIBAB_SUCCESS;
size_t i;
for(i = 0; i < element_count && result == LIBAB_SUCCESS; i++) {
result = libab_convert_lex_result(eval_config_remove(&lexer->config,
libab_reserved_operators[i].op, TOKEN_OP_RESERVED));
}
return result;
}