dl
dl_script.h
浏览该文件的文档.
1
12#pragma once
13
14#include <string>
15#include <unordered_map>
16#include <functional>
17#include <cassert>
18
19#include "io/dl_log.h"
22
24{
25 using hash_type = std::hash<std::string_view>;
26 using is_transparent = void;
27
28 std::size_t operator()(const char* str) const { return hash_type{}(str); }
29 std::size_t operator()(std::string_view str) const { return hash_type{}(str); }
30 std::size_t operator()(std::string const& str) const { return hash_type{}(str); }
31};
32
33namespace dl::Script
34{
35 void log_script(std::string_view str);
36
37
39{
40public:
42 {
43 public:
44 std::string _name;
45 void* _func;
47 std::vector<Variable::Type> _param;
48 };
49
51
52 void Call(std::string_view str);
53
54 void Test();
55
56 void RegisterFuntion(std::string_view name, Function func)
57 {
58 Variable var{ func };
59 _add_var(name, var);
60 }
61private:
62 std::unordered_map<std::string, Variable, string_hash, std::equal_to<>> _allGlobalVar;
63
64 void _add_var(std::string_view name, const Variable& var)
65 {
66 auto iter = _allGlobalVar.find(std::string{name});
67 if (iter != _allGlobalVar.end())
68 {
69 log_err("全局变量已存在:{}", name);
70 return;
71 }
72 _allGlobalVar[std::string{ name }] = var;
73 }
74 void _set_var(std::string_view name, const Variable& var)
75 {
76 _allGlobalVar[std::string{ name }] = var;
77 }
78 void _call_function(std::string_view name, std::vector<Variable>& param)
79 {
80 auto iter = _allGlobalVar.find(std::string{name});
81 if (iter == _allGlobalVar.end())
82 {
83 log_err("未找到全局变量:{}", name);
84 return;
85 }
86 Variable& var = iter->second;
87 Function func{ var };
88 func(param);
89 }
90
91 // 执行代码库
92 void Execute(Segment& seg)
93 {
94 switch (seg._type)
95 {
97 _set_var(seg._varName, seg._vecParam[0]);
98 break;
100 {
101 // 变量转换
102 std::vector<Variable> vec_param;
103 for (Variable& var : seg._vecParam)
104 {
105 if (var.GetType() == Variable::Type::VAR_LOCAL)
106 {
107 auto iter = _allGlobalVar.find(var.GetString());
108 if (iter != _allGlobalVar.end())
109 {
110 vec_param.push_back(iter->second);
111 }
112 else
113 {
114 log_err("不存在的局部变量:{}", var.GetString());
115 vec_param.push_back({});
116 }
117 }
118 else
119 vec_param.push_back(var);
120 }
121 _call_function(seg._varName, vec_param);
122 }
123 break;
124 default:
125 assert(0);
126 break;
127 }
128 }
129};
130
131}
132
133namespace dl
134{
135 extern Script::Manager* g_script;
136}
std::vector< Variable::Type > _param
Definition: dl_script.h:47
void RegisterFuntion(std::string_view name, Function func)
Definition: dl_script.h:56
void Call(std::string_view str)
日志系统
#define log_err(...)
Definition: dl_log.h:196
dl脚本代码段
dl脚本变量
std::function< int(std::vector< Variable >)> Function
void log_script(std::string_view str)
Definition: dl_array2.h:18
Script::Manager * g_script
std::hash< std::string_view > hash_type
Definition: dl_script.h:25
std::size_t operator()(std::string_view str) const
Definition: dl_script.h:29
void is_transparent
Definition: dl_script.h:26
std::size_t operator()(const char *str) const
Definition: dl_script.h:28
std::size_t operator()(std::string const &str) const
Definition: dl_script.h:30