📄 conditionalexpression.vb
字号:
If ToDouble(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Double.ToString)
bErr = True
End If
Else
If ToDouble(RSide, op2) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, RSide.GetType.ToString, KS.Double.ToString)
bErr = True
End If
If ToDouble(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Double.ToString)
bErr = True
End If
End If
If bErr Then
LSide = CDbl(0)
ElseIf DoAdd Then
LSide = op1 + op2
Else
LSide = op1 - op2
End If
End If
End While
Result = LSide
Return True
End Function
Function RuleStringConcat(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing, RSide As Object = Nothing
If RuleAdditionSubtractionStringConcat(LSide) = False Then Return False
While Reader.Peek.Equals(KS.Concat)
Reader.Next()
RuleExpression(RSide)
Dim op1, op2 As String
Dim bErr As Boolean
op1 = LSide.ToString
op2 = RSide.ToString
If bErr Then
LSide = ""
Else
LSide = op1 & op2
End If
End While
Result = LSide
Return True
End Function
Function RuleArithmeticBitshift(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing, RSide As Object = Nothing
If RuleStringConcat(LSide) = False Then Return False
While Reader.Peek.Equals(KS.ShiftLeft, KS.ShiftRight)
Dim DoLeft As Boolean
DoLeft = Reader.Peek.Equals(KS.ShiftLeft)
Reader.Next()
RuleExpression(RSide)
Dim op1 As Double, op2 As Double
Dim bErr As Boolean
If ToDouble(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Long.ToString)
bErr = True
End If
If ToDouble(RSide, op2) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, RSide.GetType.ToString, KS.Integer.ToString)
bErr = True
End If
If op1 < Long.MinValue OrElse op1 > Long.MaxValue Then
Compiler.Report.ShowMessage(Messages.VBNC30439, KS.Long.ToString)
ElseIf op2 < Integer.MinValue OrElse op2 > Integer.MaxValue Then
Compiler.Report.ShowMessage(Messages.VBNC30439, KS.Integer.ToString)
End If
If bErr Then
LSide = CDbl(0)
ElseIf DoLeft Then
LSide = CLng(op1) << CInt(op2)
Else
LSide = CLng(op1) >> CInt(op2)
End If
End While
Result = LSide
Return True
End Function
Function RuleRelational(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing, RSide As Object = Nothing
If RuleArithmeticBitshift(LSide) = False Then Return False
While Reader.Peek.Equals(KS.Equals, KS.NotEqual, KS.GT, KS.LT, KS.GE, KS.LE)
Dim DoWhat As KS = Reader.Peek.Symbol
Reader.Next()
RuleExpression(RSide)
'Compiler.Report.WriteLine(String.Format("RuleRelational: " & DoWhat.ToString() & ", Left={0}, Right={1}", LSide, RSide) & Reader.Current.Location.ToString())
Try
Select Case DoWhat
Case KS.Equals
LSide = Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(LSide, RSide, True)
Case KS.NotEqual
LSide = Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(LSide, RSide, True)
Case KS.GT
LSide = Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectGreater(LSide, RSide, True)
Case KS.LT
LSide = Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectLess(LSide, RSide, True)
Case KS.GE
LSide = Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectGreaterEqual(LSide, RSide, True)
Case KS.LE
LSide = Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectLessEqual(LSide, RSide, True)
Case Else
Throw New InternalException(Me)
End Select
Catch ex As Exception
Helper.AddError(ex.Message & VB.vbNewLine & ex.StackTrace)
Return False
End Try
End While
Result = LSide
Return True
End Function
Function RuleNot(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing
If Reader.Peek.Equals(KS.Not) Then
Reader.Next()
RuleNot = RuleRelational(LSide)
Dim op1 As Boolean
If ToBoolean(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Boolean.ToString)
LSide = 0
Else
LSide = Not op1
End If
Else
If RuleRelational(LSide) = False Then
Return False
End If
End If
Result = LSide
Return True
End Function
Function RuleAnd_AndAlso(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing, RSide As Object = Nothing
If RuleNot(LSide) = False Then Return False
While Reader.Peek.Equals(KS.And, KS.AndAlso)
Dim DoAlso As Boolean
DoAlso = Reader.Peek.Equals(KS.AndAlso)
Reader.Next()
RuleExpression(RSide)
Dim op1 As Boolean, op2 As Boolean
Dim bErr As Boolean
If ToBoolean(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Boolean.ToString)
bErr = True
End If
If ToBoolean(RSide, op2) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, RSide.GetType.ToString, KS.Boolean.ToString)
bErr = True
End If
If bErr Then
LSide = False
ElseIf DoAlso Then
LSide = op1 AndAlso op2 'Since its a constant expression, there's no difference between And & AndAlso
Else
LSide = op1 And op2
End If
End While
Result = LSide
Return True
End Function
Function RuleOr_OrElse(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing, RSide As Object = Nothing
If RuleAnd_AndAlso(LSide) = False Then Return False
While Reader.Peek.Equals(KS.Or, KS.OrElse)
Dim DoElse As Boolean
DoElse = Reader.Peek.Equals(KS.OrElse)
Reader.Next()
RuleExpression(RSide)
Dim op1 As Boolean, op2 As Boolean
Dim bErr As Boolean
If ToBoolean(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Boolean.ToString)
bErr = True
End If
If ToBoolean(RSide, op2) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, RSide.GetType.ToString, KS.Boolean.ToString)
bErr = True
End If
If bErr Then
LSide = False
ElseIf DoElse Then
LSide = op1 OrElse op2 'Since its a constant expression, there's no difference between Or & OrElse
Else
LSide = op1 Or op2
End If
End While
Result = LSide
Return True
End Function
Function RuleXor(ByRef Result As Object) As Boolean
Dim LSide As Object = Nothing, RSide As Object = Nothing
If RuleOr_OrElse(LSide) = False Then Return False
While Reader.Peek.Equals(KS.Xor)
Reader.Next()
RuleExpression(RSide)
Dim op1 As Boolean, op2 As Boolean
Dim bErr As Boolean
If ToBoolean(LSide, op1) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, LSide.GetType.ToString, KS.Boolean.ToString)
bErr = True
End If
If ToBoolean(RSide, op2) = False Then
Compiler.Report.ShowMessage(Messages.VBNC30748, RSide.GetType.ToString, KS.Boolean.ToString)
bErr = True
End If
If bErr Then
LSide = False
Else
LSide = op1 Xor op2
End If
End While
Result = LSide
Return True
End Function
Function RuleExpression(ByRef Result As Object) As Boolean
Return RuleXor(Result)
End Function
Overloads Function Parse(ByRef Result As Object) As Boolean
Parse = RuleExpression(Result)
End Function
'Helper conversion functions
Private Function ToDouble(ByVal value As Object, ByRef Result As Double) As Boolean
Dim vTp As Type = value.GetType
If Helper.CompareType(vTp, Compiler.TypeCache.System_Byte) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_Decimal) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_Double) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_Int32) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_Int64) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_SByte) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_Int16) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_Single) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_UInt32) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_UInt64) OrElse _
Helper.CompareType(vTp, Compiler.TypeCache.System_UInt16) Then
Result = CDbl(value)
Return True
Else
Return False
End If
End Function
Private Function ToBoolean(ByVal value As Object, ByRef Result As Boolean) As Boolean
Result = CBool(value)
Return True
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -