diff --git a/include/lexer.h b/include/lexer.h index b51386f..61c5f10 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -65,6 +65,8 @@ enum libab_lexer_token_e { TOKEN_KW_ARROW, TOKEN_KW_FUN, TOKEN_KW_RETURN, + TOKEN_KW_LET, + TOKEN_KW_BE, TOKEN_LAST }; diff --git a/src/lexer.c b/src/lexer.c index 1113d03..48e9252 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -17,7 +17,9 @@ libab_result libab_lexer_init(libab_lexer* lexer) { "do", "->", "fun", - "return" + "return", + "let", + "be" }; libab_lexer_token tokens[] = { TOKEN_CHAR, @@ -29,7 +31,9 @@ libab_result libab_lexer_init(libab_lexer* lexer) { TOKEN_KW_DO, TOKEN_KW_ARROW, TOKEN_KW_FUN, - TOKEN_KW_RETURN + TOKEN_KW_RETURN, + TOKEN_KW_LET, + TOKEN_KW_BE }; const size_t count = sizeof(tokens)/sizeof(libab_lexer_token); diff --git a/src/parser.c b/src/parser.c index b1600f4..39b2fab 100644 --- a/src/parser.c +++ b/src/parser.c @@ -411,6 +411,47 @@ libab_result _parse_fun_param(struct parser_state* state, libab_tree** store_int return result; } +libab_result _parse_def_fun(struct parser_state* state, libab_tree** store_into) { + libab_result result = LIBAB_SUCCESS; + libab_tree* temp; + if(_parser_is_type(state, TOKEN_KW_LET)) { + _parser_state_step(state); + if(!_parser_eof(state)) { + result = _parser_construct_node_both(state, state->current_match, store_into); + } else { + result = LIBAB_UNEXPECTED; + } + } else { + result = LIBAB_UNEXPECTED; + } + + if(result == LIBAB_SUCCESS) { + _parser_state_step(state); + (*store_into)->parse_type = NULL; + (*store_into)->variant = TREE_FUN; + result = _parser_consume_char(state, ':'); + } + + if(result == LIBAB_SUCCESS) { + result = _parse_type(state, &(*store_into)->parse_type); + } + + if(result == LIBAB_SUCCESS) { + result = _parser_consume_type(state, TOKEN_KW_BE); + } + + if(result == LIBAB_SUCCESS) { + PARSE_CHILD(result, state, _parse_expression, temp, &(*store_into)->children); + } + + if(result != LIBAB_SUCCESS && *store_into) { + libab_tree_free_recursive(*store_into); + *store_into = NULL; + } + + return result; +} + libab_result _parse_fun(struct parser_state* state, libab_tree** store_into) { libab_result result = LIBAB_SUCCESS; int is_parenth, is_comma; @@ -635,6 +676,8 @@ libab_result _parse_atom(struct parser_state* state, libab_tree** store_into) { result = _parse_braced_block(state, store_into); } else if(_parser_is_type(state, TOKEN_KW_FUN)) { result = _parse_fun(state, store_into); + } else if(_parser_is_type(state, TOKEN_KW_LET)) { + result = _parse_def_fun(state, store_into); } else if(_parser_is_type(state, TOKEN_KW_RETURN)) { result = _parse_return(state, store_into); } else {