Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
FOCAL (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language== The following description is based on FOCAL-69 as seen in the FOCAL-8 language reference manual.{{sfn|Manual|1968}} ===Direct and indirect modes=== FOCAL followed the JOSS model for interaction via a [[command line interface]]. This allowed users to type in commands in "direct mode", which were performed immediately, or to prefix them with a line number, in which case they were added to the program if they were unique, or overrode existing code if the same number had previously been used.{{sfn|Manual|1968|p=2.1}} The method of operation is similar to BASIC's "[[Direct mode|immediate mode]]" vs. "program mode". It contrasts with JOSS, in which all commands, both program and direct, were saved and loaded as part of the user's workspace. This allowed JOSS to have commands without line numbers, which they used for {{code|Form}} definitions and other tasks. FOCAL lacked this capability, so those instructions that needed to be loaded and saved became options on other program-mode commands like {{code|Type}}.{{sfn|Manual|1968|p=2.1}} ===Program statements=== Every line in a FOCAL program must start with a line number. As with JOSS, line numbers are fixed-point numbers consisting of two two-digit integers separated by a period. In FOCAL-8, valid line numbers range from 1.01 through 31.99. When printed out, using {{code|WRITE}}, the FOCAL equivalent to BASIC's {{code|LIST}}, leading zeros will be added; 1.10 will be printed as {{code|01.10}}. This makes all lines numbers five characters long when output so that all line numbers align. Statements referring to those lines do not need the leading zeros, for example, {{code|GOTO 1.10}}.{{sfn|Manual|1968|p=2.7}} The number on the left of the period is known as the "group number". Groups provide some level of code organization that is lacking in languages like Fortran or BASIC. The primary use of these was to use a group as a [[subroutine]] which can be called with {{code|DO}}, for example, {{code|DO 5}} will jump to the subroutine written in group 5. The editor also used these during edit-time, for example, one could {{code|WRITE 2}} to produce a listing of the code in group 2, or {{code|ERASE 4}} to delete all of the lines in group 4.{{sfn|Manual|1968|p=2.7}} Every line must start with a command keyword following the line number.{{sfn|Manual|1968|p=2.7}} There is no concept of a "default command" as is the case in BASIC with its optional {{code|LET}} statement. Multiple statements can be placed on a single line, separated by semicolons. Usually, the behavior is no different than if the statements had been on separate lines, except in the case of FOR loops.{{sfn|Manual|1968|p=3.7}} === Commands === ====ASK==== The {{code|ASK}} command (abbreviation A) will take a list of strings and variables, echo the strings and store the user input in variables.{{sfn|Manual|1968|p=3.2}} Equivalent to BASIC's {{code|INPUT}}. 01.01 ASK "NAME", NAME 01.02 ASK "COORDINATES", X, Y 01.03 ASK "A1",A1,"OMEGA",W,"T0",T0,"DAMPING FACTOR",DAMPINGFACTOR If the user doesn't enter a number but enters text, the system will convert the initial character to a number with "A"=1, "B"=2, etc. ====COMMENT==== The {{code|COMMENT}} command (abbreviation C) creates a remark.{{sfn|Manual|1968|p=3.6}} Equivalent to BASIC's {{code|REM}}. The original manual lists {{code|CONTINUE}} as a synonym of COMMENT, used to mark empty lines, but this has no internal difference.{{sfn|Manual|1968|p=A.1}} 01.01 COMMENT: THE SUMER GAME, BY RICHARD MERRILL ====DO==== The {{code|DO}} command (abbreviation D) branches execution to a subroutine. It is the equivalent of BASIC's {{code|GOSUB}}. The subroutine is referenced either by group number or line number. If a line number is provided, that single line is run and then automatically returns to continue execution at the statement after the {{code|DO}}. If no line number is provided, execution starts at the first line of the block and continues until the end of the block is reached. {{code|RETURN}} is supported, but is only required to return early from the group, it is not needed at the end of the group.{{sfn|Manual|1968|p=3.4}} 01.15 DO 7.24 01.16 DO 8 ====FOR==== The {{code|FOR}} command (abbreviation F) implements a [[for loop]]. When three arguments are specified, the first is the initial value of the loop variable, the second is the increment, and the third value is the terminating value for the loop. If only two values are provided, the first is the initial value and the second is the terminating value, and the increment is set to 1. This is the pattern from [[FORTRAN]]'s {{code|DO}} loops, as opposed to BASIC's {{code|FOR}} where the increment is the last value, if present. In contrast to other parts of the language where multiple statements on a line are independent, the {{code|FOR}} always runs the statements that follow it on the line before the termination has been met, and then continues to the next line. Thus, loops have to be on a single line, or alternatively, call a subroutine with {{code|DO}}. There is no equivalent of BASIC's {{code|NEXT}}.{{sfn|Manual|1968|p=3.7}} 01.01 FOR X=1,10; TYPE X,! 01.02 FOR X=0,10,100; DO 2 A sample FOR loop: 01.10 ASK "HOW MUCH MONEY DO YOU WANT TO BORROW ?",PRINCIPAL 01.20 ASK "FOR HOW MANY YEARS ?",TERM 01.30 FOR RATE=4.0,.5,10;DO 2.0 01.40 QUIT 02.10 SET INTEREST=PRINCIPAL*(RATE/100)*TERM 02.20 TYPE "RATE",RATE," ","INTEREST",INTEREST,! ====GOTO==== The {{code|GOTO}} command (abbreviation G) jumps program execution to the specified line number.{{sfn|Manual|1968|p=3.4}} It is identical to the same-named statement in BASIC. In FOCAL, {{code|GOTO}} is also used to begin execution, like BASIC's {{code|RUN}}, but in this use, the documentation refers to it as {{code|GO}} instead of {{code|GOTO}} in spite of the underlying command being the same. 01.05 GOTO 1.01 02.90 TYPE !!,"TRY AGAIN.",!!!!!;GOTO 1.1 ====IF==== The {{code|IF}} command (abbreviation I) provides a conditional branch based on the sign of the expression. After the numeric expression, the IF command can take one to three line numbers. If the expression is less than zero, execution branches to the first line number; if equal to zero, to the second line number; if greater than zero, to the third line number. The language lacked relative operators such as greater than, equal or less than. To branch if X > 5, one must compare X - 5.{{sfn|Manual|1968|p=3.5}} 02.20 IF (25-25) 2.4,2.3,2.4 03.01 IF (X) 3.1,3.02,3.1 IF could be short-formed by placing a semicolon (or end of line) beyond the first line number. For example: 02.20 IF (X)1.8; TYPE "Q" 02.30 IF (X)1.8,2.50 02.40 TYPE "P" In this case the test at 2.20 will cause the program to jump to line 1.8 if the test is negative, otherwise it will continue and type "Q" to the console. Line 2.30 will jump to 1.8 or 2.5 if the value is negative or zero, and otherwise continue to type "P" to the console.{{sfn|Manual|1968|p=3.5}} ====QUIT==== The {{code|QUIT}} command (abbreviation Q) terminates execution of the program and returns control to the editing environment.{{sfn|Manual|1968|p=3.6}} Equivalent to BASIC's {{code|STOP}} or {{code|END}}. 01.10 FOR X=-10,1,10;TYPE X 01.20 QUIT ====RETURN==== The {{code|RETURN}} command (abbreviation R) branches execution from a subroutine back to the calling location.{{sfn|Manual|1968|p=3.6}} The use of {{code|RETURN}} is optional at the last line of a subroutine, a subroutine returns at the last line in the group anyway. The following is a sample subroutine for converting a yes/no prompt into a value.[http://www.pdp8.net/lang/focal/misc.shtml] 22.78 COMMENT: 'YES OR NO' SUBROUTINE 22.80 ASK "ANSWER YES OR NO ? ",AN 22.82 IF (AN-0YES)22.84,22.86 22.84 IF (AN-0NO)22.8,22.88,22.8 22.86 SET X=2;RETURN 22.88 SET X=1;RETURN ====SET==== The {{code|SET}} command (abbreviation S) assigns the results of an expression to the specified variable.{{sfn|Manual|1968|p=3.3}} Equivalent to BASIC's {{code|LET}}. 01.30 SET PI=3.14156 01.60 SET INTEREST=PRINCIPAL*(RATE/100)*TERM ====TYPE ==== The {{code|TYPE}} command (abbreviation T) provides for output of one or more items separated by commas.{{sfn|Manual|1968|p=3.1}} Equivalent to BASIC's {{code|PRINT}}. Items can be variables, literal strings surrounded by double-quotes, or a variety of control characters. The control characters include the {{code|!}} to output a carriage return and line feed, {{code|#}} for the carriage return alone, and {{code|:}} for a tab character. Control characters can be strung together, for example, {{code|!!!}} will output three CR/LFs without the need to separate them with commas.{{sfn|Manual|1968|p=3.2}} TYPE [NUMBERS, E1, "TEXT", !, #, :, $ OR %] ...OUTPUT 01.10 TYPE "HI THERE, GOOD LOOKING. HOW MUCH MONEY DO YOU WANT TO BORROW?",! 01.50 TYPE "INTEREST",INTEREST,! 01.80 TYPE "THE INTEREST FOR",TERM," YEARS",!,"IS",INTEREST, " DOLLARS.",!! 01.90 TYPE "NEW YORK",!,"WASHINGTON",!,"ATLANTA",!,"DALLAS",! 02.10 TYPE "X",X," ","X^2",X^2," ","SQRT",FSQT(X) 03.20 TYPE ".",# 02.20 TYPE !!!!! {{code|TYPE}} also included an optional format specifier indicated using the format {{code|%x.yz}}, where x is the number of digits to the left of the decimal, and yz is the number of digits to the right of the period. The default format was {{code|%8.4}}, meaning a maximum of eight digits and four to the right of the period.{{sfn|Manual|1968|p=2.2}} So, for example: SET A=67823 TYPE %6.01,A = 67823.0 TYPE %5,A = 67823 TYPE %8.03,A = 67823.000 TYPE %,A = 6.7823E4 Note the extra leading spaces in some examples, padding out the full defined width. Using % alone caused the output to be printed in "floating point format" using the E.{{sfn|Manual|1968|p=2.3}} A special control character was {{code|$}} which caused a table of all defined variables and their values to be printed. Only the first two letters of the name will be printed, padded with a zero if need be. Arrays elements are printed on separate lines and variables with only one element will be indexed (00). For example:{{sfn|Manual|1968|p=2.5}} TYPE $ A0(00)=67823 === Variables === Variable names may start with any letter except F (F is reserved for functions) and may contain any sequence of letters and numbers. However, only the first two characters are significant. For example, the following code sample from ''FOCAL: A New Conversational Language''<ref>{{cite book |title=FOCAL: A New Conversational Language |url=http://www.cs.uiowa.edu/~jones/pdp8/focal/focal69.html |publisher=Digital}}</ref> refers to the same variable as DESTINATION and then DES. Internally, both references refer to a variable designated DE: 01.80 ASK DESTINATION 02.30 IF (DES-14) 2.4,3.1,2.4 Any variable may be treated as an array, allowing subscripts from -2048 through 2047.[http://www.pdp8.net/lang/focal/misc.shtml] === Math === FOCAL contained five mathematical operators:{{sfn|Manual|1968|p=2.4}} * {{code|^}} for exponents β the exponent is converted to a 12-bit integer * {{code|*}} for multiplication * {{code|/}} for division * {{code|+}} for addition * {{code|-}} for subtraction One curiosity of the 1968 version of FOCAL was that the operators had independent precedence, as in the order above. Although this certainly applies to exponentiation, multiplication, and division, there is some ambiguity in the 1968 FOCAL manual with regards to addition and subtraction. Specifically, examples that demonstrate operator precedence for multiplication and division are provided, but examples demonstrating operator precedence for addition and subtraction are lacking. Nevertheless, the literal interpretation of the 1968 FOCAL manual suggests that the formula {{code|SET T{{=}}2-3+1}} would be evaluated in the order 2-(3+1) and thus produce -2. This was very different than most languages, where the + and - had equal precedence and would be evaluated (2-3)+1 to produce 0.{{sfn|Manual|1968|p=2.4}} This can cause subtle errors when converting FOCAL source code to other systems. However, the + and - have the same precedence in FOCAL-69 and FOCAL-71, so {{code|SET T{{=}}2-3+1}} yields 0, as expected. FOCAL was unusual in that mathematical expressions could use (), [], and <> interchangeably in matched pairs to establish precedence.{{sfn|Manual|1968|p=2.4}} For example, the following is a valid expression: 01.30 SET A=<10*[5+1]*(1+5)> All of these are the same level of precedence and read left-to-right when at the same level, so this statement will be evaluated [], then (), then <>, to produce 360.{{sfn|Manual|1968|p=2.4}} The language contained the following built-in functions:{{sfn|Manual|1968|p=3.10}} * {{code|FABS()}} β Absolute value * {{code|FATN()}} β Arctangent * {{code|FCOS()}} β Cosine of argument in radians * {{code|FEXP()}} β Natural base to the power of the argument * {{code|FITR()}} β Integer part of the argument * {{code|FLOG()}} β [[Napierian_logarithm|Naperian log]] * {{code|FRAN()}} β Random number * {{code|FSGN()}} β Sign of the argument; FSGN(0)=1 in FOCAL-69, but FSGN(0)=0 in FOCAL-71 and later versions * {{code|FSIN()}} β Sine of an angle given in radians * {{code|FSQT()}} β Square root ===Character functions=== FOCAL-71 added two new functions for inputting single-character values, similar to ASK but returning ASCII values. * {{code|FIN}} stops until the user types a character and then returns the ASCII code * {{code|FOUT}} outputs the ASCII value as a character to the console or file ===Other functions=== FOCAL also included several special-purpose functions:{{sfn|Manual|1968|p=A.3}} * {{code|FADC}} reads a value from the PDP-8's analog-to-digital converters, with the channel number as the parameter * {{code|FDIS}} plots at a given Y location * {{code|FDXS}} plots at a given X location
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)