Add support for the return keyword.

This commit is contained in:
Danila Fedorin 2018-03-08 21:31:24 -08:00
parent f3f1cc8c43
commit bb61dbcd54
6 changed files with 38 additions and 5 deletions

View File

@ -64,6 +64,7 @@ enum libab_lexer_token_e {
TOKEN_KW_DO, TOKEN_KW_DO,
TOKEN_KW_ARROW, TOKEN_KW_ARROW,
TOKEN_KW_FUN, TOKEN_KW_FUN,
TOKEN_KW_RETURN,
TOKEN_LAST TOKEN_LAST
}; };

View File

@ -22,7 +22,8 @@ enum libab_tree_variant_e {
TREE_DOWHILE, TREE_DOWHILE,
TREE_CALL, TREE_CALL,
TREE_FUN, TREE_FUN,
TREE_FUN_PARAM TREE_FUN_PARAM,
TREE_RETURN
}; };
/** /**

View File

@ -17,7 +17,8 @@ const char* _debug_node_name(libab_tree_variant var) {
"dowhile", "dowhile",
"call", "call",
"fun", "fun",
"fun_param" "fun_param",
"return"
}; };
return names[var]; return names[var];
} }

View File

@ -16,7 +16,8 @@ libab_result libab_lexer_init(libab_lexer* lexer) {
"while", "while",
"do", "do",
"->", "->",
"fun" "fun",
"return"
}; };
libab_lexer_token tokens[] = { libab_lexer_token tokens[] = {
TOKEN_CHAR, TOKEN_CHAR,
@ -27,7 +28,8 @@ libab_result libab_lexer_init(libab_lexer* lexer) {
TOKEN_KW_WHILE, TOKEN_KW_WHILE,
TOKEN_KW_DO, TOKEN_KW_DO,
TOKEN_KW_ARROW, TOKEN_KW_ARROW,
TOKEN_KW_FUN TOKEN_KW_FUN,
TOKEN_KW_RETURN
}; };
const size_t count = sizeof(tokens)/sizeof(libab_lexer_token); const size_t count = sizeof(tokens)/sizeof(libab_lexer_token);

View File

@ -467,6 +467,31 @@ libab_result _parse_fun(struct parser_state* state, libab_tree** store_into) {
return result; return result;
} }
libab_result _parse_return(struct parser_state* state, libab_tree** store_into) {
libab_result result = LIBAB_SUCCESS;
libab_tree* child = NULL;
if(_parser_is_type(state, TOKEN_KW_RETURN)) {
result = _parser_construct_node_vec(state->current_match, store_into);
if(result == LIBAB_SUCCESS) {
(*store_into)->variant = TREE_RETURN;
_parser_state_step(state);
}
} else {
result = LIBAB_UNEXPECTED;
}
if(result == LIBAB_SUCCESS) {
PARSE_CHILD(result, state, _parse_expression, child, &(*store_into)->children);
}
if(result != LIBAB_SUCCESS) {
if(*store_into) libab_tree_free_recursive(*store_into);
*store_into = NULL;
}
return result;
}
libab_result _parse_while(struct parser_state* state, libab_tree** store_into) { libab_result _parse_while(struct parser_state* state, libab_tree** store_into) {
libab_result result = LIBAB_SUCCESS; libab_result result = LIBAB_SUCCESS;
libab_tree* condition = NULL; libab_tree* condition = NULL;
@ -610,6 +635,8 @@ libab_result _parse_atom(struct parser_state* state, libab_tree** store_into) {
result = _parse_braced_block(state, store_into); result = _parse_braced_block(state, store_into);
} else if(_parser_is_type(state, TOKEN_KW_FUN)) { } else if(_parser_is_type(state, TOKEN_KW_FUN)) {
result = _parse_fun(state, store_into); result = _parse_fun(state, store_into);
} else if(_parser_is_type(state, TOKEN_KW_RETURN)) {
result = _parse_return(state, store_into);
} else { } else {
result = LIBAB_UNEXPECTED; result = LIBAB_UNEXPECTED;
} }

View File

@ -5,7 +5,8 @@ int libab_tree_has_vector(libab_tree_variant variant) {
return variant == TREE_BASE || variant == TREE_OP || return variant == TREE_BASE || variant == TREE_OP ||
variant == TREE_UNARY_OP || variant == TREE_BLOCK || variant == TREE_UNARY_OP || variant == TREE_BLOCK ||
variant == TREE_IF || variant == TREE_CALL || variant == TREE_WHILE || variant == TREE_IF || variant == TREE_CALL || variant == TREE_WHILE ||
variant == TREE_DOWHILE || variant == TREE_FUN; variant == TREE_DOWHILE || variant == TREE_FUN ||
variant == TREE_RETURN;
} }
int libab_tree_has_string(libab_tree_variant variant) { int libab_tree_has_string(libab_tree_variant variant) {