From d76457d794dd19dee8a9c061f09afe90d8d6edaf Mon Sep 17 00:00:00 2001 From: msojocs Date: Tue, 13 Jun 2023 21:34:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 + src/include/wxml.h | 122 ++++++++++++----- src/wxml/compiler.cpp | 38 +++--- src/wxml/dom_lib/str_cache.cpp | 42 ++++++ src/wxml/dom_lib/wxml_dom.cpp | 186 ++++++++++++++++++++++++++ src/wxml/name_allocator.cpp | 8 ++ src/wxml/rewrite.cpp | 232 ++++++++++++++++++++++++++++++++- test/wcc.disassembly.cpp | 30 ++--- 8 files changed, 588 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d20dfc0..c30b5c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,11 @@ add_executable(wcc src/wcc.cpp src/include/wxml.h src/wcc/usage.cpp + src/wxml/rewrite.cpp src/wxml/name_allocator.cpp src/wxml/dom_lib/token.cpp src/wxml/compiler.cpp + src/wxml/dom_lib/str_cache.cpp src/wxml/dom_lib/wxml_dom.cpp src/wxml/dom_lib/machine.cpp src/wxml/dom_lib/parser.cpp diff --git a/src/include/wxml.h b/src/include/wxml.h index 25a3461..fd5c742 100644 --- a/src/include/wxml.h +++ b/src/include/wxml.h @@ -10,6 +10,18 @@ namespace WXML { + namespace Rewrite + { + void ToStringCode(char const*, int, std::stringstream &ss); + void ToStringCode(std::string const&, std::stringstream &); + void ToStringCode(std::string const&, std::string&); + std::string ToStringCode(std::string const&); + std::string ToStringCode2(char const*, int); + std::string ToStringCode2(std::string const&); + std::string ToStringCode3(char const*, int); + std::string ToStringCode3(std::string const&); + } + class NameAllocator { private: @@ -28,10 +40,6 @@ namespace WXML namespace DOMLib { - /** - * 参数值可能不是int - */ - bool AttrsCompartor(int a1, int a2); /** * 拼接字符串 @@ -83,36 +91,93 @@ namespace WXML ~Token(); }; + /** + * + */ + bool AttrsCompartor( + std::pair const& a1, + std::pair const& a2); + class StrCache + { + private: + /* data */ + std::map d; + std::vector offset_6; + public: + StrCache(/* args */); + ~StrCache(); + void RenderPathDefine(std::stringstream ss); + void Insert(std::string); + int GetStrID(std::string); + }; + class WXMLDom { private: /* data */ + std::string type; + bool offset_28; + int offset_92; + int offset_96; + StrCache offset_62; public: std::string tag; std::map offset_12; WXMLDom(/* args */); ~WXMLDom(); void Error(); - // void RenderMeAsFunction( - // std::string const&, - // std::string const&, - // std::string&, - // std::string const&, - // std::stringstream &, - // // WXML::NameAllocator *, - // std::string const&, - // std::string const&, - // std::string const&, - // std::string const&, - // std::string const&, - // char, - // std::string const&, - // bool, - // bool, - // uint, - // std::string const& - // ); + void RenderMeAsFunction( + std::string const&, + std::string const&, + std::string&, + std::string const&, + std::stringstream &, + WXML::NameAllocator *, + std::string const&, + std::string const&, + std::string const&, + std::string const&, + std::string const&, + char, + std::string const&, + bool, + bool, + uint, + std::string const& + ); + void RenderChildren( + std::string const& a2, + std::string const& a3, + std::string& a4, + std::string const& a5, + std::stringstream & a6, + WXML::NameAllocator * a7, + std::string const& a8, + std::string const& a9, + std::string const& a10, + std::string const& a11, + char a12, + bool a13, + uint a14, + std::map * a15 + ); + void RenderNonDefine( + std::string const& a2, + std::string const& a3, + std::string& a4, + std::string const& a5, + std::stringstream &a6, + WXML::NameAllocator *a7, + std::string const& a8, + std::string const& a9, + std::string const& a10, + std::string const& a11, + char a12, + bool a13, + uint a14, + std::map * a15 + ); bool operator==(std::string tag); }; @@ -174,6 +239,8 @@ namespace WXML void Feed(char,std::vector &,std::string &,std::vector &,int); }; + + }; @@ -281,15 +348,6 @@ namespace WXML }; - class Rewrite - { - private: - /* data */ - public: - Rewrite(/* args */); - ~Rewrite(); - - }; namespace EXPRLib diff --git a/src/wxml/compiler.cpp b/src/wxml/compiler.cpp index 656a7b6..2c5736d 100644 --- a/src/wxml/compiler.cpp +++ b/src/wxml/compiler.cpp @@ -347,28 +347,28 @@ namespace WXML{ } if (a1 == "wx-define") { + WXML::NameAllocator na(a17, a18); auto token = a1.offset_12["name"]; auto attr = token.ToAttrContent(); a3[attr].assign(a2); - // WXML::DOMLib::WXMLDom::RenderMeAsFunction( - // a1, - // (int *)a2, - // a14, - // a4, - // (int *)v31, - // a5, - // (int *)v33, - // a10, - // a11, - // a12, - // (int *)v32, // "r" - // a15, - // a9, - // a16, - // 1, - // a7, - // a8, - // a20); + a1.RenderMeAsFunction( + a2, + a14, + a4, + attr, + a5, + &na, + a10, + a11, + a12, + "r", // "r" + a15, + a9, + a16, + 1, + a7, + a8, + a20); } for (int i = 0; i < 6; i++) diff --git a/src/wxml/dom_lib/str_cache.cpp b/src/wxml/dom_lib/str_cache.cpp index e69de29..7d21bb1 100644 --- a/src/wxml/dom_lib/str_cache.cpp +++ b/src/wxml/dom_lib/str_cache.cpp @@ -0,0 +1,42 @@ + +#include "../../include/wxml.h" + + +namespace WXML { + + + namespace DOMLib + { + StrCache::StrCache(/* args */) + { + } + + StrCache::~StrCache() + { + } + void StrCache::RenderPathDefine(std::stringstream ss) + { + ss << "var x=["; + // TODO... + // return ss; + } + void StrCache::Insert(std::string s) + { + if (!this->d.count(s)) + { + // this->d[s] = this[5]; // ??? + this->offset_6.push_back(s); + } + } + + int StrCache::GetStrID(std::string s) + { + int result = -1; + if(this->d.count(s)) + { + return this->d[s]; + } + return result; + } + } +} \ No newline at end of file diff --git a/src/wxml/dom_lib/wxml_dom.cpp b/src/wxml/dom_lib/wxml_dom.cpp index dd86bf9..13efd77 100644 --- a/src/wxml/dom_lib/wxml_dom.cpp +++ b/src/wxml/dom_lib/wxml_dom.cpp @@ -13,6 +13,192 @@ namespace WXML { WXMLDom::~WXMLDom() { + } + const char off_553FDC[] = {'[','x','[','\0'}; + void WXMLDom::RenderMeAsFunction( + std::string const& a2, + std::string const& a3, + std::string& a4, + std::string const& a5, + std::stringstream & a6, + WXML::NameAllocator * a7, + std::string const& a8, + std::string const& a9, + std::string const& a10, + std::string const& a11, + std::string const& a12, + char a13, + std::string const& a14, + bool a15, + bool a16, + uint a17, + std::string const& a18 + ) + { + try + { + std::string name; + a7->GetNextName(name); + if (a15) + { + a6 << a12 << off_553FDC; + int id = offset_62.GetStrID(a2); + a6 << id; + a6 << "]][\""; + WXML::Rewrite::ToStringCode(a5, a6); + a6 << "\"]"; + } + else + { + a6 << "var " << a5; + } + a6 << "=function("; + a6 << a8 << ","; + a6 << a9 << ","; + a6 << a11 << ","; + a6 << a10 << "){"; + a6 << a13; + // if (a18) + { + a6 << "var z=" << a18 << "()" << a13; + } + if (a15) + { + a6 << "var " << name; + a6 << "=x["; + int id = this->offset_62.GetStrID(a2); + a6 << id << "]"; + a6 << "+':"; + WXML::Rewrite::ToStringCode(a5, a6); + a6 << "'" << a13 << a11 << ".wxVkey="; + a6 << name << a13 << a10 << ".f=$gdc(f_[\""; + a6 << WXML::Rewrite::ToStringCode(a2); + a6 << "\"],\"\",1)"; + a6 << a13; + a6 << "if(" << a14 << "[" << name << "]{_wl("; + a6 << name << ",x["; + id = this->offset_62.GetStrID(a2); + a6 << id << "]);return}" << a13; + + a6 << a14 << "[" << name << "]=true"; + a6 << a13 << "try{" << a13; + } + // RenderChildren + if (a15) + { + a6 << "}catch(err){" << a13; + a6 << a14 << "[" << name << "]=false"; + a6 << a13 << "throw err" << a13 << "}"; + a6 << a13 << a14 << "[" << name << "]=false" << a13; + } + a6 << "return " << a11 << a13 << "}" << a13; + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + + + } + void WXMLDom::RenderChildren( + std::string const& a2, + std::string const& a3, + std::string& a4, + std::string const& a5, + std::stringstream & a6, + WXML::NameAllocator * a7, + std::string const& a8, + std::string const& a9, + std::string const& a10, + std::string const& a11, + char a12, + bool a13, + uint a14, + std::map * a15 + ) + { + auto size = a6.tellp(); + if (size > 52428800) + { + throw "Interal error: generated code (>10M) will be too heavy to fly in a narrow wind."; + } + // while (/* condition */) + // { + // /* code */ + // } + int Stra; + // for (int i = 0; ; i = Stra) + // { + + // } + + + } + + void WXMLDom::RenderNonDefine( + std::string const& a2, + std::string const& a3, + std::string& a4, + std::string const& a5, + std::stringstream &a6, + WXML::NameAllocator *a7, + std::string const& a8, + std::string const& a9, + std::string const& a10, + std::string const& a11, + char a12, + bool a13, + uint a14, + std::map * a15 + ) + { + if (a13 && this->offset_28) + { + a6 << "cs.push(\""; + std::string sc = WXML::Rewrite::ToStringCode(a2); + a6 << sc << ":" << this->tag << ":" << this->offset_92 << ":" << this->offset_96 << "\")"; + } + if (this->type == "TEXTNODE") + { + + } + if (this->type == "wx-define" + ||this->type == "wx-import" + ||this->type == "import" + ||this->type == "template") + { + + } + if (this->type == "wx-repeat") + { + + } + if (this->type == "block") + { + + } + if (this->type == "include") + { + + } + if (this->type == "wx-template") + { + + } + // for (size_t i = 0; i < count; i++) + // { + // /* code */ + // } + // sort + // for (size_t i = 0; i < count; i++) + // { + // /* code */ + // } + // if () + // { + // ".rawAttr=" + // } + } bool WXMLDom::operator==(std::string tag) { diff --git a/src/wxml/name_allocator.cpp b/src/wxml/name_allocator.cpp index 03f67d0..3296f31 100644 --- a/src/wxml/name_allocator.cpp +++ b/src/wxml/name_allocator.cpp @@ -10,6 +10,14 @@ namespace WXML { this->offset_1.assign(a2); this->offset_7.assign(a3); } + + /** + * TODO: + */ + void NameAllocator::GetNextName(std::string & a2) + { + + } NameAllocator::~NameAllocator() { diff --git a/src/wxml/rewrite.cpp b/src/wxml/rewrite.cpp index 982ba99..9482cdd 100644 --- a/src/wxml/rewrite.cpp +++ b/src/wxml/rewrite.cpp @@ -3,12 +3,232 @@ namespace WXML { - - Rewrite::Rewrite(/* args */) - { - } - - Rewrite::~Rewrite() + namespace Rewrite { + void ToStringCode(char const* src, int len, std::stringstream &ss) + { + for (size_t i = 0; i < len; i++) + { + char v4 = src[i]; + if ( v4 == '\''/*39*/ ) + { + ss << "\\x27"; + continue; + } + if ( v4 <= '\''/*39*/ ) + { + if ( v4 == '"'/*34*/ ) + { + ss << "\\x22"; + continue; + } + if ( v4 > '"'/*34*/ ) + { + if ( v4 == '&'/*38*/ ) + { + ss << "\\x26"; + continue; + } + } + else + { + if ( v4 == '\n'/*10*/ ) + { + ss << "\\n"; + continue; + } + if ( v4 == '\r'/*13*/ ) + { + ss << "\\r"; + continue; + } + } + goto LABEL_31; + } + if ( v4 == '>' /*62*/ ) + { + ss << "\\x3e"; + continue; + } + if ( v4 <= '>' /*62*/ ) + { + if ( v4 == '<'/*60*/ ) + { + ss << "\\x3c"; + continue; + } + if ( v4 == '='/*61*/ ) + { + ss << "\\x3d"; + continue; + } + LABEL_31: + ss << v4; + continue; + } + if ( v4 != '\\'/*92*/ ){ + ss << v4; + continue; + } + char v6 = src[i + 1]; + if ( v6 - '\\'/*92*/ > 0x18u ) + { + if ( v6 == '\''/*39*/ || v6 == '"'/*34*/ ) + { + LABEL_29: + ss << "\\"; + continue; + } + } + else if ( ((0x1440001u >> (v6 - '\\'/*92*/)) & 1) != 0 ) + { + goto LABEL_29; + } + ss << "\\x5c"; + + } + + } + void ToStringCode(std::string const&src, std::stringstream & ss) + { + ToStringCode(src.data(), src.size(), ss); + } + void ToStringCode(std::string const& a1, std::string& a2) + { + try + { + std::stringstream ss; + ToStringCode(a1.data(), a1.size(), ss); + // TODO: + a2 = ss.str(); + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } + } + std::string ToStringCode(std::string const&a2) + { + std::string result; + ToStringCode(a2, result); + return result; + } + std::string ToStringCode2(char const* src, int len) + { + std::stringstream ss; + for (size_t i = 0; i < len; i++) + { + char v3 = src[i]; + if ( v3 == '\''/*39*/ ) + { + ss << "\\x27"; + continue; + } + if ( v3 > 39 ) // '\'' + { + if ( v3 == 62 ) // '>' + { + ss << "\\x3e"; + continue; + } + if ( v3 > 62 ) // '>' + { + if ( v3 == 92 ) // '\' + { + ss << "\\\\"; + continue; + } + } + else + { + if ( v3 == 60 ) // '<' + { + ss << "\\x3c"; + continue; + } + if ( v3 == 61 ) // '=' + { + ss << "\\x3d"; + continue; + } + } + } + else + { + if ( v3 == 34 ) // '"' + { + ss << "\\x22"; + continue; + } + if ( v3 > 34 ) + { + if ( v3 == 38 ) // '&' + { + ss << "\\x26"; + continue; + } + } + else + { + if ( v3 == 10 ) // '\n' + { + ss << "\\n"; + continue; + } + if ( v3 == 13 ) + { + ss << "\\r"; + continue; + } + } + } + ss << v3; + } + return ss.str(); + } + std::string ToStringCode2(std::string const&src) + { + return ToStringCode2(src.data(), src.size()); + } + std::string ToStringCode3(char const* src, int len) + { + std::stringstream ss; + for (int i = 0; i < len; i++) + { + char v3 = src[i]; + if ( v3 == 61 ) // '=' + { + ss << "\\x3d"; + continue; + } + if ( v3 > 61 ) // '=' + { + if ( v3 == 62 ) // '>' + { + ss << "\\x3e"; + continue; + } + } + else + { + if ( v3 == 38 ) // '&' + { + ss << "\\x26"; + continue; + } + if ( v3 == 60 ) // '<' + { + ss << "\\x3c"; + continue; + } + } + ss << v3; + } + return ss.str(); + } + std::string ToStringCode3(std::string const& src) + { + return ToStringCode3(src.data(), src.size()); + } } } \ No newline at end of file diff --git a/test/wcc.disassembly.cpp b/test/wcc.disassembly.cpp index 581e5b6..c1ab06e 100644 --- a/test/wcc.disassembly.cpp +++ b/test/wcc.disassembly.cpp @@ -9483,21 +9483,21 @@ std::ostream::sentry *__cdecl WXML::Rewrite::ToStringCode(int a1, int a2, int a3 for ( i = 0; i < a2; ++i ) { v4 = *(_BYTE *)(a1 + i); - if ( v4 == 39 ) + if ( v4 == '\''/*39*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\x27"); continue; } - if ( v4 <= 39 ) + if ( v4 <= '\''/*39*/ ) { - if ( v4 == 34 ) + if ( v4 == '"'/*34*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\x22"); continue; } - if ( v4 > 34 ) + if ( v4 > '"'/*34*/ ) { - if ( v4 == 38 ) + if ( v4 == '&'/*38*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\x26"); continue; @@ -9505,12 +9505,12 @@ std::ostream::sentry *__cdecl WXML::Rewrite::ToStringCode(int a1, int a2, int a3 } else { - if ( v4 == 10 ) + if ( v4 == '\n'/*10*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\n"); continue; } - if ( v4 == 13 ) + if ( v4 == '\r'/*13*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\r"); continue; @@ -9518,19 +9518,19 @@ std::ostream::sentry *__cdecl WXML::Rewrite::ToStringCode(int a1, int a2, int a3 } goto LABEL_31; } - if ( v4 == 62 ) + if ( v4 == '>' /*62*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\x3e"); continue; } - if ( v4 <= 62 ) + if ( v4 <= '>' /*62*/ ) { - if ( v4 == 60 ) + if ( v4 == '<'/*60*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\x3c"); continue; } - if ( v4 == 61 ) + if ( v4 == '='/*61*/ ) { result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\x3d"); continue; @@ -9540,19 +9540,19 @@ LABEL_31: result = std::__ostream_insert>((std::ostream::sentry *)(a3 + 8), (int)&v7, 1); continue; } - if ( v4 != 92 ) + if ( v4 != '\\'/*92*/ ) goto LABEL_31; v6 = *(_BYTE *)(a1 + i + 1); - if ( (unsigned __int8)(v6 - 92) > 0x18u ) + if ( (unsigned __int8)(v6 - '\\'/*92*/) > 0x18u ) { - if ( v6 == 39 || v6 == 34 ) + if ( v6 == '\''/*39*/ || v6 == '"'/*34*/ ) { LABEL_29: result = std::operator<<>((std::ostream::sentry *)(a3 + 8), "\\"); continue; } } - else if ( ((0x1440001u >> (v6 - 92)) & 1) != 0 ) + else if ( ((0x1440001u >> (v6 - '\\'/*92*/)) & 1) != 0 ) { goto LABEL_29; }