/** * @file dl_script.h * @brief dl脚本语言 * * * @version 1.0 * @author lveyou * @date 23-11-27 * * @note */ #pragma once #include #include #include #include "io/dl_log.h" struct string_hash { using hash_type = std::hash; using is_transparent = void; std::size_t operator()(const char* str) const { return hash_type{}(str); } std::size_t operator()(std::string_view str) const { return hash_type{}(str); } std::size_t operator()(std::string const& str) const { return hash_type{}(str); } }; namespace dl { void log_script(std::string_view str); class Script { public: using Function = std::function)>; class Variable { public: enum class Type { NIL, NUMBER, STRING, FUNCTION, TABLE }; Variable() : _type{ Type::NIL }, _func{nullptr} {} ~Variable(){} Variable& operator=(const Variable& b) { _type = b._type; _func = b._func; return *this; } union { Function _func; }; Type _type; }; class FunctionInfo { public: std::string _name; void* _func; Variable::Type _ret; std::vector _param; }; Script(); void Call(std::string_view str); void Test(); void RegisterFuntion(std::string_view name, Function func) { Variable var; var._type = Variable::Type::FUNCTION; var._func = func; _add_var(name, var); } private: std::unordered_map> _allGlobalVar; void _add_var(std::string_view name, const Variable& var) { auto iter = _allGlobalVar.find(name); if (iter != _allGlobalVar.end()) { log_err("全局变量已存在:{}", name); return; } _allGlobalVar[std::string{ name }] = var; } void _call_function(std::string_view name, std::vector& param) { auto iter = _allGlobalVar.find(name); if (iter == _allGlobalVar.end()) { log_err("未找到全局变量:{}", name); return; } Variable& var = iter->second; if (var._type != Variable::Type::FUNCTION) { log_err("变量不是函数:{}", name); return; } var._func(param); } }; extern Script* g_script; }