2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,11 +1,23 @@
[[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!
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.
<br>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]].
;Task:
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]].
<br><br>

View file

@ -1,3 +1,4 @@
---
category:
- Branches
- Simple

View file

@ -0,0 +1,24 @@
char *str;
int *array;
FILE *fp;
str = (char *) malloc(100);
if(str == NULL) {
return;
}
fp=fopen("c:\\test.csv", "r");
if(fp== NULL) {
free(str );
return;
}
array = (int *) malloc(15);
if(array==NULL) if(fp== NULL) {
free(str );
fclose(fp);
return;
}
...// read in the csv file and convert to integers

View file

@ -0,0 +1,25 @@
char *str;
int *array;
FILE *fp;
str = (char *) malloc(100);
if(str == NULL)
goto: exit;
fp=fopen("c:\\test.csv", "r");
if(fp== NULL)
goto: clean_up_str;
array = (int *) malloc(15);
if(array==NULL)
goto: clean_up_file;
...// read in the csv file and convert to integers
clean_up_array:
free(array);
clean_up_file:
fclose(fp);
clean_up_str"
free(str );
exit:
return;

View file

@ -0,0 +1,24 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. JUMPS-PROGRAM.
* Nobody writes like this, of course; but...
PROCEDURE DIVISION.
* You can jump anywhere you like.
START-PARAGRAPH.
GO TO AN-ARBITRARY-PARAGRAPH.
YET-ANOTHER-PARAGRAPH.
ALTER START-PARAGRAPH TO PROCEED TO A-PARAGRAPH-SOMEWHERE.
* That's right, folks: we don't just have GO TOs, we have GO TOs whose
* destinations can be changed at will, from anywhere in the program,
* at run time.
GO TO START-PARAGRAPH.
* But bear in mind: once you get there, the GO TO no longer goes to
* where it says it goes to.
A-PARAGRAPH-SOMEWHERE.
DISPLAY 'Never heard of him.'
STOP RUN.
SOME-OTHER-PARAGRAPH.
* You think that's bad? You ain't seen nothing.
GO TO YET-ANOTHER-PARAGRAPH.
AN-ARBITRARY-PARAGRAPH.
DISPLAY 'Edsger who now?'
GO TO SOME-OTHER-PARAGRAPH.

View file

@ -1,19 +1,29 @@
0 value goto1 0 value goto2 0 value goto3 0 value goto4 0 value goto5
0 value goto6 0 value goto7 0 value goto8 0 value goto9 0 value goto10
\ this prints five lines containing elements from the two
\ words 'proc1' and 'proc2'. gotos are used here to jump
\ into and out of the two words at various points, as well
\ as to create a loop. this functions with ficl, pfe,
\ gforth, bigforth, swiftforth, iforth, and vfxforth; it
\ may work with other forths as well.
create goto1 1 cells allot create goto2 1 cells allot
create goto3 1 cells allot create goto4 1 cells allot
create goto5 1 cells allot create goto6 1 cells allot
create goto7 1 cells allot create goto8 1 cells allot
create goto9 1 cells allot create goto10 1 cells allot
: proc1
[ here to goto1 ] s" line1 " type goto7 >r exit
[ here to goto2 ] s" line2 " type goto8 >r exit
[ here to goto3 ] s" line3 " type goto9 >r exit
[ here to goto4 ] s" line4 " type goto10 >r exit
[ here to goto5 ] s" line5" type cr ;
[ here goto1 ! ] s" item1 " type goto7 @ >r exit
[ here goto2 ! ] s" item2 " type goto8 @ >r exit
[ here goto3 ! ] s" item3 " type goto9 @ >r exit
[ here goto4 ! ] s" item4 " type goto10 @ >r exit
[ here goto5 ! ] s" item5" type cr 2dup = if 2drop exit then 1+ goto6 @ >r ;
: proc2
[ here to goto6 ] s" line6 " type goto1 >r exit
[ here to goto7 ] s" line7 " type goto2 >r exit
[ here to goto8 ] s" line8 " type goto3 >r exit
[ here to goto9 ] s" line9 " type goto4 >r exit
[ here to goto10 ] s" line10 " type goto5 >r ;
[ here goto6 ! ] s" line " type dup . s" --> item6 " type goto1 @ >r exit
[ here goto7 ! ] s" item7 " type goto2 @ >r exit
[ here goto8 ! ] s" item8 " type goto3 @ >r exit
[ here goto9 ! ] s" item9 " type goto4 @ >r exit
[ here goto10 ! ] s" item10 " type goto5 @ >r ;
proc2
5 1 proc2
bye

View file

@ -1 +1,5 @@
line6 line1 line7 line2 line8 line3 line9 line4 line10 line5
line 1 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 2 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 3 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 4 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 5 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5

View file

@ -1,35 +1,30 @@
create gotos 11 cells allot \ data structure consisting of 10 cells for
\ storing addrs of goto markers/labels.
\ 11 cells are allocated, with the 1st cell
\ unused, for the offset would be 0. to begin
\ counting from 1, the offset of the 2nd cell,
\ is easier to remember. this is merely a preference.
\ if additional goto markers are needed, merely
\ increase the quantity of cells in this data structure.
\ this is congruent to the previous demonstration, employing
\ a data structure to store goto/jump addresses instead of
\ separate variables, and two additional words 'mark_goto' and
\ 'goto'. works with ficl, pfe, gforth, bigforth, and vfxforth.
\ swiftforth and iforth crash.
: mark_goto here swap cells gotos + ! ; immediate \ position (offset) of cell within
\ data structure must be on stack.
: goto r> drop cells gotos + @ >r exit ; \ "exit" is needed for iforth only.
\ position (offset) of cell within
\ data structure must be on stack.
create gotos 10 cells allot \ data structure for storing goto/jump addresses
: mark_goto here swap 1- cells gotos + ! ; immediate \ save addresses for jumping
: goto r> drop 1- cells gotos + @ >r ;
\ designations for commands are immaterial when using goto's,
\ since the commands are not referenced by name, and are instead
\ jumped into by means of the goto marker.
: command1
[ 1 ] mark_goto s" line1 " type 7 goto
[ 2 ] mark_goto s" line2 " type 8 goto
[ 3 ] mark_goto s" line3 " type 9 goto
[ 4 ] mark_goto s" line4 " type 10 goto
[ 5 ] mark_goto s" line5" type cr bye ;
[ 1 ] mark_goto s" item1 " type 7 goto
[ 2 ] mark_goto s" item2 " type 8 goto
[ 3 ] mark_goto s" item3 " type 9 goto
[ 4 ] mark_goto s" item4 " type 10 goto
[ 5 ] mark_goto s" item5 " type cr 2dup = if 2drop exit then 1+ 6 goto ;
: command2
[ 6 ] mark_goto s" line6 " type 1 goto
[ 7 ] mark_goto s" line7 " type 2 goto
[ 8 ] mark_goto s" line8 " type 3 goto
[ 9 ] mark_goto s" line9 " type 4 goto
[ 10 ] mark_goto s" line10 " type 5 goto ;
[ 6 ] mark_goto s" line " type dup . s" --> item6 " type 1 goto
[ 7 ] mark_goto s" item7 " type 2 goto
[ 8 ] mark_goto s" item8 " type 3 goto
[ 9 ] mark_goto s" item9 " type 4 goto
[ 10 ] mark_goto s" item10 " type 5 goto ;
: go 6 goto ; go
: go 5 1 6 goto ; go
bye

View file

@ -1 +1,5 @@
line6 line1 line7 line2 line8 line3 line9 line4 line10 line5
line 1 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 2 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 3 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 4 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5
line 5 --> item6 item1 item7 item2 item8 item3 item9 item4 item10 item5

View file

@ -0,0 +1,38 @@
\ works with ficl, pfe, gforth, bigforth, and vfx.
\ swiftforth may crash, iforth does not function.
: create_goto create 4 allot does> r> drop @ >r ;
: mark_goto here ' >body ! ; immediate
create_goto goto1
create_goto goto2
create_goto goto3
create_goto goto4
create_goto goto5
create_goto goto6
create_goto goto7
create_goto goto8
create_goto goto9
create_goto stop_here
:noname
mark_goto goto1 s" iteration " type dup . s" --> " type
s" goto1 " type goto3
mark_goto goto2 s" goto2 " type goto4
mark_goto goto3 s" goto3 " type goto5
mark_goto goto4 s" goto4 " type goto6
mark_goto goto5 s" goto5 " type goto7
mark_goto goto6 s" goto6 " type goto8
mark_goto goto7 s" goto7 " type goto9
mark_goto goto8 s" goto8 " type stop_here
mark_goto goto9 s" goto9 " type goto2 ; drop
:noname mark_goto stop_here
cr 2dup = if 2drop exit then 1+ goto1
\ cr 2dup = if 2drop bye then 1+ goto1 \ for swiftforth
; drop
: go goto1 ;
5 1 go
bye

View file

@ -0,0 +1,5 @@
iteration 1 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 2 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 3 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 4 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8
iteration 5 --> goto1 goto3 goto5 goto7 goto9 goto2 goto4 goto6 goto8

View file

@ -0,0 +1 @@
: GOTO ['] EXECUTE ;

View file

@ -0,0 +1,5 @@
: WORLD ." World!" ;
: HELLO ." Hello" ;
GOTO CR GOTO HELLO GOTO SPACE GOTO WORLD
Hello World!

View file

@ -0,0 +1,13 @@
import Control.Monad.Cont
data LabelT r m = LabelT (ContT r m ())
label :: ContT r m (LabelT r m)
label = callCC subprog
where subprog lbl = let l = LabelT (lbl l) in return l
goto :: LabelT r m -> ContT r m b
goto (LabelT l) = const undefined <$> l
runProgram :: Monad m => ContT r m r -> m r
runProgram program = runContT program return

View file

@ -0,0 +1,9 @@
main = runProgram $
do
start <- label
lift $ putStrLn "Enter your name, please"
name <- lift $ getLine
if name == ""
then do lift $ putStrLn "Name can't be empty!"
goto start
else lift $ putStrLn ("Hello, " ++ name)

View file

@ -0,0 +1,6 @@
import Data.IORef
readInt = lift $ readLn >>= newIORef
get ref = lift $ readIORef ref
set ref expr = lift $ modifyIORef ref (const expr)
output expr = lift $ putStrLn expr

View file

@ -0,0 +1,19 @@
gcdProg = runProgram $ callCC $ \exit -> -- <--------+
do -- |
start <- label -- <-----+ |
output "Enter two integers, or zero to exit" -- | |
nr <- readInt -- | |
n <- get nr -- | |
when (n == 0) $ -- | |
do output "Exiting" -- | |
exit () -- ---------+
mr <- readInt -- |
loop <- label -- <--+ |
n <- get nr -- | |
m <- get mr -- | |
when (n == m) $ -- | |
do output ("GCD: " ++ show n) -- | |
goto start -- ------+
when (n > m) $ set nr (n - m) -- |
when (m > n) $ set mr (m - n) -- |
goto loop -- ---+

View file

@ -0,0 +1,17 @@
gcdFProg = start
where
start = do
putStrLn "Enter two integers, or zero to exit"
n <- readLn
if n == 0
then
putStrLn "Exiting"
else do
m <- readLn
putStrLn $ "GCD: " ++ show (loop n m)
start
loop n m
| n == m = n
| n < m = loop n (m-n)
| n > m = loop (n-m) m

View file

@ -0,0 +1,12 @@
import Control.Applicative
import Control.Monad.Trans.Maybe
gcdFProg2 = forever mainLoop <|> putStrLn "Exiting"
where
mainLoop = putStrLn "Enter two integers, or zero to exit" >>
runMaybeT process >>=
maybe empty (\r -> putStrLn ("GCD: " ++ show r))
process = gcd <$> (lift readLn >>= exitOn 0) <*> lift readLn
exitOn n x = if x == n then empty else pure x

View file

@ -0,0 +1,13 @@
outer-loop: loop {
inner-loop: loop {
# NYI # goto inner-loop if rand > 0.5; # Hard goto
next inner-loop if rand > 0.5; # Next loop iteration
redo inner-loop if rand > 0.5; # Re-execute block
last outer-loop if rand > 0.5; # Exit the loop
ENTER { say "Entered inner loop block" }
LEAVE { say "Leaving inner loop block" }
}
ENTER { say "Entered outer loop block" }
LEAVE { say "Leaving outer loop block" }
LAST { say "Ending outer loop" }
}

View file

@ -0,0 +1,8 @@
my @list = lazy gather for ^100 -> $i {
if $i.is-prime {
say "Taking prime $i";
take $i;
}
}
say @list[5];

View file

@ -0,0 +1 @@
die "This is a generic, untyped exception";

View file

@ -0,0 +1,6 @@
sub foo() { fail "oops" }
my $failure = foo;
say "Called foo";
say "foo not true" unless $failure;
say "foo not defined" unless $failure.defined;
say "incremented foo" if $failure++; # exception

View file

@ -0,0 +1,16 @@
sub foo($i) {
if $i == 0 {
die "Are you sure you want /0?";
}
say "Dividing by $i";
1/$i.Num + 0; # Fighting hard to make this fail
}
for ^10 -> $n {
my $recip = foo($n);
say "1/{$n} = {$recip.perl}";
}
CATCH {
when ~$_ ~~ m:s/Are you sure/ { .resume; #`(yes, I'm sure) }
}

View file

@ -0,0 +1 @@
:myLabel while (<condition>) { <statement list>}

View file

@ -0,0 +1,9 @@
:myLabel while (<condition 1>)
{
for ($item in $items)
{
if (<condition 2>) { break myLabel }
$item = $x # A statement inside the For-loop
}
}
$a = $c # A statement after the labeled While-loop

View file

@ -0,0 +1,15 @@
:red while (<condition1>)
{
:yellow while (<condition2>)
{
while (<condition3>)
{
if ($a) {break}
if ($b) {break red}
if ($c) {break yellow}
}
# After innermost loop
}
# After "yellow" loop
}
# After "red" loop