将错误信息输出到标准异常流 (#4)
This commit is contained in:
parent
8f01835cab
commit
c263344c66
@ -23,20 +23,20 @@ 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 'runtime error' in result.stderr or result.returncode not in [0, 1]:
|
if 'runtime error' in result.stderr or result.returncode != 0:
|
||||||
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
|
||||||
|
|
||||||
if should_fail:
|
if should_fail:
|
||||||
if 'error' not in result.stdout:
|
if 'error' not in result.stderr:
|
||||||
print(f' {Color.RED}测试失败:{Color.RESET} {file_path} 应该编译失败,但是没有发现错误')
|
print(f' {Color.RED}测试失败:{Color.RESET} {file_path} 应该编译失败,但是没有发现错误')
|
||||||
has_failed_tests = True
|
has_failed_tests = True
|
||||||
else:
|
else:
|
||||||
print(f' {Color.GREEN}测试通过:{Color.RESET} {file_path}')
|
print(f' {Color.GREEN}测试通过:{Color.RESET} {file_path}')
|
||||||
passed_files += 1
|
passed_files += 1
|
||||||
else:
|
else:
|
||||||
if 'error' in result.stdout:
|
if 'error' in result.stderr:
|
||||||
print(f' {Color.RED}测试失败:{Color.RESET} {file_path} 应该编译通过,但是发现错误。')
|
print(f' {Color.RED}测试失败:{Color.RESET} {file_path} 应该编译通过,但是发现错误。')
|
||||||
has_failed_tests = True
|
has_failed_tests = True
|
||||||
else:
|
else:
|
||||||
|
@ -50,14 +50,14 @@ vector<Token> scan(string s) {
|
|||||||
if (pt + 1 < s.size() && s[pt + 1] == '>') {
|
if (pt + 1 < s.size() && s[pt + 1] == '>') {
|
||||||
type = TokenType::IMPLY, pt += 2;
|
type = TokenType::IMPLY, pt += 2;
|
||||||
} else {
|
} else {
|
||||||
printf("error on line %d", line), exit(1);
|
fprintf(stderr, "error on line %d", line), exit(0);
|
||||||
}
|
}
|
||||||
} else if (s[pt] == '/') {
|
} else if (s[pt] == '/') {
|
||||||
if (pt + 1 < s.size() && s[pt + 1] == '/') {
|
if (pt + 1 < s.size() && s[pt + 1] == '/') {
|
||||||
for (pt += 2; pt < s.size() && s[pt] != '\n'; pt++) {}
|
for (pt += 2; pt < s.size() && s[pt] != '\n'; pt++) {}
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
printf("error on line %d", line), exit(1);
|
fprintf(stderr, "error on line %d", line), exit(0);
|
||||||
}
|
}
|
||||||
} else if (isalpha(s[pt]) || s[pt] == '_') {
|
} else if (isalpha(s[pt]) || s[pt] == '_') {
|
||||||
size_t r = pt + 1;
|
size_t r = pt + 1;
|
||||||
@ -89,7 +89,7 @@ vector<Token> scan(string s) {
|
|||||||
}
|
}
|
||||||
pt = r;
|
pt = r;
|
||||||
} else {
|
} else {
|
||||||
printf("error on line %d", line), exit(1);
|
fprintf(stderr, "error on line %d", line), exit(0);
|
||||||
}
|
}
|
||||||
tokens.push_back({line, type, id});
|
tokens.push_back({line, type, id});
|
||||||
}
|
}
|
||||||
|
44
src/work.cpp
44
src/work.cpp
@ -68,11 +68,11 @@ int jump(TokenType t) {
|
|||||||
return tokens[pt++].s;
|
return tokens[pt++].s;
|
||||||
}
|
}
|
||||||
if (pt < tokens.size()) {
|
if (pt < tokens.size()) {
|
||||||
printf("error on line %d", tokens[pt].line);
|
fprintf(stderr, "error on line %d", tokens[pt].line);
|
||||||
} else {
|
} else {
|
||||||
puts("error on end");
|
fprintf(stderr, "error on end");
|
||||||
}
|
}
|
||||||
exit(1);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<ValType> _createVal(shared_ptr<StructType> t);
|
shared_ptr<ValType> _createVal(shared_ptr<StructType> t);
|
||||||
@ -90,7 +90,7 @@ shared_ptr<ValType> _createVal(shared_ptr<StructType> ft) {
|
|||||||
auto str = ft->str;
|
auto str = ft->str;
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (str->vars.find(s) == str->vars.end()) {
|
if (str->vars.find(s) == str->vars.end()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
auto t = struct_replace(str->vars[s], ft);
|
auto t = struct_replace(str->vars[s], ft);
|
||||||
while (t->type() == ValType::FUNCTION
|
while (t->type() == ValType::FUNCTION
|
||||||
@ -101,7 +101,7 @@ shared_ptr<ValType> _createVal(shared_ptr<StructType> ft) {
|
|||||||
types = createTypes();
|
types = createTypes();
|
||||||
}
|
}
|
||||||
if (tt->c1.size() != types.size()) {
|
if (tt->c1.size() != types.size()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
map<shared_ptr<TemplateType>, shared_ptr<ValType>> mp;
|
map<shared_ptr<TemplateType>, shared_ptr<ValType>> mp;
|
||||||
for (size_t i = 0; i < types.size(); i++) {
|
for (size_t i = 0; i < types.size(); i++) {
|
||||||
@ -118,7 +118,7 @@ shared_ptr<ValType> _createVal(shared_ptr<StructType> ft) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!legal) {
|
if (!legal) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
t = function_replace(tt->c3, mp);
|
t = function_replace(tt->c3, mp);
|
||||||
}
|
}
|
||||||
@ -126,7 +126,7 @@ shared_ptr<ValType> _createVal(shared_ptr<StructType> ft) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
if (t->type() != ValType::STRUCT) {
|
if (t->type() != ValType::STRUCT) {
|
||||||
printf("error on line %d", tokens[pt].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt].line), exit(0);
|
||||||
}
|
}
|
||||||
pt++;
|
pt++;
|
||||||
return _createVal(static_pointer_cast<StructType>(t));
|
return _createVal(static_pointer_cast<StructType>(t));
|
||||||
@ -165,7 +165,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
auto ret = createVal();
|
auto ret = createVal();
|
||||||
jump(TokenType::SEMI);
|
jump(TokenType::SEMI);
|
||||||
if (!sameType(tt->c3, ret)) {
|
if (!sameType(tt->c3, ret)) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
for (const auto& pr : tems) {
|
for (const auto& pr : tems) {
|
||||||
ndefs.erase(pr.first);
|
ndefs.erase(pr.first);
|
||||||
@ -200,7 +200,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
if (flag) {
|
if (flag) {
|
||||||
t = createType();
|
t = createType();
|
||||||
if (t->type() != ValType::STRUCT) {
|
if (t->type() != ValType::STRUCT) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
auto tt = static_pointer_cast<StructType>(t);
|
auto tt = static_pointer_cast<StructType>(t);
|
||||||
auto vals = createVals();
|
auto vals = createVals();
|
||||||
@ -215,12 +215,12 @@ shared_ptr<ValType> createVal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!legal) {
|
if (!legal) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() != Def::VAR) {
|
if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() != Def::VAR) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
t = static_pointer_cast<DefVar>(ndefs[s])->def_var;
|
t = static_pointer_cast<DefVar>(ndefs[s])->def_var;
|
||||||
}
|
}
|
||||||
@ -233,7 +233,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
types = createTypes();
|
types = createTypes();
|
||||||
}
|
}
|
||||||
if (tt->c1.size() != types.size()) {
|
if (tt->c1.size() != types.size()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
map<shared_ptr<TemplateType>, shared_ptr<ValType>> mp;
|
map<shared_ptr<TemplateType>, shared_ptr<ValType>> mp;
|
||||||
for (size_t i = 0; i < types.size(); i++) {
|
for (size_t i = 0; i < types.size(); i++) {
|
||||||
@ -250,7 +250,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!legal) {
|
if (!legal) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
t = function_replace(tt->c3, mp);
|
t = function_replace(tt->c3, mp);
|
||||||
}
|
}
|
||||||
@ -258,7 +258,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
if (t->type() != ValType::STRUCT) {
|
if (t->type() != ValType::STRUCT) {
|
||||||
printf("error on line %d", tokens[pt].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt].line), exit(0);
|
||||||
}
|
}
|
||||||
pt++;
|
pt++;
|
||||||
return _createVal(static_pointer_cast<StructType>(t));
|
return _createVal(static_pointer_cast<StructType>(t));
|
||||||
@ -279,7 +279,7 @@ shared_ptr<ValType> _createType(shared_ptr<StructType> t) {
|
|||||||
auto str = t->str;
|
auto str = t->str;
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (str->structs.find(s) == str->structs.end()) {
|
if (str->structs.find(s) == str->structs.end()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
auto tt = make_shared<StructType>();
|
auto tt = make_shared<StructType>();
|
||||||
tt->str = str->structs[s], tt->mp = t->mp;
|
tt->str = str->structs[s], tt->mp = t->mp;
|
||||||
@ -288,7 +288,7 @@ shared_ptr<ValType> _createType(shared_ptr<StructType> t) {
|
|||||||
types = createTypes();
|
types = createTypes();
|
||||||
}
|
}
|
||||||
if (tt->str->c1.size() != types.size()) {
|
if (tt->str->c1.size() != types.size()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < types.size(); i++) {
|
for (size_t i = 0; i < types.size(); i++) {
|
||||||
tt->mp[tt->str->c1[i]] = types[i];
|
tt->mp[tt->str->c1[i]] = types[i];
|
||||||
@ -328,7 +328,7 @@ shared_ptr<ValType> createType() {
|
|||||||
} else {
|
} else {
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() == Def::VAR) {
|
if (ndefs.find(s) == ndefs.end() || ndefs[s]->type() == Def::VAR) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
if (ndefs[s]->type() == Def::STRUCT) {
|
if (ndefs[s]->type() == Def::STRUCT) {
|
||||||
auto tt = make_shared<StructType>();
|
auto tt = make_shared<StructType>();
|
||||||
@ -338,7 +338,7 @@ shared_ptr<ValType> createType() {
|
|||||||
types = createTypes();
|
types = createTypes();
|
||||||
}
|
}
|
||||||
if (tt->str->c1.size() != types.size()) {
|
if (tt->str->c1.size() != types.size()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < types.size(); i++) {
|
for (size_t i = 0; i < types.size(); i++) {
|
||||||
tt->mp[tt->str->c1[i]] = types[i];
|
tt->mp[tt->str->c1[i]] = types[i];
|
||||||
@ -353,7 +353,7 @@ shared_ptr<ValType> createType() {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
if (t->type() != ValType::STRUCT) {
|
if (t->type() != ValType::STRUCT) {
|
||||||
printf("error on line %d", tokens[pt].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt].line), exit(0);
|
||||||
}
|
}
|
||||||
pt++;
|
pt++;
|
||||||
return _createType(static_pointer_cast<StructType>(t));
|
return _createType(static_pointer_cast<StructType>(t));
|
||||||
@ -377,7 +377,7 @@ vector<pair<int, shared_ptr<TemplateType>>> createTems() {
|
|||||||
auto single = [&]() {
|
auto single = [&]() {
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (ndefs.find(s) != ndefs.end()) {
|
if (ndefs.find(s) != ndefs.end()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
auto x = make_shared<TemplateType>();
|
auto x = make_shared<TemplateType>();
|
||||||
auto d = make_shared<DefTemplate>();
|
auto d = make_shared<DefTemplate>();
|
||||||
@ -402,7 +402,7 @@ vector<pair<int, shared_ptr<ValType>>> createPars(map<int, shared_ptr<ValType>>*
|
|||||||
}
|
}
|
||||||
auto s = jump(TokenType::ID);
|
auto s = jump(TokenType::ID);
|
||||||
if (ndefs.find(s) != ndefs.end()) {
|
if (ndefs.find(s) != ndefs.end()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
jump(TokenType::COLON);
|
jump(TokenType::COLON);
|
||||||
auto t = createType();
|
auto t = createType();
|
||||||
@ -514,7 +514,7 @@ pair<int, shared_ptr<ValType>> createVar() {
|
|||||||
jump(TokenType::SEMI);
|
jump(TokenType::SEMI);
|
||||||
}
|
}
|
||||||
if (ndefs.find(s) != ndefs.end()) {
|
if (ndefs.find(s) != ndefs.end()) {
|
||||||
printf("error on line %d", tokens[pt - 1].line), exit(0);
|
fprintf(stderr, "error on line %d", tokens[pt - 1].line), exit(0);
|
||||||
}
|
}
|
||||||
auto d = make_shared<DefVar>();
|
auto d = make_shared<DefVar>();
|
||||||
d->def_var = t;
|
d->def_var = t;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user