diff --git a/dl/script/dl_script.cpp b/dl/script/dl_script.cpp index 27d5c625..202deb80 100644 --- a/dl/script/dl_script.cpp +++ b/dl/script/dl_script.cpp @@ -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& 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); diff --git a/dl/script/dl_script.h b/dl/script/dl_script.h index cc64324b..960d8fd4 100644 --- a/dl/script/dl_script.h +++ b/dl/script/dl_script.h @@ -317,6 +317,9 @@ namespace dl public: ScriptBlock Compile(std::string_view src); + //! 预处理代码 + std::string Preprocess(std::string_view src); + //! 是否操作符 bool IsOperator(std::string_view str); //! 是否可能为操作符内容(变量名不可能出现) diff --git a/dlconsole/main.cpp b/dlconsole/main.cpp index 9b3032a3..166514a2 100644 --- a/dlconsole/main.cpp +++ b/dlconsole/main.cpp @@ -57,7 +57,13 @@ int main() // )"); ScriptBlock block = g_script->Compile(R"( -print(a+b)*c +a = 1 +b = 2 +c = 3 +print a+b*c +d = 4 +e = 5 +print a+((b+c)*d)*e )"); ScriptVM vm;