A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
11
Task/Jump-anywhere/0DESCRIPTION
Normal file
11
Task/Jump-anywhere/0DESCRIPTION
Normal 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]].
|
||||
3
Task/Jump-anywhere/1META.yaml
Normal file
3
Task/Jump-anywhere/1META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
category:
|
||||
- Branches
|
||||
48
Task/Jump-anywhere/Ada/jump-anywhere.ada
Normal file
48
Task/Jump-anywhere/Ada/jump-anywhere.ada
Normal 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;
|
||||
37
Task/Jump-anywhere/AutoHotkey/jump-anywhere.ahk
Normal file
37
Task/Jump-anywhere/AutoHotkey/jump-anywhere.ahk
Normal 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
|
||||
|
||||
*/
|
||||
2
Task/Jump-anywhere/BASIC/jump-anywhere-1.bas
Normal file
2
Task/Jump-anywhere/BASIC/jump-anywhere-1.bas
Normal 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
|
||||
1
Task/Jump-anywhere/BASIC/jump-anywhere-2.bas
Normal file
1
Task/Jump-anywhere/BASIC/jump-anywhere-2.bas
Normal file
|
|
@ -0,0 +1 @@
|
|||
GOTO mylabel
|
||||
11
Task/Jump-anywhere/C/jump-anywhere-1.c
Normal file
11
Task/Jump-anywhere/C/jump-anywhere-1.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
if (x > 0) goto positive;
|
||||
else goto negative;
|
||||
|
||||
positive:
|
||||
printf("pos\n"); goto both;
|
||||
|
||||
negative:
|
||||
printf("neg\n");
|
||||
|
||||
both:
|
||||
...
|
||||
1
Task/Jump-anywhere/C/jump-anywhere-2.c
Normal file
1
Task/Jump-anywhere/C/jump-anywhere-2.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
goto (x > 0 ? positive : negative);
|
||||
5
Task/Jump-anywhere/C/jump-anywhere-3.c
Normal file
5
Task/Jump-anywhere/C/jump-anywhere-3.c
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
for (i = 0; ...) {
|
||||
for (j = 0; ...) {
|
||||
if (condition_met) goto finish;
|
||||
}
|
||||
}
|
||||
5
Task/Jump-anywhere/C/jump-anywhere-4.c
Normal file
5
Task/Jump-anywhere/C/jump-anywhere-4.c
Normal 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);
|
||||
}
|
||||
14
Task/Jump-anywhere/J/jump-anywhere.j
Normal file
14
Task/Jump-anywhere/J/jump-anywhere.j
Normal 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
|
||||
18
Task/Jump-anywhere/Java/jump-anywhere.java
Normal file
18
Task/Jump-anywhere/Java/jump-anywhere.java
Normal 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
|
||||
}
|
||||
1
Task/Jump-anywhere/MBS/jump-anywhere.mbs
Normal file
1
Task/Jump-anywhere/MBS/jump-anywhere.mbs
Normal file
|
|
@ -0,0 +1 @@
|
|||
goto mylabel;
|
||||
17
Task/Jump-anywhere/MUMPS/jump-anywhere.mumps
Normal file
17
Task/Jump-anywhere/MUMPS/jump-anywhere.mumps
Normal 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
|
||||
35
Task/Jump-anywhere/Perl/jump-anywhere.pl
Normal file
35
Task/Jump-anywhere/Perl/jump-anywhere.pl
Normal 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
|
||||
4
Task/Jump-anywhere/PicoLisp/jump-anywhere.l
Normal file
4
Task/Jump-anywhere/PicoLisp/jump-anywhere.l
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(de foo (N)
|
||||
(prinl "This is 'foo'")
|
||||
(printsp N)
|
||||
(or (=0 (dec 'N)) (run (cddr foo))) )
|
||||
13
Task/Jump-anywhere/Python/jump-anywhere-1.py
Normal file
13
Task/Jump-anywhere/Python/jump-anywhere-1.py
Normal 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"
|
||||
1
Task/Jump-anywhere/Python/jump-anywhere-2.py
Normal file
1
Task/Jump-anywhere/Python/jump-anywhere-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
: foo 19 jump: 1+ 21 ;
|
||||
19
Task/Jump-anywhere/REXX/jump-anywhere-1.rexx
Normal file
19
Task/Jump-anywhere/REXX/jump-anywhere-1.rexx
Normal 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*/
|
||||
11
Task/Jump-anywhere/REXX/jump-anywhere-2.rexx
Normal file
11
Task/Jump-anywhere/REXX/jump-anywhere-2.rexx
Normal 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:
|
||||
17
Task/Jump-anywhere/REXX/jump-anywhere-3.rexx
Normal file
17
Task/Jump-anywhere/REXX/jump-anywhere-3.rexx
Normal 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'
|
||||
22
Task/Jump-anywhere/REXX/jump-anywhere-4.rexx
Normal file
22
Task/Jump-anywhere/REXX/jump-anywhere-4.rexx
Normal 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
|
||||
20
Task/Jump-anywhere/REXX/jump-anywhere-5.rexx
Normal file
20
Task/Jump-anywhere/REXX/jump-anywhere-5.rexx
Normal 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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue