If Statements
As you should know, If statements have at least two parts to them: a condition, and a command to execute if this condition is met. They can also have a third part (the 'Else' in VB), which is executed if the condition is NOT met. The following function, when added to your CScriptParser class will add the ability to decide which of two command blocks to execute (or just whether or not to execute one) depending on the condition of a flag.
Public Function ExecuteCmdIf(a() As String) As Integer
Dim b(1) As String, c(0) As String
'Two temporary arrays of strings. b is an array
'of two strings, used to pass to the GetFlag
'function. c is an array of one string (yes,
'seems pointless, but it has to be like this to
'pass a single parameter, the name of a command
'block, to ExecuteBlock
Select Case a(0)
'The first parameter of your if statement is what
'KIND of condition it is to check; right now only
'the "Flag" type is implemented, but this allows
'for easy expansion.
Case "Flag"
b(0) = a(1) 'Fill out the b() array with the
b(1) = a(2) 'flag parameters
If ExecuteCmdGetFlag(b()) > 0 Then 'Check if the flag is set
c(0) = a(3)
ExecuteCmdExecuteBlock c()
'Sets the c() array to contain the name of the
'command block to be executed if the specified
'flag is set, then calls that block using
'ExecuteBlock
Else 'The flag was NOT set
If a(4) = "*" Then
'If the script specifies that there is no
'Else block (by using a * for it), exit the
'function
ExecuteCmdIf = 1
Exit Function
End If
'If there *was* an else block specified,
'execute it.
c(0) = a(4)
ExecuteCmdExecuteBlock c()
End If
Case Else
'If the specified condition type wasn't found,
'do a bunch of error type stuff :)
Beep
Debug.Print "ExecuteCmdIf: Invalid type: " & a(0)
ExecuteCmdIf = 0
Exit Function
End Select
ExecuteCmdIf = 1
End Function
An explanation
(Gee, I like that heading don't I! lol) Anyways, that function should be pretty self-explanatory with the comments in it. All that it does is this:
- Check what type of condition the script specified as the first parameter - a(0)
- Because the "Flag" type is the only one defined at this point, it will proceed into that portion of the code (if not, there's a problem, and an error is printed to the 'Immediate Pane' of VB)
- The second and third parameters, a(1) and a(2), are put into the array b(). This array is used to call the GetFlag function.
- If this call returns 1 (which means the flag is set), the only element of the array c() is filled with the fourth parameter of the If function: the name of the command block to be executed if the condition is true. Then, the ExecuteBlock command is called, using c() as it's parameter list.
- Otherwise, if the call to GetFlag returns 0, meaning the flag isn't set, a check is made on the fifth parameter, a(4). If a(4) is an asterisk (*), then there is no 'else' part to be executed, and the function exits. If it's anything but an asterisk, then the same as in #4 happens, c() is filled with the name of the command block to be executed if the condition was false, and it is passed to ExecuteBlock to be parsed.
One final note about this implementation of the If statement: it is not like the VB if statement where you place it 'around' your commands that you want conditionally executed. You tell it which command blocks to execute when the condition is true or false, in one line, like so:
If (Flag,5,7,blocktrue,blockfalse)
This would check if bit 7 of flag 5 was true; if it is, then execute the command block 'blocktrue', otherwise execute the commandblock 'blockfalse'.
|