home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / basic / ebasic.lbr / EBASIC.DQC / EBASIC.DOC
Encoding:
Text File  |  1986-05-08  |  14.3 KB  |  452 lines

  1. .HM0
  2. .MT3
  3. .FM0
  4. .MB4
  5.                   BASIC-E DOCUMENTATION
  6.                   =====================
  7.  
  8.         The EBASIC compiler system is an early two-step BASIC
  9.     compiler and interpreter now available as a public-domain system
  10.     under CP/M.  More elaborate proprietary versions of EBASIC are
  11.     marketed under the name CBASIC, with CBASIC2 being the latest
  12.     version.  This EBASIC can be obtained from Heath User's Group
  13.     as well as the SIG/M library.
  14.  
  15.         Our interest in EBASIC is (1) it is in public domain and
  16.     (2) it is a compiled language.  This last means that programs can
  17.     be presented in a way that aids comprehension and understanding,
  18.     since there is no space penalty -- the comments, blank lines, and
  19.     other pleasantries are removed by the compiler.
  20.  
  21. WRITING SOURCE CODE
  22.  
  23.     Source  code  for EBASIC  programs must be writtem  with  a  text 
  24.     editor.   Spaces and tabs may be used freely in a program to make 
  25.     it  more  readable,  and will have no effect on the size  of  the 
  26.     compiler  output file.   Upper or lower case letters may be used, 
  27.     and the compiler will normally translate lower case key words  to 
  28.     upper  case for syntax checking.   File names should be in  upper 
  29.     case.
  30.  
  31. STATEMENT NUMBERS
  32.  
  33.     Line  numbers in EBASIC are optional,   and serve only as  labels 
  34.     referenced by GOTO, GOSUB, etc.  They take from  2 to 32  digits,
  35.     and need not be in ascending order, so long as unique.  Line nos.
  36.     are ignored by the compiler if they are not referenced.  
  37.     Line number 00 is not allowed.
  38.  
  39. CONTINUATION AND MULTIPLE LINES
  40.  
  41.     EBASIC   allows multiple statements on a line (separated  by  co-
  42.     lons) with the following exceptions.  IF (with THEN and ELSE), IF 
  43.     END,  DEF, DIM, and END must be the first and only instruction on 
  44.     a line.  INPUT and REM must be the last instruction on a line.
  45.  
  46.     The  backslash  (\) may be used to break up a logical  line  into 
  47.     several physical lines, as in this example.
  48.  
  49.         IF GAME.END=0 \
  50.         THEN PRINT "THIS IS THE END" \
  51.              ELSE PRINT "KEEP GOING"
  52.  
  53.  
  54.                         EBASIC LANGUAGE SUMMARY
  55.             ========================
  56.  
  57. VARIABLE NAMES
  58.  
  59.     Variable  names may be one to 32 characters long,  consisting  of 
  60.     letters,  digits,  or  periods.   The initial character must be a 
  61.     letter.   The final character must be "$" if,  and only,  if  the 
  62.     variable  is  a string name.   Undefinded numeric  variables  are 
  63.     zero, and undefined strings are null.
  64. DATA TYPES
  65.  
  66.     Data types are Numeric, String and Logical.  Numbers are floating 
  67.     point  with six significant figures and E notation for very large 
  68.     or  small numbers.   The range allowed is 2.7 E-39 to  3.6  E+38.  
  69.     Strings  may  be  one to 255 characters  long.   Logicals  are  0 
  70.     (false) and -1 (true).
  71.  
  72. ARRAYS
  73.  
  74.     Arrays  can have many dimensions,  with subscripts rounded to the 
  75.     nearest integer.  The implied lower limit of a subscript is zero.  
  76.     DIM is used to reserve space (from 0 to the given size).
  77.  
  78. ASSIGNMENT OR REPLACEMENT
  79.  
  80.     To assign a value to a variable, use
  81.  
  82.         <var> = <exp>
  83.  
  84.     or    LET <var> = <exp>
  85.  
  86.     where  <var> is any variable reference
  87.            <exp> is an expression  that is evaluated to the value 
  88.              assigned to <var>
  89.  
  90. OPERATORS
  91.  
  92.     EBASIC provides Arithmetic,  Relational,  and Logical operators, 
  93.     as listed below.
  94.  
  95.         OPERATOR       OPERATION              USAGE
  96.         --------       ---------          -----
  97.  
  98.     ARITHMETIC OPERATORS (Numeric expression only)
  99.  
  100.         ^              Exponentiation      Powers or roots
  101.         -              Unary minus         Negates following number
  102.         *              Multiplication
  103.         /              Division            Arithmetic quotient
  104.         +              Addition            Also string concatenation
  105.         -              Subtraction
  106.  
  107.     RELATIONAL OPERATORS (Strings or numbers)
  108.  
  109.         < or LT        Less than
  110.         <= or LE       Less than or equal to
  111.         = or EQ        Equal to
  112.         >= or GE       Greater than or equal to
  113.         > or GT        Greater than
  114.  
  115.     LOGICAL OPERATORS (Relational expressions)
  116.  
  117.         NOT            True if false, else false
  118.         AND            True if all true, else false
  119.         OR             True if any true, else false
  120.         XOR            True if only one true, else false
  121. .PA
  122.     PRECEDENCE OF OPERATORS
  123.  
  124.         1.  Parentheses
  125.         2.  Functions
  126.         3.  Exponentiation
  127.         4.  Unary minus
  128.         5.  Multiplication and division
  129.         6.  Addition and subtraction
  130.         7.  Relational operators
  131.         8.  NOT
  132.         9.  AND
  133.         10. OR and XOR
  134.  
  135. BUILT-IN FUNCTIONS
  136.  
  137.     MATHEMATICAL (x is arithmetic quantity)
  138.  
  139.         ABS(x)         Absolute value
  140.         ATN(x)         Angle (in radians) whose tangent is X
  141.         COS(x)         Cosine of X (X in radians)
  142.         COSH(x)        Hyperbolic cosine of X
  143.         EXP(x)         Natural exponent of X (e^X)
  144.         INT(x)         Greatest integer less than or equal to X
  145.         LOG(x)         Natural logarithm of X
  146.         RND            Random number from 0 to 1
  147.         SGN(x)         Sign of X (1 = pos., 0 = 0, -1 = neg.)
  148.         SIN(x)         Sine of X
  149.         SINH(x)        Hyperbolic sine of X
  150.         SQR(x)         Square root of X
  151.         TAN(x)         Tangent of X
  152.  
  153.     STRING (s any string; n,m integer values)
  154.  
  155.         ASC(s)         ASCII code of first character of S
  156.         CHR$(n)        ASCII character whose ASCII code is N
  157.         LEFT$(s,n)     Substring, first N characters of S
  158.         LEN(s)         Number of characters in S
  159.         MID$(s,n,m)    Substring, M characters starting from N
  160.         RIGHT$(s,n)    Substring, last N characters of S
  161.         STR$(x)        ASCII representation of X, any number
  162.         VAL(s)         Numeric value of S taken as a number form
  163.  
  164.     MISCELLANEOUS (n is numberic)
  165.  
  166.         POS            Position (column) in current output line
  167.         TAB(n)         Spaces output line up to column N
  168.         FRE            Returns unused memory space
  169.         INP(n)         Returns value at port N
  170.  
  171. USER-DEFINED FUNCTIONS
  172.  
  173.     To define a function, use
  174.  
  175.         DEF FN name = expression
  176.  
  177.     where name is the name of the function,  following variable  name 
  178.            rules.  
  179.  
  180.     The function must be defined before its use.
  181. PROGRAM STATEMENTS
  182.  
  183.     INPUT/OUTPUT STATEMENTS
  184.  
  185.         INPUT "optional prompt string"; variable list
  186.           The (variable list) is one or more variables, separated 
  187.           by commas.   Strings must be enclosed in quotes if they 
  188.           contain commas.   INPUT must be the last instruction on 
  189.           a line.
  190.     
  191.         READ variable list
  192.           Read DATA statement constants.
  193.  
  194.         READ #f; variable list
  195.           Read  sequential  file number f into listed  variables.  
  196.           Most  BASICs use INPUT instead of READ  here.   Strings 
  197.           must be enclosed in quotes if they contain commas.
  198.  
  199.         READ #f,r; variable list
  200.           Read record r of random file f into listed variables.
  201.  
  202.         PRINT list
  203.           Print list of expressions at terminal.
  204.  
  205.         PRINT #f; list
  206.           Print list of expressions into sequential file f.  Com-
  207.           mas  are used to separate items in list,  and  are  in-
  208.           cluded in the output (they are not interpreted as tabs, 
  209.           as  with  the terminal PRINT  command).   EBASIC   will 
  210.           automatically  put quotes around any string expression.  
  211.           PRINT  and READ statements are directly  complementary.  
  212.           That is, if you say
  213.  
  214.               PRINT #1; A, B$, C
  215.  
  216.           it can be read with
  217.  
  218.               READ #1; A, B$, C
  219.  
  220.         PRINT #f,r; list
  221.           Print list of expressions into random file f at  record 
  222.           r.
  223.  
  224.         FILE variable list
  225.           Open  files for read or write.   Up to 8 files  may  be 
  226.           opened  at  a time.   The file names must be in  string 
  227.           variables,  and  if more than one file is  opened,  the 
  228.           variables must be separated with commas.  The files are 
  229.           assigned  file numbers in the order they appear in  the 
  230.           FILE  statement.   If the file to be opened is  random, 
  231.           its record size is shown in a  subscript  to the varia-
  232.           ble name.  
  233.  
  234.           FILE1$ = "DATA1.DAT"
  235.           FILE2$ = "DATA2.DAT"
  236.           FILE FILE1$, FILE2$(100)
  237.  
  238.           In  this  example,  DATA1.DAT (file #1) is opened as  a 
  239.           sequential file, and DATA2.DAT (file #2) is opened as a 
  240.           random file with a block size of 100 bytes.   The block 
  241.           size  does  not have to be related to the  disk  sector 
  242.           size,   but  should  include  room  for  quotes  around 
  243.           strings,  commas between items in PRINT statements, and 
  244.           a CR-LF at the end of the block.
  245.  
  246.         CLOSE (list)
  247.           Close the files whose numbers are in the  list.   Close 
  248.           cancels  any  IF END statement.   It removes  the  file 
  249.           number,  renumbering the  remaining files  without  any
  250.           gap.   CLOSE  (1,2) will close the files in  the  above 
  251.           example.
  252.  
  253.         IF END #f THEN label
  254.           Transfer to labeled instruction (line number) if an end 
  255.           of file is read.  IF END should appear outside the loop 
  256.           where reading is done:
  257.  
  258.                IF END #1 THEN 100
  259.                INSTRUCTIONS$ = "INSTRUCT.DAT"
  260.                FILE INSTRUCTIONS$
  261.                IF END #1 THEN 100
  262.  
  263.               10 READ #1; A$: REM    READ INSTRUCTIONS
  264.                  PRINT A$:    REM    PRINT THEM
  265.                  GOTO 10:     REM    LOOP UNTIL END
  266.  
  267.           100  CLOSE (1): PRINT "END OF INSTRUCTIONS"
  268.                REM  CONTINUE WITH PROGRAM
  269.  
  270.  
  271.           In this example, EBASIC will read and print lines from 
  272.           the file INSTRUCT.DAT until the end is read.
  273.  
  274.  
  275.         DATA list
  276.           Compile a list of constants.   The list may consist  of 
  277.           numeric  and/or string constants.   The strings must be 
  278.           enclosed in quotes if they contain commas.   List items 
  279.           are separated by commas.
  280.  
  281.         RESTORE
  282.           Reset DATA list pointer to the first item.
  283.  
  284.         OUT n,m
  285.           Output a byte m to port n.
  286.  
  287.     CONTROL STATEMENTS
  288.  
  289.         END
  290.           In EBASIC,  END is a compiler directive, not a program 
  291.           statement.   It  instructs the compiler to  reject  any 
  292.           succeeding lines -- there is no more program text.
  293.  
  294.         GOTO label
  295.           Transfer control to the label (line number).
  296.  
  297.         GOSUB label
  298.           Call subroutine at the label.
  299.         IF logical expression
  300.            THEN label or command 
  301.            [ELSE label or command]
  302.           Conditional:  If true THEN, if false ELSE.  The default 
  303.           ELSE  is the next instruction.   IF expression GOTO  is 
  304.           not  allowed.   Always use THEN after IF (THEN label or 
  305.           THEN GOTO label).
  306.  
  307.         ON expression GOTO label list
  308.         ON expression GOSUB label list
  309.           Computed  transfer.   The value of the expression gives 
  310.           the number of the label in the list to transfer to.
  311.  
  312.         RETURN
  313.           At the end of a subroutine, RETURN transfers control to 
  314.           the instruction following the GOSUB call of the subrou-
  315.           tine.  Subroutines may be nested 20 deep.
  316.  
  317.         STOP
  318.           End processing and return control to CP/M.
  319.  
  320.         FOR v = a TO b [STEP c]
  321.           Loop control:   V is an unsubscripted variable,  and A, 
  322.           B,  and C are expressions.   The default step is 1.   A 
  323.           step of 0 makes an infinite loop.  Loops may be nested, 
  324.           but must not be crossed.  For example
  325.  
  326.               FOR X = 1 TO 10
  327.               FOR Y = 1 TO 10
  328.               NEXT Y: NEXT X
  329.  
  330.           is allowed, but
  331.  
  332.               FOR X = 1 TO 10
  333.               FOR Y = 1 TO 10
  334.               NEXT X: NEXT Y
  335.  
  336.           is NOT allowed.
  337.  
  338.         NEXT v
  339.           NEXT complements FOR and ends the loop.
  340.  
  341.     MISCELLANEOUS STATEMENTS
  342.  
  343.         REM text
  344.           Introduce a comment (which is ignored by the compiler).
  345.  
  346.         RANDOMIZE
  347.           Initialize the random number generator.  RANDOMIZE must 
  348.           follow an INPUT statement.
  349.  
  350. COMPILING A PROGRAM
  351.  
  352.     To compile a program, enter the CP/M command
  353.  
  354.         d>x:EBASIC x:fname <$options>
  355.  
  356.     where  x: is a valid drive designation (A:-P:), which may be left 
  357.               off if the default system drive is used, and fname is the 
  358.                   name of the .BAS-type program to be compiled.
  359.  
  360.     The  following options may be specified after the file name.
  361.  
  362.         B List  only source code lines  with  errors.   
  363.           Normally,  the entire  source is listed during the 
  364.           second pass of the  compilation.
  365.  
  366.         C Syntax check only (no output file produced).
  367.  
  368.         D Do not convert lower case to upper case.
  369.  
  370.         E Remember line numbers in error message produced when the
  371.               program is running (takes more space but helps debugging)
  372.  
  373.     The output of the compiler is an intermediate-code .INT-type file.
  374.     E.g.,
  375.  
  376.         A>EBASIC GAME $E
  377.  
  378.     In  this example,  both the compiler (EBASIC.COM) and the  program 
  379.     (GAME.BAS)  are  found by default file rules.   The  output  file 
  380.     produced  is  GAME.INT on the same drive as GAME.BAS.  Line numbers
  381.     will be remembered for use in error messages.
  382.  
  383.  
  384. RUNNING A PROGRAM
  385.  
  386.     To run a compiled program, enter
  387.  
  388.  
  389.         d>x:INT x:fname
  390.  
  391.     where  x: is a valid drive designation (A:-P:), which may be left 
  392.           off if the default system drive is used
  393.         fname is the name of the .INT-type program file to be run.
  394.  
  395. COMPILER ERROR MESSAGES
  396.  
  397.     CE  Can't close file
  398.     DE  Disk error
  399.     DF  Disk or directory full
  400.     DL  Duplicate label or synchronization error
  401.     DP  DIM variable name previously defined
  402.     FC  File name previously defined
  403.     FD  Function name previously defined
  404.     FI  FOR loop index is not an unsubscripted numeric variable
  405.     FN  Wrong number of arguments for function
  406.     FP  Invalid argument mode for function
  407.     FU  Undefined function
  408.     IC  Invalid character in source code
  409.     IE  IF logical expression is not floating point
  410.     IS  Subscript on undimensioned variable
  411.     IU  Array variable not subscripted
  412.     MF  String mode used for numeric expression
  413.     MM  Mixed mode in expression
  414.     NI  NEXT index fails to match FOR index
  415.     NP  Illegal instruction
  416.     NS  No source file
  417.     NU  Undefined NEXT (no FOR)
  418.     SN  Wrong number of subscripts or DIM error
  419.     SO  Compiler stack overflow
  420.     TO  Symbol table overflow
  421.     UL  Undefined label (no destination)
  422.     VO  Variable overflow
  423.  
  424. RUN-TIME ERROR MESSAGES
  425.  
  426.     AC  Null string in ASC argument
  427.     CE  File close error
  428.     DR  Disk read error (random access)
  429.     DW  Disk write error
  430.     DZ  Divide by zero
  431.     EF  End of file on disk file (and no IF END)
  432.     ER  Exceed block size on record
  433.     II  Invalid input from terminal
  434.     IR  Invalid record number in random access
  435.     FU  Attempt access of unopened file
  436.     ME  File creation error
  437.     MF  File identifier not 1 to 8
  438.     NE  Negative exponent
  439.     NI  No .INT (P-code) file
  440.     NM  No memory left
  441.     OD  Attempt to read past data area
  442.     OE  File open error
  443.     OI  Out of bounds on index
  444.     OM  Out of memory (arrays too big)
  445.     OS  String overflow on blocked record
  446.     RE  Attempt read past end of blocked record
  447.     RU  Attempted random access of sequential file
  448.     SB  Array subscript out of bounds
  449.     SL  String length exceeds 255
  450.     SS  MID$ start argument negative
  451.     TZ  TAN argument is pi/2
  452.