improve re check

This commit is contained in:
方而静 2023-08-30 09:51:05 +08:00
parent 7c43815968
commit d49a1ae9d2
Signed by: szTom
GPG Key ID: 072D999D60C6473C
4 changed files with 92 additions and 33 deletions

View File

@ -2,15 +2,15 @@
#define ACPA_ELEMENT_H #define ACPA_ELEMENT_H
#include <map> #include <map>
#include <set>
#include <utility>
#include <memory> #include <memory>
#include <set>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
using std::map; using std::map;
using std::set;
using std::pair; using std::pair;
using std::set;
using std::shared_ptr; using std::shared_ptr;
using std::string; using std::string;
using std::vector; using std::vector;
@ -37,40 +37,70 @@ struct ValType {
FUNCTION FUNCTION
}; };
virtual ValType::Type type() const = 0; virtual ValType::Type type() const = 0;
virtual shared_ptr<ValType> struct_replace(shared_ptr<StructType>, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) = 0; virtual shared_ptr<ValType> struct_replace(
virtual shared_ptr<ValType> function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) = 0; shared_ptr<StructType>,
virtual bool sameType(shared_ptr<ValType>, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) = 0; map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&)
= 0;
virtual shared_ptr<ValType> function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&)
= 0;
virtual bool sameType(shared_ptr<ValType>,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&)
= 0;
virtual ~ValType() = default; virtual ~ValType() = default;
}; };
struct TemplateType : ValType, std::enable_shared_from_this<TemplateType> { struct TemplateType
: ValType
, std::enable_shared_from_this<TemplateType> {
ValType::Type type() const override; ValType::Type type() const override;
shared_ptr<ValType> struct_replace(shared_ptr<StructType>, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override; shared_ptr<ValType> struct_replace(
shared_ptr<ValType> function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override; shared_ptr<StructType>,
bool sameType(shared_ptr<ValType>, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) override; map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override;
shared_ptr<ValType> function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override;
bool sameType(shared_ptr<ValType>,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) override;
}; };
struct StructType : ValType, std::enable_shared_from_this<StructType> { struct StructType
: ValType
, std::enable_shared_from_this<StructType> {
ValType::Type type() const override; ValType::Type type() const override;
shared_ptr<ValType> struct_replace(shared_ptr<StructType>, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override; shared_ptr<ValType> struct_replace(
shared_ptr<ValType> function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override; shared_ptr<StructType>,
bool sameType(shared_ptr<ValType>, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) override; map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override;
shared_ptr<ValType> function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override;
bool sameType(shared_ptr<ValType>,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) override;
shared_ptr<Struct> str; shared_ptr<Struct> str;
map<shared_ptr<TemplateType>, shared_ptr<ValType>> mp; map<shared_ptr<TemplateType>, shared_ptr<ValType>> mp;
}; };
struct FunctionType : ValType, std::enable_shared_from_this<FunctionType> { struct FunctionType
: ValType
, std::enable_shared_from_this<FunctionType> {
ValType::Type type() const override; ValType::Type type() const override;
shared_ptr<ValType> struct_replace(shared_ptr<StructType>, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override; shared_ptr<ValType> struct_replace(
shared_ptr<ValType> function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override; shared_ptr<StructType>,
bool sameType(shared_ptr<ValType>, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) override; map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override;
shared_ptr<ValType> function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>&) override;
bool sameType(shared_ptr<ValType>,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>&) override;
vector<shared_ptr<TemplateType>> c1; vector<shared_ptr<TemplateType>> c1;
vector<shared_ptr<ValType>> c2; vector<shared_ptr<ValType>> c2;
shared_ptr<ValType> c3; shared_ptr<ValType> c3;
}; };
shared_ptr<ValType> struct_replace(shared_ptr<ValType>, shared_ptr<StructType>); shared_ptr<ValType> struct_replace(shared_ptr<ValType>, shared_ptr<StructType>);
shared_ptr<ValType> function_replace(shared_ptr<ValType>, const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&); shared_ptr<ValType> function_replace(shared_ptr<ValType>,
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>&);
bool sameType(shared_ptr<ValType>, shared_ptr<ValType>); bool sameType(shared_ptr<ValType>, shared_ptr<ValType>);
#endif #endif

View File

@ -23,7 +23,7 @@ def test_compiler(test_dir, should_fail):
result = subprocess.run(['./acpa', file_path], capture_output=True, text=True) result = subprocess.run(['./acpa', file_path], capture_output=True, text=True)
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
if result.returncode != 0: if 'runtime error' in result.stderr or result.returncode not in [0, 1]:
print(f' {Color.RED}测试失败:{Color.RESET} 在编译 {file_path} 时发生运行时错误') print(f' {Color.RED}测试失败:{Color.RESET} 在编译 {file_path} 时发生运行时错误')
has_failed_tests = True has_failed_tests = True
continue continue

View File

@ -15,7 +15,9 @@ ValType::Type FunctionType::type() const {
return ValType::FUNCTION; return ValType::FUNCTION;
} }
shared_ptr<ValType> TemplateType::struct_replace(shared_ptr<StructType> ut, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> &eq) { shared_ptr<ValType> TemplateType::struct_replace(
shared_ptr<StructType> ut,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>& eq) {
auto vt = shared_from_this(); auto vt = shared_from_this();
auto it1 = ut->mp.find(vt); auto it1 = ut->mp.find(vt);
auto it2 = eq.find(vt); auto it2 = eq.find(vt);
@ -29,15 +31,16 @@ shared_ptr<ValType> TemplateType::struct_replace(shared_ptr<StructType> ut, map<
return static_pointer_cast<ValType>(vt); return static_pointer_cast<ValType>(vt);
} }
shared_ptr<ValType> StructType::struct_replace(shared_ptr<StructType> ut, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> &eq) { shared_ptr<ValType> StructType::struct_replace(
shared_ptr<StructType> ut,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>& eq) {
auto tt = make_shared<StructType>(); auto tt = make_shared<StructType>();
(*tt) = (*ut); (*tt) = (*ut);
set<shared_ptr<Struct>> vis; set<shared_ptr<Struct>> vis;
for (auto u = str; u.get() != nullptr; u = u->fa.lock()) { for (auto u = str; u.get() != nullptr; u = u->fa.lock()) {
vis.insert(u); vis.insert(u);
} }
for (auto u = ut->str; u.get() != nullptr && vis.find(u) == vis.end(); for (auto u = ut->str; u.get() != nullptr && vis.find(u) == vis.end(); u = u->fa.lock()) {
u = u->fa.lock()) {
for (auto tem : u->c1) { for (auto tem : u->c1) {
tt->mp.erase(tem); tt->mp.erase(tem);
} }
@ -48,7 +51,9 @@ shared_ptr<ValType> StructType::struct_replace(shared_ptr<StructType> ut, map<sh
return static_pointer_cast<ValType>(tt); return static_pointer_cast<ValType>(tt);
} }
shared_ptr<ValType> FunctionType::struct_replace(shared_ptr<StructType> ut, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> &eq) { shared_ptr<ValType> FunctionType::struct_replace(
shared_ptr<StructType> ut,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>& eq) {
auto tt = make_shared<FunctionType>(); auto tt = make_shared<FunctionType>();
for (size_t i = 0; i < c1.size(); i++) { for (size_t i = 0; i < c1.size(); i++) {
tt->c1.push_back(eq[c1[i]] = make_shared<TemplateType>()); tt->c1.push_back(eq[c1[i]] = make_shared<TemplateType>());
@ -65,10 +70,12 @@ shared_ptr<ValType> FunctionType::struct_replace(shared_ptr<StructType> ut, map<
shared_ptr<ValType> struct_replace(shared_ptr<ValType> vt, shared_ptr<StructType> ut) { shared_ptr<ValType> struct_replace(shared_ptr<ValType> vt, shared_ptr<StructType> ut) {
static map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> eq; static map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> eq;
return vt->struct_replace(ut,eq); return vt->struct_replace(ut, eq);
} }
shared_ptr<ValType> TemplateType::function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>> &mp, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> &eq) { shared_ptr<ValType> TemplateType::function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>& mp,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>& eq) {
auto tt = shared_from_this(); auto tt = shared_from_this();
auto it1 = mp.find(tt); auto it1 = mp.find(tt);
auto it2 = eq.find(tt); auto it2 = eq.find(tt);
@ -82,7 +89,9 @@ shared_ptr<ValType> TemplateType::function_replace(const map<shared_ptr<Template
return static_pointer_cast<ValType>(tt); return static_pointer_cast<ValType>(tt);
} }
shared_ptr<ValType> StructType::function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>> &mp, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> &eq) { shared_ptr<ValType> StructType::function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>& mp,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>& eq) {
auto tt = make_shared<StructType>(); auto tt = make_shared<StructType>();
(*tt) = (*this); (*tt) = (*this);
for (auto& pr : tt->mp) { for (auto& pr : tt->mp) {
@ -91,7 +100,9 @@ shared_ptr<ValType> StructType::function_replace(const map<shared_ptr<TemplateTy
return tt; return tt;
} }
shared_ptr<ValType> FunctionType::function_replace(const map<shared_ptr<TemplateType>, shared_ptr<ValType>> &mp, map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> &eq) { shared_ptr<ValType> FunctionType::function_replace(
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>& mp,
map<shared_ptr<TemplateType>, shared_ptr<TemplateType>>& eq) {
auto tt = make_shared<FunctionType>(); auto tt = make_shared<FunctionType>();
for (size_t i = 0; i < c1.size(); i++) { for (size_t i = 0; i < c1.size(); i++) {
tt->c1.push_back(eq[c1[i]] = make_shared<TemplateType>()); tt->c1.push_back(eq[c1[i]] = make_shared<TemplateType>());
@ -106,17 +117,21 @@ shared_ptr<ValType> FunctionType::function_replace(const map<shared_ptr<Template
return static_pointer_cast<ValType>(tt); return static_pointer_cast<ValType>(tt);
} }
shared_ptr<ValType> function_replace(shared_ptr<ValType> t, const map<shared_ptr<TemplateType>, shared_ptr<ValType>>& mp) { shared_ptr<ValType> function_replace(
shared_ptr<ValType> t,
const map<shared_ptr<TemplateType>, shared_ptr<ValType>>& mp) {
static map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> eq; static map<shared_ptr<TemplateType>, shared_ptr<TemplateType>> eq;
return t->function_replace(mp, eq); return t->function_replace(mp, eq);
} }
bool TemplateType::sameType(shared_ptr<ValType> b, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>> &eq) { bool TemplateType::sameType(shared_ptr<ValType> b,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>& eq) {
auto aa = shared_from_this(), bb = static_pointer_cast<TemplateType>(b); auto aa = shared_from_this(), bb = static_pointer_cast<TemplateType>(b);
return aa == bb || eq.find({aa, bb}) != eq.end(); return aa == bb || eq.find({aa, bb}) != eq.end();
} }
bool StructType::sameType(shared_ptr<ValType> b, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>> &eq) { bool StructType::sameType(shared_ptr<ValType> b,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>& eq) {
auto bb = static_pointer_cast<StructType>(b); auto bb = static_pointer_cast<StructType>(b);
if (str != bb->str) { if (str != bb->str) {
return false; return false;
@ -131,7 +146,8 @@ bool StructType::sameType(shared_ptr<ValType> b, set<pair<shared_ptr<TemplateTyp
return true; return true;
} }
bool FunctionType::sameType(shared_ptr<ValType> b, set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>> &eq) { bool FunctionType::sameType(shared_ptr<ValType> b,
set<pair<shared_ptr<TemplateType>, shared_ptr<TemplateType>>>& eq) {
auto bb = static_pointer_cast<FunctionType>(b); auto bb = static_pointer_cast<FunctionType>(b);
if (c1.size() != bb->c1.size() || c2.size() != bb->c2.size()) { if (c1.size() != bb->c1.size() || c2.size() != bb->c2.size()) {
return false; return false;

View File

@ -4,10 +4,23 @@
#include "token.h" #include "token.h"
#include "work.h" #include "work.h"
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include <csignal>
using namespace std; using namespace std;
void signal_handler(int signal) {
std::cerr << "runtime error, signal: " << signal << std::endl;
std::exit(signal);
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::signal(SIGSEGV, signal_handler); // 捕获段错误,如空指针异常
std::signal(SIGABRT, signal_handler); // 捕获异常终止,如释放后使用
std::signal(SIGFPE, signal_handler); // 捕获浮点异常
std::signal(SIGILL, signal_handler); // 捕获非法指令
std::signal(SIGINT, signal_handler); // 捕获中断信号
std::signal(SIGTERM, signal_handler); // 捕获终止信号
argparse::ArgumentParser program("acpa", argparse::ArgumentParser program("acpa",
"0.1a0", "0.1a0",
argparse::default_arguments::help, argparse::default_arguments::help,