* 增加逗号操作符解析、操作符优先级修正
This commit is contained in:
1 parent
8bc7316134
commit
f6872029c9
8 files changed
+203
-72
No files matched your search
+119
-59
@@ -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";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user