* 脚本系统改进

This commit is contained in:
2024-07-03 18:02:17 +08:00
parent f1eb90106a
commit 15f72e4d03
3 changed files with 107 additions and 23 deletions
+97 -22
View File
@@ -49,15 +49,20 @@ namespace dl
}
ScriptBlock ScriptSystem::Compile(std::string_view src)
ScriptBlock ScriptSystem::Compile(std::string_view src_raw)
{
script_print(ScriptLogType::E_debug, fmt::format("{:=^32}", "开始编译脚本"));
script_print(ScriptLogType::E_debug, fmt::format("源:\n{}", src));
script_print(ScriptLogType::E_debug, fmt::format("源:\n{}", src_raw));
ScriptBlock ret;
ret._name = src;
ret._name = src_raw;
ret._varSpaceIteral._index = SCRIPT_INDEX_VS_NAG_1;
// 预处理
std::string src = g_script->Preprocess(src_raw);
std::vector<ScriptSegment>& vec_seg = ret._allSeg;
std::size_t beg = 0;
@@ -84,14 +89,46 @@ namespace dl
return std::string_view{ str.begin(), end - beg };
#endif
};
// 构造word
auto fn_make_word = [&](ScriptToken::Type type) {
// 构造word 返回值 0正常、1失败
auto fn_make_word = [&](ScriptToken::Type type) -> int {
std::string_view word = fn_string();
if (!word.empty())
{
script_print(ScriptLogType::E_debug, fmt::format("匹配到token{}:[{}]", (int)type, word));
vec_token.push_back({ type, word });
if (type == ScriptToken::Type::OPERATOR)
{
// 操作符拆分、例如 ++-( 拆分为 ++、-、(
size_t beg = 0;
size_t end = word.size();
while (beg != end)
{
std::string_view word_sub{ word.data() + beg, word.data() + end };
if (g_script->IsOperator(word_sub))
{
script_print(ScriptLogType::E_debug,
fmt::format("匹配到token{}:[{}]", (int)type, word_sub));
vec_token.push_back({ type, word_sub });
beg = end;
end = word.size();
}
else
{
if (word_sub.size() == 1)
{
script_print(ScriptLogType::E_compile,
fmt::format("无效操作符:{} -> {}", word, word_sub));
return 1;
}
--end;
}
}
}
else
{
script_print(ScriptLogType::E_debug, fmt::format("匹配到token{}:[{}]", (int)type, word));
vec_token.push_back({ type, word });
}
}
return 0;
};
// 一段完成
auto fn_segment = [&]() {
@@ -233,6 +270,7 @@ namespace dl
while (true)
{
// end为当前字符
char ch = src[end];
switch (state)
{
@@ -268,7 +306,7 @@ namespace dl
++beg;
++end;
}
else if (ch == '(' || ch == '{' || ch == '[')
/*else if (ch == '(' || ch == '{' || ch == '[')
{
fn_make_word(ScriptToken::Type::NORMAL);
beg = end;
@@ -300,7 +338,7 @@ namespace dl
else
++end;
--level_brace;
}
}*/
else if (g_script->IsMaybeOperator(ch))
{
if (beg != end)
@@ -323,7 +361,7 @@ namespace dl
++end;
beg = end;
state = State::NORMAL;
state = State::FREE;
}
else
++end;
@@ -331,20 +369,16 @@ namespace dl
break;
case State::SYMBOL:
{
std::string_view word{ src.data() + beg, src.data() + end };
bool valid = g_script->IsOperator(word);
if (!valid)
if (g_script->IsMaybeOperator(ch))
{// 连续解析,后面再拆分
++end;
}
else
{
script_print(ScriptLogType::E_compile,
fmt::format("不是合法的操作符:{}", word));
return {};
fn_make_word(ScriptToken::Type::OPERATOR);
beg = end;
state = State::FREE;
}
script_print(ScriptLogType::E_debug, fmt::format("匹配到token{}:[{}]",
(int)ScriptToken::Type::OPERATOR, word));
vec_token.push_back({ ScriptToken::Type::OPERATOR, word });
beg = end;
++end;
state = State::NORMAL;
}
break;
default:
@@ -365,6 +399,47 @@ namespace dl
return ret;
}
std::string ScriptSystem::Preprocess(std::string_view src)
{
std::string ret;
int mode = 0; // 1.string 2.comment
for (size_t i = 0; i < src.size(); ++i)
{
const char& ch = src[i];
if (mode == 0)
{
if (ch == '\"')
{
mode = 1;
ret.push_back(ch);
}
else
{
if (ch == '/'
&& i + 1 < src.size()
&& src[i + 1] == '/')
mode = 2;
else
ret.push_back(ch);
}
}
else if (mode == 1)
{
if(ch == '\"')
mode = 0;
ret.push_back(ch);
}
else
{
if (ch == '\n')
{
mode = 0;
}
}
}
return ret;
}
bool ScriptSystem::IsOperator(std::string_view str)
{
return _allOperator.contains(str);