diff --git a/CMakeLists.txt b/CMakeLists.txt index f506b20..4b92779 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,9 +19,7 @@ file(GLOB TEST_SOURCES ${CMAKE_SOURCE_DIR}/unit/*.cpp) foreach(TEST_SOURCE ${TEST_SOURCES}) get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE) - add_executable(${TEST_NAME} ${TEST_SOURCE} ${SOURCES} - include/scanner.h - include/Tbs.h) + add_executable(${TEST_NAME} ${TEST_SOURCE} ${SOURCES}) add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) endforeach(TEST_SOURCE ${TEST_SOURCES}) diff --git a/include/Scanner.h b/include/Scanner.h index bbf2153..6170c97 100644 --- a/include/Scanner.h +++ b/include/Scanner.h @@ -22,11 +22,13 @@ public: i += len - 1; len = 0; } else if (len = process_key_table(i)) { - i += len - 1; + i += len - 1; len = 0; } else if (len = process_punct_table(i)) { i += len - 1; len = 0; + } else if (m_source_code[i] == ' ' || m_source_code[i] == '\t' || m_source_code[i] == '\n') { + continue; } else { std::cerr << "Error: Tokenize" << std::endl; exit(0); @@ -50,4 +52,7 @@ private: std::vector m_token_list; Tbs m_tables; int index; + + // 记录标识符表的索引 + int identifier_index = 0; }; \ No newline at end of file diff --git a/src/Scanner.cpp b/src/Scanner.cpp index cf8b5d7..7387059 100644 --- a/src/Scanner.cpp +++ b/src/Scanner.cpp @@ -1,15 +1,40 @@ #include "Scanner.h" -using std::cout; +#include int Scanner::process_const_table(int index) { + return 0; } int Scanner::process_identifier_table(int index) { + std::stringstream buffer; + int old_index = index; + if (std::isalpha(m_source_code[index])) { + buffer << m_source_code[index]; + index += 1; + while(std::isalnum(m_source_code[index])) { + buffer << m_source_code[index]; + index += 1; + } + + std::string identifier = buffer.str(); + for (const auto& key : m_tables.KeyTable) { + if (identifier == key.second) { + return 0; + } + } + + m_tables.IdTable.insert({identifier_index, identifier}); + m_token_list.push_back(Token{identifier_index, ID_TABLE}); + identifier_index++; + return index - old_index; + } else { + return 0; + } } int Scanner::process_key_table(int index) { - + return 0; } int Scanner::process_punct_table(int index) { diff --git a/unit/scanner_test.cpp b/unit/scanner_test.cpp index e9918d8..1246e77 100644 --- a/unit/scanner_test.cpp +++ b/unit/scanner_test.cpp @@ -6,6 +6,17 @@ #include using std::string,std::vector; +TEST_CASE("Scanner test identifier table") { + Tbs tables; + std::string src = "abcvljl laadfs fafarwrw"; + Scanner scan(src, tables); + scan.scan(); + + for (auto value_src: scan.get_token_list()) { + std::cout << value_src.id << " " << value_src.type << "\n"; + } +} + TEST_CASE("Scanner test Punct table") { Tbs tables = {};