-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.cpp
75 lines (52 loc) · 1.72 KB
/
cli.cpp
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
// The Command Line Interface for fn,
// utilising cxxopts.
#include <iostream> // std::cout
#include <vector> // std::vector
#include "vendor/cxxopts.h" // cxxopts
#include "src/exec.h" // fn::exec
using namespace fn;
int run(const char fileName[], bool debug) {
Execution context = Execution(debug);
vm::Value* result = context.execFile(fileName);
std::cout << result->toString();
return 0;
}
int repl(bool debug) {
Execution context = Execution(debug);
std::string currentLine;
// Start the REPL loop
std::cout << "fn REPL\nCTRL+C to exit\n";
while(true) {
// Read
std::cout << "> ";
std::getline(std::cin, currentLine);
// Eval
vm::Value* lastReturnValue = context.execCode(currentLine);
// Print
std::cout << lastReturnValue->toString() << std::endl;
}
}
int parseCli(int argc, char* argv[])
{
cxxopts::Options options("fn", " - a fun-ctional programming language!");
options.add_options()
("command", "One of: run, repl, help", cxxopts::value<std::string>())
("args", "", cxxopts::value<std::vector<std::string>>())
("d,debug", "Prints debugging information with the command");
options.parse_positional(std::vector<std::string>{"command", "args"});
options.parse(argc, argv);
std::string command = options["command"].as<std::string>();
bool debug = options.count("debug");
if(command == "help" || command == "") {
std::cout << options.help();
return 0;
} else if(command == "run") {
std::vector<std::string> args = options["args"].as<std::vector<std::string>>();
std::string fileName = args[0];
return run(fileName.c_str(), debug);
} else if(command == "repl") {
return repl(debug);
}
// We did nothing. That's bad.
return -1;
}