-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
69 lines (55 loc) · 1.73 KB
/
main.cc
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
#include <includejs/engine.h>
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <filesystem> // std::path
#include <fstream> // std::ifstream
#include <iostream> // std::cout
#include <optional> // std::optional
#include <string> // std::string
struct InputFile {
std::string source;
std::string file_name;
};
bool run(const std::string &source, const std::string &file_name) {
includejs::Engine engine;
includejs::Value result{engine.evaluate(source, file_name)};
if (!result.is_number()) {
std::cerr << "Error: result is not a number\n";
return false;
}
std::cout << result.to_number() << "\n";
return true;
}
void print_usage() { std::cerr << "Usage: smallruntime <path-to-js-file>\n"; }
std::optional<InputFile> read_file(const char *input) {
const std::filesystem::path path{input};
if (!std::filesystem::exists(path)) {
std::cerr << "Error: file does not exist\n";
return std::nullopt;
}
std::ifstream file(path, std::ios::in);
if (!file.is_open()) {
std::cerr << "Error: could not open file\n";
return std::nullopt;
}
std::string source{std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>()};
std::string file_name{path.filename()};
return std::make_optional<InputFile>(InputFile{source, file_name});
}
int main(int argc, char *argv[]) {
// Check if the user provided enough arguments
if (argc < 2) {
print_usage();
return EXIT_FAILURE;
}
// Check if the file exists and can be opened
const auto input_file{read_file(argv[1])};
if (!input_file.has_value()) {
return EXIT_FAILURE;
}
// Run the code
if (!run(input_file->source, input_file->file_name)) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}