Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
category:
- Branches
- Simple
from: http://rosettacode.org/wiki/Jump_anywhere

View file

@ -0,0 +1,24 @@
[[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 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!
* 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).
<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.
;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

@ -0,0 +1,5 @@
...
B ANYWHERE branch
...
ANYWHERE EQU * label
...

View file

@ -0,0 +1 @@
JMP PrintChar ; jump to the label "PrintChar" where a routine to print a letter to the screen is located.

View file

@ -0,0 +1,5 @@
lda #<PrintChar ;load into A the low byte of the address that PrintChar references.
sta $00
lda #>PrintChar ;load into A the high byte of the address that PrintChar references.
sta $01 ;these need to be stored low then high because the 6502 is a little-endian cpu
JMP ($00) ;dereferences to JMP PrintChar

View file

@ -0,0 +1,8 @@
JumpTable_Lo: db <PrintChar, <WaitChar, <ReadKeys, <MoveMouse ;each is the low byte of a memory address
JumpTable_Hi: db >PrintChar, >WaitChar, >ReadKeys, >MoveMouse ;each is the high byte of a memory address
lda JumpTable_Lo,x
sta $10
lda JumpTable_Hi,x
sta $11
JMP ($0010)

View file

@ -0,0 +1,5 @@
.org $8000
JMP PrintChar
JMP WaitChar
JMP ReadKeys
JMP MoveMouse

View file

@ -0,0 +1,7 @@
lda $80
sta $21
txa
clc
adc #$03
sta $20
JMP ($0020)

View file

@ -0,0 +1,7 @@
JumpTable:
word PrintChar
word PrintString
word MoveMouse
ldx #$02
JMP (JumpTable,x) ;dereferences to JMP PrintString

View file

@ -0,0 +1 @@
ReturnTable: dw PrintChar-1,WaitChar-1,ReadKeys-1,MoveMouse-1

View file

@ -0,0 +1,15 @@
;execution is currently here
JSR UseReturnTable ;the address just after this label is pushed onto the stack.
;the chosen subroutine's RTS will bring you here.
UseReturnTable: ;pretend this is somewhere far away from where execution is, its distance doesn't matter.
ASL ;double the value of the variable, because this is a table of words.
TAX
LDA ReturnTable+1,x ;load the chosen subroutine's high byte into the accumulator
PHA ;push it onto the stack
LDA ReturnTable,x ;load the chosen subroutine's low byte into the accumulator
PHA ;push it onto the stack.
RTS ;this "RTS" actually takes you to the desired subroutine.
;The top two bytes of the stack are popped, the low byte is incremented by 1,
;and this value becomes the new program counter.

View file

@ -0,0 +1,8 @@
bra foo
nop ;execution will never reach here
foo:
CMP.L D0,D1
BGE D1_Is_Greater_Than_Or_Equal_To_D0
; your code for what happens when D1 is less than D0 goes here
D1_Is_Greater_Than_Or_Equal_To_D0:

View file

@ -0,0 +1,6 @@
JSR foo:
; this is somewhere far away from the JSR above
foo:
MOVE.L D0,(SP)+
RTS ;the CPU will "return" to the value that was in D0, not the actual return address.

View file

@ -0,0 +1,17 @@
;For this example, we want to execute "ReadJoystick"
MOVE.W D0,#1
JSR Retpoline
;;;;; ReadJoystick's RTS will return the program counter to here
; rest of program
;assume this is somewhere far away from the JSR above, and that execution won't fall through to here.
Retpoline: ;uses D0 as the index into the table.
LEA A0,SubroutineTable
LSL.B #2,D0 ;multiply D0 by 4 since this is a table of 32-bit memory addresses
MOVE.L (A0,D0),A0 ;offset and deference A0, now A0 contains the address of ReadJoystick
PEA A0 ;push the address of A0 onto the stack
RTS ;now we'll "return" to ReadJoystick. If it ends in an RTS, that RTS shall return to the area specified above.
SubroutineTable:
DC.L MoveMouse, DC.L ReadJoystick, DC.L ReadKeyboard, DC.L PrintString

View file

@ -0,0 +1,4 @@
JCXZ bar
foo:
LOOP foo ;subtract 1 from CX, and if CX is still nonzero jump back to foo
bar:

View file

@ -0,0 +1,153 @@
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program julpanaywhere64.s */
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "loop indice : "
szMessage1: .asciz "Display to routine call by register\n"
szMessage2: .asciz "Equal to zero.\n"
szMessage3: .asciz "Not equal to zero.\n"
szMessage4: .asciz "loop start\n"
szMessage5: .asciz "No executed.\n"
szMessResult1: .asciz ","
szMessResult2: .asciz "]\n"
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess // branch and link to routine
// return here after routine execution
b label1 // branch unconditional to label
ldr x0,qAdrszMessage5 // this instruction is never executed
bl affichageMess // and this
label1:
ldr x0,qAdrszMessage4
bl affichageMess
mov x20,0
1:
mov x0,x20
ldr x1,qAdrsZoneConv
bl conversion10 // decimal conversion
strb wzr,[x1,x0]
mov x0,#3 // number string to display
ldr x1,qAdrszMessResult
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszCarriageReturn
bl displayStrings // display message
add x20,x20,1 // increment indice
cmp x20,5
blt 1b // branch for loop if lower
mov x0,0
cbz x0,2f // jump to label 2 if x0 = 0
2:
adr x1,affichageMess // load routine address in register
ldr x0,qAdrszMessage1
blr x1
mov x4,4
cmp x4,10
bgt 3f // branch if higter
mov x0,x4
3:
mov x0,0b100 // 1 -> bit 2
tbz x0,2,labzero // if bit 2 equal 0 jump to label
ldr x0,qAdrszMessage3 // bit 2 <> 0
bl affichageMess
b endtest // jump end if else
labzero: // display if bit equal to 0
ldr x0,qAdrszMessage2
bl affichageMess
endtest:
mov x0,0b000 // 0 -> bit 2
tbnz x0,2,4f // if bit 2 <> 0 jump to label
ldr x0,qAdrszMessage2 // bit 2 = 0
bl affichageMess
b 5f // jump end test
4: // display if bit equal to 1
ldr x0,qAdrszMessage3
bl affichageMess
5:
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszMessage1: .quad szMessage1
qAdrszMessage2: .quad szMessage2
qAdrszMessage3: .quad szMessage3
qAdrszMessage4: .quad szMessage4
qAdrszMessage5: .quad szMessage5
qAdrszMessStart: .quad szMessStart
/***************************************************/
/* display multi strings */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: // INFO: displayStrings
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
add fp,sp,#48 // save paraméters address (6 registers saved * 8 bytes)
mov x4,x0 // save strings number
cmp x4,#0 // 0 string -> end
ble 100f // branch to equal or smaller
mov x0,x1 // string 1
bl affichageMess
cmp x4,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x4,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x4,#3
ble 100f
mov x3,#3
sub x2,x4,#4
1: // loop extract address string on stack
ldr x0,[fp,x2,lsl #3]
bl affichageMess
subs x2,x2,#1
bge 1b
100:
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret // return to addtress stored in lr
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"

View file

@ -0,0 +1,2 @@
MOV PC,R0 ;loads the program counter with the value in R0. (Any register can be used for this)
LDR PC,[R0] ;loads the program counter with the 32-bit value at the memory location specified by R0

View file

@ -0,0 +1,2 @@
PUSH {R0-R12,LR}
POP {R0-R12,PC}

View file

@ -0,0 +1,2 @@
STMFD sp!,{r0-r12,lr}
LDMFD sp!,{r0-r12,pc}

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,22 @@
0 REM GOTO
100 GOTO 110 : REM JUMP TO A SPECIFIC LINE
110 RUN 120 : REM START THE PROGRAM RUNNING FROM A SPECIFIC LINE
120 IF 1 GOTO 130 : REM CONDITIONAL JUMP
130 IF 1 THEN 140 : REM THEN ALSO WORKS IN PLACE OF GOTO
140 IF 1 THEN GOTO 150 : REM BE VERBOSE BY USING THEN GOTO
150 ON A GOTO 170, 180, 190 : REM JUMP A SPECIFIC LINE NUMBER IN THE LIST INDEXED BY THE VALUE OF A STARTING AT 1, IF A IS OUT OF RANGE DO NOT JUMP
160 ON ERR GOTO 270 : REM WHEN AN ERROR OCCURS JUMP TO A SPECIFIC LINE
170 GOSUB 180 : REM JUMP TO LINE 180, PUSHING THE CURRENT PLACE ON THE STACK
180 POP : REM POP THE CURRENT PLACE FROM THE STACK, EFFECTIVELY MAKING THE PREVIOUS LINE A JUMP
190 CALL -151 : REM CALL ANY MACHINE LANGUAGE SUBROUTINE, IT MIGHT RETURN, IT MIGHT NOT
200 & : REM CALL THE USER-DEFINED AMPERSAND ROUTINE, IT MIGHT RETURN, IT MIGHT NOT
210 ? USR(0) : REM CALL THE USER-DEFINED FUNCTION, IT MIGHT RETURN, IT MIGHT NOT
220 S = 6 : ?CHR$(4)"PR#"S : REM CALL THE ROM ROUTINE IN SLOT S
230 S = 6 : ?CHR$(4)"IN#"S : REM CALL THE ROM ROUTINE IN SLOT S
240 ?CHR$(4)"RUN PROGRAM" : REM RUN A BASIC PROGRAM FROM DISK
250 ?CHR$(4)"BRUN BINARY PROGRAM": REM RUN A MACHINE LANGUAGE BINARY PROGRAM FROM DISK
260 ?CHR$(4)"EXEC PROGRAM.EX" : REM EXECUTE THE TEXT THAT IS CONTAINED IN THE FILE PROGRAM.EX
270 RESUME : REM JUMP BACK TO THE STATEMENT THAT CAUSED THE ERROR
280 STOP : REM BREAK THE PROGRAM
290 END : REM END THE PROGRAM
300 GOTO : REM NO LINE NUMBER, JUMPS TO LINE 0

View file

@ -0,0 +1 @@
CONT : REM CONTINUE, JUMP BACK TO WHERE THE PROGRAM STOPPED

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,22 @@
print "First line."
gosub sub1
print "Fifth line."
goto Ending
sub1:
print "Second line."
gosub sub2
print "Fourth line."
return
Ending:
print "We're just about done..."
goto Finished
sub2:
print "Third line."
return
Finished:
print "... with goto and gosub, thankfully."
end

View file

@ -0,0 +1,2 @@
{ 0 ? "Will not execute"; "Will execute" }
"Will execute"

View file

@ -0,0 +1,6 @@
F{𝕩•Out"In Second"}
{
•Out "First"
F@
•Out "Last"
}

View file

@ -0,0 +1,3 @@
First
In Second
Last

View file

@ -0,0 +1,46 @@
#include <iostream>
#include <utility>
using namespace std;
int main(void)
{
cout << "Find a solution to i = 2 * j - 7\n";
pair<int, int> answer;
for(int i = 0; true; i++)
{
for(int j = 0; j < i; j++)
{
if( i == 2 * j - 7)
{
// use brute force and run until a solution is found
answer = make_pair(i, j);
goto loopexit;
}
}
}
loopexit:
cout << answer.first << " = 2 * " << answer.second << " - 7\n\n";
// jumping out of nested loops is the main usage of goto in
// C++. goto can be used in other places but there is usually
// a better construct. goto is not allowed to jump across
// initialized variables which limits where it can be used.
// this is case where C++ is more restrictive than C.
goto spagetti;
int k;
k = 9; // this is assignment, can be jumped over
/* The line below won't compile because a goto is not allowed
* to jump over an initialized value.
int j = 9;
*/
spagetti:
cout << "k = " << k << "\n"; // k was never initialized, accessing it is undefined behavior
}

View file

@ -0,0 +1,11 @@
if (x > 0) goto positive;
else goto negative;
positive:
Console.WriteLine("pos\n"); goto both;
negative:
Console.WriteLine("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:
Console.WriteLine(i);
}

View file

@ -0,0 +1,12 @@
int i = 0;
tryAgain:
try {
i++;
if (i < 10) goto tryAgain;
}
catch {
goto tryAgain;
}
finally {
//goto end; //Error
}

View file

@ -0,0 +1,15 @@
public int M(int n) {
int result = 0;
switch (n) {
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
case 3:
cost += 50;
goto case 1;
}
return result;
}

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,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

@ -0,0 +1,2 @@
01 province pic 99 value 2.
GO TO quebec, ontario, manitoba DEPENDING ON province

View file

@ -0,0 +1,19 @@
100 CLS
110 PRINT "First line."
120 GOSUB sub1
130 PRINT "Fifth line."
140 GOTO Ending
150 sub1:
160 PRINT "Second line."
170 GOSUB sub2
180 PRINT "Fourth line."
190 RETURN
200 Ending:
210 PRINT "We're just about done..."
220 GOTO Finished
230 sub2:
240 PRINT "Third line."
250 RETURN
260 Finished:
270 PRINT "... with goto and gosub, thankfully."
280 END

View file

@ -0,0 +1,13 @@
(tagbody
beginning
(format t "I am in the beginning~%")
(sleep 1)
(go end)
middle
(format t "I am in the middle~%")
(sleep 1)
(go beginning)
end
(format t "I am in the end~%")
(sleep 1)
(go middle))

View file

@ -0,0 +1,5 @@
LDA goto
SUB somewhere
ADD somewhereElse
STA goto
goto: JMP somewhere

View file

@ -0,0 +1,71 @@
$ return ! ignored since we haven't done a gosub yet
$
$ if p1 .eqs. "" then $ goto main
$ inner:
$ exit
$
$ main:
$ goto label ! if label hasn't been read yet then DCL will read forward to find label
$ label:
$ write sys$output "after first occurrence of label"
$
$ on control_y then $ goto continue1 ! we will use this to get out of the loop that's coming up
$
$ label: ! duplicate labels *are* allowed, the most recently read is the one that will be the target
$ write sys$output "after second occurrence of label"
$ wait 0::2 ! since we are in a loop this will slow things down
$ goto label ! hit ctrl-y to break out
$
$ continue1: ! the previous "on control_y" remains in force despite having been triggered
$
$ label = "jump"
$ goto 'label ! target can be a variable; talk about handy
$ jump:
$ write sys$output "after first occurrence of jump"
$
$ first_time = "true"
$ continue_label = "continue2"
$ 'continue_label: ! even the label can be a variable (but only backwards); talk about handy
$ if first_time then $ goto skip
$ break = "true"
$ return
$
$ skip:
$ first_time = "false"
$
$ on control_y then $ gosub 'continue_label ! setup a new on control_y to get out the next loop coming up
$
$ break = "false"
$ 'label:
$ write sys$output "after second occurrence of jump"
$ wait 0::2
$ if .not. break then $ goto 'label
$
$ gosub sub1 ! no new scope or parameters
$ label = "sub1"
$ gosub 'label
$
$ call sub4 a1 b2 c3 ! new scope and parameters
$
$ @nl: ! new scope and parameters in another file but same process
$
$ procedure_filename = f$environment( "procedure " ) ! what is our own filename?
$ @'procedure_filename inner
$
$ exit ! exiting outermost scope exits the command procedure altogether, i.e. back to shell
$
$ sub1:
$ return
$
$ sub2:
$ goto break ! structurally disorganized but allowed
$
$ sub3:
$ return
$
$ break:
$ return
$
$ sub4: subroutine
$ exit
$ endsubroutine

View file

@ -0,0 +1,21 @@
var
x: Integer = 5;
label
positive, negative, both;
begin
if (x > 0) then
goto positive
else
goto negative;
positive:
writeln('pos');
goto both;
negative:
writeln('neg');
both:
readln;
end.

View file

@ -0,0 +1,18 @@
label
inloop, outloop;
begin
x := 2;
if x > 0 then
goto inloop;
for x := -10 to 10 do
begin
inloop:
Writeln(x);
if x = 8 then
goto outloop;
end;
outloop:
readln;
end.

View file

@ -0,0 +1,17 @@
label
tryAgain, finished;
begin
x := 0;
try
Writeln(x);
inc(x);
if (x < 10) then
goto tryAgain;
finally
goto finished
end;
finished:
readln;
end.

View file

@ -0,0 +1,47 @@
^|EMal has no goto statement.
|The closes statements are break, continue, return, exit
|and exceptions management.
|^
fun sample = void by block
for int i = 1; i < 10; ++i
if i == 1 do continue end # jumps to next iteration when 'i' equals 1
writeLine("i = " + i)
if i > 4 do break end # exits the loop when 'i' exceeds 4
end
for int j = 1; j < 10; ++j
writeLine("j = " + j)
if j == 3 do return end # returns from the function when 'j' exceeds 3
end
end
sample()
type StateMachine
^|this code shows how to selectevely jump to specific code
|to simulate a state machine as decribed here:
|https://wiki.tcl-lang.org/page/A+tiny+state+machine
|Functions return the next state.
|^
int n = -1
Map stateMachine = int%fun[
0 => int by block
if Runtime.args.length == 1
n = when(n == -1, int!Runtime.args[0], 0)
else
n = ask(int, "hello - how often? ")
end
return when(n == 0, 2, 1)
end,
1 => int by block
if n == 0 do return 0 end
writeLine(n + " Hello")
n--
return 1
end,
2 => int by block
writeLine("Thank you, bye")
return -1
end]
int next = 0
for ever
next = stateMachine[next]()
if next == -1 do break end
end

View file

@ -0,0 +1,19 @@
PROGRAM GOTO_ERR
LABEL 99,100
PROCEDURE P1
INPUT(I)
IF I=0 THEN GOTO 99 END IF
END PROCEDURE
PROCEDURE P2
99: PRINT("I'm in procdedure P2")
END PROCEDURE
BEGIN
100:
INPUT(J)
IF J=1 THEN GOTO 99 END IF
IF J<>0 THEN GOTO 100 END IF
END PROGRAM

View file

@ -0,0 +1,3 @@
USING: continuations prettyprint ;
current-continuation short.

View file

@ -0,0 +1,3 @@
USING: continuations math prettyprint ;
[ 10 2 / . continue 1 0 / . ] callcc0

View file

@ -0,0 +1,3 @@
USING: continuations kernel math ;
[ 10 2 / swap continue-with 1 0 / ] callcc1

View file

@ -0,0 +1,29 @@
\ 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 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 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 ;
5 1 proc2
bye

View file

@ -0,0 +1,5 @@
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,30 @@
\ 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.
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" 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" 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 5 1 6 goto ; go
bye

View file

@ -0,0 +1,5 @@
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,50 @@
' FB 1.05.0 Win64
' compiled with -lang fb (the default) and -e switches
Sub MySub()
Goto localLabel
Dim a As Integer = 10 '' compiler warning that this variable definition is being skipped
localLabel:
Print "localLabel reached"
Print "a = "; a '' prints garbage as 'a' is uninitialized
End Sub
Sub MySub2()
On Error Goto handler
Open "zzz.zz" For Input As #1 '' this file doesn't exist!
On Error Goto 0 '' turns off error handling
End
handler:
Dim e As Integer = Err '' cache error number before printing message
Print "Error number"; e; " occurred - file not found"
End Sub
Sub MySub3()
Dim b As Integer = 2
On b Goto label1, label2 '' jumps to label2
label1:
Print "Label1 reached"
Return '' premature return from Sub
label2:
Print "Label2 reached"
End Sub
Sub MySub4()
Dim c As Integer = 3
If c = 3 Goto localLabel2 '' better to use If ... Then Goto ... in new code
Print "This won't be seen"
localLabel2:
Print "localLabel2 reached"
End Sub
MySub
MySub2
MySub3
MySub4
Print
Print "Pres any key to quit"
Print

View file

@ -0,0 +1,21 @@
window 1
print "First line."
gosub "sub1"
print "Fifth line."
goto "Almost over"
"sub1"
print "Second line."
gosub "sub2"
print "Fourth line."
return
"Almost over"
print "We're just about done..."
goto "Outa here"
"sub2"
print "Third line."
return
"Outa here"
print "... with goto and gosub, thankfully."
HandleEvents

View file

@ -0,0 +1,18 @@
100 PRINT "First line."
110 GOSUB 140
120 PRINT "Fifth line."
130 GOTO 190
140 REM sub1:
150 PRINT "Second line."
160 GOSUB 220
170 PRINT "Fourth line."
180 RETURN
190 REM Ending:
200 PRINT "We're just about done..."
210 GOTO 250
220 REM sub2:
230 PRINT "Third line."
240 RETURN
250 REM Finished:
260 PRINT "... with goto and gosub, thankfully."
270 END

View file

@ -0,0 +1,21 @@
package main
import "fmt"
func main() {
outer:
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if i + j == 4 { continue outer }
if i + j == 5 { break outer }
fmt.Println(i + j)
}
}
k := 3
if k == 3 { goto later }
fmt.Println(k) // never executed
later:
k++
fmt.Println(k)
}

View file

@ -0,0 +1,35 @@
package main
import "fmt"
func main() {
// defer run last
defer fmt.Println("World")
fmt.Println("Hello")
// goto
i := 0
Here:
if i < 5 {
fmt.Println(i)
i++
goto Here
} else {
fmt.Println("i is less than 5")
}
// break & continue
number := 0
for {
number++
if number > 3 {
break
}
if number == 2 {
continue
}
fmt.Printf("%v\n", number)
}
}
// init
func init() {
fmt.Println("run first")
}

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,29 @@
//'i' does not have goto statements, instead control flow is the only legal way to navigate the program.
concept there() {
print("Hello there")
return //The return statement goes back to where the function was called.
print("Not here")
}
software {
loop {
break //This breaks the loop, the code after the loop block will be executed next.
print("This will never print")
}
loop {
loop {
loop {
break //This breaks out of 1 loop.
}
print("This will print")
break 2 //This breaks out of 2 loops.
}
print("This will not print")
}
//Move to the code contained in the 'there' function.
there()
}

View file

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

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 @@
H=: verb define
smoutput 'a'
label_b.
smoutput 'c'
goto_f.
label_d.
smoutput 'e' return.
label_f.
smoutput 'g'
goto_d.
smoutput 'h'
)
H''
a
c
g
e

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,17 @@
public class FizzBuzzThrower {
public static void main( String [] args ) {
for ( int i = 1; i <= 30; i++ ) {
try {
String message = "";
if ( i % 3 == 0 ) message = "Fizz";
if ( i % 5 == 0 ) message += "Buzz";
if ( ! "".equals( message ) ) throw new RuntimeException( message );
System.out.print( i );
} catch ( final RuntimeException x ) {
System.out.print( x.getMessage() );
} finally {
System.out.println();
}
}
}
}

View file

@ -0,0 +1 @@
label $out | ... break $out ...

View file

@ -0,0 +1 @@
label $out | 1e7 | while(true; .+1) | if (1/.) | . == sin then (., break $out) else empty end

View file

@ -0,0 +1 @@
1e7 | until(1/. | . == sin; .+1)

View file

@ -0,0 +1,7 @@
function example()
println("Hello ")
@goto world
println("Never printed")
@label world
println("world")
end

View file

@ -0,0 +1,16 @@
// version 1.0.6
fun main(args: Array<String>) {
intArrayOf(4, 5, 6).forEach lambda@ {
if (it == 5) return@lambda
println(it)
}
println()
loop@ for (i in 0 .. 3) {
for (j in 0 .. 3) {
if (i + j == 4) continue@loop
if (i + j == 5) break@loop
println(i + j)
}
}
}

View file

@ -0,0 +1,8 @@
on foo
abort()
end
on bar ()
foo()
put "This will never be printed"
end

View file

@ -0,0 +1,12 @@
on testPlayDone ()
-- Start some asynchronous process (like e.g. a HTTP request).
-- The result might be saved in some global variable, e.g. in _global.result
startAsyncProcess()
-- pauses execution of current function
play(_movie.frame)
-- The following will be executed only after 'play done' was called in the asynchronous process code
put "Done. The asynchronous process returned the result:" && _global.result
end

View file

@ -0,0 +1,38 @@
-- Forward jump
goto skip_print
print "won't print"
::skip_print::
-- Backward jump
::loop::
print "infinite loop"
goto loop
-- Labels follow the same scoping rules as local variables, but with no equivalent of upvalues
goto next
do
::next:: -- not visible to above goto
print "won't print"
end
::next:: -- goto actually jumps here
-- goto cannot jump into or out of a function
::outside::
function nope () goto outside end -- error: no visible label 'outside' for <goto> at line 2
goto inside
function nope () ::inside:: end -- error: no visible label 'inside' for <goto> at line 1
-- Convenient for breaking out of nested loops
for i = 1, 10 do
for j = 1, 10 do
for k = 1, 10 do
if i^2 + j^2 == k^2 then
print(("found: i=%d j=%d k=%d"):format(i, j, k))
goto exit
end
end
end
end
print "not found"
::exit::

View file

@ -0,0 +1,33 @@
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

View file

@ -0,0 +1,23 @@
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

View file

@ -0,0 +1,80 @@
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

View file

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

View file

@ -0,0 +1,7 @@
j GoHere ;the assembler will convert this label to a constant memory address for us.
nop ; branch delay slot. This instruction would get executed DURING the jump.
; But since NOP intentionally does nothing, it's not a problem.
GoHere:
addiu $t0,1 ;this instruction is the first one executed after jumping.

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,5 @@
$print("start\n")
$goto(skip)
$print("Jumped over")
skip:
$print("end\n")

View file

@ -0,0 +1,5 @@
block outer:
for i in 0..1000:
for j in 0..1000:
if i + j == 3:
break outer

View file

@ -0,0 +1,13 @@
; recursion:
(let loop ((n 10))
(unless (= n 0)
(loop (- n 1))))
; continuation
(call/cc (lambda (break)
(let loop ((n 10))
(if (= n 0)
(break 0))
(loop (- n 1)))))
(print "ok.")

View file

@ -0,0 +1 @@
on conversion goto restart;

View file

@ -0,0 +1,26 @@
DECLARE
i number := 5;
divide_by_zero EXCEPTION;
PRAGMA exception_init(divide_by_zero, -20000);
BEGIN
DBMS_OUTPUT.put_line( 'startLoop' );
<<startLoop>>
BEGIN
if i = 0 then
raise divide_by_zero;
end if;
DBMS_OUTPUT.put_line( 100/i );
i := i - 1;
GOTO startLoop;
EXCEPTION
WHEN divide_by_zero THEN
DBMS_OUTPUT.put_line( 'Oops!' );
GOTO finally;
END;
<<endLoop>>
DBMS_OUTPUT.put_line( 'endLoop' );
<<finally>>
DBMS_OUTPUT.put_line( 'Finally' );
END;
/

Some files were not shown because too many files have changed in this diff Show more