From b2c1f28b37ccd687357f173ce7e0d1bdfbd4da33 Mon Sep 17 00:00:00 2001 From: Gary Gan Date: Tue, 3 Jun 2025 19:46:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=AF=8D=E6=B3=95=E5=88=86?= =?UTF-8?q?=E6=9E=90=E7=9B=B8=E5=85=B3=E5=87=BD=E6=95=B0=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +-- include/Scanner.h | 45 +++++++++++++++++++++-- include/Tbs.h | 85 ++++++++++++++++++++++++++++++++++++++++++- include/Token.h | 5 ++- src/Scanner.cpp | 15 +++++++- unit/scanner_test.cpp | 16 ++++---- 6 files changed, 150 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 14dfb53..b2fc96e 100644 --- a/README.md +++ b/README.md @@ -259,10 +259,7 @@ a = point {x:24, y:43}; | tuple | 21 | | print | 22 | | println | 23 | -| -| | -| | | -| | | + #### 界符表 diff --git a/include/Scanner.h b/include/Scanner.h index 9b0cb08..bbf2153 100644 --- a/include/Scanner.h +++ b/include/Scanner.h @@ -2,13 +2,52 @@ #include "stdc++.h" #include "Token.h" #include "Tbs.h" +#include +#include +#include + class Scanner { public: - Scanner(std::string source_code) - : m_source_code(source_code) {} + Scanner(std::string source_code, Tbs tables) + : m_source_code(source_code), m_tables(tables) {} + + + void scan() { + int len = 0; + for (int i = 0; i < m_source_code.size(); i++) { + if (len = process_const_table(i)) { + i += len - 1; + len = 0; + } else if (len = process_identifier_table(i)) { + i += len - 1; + len = 0; + } else if (len = process_key_table(i)) { + i += len - 1; + len = 0; + } else if (len = process_punct_table(i)) { + i += len - 1; + len = 0; + } else { + std::cerr << "Error: Tokenize" << std::endl; + exit(0); + } + + } + } + + inline std::vector get_token_list() { + return m_token_list; + } + + int process_const_table(int i); + int process_identifier_table(int i); + int process_key_table(int i); + int process_punct_table(int i); + - int is_int(int index); private: std::string m_source_code; + std::vector m_token_list; + Tbs m_tables; int index; }; \ No newline at end of file diff --git a/include/Tbs.h b/include/Tbs.h index 90732c2..70c3df1 100644 --- a/include/Tbs.h +++ b/include/Tbs.h @@ -1,9 +1,90 @@ # pragma once - #include "stdc++.h" +#include + using std::unordered_map,std::string; class Tbs { public: unordered_map ConstTable; unordered_map IdTable; -}; \ No newline at end of file + std::unordered_map KeyTable = { + {1, "var"}, + {2, "i8"}, + {3, "i16"}, + {4, "i32"}, + {5, "i64"}, + {6, "u8"}, + {7, "u16"}, + {8, "u32"}, + {9, "u64"}, + {10, "float32"}, + {11, "float64"}, + {12, "char"}, + {13, "for"}, + {14, "if"}, + {15, "else"}, + {16, "bool"}, + {17, "string"}, + {18, "vector"}, + {19, "array"}, + {20, "struct"}, + {21, "tuple"}, + {22, "print"}, + {23, "println"} + }; + + std::unordered_map PunctTable = { + {1, "-"}, + {2, "!"}, + {3, "~"}, + {4, "/"}, + {5, "*"}, + {6, "%"}, + {7, "+"}, + {8, "-"}, + {9, "<<"}, + {10, ">>"}, + {11, ">"}, + {12, ">="}, + {13, "<"}, + {14, "<="}, + {15, "=="}, + {16, "!="}, + {17, "&"}, + {18, "^"}, + {19, "|"}, + {20, "&&"}, + {21, "||"}, + {22, "="}, + {23, "%="}, + {24, "*="}, + {25, "/="}, + {26, "+="}, + {27, "-="}, + {28, "|="}, + {29, "&="}, + {30, "^="}, + {31, "<<="}, + {32, ">>="}, + {33, "("}, + {34, ")"}, + {35, "<"}, + {36, ">"}, + {37, ","}, + {38, "."}, + {39, "["}, + {40, "]"}, + {41, "?"}, + {42, ":"}, + {43, "->"} + }; + +}; + +enum Table_Type { + CONST_TABLE, + ID_TABLE, + KEY_TABLE, + PUNCT_TABLE +}; + diff --git a/include/Token.h b/include/Token.h index c4eaacc..f5f55ff 100644 --- a/include/Token.h +++ b/include/Token.h @@ -1,7 +1,8 @@ #pragma once #include "stdc++.h" +#include "Tbs.h" struct Token{ - int id,type; - Token (int id,int type):id(id),type(type){} + int id; + Table_Type type; }; \ No newline at end of file diff --git a/src/Scanner.cpp b/src/Scanner.cpp index 8b6e530..e55365e 100644 --- a/src/Scanner.cpp +++ b/src/Scanner.cpp @@ -1,6 +1,17 @@ #include "Scanner.h" -using std::vector,std::string; -int Scanner::is_int(int index) { +int Scanner::process_const_table(int index) { +} + +int Scanner::process_identifier_table(int index) { + +} + +int Scanner::process_key_table(int index) { + +} + +int Scanner::process_punct_table(int index) { + } diff --git a/unit/scanner_test.cpp b/unit/scanner_test.cpp index c2f7265..8537314 100644 --- a/unit/scanner_test.cpp +++ b/unit/scanner_test.cpp @@ -1,16 +1,16 @@ +#include "Token.h" #include "doctest.h" #include "stdc++.h" #include "Scanner.h" #include "Tbs.h" +#include using std::string,std::vector; -TEST_CASE("Scanner_Test") { - Scanner scanner; - Tbs tb; - scanner.scan("to be or not to be is a problem.",tb); - unordered_map test; - test[1] = "to",test[2] = "be",test[3] = "or",test[4] = "not",test[5] = "to",test[6] ="be",test[7] = "is",test[8] = "a"; +TEST_CASE("Scanner test const table") { + Tbs tables; + std::string src = "1.23"; + Scanner scan(src, tables); + scan.scan(); - CHECK(tb.ConstTable == test); - CHECK(tb.IdTable == test); + CHECK(scan.get_token_list() == std::vector({Token{1, CONST_TABLE}})); }