-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
878 lines (708 loc) · 24.4 KB
/
Copy pathmain.cpp
File metadata and controls
878 lines (708 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/**
* @file main.cpp
* @brief Entry point for the Bishop compiler.
*
* Provides the main() function and CLI handling for the Bishop compiler.
* Supports two modes:
* - bishop <source.b> : Transpile to C++ and output to stdout
* - bishop test <path> : Run tests on .b files, compiling and executing them
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <filesystem>
#include <cstdlib>
#include <set>
#include <thread>
#include <future>
#include <mutex>
#include <atomic>
#include "lexer/lexer.hpp"
#include "parser/parser.hpp"
#include "codegen/codegen.hpp"
#include "typechecker/typechecker.hpp"
#include "project/project.hpp"
#include "project/module.hpp"
using namespace std;
namespace fs = filesystem;
/**
* Result of transpiling a Bishop source file.
* Contains both the generated C++ code and metadata about module usage.
*/
struct TranspileResult {
string cpp_code; ///< Generated C++ source code
bool uses_http = false; ///< True if http module is imported
bool uses_fs = false; ///< True if fs module is imported
bool uses_crypto = false; ///< True if crypto module is imported
bool uses_yaml = false; ///< True if yaml module is imported
set<string> extern_libs; ///< Libraries needed by extern functions
};
/**
* Gets the directory where the bishop executable is located.
* Used to find runtime libraries relative to the compiler.
*/
fs::path get_executable_dir() {
return fs::read_symlink("/proc/self/exe").parent_path();
}
/**
* Gets the library and include paths based on where bishop is installed.
* Development: build/bishop -> build/lib, build/include
* Installed: ~/.local/bin/bishop -> ~/.local/lib/bishop, ~/.local/include
*
* Include path points to parent so #include <bishop/http.hpp> works.
*/
pair<fs::path, fs::path> get_runtime_paths() {
fs::path exe_dir = get_executable_dir();
// Check if we're in a build directory (has lib/ subdirectory)
fs::path build_lib = exe_dir / "lib";
fs::path build_include = exe_dir / "include";
if (fs::exists(build_lib) && fs::exists(build_include)) {
return {build_lib, build_include};
}
// Otherwise assume installed layout: bin/bishop -> lib/bishop, include
// Include path is ~/.local/include (so bishop/http.hpp is found)
// Lib path is ~/.local/lib/bishop (where our libs are)
fs::path install_base = exe_dir.parent_path();
return {install_base / "lib" / "bishop", install_base / "include"};
}
/**
* Builds the g++ compile command (source to object file).
* Uses ccache for caching compiled objects.
*
* Precompiled headers: GCC automatically uses .gch files when found
* alongside the .hpp file in the include path.
*/
string build_compile_cmd(const string& obj_output, const string& input) {
auto [lib_path, include_path] = get_runtime_paths();
string cmd = "CCACHE_SLOPPINESS=pch_defines,time_macros CCACHE_DEPEND=1 ccache g++ -std=c++23 -pipe -c -MD -o " + obj_output + " " + input;
cmd += " -I" + include_path.string();
cmd += " 2>&1";
return cmd;
}
/**
* Builds the g++ link command (object file to executable).
* Adds runtime libraries based on which modules are used.
*
* When static_link is true, links boost statically for portable binaries.
* When false, uses dynamic linking for faster dev builds.
*/
string build_link_cmd(const TranspileResult& result, const string& exe_output,
const string& obj_input, bool static_link = false) {
auto [lib_path, include_path] = get_runtime_paths();
string cmd = "g++ -pipe -o " + exe_output + " " + obj_input;
// Always add library path and link bishop_std_runtime (contains fiber runtime)
cmd += " -L" + lib_path.string();
cmd += " -lbishop_std_runtime";
if (result.uses_http) {
cmd += " -lbishop_http_runtime";
cmd += " -lllhttp";
}
if (result.uses_crypto) {
cmd += " -lssl -lcrypto";
}
if (result.uses_yaml) {
cmd += " -lyaml-cpp";
}
// Add extern library flags (skip "c" as libc is implicit)
for (const auto& lib : result.extern_libs) {
if (lib != "c") {
cmd += " -l" + lib;
}
}
// Link boost_fiber and boost_context for fiber-based concurrency
if (static_link) {
// Static linking for portable standalone binaries
cmd += " -l:libboost_fiber.a -l:libboost_context.a -lpthread";
} else {
// Dynamic linking for faster dev builds
cmd += " -lboost_fiber -lboost_context -lpthread";
}
cmd += " 2>&1";
return cmd;
}
/**
* Reads the entire contents of a file into a string.
* Returns empty string and prints error if file cannot be opened.
*/
string read_file(const string& path) {
ifstream file(path);
if (!file) {
cerr << "Error: Could not open file " << path << endl;
return "";
}
stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
/**
* @brief Merges all .b files from a directory into a single AST.
* The root directory follows the same rules as any module directory -
* all .b files are merged and share scope (Go-style implicit packages).
*
* Uses a two-pass approach:
* 1. First pass: collect all struct/error names from all files
* 2. Second pass: parse each file with the combined struct names
*/
unique_ptr<Program> merge_package_files(const fs::path& dir, const string& filename) {
auto files = get_module_files(dir);
auto merged = make_unique<Program>();
// First pass: collect all struct and error names from all files
vector<string> all_struct_names;
for (const auto& file : files) {
string source = read_file(file.string());
if (source.empty()) {
continue;
}
Lexer lexer(source);
vector<Token> tokens;
try {
tokens = lexer.tokenize();
} catch (const runtime_error&) {
continue;
}
// Scan for struct and error definitions: Name :: struct or Name :: err
for (size_t i = 0; i + 2 < tokens.size(); i++) {
if (tokens[i].type == TokenType::IDENT &&
tokens[i+1].type == TokenType::DOUBLE_COLON &&
(tokens[i+2].type == TokenType::STRUCT || tokens[i+2].type == TokenType::ERR)) {
all_struct_names.push_back(tokens[i].value);
}
}
}
// Second pass: parse each file with the combined struct names
for (const auto& file : files) {
string source = read_file(file.string());
if (source.empty()) {
continue;
}
Lexer lexer(source);
vector<Token> tokens;
try {
tokens = lexer.tokenize();
} catch (const runtime_error& e) {
cerr << file.string() << ": lexer error: " << e.what() << endl;
continue;
}
ParserState state(tokens);
// Pre-populate struct names from all files in the package
for (const auto& name : all_struct_names) {
state.struct_names.push_back(name);
}
unique_ptr<Program> ast;
try {
ast = parser::parse(state);
} catch (const runtime_error& e) {
cerr << file.string() << ": parse error: " << e.what() << endl;
continue;
}
// Merge imports
for (auto& imp : ast->imports) {
merged->imports.push_back(move(imp));
}
// Merge structs
for (auto& s : ast->structs) {
merged->structs.push_back(move(s));
}
// Merge functions
for (auto& f : ast->functions) {
merged->functions.push_back(move(f));
}
// Merge methods
for (auto& m : ast->methods) {
merged->methods.push_back(move(m));
}
// Merge constants
for (auto& c : ast->constants) {
merged->constants.push_back(move(c));
}
// Merge externs
for (auto& e : ast->externs) {
merged->externs.push_back(move(e));
}
// Merge error definitions
for (auto& e : ast->errors) {
merged->errors.push_back(move(e));
}
// Merge using statements
for (auto& u : ast->usings) {
merged->usings.push_back(move(u));
}
}
return merged;
}
/**
* Transpiles a pre-parsed AST to C++ code.
* Used when files have already been merged at the AST level.
*/
TranspileResult transpile_ast(unique_ptr<Program> ast, const string& filename, bool test_mode) {
TranspileResult result;
// Detect which builtin modules are used from the parsed AST
for (const auto& imp : ast->imports) {
if (imp->module_path == "http") {
result.uses_http = true;
} else if (imp->module_path == "fs") {
result.uses_fs = true;
} else if (imp->module_path == "crypto") {
result.uses_crypto = true;
} else if (imp->module_path == "yaml") {
result.uses_yaml = true;
}
}
// Collect libraries from extern functions
for (const auto& ext : ast->externs) {
result.extern_libs.insert(ext->library);
}
// Find project configuration (for module resolution)
auto config = find_project(fs::path(filename));
// Load imported modules if we have a project config
map<string, const Module*> imports;
unique_ptr<ModuleManager> module_manager;
if (config && !ast->imports.empty()) {
module_manager = make_unique<ModuleManager>(*config);
for (const auto& imp : ast->imports) {
const Module* mod = module_manager->load_module(imp->module_path);
if (!mod) {
for (const auto& err : module_manager->get_errors()) {
cerr << filename << ": error: " << err << endl;
}
return result;
}
imports[imp->alias] = mod;
}
} else if (!ast->imports.empty()) {
cerr << filename << ": error: imports require a bishop.toml file (run 'bishop init')" << endl;
return result;
}
// Type check with imports
TypeChecker checker;
for (const auto& [alias, mod] : imports) {
checker.register_module(alias, *mod);
}
if (!checker.check(*ast, filename)) {
for (const auto& err : checker.get_errors()) {
cerr << err.filename << ":" << err.line << ": error: " << err.message << endl;
}
return result;
}
// Generate code with imports
CodeGen codegen;
if (imports.empty()) {
result.cpp_code = codegen.generate(ast, test_mode);
} else {
result.cpp_code = codegen.generate_with_imports(ast, imports, test_mode);
}
return result;
}
/**
* Transpiles Bishop source to C++ code.
* Runs lexer -> parser -> module loading -> type checker -> code generator pipeline.
* Returns result with empty cpp_code and prints errors if type checking or module loading fails.
*/
TranspileResult transpile(const string& source, const string& filename, bool test_mode) {
TranspileResult result;
Lexer lexer(source);
vector<Token> tokens;
try {
tokens = lexer.tokenize();
} catch (const runtime_error& e) {
cerr << filename << ": lexer error: " << e.what() << endl;
return result;
}
ParserState state(tokens);
unique_ptr<Program> ast;
try {
ast = parser::parse(state);
} catch (const runtime_error& e) {
cerr << filename << ": parse error: " << e.what() << endl;
return result;
}
// Delegate to transpile_ast
return transpile_ast(move(ast), filename, test_mode);
}
/**
* Checks if a path component is "errors" directory.
* Used to identify negative test files.
*/
bool is_error_test(const fs::path& path) {
for (const auto& part : path) {
if (part == "errors") {
return true;
}
}
return false;
}
/**
* Result of running a single test.
*/
struct TestResult {
fs::path file;
bool passed;
string message;
};
/**
* Runs a single positive test (expects success).
* Thread-safe: uses unique temp files based on test_id.
*/
TestResult run_positive_test(const fs::path& test_file, int test_id) {
TestResult result{test_file, false, ""};
string source = read_file(test_file.string());
if (source.empty()) {
result.message = "could not read file";
return result;
}
TranspileResult transpile_result = transpile(source, test_file.string(), true);
if (transpile_result.cpp_code.empty()) {
result.message = "type errors";
return result;
}
string tmp_cpp = "/tmp/bishop_test_" + to_string(test_id) + ".cpp";
string tmp_obj = "/tmp/bishop_test_" + to_string(test_id) + ".o";
string tmp_bin = "/tmp/bishop_test_" + to_string(test_id);
ofstream out(tmp_cpp);
out << transpile_result.cpp_code;
out.close();
string compile_cmd = build_compile_cmd(tmp_obj, tmp_cpp);
if (system(compile_cmd.c_str()) != 0) {
result.message = "compile failed";
return result;
}
string link_cmd = build_link_cmd(transpile_result, tmp_bin, tmp_obj);
if (system(link_cmd.c_str()) != 0) {
result.message = "link failed";
return result;
}
string run_cmd = tmp_bin + " 2>&1";
int test_status = system(run_cmd.c_str());
if (test_status != 0) {
result.message = "test failed";
return result;
}
result.passed = true;
return result;
}
/// Mutex to protect cerr redirection in negative tests
static mutex cerr_mutex;
/**
* Runs a single negative test (expects failure with specific error).
* Thread-safe: uses mutex to protect cerr redirection.
*/
TestResult run_negative_test(const fs::path& test_file) {
TestResult result{test_file, false, ""};
string source = read_file(test_file.string());
if (source.empty()) {
result.message = "could not read file";
return result;
}
string error_output;
{
// Protect cerr redirection with mutex
lock_guard<mutex> lock(cerr_mutex);
stringstream error_capture;
streambuf* old_cerr = cerr.rdbuf(error_capture.rdbuf());
TranspileResult transpile_result = transpile(source, test_file.string(), false);
cerr.rdbuf(old_cerr);
error_output = error_capture.str();
if (!transpile_result.cpp_code.empty()) {
result.message = "expected error, but compiled";
return result;
}
}
// Extract expected error from filename (replace underscores with spaces)
string expected_error = test_file.stem().string();
for (char& c : expected_error) {
if (c == '_') {
c = ' ';
}
}
if (error_output.find(expected_error) == string::npos) {
result.message = "expected '" + expected_error + "', got: " + error_output;
return result;
}
result.passed = true;
return result;
}
/**
* Runs tests on all .b files in a directory (or a single file).
* Each test file is transpiled, compiled with g++, and executed.
* Test functions (test_*) use assert_eq for assertions.
* Files in tests/errors/ are expected to fail with specific error messages.
* Uses parallel execution for faster test runs.
* Returns 0 if all tests pass, 1 if any fail.
*/
int run_tests(const string& path) {
vector<fs::path> test_files;
vector<fs::path> error_test_files;
if (fs::is_directory(path)) {
for (const auto& entry : fs::recursive_directory_iterator(path)) {
if (entry.path().extension() == ".b") {
if (is_error_test(entry.path())) {
error_test_files.push_back(entry.path());
} else {
test_files.push_back(entry.path());
}
}
}
} else if (fs::exists(path)) {
if (is_error_test(path)) {
error_test_files.push_back(path);
} else {
test_files.push_back(path);
}
} else {
cerr << "Error: Path does not exist: " << path << endl;
return 1;
}
if (test_files.empty() && error_test_files.empty()) {
cerr << "No .b files found" << endl;
return 1;
}
// Launch positive tests in parallel
vector<future<TestResult>> positive_futures;
for (size_t i = 0; i < test_files.size(); i++) {
positive_futures.push_back(
async(launch::async, run_positive_test, test_files[i], static_cast<int>(i))
);
}
// Launch negative tests in parallel
vector<future<TestResult>> negative_futures;
for (const auto& test_file : error_test_files) {
negative_futures.push_back(
async(launch::async, run_negative_test, test_file)
);
}
// Collect and print results
int total_failures = 0;
for (auto& fut : positive_futures) {
TestResult result = fut.get();
if (result.passed) {
cout << "\033[32mPASS\033[0m " << result.file.string() << endl;
} else {
cout << "\033[31mFAIL\033[0m " << result.file.string()
<< " (" << result.message << ")" << endl;
total_failures++;
}
}
for (auto& fut : negative_futures) {
TestResult result = fut.get();
if (result.passed) {
cout << "\033[32mPASS\033[0m " << result.file.string() << endl;
} else {
cout << "\033[31mFAIL\033[0m " << result.file.string()
<< " (" << result.message << ")" << endl;
total_failures++;
}
}
return total_failures > 0 ? 1 : 0;
}
/**
* Initializes a new Bishop project by creating a bishop.toml file in the current directory.
*/
int init_project(const string& project_name) {
if (project_name.empty()) {
cerr << "Usage: bishop init <project_name>" << endl;
return 1;
}
fs::path init_file = fs::current_path() / "bishop.toml";
if (fs::exists(init_file)) {
cerr << "Error: bishop.toml already exists" << endl;
return 1;
}
ofstream out(init_file);
if (!out) {
cerr << "Error: Could not create bishop.toml" << endl;
return 1;
}
out << "[project]" << endl;
out << "name = \"" << project_name << "\"" << endl;
cout << "Initialized project '" << project_name << "'" << endl;
return 0;
}
/**
* Compiles and runs a bishop source file or project directory.
* Extra arguments are passed to the compiled program.
*/
int run_file(const string& path, const vector<string>& extra_args = {}) {
string filename;
if (fs::is_directory(path)) {
fs::path dir_path = fs::absolute(path);
fs::path toml_path = dir_path / "bishop.toml";
if (!fs::exists(toml_path)) {
cerr << "Error: No bishop.toml found in " << path << endl;
return 1;
}
auto config = parse_init_file(toml_path);
if (!config) {
cerr << "Error: Could not parse bishop.toml" << endl;
return 1;
}
if (!config->entry) {
cerr << "Error: No entry field in bishop.toml" << endl;
return 1;
}
fs::path entry_path = dir_path / *config->entry;
if (!fs::exists(entry_path)) {
cerr << "Error: Entry file not found: " << *config->entry << endl;
return 1;
}
filename = entry_path.string();
} else {
filename = path;
}
// Merge all .b files in the directory as the 'main' package
fs::path entry_dir = fs::path(filename).parent_path();
if (entry_dir.empty()) {
entry_dir = ".";
}
auto ast = merge_package_files(entry_dir, filename);
if (!ast || (ast->functions.empty() && ast->structs.empty())) {
cerr << "Error: No valid source files found" << endl;
return 1;
}
TranspileResult result = transpile_ast(move(ast), filename, false);
if (result.cpp_code.empty()) {
return 1;
}
// Write to temp file
string cpp_file = "/tmp/bishop_run.cpp";
string obj_file = "/tmp/bishop_run.o";
string exe_file = "/tmp/bishop_run";
ofstream out(cpp_file);
if (!out) {
cerr << "Error: Could not create temp file" << endl;
return 1;
}
out << result.cpp_code;
out.close();
// Compile (cached)
string compile_cmd = build_compile_cmd(obj_file, cpp_file);
if (system(compile_cmd.c_str()) != 0) {
cerr << "Compile failed" << endl;
return 1;
}
// Link
string link_cmd = build_link_cmd(result, exe_file, obj_file);
if (system(link_cmd.c_str()) != 0) {
cerr << "Link failed" << endl;
return 1;
}
// Run (static linking, no LD_LIBRARY_PATH needed)
string run_cmd = exe_file;
for (const auto& arg : extra_args) {
// Quote arguments that contain spaces
if (arg.find(' ') != string::npos) {
run_cmd += " \"" + arg + "\"";
} else {
run_cmd += " " + arg;
}
}
return system(run_cmd.c_str());
}
/**
* Builds a bishop source file to an executable.
* If a directory is provided, looks for bishop.toml with entry field.
*/
int build_file(const string& path) {
string filename;
string exe_name;
if (fs::is_directory(path)) {
fs::path dir_path = fs::absolute(path);
fs::path toml_path = dir_path / "bishop.toml";
if (!fs::exists(toml_path)) {
cerr << "Error: No bishop.toml found in " << path << endl;
return 1;
}
auto config = parse_init_file(toml_path);
if (!config) {
cerr << "Error: Could not parse bishop.toml" << endl;
return 1;
}
if (!config->entry) {
cerr << "Error: No entry field in bishop.toml" << endl;
return 1;
}
fs::path entry_path = dir_path / *config->entry;
if (!fs::exists(entry_path)) {
cerr << "Error: Entry file not found: " << *config->entry << endl;
return 1;
}
filename = entry_path.string();
exe_name = config->name;
} else {
filename = path;
exe_name = fs::path(path).stem().string();
}
// Merge all .b files in the directory as the 'main' package
fs::path entry_dir = fs::path(filename).parent_path();
if (entry_dir.empty()) {
entry_dir = ".";
}
auto ast = merge_package_files(entry_dir, filename);
if (!ast || (ast->functions.empty() && ast->structs.empty())) {
cerr << "Error: No valid source files found" << endl;
return 1;
}
TranspileResult result = transpile_ast(move(ast), filename, false);
if (result.cpp_code.empty()) {
return 1;
}
string cpp_file = "/tmp/bishop_build.cpp";
string obj_file = "/tmp/bishop_build.o";
ofstream out(cpp_file);
if (!out) {
cerr << "Error: Could not create temp file" << endl;
return 1;
}
out << result.cpp_code;
out.close();
// Compile (cached)
string compile_cmd = build_compile_cmd(obj_file, cpp_file);
if (system(compile_cmd.c_str()) != 0) {
cerr << "Compile failed" << endl;
return 1;
}
// Link with static boost for standalone binary
string link_cmd = build_link_cmd(result, exe_name, obj_file, true);
if (system(link_cmd.c_str()) != 0) {
cerr << "Link failed" << endl;
return 1;
}
return 0;
}
/**
* Main entry point. Usage:
* bishop <file|dir> - Build executable from source file or project directory
* bishop run <file|dir> - Build and run
* bishop test <path> - Run tests
* bishop init <name> - Initialize project
*/
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << "Usage: bishop <file|dir>" << endl;
cerr << " bishop run <file|dir>" << endl;
cerr << " bishop test <path>" << endl;
cerr << " bishop init <name>" << endl;
return 1;
}
string cmd = argv[1];
if (cmd == "test") {
string path = argc >= 3 ? argv[2] : "tests/";
return run_tests(path);
}
if (cmd == "init") {
string name = argc >= 3 ? argv[2] : "";
return init_project(name);
}
if (cmd == "run") {
if (argc < 3) {
cerr << "Usage: bishop run <file> [args...]" << endl;
return 1;
}
// Collect extra arguments for the program
vector<string> extra_args;
for (int i = 3; i < argc; i++) {
extra_args.push_back(argv[i]);
}
return run_file(argv[2], extra_args);
}
return build_file(cmd);
}