home *** CD-ROM | disk | FTP | other *** search
/ Between Heaven & Hell 2 / BetweenHeavenHell.cdr / 100 / 88 / sci.doc < prev    next >
Text File  |  1984-08-31  |  43KB  |  1,118 lines

  1.                                            ii
  2.                                           iiii
  3.                                            ii
  4.                sss           cccc             
  5.              ssssssss      cccccccc      iiiii
  6.             sss    sss    ccc    ccc     iiiii
  7.             sssss        ccc               iii
  8.              sssss       ccc               iii
  9.                 sssss    ccc               iii
  10.             sss    sss    ccc    ccc       iii
  11.              ssssssss      cccccccc     iiiiiiiii
  12.                ssss          cccc       iiiiiiiii
  13.  
  14.  
  15.                       Small C Interpreter
  16.                     Version 1.3 for MS-DOS
  17.                    by Bob Brodt, 28 Nov 1985
  18.  
  19.  
  20.                           DISCLAIMER
  21.  
  22. The  author makes no expressed or implied warranties of any kind
  23. regarding this program and documentation.  In no event will  the
  24. author be liable for any damages or losses arising out of use of
  25. this program and documentation.
  26.  
  27.  
  28.                           Bob Brodt
  29.                        34 Mehrhof Road
  30.                    Little Ferry, NJ 07643
  31.                     Phone (201) 641-9582
  32.  
  33.  
  34.  
  35.  
  36. INTRODUCTION
  37.  
  38.    SCI is a "C" language interpreter loosely based on the language
  39. subset described in James Hendrix' "Small C" (see the section
  40. "DIFFERENCES FROM SMALL C", below).  It is a fully interactive
  41. interpreter (no compilation!) that includes a simple line editor and
  42. a program trace facility.  SCI was meant to be a stepping stone for
  43. the experienced BASIC programmer who is ready to move up to the
  44. exciting world of "C"!
  45.  
  46.    The SCI interpreter is very similar to BASIC internally - each
  47. line of input is first scanned and "tokenized", a process in which
  48. language elements (numbers, variables, keywords, etc.) are converted
  49. to one-, two- or three-byte "tokens" which are easier to handle by a
  50. computer than arbitrarily long character sequences.  Unlike BASIC
  51. however, SCI programs do not require line numbers.  The program flow
  52. is directed by SCI purely through the structured programming
  53. constructs inherent in the "C" language.
  54.  
  55.    SCI should run on any MS-DOS or PC-DOS computer that has a minimum
  56. of 64Kbytes of RAM.  The available free memory (that portion of memory
  57. not used by the interpreter's code and data storage) is automatically
  58. divided among "user progam code", "variable table", "function table"
  59. and "stack" memory segments.  If necessary, the sizes of these
  60. segments may be changed at program startup, however their total may
  61. not exceed 64Kbytes.
  62.  
  63.    The integrated editor does require that the terminal emulation
  64. firmware in your particular computer be capable of responding to the
  65. following ASCII control codes:
  66.  
  67.       backspace (08 HEX) - moves the cursor one column to the left
  68.                            without destroying the character at the
  69.                            new column position.
  70.       return (0D HEX)    - positions the cursor at the left margin
  71.                            of the current line on the CRT.
  72.       linefeed (OA HEX)  - moves the cursor down one line and causes
  73.                            the screen to scroll up at the last CRT
  74.                            display line.
  75.  
  76.    These control codes are fairly standard and should work on your
  77. machine.
  78.  
  79.  
  80. STARTING OUT
  81.  
  82.    The files on the distribution diskette are:
  83.  
  84.       SCI.DOC   - documentation
  85.       SCI.COM   - the interpreter proper
  86.       SHELL.SCI - the command shell, written in "Small C"
  87.       CALC.SCI  - a sample calculator program written in "Small C"
  88.  
  89.    To start up SCI, make sure that SHELL.SCI resides on the current
  90. drive and then execute SCI.COM. The interpreter will then read and
  91. execute the program it finds in SHELL.SCI.
  92.  
  93.    You may also specify a different "Small C" program file for SCI
  94. to execute on startup.  By typing the following operating system
  95. command line, for example:
  96.  
  97.    A>SCI B:STARTUP
  98.  
  99. the interpreter will search disk B: for a file called STARTUP and
  100. execute it in lieu of the default SHELL.SCI file.  Be aware,
  101. however, that the standard SHELL.SCI file contains many often-used
  102. operating system interface functions that must be copied over to
  103. your customized startup program if they are going to be used by
  104. that program.  See the section on "LIBRARY FUNCTIONS" for a list of
  105. these functions.
  106.  
  107. MEMORY ALLOCATION
  108.  
  109.    The interpreter automatically divides up whatever free memory is
  110. available (after SCI is loaded) among four segments for use by your
  111. programs and data.  These segments are: the Program Code, Variable
  112. Table, Function Table and Stack.  The Program Code segment contains
  113. the tokenized version of your program code.  The Variable Table
  114. contains information about all "active" variables.  All global
  115. variables are active at all times.  Local variables are considered
  116. to be active if the function in which they were declared is still
  117. active.  A function is active even though it may be waiting for
  118. another called function to return to it.  Thus, if you have a habit
  119. of writing deeply recursive functions, you will soon run out of
  120. Variable Table space.  Each function takes up exactly one
  121. entry in the Function Table.
  122.  
  123.    If the interpreter complains about "out of memory", "too many
  124. variables", "too many functions" or "stack overflow", you may be
  125. able to circumvent the problem by telling SCI how much memory to
  126. assign to each of these four segments with the following options:
  127.  
  128.       -P nnnn    - assign "nnnn" decimal bytes for program storage.
  129.       -V nn      - allow space for a maximum of "nn" active variables.
  130.       -F nn      - allow space for a maximum of "nn" functions.
  131.       -S nn      - set the stack size to "nn".
  132.  
  133.    Thus, the command line:
  134.  
  135.       A>SCI -P 8000 -V 30 -F 80 -S 40
  136.  
  137. will set aside 8000 bytes for program storage, allow a maximum of
  138. 30 variables to be active at one time, allow a maximum of 80 function
  139. declarations and leave 40 stack entries.
  140.  
  141.  
  142. COMMAND LINE OPTIONS
  143.  
  144.    It is possible to pass command line parameters to the startup
  145. program with the "-A" option, thus:
  146.  
  147.       A>SCI STARTUP -A FILE1.DAT FILE2.DAT FILE3.DAT
  148.  
  149. will pass the strings FILE1.DAT, FILE2.DAT and FILE3.DAT to the
  150. startup program in the file called STARTUP.  See the section on
  151. "PASSING ARGUMENTS TO THE SHELL" for an example of how these
  152. arguments are handled from within a program.
  153.  
  154.  
  155. THE SHELL
  156.  
  157.    The "stock" version of the shell simply displays a prompt on the
  158. console, reads a line of input, and attempts to execute the input
  159. line.  The entire shell program (sans Library Functions) is shown
  160. here:
  161.  
  162. int size,top;
  163. char line[80];
  164. char program[24000];
  165.  
  166. main()
  167. {
  168.    int from, to;
  169.  
  170.    top=24000;
  171.    program[0]='Z';    # This is an "End of program" token - required
  172.    size=1;
  173.  
  174.    # print sign-on message
  175.    printf("%s\nShell V1.1, 24 Oct 1985\n",sys(0));
  176.  
  177.    while(1)
  178.    {
  179.       puts("> ");
  180.       if(gets(line))
  181.       {
  182.          if (!strncmp(line,"edit",4))
  183.             size = sys(atoi(line+5),program,15); # envoke the editor
  184.          else if (!strncmp(line,"list",4))
  185.          {
  186.             if(line[4])
  187.                sscanf(line+4,"%d %d",&from,&to);
  188.             else
  189.             {
  190.                from=1;
  191.                to=32765;
  192.             }
  193.             sys(program,from,to,22);        # list the program buffer
  194.          }
  195.          else if (!strncmp(line,"save",4))
  196.             sys(line+5,program,21);         # save the program buffer
  197.          else if (!strncmp(line,"load",4))
  198.             size = sys(line+5,program,20);  # load the program buffer
  199.          else if (!strncmp(line,"core",4))
  200.             printf("%d bytes free\n",top-size); # memory available
  201.          else
  202.             #
  203.             # attempt to parse the line as a "Small C" statement.
  204.             # Note that the shell always displays the results of the
  205.             # statement, so you could enter something like: 2+2 and
  206.             # a 4 would be printed.
  207.             #
  208.             printf("%d\n",sys(line,program,12));
  209.       }
  210.    }
  211. }
  212.  
  213.  
  214.    Later, as you become more familiar with the "C" language, you may
  215. wish to modify the shell program and add your own commands.  The
  216. stock version of the shell recognizes four basic (pardon the pun)
  217. commands: "edit", "list", "load", "save", "core" and "exit". These
  218. are functionally similar to the BASIC commands: "EDIT", "LIST",
  219. "LOAD", "SAVE", "FREE" and "SYSTEM" respectively, with the exception
  220. that no quote (") is required in front of the file name for the
  221. "load" and "save" commands.  Anything else typed at the shell prompt
  222. is assumed to be a valid "Small C" statement and is handed off to the
  223. interpreter to be executed.
  224.  
  225.    By the way, programs that are "save"d by SCI are ordinary text
  226. files, suitable for editing with your favourite text editor.
  227.  
  228.  
  229. THE EDITOR
  230.  
  231.    To edit a program, simply type "edit", or "edit n", where "n" is
  232. the line number in the program to start editing.  The requested line
  233. number is displayed followed by a colon (:) and then by the program
  234. text of that line.  The cursor is positioned on the first character
  235. of the program text, i.e. immediately to the right of the colon.
  236.  
  237.    The editor has two different operating modes: EDIT and INSERT.
  238. In EDIT mode, you may move the current character position ("cursor")
  239. anywhere within the program and you may delete text.  In INSERT mode,
  240. any characters that you type are inserted into the program until you
  241. return to EDIT mode.  There are two flavors of INSERT: CHARACTER
  242. INSERT and LINE INSERT.  CHARACTER INSERT mode remains in effect only
  243. until you type a <RETURN>, in other words you may only insert
  244. characters on the current line.  LINE INSERT mode allows you to enter
  245. entire lines into the program buffer and is ended when you type an
  246. <ESCAPE>.  These will be described more fully below.
  247.  
  248.    Any changes you make during an editing session affect only the
  249. program currently residing in memory. The copy of the program on disk
  250. (if one exists) is not changed until you issue a "save" command from
  251. the standard command shell.
  252.  
  253.    To leave the editor and return to the command shell, type an
  254. <ESCAPE> character.
  255.  
  256.    Editing commands are divided into 3 categories: cursor movement,
  257. text insertion and text deletion. The cursor movement commands allow
  258. you to position the cursor anywhere in the program. These are:
  259.  
  260.    'h' or <BACKSPACE> -
  261.       moves the cursor to the left by 1 character position.
  262.  
  263.    'l' (that's a lower-case "ell") or <SPACE> -
  264.       moves the cursor to the right by 1 character position.
  265.  
  266.    'j' or <RETURN> or <LINEFEED> -
  267.       advances the cursor to the next line ON THE SCREEN and displays
  268.       the next line of the program.
  269.  
  270.    'k' -
  271.       advances the cursor to the next line ON THE SCREEN and displays
  272.       the previous line of the program.
  273.  
  274.    'g' -
  275.       prompts for a line number, advances the cursor to the next line
  276.       on the screen and displays the requested line of the program.
  277.       If the requested line number is beyond the last line (or before
  278.       the first) line of program text, the last (resp. first) line is
  279.       displayed.
  280.  
  281.    'f' -
  282.       prompts for a character sequence to be searched for in the
  283.       program, advances the cursor to the next line on the screen and
  284.       displays the requested line of the program.  The cursor is
  285.       positioned to the first character of the string sought.
  286.  
  287.    If the cursor is moved past the end of the program, the editor will
  288. display an <EOF> and will not allow you to move past it. Similarly, if
  289. the cursor is moved to the first line of the program, the 'k' command
  290. will be inoperative.
  291.  
  292.    The text insertion commands are:
  293.  
  294.    'i' -
  295.       will cause characters to be inserted before the current cursor
  296.       position. As you type, the characters to the right of the
  297.       cursor will appear to be overwritten on the screen, but are
  298.       actually shifted (in the editor's buffer) to make room for the
  299.       inserted text.  To leave the character insertion mode, end the
  300.       line with a <RETURN>.  The cursor will advance to the next line
  301.       on the screen and the line will be redrawn with the newly
  302.       inserted text.  Typing an <ESCAPE> will cancel anything that
  303.       was typed before it and return to EDIT mode with the original
  304.       line intact.
  305.  
  306.    'I' -
  307.       will enter LINE INSERT mode. All characters typed will be
  308.       inserted into the program in front of the line the cursor was
  309.       resting on.  When in LINE INSERT mode, you will be prompted
  310.       with the word "new:" instead of the current line number, to
  311.       indicate that the editor is in the special INSERT mode.  To
  312.       leave LINE INSERT mode type an <ESCAPE>.
  313.  
  314.    Text may be deleted from the program either a character-at-a-time
  315. or a line-at-a-time:
  316.  
  317.    'd' -
  318.       will delete the character the cursor is resting on. The cursor
  319.       advances to the next line on the screen and the current line is
  320.       redrawn with the deleted character missing.
  321.  
  322.    'D' -
  323.       will delete the line the cursor is resting on. The cursor
  324.       advances to the next line on the screen and the next line in
  325.       the program is displayed.
  326.  
  327.    Remember, type <ESCAPE> to exit the editor!
  328.  
  329.  
  330. LANGUAGE ELEMENTS
  331.  
  332.    Although SCI implements only a subset of the "C" language, it is
  333. powerful enough to teach you the major principles of "C". We're going
  334. to cop out now and refer you to a higher authority for a sound
  335. introduction to "C", but we will list here the major features of the
  336. language subset recognized by SCI for easy reference.
  337.  
  338. Line Terminators
  339.  
  340.    In standard "C", a statement is typically terminated with a semi-
  341. colon (;).  In SCI, all statements are terminated by either the end
  342. of the line or by a semicolon, although the semicolon is optional.
  343. This means that an expression MUST be completely contained on one
  344. line.  This is not as major a deficiency as it may seem at first,
  345. when you consider the fact that extremely long expressions are very
  346. difficult for the human reader to understand and, after all, we ARE
  347. interested in learning how to write readable programs.
  348.  
  349. Comments
  350.  
  351.    Comments start with a number symbol (#) and end at the end of the
  352. current program line.  The standard "C" comment delimiters, /* and */
  353. are not recognized and will cause a "syntax error" message.
  354.  
  355. Identifiers
  356.  
  357.    Identifiers may be a maximum of 8 characters long, the first
  358. character must be an uppercase or lowercase letter ("a-z", "A-Z") or
  359. an underscore ("_"). The remaining 7 characters may include digits
  360. ("0-9").
  361.  
  362.    The following are all examples of valid identifiers:
  363.  
  364.         this_is_an_identifier
  365.         _0123
  366.         XYZ
  367.  
  368. Keywords
  369.  
  370.    The following words are reserved by SCI and may not be used as
  371. variable names:
  372.  
  373.         break     entry    return
  374.         char      if       sys
  375.         else      int      while
  376.  
  377.    The "entry" keyword is used to tell the interpreter which function
  378. in the startup program (normally SHELL.SCI) is to be executed first
  379. after the program has been loaded.  There may be only one "entry"
  380. within a program.  Any functions or variables that were declared
  381. before the "entry" keyword are considered "Library Functions" and are
  382. globally known to all functions.  Functions and variables declared
  383. after the "entry" keyword are hidden from user-written functions.
  384. Please read the section on "SCOPE OF VARIABLES" for more information.
  385.  
  386.    The keyword "sys" is a user program interface to some useful
  387. interpreter functions, such as the program editor.  These are
  388. described in the "LIBRARY FUNCTIONS" section.
  389.  
  390. Constants
  391.  
  392. Numeric Constants
  393.  
  394.    SCI supports decimal integer constants in the range -32767 to
  395. 32766.  SCI also supports the standard "C" notation for integer
  396. hexadecimal and octal notation.
  397.  
  398. Character Constants
  399.  
  400.    A character constant must be surrounded by apostrophes (')
  401. and may be one of the following:
  402.  
  403.         * a single ASCII alphanumeric or punctuation (i.e. printable)
  404.           character
  405.         * a backslash (\) character followed by 3 octal digits which
  406.           may be used to represent any 1 byte value
  407.         * a backslash followed by one of the characters n, r, b or t
  408.           which represent the <LINEFEED> (a.k.a. "newline"), <RETURN>
  409.           carriage return), backspace and tab characters respectively
  410.  
  411.    The following are examples of valid character constants:
  412.  
  413.         'A'      the ASCII character "A"
  414.         '\177'   octal character
  415.         '\n'     newline
  416.         '\r'     carriage return
  417.         '\b'     backspace
  418.         '\t'     horizontal tab
  419.         '\\'     the backslash character
  420.         '\''     the apostrophe character
  421.  
  422. Strings
  423.  
  424.    Strings may include any of the character constants mentioned
  425. above, as for example:
  426.  
  427.       "this is a string\007\n"
  428.  
  429.    Strings always include a null (zero) byte, marking the end of the
  430. string.  A zero-length string may be written as two consecutive
  431. quotes, thus: ""
  432.  
  433. Data Types
  434.  
  435.    Only the "char" and "int" data types are supported.  Characters
  436. (char's) are 1 byte and integers (int's) 2 bytes in length.  Both
  437. are treated as signed quantities. Chars may range in value from -128
  438. to +127. Ints range from -32767 to 32766.
  439.  
  440.    SCI also supports pointers and arrays of both char and int.
  441. Pointers require 2 bytes of storage.
  442.  
  443. Operators
  444.  
  445. Unary Operators
  446.  
  447.    The following unary operators are supported, all are evaluated from
  448. right to left:
  449.  
  450.         +--------------------------------+
  451.         | *     pointer                  |
  452.         | &     address                  |
  453.         | -     negation                 |
  454.         | !     logical NOT              |
  455.         | ~     one's complement         |
  456.         | ++    pre- and post-increment  |
  457.         | --    pre- and post-decrement  |
  458.         +--------------------------------+
  459.  
  460. Binary Operators
  461.  
  462.    The following binary operators are supported, all are evaluated
  463. from left to right.  The operators are listed from highest to lowest
  464. precedence, each group of operators (in the boxes) have the same
  465. precedence:
  466.  
  467.         +--------------------------------+
  468.         | *     multiplication           |
  469.         | /     division                 |
  470.         | %     modulo (remainder)       |
  471.         +--------------------------------+
  472.         | +     addition                 |
  473.         | -     subtraction              |
  474.         +--------------------------------+
  475.         | <<    shift left               |
  476.         | >>    shift right              |
  477.         +--------------------------------+
  478.         | <     less than                |
  479.         | >     greater than             |
  480.         | <=    less than or equal to    |
  481.         | >=    greater than or equal to |
  482.         +--------------------------------+
  483.         | ==    equal                    |
  484.         | !=    not equal                |
  485.         +--------------------------------+
  486.         | &     bitwise AND              |
  487.         +--------------------------------+
  488.         | ^     bitwise exclusive OR     |
  489.         +--------------------------------+
  490.         | |     bitwise OR               |
  491.         +--------------------------------+
  492.         | &&    logical AND              |
  493.         +--------------------------------+
  494.         | ||    logical OR               |
  495.         +--------------------------------+
  496.  
  497. Assignment Operator
  498.  
  499.    The assignment operator, "=", is evaluated from from right to left
  500. and has lowest precedence of all operators.
  501.  
  502. Comma Operator
  503.  
  504.    The Comma Operator (,) as used by SCI is not an operator in the
  505. strict sense, but rather as a function argument and variable seperator.
  506. Trying to use a comma anywhere other than in function calls, or data
  507. declarations will cause a "syntax error".
  508.  
  509.  
  510. SCOPE OF VARIABLES
  511.  
  512.    When we talk about the "scope" of a variable, we are referring to
  513.  
  514.  
  515.  
  516. MY FIRST PROGRAM
  517.  
  518.    To get you started writing "C" programs, step right up to your
  519. computer (don't be shy), fire 'er up and insert the disk.  Then type
  520. "SCI" and wait for the shell program to load (don't get too anxious
  521. now!).  When the shell prompts you with "> " boldly type the word:
  522.  
  523.         > edit
  524.  
  525. and hit <RETURN>.  You are now in the editor.  It's time to refer
  526. back to "THE EDITOR" section of this manual.  Since there is nothing
  527. in the program buffer, the editor displays:
  528.  
  529.         1:<EOF>
  530.  
  531.    Hold down that shift key and poke the key with the letter 'I' on
  532. it.  You are now in LINE INSERT mode. Anything you type from here on
  533. will be inserted into the program buffer until you press <ESCAPE>.
  534. So, why not just type in the little program below.  Don't type in the
  535. word "new:" each time, silly - they are the editor's LINE INSERT
  536. prompts.
  537.  
  538.         new:hi()
  539.         new:{
  540.         new:   puts( "hello, world\n" );
  541.         new:}
  542.  
  543.    When you're done, hit <ESCAPE> to end LINE INSERT mode, and then
  544. <ESCAPE> again to leave the editor. When the shell gives you its
  545. prompt, type:
  546.  
  547.         > hi
  548.  
  549.    The shell will hand the input line off to the interpreter, the
  550. interpreter will scan the program buffer for a function named "hi"
  551. and call that function.  The "hi" function will then call the library
  552. function "puts" to print the string "hello, world\n" on the console.
  553. What you see is simply:
  554.  
  555.         hello, world
  556.         >
  557.  
  558.    Congratulations, even if six months ago you couldn't spell
  559. "programmur", you now are one!
  560.  
  561.  
  562. THE LIBRARY FUNCTIONS
  563.  
  564.    The "sys" keyword has been reserved by SCI as a means of
  565. communicating between a user's program and the interpreter.  It is
  566. treated exactly as a (pre-defined) function that may be referenced by
  567. a user program.  This interface has been provided mainly as a
  568. convenience to the user.
  569.  
  570.    A "sys" call may have several arguments, the number and type of
  571. which depends upon the integer value of the last (right-most)
  572. argument. This righ-most argument is called the "sys number" and
  573. defines the actual function performed by a "sys" call.  To make life
  574. easier and to provide a more or less "standard" programming
  575. environment, higher-level functions have been wrapped around these
  576. "sys" calls - these are known as the "Library Functions" and may be
  577. found in the standard command shell found on the distribution disk.
  578. These "sys numbers" and their corresponding Library Functions are
  579. listed below.
  580.  
  581.         1               fputc
  582.         2               fgetc
  583.         3               fputs
  584.         4               fgets
  585.         5               sprintf
  586.         6               sscanf
  587.         7               fopen
  588.         8               fread
  589.         9               fwrite
  590.        10               fclose
  591.        11               exit
  592.        12               stmt
  593.        13               totok
  594.        14               untok
  595.        15               edit
  596.        16               strcmp and strncmp
  597.        17               memleft
  598.        18               malloc
  599.        19               free
  600.        20               load
  601.        21               save
  602.        22               list
  603.        23               trace
  604.  
  605.    The Library functions are described below in more detail:
  606.  
  607.  
  608. atoi( string )
  609. char *string;
  610.  
  611.    Converts the string "str" containing the ASCII representation of
  612.    a decimal number to an integer. The string consits of optional
  613.    leading spaces or tabs, an optional plus or minus sign (+ or -)
  614.    followed by one or more decimal digits.
  615.    Returns the integer value of the ASCII number string.
  616.  
  617. edit( line_num, program )
  618. int line_num
  619. char *program
  620.  
  621.    Envokes the built-in program editor, starting at the line given
  622.    by "line_num" on the tokenized program buffer "program".  Returns
  623.    the new size of the program buffer after the editing session.
  624.  
  625. exit( value )
  626. int value
  627.  
  628.    Causes the currently executing program to return control to the
  629.    operating system.
  630.  
  631. fclose( channel )
  632. int channel;
  633.  
  634.    Closes a disk file previously opened by the Library Function
  635.    "fopen".  Returns zero if successful, -1 on error.
  636.  
  637. fgetc( channel )
  638.  
  639.    Reads a single character from the given channel.  Returns the
  640.    character read, or a -1 on error or end of file.
  641.  
  642. fgets( buffer, length, channel )
  643. char *buffer;
  644. int length;
  645. int channel;
  646.  
  647.    Reads characters from the file associated with "channel".
  648.    Characters are read from the file until either a newline is
  649.    encountered, length minus 1 characters have been read, or an end
  650.    of file is found.  Returns the address of "buffer", or a zero on
  651.    end of file.
  652.  
  653. fopen( file_name, mode )
  654. char *file_name, *mode;
  655.  
  656.    Opens a disk file whose name is given by the string "file_name".
  657.    The string "mode" determines how the file will be opened:
  658.  
  659.       "r"  - open for ASCII read.  The file MUST exist or fopen
  660.              returns an error code.
  661.       "rw" - open for ASCII read/write.  The file MUST exist.
  662.       "w"  - open for ASCII write.  If the file exists, it is first
  663.              deleted.
  664.       "wr" - open for ASCII read/write. If the file already exists,
  665.              it is first deleted.
  666.       "a"  - open for ASCII write append.  If the file does not exist,
  667.              it is created. If it does exist, the file pointer is
  668.              positioned to the end of the file.
  669.       "ar" - open for ASCII read/write append.  If the file does not
  670.              exist it is created. If it DOES exist, it is opened and
  671.              the file pointer positioned to the end of the file.  The
  672.              file may then be read or written.
  673.  
  674.    By appending a "b" to any of the modes above, the file is opened in
  675.    BINARY mode instead of ASCII.
  676.  
  677.    When a file is opened in ASCII mode, all <LINEFEED>s ("\n") are
  678.    converted to <RETURN> <LINEFEED> character pairs when writing to
  679.    the file.  Similarly, <RETURN> <LINEFEED> pairs are converted to
  680.    a single <LINEFEED> character on input from the file.
  681.  
  682.    In binary read/write mode, no such conversions are performed.
  683.  
  684.    Returns an integer value known as a "channel". The special
  685.    channels 0, 1 and 2 refer to the standard input, standard output
  686.    and standard error files.  The channel number must be used in all
  687.    Library calls that perform file I/O functions.  A Minus one is
  688.    returned if the file could not be opened.
  689.  
  690. fputc( c, channel )
  691. char c;
  692. int channel;
  693.  
  694.    Writes the character "c" to the specified file channel.  Returns
  695.    the character written, or a -1 on error.
  696.  
  697. fputs( string, channel )
  698. char *string;
  699. int channel;
  700.  
  701.    Writes the string to the specified file channel.  Returns zero, or
  702.    a -1 on error.
  703.  
  704. fread( buffer, length, channel )
  705. int channel;
  706. char *buffer;
  707. int length;
  708.  
  709.    Reads a maximum of "length" bytes into memory at the address
  710.    pointed to by "buffer" from the open file, "channel".  If the file
  711.    was opened in ASCII read or read/write mode, "fread" only reads up
  712.    to and including a newline character. If the file was opened in
  713.    binary read mode, "length" number of characters are read if they
  714.    are available.  Note that "channel" must be the value returned
  715.    from a previous "fopen" Library Function call, and the file must
  716.    still be open.  Returns the number of characters that were
  717.    actually read, or 0 on EOF.
  718.  
  719. free( memory )
  720. char *memory
  721.  
  722.    Returns the block of memory pointed to by "memory" that was
  723.    previously gotten from a "malloc" Library Function call.
  724.  
  725. fwrite( channel, buffer, length )
  726. int channel;
  727. char *buffer;
  728. int length;
  729.  
  730.    Writes "length" number of characters from memory at the address
  731.    pointed to by "buffer" to the file associated with "channel".
  732.    Note that "channel" must be the value returned from a previous
  733.    "fopen" Library Function call, and the file must still be open.
  734.    Returns the number of characters actually written if successful,
  735.    or a -1 on error.
  736.  
  737. load( file, program )
  738. char *file, *program
  739.  
  740.    Loads a program from the file whose name is in the string "file"
  741.    tokenizes the statements therein and places the tokenized program
  742.    at "program".  Returns the size of the resulting tokenized
  743.    program.  If the file could not be found, an error message is
  744.    printed on the console.
  745.  
  746. memleft()
  747.  
  748.    Returns a count of the number of free memory bytes available from
  749.    the main memory allocator.
  750.  
  751. malloc( size )
  752. int size
  753.  
  754.    Main memory allocator. This function is responsible for handing
  755.    out (to programs) chunks of free memory to be used by the program
  756.    as it sees fit. WARNING: wreckless programs that write data beyond
  757.    the size of the allocated chunk of memory will cause the allocator
  758.    to loose its mind and dole out "rotten" chunks of memory.  This
  759.    will almost surely cause SCI to crash and burn.  Returns a pointer
  760.    to a block of memory "size" bytes long.  Be sure to de-allocate
  761.    the block using the Library Function "free" when you're done with
  762.    it, or it will be lost forever...
  763.  
  764. printf( format, arg1, arg2, ... arg8 )
  765. char *format;
  766. int arg1, arg2, ... arg8;
  767.  
  768.    Prints a formatted string to the standard output file.
  769.    The characters in the string "format" are copied to the output
  770.    file. If a percent character ("%") is encountered in the
  771.    format string, it is assumed to be a conversion specification.
  772.    Each conversion specification converts exactly one argument in
  773.    the list "arg1", "arg2", ... "arg8". Once an argument has been
  774.    converted and output, it is skipped over the next time a
  775.    conversion specification is encountered. Therefore, you need just
  776.    as many arguments as you have conversion specifications.
  777.  
  778.    The character(s) that follow a conversion specification may be
  779.    one or more of the following:
  780.  
  781.       minus sign (-)
  782.          Indicates that output should be left justified instead of
  783.          right justified.
  784.  
  785.       zero fill character (0)
  786.          The field will be padded on the left with zeros.
  787.  
  788.       field width (number other than 0)
  789.          This is the minimum # of characters output. The field will
  790.          be padded with zeros or blanks depending on the fill
  791.          character (above).
  792.  
  793.       precision (. followed by a number)
  794.          For numeric fields, this is the # of decimal places to the
  795.          right of the decimal point. For string fields, at most that
  796.          many characters of the string will be output.
  797.  
  798.       conversion code (the letter d, u, x, o, b, s or c)
  799.          A conversion code indicates the type of conversion that is
  800.          to be done on the argument as follows:
  801.  
  802.             d - convert to a decimal (base 10) number
  803.             u - unsigned decimal number
  804.             x - hexadecimal (base 16) number
  805.             o - octal (base 8) number
  806.             b - binary (base 2) number
  807.             s - string constant
  808.             c - single ASCII character
  809.  
  810. save( file, program )
  811. char *file, *program
  812.  
  813.    Saves a program in its tokenized form at "program" to the file
  814.    whose name is in the string "file".  Returns the number of bytes
  815.    written to the file.  If the file could not be created, an error
  816.    message is printed on the console.
  817.  
  818. scanf( format, arg1, arg2, ... arg8 )
  819. char *format
  820. int arg1, arg2, ... arg8
  821.  
  822.    Read characters from the standard input file and converts them
  823.    using the conversion string "format" (same as for Library Function
  824.    "printf").  The arguments "arg1", "arg2", ... "arg8" must be
  825.    POINTERS to the appropriate data types.
  826.  
  827. sprintf( buffer, format, arg1, arg2, ... arg8 )
  828. char *buffer, *format
  829. int arg1, arg2, ... arg8
  830.  
  831.    Performs formated conversion of the arguments, exactly like the
  832.    Library Function "printf", but writes its output to the "buffer"
  833.    instead of the standard output file.
  834.  
  835. sscanf( buffer, format, arg1, arg2, ... arg8 )
  836. char *buffer, *format
  837. int arg1, arg2, ... arg8
  838.  
  839.    Performs formated input conversion of the arguments, exactly like
  840.    the Library Function "scanf" but reads its input from the "buffer"
  841.    instead of the standard input file.
  842.  
  843. stmt( line, program )
  844. char *line, *program
  845.  
  846.    Hands a "Small C" statement off to the interpreter for execution.
  847.    The statement is in its untokenized form at "line".  The program
  848.    in its tokenized form at "program" is used by the interpreter to
  849.    satisfy any global variable or function references made by the
  850.    statement.
  851.  
  852. strcmp( string_1, string_2 )
  853. char *string_1, *string_2 
  854.  
  855.    or
  856.  
  857. strncmp( string_1, string_2, limit )
  858. char *string_1, *string_2 
  859. int limit
  860.  
  861.    Compares (character-by-character) the two strings. The comparison
  862.    stops when a zero is encountered in either of the two strings.
  863.    "strncmp" does the same thing, but at most "limit" number of bytes
  864.    are compared.  Returns a 0 if the two strings are identical,
  865.    non-zero if they differ.
  866.  
  867. totok( sourcebuf, tokenbuf )
  868. char *sourcebuf, *tokenbuf
  869.  
  870.    Converts the C statement at "sourcebuf" to its tokenized
  871.    equivalent and places the result at "tokenbuf".  Returns the
  872.    length of "tokenbug".
  873.  
  874. trace( on_off )
  875. int on_off;
  876.  
  877.    Allows a program to enable or disable SCI's trace feature.  If
  878.    "on_off" is zero, trace is disabled, anything else enables it.
  879.  
  880. untok( tokenbuf, sourcebuf )
  881. char *tokenbuf, *sourcebuf
  882.  
  883.    Converts the tokenized statement at "tokenbuf" to its printable
  884.    form at "sourcebuf".  Returns the length of "tokenbuf" (not
  885.    "sourcebuf"!).  This function is the opposite of the "totok"
  886.    function.
  887.  
  888.  
  889. TRACE/DEBUG FEATURE
  890.  
  891.    The Library Function "trace", described in the previous section,
  892. allows you to enable or disable the program trace/debug feature.
  893. In the trace mode, you can set and remove breakpoints, examine and
  894. modify program variables and control the execution of your program.
  895.  
  896.   When trace is enabled, the interpreter first displays each line of
  897. the program on the console before it is executed, and then displays
  898. a question mark (?) prompt.  At this point, you may either enter
  899. another "Small C" statement (to display the contents of a variable
  900. for example), or enter one of the following commands (NOTE: all of
  901. these commands must start with a dot (.) immediately following the
  902. "?" prompt to distinguish them from a valid "Small C" statement):
  903.                                                                      
  904.    .b#  sets a breakpoint at a given line in your program.  The "#"
  905.         symbol represents the line number at which the program will
  906.         be halted.
  907.  
  908.    .B   displays all breakpoints set in the program.
  909.  
  910.    .c   continues program execution until the next breakpoint is
  911.         encountered.
  912.  
  913.    .d#  deletes the breakpoint at line number "#".  The breakpoint
  914.         must have been previously set with a ".b" command.
  915.  
  916.    .D   deletes all breakpoints.
  917.  
  918.    .e#  lets you examine the program with the program editor.  Editor
  919.         commands that would normally modify the program are disabled.
  920.  
  921.    .g   displays all of the program's global variables and their
  922.         values.  If the variable is an array, its address is printed
  923.         followed by the first 10 elements of the array.
  924.  
  925.    .G   same as ".g" but also displays the first line of every
  926.         function in the program along with its line number.  This is
  927.         useful for locating a function in a long program.
  928.  
  929.    .q   quits program execution and returns to the shell program.
  930.  
  931.    .s#  steps through the program without displaying each line as it
  932.         is executed.  The "#" is the number of lines to be executed
  933.         before control returns to the debuger.
  934.  
  935.    .t   displays the list of active functions with the current one
  936.         at the top of the list.  This is handy for finding out "how
  937.         did I get here?".
  938.  
  939.    .T   same as ".t" but also displays each function's local variables
  940.         and their values.
  941.  
  942. <RETURN>     repeats the last ".s" or ".c" command entered.  This
  943.         is a handy way of tracing program flow from one breakpoint to
  944.         the next.
  945.  
  946. <ESCAPE>     disables the trace/debug facility and continues execution
  947.         normally.  The interpreter will no longer stop at
  948.                                                                      
  949.    If the interpreter finds an error in your program, it will auto-
  950. matically enable trace/debug and halt the program at the line where
  951. the error occurred.  This can be a little confusing if you are already
  952. in the debuger and you happen to make a mistake while typing a
  953. statement for the debuger to execute, because the resulting error
  954. message and the "?" prompt will refer to the statement you just
  955. entered.  If this happens, just hit a <RETURN> and you will be
  956. returned to the program trace session.
  957.  
  958.  
  959. PASSING ARGUMENTS TO THE SHELL
  960.  
  961.    A mechanism has been provided to pass operating system command
  962. line areguments to the startup program in a way similar to most
  963. commercially available "C" compilers.  When you start up SCI, a "-A"
  964. will cause all following arguments to be passed to the startup
  965. program.  For example, suppose the following program is to be
  966. executed by SCI at startup:
  967.  
  968.         main(argc, arv)
  969.         int argc, *argv;
  970.         {
  971.            int i;
  972.  
  973.            while ( i<argc )
  974.               printf("<%s>\n",argv[i++]);
  975.         }
  976.  
  977. Then, when the following command is entered at the operating system
  978. level:
  979.  
  980.         A>SCI -A HELLO OUT THERE
  981.  
  982. the program would print:
  983.  
  984.         <HELLO>
  985.         <OUT>
  986.         <THERE>
  987.  
  988. Note that since SCI does not support pointers to pointers, the "argv"
  989. variable must be declared as a pointer to "int".  Then, each "argv[i]"
  990. is a pointer to a string that points to one of the words passed on
  991. the command line.
  992.  
  993.  
  994. DIFFERENCES FROM "SMALL C"
  995.  
  996.    SCI supports the subset of the "C" language defined by J. Hendrix'
  997. public domain "Small C" compiler, available for many CP/M and MS-DOS
  998. machines. There are, however, some minor differences that have been
  999. dictated in part by the fact that this is an interpreter. These are:
  1000.  
  1001.    0) Program source lines are limited to 80 characters, just to keep
  1002.       things simple.  The editor will complain if you try to create
  1003.       a line longer than 80 characters.
  1004.    1) There are no #define or #include statements, simply because
  1005.       there is no pre-processor!
  1006.    2) Comments are introduced by the number symbol (#) and are
  1007.       terminated by the end of the line.  The "/*" and "*/"
  1008.       combinations will only be barfed at by SCI.
  1009.    3) Statements in general are terminated by the end of a line - the
  1010.       semicolon required by the "C" language at the end of a statement
  1011.       is superfluous. This restriction imposes that an expression be
  1012.       completely on one line (boooo, hissss!), however you no longer
  1013.       get penalized for forgeting a semicolon (hurray, yay!).  This
  1014.       also implies that string and character constants are terminated
  1015.       by the end of the line.  In fact, if you forget to add the
  1016.       terminating quote or apostrophe delimiter, SCI will append one
  1017.       for you.
  1018.    4) The comma operator is recognized only when used in function
  1019.       calls and data declarations.  For instance, it would be illegal
  1020.       to say:
  1021.          while ( argv++, argc-- );
  1022.    5) Data declarations may appear ANYWHERE within a program or
  1023.       function (hurray, yay!), however local data declarations remain
  1024.       active up to the end of the function - they don't go away at
  1025.       the end of the compound statement they were declared in (boooo,
  1026.       hissss!).
  1027.    6) Pointer arithmetic is not smart enough to recognize data types
  1028.       so that pointers to integers are advanced to the next BYTE, not
  1029.       the next integer.  For example, *(ip+1) will point to the least
  1030.       significant byte of the next integer (on an 8086 machine), not
  1031.       the next word (booo, hisss!). But, ip[1] and ++ip still point
  1032.       to the next integer (whew!).
  1033.  
  1034.  
  1035. ERROR MESSAGES
  1036.  
  1037.    When SCI detects an error in your program, it will stop execution,
  1038. display an error message and then the line number and program text of
  1039. the offending line.  The possible error messages are listed below:
  1040.  
  1041. no entry point
  1042.    You forgot to specify an entry function with the "entry" keyword.
  1043.  
  1044. not enough table space
  1045.    There was insufficient free memory available for the program,
  1046.    variable, function and stack segments.  Specify a smaller
  1047.    -P, -V, -F or -S value.
  1048.  
  1049. can't find '<file>'
  1050.    The specified program file could not be loaded, or SHELL.SCI
  1051.    does not exist on the default disk drive.
  1052.  
  1053. can't create '<file>'
  1054.    The specified file could not be save on disk because of an
  1055.    operating system error.
  1056.  
  1057. missing '}'
  1058.    The interpreter wandered off the edge of your program because
  1059.    you forgot a closing brace.
  1060.  
  1061. missing operator
  1062.    The interpreter found two consecutive operands as for example
  1063.    the statement:  var  3;
  1064.  
  1065. missing ')' or ']'
  1066.    Mismatched left and right parentheses or brackets were detected.
  1067.  
  1068. not a pointer
  1069.    A variable or constant expression was used as an array or
  1070.    pointer as for example the statement:  *(buf+1);
  1071.  
  1072. syntax error
  1073.    An error was detected in the structure of a statement.
  1074.  
  1075. lexical error: ... <stmt>
  1076.    An illegal character was found in a statement.  The offending
  1077.    character along with the remainder of the statement is printed.
  1078.  
  1079. need an lvalue
  1080.    An attempt was made to assign a value to a constant.
  1081.  
  1082. stack underflow
  1083.    Something is wrong!
  1084.  
  1085. stack overflow
  1086.    The expression is too complex or function calls are nested too
  1087.    deeply.  Try specifying a larger -S value when starting up SCI.
  1088.  
  1089. too many functions
  1090.    The interpreter ran out of Function Table space.  Try specifying
  1091.    a larger -F value when starting up SCI.
  1092.  
  1093. too many symbols
  1094.    The interpreter ran out of Variabl Table space.  Try specifying
  1095.    a larger -V value when starting up SCI.
  1096.  
  1097. out of memory
  1098.    The interpreter ran out of Program space.  Try specifying a
  1099.    larger -P value when starting up SCI.
  1100.  
  1101. stmt outside of function
  1102.    There is something wrong with the structure of your program.
  1103.    Typically this results from a statement appearing outside of
  1104.    any function body.
  1105.  
  1106. missing '{'
  1107.    The left brace that introduces a function body was missing.
  1108.  
  1109. wrong # of arguments in sys call
  1110.    The number of arguments passed to a "sys" call is incorrect for
  1111.    the specified "sys" number.
  1112.  
  1113. undefined symbol
  1114.    A variable has been used without ever having been declared.
  1115.  
  1116. interrupt
  1117.    The program was interrupted by pressing the <ESCAPE> key.
  1118.