* 源代码位置移动

This commit is contained in:
2023-12-23 16:39:54 +08:00
parent 02fc9d9532
commit c94d3296e0
56 changed files with 123 additions and 74 deletions
+154
View File
@@ -0,0 +1,154 @@
#include "dl_script.h"
namespace dl
{
void log_script(std::string_view str)
{
log_msg0(str);
}
int Tss_print(const std::vector<std::string_view>& param)
{
if (param.size() < 2)
return -1;
log_script(param[1]);
return 0;
}
Script::Script()
{
RegisterFuntion("print", Tss_print);
}
void Script::Call(std::string_view str)
{
constexpr bool SHOW_LOG = true;
if (SHOW_LOG)
{
log_test("{:=^32}", "开始执行脚本");
log_test("源:\n{}", str);
}
std::size_t beg = 0;
std::size_t end = 0;
std::vector<std::string_view> vec_word;
enum class State
{
FREE,// 空闲
NORMAL,// 单词匹配
STRING,// 字符串
};
State state = State::FREE;
// 构造word
auto fn_string = [&]() {
return std::string_view{ str.begin() + beg, str.begin() + end };
};
// 单行完成
auto fn_line = [&]() {
if (SHOW_LOG)
{
log_test("单行完成,词数量 {}", vec_word.size());
for (auto& iter : vec_word)
{
log_test("{}", iter);
}
_call_function(vec_word[0], vec_word);
}
++end;
};
while (true)
{
char ch = str[end];
switch (state)
{
case State::FREE:
{
if (ch == ' ' || ch == '\t' || ch == '\n')
{
++beg;
++end;
}
else
{
state = State::NORMAL;
++end;
}
}
break;
case State::NORMAL:
{
if (ch == ' ' || ch == '\t')
{
std::string_view word = fn_string();
if (SHOW_LOG)
{
log_test("匹配到word:[{}]", word);
}
vec_word.push_back(word);
++end;
beg = end;
}
else if (ch == '\n')
{
fn_line();
}
else if (ch == '\'' || ch == '\"')
{
state = State::STRING;
++beg;
++end;
}
else
++end;
}
break;
case State::STRING:
{
if (ch == '\'' || ch == '\"')
{
std::string_view word = fn_string();
if (SHOW_LOG)
{
log_test("匹配到string:\"{}\"", word);
}
vec_word.push_back(word);
++end;
beg = end;
state = State::NORMAL;
}
else
++end;
}
break;
default:
break;
}
if (end >= str.size())
{
fn_line();
break;
}
}
if (SHOW_LOG)
{
log_test("{:=^32}", "词法分析完成");
}
}
void Script::Test()
{
Call(R"(
print "hello lve!")"
);
}
}