diff --git a/include/lexer.h b/include/lexer.h index 9a57655..764995f 100644 --- a/include/lexer.h +++ b/include/lexer.h @@ -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, diff --git a/include/tree.h b/include/tree.h index d6b1870..223dc8b 100644 --- a/include/tree.h +++ b/include/tree.h @@ -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, diff --git a/src/interpreter.c b/src/interpreter.c index 499a0ee..08a0292 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -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); } diff --git a/src/lexer.c b/src/lexer.c index 7ad154a..4f9a53f 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -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); diff --git a/src/parser.c b/src/parser.c index 28271c1..736aa32 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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; }