Add initial code for statement parsing.

This commit is contained in:
Danila Fedorin 2018-02-10 23:06:37 -08:00
parent 0958ecd1eb
commit 31329a13a6

View File

@ -32,6 +32,10 @@ int _parser_is_char(struct parser_state* state, char to_expect) {
state->string[state->current_match->from] == to_expect);
}
int _parser_is_type(struct parser_state* state, lexer_token to_expect) {
return (state->current_match && state->current_match->type == to_expect);
}
int _parser_eof(struct parser_state* state) {
return state->current_match == NULL;
}
@ -49,18 +53,43 @@ libab_result _parser_consume_char(struct parser_state* state, char to_consume) {
return result;
}
libab_result _parse_block(struct parser_state*, tree**, int);
libab_result _parse_expression(struct parser_state* state, tree** store_into) {
libab_result result = LIBAB_SUCCESS;
return result;
}
libab_result _parse_statement(struct parser_state* state, tree** store_into) {
libab_result result = LIBAB_SUCCESS;
if(_parser_is_char(state, '{')) result = _parse_block(state, store_into, 1);
else if(_parser_is_type(state, TOKEN_ID) ||
_parser_is_type(state, TOKEN_NUM) ||
_parser_is_type(state, TOKEN_STR) ||
_parser_is_type(state, TOKEN_CHAR_LIT) ||
_parser_is_type(state, TOKEN_TRUE) ||
_parser_is_type(state, TOKEN_FALSE) ||
_parser_is_char(state, '(') ||
_parser_is_type(state, TOKEN_OP_PREFIX)) {
result = _parse_expression(state, store_into);
if(result == LIBAB_SUCCESS) result = _parser_consume_char(state, ';');
}
return result;
}
libab_result _parse_block(struct parser_state* state,
tree** store_into, int expect_braces) {
libab_result result = LIBAB_SUCCESS;
*store_into = malloc(sizeof(**store_into));
if(*store_into == NULL) result = LIBAB_MALLOC;
if((*store_into = malloc(sizeof(**store_into))) == NULL) result = LIBAB_MALLOC;
if(expect_braces && result == LIBAB_SUCCESS) result = _parser_consume_char(state, '{');
while(result == LIBAB_SUCCESS &&
!_parser_eof(state) &&
!(expect_braces && _parser_is_char(state, '}'))) {
}
if(expect_braces && result == LIBAB_SUCCESS) result = _parser_consume_char(state, '}');