A simple scripting language in C++
Ferenc Szontágh
2025-04-17 ba9a9199d01b0fdd4bf9a54914f8058bf71f30c5
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
#ifndef SLEEPFUNCTION_HPP
#define SLEEPFUNCTION_HPP
 
#include <thread>
 
#include "BaseFunction.hpp"
 
class SleepFunction : public BaseFunction {
  public:
    SleepFunction() : BaseFunction("sleep") {}
 
    void validateArgs(const std::vector<Token> &                     args,
                      const std::unordered_map<std::string, Value> & variables) override {
        if (args.size() != 1) {
            throw std::runtime_error("sleep() requires exactly one argument");
        }
 
        const Token & arg = args[0];
 
        if (arg.type == TokenType::IntLiteral) {
            return;
        }
 
        if (arg.type == TokenType::Variable) {
            const auto & value = variables.at(arg.lexeme);
            if (value.type != Variables::Type::VT_INT) {
                THROW_VARIABLE_TYPE_MISSMATCH_ERROR(arg.lexeme, Variables::TypeToString(Variables::Type::VT_INT), "",
                                                    Variables::TypeToString(value.type), arg);
            }
            return;
        }
 
        THROW_UNEXPECTED_TOKEN_ERROR(arg, "int literal or variable");
    }
 
 
    Value call(const std::vector<Value> & args, bool debug = false) const override {
        std::this_thread::sleep_for(std::chrono::seconds(args[0].ToInt()));
        return Value();
    }
};
 
#endif  // SLEEPFUNCTION_HPP