In computer programming, COMEFROM is a control flow statement that causes control flow to jump to the statement after it when control reaches the point specified by the COMEFROM argument. The statement is intended to be the opposite of goto and is considered to be more a joke than serious computer science. Often the specified jump point is identified as a label. For example, COMEFROM x
specifies that when control reaches the label x
, then control continues at the statement after the COMEFROM.
A major difference with goto is that goto depends on the local structure of the code, while COMEFROM depends on the global structure. A goto statement transfers control when control reaches the statement, but COMEFROM requires the processor (i.e. interpreter) to scan for COMEFROM statements so that when control reaches any of the specified points, the processor can make the jump. The resulting logic tends to be difficult to understand since there is no indication near a jump point that control will in fact jump. One must study the entire program to see if any COMEFROM statements reference that point.
The semantics of a COMEFROM statement varies by programming language. In some languages, the jump occurs before the statement at the specified point is executed and in others the jump occurs after. Depending on the language, multiple COMEFROM statements that reference the same point may be invalid, non-deterministic, executed in some order, or induce parallel or otherwise concurrent processing as seen in Threaded Intercal.[ citation needed ]
COMEFROM was initially seen in lists of joke assembly language instructions (as 'CMFRM'). It was elaborated upon in a Datamation article by R. Lawrence Clark in 1973, [1] written in response to Edsger Dijkstra's letter Go To Statement Considered Harmful . COMEFROM was eventually implemented in the C-INTERCAL variant of the esoteric programming language INTERCAL along with the even more obscure 'computed COMEFROM'. There were also Fortran proposals [2] for 'assigned COME FROM' and a 'DONT' statement (to complement the existing 'DO' loop).
The following code is for a hypothetical BASIC dialect with COMEFROM
. It asks for a name, greets with the name, and repeats. Line 40 is the jump point specified by the COMEFROM, so when control reaches 40 it jumps to 10.
10COMEFROM4020INPUT"WHAT IS YOUR NAME? ";A$30PRINT"HELLO, ";A$40REM
On 1 April 2004, Richie Hindle published an implementation of COMEFROM for Python that uses debugger hooks. Despite being released on April Fools' Day and not being intended for serious use, the syntax is valid and the implementation fully works. [3]
The code below, which is actually runnable, uses this Python implementation.
fromgotoimportcomefrom,labelcomefrom.repeatname=raw_input("What is your name? ")ifname:print("Hello",name)label.repeatprint("Goodbye!")
This is an implementation in Ruby of the Intercal COME FROM statement.
$come_from_labels={}deflabel(l)if$come_from_labels[l]$come_from_labels[l].callendenddefcome_from(l)callccdo|block|$come_from_labels[l]=blockendend
In the OS/360 Fortran G compiler debug packet, the AT
statement acts like COMEFROM in that it hands the control flow over to the debug block – similar to a breakpoint. [4]
In the following code, the values of SOLON
, GFAR
, and EWELL
are examined as they were at the completion of statement 10. The AT
statement indicates statement 11.
INTEGER SOLON,GFAR,EWELL...10 SOLON=GFAR*SQRT(FLOAT(EWELL))11 IF(SOLON)40,50,60...DEBUGUNIT(3)AT11DISPLAYGFAR,SOLON,EWELLEND
In the following code, the values of STOCK
are displayed when statement 35 is encountered.
DIMENSION STOCK(1000),OUT(1000)...DO 30I=1,100025 STOCK(I)=STOCK(I)-OUT(I)30 CONTINUE35 A=B+C...DEBUGUNIT(3)AT35DISPLAYSTOCKEND
In the following code, tracing begins at statement 10, at statement 20, tracing stops while the loop is executed, and resumes after the loop. Tracing stops just before statement 30 is executed.
10 A=1.512 L=115 B=A+1.520 DO 22I=1,5...22 CONTINUE25 C=B+3.1630 D=C/2STOP...DEBUGUNIT(3),TRACEC DEBUG PACKET NUMBER 1AT10TRACEONC DEBUG PACKET NUMBER 2AT20TRACEOFFDO 35I=1,3...35 CONTINUETRACEONC DEBUG PACKET NUMBER 3AT30TRACEOFFEND