COMEFROM
Template:Short description
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.Template:Cn
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,<ref>Template:Citation.</ref> 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<ref>Template:Cite journal</ref> for 'assigned COME FROM' and a 'DONT' statement (to complement the existing 'DO' loop).
Examples
BASIC
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.
<syntaxhighlight lang="qbasic"> 10 COMEFROM 40 20 INPUT "WHAT IS YOUR NAME? "; A$ 30 PRINT "HELLO, "; A$ 40 REM </syntaxhighlight>
Python
Template:AnchorOn 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.<ref name=":0">Template:Citation.</ref>
The code below, which is actually runnable, uses this Python implementation.
<syntaxhighlight lang="python"> from goto import comefrom, label comefrom .repeat
name: str = raw_input("What is your name? ") if name:
print(f"Hello, {name}")
label .repeat
print("Goodbye!") </syntaxhighlight>
Ruby
This is an implementation in Ruby of the Intercal COME FROM statement.
<syntaxhighlight lang="ruby"> $come_from_labels = {}
def label(l)
if $come_from_labels[l] $come_from_labels[l].call end
end
def come_from(l)
callcc do |block| $come_from_labels[l] = block end
end </syntaxhighlight>
OS/360 Fortran G
In the OS/360 Fortran G compiler debug packet, the <syntaxhighlight lang="text" class="" style="" inline="1">AT</syntaxhighlight> statement acts like COMEFROM in that it hands the control flow over to the debug block Template:Endash similar to a breakpoint.<ref>IBM System/360 and System/370 Fortran IV Language, GC28-6515-10, May 1974</ref>
In the following code, the values of <syntaxhighlight lang="text" class="" style="" inline="1">SOLON</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">GFAR</syntaxhighlight>, and <syntaxhighlight lang="text" class="" style="" inline="1">EWELL</syntaxhighlight> are examined as they were at the completion of statement 10. The <syntaxhighlight lang="text" class="" style="" inline="1">AT</syntaxhighlight> statement indicates statement 11.
<syntaxhighlight lang="fortranfixed">
INTEGER SOLON, GFAR, EWELL
.
.
.
10 SOLON = GFAR * SQRT(FLOAT(EWELL)) 11 IF (SOLON) 40, 50, 60
.
.
.
DEBUG UNIT(3)
AT 11
DISPLAY GFAR, SOLON, EWELL
END
</syntaxhighlight>
In the following code, the values of <syntaxhighlight lang="text" class="" style="" inline="1">STOCK</syntaxhighlight> are displayed when statement 35 is encountered.
<syntaxhighlight lang="fortranfixed">
DIMENSION STOCK(1000),OUT(1000)
.
.
.
DO 30 I=1, 1000
25 STOCK(I)=STOCK(I) - OUT(I) 30 CONTINUE 35 A = B + C
.
.
.
DEBUG UNIT(3)
AT 35
DISPLAY STOCK
END
</syntaxhighlight>
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.
<syntaxhighlight lang="fortranfixed"> 10 A = 1.5 12 L = 1 15 B = A + 1.5 20 DO 22 I = 1,5
.
.
.
22 CONTINUE 25 C = B + 3.16 30 D = C/2
STOP
.
.
.
DEBUG UNIT(3), TRACE
C DEBUG PACKET NUMBER 1
AT 10
TRACE ON
C DEBUG PACKET NUMBER 2
AT 20
TRACE OFF
DO 35 I = 1,3
.
.
.
35 CONTINUE
TRACE ON
C DEBUG PACKET NUMBER 3
AT 30
TRACE OFF
END
</syntaxhighlight>
See also
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link
- F. X. Reid Template:Endash an expert on the semantics of COMEFROM<ref>F. X. Reid, On the Formal Semantics of the COMEFROM Statement. FACS FACTS, Issue 2006-1, pages 18–20, March 2006.</ref>
- Goto/From signal routing blocks in MATLAB Simulink
- Template:Annotated link
- Template:Annotated link
- Template:Annotated link