Skip to content

Commit

Permalink
implement lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis committed Oct 8, 2023
1 parent 3c118a3 commit dfcfac6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ctoken/ctoken.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
"[", "]", "(", ")", "{", "}", ".", "->", "++", "--", "&", "*", "+", "-", "~", "!", "/", "%", "<", ">",
"<=", ">=", "==", "!=", "^", "|", "&&", "||", "?", ":", ";", "=",
Expand Down
1 change: 1 addition & 0 deletions src/ctoken/ctoken.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/lexer/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions tests/lexer/test_lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;";

Expand Down

0 comments on commit dfcfac6

Please sign in to comment.