Skip to content

Commit 3bf1e4e

Browse files
committed
refactor: Exit codes, check for multiple function defenitions
1 parent 044931e commit 3bf1e4e

8 files changed

Lines changed: 38 additions & 20 deletions

File tree

src/compiler.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ int main(int argc, char* argv[]) {
7070
auto srcs = result["source"].as<std::vector<std::string>>();
7171
if (srcs.size() > 1) {
7272
std::cerr << "Module can be compiled from only one source file.\n";
73-
exit(1);
73+
exit(func::error_codes::E_PARAMS);
7474
}
7575
if(drv.parse(srcs[0]) != 0) {
7676
std::cerr << "Failed to parse - exiting.\n";
77-
exit(1);
77+
exit(func::error_codes::E_SYTNTAX);
7878
}
7979
} else {
8080
exit(0); // No input files were given.
@@ -87,12 +87,12 @@ int main(int argc, char* argv[]) {
8787
target_arch = Arch::X64;
8888
} else {
8989
std::cerr << "Unknown architecture: " << arch_str << "\n";
90-
exit(1);
90+
exit(func::error_codes::E_PARAMS);
9191
}
9292

9393
} catch(const cxxopts::exceptions::exception& e) {
9494
std::cerr << "Error parsing options: " << e.what() << '\n';
95-
exit(1);
95+
exit(func::error_codes::E_PARAMS);
9696
}
9797

9898
func::print_visitor print_visitor{ std::cout };
@@ -132,21 +132,24 @@ int main(int argc, char* argv[]) {
132132
}
133133
default: {
134134
std::cerr << "Unsupported architecture\n";
135-
exit(1);
135+
exit(func::error_codes::E_PARAMS);
136136
}
137137
}
138138

139139
} catch(func::unexpected_type_exception& e) {
140140
std::cerr << "Syntax error: unexpected type " << e << "\n";
141-
exit(1);
141+
exit(func::error_codes::E_TYPE);
142142
} catch(func::symbol_not_found_exception& e) {
143143
std::cerr << "Syntax error: symbol not found " << e << "\n";
144-
exit(1);
144+
exit(func::error_codes::E_SYMBOL);
145+
} catch(func::symbol_redeclaration_exception& e) {
146+
std::cerr << "Syntax error: symbol redeclaration " << e << "\n";
147+
exit(func::error_codes::E_SYMBOL);
145148
} catch(func::syntax_exception& e) {
146149
std::cerr << "Syntax error: " << e << "\n";
147-
exit(1);
150+
exit(func::error_codes::E_SYTNTAX);
148151
} catch(func::global_syntax_exception& e) {
149152
std::cerr << "Syntax error: " << e << "\n";
150-
exit(1);
153+
exit(func::error_codes::E_SYTNTAX);
151154
}
152155
}

src/exception.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
#include <string>
77
namespace func {
88

9+
enum error_codes{
10+
E_SYTNTAX = 1,
11+
E_SYMBOL = 2,
12+
E_TYPE = 3,
13+
E_PARAMS = 4,
14+
E_OTHER = 5,
15+
};
16+
917
struct syntax_exception {
1018
std::string reason;
1119
yy::location loc;

src/visitor/llvm_visitor/llvm_visitor.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ void llvm_visitor::visit(const function& node) {
8989
return;
9090
}
9191

92+
if (!f->empty()){
93+
throw syntax_exception{ "Multiple defenition of function '" + node.get_identifier() + "'.",
94+
node.get_loc() };
95+
}
96+
9297
BasicBlock* bb = BasicBlock::Create(ctx, "entry", f);
9398
builder.SetInsertPoint(bb);
9499

@@ -128,7 +133,7 @@ void llvm_visitor::visit(const function& node) {
128133
std::string error_msg;
129134
llvm::raw_string_ostream os(error_msg);
130135
if(llvm::verifyFunction(*f, &os)) {
131-
throw syntax_exception{ error_msg, node.get_loc() };
136+
throw unexpected_type_exception {error_msg, node.get_loc()};
132137
}
133138

134139
fpm.run(*f, fam);

tests/negative/call_undeclared.fc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// test: call-undeclared-func-test
22
// compile-fail: true
3-
// exit-code: 1
3+
// exit-code: 2
44

55
int write(int _);
66
void exit(int _);

tests/negative/call_wrong_type_args.fc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// test: call-with-wrong-type-of-arguments-test
22
// compile-fail: true
3-
// exit-code: 1
3+
// exit-code: 3
44

55
int write(int _);
66
void exit(int _);

tests/negative/call_wrong_type_args_2.fc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// test: call-with-wrong-type-of-func-arguments-test
22
// compile-fail: true
3-
// exit-code: 1
3+
// exit-code: 3
44

55
int write(int _);
66
void exit(int _);

tests/negative/declare_diff_body.fc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
// compile-fail: true
33
// exit-code: 1
44

5+
int write(int _);
56
void exit(int _);
67

7-
int foo(int arg){
8-
return 1;
9-
};
8+
int foo(){
9+
return 97;
10+
}
1011

11-
int foo(int arg){
12-
return 2;
13-
};
12+
int foo(){
13+
return 98;
14+
}
1415

1516
void main(){
17+
write(foo());
1618
exit(0);
1719
}
1820

tests/negative/declare_diff_sign.fc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// test: declare-with-different-signature-test
22
// compile-fail: true
3-
// exit-code: 1
3+
// exit-code: 2
44

55
void exit(int _);
66

0 commit comments

Comments
 (0)