Add true and false keywords to the parser.

This commit is contained in:
Danila Fedorin 2018-08-10 16:06:30 -07:00
parent 7c8c547540
commit 3b79b5751a
5 changed files with 49 additions and 3 deletions

View File

@ -60,6 +60,8 @@ enum libab_lexer_token_e {
TOKEN_OP_PREFIX,
TOKEN_OP_POSTFIX,
TOKEN_OP_RESERVED,
TOKEN_KW_TRUE,
TOKEN_KW_FALSE,
TOKEN_KW_IF,
TOKEN_KW_ELSE,
TOKEN_KW_WHILE,

View File

@ -19,6 +19,8 @@ enum libab_tree_variant_e {
TREE_POSTFIX_OP,
TREE_BLOCK,
TREE_VOID,
TREE_TRUE,
TREE_FALSE,
TREE_IF,
TREE_WHILE,
TREE_DOWHILE,

View File

@ -1208,6 +1208,10 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
vec_index(&tree->children, 0),
vec_index(&tree->children, 1),
into);
} else if(tree->variant == TREE_TRUE) {
libab_get_true_value(state->ab, into);
} else if(tree->variant == TREE_FALSE) {
libab_get_false_value(state->ab, into);
} else {
libab_get_unit_value(state->ab, into);
}

View File

@ -10,6 +10,8 @@ libab_result libab_lexer_init(libab_lexer* lexer) {
const char* words[] = {".",
"[a-zA-Z][a-zA-Z0-9_]*",
"[0-9]+(\\.[0-9]*)?",
"true",
"false",
"if",
"else",
"while",
@ -18,9 +20,9 @@ libab_result libab_lexer_init(libab_lexer* lexer) {
"fun",
"return"};
libab_lexer_token tokens[] = {
TOKEN_CHAR, TOKEN_ID, TOKEN_NUM, TOKEN_KW_IF,
TOKEN_KW_ELSE, TOKEN_KW_WHILE, TOKEN_KW_DO, TOKEN_KW_ARROW,
TOKEN_KW_FUN, TOKEN_KW_RETURN };
TOKEN_CHAR, TOKEN_ID, TOKEN_NUM, TOKEN_KW_TRUE,
TOKEN_KW_FALSE, TOKEN_KW_IF, TOKEN_KW_ELSE, TOKEN_KW_WHILE,
TOKEN_KW_DO, TOKEN_KW_ARROW, TOKEN_KW_FUN, TOKEN_KW_RETURN};
const size_t count = sizeof(tokens) / sizeof(libab_lexer_token);
eval_config_init(&lexer->config);

View File

@ -406,6 +406,38 @@ libab_result _parse_void(struct parser_state* state, libab_tree** store_into) {
return result;
}
libab_result _parse_true(struct parser_state* state, libab_tree** store_into) {
libab_result result = _parser_consume_type(state, TOKEN_KW_TRUE);
if(result == LIBAB_SUCCESS) {
if ((*store_into = malloc(sizeof(**store_into)))) {
(*store_into)->variant = TREE_TRUE;
} else {
result = LIBAB_MALLOC;
}
}
if(result != LIBAB_SUCCESS) {
*store_into = NULL;
}
return result;
}
libab_result _parse_false(struct parser_state* state, libab_tree** store_into) {
libab_result result = _parser_consume_type(state, TOKEN_KW_FALSE);
if(result == LIBAB_SUCCESS) {
if ((*store_into = malloc(sizeof(**store_into)))) {
(*store_into)->variant = TREE_FALSE;
} else {
result = LIBAB_MALLOC;
}
}
if(result != LIBAB_SUCCESS) {
*store_into = NULL;
}
return result;
}
libab_result _parse_if(struct parser_state* state, libab_tree** store_into) {
libab_result result = LIBAB_SUCCESS;
libab_tree* condition = NULL;
@ -736,6 +768,10 @@ libab_result _parse_atom(struct parser_state* state, libab_tree** store_into) {
result = _parse_fun(state, store_into);
} else if (_parser_is_type(state, TOKEN_KW_RETURN)) {
result = _parse_return(state, store_into);
} else if (_parser_is_type(state, TOKEN_KW_TRUE)) {
result = _parse_true(state, store_into);
} else if (_parser_is_type(state, TOKEN_KW_FALSE)) {
result = _parse_false(state, store_into);
} else {
result = LIBAB_UNEXPECTED;
}