diff --git a/dl/script/dl_script.cpp b/dl/script/dl_script.cpp index 19a263a7..5353c78e 100644 --- a/dl/script/dl_script.cpp +++ b/dl/script/dl_script.cpp @@ -101,7 +101,7 @@ namespace dl if (g_script->IsOperator(word_sub)) { script_print(ScriptLogType::E_debug, - fmt::format("操作符{}:[{}]", (int)type, word_sub)); + fmt::format("操作符:[{}]", word_sub)); vec_token.push_back({ type, word_sub }); beg = end; end = word.size(); @@ -120,12 +120,12 @@ namespace dl } else if(type == ScriptToken::Type::STRING) { - script_print(ScriptLogType::E_debug, fmt::format("字符串{}:[\"{}\"]", (int)type, word)); + script_print(ScriptLogType::E_debug, fmt::format("字符串:[\"{}\"]", word)); vec_token.push_back({ type, word }); } else { - script_print(ScriptLogType::E_debug, fmt::format("token {}:[{}]", (int)type, word)); + script_print(ScriptLogType::E_debug, fmt::format("token :[{}]", word)); vec_token.push_back({ type, word }); } } @@ -135,7 +135,7 @@ namespace dl auto fn_expr = [&](ScriptSegment& seg) { if (vec_token.empty()) { - log_warn("空表达式!"); + script_print(ScriptLogType::E_debug, "空行"); return; } @@ -501,7 +501,7 @@ namespace dl { auto iter = _allOperator.find(str); if (iter == _allOperator.end()) - return INT_MAX; + return -1; return iter->second; } @@ -582,8 +582,8 @@ namespace dl if (token._type == ScriptToken::STRING) ret = block._varSpaceIteral.AddString(token._str); else - {// 字面量类型有限 数字、字符串、表 - ret._indexVarSpace = SCRIPT_INDEX_VS_NAG_1; + { + // 字面量类型有限 数字、字符串、表 StringType type = String::GetType(token._str); switch (type) { @@ -599,6 +599,8 @@ namespace dl default: assert(0); } + // 赋值后再设置 + ret._set_space_index(SCRIPT_INDEX_VS_ITERAL); } return ret; } @@ -615,16 +617,18 @@ namespace dl return ScriptOperator::E_mul; if (str == "/") return ScriptOperator::E_div; - return {}; + if (str == ",") + return ScriptOperator::E_comma; + return ScriptOperator::E_error; } dl::ScriptVar ScriptSystem::OpArithmetic(ScriptVM& vm, ScriptOperator op, const ScriptVar& v0, const ScriptVar& v1) { ScriptVar ret; - if (v0._type == ScriptVarType::E_int) + if (v0.GetType() == ScriptVarType::E_int) { ScriptInt vv0 = v0.ToInt(); - if (v1._type == ScriptVarType::E_int) + if (v1.GetType() == ScriptVarType::E_int) { ScriptInt vv1 = v1.ToInt(); ScriptInt vv_ret; @@ -649,10 +653,10 @@ namespace dl ret.FromInt(vv_ret); } } - else if (v0._type == ScriptVarType::E_string) + else if (v0.GetType() == ScriptVarType::E_string) { ScriptString vv0 = vm.GetVariableString(v0); - if (v1._type == ScriptVarType::E_string) + if (v1.GetType() == ScriptVarType::E_string) { ScriptString vv1 = vm.GetVariableString(v1); ScriptString vv_ret; @@ -672,7 +676,7 @@ namespace dl ret = vm._varSpaceTemp.AddString(vv_ret); } } - if (ret._type == ScriptVarType::E_null) + if (ret.GetType() == ScriptVarType::E_null) { script_print(ScriptLogType::E_err, fmt::format("OP:算术运算失败:{} {} {}", v0.GetTypeString(), dl::to_string(op), v1.GetTypeString())); @@ -682,41 +686,61 @@ namespace dl void ScriptSystem::OpAssign(ScriptVM& vm, ScriptVar& v0, const ScriptVar& v1) { - if (v0._type == ScriptVarType::E_null) + if (v0.GetType() == ScriptVarType::E_null) { - v0._type = v1._type; + // nil -> 构造实际的变量 + if (v1.GetType() == ScriptVarType::E_string) + { + v0 = vm.GetVariableSpaceRef(v0)->AddString(""); + } + else + { + // nil -> 基础类型默认值0 + v0.SetType(v1.GetType()); + v0.SetValue(0); + } } - if (v0._type == ScriptVarType::E_int) + if (v0.GetType() == ScriptVarType::E_int) { - if (v1._type == ScriptVarType::E_int) + if (v1.GetType() == ScriptVarType::E_int) { ScriptInt vv1 = v1.ToInt(); v0.FromInt(vv1); + return; } - else if (v1._type == ScriptVarType::E_float) + else if (v1.GetType() == ScriptVarType::E_float) { ScriptFloat vv1 = v1.ToFloat(); v0.FromInt((ScriptInt)vv1); + return; } } - else if (v0._type == ScriptVarType::E_float) + else if (v0.GetType() == ScriptVarType::E_float) { - if (v1._type == ScriptVarType::E_int) + if (v1.GetType() == ScriptVarType::E_int) { ScriptInt vv1 = v1.ToInt(); v0.FromFloat((ScriptFloat)vv1); + return; } - else if (v1._type == ScriptVarType::E_float) + else if (v1.GetType() == ScriptVarType::E_float) { ScriptFloat vv1 = v1.ToFloat(); v0.FromFloat(vv1); + return; } } - else + else if (v0.GetType() == ScriptVarType::E_string) { - script_print(ScriptLogType::E_err, - fmt::format("OP:赋值失败:{} = {}", v0.GetTypeString(), v1.GetTypeString())); + if (v1.GetType() == ScriptVarType::E_string) + { + ScriptString vv1 = vm.GetVariableString(v1); + vm.SetVariableString(v0, vv1); + return; + } } + script_print(ScriptLogType::E_err, + fmt::format("OP:赋值失败:{} = {}", v0.GetTypeString(), v1.GetTypeString())); } int ex_script_print(ScriptVM& vm, ScriptParam param) @@ -748,12 +772,12 @@ namespace dl _allKeyword["for"] = 0; _allKeyword["goto"] = 0; - _allOperator["=="] = 0; - _allOperator["+"] = 10; - _allOperator["-"] = 10; - _allOperator["*"] = 20; - _allOperator["/"] = 20; - _allOperator["="] = 30; + _allOperator["="] = 10; + _allOperator["=="] = 15; + _allOperator["+"] = 20; + _allOperator["-"] = 21; + _allOperator["*"] = 22; + _allOperator["/"] = 23; _allOperator[","] = 50; _allOperator["("] = SCRIPT_OPERATOR_PRIORITY_PARENTHESES_L; _allOperator[")"] = SCRIPT_OPERATOR_PRIORITY_PARENTHESES_R; @@ -842,13 +866,13 @@ namespace dl if(_bThrow) script_print(ScriptLogType::E_info, fmt::format("执行代码块出错:\n{}", block.GetSource())); else - script_print(ScriptLogType::E_info, fmt::format("执行代码块完成:\n{}", block.GetSource())); + script_print(ScriptLogType::E_info, fmt::format("执行代码块完成", "")); } ScriptVM::ScriptVM() : _bThrow{ false }, _varSpaceGlobal{ 0 , 0 }, - _varSpaceTemp{ SCRIPT_INDEX_VS_NAG_2 , 0 } + _varSpaceTemp{ SCRIPT_INDEX_VS_TEMP , 0 } { g_script->CopyVariableToVm(*this); _allVarSpace.push_back(&_varSpaceGlobal); @@ -878,11 +902,11 @@ namespace dl case ScriptOperator::E_div: _op_arithmetic(op); break; - //break; - //break; - //break; + case ScriptOperator::E_comma: + _op_comma(); + break; default: - script_print(ScriptLogType::E_damage, fmt::format("未知操作符:{}", (int)op)); + script_print(ScriptLogType::E_damage, fmt::format("未知操作符:{}", token._str)); break; } } @@ -906,7 +930,10 @@ namespace dl func(*this, _param); } else - assert(0); + { + script_print(ScriptLogType::E_damage, + fmt::format("剩余栈错误:{}", dl::to_string(_stackVar))); + } int a = 3; } @@ -984,12 +1011,27 @@ namespace dl _return._str = t0._str; } + void ScriptVM::_op_comma() + { + ScriptVar& var0 = _get_stack_var_top(1); + ScriptVar& var1 = _get_stack_var_top(0); + _stack_pop(2); + + //g_script->OpAssign(*this, var0, var1); + + //Creat + + _stackVar.push_back(SCRIPT_INDEX_NAG_1); + _return._type = ScriptExprTokenType::ESET_variable; + _return._value = var0; + _return._str = "vector"; + } + ScriptVar& ScriptVM::_get_variable(const ScriptExprToken& t) { if (t._type == ScriptExprTokenType::ESET_literal) {// 字面量 - _varIiteral = t._value; - return _varIiteral; + return (ScriptVar&)t._value; } // 空名变量视为返回值 if (t._str.empty()) @@ -1002,11 +1044,11 @@ namespace dl // 新建 ScriptVar& var = _allVar[t._str]; if (g_script->IsGlobalVariable(t._str)) - var._indexVarSpace = 0; + var._set_space_index(0); else { assert(_allVarSpace.size() > 1); - var._indexVarSpace = (ScriptIndexVS)_allVarSpace.size() - 1; + var._set_space_index((ScriptIndexVS)_allVarSpace.size() - 1); } return var; } @@ -1017,7 +1059,7 @@ namespace dl { for (auto iter = _allVar.begin(); iter != _allVar.end();) { - if (iter->second._indexVarSpace == vs->GetIndex()) + if (iter->second.GetSpaceIndex() == vs->GetIndex()) iter = _allVar.erase(iter); else ++iter; @@ -1025,32 +1067,48 @@ namespace dl int x = 3; } - dl::ScriptFunction ScriptVM::GetVariableFunction(ScriptVar var) + dl::ScriptVarSpace* ScriptVM::GetVariableSpaceRef(ScriptVar var) { - if(var._indexVarSpace == SCRIPT_INDEX_VS_NAG_1) - return _block->_varSpaceIteral.GetFunction(var._value); - if (var._indexVarSpace == SCRIPT_INDEX_VS_NAG_2) - return _varSpaceTemp.GetFunction(var._value); - if (var._indexVarSpace >= _allVarSpace.size()) + if (var.GetSpaceIndex() == SCRIPT_INDEX_VS_TEMP) + return &_varSpaceTemp; + if (var.GetSpaceIndex() >= _allVarSpace.size()) { script_print(ScriptLogType::E_damage, "变量错误!"); return {}; } - return _allVarSpace[var._indexVarSpace]->GetFunction(var._value); + return _allVarSpace[var.GetSpaceIndex()]; + } + + const dl::ScriptVarSpace* ScriptVM::GetVariableSpace(ScriptVar var) + { + if (var.GetSpaceIndex() == SCRIPT_INDEX_VS_ITERAL) + return &_block->_varSpaceIteral; + if (var.GetSpaceIndex() == SCRIPT_INDEX_VS_TEMP) + return &_varSpaceTemp; + if (var.GetSpaceIndex() >= _allVarSpace.size()) + { + script_print(ScriptLogType::E_damage, "变量错误!"); + return {}; + } + return _allVarSpace[var.GetSpaceIndex()]; + } + + dl::ScriptFunction ScriptVM::GetVariableFunction(ScriptVar var) + { + auto space = GetVariableSpace(var); + return space->GetFunction(var.GetIndex()); } dl::ScriptString ScriptVM::GetVariableString(ScriptVar var) { - if (var._indexVarSpace == SCRIPT_INDEX_VS_NAG_1) - return _block->_varSpaceIteral.GetString(var._value); - if (var._indexVarSpace == SCRIPT_INDEX_VS_NAG_2) - return _varSpaceTemp.GetString(var._value); - if (var._indexVarSpace >= _allVarSpace.size()) - { - script_print(ScriptLogType::E_damage, "变量错误!"); - return {}; - } - return _allVarSpace[var._indexVarSpace]->GetString(var._value); + auto space = GetVariableSpace(var); + return space->GetString(var.GetIndex()); + } + + void ScriptVM::SetVariableString(ScriptVar var, ScriptString str) + { + auto space = GetVariableSpaceRef(var); + return space->SetString(var.GetIndex(), str); } std::string ScriptVM::Variable2String(const ScriptVar& var) @@ -1152,6 +1210,8 @@ namespace dl return "*"; case dl::ScriptOperator::E_div: return "/"; + case dl::ScriptOperator::E_comma: + return "/"; default: return "error_op"; } diff --git a/dl/script/dl_script.h b/dl/script/dl_script.h index 669291a0..bfa57455 100644 --- a/dl/script/dl_script.h +++ b/dl/script/dl_script.h @@ -39,8 +39,9 @@ namespace dl constexpr ScriptIndex SCRIPT_INDEX_NAG_1 = (ScriptIndex)-1; // 返回值 constexpr ScriptIndexVS SCRIPT_INDEX_VS_0 = 0; // 全局空间 - constexpr ScriptIndexVS SCRIPT_INDEX_VS_NAG_1 = (ScriptIndexVS)-1; // 字面量 - constexpr ScriptIndexVS SCRIPT_INDEX_VS_NAG_2 = (ScriptIndexVS)-2; // 临时变量 + constexpr ScriptIndexVS SCRIPT_INDEX_VS_ITERAL = (ScriptIndexVS)-3; // 字面量 + constexpr ScriptIndexVS SCRIPT_INDEX_VS_TEMP = (ScriptIndexVS)-2; // 临时变量 + constexpr ScriptIndexVS SCRIPT_INDEX_VS_ERROR = (ScriptIndexVS)-1; // 初始变量 //! 变量类型(用户层面) enum class ScriptVarType : ScriptIndex @@ -57,38 +58,55 @@ namespace dl E_operator, // 操作符 E_max }; + //! 是基础类型 + inline bool IsBaseType(ScriptVarType type) + { + return type == ScriptVarType::E_null + || type == ScriptVarType::E_int + || type == ScriptVarType::E_float + || type == ScriptVarType::E_userdata + || type == ScriptVarType::E_operator; + } //! 操作类型 enum class ScriptOperator : ScriptIndex { E_assign, // 赋值 - E_add, // + E_add, // E_sub, E_mul, E_div, + E_comma, // 逗号,视为 push_back + E_error }; std::string_view to_string(ScriptOperator op); //! 变量(大小严格控制) struct ScriptVar { + friend class ScriptVarSpace; public: ScriptVar() { _type = ScriptVarType::E_null; + _indexVarSpace = SCRIPT_INDEX_VS_ERROR; + _value = 0; } void FromInt(ScriptInt v) { + assert(_indexVarSpace != SCRIPT_INDEX_VS_ITERAL); _type = ScriptVarType::E_int; *(ScriptInt*)&_value = *(ScriptInt*)&v; } void FromFloat(ScriptFloat v) { + assert(_indexVarSpace != SCRIPT_INDEX_VS_ITERAL); _type = ScriptVarType::E_float; *(ScriptFloat*)&_value = *(ScriptFloat*)&v; } void FromOperator(ScriptOperator v) { + assert(_indexVarSpace != SCRIPT_INDEX_VS_ITERAL); _type = ScriptVarType::E_operator; *(ScriptOperator*)&_value = *(ScriptOperator*)&v; } @@ -112,7 +130,38 @@ namespace dl ScriptVarType GetType() const { return _type; } std::string_view GetTypeString() const; - public: + + uint64_t GetValue() const + { + assert(IsBaseType(_type)); + return _value; + } + uint64_t GetIndex() const + { + assert(!IsBaseType(_type)); + return _value; + } + ScriptIndexVS GetSpaceIndex() const + { + return _indexVarSpace; + } + void SetValue(uint64_t v) + { + assert(IsBaseType(_type)); + assert(_indexVarSpace != SCRIPT_INDEX_VS_ITERAL); + _value = v; + } + void SetType(ScriptVarType type) + { + assert(_type == ScriptVarType::E_null); + _type = type; + } + + void _set_space_index(ScriptIndexVS index) + { + _indexVarSpace = index; + } + private: ScriptVarType _type; ScriptIndexVS _indexVarSpace; // 所在变量空间(基础类型需要释放,也需要标记) uint64_t _value; // 基础类型直接存储(或变量下标) @@ -213,6 +262,10 @@ namespace dl _vecFunction.push_back(v); return ret; } + void SetString(size_t i, const ScriptString& str) + { + _vecString[i] = str; + } const ScriptString& GetString(size_t i) const { @@ -257,7 +310,7 @@ namespace dl struct ScriptBlock { ScriptBlock() : - _varSpaceIteral{ SCRIPT_INDEX_VS_NAG_1, 0 } + _varSpaceIteral{ SCRIPT_INDEX_VS_ITERAL, 0 } {} std::vector _allSeg; @@ -323,14 +376,12 @@ namespace dl const ScriptBlock* _block; // 名字 -> 变量(变量) std::unordered_map _allVar; - // 全局变量空间(包含字面量,字面量没有名字) + // 全局变量空间 ScriptVarSpace _varSpaceGlobal; // 变量空间 std::vector _allVarSpace; // 函数参数 ScriptParam _param; - // 字面量 - ScriptVar _varIiteral; //-------------------------------expr------------------------------ // 返回值(可变) @@ -357,15 +408,21 @@ namespace dl void _op_arithmetic(ScriptOperator op); void _op_assign(); + void _op_comma(); // 获取(新建)变量,新建则在当前变量空间初始化 ScriptVar& _get_variable(const ScriptExprToken& var0); // 移除变量空间后,从变量表中移除 void _delete_var_space(ScriptVarSpace* vs); + // 返回变量所在空间 + ScriptVarSpace* GetVariableSpaceRef(ScriptVar var); + const ScriptVarSpace* GetVariableSpace(ScriptVar var); //------------------------------------变量获取-------------------------------- ScriptFunction GetVariableFunction(ScriptVar var); ScriptString GetVariableString(ScriptVar var); + //------------------------------------变量设置-------------------------------- + void SetVariableString(ScriptVar var, ScriptString str); }; @@ -428,7 +485,7 @@ namespace dl private: // 关键字 std::unordered_map _allKeyword; - // 操作符、优先级(越小越优先) + // 操作符、优先级(越大越优先) std::unordered_map _allOperator; // 变量空间池 std::list _listVarSpaceFree; diff --git a/dl/script/dl_script_test.h b/dl/script/dl_script_test.h index 0704af69..632b2eba 100644 --- a/dl/script/dl_script_test.h +++ b/dl/script/dl_script_test.h @@ -31,7 +31,9 @@ print "结果应该为0" constexpr std::string_view SCRIPT_TEST_STRING = R"( a = "hello" b = "dl" +c = a + b print a+" "+b+"!" +print c )"; // 断言 constexpr std::string_view SCRIPT_TEST_ASSERT = R"( @@ -63,6 +65,14 @@ print c a = 13 vec = { 1,3, 5, 7 ,9 ,11} print vec +)"; + // function + constexpr std::string_view SCRIPT_TEST_FUNCTION = R"( +function add(a, b) +{ + return a + b +} +print(add(2, 3)) )"; inline void TestScript(std::string_view str) @@ -81,5 +91,6 @@ print vec TestScript(SCRIPT_TEST_ASSERT); TestScript(SCRIPT_TEST_SCOPE); TestScript(SCRIPT_TEST_VECTOR); + TestScript(SCRIPT_TEST_FUNCTION); } } \ No newline at end of file diff --git a/project/dl_chess b/project/dl_chess index 3b7efbae..fe8163ab 160000 --- a/project/dl_chess +++ b/project/dl_chess @@ -1 +1 @@ -Subproject commit 3b7efbaecb7b67897f5c780e5cd9b25fc1bf67cb +Subproject commit fe8163ab352bb7eee4f1f4da77c46c420c4bc147 diff --git a/project/dl_config b/project/dl_config index fc2d24fa..0bcbea58 160000 --- a/project/dl_config +++ b/project/dl_config @@ -1 +1 @@ -Subproject commit fc2d24fa0b55e2a7ed2ae72ad70cc887a87b259e +Subproject commit 0bcbea58ffe6a88350341597075277cef3f12876 diff --git a/project/dl_editor b/project/dl_editor index c1d44ff8..37ea7da5 160000 --- a/project/dl_editor +++ b/project/dl_editor @@ -1 +1 @@ -Subproject commit c1d44ff82fe17e0c5f483ebe2315a0e0cde036ef +Subproject commit 37ea7da51411378ebf4d3d660a9dee65fe8538e9 diff --git a/project/dl_test/head.cpp b/project/dl_test/head.cpp index d724ff35..0ec1a0a6 100644 --- a/project/dl_test/head.cpp +++ b/project/dl_test/head.cpp @@ -14,6 +14,9 @@ void Test() Tool::Manager* g_Tool; void Init() { + //TestScriptAll(); + TestScript(SCRIPT_TEST_VECTOR); + g_Tool = Tool::Manager::Create(); g_Tool->SetVisible(false); diff --git a/project/hs b/project/hs index bc654aea..3ca3657e 160000 --- a/project/hs +++ b/project/hs @@ -1 +1 @@ -Subproject commit bc654aea907ecdc18c85c078edf2d4c2a945da34 +Subproject commit 3ca3657e4f9c6f69375e18860a6913725ecd5700