This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,11 @@
[[Imperative programming|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 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!
* Some languages can ''go to'' any global label in a program.
* Some languages can break multiple function calls, also known as ''unwinding the call stack''.
* Some languages can save a ''continuation''. The program can later continue from the same place. So you can jump anywhere, but only if you have a previous visit there (to save the continuation).
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]].

View file

@ -0,0 +1,3 @@
---
category:
- Branches

View file

@ -0,0 +1,48 @@
procedure Goto_Test is
begin
Stuff;
goto The_Mother_Ship; -- You can do this if you really must!
Stuff;
if condition then
Stuff;
<<Jail>>
Stuff;
end if;
Stuff;
-- Ada does not permit any of the following
goto Jail;
goto The_Sewer;
goto The_Morgue;
Stuff;
case condition is
when Arm1 =>
Stuff;
goto The_Gutter; -- Cant do this either
Stuff;
when Arm2 =>
Stuff;
<<The_Gutter>>
Stuff;
<<The_Sewer>>
Stuff;
end case;
Stuff;
for I in Something'Range loop
Stuff;
<<The_Morgue>>
if You_Are_In_Trouble then
goto The_Mother_Ship;
-- This is the usual use of a goto.
end if;
Stuff;
end loop;
Stuff;
<<The_Mother_Ship>>
Stuff;
end Goto_Test;

View file

@ -0,0 +1,37 @@
; Define a function.
function()
{
MsgBox, Start
gosub jump
free:
MsgBox, Success
}
; Call the function.
function()
goto next
return
jump:
MsgBox, Suspended
return
next:
Loop, 3
{
gosub jump
}
return
/*
Output (in Message Box):
Start
Suspended
Success
Suspended
Suspended
Suspended
*/

View file

@ -0,0 +1,2 @@
10 GOTO 100: REM jump to a specific line
20 RUN 200: REM start the program running from a specific line

View file

@ -0,0 +1 @@
GOTO mylabel

View file

@ -0,0 +1,11 @@
if (x > 0) goto positive;
else goto negative;
positive:
printf("pos\n"); goto both;
negative:
printf("neg\n");
both:
...

View file

@ -0,0 +1 @@
goto (x > 0 ? positive : negative);

View file

@ -0,0 +1,5 @@
for (i = 0; ...) {
for (j = 0; ...) {
if (condition_met) goto finish;
}
}

View file

@ -0,0 +1,5 @@
goto danger;
for (i = 0; i < 10; i++) {
danger: /* unless you jumped here with i set to a proper value */
printf("%d\n", i);
}

View file

@ -0,0 +1,14 @@
F=: verb define
smoutput 'Now we are in F'
G''
smoutput 'Now we are back in F'
)
G=: verb define
smoutput 'Now we are in G'
throw.
)
F''
Now we are in F
Now we are in G

View file

@ -0,0 +1,18 @@
loop1: while (x != 0) {
loop2: for (int i = 0; i < 10; i++) {
loop3: do {
//some calculations...
if (/*some condition*/) {
//this continue will skip the rest of the while loop code and start it over at the next iteration
continue loop1;
}
//more calculations skipped by the continue if it is executed
if (/*another condition*/) {
//this break will end the for loop and jump to its closing brace
break loop2;
}
} while (y < 10);
//loop2 calculations skipped if the break is executed
}
//loop1 calculations executed after loop2 is done or if the break is executed, skipped if the continue is executed
}

View file

@ -0,0 +1 @@
goto mylabel;

View file

@ -0,0 +1,17 @@
;Go to a label within the program file
Goto Label
;Go to a line below a label
Goto Label+lines
;Go to a different file
Goto ^Routine
;Go to a label within a different file
Goto Label^Routine
;and with offset
Goto Label+2^Routine
;
;The next two goto commands will both return error M45 in ANSI MUMPS.
NoNo
For
. Goto Out
Out Quit
Goto NoNo+2

View file

@ -0,0 +1,35 @@
sub outer {
print "In outer, calling inner:\n";
inner();
OUTER:
print "at label OUTER\n";
}
sub inner {
print "In inner\n";
goto SKIP; # goto same block level
print "This should be skipped\n";
SKIP:
print "at label SKIP\n";
goto OUTER; # goto whatever OUTER label there is on frame stack.
# if there isn't any, exception will be raised
print "Inner should never reach here\n";
}
sub disguise {
goto &outer; # a different type of goto, it replaces the stack frame
# with the outer() function's and pretend we called
# that function to begin with
print "Can't reach this statement\n";
}
print "Calling outer:\n";
outer();
print "\nCalling disguise:\n";
disguise();
print "\nCalling inner:\n";
inner(); # will die

View file

@ -0,0 +1,4 @@
(de foo (N)
(prinl "This is 'foo'")
(printsp N)
(or (=0 (dec 'N)) (run (cddr foo))) )

View file

@ -0,0 +1,13 @@
# Example 2: Restarting a loop:
from goto import goto, label
label .start
for i in range(1, 4):
print i
if i == 2:
try:
output = message
except NameError:
print "Oops - forgot to define 'message'! Start again."
message = "Hello world"
goto .start
print output, "\n"

View file

@ -0,0 +1 @@
: foo 19 jump: 1+ 21 ;

View file

@ -0,0 +1,19 @@
/*REXX pgm demonstrates various jumps (GOTOs). In REXX, it's a SIGNAL. */
say 'starting...'
signal aJump
say 'this statement is never executed.'
aJump: say 'and here we are at aJump.'
do j=1 to 10
say 'j=' j
if j==7 then signal bJump
end /*j*/
bJump: say 'and here we are at bJump.'
signal cJump
say 'this statement is never executed.'
do k=1 to 10
say 'k=' k
cJump: say 'and here we are at cJump.'
exit
end /*k*/

View file

@ -0,0 +1,11 @@
i=13
signal label
say 'This is never executed'
sub: Procedure Expose i
Do i=1 To 10;
label:
Say 'label reached, i='i
Signal real_start
End
Return
real_start:

View file

@ -0,0 +1,17 @@
/* REXX ***************************************************************
* 12.12.2012 Walter Pachl
**********************************************************************/
Signal On Syntax
Parse Upper Arg part
If part<>'' Then
Interpret 'Signal' part
Say 'Executing default part'
Signal eoj
a:Say 'executing part A'
Signal eoj
b:Say 'executing part B'
Signal eoj
Syntax:
Say 'argument must be a or b or omitted'
Exit
eoj: say 'here we could print statistics'

View file

@ -0,0 +1,22 @@
require 'continuation' unless defined? Continuation
if a = callcc { |c| [c, 1] }
c, i = a
c[nil] if i > 100
case 0
when i % 3
print "Fizz"
case 0
when i % 5
print "Buzz"
end
when i % 5
print "Buzz"
else
print i
end
puts
c[c, i + 1]
end

View file

@ -0,0 +1,20 @@
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
wend
end
[label6]
print i
if i = 6 then goto [finish]
print "Why am I here"
[finish]
print "done"