A simple scripting language in C++
Ferenc Szontágh
2025-04-13 86904d513734134beffc29c6f4012d53a99f25c5
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
#include <filesystem>
#include <fstream>
 
#include "Builtins/PrintModule.hpp"
#include "Builtins/SleepModule.hpp"
#include "ScriptInterpreter.hpp"
 
static bool DEBUG = false;
 
int main(int argc, char * argv[]) {
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " [-d / --debug] <script_file>\n";
        return 1;
    }
 
    std::string file;
    if (argc == 2) {
        file = argv[1];
    } else if (argc == 3) {
        if (std::string(argv[1]) == "-d" || std::string(argv[1]) == "--debug") {
            DEBUG = true;
            file  = argv[2];
        } else if (argv[1] == "-h" || argv[1] == "--help") {
            std::cout << "Usage: " << argv[0] << " [-d / --debug] <script_file>\n";
            return 0;
        } else if (argv[1] == "-v" || argv[1] == "--vrsion") {
            std::cout << "VoidScript v" << VERSION_STRING << "\n";
            return 0;
        } else {
            std::cerr << "Usage: " << argv[0] << " [-d / --debug] <script_file>\n";
            return 1;
        }
    } else {
        std::cerr << "Usage: " << argv[0] << " [-d / --debug] <script_file>\n";
        return 1;
    }
 
    if (!std::filesystem::exists(file)) {
        std::cerr << "Error: File " << file << " does not exist.\n";
        return 1;
    }
 
    const std::string filename = std::filesystem::canonical(file).string();
 
    try {
        std::ifstream input(filename);
        if (!input.is_open()) {
            std::cerr << "Error: Could not open file " << filename << "\n";
            return 1;
        }
 
        std::string       content((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
        ScriptInterpreter interp;
        interp.registerModule("print", std::make_shared<PrintFunction>());
        interp.registerModule("sleep", std::make_shared<SleepFunction>());
        interp.executeScript(content, filename, DEBUG);
    } catch (const std::exception & e) {
        std::cerr << "Parser error: " << e.what() << "\n";
        return 1;
    }
 
    return 0;
}