A simple scripting language in C++
Ferenc Szontágh
2025-04-19 55abb4f6f81fc370e349385b38dffb05fa9d5dcb
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
// PrintModule.hpp
#ifndef MODULES_PRINTMODULE_HPP
#define MODULES_PRINTMODULE_HPP
 
#include <iostream>
 
#include "Modules/BaseModule.hpp"
#include "Modules/ModuleManager.hpp"
#include "Symbols/Value.hpp"
 
namespace Modules {
 
/**
 * @brief Module that provides a built-in print function.
 */
class PrintModule : public BaseModule {
  public:
    void registerModule() override {
        auto & mgr = ModuleManager::instance();
        mgr.registerFunction("print", [](const std::vector<Symbols::Value> & args) {
            for (const auto & v : args) {
                std::cout << Symbols::Value::to_string(v);
            }
            return Symbols::Value();
        });
        mgr.registerFunction("printnl", [](const std::vector<Symbols::Value> & args) {
            for (const auto & v : args) {
                std::cout << Symbols::Value::to_string(v);
            }
            std::cout << "\n";
            return Symbols::Value();
        });
        mgr.registerFunction("error", [](const std::vector<Symbols::Value> & args) {
            for (const auto & v : args) {
                std::cerr << Symbols::Value::to_string(v);
            }
            std::cerr << "\n";
            return Symbols::Value();
        });
        // Built-in error thrower: throws a module exception with provided message
        mgr.registerFunction("throw_error", [](const std::vector<Symbols::Value> & args) {
            if (args.size() != 1 || args[0].getType() != Symbols::Variables::Type::STRING) {
                throw Exception("throw_error requires exactly one string argument");
            }
            std::string msg = args[0].get<std::string>();
            throw Exception(msg);
            return Symbols::Value();  // never reached
        });
    }
};
 
}  // namespace Modules
#endif  // MODULES_PRINTMODULE_HPP