How to resolve the algorithm Jump anywhere step by step in the M2000 Interpreter programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jump anywhere step by step in the M2000 Interpreter programming language

Table of Contents

Problem Statement

Imperative programs like to jump around, but some languages restrict these jumps. Many structured languages restrict their conditional structures and loops to local jumps within a function. Some assembly languages limit certain jumps or branches to a small range. This task is to demonstrate a local jump and a global jump and the various other types of jumps that the language supports. For the purpose of this task, the jumps need not be used for a single purpose and you have the freedom to use these jumps for different purposes. You may also defer to more specific tasks, like Exceptions or Generator. This task provides a "grab bag" for several types of jumps. There are non-local jumps across function calls, or long jumps to anywhere within a program. Anywhere means not only to the tops of functions! These jumps are not all alike. A simple goto never touches the call stack. A continuation saves the call stack, so you can continue a function call after it ends.

Use your language to demonstrate the various types of jumps that it supports. Because the possibilities vary by language, this task is not specific. You have the freedom to use these jumps for different purposes. You may also defer to more specific tasks, like Exceptions or Generator.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jump anywhere step by step in the M2000 Interpreter programming language

Source code in the m2000 programming language

Module Checkit {
      Module Alfa {
            10 Rem this code is like basic
            20 Let K%=1
            30 Let A=2
            40 Print "Begin"
            50 On K% Gosub 110
            60 If A=2 then 520
            70 For I=1 to 10
            80 if i>5 then exit for 120
            90 Gosub 110
            100 Next i 
            110 On A Goto 150,  500
            120 Print "This is the End ?"
            130 Return
            150 Print "from loop pass here", i
            160 Return
            200 Print "ok"
            210 Return
            500 Print "Routine 500"
            510 Goto 200
            520 Let A=1
            530 Gosub 70
            540 Print "Yes"
      }
      Alfa
      \\ this can be done. Code executed like it is from this module
      \\ because 200 is a label inside code of Module Checkit
      \\ and search is not so smart. After first search. position saved in a hash table
      Gosub 200  ' print "ok"
      Gosub 200  ' print "ok"
}
Checkit

Module LikeRunBasic { 
      for i = 1 to 10
      if i = 5 then goto label5
      next i
      end
       
      label5:
      print i
      while i < 10 {
            if i = 6 then  goto label6
            i = i + 1
      }
      end
       
      label6:
      print i
      if i = 6 then goto finish
      print "Why am I here"
       
      finish:
      print "done"
}
LikeRunBasic

Module LikeGo {
      \\ simulate Go for
      \\ need to make var Empty, swapping uninitialized array item
      Module EmptyVar (&x) {
            Dim A(1)
            Swap A(0), x
      }
      Function GoFor(&i,  first, comp$, step$) {
            \\ checking for empty we now if i get first value
            if type$(i)="Empty" then {
                  i=first
                  } else {
                  i+=Eval(step$)
            }
            if Eval("i"+comp$) Else Exit
            =true
          }
      def i, j
      EmptyVar &i
      outer:
       {
            if GoFor(&i, 0, "<4", "+1") else exit
            EmptyVar &j
            {
                 if GoFor(&j, 0, "<4", "+1") else exit
                 if i+j== 4 then goto outer
                 if i+j == 5 then goto break_outer
                 print i+j
                 loop
            }
            loop
      } 
      break_outer:
      k = 3
      if k == 3 Then Goto later
      Print k  \\ never executed
      later:
      k++
      Print k
}
LikeGo


\\ or we can use For {} block and put label outer to right place
Module LikeGo {
      For i=0 to 3 {
            For j=0 to 3  {
                  if i+j== 4 then goto outer
                  if i+j == 5 then goto break_outer
                  print i+j
            }
            outer:            
      } 
      break_outer:
      k = 3
      if k == 3 Then Goto later
      Print k  \\ never executed
      later:
      k++
      Print k
}
LikeGo


Module LikeGo_No_Labels {
      For i=0 to 3 {
            For j=0 to 3  {
                  if i+j== 4 then exit ' exit breaks only one block
                  if i+j == 5 then break ' break breaks all blocks, but not the Module's block.
                  print i+j
            }         
      } 
      k = 3
      if k == 3 Else {
            Print k  \\ never executed
      }
      k++
      Print k
}
LikeGo_No_Labels

  

You may also check:How to resolve the algorithm Integer comparison step by step in the Groovy programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Haskell programming language
You may also check:How to resolve the algorithm A+B step by step in the Lang5 programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the jq programming language
You may also check:How to resolve the algorithm Abelian sandpile model step by step in the Delphi programming language