diff --git a/acpa.exe.stackdump b/acpa.exe.stackdump new file mode 100644 index 0000000..d549587 --- /dev/null +++ b/acpa.exe.stackdump @@ -0,0 +1,58 @@ +Exception: STATUS_STACK_OVERFLOW at rip=000100418AEB +rax=00000007FFE0410F rbx=00000007FFE040E0 rcx=00000007FFE040F0 +rdx=00000007FFE0410F rsi=00000007FFE04108 rdi=0000000000000001 +r8 =00000007FFE0410F r9 =0000000000000001 r10=0000000800000000 +r11=000000010040D18C r12=00000007FFFFCC70 r13=00000007FFFFCDF0 +r14=0000000000000000 r15=0000000000000000 +rbp=00000007FFE04020 rsp=00000007FFE04000 +program=C:\Users\lcw\Desktop\code\acpa\acpa.exe, pid 724, thread +cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B +Stack trace: +Frame Function Args +0007FFE04020 000100418AEB (0007FFE0410F, 000000000000, 000000000000, 0007FFE040A0) acpa.exe+0x18AEB +0007FFE04050 000100418BD0 (0007FFE0410F, 000A001E79F0, 000000000001, 0007FFE04108) acpa.exe+0x18BD0 +0007FFE040A0 000100424A96 (000000000001, 0007FFE0410F, 0001004345DB, 0007FFE04108) acpa.exe+0x24A96 +0007FFE04110 000100403F0F (000A001E79F4, 000A001E79F0, 0007FFE0432C, 0007FFE0435C) acpa.exe+0x3F0F +0007FFE041C0 0001004067B1 (000A001E79F4, 000A001E79F4, 000100000007, 000100451110) acpa.exe+0x67B1 +0007FFE04370 00010040702E (000000000006, 000A001E79F0, 000A001E79F4, 000A001E79F4) acpa.exe+0x702E +0007FFE043D0 0001004070C7 (0007FFE045C0, 000A001E7980, 7FFA4F13C600, 0007FFE04440) acpa.exe+0x70C7 +0007FFE04470 000100405BF3 (000A001E79F4, 000A001E79F4, 000100000005, 000100451110) acpa.exe+0x5BF3 +0007FFE048A0 0001004062B2 (000A00000004, 000000000000, 000A001E79A0, 0007FFE04910) acpa.exe+0x62B2 +0007FFE04900 00010040634B (000A001E7980, 000A001E7910, 7FFA4F13C600, 0007FFE04970) acpa.exe+0x634B +0007FFE049A0 000100405D12 (000A001E7984, 000A001E7984, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE04DD0 0001004062B2 (000A00000004, 000000000000, 000A001E7930, 0007FFE04E40) acpa.exe+0x62B2 +0007FFE04E30 00010040634B (000A001E7910, 000A001E78A0, 7FFA4F13C600, 0007FFE04EA0) acpa.exe+0x634B +0007FFE04ED0 000100405D12 (000A001E7914, 000A001E7914, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE05300 0001004062B2 (000A00000004, 000000000000, 000A001E78C0, 0007FFE05370) acpa.exe+0x62B2 +0007FFE05360 00010040634B (000A001E78A0, 000A001E7830, 7FFA4F13C600, 0007FFE053D0) acpa.exe+0x634B +0007FFE05400 000100405D12 (000A001E78A4, 000A001E78A4, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE05830 0001004062B2 (000A00000004, 000000000000, 000A001E7850, 0007FFE058A0) acpa.exe+0x62B2 +0007FFE05890 00010040634B (000A001E7830, 000A001E77C0, 7FFA4F13C600, 0007FFE05900) acpa.exe+0x634B +0007FFE05930 000100405D12 (000A001E7834, 000A001E7834, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE05D60 0001004062B2 (000A00000004, 000000000000, 000A001E77E0, 0007FFE05DD0) acpa.exe+0x62B2 +0007FFE05DC0 00010040634B (000A001E77C0, 000A001E7750, 7FFA4F13C600, 0007FFE05E30) acpa.exe+0x634B +0007FFE05E60 000100405D12 (000A001E77C4, 000A001E77C4, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE06290 0001004062B2 (000A00000004, 000000000000, 000A001E7770, 0007FFE06300) acpa.exe+0x62B2 +0007FFE062F0 00010040634B (000A001E7750, 000A001E76E0, 7FFA4F13C600, 0007FFE06360) acpa.exe+0x634B +0007FFE06390 000100405D12 (000A001E7754, 000A001E7754, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE067C0 0001004062B2 (000A00000004, 000000000000, 000A001E7700, 0007FFE06830) acpa.exe+0x62B2 +0007FFE06820 00010040634B (000A001E76E0, 000A001E7670, 7FFA4F13C600, 0007FFE06890) acpa.exe+0x634B +0007FFE068C0 000100405D12 (000A001E76E4, 000A001E76E4, 000100000005, 000100451110) acpa.exe+0x5D12 +0007FFE06CF0 0001004062B2 (000A00000004, 000000000000, 000A001E7690, 0007FFE06D60) acpa.exe+0x62B2 +0007FFE06D50 00010040634B (000A001E7670, 000A001E7600, 7FFA4F13C600, 0007FFE06DC0) acpa.exe+0x634B +0007FFE06DF0 000100405D12 (000A001E7674, 000A001E7674, 000100000005, 000100451110) acpa.exe+0x5D12 +End of stack trace (more stack frames may be present) +Loaded modules: +000100400000 acpa.exe +7FFAE2E50000 ntdll.dll +7FFAE14F0000 KERNEL32.DLL +7FFAE0420000 KERNELBASE.dll +7FFA4EF10000 cygwin1.dll +0003FB660000 cygstdc++-6.dll +0003FF100000 cyggcc_s-seh-1.dll +7FFAE2370000 advapi32.dll +7FFAE1BB0000 msvcrt.dll +7FFAE0F80000 sechost.dll +7FFAE1640000 RPCRT4.dll +7FFADF8A0000 CRYPTBASE.DLL +7FFAE0A60000 bcryptPrimitives.dll diff --git a/include/element.h b/include/element.h index ce110f6..d5a48dc 100644 --- a/include/element.h +++ b/include/element.h @@ -2,11 +2,15 @@ #define ACPA_ELEMENT_H #include +#include +#include #include #include #include using std::map; +using std::set; +using std::pair; using std::shared_ptr; using std::string; using std::vector; @@ -27,27 +31,46 @@ struct Struct { }; struct ValType { - virtual int type() const = 0; + enum Type { + TEMPLATE, + STRUCT, + FUNCTION + }; + virtual ValType::Type type() const = 0; + virtual shared_ptr struct_replace(shared_ptr, map, shared_ptr>&) = 0; + virtual shared_ptr function_replace(const map, shared_ptr>&, map, shared_ptr>&) = 0; + virtual bool sameType(shared_ptr, set, shared_ptr>>&) = 0; virtual ~ValType() = default; }; struct TemplateType : ValType { - int type() const override; + ValType::Type type() const override; + shared_ptr struct_replace(shared_ptr, map, shared_ptr>&) override; + shared_ptr function_replace(const map, shared_ptr>&, map, shared_ptr>&) override; + bool sameType(shared_ptr, set, shared_ptr>>&) override; }; struct StructType : ValType { - int type() const override; + ValType::Type type() const override; + shared_ptr struct_replace(shared_ptr, map, shared_ptr>&) override; + shared_ptr function_replace(const map, shared_ptr>&, map, shared_ptr>&) override; + bool sameType(shared_ptr, set, shared_ptr>>&) override; shared_ptr str; map, shared_ptr> mp; }; struct FunctionType : ValType { - int type() const override; + ValType::Type type() const override; + shared_ptr struct_replace(shared_ptr, map, shared_ptr>&) override; + shared_ptr function_replace(const map, shared_ptr>&, map, shared_ptr>&) override; + bool sameType(shared_ptr, set, shared_ptr>>&) override; vector> c1; vector> c2; shared_ptr c3; }; +shared_ptr struct_replace(shared_ptr, shared_ptr); +shared_ptr function_replace(shared_ptr, const map, shared_ptr>&); bool sameType(shared_ptr, shared_ptr); #endif \ No newline at end of file diff --git a/src/element.cpp b/src/element.cpp index d82b7c1..2d8fc84 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -1,19 +1,158 @@ #include "element.h" #include -#include using namespace std; -int TemplateType::type() const { - return 0; +ValType::Type TemplateType::type() const { + return ValType::TEMPLATE; } -int StructType::type() const { - return 1; +ValType::Type StructType::type() const { + return ValType::STRUCT; } -int FunctionType::type() const { - return 2; +ValType::Type FunctionType::type() const { + return ValType::FUNCTION; +} + +shared_ptr TemplateType::struct_replace(shared_ptr ut, map, shared_ptr> &eq) { + auto vt = shared_ptr(this); + auto it1 = ut->mp.find(vt); + auto it2 = eq.find(vt); + assert(it1 == ut->mp.end() || it2 == eq.end()); + if (it1 != ut->mp.end()) { + return it1->second; + } + if (it2 != eq.end()) { + return static_pointer_cast(it2->second); + } + return static_pointer_cast(vt); +} + +shared_ptr StructType::struct_replace(shared_ptr ut, map, shared_ptr> &eq) { + auto tt = make_shared(); + (*tt) = (*ut); + set> vis; + for (auto u = str; u.get() != nullptr; u = u->fa.lock()) { + vis.insert(u); + } + for (auto u = ut->str; u.get() != nullptr && vis.find(u) == vis.end(); + u = u->fa.lock()) { + for (auto tem : u->c1) { + tt->mp.erase(tem); + } + } + for (auto pr : mp) { + tt->mp[pr.first] = pr.second->struct_replace(ut, eq); + } + return static_pointer_cast(tt); +} + +shared_ptr FunctionType::struct_replace(shared_ptr ut, map, shared_ptr> &eq) { + auto tt = make_shared(); + for (size_t i = 0; i < c1.size(); i++) { + tt->c1.push_back(eq[c1[i]] = make_shared()); + } + for (auto cc : c2) { + tt->c2.push_back(cc->struct_replace(ut, eq)); + } + tt->c3 = c3->struct_replace(ut, eq); + for (size_t i = 0; i < c1.size(); i++) { + eq.erase(c1[i]); + } + return static_pointer_cast(tt); +} + +shared_ptr struct_replace(shared_ptr vt, shared_ptr ut) { + static map, shared_ptr> eq; + return vt->struct_replace(ut,eq); +} + +shared_ptr TemplateType::function_replace(const map, shared_ptr> &mp, map, shared_ptr> &eq) { + auto tt = shared_ptr(this); + auto it1 = mp.find(tt); + auto it2 = eq.find(tt); + assert(it1 == mp.end() || it2 == eq.end()); + if (it1 != mp.end()) { + return it1->second; + } + if (it2 != eq.end()) { + return static_pointer_cast(it2->second); + } + return static_pointer_cast(tt); +} + +shared_ptr StructType::function_replace(const map, shared_ptr> &mp, map, shared_ptr> &eq) { + auto tt = make_shared(); + (*tt) = (*this); + for (auto& pr : tt->mp) { + pr.second = pr.second->function_replace(mp, eq); + } + return tt; +} + +shared_ptr FunctionType::function_replace(const map, shared_ptr> &mp, map, shared_ptr> &eq) { + auto tt = make_shared(); + for (size_t i = 0; i < c1.size(); i++) { + tt->c1.push_back(eq[c1[i]] = make_shared()); + } + for (auto cc : c2) { + tt->c2.push_back(cc->function_replace(mp, eq)); + } + tt->c3 = c3->function_replace(mp, eq); + for (size_t i = 0; i < c1.size(); i++) { + eq.erase(c1[i]); + } + return static_pointer_cast(tt); +} + +shared_ptr function_replace(shared_ptr t, const map, shared_ptr>& mp) { + static map, shared_ptr> eq; + return t->function_replace(mp, eq); +} + +bool TemplateType::sameType(shared_ptr b, set, shared_ptr>> &eq) { + auto aa = shared_ptr(this), bb = static_pointer_cast(b); + return aa == bb || eq.find({aa, bb}) != eq.end(); +} + +bool StructType::sameType(shared_ptr b, set, shared_ptr>> &eq) { + auto bb = static_pointer_cast(b); + if (str != bb->str) { + return false; + } + assert(mp.size() == bb->mp.size()); + for (auto ia = mp.begin(), ib = bb->mp.begin(); ia != mp.end(); ia++, ib++) { + assert(ia->first == ib->first); + if (!ia->second->sameType(ib->second, eq)) { + return false; + } + } + return true; +} + +bool FunctionType::sameType(shared_ptr b, set, shared_ptr>> &eq) { + auto bb = static_pointer_cast(b); + if (c1.size() != bb->c1.size() || c2.size() != bb->c2.size()) { + return false; + } + for (size_t i = 0; i < c1.size(); i++) { + eq.insert({c1[i], bb->c1[i]}); + } + bool flag = 1; + for (size_t i = 0; i < c2.size(); i++) { + if (!c2[i]->sameType(bb->c2[i], eq)) { + flag = 0; + break; + } + } + if (!flag && !c3->sameType(bb->c3, eq)) { + flag = 0; + } + for (size_t i = 0; i < c1.size(); i++) { + eq.erase({c1[i], bb->c1[i]}); + } + return flag; } bool sameType(shared_ptr a, shared_ptr b) { @@ -21,50 +160,5 @@ bool sameType(shared_ptr a, shared_ptr b) { if (a->type() != b->type()) { return false; } - if (a->type() == 0) { - auto aa = static_pointer_cast(a), - bb = static_pointer_cast(b); - return aa == bb || eq.find({aa, bb}) != eq.end(); - } - if (a->type() == 1) { - auto aa = static_pointer_cast(a); - auto bb = static_pointer_cast(b); - if (aa->str != bb->str) { - return false; - } - assert(aa->mp.size() == bb->mp.size()); - for (auto ia = aa->mp.begin(), ib = bb->mp.begin(); ia != aa->mp.end(); - ia++, ib++) { - assert(ia->first == ib->first); - if (!sameType(ia->second, ib->second)) { - return false; - } - } - return true; - } - // if(a->type()==2) - { - auto aa = static_pointer_cast(a); - auto bb = static_pointer_cast(b); - if (aa->c1.size() != bb->c1.size() || aa->c2.size() != bb->c2.size()) { - return false; - } - for (size_t i = 0; i < aa->c1.size(); i++) { - eq.insert({aa->c1[i], bb->c1[i]}); - } - bool flag = 1; - for (size_t i = 0; i < aa->c2.size(); i++) { - if (!sameType(aa->c2[i], bb->c2[i])) { - flag = 0; - break; - } - } - if (!flag && !sameType(aa->c3, bb->c3)) { - flag = 0; - } - for (size_t i = 0; i < aa->c1.size(); i++) { - eq.erase({aa->c1[i], bb->c1[i]}); - } - return flag; - } + return a->sameType(b, eq); } \ No newline at end of file diff --git a/src/work.cpp b/src/work.cpp index 56f995e..095ca69 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -1,6 +1,5 @@ #include "work.h" #include -#include using namespace std; @@ -18,27 +17,32 @@ vector tokens; // } struct Def { - virtual int type() const = 0; + enum Type { + TEMPLATE, + STRUCT, + VAR + }; + virtual Def::Type type() const = 0; virtual ~Def() = default; }; struct DefTemplate : Def { - int type() const override { - return 0; + Def::Type type() const override { + return Def::TEMPLATE; } shared_ptr def_template; }; struct DefStruct : Def { - int type() const override { - return 1; + Def::Type type() const override { + return Def::STRUCT; } shared_ptr def_struct; }; struct DefVar : Def { - int type() const override { - return 2; + Def::Type type() const override { + return Def::VAR; } shared_ptr def_var; }; @@ -82,101 +86,6 @@ vector>> createPars(); pair> createStruct(); pair> createVar(); -shared_ptr struct_replace(shared_ptr vt, shared_ptr ut) { - static map, shared_ptr> eq; - if (vt->type() == 0) { - auto vtt = static_pointer_cast(vt); - auto it1 = ut->mp.find(vtt); - auto it2 = eq.find(vtt); - assert(it1 == ut->mp.end() || it2 == eq.end()); - if (it1 != ut->mp.end()) { - return it1->second; - } - if (it2 != eq.end()) { - return static_pointer_cast(it2->second); - } - return vt; - } - if (vt->type() == 1) { - auto vtt = static_pointer_cast(vt); - auto tt = make_shared(); - (*tt) = (*ut); - set> vis; - for (auto u = vtt->str; u.get() != nullptr; u = u->fa.lock()) { - vis.insert(u); - } - for (auto u = ut->str; u.get() != nullptr && vis.find(u) == vis.end(); - u = u->fa.lock()) { - for (auto tem : u->c1) { - tt->mp.erase(tem); - } - } - for (auto pr : vtt->mp) { - tt->mp[pr.first] = struct_replace(pr.second, ut); - } - return static_pointer_cast(tt); - } - // if(vt->type()==2) - { - auto vtt = static_pointer_cast(vt); - auto tt = make_shared(); - for (size_t i = 0; i < vtt->c1.size(); i++) { - tt->c1.push_back(eq[vtt->c1[i]] = make_shared()); - } - for (auto cc : vtt->c2) { - tt->c2.push_back(struct_replace(cc, ut)); - } - tt->c3 = struct_replace(vtt->c3, ut); - for (size_t i = 0; i < vtt->c1.size(); i++) { - eq.erase(vtt->c1[i]); - } - return static_pointer_cast(tt); - } -} - -shared_ptr function_replace( - shared_ptr t, - const map, shared_ptr>& mp) { - static map, shared_ptr> eq; - if (t->type() == 0) { - auto tt = static_pointer_cast(t); - auto it1 = mp.find(tt); - auto it2 = eq.find(tt); - assert(it1 == mp.end() || it2 == eq.end()); - if (it1 != mp.end()) { - return it1->second; - } - if (it2 != eq.end()) { - return static_pointer_cast(it2->second); - } - return t; - } - if (t->type() == 1) { - auto tt = make_shared(); - (*tt) = (*static_pointer_cast(t)); - for (auto& pr : static_pointer_cast(tt)->mp) { - pr.second = function_replace(pr.second, mp); - } - return tt; - } - // if(t->type()==2) - { - auto vt = static_pointer_cast(t); - auto tt = make_shared(); - for (size_t i = 0; i < vt->c1.size(); i++) { - tt->c1.push_back(eq[vt->c1[i]] = make_shared()); - } - for (auto cc : vt->c2) { - tt->c2.push_back(function_replace(cc, mp)); - } - tt->c3 = function_replace(vt->c3, mp); - for (size_t i = 0; i < vt->c1.size(); i++) { - eq.erase(vt->c1[i]); - } - return static_pointer_cast(tt); - } -} - shared_ptr _createVal(shared_ptr ft) { auto str = ft->str; auto s = jump(TokenType::ID); @@ -184,7 +93,8 @@ shared_ptr _createVal(shared_ptr ft) { printf("error on line %d", tokens[pt - 1].line), exit(0); } auto t = struct_replace(str->vars[s], ft); - while (t->type() == 2 && (preview(TokenType::LT) || preview(TokenType::LP))) { + while (t->type() == ValType::FUNCTION + && (preview(TokenType::LT) || preview(TokenType::LP))) { auto tt = static_pointer_cast(t); vector> types; if (preview(TokenType::LT)) { @@ -215,7 +125,7 @@ shared_ptr _createVal(shared_ptr ft) { if (!preview(TokenType::DOT)) { return t; } - if (t->type() != 1) { + if (t->type() != ValType::STRUCT) { printf("error on line %d", tokens[pt].line), exit(0); } pt++; @@ -275,14 +185,14 @@ shared_ptr createVal() { flag = 1; } else if (preview(TokenType::ID)) { auto s = tokens[pt].s; - if (ndefs.find(s) != ndefs.end() && ndefs[s]->type() == 1) { + if (ndefs.find(s) != ndefs.end() && ndefs[s]->type() == Def::STRUCT) { flag = 1; } } if (flag) { t = createType(); - if (t->type() != 1) { + if (t->type() != ValType::STRUCT) { printf("error on line %d", tokens[pt - 1].line), exit(0); } auto tt = static_pointer_cast(t); @@ -302,13 +212,14 @@ shared_ptr createVal() { } } else { auto s = jump(TokenType::ID); - if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() != 2) { + if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() != Def::VAR) { printf("error on line %d", tokens[pt - 1].line), exit(0); } t = static_pointer_cast(ndefs[s])->def_var; } } - while (t->type() == 2 && (preview(TokenType::LT) || preview(TokenType::LP))) { + while (t->type() == ValType::FUNCTION + && (preview(TokenType::LT) || preview(TokenType::LP))) { auto tt = static_pointer_cast(t); vector> types; if (preview(TokenType::LT)) { @@ -339,7 +250,7 @@ shared_ptr createVal() { if (!preview(TokenType::DOT)) { return t; } - if (t->type() != 1) { + if (t->type() != ValType::STRUCT) { printf("error on line %d", tokens[pt].line), exit(0); } pt++; @@ -351,9 +262,7 @@ vector> createVals() { jump(TokenType::LP); if (!preview(TokenType::RP)) { auto single = [&]() { vals.push_back(createVal()); }; - for (single(); preview(TokenType::COMMA); pt++, single()) { - ; - } + for (single(); preview(TokenType::COMMA); pt++, single()) {} } jump(TokenType::RP); return vals; @@ -411,10 +320,10 @@ shared_ptr createType() { jump(TokenType::RP); } else { auto s = jump(TokenType::ID); - if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() == 2) { + if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() == Def::VAR) { printf("error on line %d", tokens[pt - 1].line), exit(0); } - if (ndefs[s]->type() == 1) { + if (ndefs[s]->type() == Def::STRUCT) { auto tt = make_shared(); tt->str = static_pointer_cast(ndefs[s])->def_struct; vector> types; @@ -436,7 +345,7 @@ shared_ptr createType() { if (!preview(TokenType::SCOPE)) { return t; } - if (t->type() != 1) { + if (t->type() != ValType::STRUCT) { printf("error on line %d", tokens[pt].line), exit(0); } pt++; @@ -448,9 +357,7 @@ vector> createTypes() { jump(TokenType::LT); if (!preview(TokenType::RT)) { auto single = [&]() { types.push_back(createType()); }; - for (single(); preview(TokenType::COMMA); pt++, single()) { - ; - } + for (single(); preview(TokenType::COMMA); pt++, single()) {} } jump(TokenType::RT); return types; @@ -471,9 +378,7 @@ vector>> createTems() { ndefs[s] = static_pointer_cast(d); tems.push_back({s, x}); }; - for (single(); preview(TokenType::COMMA); pt++, single()) { - ; - } + for (single(); preview(TokenType::COMMA); pt++, single()) {} } jump(TokenType::RT); return tems; @@ -495,9 +400,7 @@ vector>> createPars() { ndefs[s] = static_pointer_cast(d); pars.push_back({s, t}); }; - for (single(); preview(TokenType::COMMA); pt++, single()) { - ; - } + for (single(); preview(TokenType::COMMA); pt++, single()) {} } jump(TokenType::RP); return pars;