diff --git a/.gitignore b/.gitignore index 8d3d3c61..c2bafcaa 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ rules.ninja compile_commands.json #clangd缓存 .cache/ +lib/ #for rust target/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 714c9867..d9a86293 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,25 +15,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror") add_subdirectory("cmake_third_party/googletest") include_directories("cmake_third_party/googletest/googletest/include", "include") -add_library(language_module) -target_sources(language_module - PUBLIC - FILE_SET CXX_MODULES FILES - language/language.cppm -) - -message("${LANGUAGE_SOURCES}") - -add_library(Chinese language/chinese.cpp) - -add_library(English language/english.cpp) - -add_library(language language/english.cpp) - -target_link_libraries(Chinese language_module) -target_link_libraries(English language_module) -target_link_libraries(language language_module) - file(GLOB_RECURSE CPPMSOURCES "*.cppm") file(GLOB_RECURSE CPP_SOURCES "src/*.cpp") @@ -50,7 +31,6 @@ target_sources(foo add_executable(Trc ${CPP_SOURCES}) target_link_libraries(Trc foo) -target_link_libraries(Trc language) file(GLOB_RECURSE TEST_SOURCES "tests/unittest/*.cpp") @@ -69,7 +49,7 @@ add_executable(unittest ${TEST_SOURCES} ) target_compile_definitions(unittest PRIVATE "UNITTEST") -target_link_libraries(unittest testmodulefiles language gtest) +target_link_libraries(unittest testmodulefiles gtest) add_dependencies(Trc flex bison) add_dependencies(unittest flex bison) diff --git a/README.md b/README.md index fb260f67..0b9470db 100644 --- a/README.md +++ b/README.md @@ -12,15 +12,15 @@ Using c++ 20 standard and use module feature But here is a developing ```rust``` instance now! -in directory ```rust``` +[rust_trc](rust/) ## Goal -#### as easy as Python +**as easy as Python** -#### as fast as Java +**as fast as Java** -#### as small as lua +**as small as lua** ## project website @@ -43,13 +43,13 @@ trc support many languages,the way to change language is that uses the languag ### hello world -``` +```go println("hello world!") ``` ### calculate even numbers from 1 to n -``` +```go a := int(input()) while a > 0 { if a %2 == 0{ @@ -61,7 +61,7 @@ while a > 0 { ### Big num calculation -``` +```go a := 1231234561234 b := a println(a) @@ -85,6 +85,6 @@ QQ:3570249647 | Library |Usage | |:-----------|:-------| -| googletest | Uniitest | +| googletest | Unittest | Express my thanks to Open-source software contributors! diff --git a/src/compiler/parser.yy b/doc/developer/parser.yy similarity index 100% rename from src/compiler/parser.yy rename to doc/developer/parser.yy diff --git a/src/compiler/token.ll b/doc/developer/token.ll similarity index 100% rename from src/compiler/token.ll rename to doc/developer/token.ll diff --git a/language/chinese.cpp b/language/chinese.cpp deleted file mode 100644 index c3f3d59b..00000000 --- a/language/chinese.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/** - * 中文,作为dll插件提供不同语言 - */ - -module; -#include -#include -module language; - -namespace language { -namespace error { - const char* error_map[] = { "名字错误:", "值错误:", "语法错误:", - "版本错误:", "文件打开错误:", "模块找不到错误:", "参数错误:", - "除零错误:", "运行错误:", "断言错误:", "索引错误:", "内存错误:", - "键错误:", "系统错误:", "运算符错误:", "重定义错误:" }; - const char* error_from = "错误来自"; - const char* error_in_line = "错误发生在行"; - - const char* nameerror = "名字\"%\"没有被定义."; - const char* openfileerror = "无法打开?\"%\"."; - const char* versionerror - = R"(无法执行它们,因为他们的版本号%比trc版本号%高)"; - const char* memoryerror = "无法从操作系统中申请内存."; - const char* zerodiverror = "\"%\"被零除"; - const char* modulenotfounderror = "无法找到\"%\"模块."; - const char* keyerror = "键\"%\"未定义"; - const char* indexerror = R"(%超出了%的范围)"; - const char* valueerror = R"("%"不能被转换为"%")"; - const char* operatorerror = R"(不能使用运算符"%"对于类型:% and %)"; - const char* funcredefinederror = "函数%被重定义"; - const char* varredefinederror = "变量%被重定义"; - - const char* argumenterror = R"(%需要%个参数.)"; - - const char* syntaxerror_int = R"(数字%不正确.)"; - const char* syntaxerror_lexstring = R"(这个字符串不以"或'结尾)"; - const char* syntaxerror_no_expect = R"(%是不被期待的)"; - const char* syntaxerror_expect = R"(%是被期待的.)"; - const char* syntaxerror_lexanno = "多行注释应当以*/结尾"; - const char* syntaxerror_escape_char = R"(转义符%未定义)"; - const char* syntaxerror = "错误的语法"; - const char* syntaxerror_unmatched_char = "未匹配的'%'"; - - const char* asserterror_default = "断言"; - const char* asserterror_user = "%"; - - const char* dll_open_err = "找不到dll\"%\""; - - const char* noreach = "这个项目运行了不应该被运行的代码" - ".请将这个问题报告给github仓库"; - const char* magic_value_error - = "Trc:\"%s\"不是一个ctree文件.因为它的魔数不正确." - "\n"; -} -namespace help { - const char* help_msg = "Trc是一门基于栈的编程语言。这个项目实现" - "了大部分现代编程语言的功能,提供了一个完" - "善的工具链。它很方便被嵌入到你的项目中或" - "者作为工作脚本,也可以帮助你去学习编译原理."; -} - -namespace trc { - const char* mode_not_found = "模式没有被定义\n"; -} - -namespace tdb { - const char* var = "变量"; - const char* not_defined = "没有被定义"; - const char* start_tip = "trc的调试器正在运行.你可以阅读'Doc/" - "use/TDB.txt'以寻找帮助.\n"; - const char* instruction = "指令"; -} - -void locale_init() { - if (setlocale(LC_ALL, "zh_CN.UTF-8") == nullptr) { - fprintf(stderr, - "Can't load Chinese language.Because there isn't Chinese package " - "installed on your computer\n"); - } -} -} diff --git a/language/english.cpp b/language/english.cpp deleted file mode 100644 index ea67dcb7..00000000 --- a/language/english.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/** - * 英文的语句 - */ - -module; -#include -#include -module language; - -namespace language { -namespace error { - // 错误的信息具体定义,$标识待补充 - const char* error_map[] = { "NameError:", "ValueError:", "SyntaxError:", - "VersionError:", "OpenFileError:", "ModuleNotFoundError:", - "ArgumentError:", "ZeroDivError:", "RunError:", "AssertError:", - "IndexError:", "MemoryError:", "KeyError:", "SystemError:", - "OperatorError:", "RedefinedError:" }; - - const char* error_from = "Error from "; - const char* error_in_line = "Error in line "; - - // 格式化报错信息,规则:%代表插入的是字符串 - const char* nameerror = "Name \"%\" is not defined."; - const char* openfileerror = "Could't open \"%\"."; - const char* versionerror = R"(Could't run them.Because % is higher than %)"; - const char* memoryerror = "Can't get the memory from os."; - const char* zerodiverror = "\"%\" division by zero."; - const char* modulenotfounderror = "Could't find \"%\" module."; - const char* keyerror = "Key \"%\" is not defined."; - const char* indexerror = R"(% is out of %)"; - const char* valueerror = R"("%" could not be "%")"; - const char* operatorerror = R"(Couldn't use % for types:% and %)"; - const char* funcredefinederror = "Func % is redefined"; - const char* varredefinederror = "Var % is redefined."; - - const char* argumenterror = R"(% need % arguments.)"; - - const char* syntaxerror_int = R"(Number % is incorrect.)"; - const char* syntaxerror_lexstring = R"(The string isn't end with " or ')"; - const char* syntaxerror_no_expect = R"(% is not be expected.)"; - const char* syntaxerror_expect = R"(% is excepted.)"; - const char* syntaxerror_lexanno = "Comments should end with */"; - const char* syntaxerror_escape_char - = R"(Escape character % is not defined.)"; - const char* syntaxerror = "Invalid syntax."; - const char* syntaxerror_unmatched_char = "Unmatched '%'"; - - const char* asserterror_default = "Assert"; - const char* asserterror_user = "%"; - - const char* dll_open_err = R"(Dll % was not found)"; - - const char* noreach = "The program executed code that should " - "not have been executed.Please " - "report the problem to the Github " - "repository."; - const char* magic_value_error - = "Trc:\"%s\" is not a ctree file.Because its " - "magic number is error\n"; -} - -namespace help { - const char* help_msg = "Trc is a stack programming language. This " - "project implements most of the modern " - "programming language basics, provides a perfect " - "tool chain, which is suitable for working " - "scripts or embedded in your projects, and helps " - "to learn how to compile. "; -} - -namespace trc { - const char* mode_not_found = " mode is not defined.\n"; -} - -namespace tdb { - const char* var = "var"; - const char* not_defined = " is not defined.\n"; - const char* start_tip - = R"(Trc debugger is running.You can read 'Doc/use/TDB.txt' to find the help.\n)"; - const char* instruction = "instruction "; -} - -void locale_init() { - if (setlocale(LC_ALL, "en_US.UTF-8") == nullptr) { - fprintf(stderr, - "Can't load English language.Because there isn't English package " - "installed on your computer\n"); - } -} -} diff --git a/language/language.cppm b/language/language.cppm deleted file mode 100644 index 12693c9c..00000000 --- a/language/language.cppm +++ /dev/null @@ -1,72 +0,0 @@ -export module language; - -export namespace language { -/** - * @brief 地域化初始化设置 - */ -void locale_init(); - -namespace help { - extern const char* help_msg; -} -namespace trc { - extern const char* mode_not_found; -} -namespace tdb { - extern const char* var; - extern const char* not_defined; - extern const char* start_tip; - extern const char* instruction; -} - -namespace error { - extern const char* error_map[]; - extern const char* error_from; - extern const char* error_in_line; - - // 这些是常用的在不同场景下的报错信息,也可以自己定义 - - extern const char* nameerror; - extern const char* openfileerror; - extern const char* versionerror; - extern const char* memoryerror; - extern const char* argumenterror; - extern const char* modulenotfounderror; - extern const char* keyerror; - extern const char* indexerror; - extern const char* valueerror; - extern const char* operatorerror; - extern const char* funcredefinederror; - extern const char* varredefinederror; - - // 对数字和浮点数的解析 - extern const char* syntaxerror_int; - // 对字符串的解析 - extern const char* syntaxerror_lexstring; - // 对注释的解析 - extern const char* syntaxerror_lexanno; - // 遇到不期待字符时报的错 - extern const char* syntaxerror_no_expect; - // 未遇到期待字符,表示期待的字符 - extern const char* syntaxerror_expect; - // 未定义的转义符被读取到 - extern const char* syntaxerror_escape_char; - // 不明确的语法错误 - extern const char* syntaxerror; - // 无法匹配的符号 - extern const char* syntaxerror_unmatched_char; - - // 断言的默认报错信息 - extern const char* asserterror_default; - // 断言的用户定义报错信息 - extern const char* asserterror_user; - - extern const char* zerodiverror; - - extern const char* dll_open_err; - - extern const char* noreach; - - extern const char* magic_value_error; -} -} diff --git a/language/xmake.lua b/language/xmake.lua deleted file mode 100644 index 6e42c97e..00000000 --- a/language/xmake.lua +++ /dev/null @@ -1,12 +0,0 @@ --- 编译为dll才可以更换语言,所以无法编译到同一个文件中 -target("Chinese") - set_kind("shared") - add_files("chinese.cpp", "./**.cppm") - -target("English") - set_kind("shared") - add_files("english.cpp", "./**.cppm") - -target("language") - set_kind("shared") - add_files("english.cpp", "./**.cppm") diff --git a/locales/zh_CN/LC_MESSAGES/trans.po b/locales/zh_CN/LC_MESSAGES/trans.po new file mode 100644 index 00000000..3d21edc8 --- /dev/null +++ b/locales/zh_CN/LC_MESSAGES/trans.po @@ -0,0 +1,163 @@ + +msgid "NamaError" +msgstr "名字错误:" + +msgid "ValueError" +msgstr "值错误:" + +msgid "SyntaxError" +msgstr "语法错误:" + +msgid "VersionError" +msgstr "版本错误:" + +msgid "OpenFileError" +msgstr "文件打开错误:" + +msgid "ModuleNotFoundError" +msgstr "模块找不到错误:" + +msgid "ArgumentError" +msgstr "参数错误:" + +msgid "ZeroDivError" +msgstr "除零错误:" + +msgid "RunError" +msgstr "运行错误:" + +msgid "AssertError" +msgstr "断言错误:" + +msgid "IndexError" +msgstr "索引错误:" + +msgid "MemoryError" +msgstr "内存错误:" + +msgid "KeyError" +msgstr "键错误:" + +msgid "SystemError" +msgstr "系统错误:" + +msgid "OperatorError" +msgstr "运算符错误:" + +msgid "RedefinedError" +msgstr "重定义错误:" + +msgid "Error from " +msgstr "错误来自" + +msgid "Error in line" +msgstr "错误发生在行" + +msgid "Name \"%s\" is not defined." +msgstr "名字\"%s\"没有被定义." + +msgid "Could't open \"%s." +msgstr "无法打开?\"%s\"." + +msgid "Could't run them.Because %s is higher than %s" +msgstr "无法执行它们,因为他们的版本号%s比trc版本号%s高" + +msgid "Can't alloc the memory from os." +msgstr "无法从操作系统中申请内存." + +msgid "\"%s\" division by zero." +msgstr "\"%s\"被零除" + +msgid "Could't find \"%\s\" module." +msgstr "无法找到\"%s\"模块." + +msgid "Key \"%s\" is not defined." +msgstr "键\"%s\"未定义" + +msgid "%s is out of %s" +msgstr "%超出了%的范围" + +msgid "\"%s\" could not be \"%s\""" +msgstr "\"%s\"不能被转换为\"%s\"" + +msgid "Couldn't use %s for types:%s and %s" +msgstr "不能使用运算符\"%s\"对于类型:%s and %s" + +msgid "Function %s is redefined" +msgstr "函数%s被重定义" + +msgid "Variable %s is redefined" +msgstr "变量%s被重定义" + +msgid "%s need %s arguments." +msgstr "%s需要%s个参数." + +msgid "Number %s is incorrect." +msgstr "数字%s不正确." + +msgid "The string isn't end with \" or \'" +msgstr "这个字符串不以\"或\'结尾" + +msgid "%s is not be expected." +msgstr "%s是不被期待的" + +msgid "%s is excepted." +msgstr "%s是被期待的."; + +msgid "Comments should end with */" +msgstr "多行注释应当以*/结尾"; + +msgid "Escape character %s is not defined." +msgstr "转义字符%s未定义." + +msgid "Invalid syntax." +msgstr "错误的语法" + +msgid "Unmatched '%s'" +msgstr "未匹配的'%'" + +msgid "assert" +msgstr "断言" + +msgid "%s" +msgstr "%s" + +msgid "Dll %s was not found" +msgstr "找不到dll\"%s\"" + + +msgid "The program executed code that should not have been executed.Please +report the problem to the Github repository." +msgstr "这个项目运行了不应该被运行的代码 +.请将这个问题报告给github仓库"; + +msgid "Trc:\"%s\" is not a ctree file.Because its +magic number is error\n" +msgstr "Trc:\"%s\"不是一个ctree文件.因为它的魔数不正确.\n" + +msgid "Trc is a stack programming language. This +project implements most of the modern +programming language basics, provides a perfect +tool chain, which is suitable for working +scripts or embedded in your projects, and helps +to learn how to compile. " +msgstr "Trc是一门基于栈的编程语言。这个项目实现 +了大部分现代编程语言的功能,提供了一个完 +善的工具链。它很方便被嵌入到你的项目中或 +者作为工作脚本,也可以帮助你去学习编译原理." + +msgid "mode is not defined.\n" +msgstr "模式没有被定义\n"; + +msgid "var" +msgstr "变量" + +msgid " is not defined.\n" +msgstr "没有被定义" + +msgid "Trc debugger is running.You can read 'doc/use/TDB.md' to find the help." +msgstr "trc的调试器正在运行.你可以阅读'doc/ +use/TDB.md'以寻找帮助.\n" + +msgid "instruction" +msgstr "指令" diff --git a/rust/locales/en_US/LC_MESSAGES/trans.mo b/rust/locales/en/LC_MESSAGES/trans.mo similarity index 100% rename from rust/locales/en_US/LC_MESSAGES/trans.mo rename to rust/locales/en/LC_MESSAGES/trans.mo diff --git a/rust/locales/en_US/LC_MESSAGES/trans.po b/rust/locales/en/LC_MESSAGES/trans.po similarity index 100% rename from rust/locales/en_US/LC_MESSAGES/trans.po rename to rust/locales/en/LC_MESSAGES/trans.po diff --git a/rust/src/tvm/algo/string.rs b/rust/src/tvm/algo/string.rs index d051f642..f76c40b5 100644 --- a/rust/src/tvm/algo/string.rs +++ b/rust/src/tvm/algo/string.rs @@ -25,9 +25,9 @@ pub fn kmp(main_string:&str, pattern:&str) -> usize { /// ``` /// let next = kmp_next("ababab"); /// ``` -pub fn kmp_next(pattern:&str) -> Vec { +pub fn kmp_next(pattern:&str) -> Vec { let mut j:i64 = -1; - let mut ret: Vec = vec![0]; + let mut ret: Vec = vec![-1]; let pattern:Vec = pattern.chars().collect(); // 从1开始匹配是因为第零个不需要匹配 for i in 1..pattern.len() { @@ -37,7 +37,7 @@ pub fn kmp_next(pattern:&str) -> Vec { if pattern[(j + 1) as usize ] == pattern[i] { j += 1; } - ret.push(j as usize); + ret.push(j); } ret } diff --git a/script/gen_locales.py b/script/gen_locales.py new file mode 100644 index 00000000..f14f6941 --- /dev/null +++ b/script/gen_locales.py @@ -0,0 +1,20 @@ +""" +A easy script for auto generate the locales files +""" + +import os + +os.chdir("../locales") +tt = os.listdir(".") +for i in tt: + os.chdir(i) + os.chdir("LC_MESSAGES") + l = os.listdir() + for j in l: + tmp = os.path.splitext(j) + if tmp[1] != ".po": + continue + if os.system(f"msgfmt -o {tmp[0]}.mo {j}"): + print("Cannot generate your locales files.Stop.") + exit(1) + os.chdir("../..") diff --git a/src/compiler/compiler.hpp b/src/compiler/compiler.hpp index 35b86930..ecb2b25d 100644 --- a/src/compiler/compiler.hpp +++ b/src/compiler/compiler.hpp @@ -1,6 +1,5 @@ #pragma once -#include "parser.tab.hpp" #include namespace trc::TVM_space { @@ -29,7 +28,6 @@ class compiler { // Whether to generate scanner debug traces. bool trace_scanning = false; // The token's location used by the scanner. - yy::location location; private: FILE* open_file(const std::string& file); @@ -38,7 +36,3 @@ class compiler { }; } -// Give Flex the prototype of yylex we want ... -#define YY_DECL yy::parser::symbol_type yylex(trc::compiler::compiler& drv) -// ... and declare it for the parser's sake. -YY_DECL; diff --git a/src/trc/Trc.cpp b/src/trc/Trc.cpp index b2d8bd62..c89b0a9a 100644 --- a/src/trc/Trc.cpp +++ b/src/trc/Trc.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #ifdef UNITTEST #include #endif @@ -24,7 +26,6 @@ import tdb; import tools.token; import tshell; import basic_def; -import language; import data; import color; import help; @@ -82,7 +83,9 @@ static inline void quit_mem() { int main(int argc, char* argv[]) { // 初始化系统 // 初始化地域化设置 - language::locale_init(); + std::setlocale(LC_ALL, ""); + bindtextdomain("trans", "locales"); + textdomain("trans"); /*控制台初始化*/ #ifdef _WIN32 trc::color::console_init(); diff --git a/xmake.lua b/xmake.lua index 26b0f95f..d8a72ee0 100644 --- a/xmake.lua +++ b/xmake.lua @@ -17,21 +17,12 @@ add_rules("lex", "yacc") target("Trc") set_kind("binary") - -- on_load(function (target) - -- -- 用于生成bison和flex的文件,该跨平台问题并不显著,因为bison和flex名称接口和行为是固定的 - -- os.run("flex -o src/compiler/token.cpp src/compiler/token.ll") - -- os.run("bison -o src/compiler/parser.cpp src/compiler/parser.yy") - -- end) add_files("src/**.cpp", "src/**.cppm", "language/**.cppm") add_files("src/**.ll", "src/**.yy") - includes("language") - add_deps("language") target("unittest") set_kind("binary") set_default(false) add_defines("UNITTEST") add_files("src/**.cpp", "src/**.cppm", "tests/unittest/**.cpp", "tests/unittest/**.cppm") - includes("language") - add_deps("language") add_packages("gtest")