A simple scripting language in C++
Szontágh Ferenc
2025-04-19 571a9a1bafe0d8adddf3141f82213a47e4568baa
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
// ArrayModule.hpp
#ifndef MODULES_ARRAYMODULE_HPP
#define MODULES_ARRAYMODULE_HPP
 
#include <stdexcept>
#include <string>
#include <vector>
 
#include "Modules/BaseModule.hpp"
#include "Modules/ModuleManager.hpp"
#include "Symbols/Value.hpp"
#include "Symbols/VariableTypes.hpp"
 
namespace Modules {
 
/**
 * @brief Module providing a sizeof() function for array variables.
 * Usage:
 *   sizeof($array)   -> returns number of elements in the array
 */
class ArrayModule : public BaseModule {
  public:
    void registerModule() override {
        auto & mgr = ModuleManager::instance();
        mgr.registerFunction("sizeof", [](const std::vector<Symbols::Value> & args) {
            using namespace Symbols;
            if (args.size() != 1) {
                throw std::runtime_error("sizeof expects exactly one argument");
            }
            const auto & val  = args[0];
            auto   type = val.getType();
            // Only allow array types (OBJECT)
            if (type == Variables::Type::OBJECT) {
                const auto & map = std::get<Value::ObjectMap>(val.get());
                return Value(static_cast<int>(map.size()));
            }
            throw std::runtime_error("sizeof expects an array variable");
        });
    }
};
 
}  // namespace Modules
 
#endif  // MODULES_ARRAYMODULE_HPP