From 971583b1c78653fb75085a7fc4d551f95b12e05b Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Wed, 22 May 2024 19:23:09 +0800 Subject: [PATCH] :sparkles: support utf8 ast dump on windows --- src/ast_dumper.h | 16 ++++++++++++++++ src/natives/nasal_builtin.cpp | 14 ++++++++++++++ src/natives/nasal_builtin.h | 3 +++ std/runtime.nas | 6 ++++++ test/utf8chk.nas | 21 +++++++++++++-------- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/ast_dumper.h b/src/ast_dumper.h index 5825ed7..f95f6bd 100644 --- a/src/ast_dumper.h +++ b/src/ast_dumper.h @@ -2,6 +2,10 @@ #include "ast_visitor.h" +#ifdef _WIN32 +#include +#endif + #include #include #include @@ -89,7 +93,19 @@ public: public: void dump(code_block* root) { +#ifdef _WIN32 + // store previous code page + auto cp = GetConsoleOutputCP(); + // allow 65001 code page + SetConsoleOutputCP(CP_UTF8); +#endif + root->accept(this); + +#ifdef _WIN32 + // restore previous code page + SetConsoleOutputCP(cp); +#endif } }; diff --git a/src/natives/nasal_builtin.cpp b/src/natives/nasal_builtin.cpp index 628c711..d9beaba 100644 --- a/src/natives/nasal_builtin.cpp +++ b/src/natives/nasal_builtin.cpp @@ -1,6 +1,10 @@ #include "natives/nasal_builtin.h" #include +#ifdef _WIN32 +#include +#endif + namespace nasal { var builtin_unsafe(context* ctx, gc* ngc) { @@ -717,6 +721,15 @@ var builtin_ghosttype(context* ctx, gc* ngc) { return ngc->newstr(name); } +var builtin_set_utf8_output(context* ctx, gc* ngc) { +#ifdef _WIN32 + // allow 65001 code page + SetConsoleOutputCP(CP_UTF8); +#endif + // do nothing on other platforms + return nil; +} + nasal_builtin_table builtin[] = { {"__print", builtin_print}, {"__println", builtin_println}, @@ -763,6 +776,7 @@ nasal_builtin_table builtin[] = { {"__gcinfo", builtin_gcinfo}, {"__logtime", builtin_logtime}, {"__ghosttype", builtin_ghosttype}, + {"__set_utf8_output", builtin_set_utf8_output}, {nullptr, nullptr} }; diff --git a/src/natives/nasal_builtin.h b/src/natives/nasal_builtin.h index 11f096d..2239a17 100644 --- a/src/natives/nasal_builtin.h +++ b/src/natives/nasal_builtin.h @@ -81,6 +81,9 @@ var builtin_gcinfo(context*, gc*); var builtin_logtime(context*, gc*); var builtin_ghosttype(context*, gc*); +// only useful on windows platform +var builtin_set_utf8_output(context*, gc*); + // register builtin function's name and it's address here in this table below // this table must end with {nullptr, nullptr} struct nasal_builtin_table { diff --git a/std/runtime.nas b/std/runtime.nas index bad3203..2cd57a1 100644 --- a/std/runtime.nas +++ b/std/runtime.nas @@ -27,3 +27,9 @@ var gc = { }, info: func() {return __gcinfo;} }; + +var windows = { + set_utf8_output: func() { + return __set_utf8_output; + } +}; diff --git a/test/utf8chk.nas b/test/utf8chk.nas index ff2b445..63c5cfe 100644 --- a/test/utf8chk.nas +++ b/test/utf8chk.nas @@ -1,11 +1,16 @@ +use std.runtime; + +# do nothing if platform is not windows +runtime.windows.set_utf8_output(); + var unicode测试 = func() { var 输出=print; var 测试成功=[ - "unicode: utf-8支持测试成功", - "目前仅支持utf-8以及ascii格式文件", - "注意: windows系统请开启chcp 65001代码页" + "unicode: utf-8 支持测试成功", + "目前仅支持 utf-8 以及 ascii 格式文件", + "注意: windows 系统请开启 chcp 65001 代码页" ]; - foreach(var 内容;测试成功) + foreach(var 内容; 测试成功) 输出(内容~"\n"); } @@ -41,10 +46,10 @@ var emoji测试 = func() { 🍾:🍾, 🐘:🐘 }; - foreach(var 📄;📁) - 💻(📄,🎤); - foreach(var 📄;keys(🗄️)) - 💻(📄,🗄️[📄],🎤); + foreach(var 📄; 📁) + 💻(📄, 🎤); + foreach(var 📄; keys(🗄️)) + 💻(📄, 🗄️[📄], 🎤); } var dotsgen = func() {