From WikiChip
Difference between revisions of "mirc/goto statements"
< mirc

(Error Handling)
m
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{mIRC Guide}}
+
{{mirc title|Goto Statements}}
 
'''Goto''' is a command that allows you to jump unconditionally to a specific location within a procedure. Gotos can 'jump' forward or backward within a script but they may not leave the alias or event itself (they cannot jump to any calling routine as well). The goto command tells mIRC to jump to another line which matches a label.
 
'''Goto''' is a command that allows you to jump unconditionally to a specific location within a procedure. Gotos can 'jump' forward or backward within a script but they may not leave the alias or event itself (they cannot jump to any calling routine as well). The goto command tells mIRC to jump to another line which matches a label.
 
+
 
Although in many cases the use of gotos can often lead to [[Wikipedia:spaghetti-code|spaghetti-code]]. and can usually be replaced with easier to read and follow [[While Loops - mIRC|while statements]] and [[Conditional statements - mIRC|if statements]], it is still important to understand this command and have it in your toolbox.
 
Although in many cases the use of gotos can often lead to [[Wikipedia:spaghetti-code|spaghetti-code]]. and can usually be replaced with easier to read and follow [[While Loops - mIRC|while statements]] and [[Conditional statements - mIRC|if statements]], it is still important to understand this command and have it in your toolbox.
  
Line 14: Line 14:
  
 
The label has to start with the colon symbol (:) and must be a single word.</p>
 
The label has to start with the colon symbol (:) and must be a single word.</p>
 +
 +
== Jumping ==
 +
 +
There is a neat difference in the way mIRC jumps backward and the way it jumps forward.
 +
To get mIRC to reach a goto label that is before the current position, mIRC must has seen that goto label, for example:
 +
<syntaxhighlight lang="mirc">
 +
alias test {
 +
if (a == b) {
 +
  :label
 +
}
 +
goto label
 +
}
 +
</syntaxhighlight>
 +
displays "* /goto: 'label' not found (line N, script.mrc)"
 +
since the condition does not get executed, the goto label is not seen by mIRC.
 +
 +
Change the code to:
 +
<syntaxhighlight lang="mirc">
 +
alias test {
 +
if (a == a) {
 +
  :label
 +
  inc -us %test
 +
if (%test == 2) return
 +
}
 +
goto label
 +
}
 +
</syntaxhighlight>
 +
Now the goto is seen by mIRC and you should see %test being increased to 1 and then to 2.
 +
 +
To get mIRC to jump to a goto label that is after the current position, this is less strict: mIRC parse the code without executing it, which means it is basically parsing the code according to major rules defining the language, the '{', '}', '|' and newlines tokens. so considering the following:
 +
<syntaxhighlight lang="mirc">
 +
alias test {
 +
goto label
 +
if (a == b) {
 +
  :label
 +
  inc -us %test
 +
if (%test == 2) return
 +
}
 +
goto label
 +
}
 +
</syntaxhighlight>The first /goto label statement gets mIRC to parse the rest as token, probably just words, and :label is found despite the if statement being false, %test is increased to 1 and then to 2, proving that the label is found from the first time with the first goto label (forward jump) and then backward since the label was found the first time.
  
 
== Conditional transfer of control ==
 
== Conditional transfer of control ==
Line 43: Line 84:
  
 
== Error Handling ==
 
== Error Handling ==
Although it is rare to use this feature, the "error" label is a designated goto section for error checking. If there is an error in the script, mIRC try to find an :error label in the current routine and will propagate backward to any calling routine and look for one there. This gives you the ability to continue with the script regardless of the error if you want. Note that mIRC is an error state in this case, after catching an error with :error, you must check for error with a typical /if statement to make sure you reached the :error because of an error and not because the script is just reaching that point. If you want to keep going with the code, use /reseterror as soon as possible after checking for $error (which return the error catched), this reset the error (and therefore $error's value) and you can go from that, if you try to use anything else before resetting the error or before checking for $error, mIRC can broke: when mIRC is in an error state, it must still process your code but it's unclear what you can do and what you can't do besides using /reseterror. But since you must check for $error, you would think mIRC is somehow forced to allow you to use the language to check for error, the exact list of features that are working in this situation are unknown, it's recommended not to do anything besides /reseterror.
+
Although it is rare to use this feature, the "error" label is a designated goto section for error checking.
 +
If there is an error in the script, mIRC try to find an :error label in the current routine.
 +
If no :error label is found in the current routine, mIRC will propagate the error backward to any calling routine and look for an :error label in those routines.
  
<syntaxhighlight lang="mirc">Alias Example {
+
 
  /*
+
This gives you the ability to continue with the script at any point regardless of the error, if you want.
     Second parameters of the $rand identifier is missing.
+
 
    Deliberate Error
+
 
  */
+
After catching an error with :error, you must check for an error by checking $error with a typical /if statement to make sure you reached the :error part because of an error and not because the script is just reaching that point.
 +
 
 +
If you want to keep going with the code, use /reseterror as soon as possible, this reset the error (and therefore $error's value), mIRC leaves the error state and you can safely execute anything you want.
 +
 
 +
If you don't use /reseterror, you can also use /return to tell mIRC to look for a different :error label in previous routines.
 +
If you don't use /reseterror or /return you must be careful:
 +
mIRC is in an error state, yet it must still process your code somehow to allow you to *at least* check for error and reset it, so the if statement feature probably always works, /echo itself also probably does always work but it's unclear what you can do and what you can't do. You would think that since mIRC is somehow forced to allow you to use the scripting engine to check for error and reset it, you could be able to do anything even without calling /reseterror first, this is not true (see examples), the exact list of features that are working in this situation are unknown, it's recommended not to do anything before using /reseterror in this situation.
 +
 
 +
<syntaxhighlight lang="mirc">
 +
/* EXAMPLE 1
 +
     This typical example shows that after reaching :error and without using /reseterror, /echo itself works, but see the next example, a lot of simple operation might be done there.
 +
    Since we don't halt or reset the error, mIRC will display both our echo and its own error for $rand.
 +
*/
 +
Alias Example {
 
   echo -a $rand(1,)
 
   echo -a $rand(1,)
 
   return
 
   return
Line 55: Line 111:
 
   echo -a Error: $error
 
   echo -a Error: $error
 
}
 
}
;In this case, we try to execute something else than /reseterror after reaching :error, for some reasons, that $regsubex is just too much, you should get the first echo 'ok' and an echo '> $str(ab,2000)' is expected but you shouldn't see it.
+
/* EXAMPLE 2
 
+
  In this case, we try to execute /echo again after a /if error, but on $regsubex, for some reasons, that $regsubex gets mIRC to silentely halts while inside a routine.
 +
  You should get the first echo 'ok' and an echo '> $str(ab,2000)' is expected, but you shouldn't see it.
 +
  Use /reseterror first and it works as expected.
 +
*/
 
alias testgoto {
 
alias testgoto {
 
   if
 
   if
Line 62: Line 121:
 
   echo -a ok | echo -a > $regsubex($str(ab,2000),/(a*)b*(a*)*b/,)
 
   echo -a ok | echo -a > $regsubex($str(ab,2000),/(a*)b*(a*)*b/,)
 
}
 
}
</syntaxhighlight>
+
/* EXAMPLE 3
 +
Although this example might look a little confusing, bear with me:
 +
Calling alias /Foo will echo what alias FooBar returns.
 +
Alias foobar will return what alias bar returns.
 +
Because alias bar errors out (since $mid is missing a few parameters), mIRC will look for an :error label in alias Bar but won't find any.
 +
So it will look in the previous routine, the alias FooBar. an :error label is found in the alias FooBar.
 +
It is executed, echoing Error followed by the error message and returning 1000 to alias foo.
 +
The /reseterror command effectively prevents mIRC from halting the script and allows it to finish executing.
 +
*/
  
== Error Resetting ==
+
Alias Foo {
Using the /reseterror you can continue on with the rest of the script. Not resetting the error will leave mIRC in a error state, if you do not reset the error, you can use /return to allow mirc to directly try to find a :error goto in the previous calling routine; if you do not use /return and mIRC is in an error state, you can't really keep going with the code, mIRC will not work correctly after using 'some function' but can operate correctly if you don't those function. If no :error goto is found after callingwhen an error occurs, mIRC will display is own shouldn't be trying to do anything else halt script execution (in such case, mIRC will keep looking for ":error" in all calling routines).
 
 
 
<syntaxhighlight lang="mirc">Alias Foo {
 
 
   echo -a $FooBar
 
   echo -a $FooBar
 
}
 
}
Line 79: Line 143:
 
   reseterror
 
   reseterror
 
   return 1000
 
   return 1000
}</syntaxhighlight>
+
}
 
+
</syntaxhighlight>
 
 
Although the code might look a little confusing, bear with me. Calling alias /Foo will echo what alias FooBar returns.  Alias foobar will return what alias bar returns. Because alias bar errors out (since $mid is missing a few parameters) , the "error" section of the foobar alias will get executed, echoing Error followed by the error message and returning 1000 to alias foo. The /reseterror command effectively prevents mIRC from halting the script and allows it to finish executing.
 
  
[[Category:mIRC]]
+
[[Category:mIRC|goto]]

Latest revision as of 09:34, 22 September 2017

Goto is a command that allows you to jump unconditionally to a specific location within a procedure. Gotos can 'jump' forward or backward within a script but they may not leave the alias or event itself (they cannot jump to any calling routine as well). The goto command tells mIRC to jump to another line which matches a label.

Although in many cases the use of gotos can often lead to spaghetti-code. and can usually be replaced with easier to read and follow while statements and if statements, it is still important to understand this command and have it in your toolbox.

Syntax[edit]

The goto command has the following syntax:

goto label

A line is labeled using the following syntax:

:label

The label has to start with the colon symbol (:) and must be a single word.</p>

Jumping[edit]

There is a neat difference in the way mIRC jumps backward and the way it jumps forward. To get mIRC to reach a goto label that is before the current position, mIRC must has seen that goto label, for example:

alias test {
if (a == b) {
  :label
}
goto label
}

displays "* /goto: 'label' not found (line N, script.mrc)" since the condition does not get executed, the goto label is not seen by mIRC.

Change the code to:

alias test {
if (a == a) {
  :label
  inc -us %test
if (%test == 2) return
}
goto label
}

Now the goto is seen by mIRC and you should see %test being increased to 1 and then to 2.

To get mIRC to jump to a goto label that is after the current position, this is less strict: mIRC parse the code without executing it, which means it is basically parsing the code according to major rules defining the language, the '{', '}', '|' and newlines tokens. so considering the following:

alias test {
goto label
if (a == b) {
  :label
  inc -us %test
if (%test == 2) return
}
goto label
}
The first /goto label statement gets mIRC to parse the rest as token, probably just words, and :label is found despite the if statement being false, %test is increased to 1 and then to 2, proving that the label is found from the first time with the first goto label (forward jump) and then backward since the label was found the first time.

Conditional transfer of control[edit]

Many times you only want a script to go to a specific location according to a certain condition. You can use an if statement to achieve this. A simple syntax looks like this:

if (<condition>) {
  goto label
}

Goto Loops[edit]

Goto statements can also be used to create a loop by jumping back to a previous location in the script. The example below is a simple loop counting from 1 to 10. If for any reason you mistakenly caused your script to execute endlessly, you manually break the loop using the Ctrl+Break keys.

Alias Count {
  :loop
  var %c $calc(%c + 1)
  echo -a %c
  if (%c <= 9) goto loop
}

Random Name Example[edit]

Sometimes a goto can result in smaller code if we need a do-while style loop like other languages. A practical example of this is a random nickname generator which excludes us from the list. Such alias might look like this:

; precondition: $nick($chan, 0) > 1
alias rnd {
  :retry
  if ($nick($chan, $rand(1, $nick($chan, 0))) == $me) goto retry
  return $v1
}

Error Handling[edit]

Although it is rare to use this feature, the "error" label is a designated goto section for error checking. If there is an error in the script, mIRC try to find an :error label in the current routine. If no :error label is found in the current routine, mIRC will propagate the error backward to any calling routine and look for an :error label in those routines.


This gives you the ability to continue with the script at any point regardless of the error, if you want.


After catching an error with :error, you must check for an error by checking $error with a typical /if statement to make sure you reached the :error part because of an error and not because the script is just reaching that point.

If you want to keep going with the code, use /reseterror as soon as possible, this reset the error (and therefore $error's value), mIRC leaves the error state and you can safely execute anything you want.

If you don't use /reseterror, you can also use /return to tell mIRC to look for a different :error label in previous routines. If you don't use /reseterror or /return you must be careful: mIRC is in an error state, yet it must still process your code somehow to allow you to *at least* check for error and reset it, so the if statement feature probably always works, /echo itself also probably does always work but it's unclear what you can do and what you can't do. You would think that since mIRC is somehow forced to allow you to use the scripting engine to check for error and reset it, you could be able to do anything even without calling /reseterror first, this is not true (see examples), the exact list of features that are working in this situation are unknown, it's recommended not to do anything before using /reseterror in this situation.

/*  EXAMPLE 1
    This typical example shows that after reaching :error and without using /reseterror, /echo itself works, but see the next example, a lot of simple operation might be done there.
    Since we don't halt or reset the error, mIRC will display both our echo and its own error for $rand.
 */
Alias Example {
  echo -a $rand(1,)
  return
  :error
  echo -a Error: $error
}
/* EXAMPLE 2
   In this case, we try to execute /echo again after a /if error, but on $regsubex, for some reasons, that $regsubex gets mIRC to silentely halts while inside a routine.
   You should get the first echo 'ok' and an echo '> $str(ab,2000)' is expected, but you shouldn't see it.
   Use /reseterror first and it works as expected.
*/
alias testgoto {
  if
  :error
  echo -a ok | echo -a > $regsubex($str(ab,2000),/(a*)b*(a*)*b/,)
}
/* EXAMPLE 3
Although this example might look a little confusing, bear with me:
Calling alias /Foo will echo what alias FooBar returns.
Alias foobar will return what alias bar returns.
Because alias bar errors out (since $mid is missing a few parameters), mIRC will look for an :error label in alias Bar but won't find any.
So it will look in the previous routine, the alias FooBar. an :error label is found in the alias FooBar.
It is executed, echoing Error followed by the error message and returning 1000 to alias foo.
The /reseterror command effectively prevents mIRC from halting the script and allows it to finish executing.
*/
 
Alias Foo {
  echo -a $FooBar
}
Alias Bar { 
  return $mid($1)
}
Alias FooBar {
  return $Bar(Example)
  :error
  echo -a Error Using Value 1000 instead! ( $+ $error $+ )
  reseterror
  return 1000
}