📄 sexpeval.cpp
字号:
return 0;
}
static int EvalIsGTE( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[0].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 1;
}
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = false;
EndResult->NumValue = e[0].NumValue >= e[1].NumValue;
return 0;
}
static int EvalLeftShift( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
strcpy( EndResult->StrValue, e[0].StrValue+e[1].NumValue );
else
EndResult->NumValue = (e[1].NumValue>=32) ? 0 : (e[0].NumValue<<e[1].NumValue);
return 0;
}
static int EvalRightShift( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
{
strcpy( EndResult->StrValue, e[0].StrValue );
EndResult->StrValue[strlen(EndResult->StrValue)-e[1].NumValue] = '\0';
}
else
EndResult->NumValue = e[0].NumValue >> e[1].NumValue;
return 0;
}
static int EvalLeftRotate( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
{
UInt32 StrLen = strlen( e[0].StrValue );
UInt32 i;
if ( e[1].NumValue >= StrLen )
e[1].NumValue = e[1].NumValue % StrLen;
for ( i=0; i<StrLen-e[1].NumValue; i++ )
EndResult->StrValue[i] = e[0].StrValue[i+e[1].NumValue];
for ( i=0; i<e[1].NumValue; i++ )
EndResult->StrValue[StrLen-e[1].NumValue+i] = e[0].StrValue[i];
EndResult->StrValue[StrLen] = '\0';
}
else
{
if ( e[1].NumValue >= 32 )
e[1].NumValue = 32 % e[1].NumValue;
EndResult->NumValue = e[0].NumValue << e[1].NumValue;
EndResult->NumValue |= e[0].NumValue >> (32 - e[1].NumValue);
}
return 0;
}
static int EvalRightRotate( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
{
UInt32 StrLen = strlen( e[0].StrValue );
UInt32 i;
if ( e[1].NumValue >= StrLen )
e[1].NumValue = e[1].NumValue % StrLen;
for ( i=0; i<e[1].NumValue; i++ )
EndResult->StrValue[i] = e[0].StrValue[StrLen-e[1].NumValue+i];
for ( i=0; i<StrLen-e[1].NumValue; i++ )
EndResult->StrValue[i+e[1].NumValue] = e[0].StrValue[i];
EndResult->StrValue[StrLen] = '\0';
}
else
{
if ( e[1].NumValue >= 32 )
e[1].NumValue = 32 % e[1].NumValue;
EndResult->NumValue = e[0].NumValue >> e[1].NumValue;
EndResult->NumValue |= e[0].NumValue << (32 - e[1].NumValue);
}
return 0;
}
static int EvalAdd( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( (e[0].IsString ^ e[1].IsString) == 1 )
{
if ( ErrorMsg )
*ErrorMsg = "Cannot mix numerical and string values here.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
{
strcpy( EndResult->StrValue, e[0].StrValue );
strcat( EndResult->StrValue, e[1].StrValue );
}
else
EndResult->NumValue = e[0].NumValue + e[1].NumValue;
return 0;
}
static int EvalSubtract( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( !e[0].IsString && e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
{
strcpy( EndResult->StrValue, e[0].StrValue );
if ( EndResult->IsString )
{
char* Location = strstr( EndResult->StrValue, e[1].StrValue );
if ( Location )
{
for ( char* RightLocation=Location+strlen(e[1].StrValue); *RightLocation; RightLocation++,Location++ )
*Location = *RightLocation;
*Location = '\0';
}
}
else
EndResult->StrValue[strlen(EndResult->StrValue)-e[1].NumValue] = '\0';
}
else
EndResult->NumValue = e[0].NumValue - e[1].NumValue;
return 0;
}
static int EvalMultiply( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Expected a numerical value here.";
return 2;
}
EndResult->IsString = e[0].IsString;
if ( e[0].IsString )
{
EndResult->StrValue[0] = '\0';
for ( UInt32 i=0; i<e[1].NumValue; i++ )
strcat( EndResult->StrValue, e[0].StrValue );
}
else
EndResult->NumValue = e[0].NumValue * e[1].NumValue;
return 0;
}
static int EvalDivide( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[0].IsString || e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Division not supported for strings.";
return 1;
}
if ( e[1].NumValue == 0 )
{
if ( ErrorMsg )
*ErrorMsg = "Cannot divide by zero.";
return 2;
}
EndResult->IsString = false;
EndResult->NumValue = e[0].NumValue / e[1].NumValue;
return 0;
}
static int EvalModulus( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[0].IsString || e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Modulus not supported for strings.";
return 1;
}
EndResult->IsString = false;
EndResult->NumValue = e[0].NumValue % e[1].NumValue;
return 0;
}
static int EvalLogNot( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 1 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
EndResult->IsString = false;
EndResult->NumValue = !ISEXPRTRUE(0);
return 0;
}
static int EvalBitComplement( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 1 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
if ( e[0].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 1;
}
EndResult->IsString = false;
EndResult->NumValue = ~e[0].NumValue;
return 0;
}
static int EvalExponential( in const char* Expr, in UInt32* ExprStart, in UInt32* ExprEnd, in UInt32 nExprs, inout SValueDef* EndResult, in const char* NameSpace, out const char* *ErrorMsg )
{
SValueDef e[MaxExprsPerTemplate];
if ( nExprs < 2 )
{
if ( ErrorMsg )
*ErrorMsg = "Missing an expression after the operator.";
return 1;
}
DOEXPRESSION( 0 );
DOEXPRESSION( 1 );
if ( e[0].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 1;
}
if ( e[1].IsString )
{
if ( ErrorMsg )
*ErrorMsg = "Unexpected string value.";
return 2;
}
EndResult->IsString = false;
EndResult->NumValue = (UInt32)pow( (double)e[0].NumValue, (double)e[1].NumValue );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -