A simple scripting language in C++
Ferenc Szontágh
2025-04-14 d7cd4947b37a168034e9fca2501d98553fdcc137
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
#ifndef VARIABLE_TYPES_HPP
#define VARIABLE_TYPES_HPP
 
#include <cstdint>
#include <string>
#include <unordered_map>
 
namespace Symbols::Variables {
 
enum class Type : std::uint8_t { VT_INT, VT_DOUBLE, VT_STRING, VT_BOOLEAN, VT_NULL, VT_UNDEFINED };
 
const std::unordered_map<std::string, Type> StringToTypeMap = {
    { "int",       Type::VT_INT       },
    { "double",    Type::VT_DOUBLE    },
    { "string",    Type::VT_STRING    },
    { "bool",      Type::VT_BOOLEAN   },
    { "boolean",   Type::VT_BOOLEAN   },
    { "null",      Type::VT_NULL      },
 
    { "undefined", Type::VT_UNDEFINED },
};
const std::unordered_map<Type, std::string> StypeToStringMap = {
    { Type::VT_INT,       "int"        },
    { Type::VT_DOUBLE,    "double"     },
    { Type::VT_STRING,    "string"     },
    { Type::VT_BOOLEAN,   "bool"       },
    { Type::VT_NULL,      "null"       },
    { Type::VT_UNDEFINED, "undeffined" },
};
 
inline static std::string TypeToString(Type type) {
    if (StypeToStringMap.find(type) != StypeToStringMap.end()) {
        return StypeToStringMap.at(type);
    }
    return "null";
};
 
inline static Type StringToType(const std::string & type) {
    if (StringToTypeMap.find(type) != StringToTypeMap.end()) {
        return StringToTypeMap.at(type);
    }
    return Type::VT_NULL;
};
 
};  // namespace Symbols
#endif  // VARIABLE_TYPES_HPP