From dfcfac60588063fc576c18e05156be84ab7110f5 Mon Sep 17 00:00:00 2001 From: kumachan-mis <29433058+kumachan-mis@users.noreply.github.com> Date: Sun, 8 Oct 2023 08:24:29 +0000 Subject: [PATCH] implement lexer --- src/ctoken/ctoken.c | 4 ++-- src/ctoken/ctoken.h | 1 + src/lexer/util.c | 1 + tests/lexer/test_lexer.c | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/ctoken/ctoken.c b/src/ctoken/ctoken.c index cb04bd37..d34ff156 100644 --- a/src/ctoken/ctoken.c +++ b/src/ctoken/ctoken.c @@ -9,8 +9,8 @@ BaseType t_ctoken = { char* ctoken_types[] = { // non-punctuators - "char", "else", "enum", "if", "int", "long", "return", "sizeof", "struct", "typedef", "unsigned", "void", "while", - "identifier", "integer-constant", "character-constant", "string-literal", + "char", "else", "enum", "for", "if", "int", "long", "return", "sizeof", "struct", "typedef", "unsigned", "void", + "while", "identifier", "integer-constant", "character-constant", "string-literal", // punctuators "[", "]", "(", ")", "{", "}", ".", "->", "++", "--", "&", "*", "+", "-", "~", "!", "/", "%", "<", ">", "<=", ">=", "==", "!=", "^", "|", "&&", "||", "?", ":", ";", "=", diff --git a/src/ctoken/ctoken.h b/src/ctoken/ctoken.h index 7e4cdcf9..ad8fbc32 100644 --- a/src/ctoken/ctoken.h +++ b/src/ctoken/ctoken.h @@ -10,6 +10,7 @@ typedef enum CTokenType { CTOKEN_KEYWORD_CHAR, CTOKEN_KEYWORD_ELSE, CTOKEN_KEYWORD_ENUM, + CTOKEN_KEYWORD_FOR, CTOKEN_KEYWORD_IF, CTOKEN_KEYWORD_INT, CTOKEN_KEYWORD_LONG, diff --git a/src/lexer/util.c b/src/lexer/util.c index 1a68f00c..1ce05b16 100644 --- a/src/lexer/util.c +++ b/src/lexer/util.c @@ -12,6 +12,7 @@ Map* new_keyword_map(void) { ctoken_map_add(keyword_map, "char", CTOKEN_KEYWORD_CHAR); ctoken_map_add(keyword_map, "else", CTOKEN_KEYWORD_ELSE); ctoken_map_add(keyword_map, "enum", CTOKEN_KEYWORD_ENUM); + ctoken_map_add(keyword_map, "for", CTOKEN_KEYWORD_FOR); ctoken_map_add(keyword_map, "if", CTOKEN_KEYWORD_IF); ctoken_map_add(keyword_map, "int", CTOKEN_KEYWORD_INT); ctoken_map_add(keyword_map, "long", CTOKEN_KEYWORD_LONG); diff --git a/tests/lexer/test_lexer.c b/tests/lexer/test_lexer.c index 193df4f2..0941457d 100644 --- a/tests/lexer/test_lexer.c +++ b/tests/lexer/test_lexer.c @@ -14,6 +14,7 @@ void test_read_declaration_pointer_with_init(void); void test_read_declaration_aggregate_with_init(void); void test_read_statement_if_else(void); void test_read_statement_while(void); +void test_read_statement_for(void); void test_read_assignment_expr(void); void test_read_conditional_expr(void); void test_read_logical_expr(void); @@ -48,6 +49,7 @@ CU_Suite* add_test_suite_lexer(void) { CU_ADD_TEST(suite, test_read_declaration_aggregate_with_init); CU_ADD_TEST(suite, test_read_statement_if_else); CU_ADD_TEST(suite, test_read_statement_while); + CU_ADD_TEST(suite, test_read_statement_for); CU_ADD_TEST(suite, test_read_assignment_expr); CU_ADD_TEST(suite, test_read_conditional_expr); CU_ADD_TEST(suite, test_read_decimal_integer_constant); @@ -416,6 +418,41 @@ void test_read_statement_while(void) { delete_vector(expected); } +void test_read_statement_for(void) { + char* input = "for (int i = 0; i < 10; i++) { x += 1; y *= 2; }"; + + Vector* expected = new_vector(&t_ctoken); + vector_push(expected, new_ctoken(CTOKEN_KEYWORD_FOR)); + vector_push(expected, new_ctoken(CTOKEN_LPAREN)); + vector_push(expected, new_ctoken(CTOKEN_KEYWORD_INT)); + vector_push(expected, new_identifier_ctoken(CTOKEN_IDENT, new_string("i"))); + vector_push(expected, new_ctoken(CTOKEN_EQUAL)); + vector_push(expected, new_iliteral_ctoken(CTOKEN_INT, new_signed_iliteral(INTEGER_INT, 0))); + vector_push(expected, new_ctoken(CTOKEN_SEMICOLON)); + vector_push(expected, new_identifier_ctoken(CTOKEN_IDENT, new_string("i"))); + vector_push(expected, new_ctoken(CTOKEN_LESS)); + vector_push(expected, new_iliteral_ctoken(CTOKEN_INT, new_signed_iliteral(INTEGER_INT, 10))); + vector_push(expected, new_ctoken(CTOKEN_SEMICOLON)); + vector_push(expected, new_identifier_ctoken(CTOKEN_IDENT, new_string("i"))); + vector_push(expected, new_ctoken(CTOKEN_PLUS_PLUS)); + vector_push(expected, new_ctoken(CTOKEN_RPAREN)); + vector_push(expected, new_ctoken(CTOKEN_LBRACE)); + vector_push(expected, new_identifier_ctoken(CTOKEN_IDENT, new_string("x"))); + vector_push(expected, new_ctoken(CTOKEN_PLUS_EQUAL)); + vector_push(expected, new_iliteral_ctoken(CTOKEN_INT, new_signed_iliteral(INTEGER_INT, 1))); + vector_push(expected, new_ctoken(CTOKEN_SEMICOLON)); + vector_push(expected, new_identifier_ctoken(CTOKEN_IDENT, new_string("y"))); + vector_push(expected, new_ctoken(CTOKEN_ASTERISK_EQUAL)); + vector_push(expected, new_iliteral_ctoken(CTOKEN_INT, new_signed_iliteral(INTEGER_INT, 2))); + vector_push(expected, new_ctoken(CTOKEN_SEMICOLON)); + vector_push(expected, new_ctoken(CTOKEN_RBRACE)); + vector_push(expected, new_ctoken(CTOKEN_EOF)); + + run_lexer_test(input, expected); + + delete_vector(expected); +} + void test_read_assignment_expr(void) { char* input = "x86 = 64; x += 1; x -= 1; x *= 2; x /= 2; x %= 2; x &= 1; x ^= 1; x |= 1;";