home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / refman.doc < prev    next >
Text File  |  1994-03-10  |  79KB  |  2,008 lines

  1.  
  2.         Euphoria Programming Language 
  3.             version 1.2
  4.               Reference Manual
  5.  
  6.  
  7.         (c) 1994 Rapid Deployment Software
  8.     
  9.         Permission is freely granted to anyone
  10.         to copy this manual.
  11.  
  12.  
  13.  
  14.  
  15.             TABLE OF CONTENTS
  16.             =================
  17.  
  18.     1. Introduction 
  19.       
  20.        1.1  Example Program                  
  21.        1.2  Installation             
  22.        1.3  How To Run A Program    
  23.        1.4  How To Edit A Program   
  24.        1.5  How To Distribute A Program
  25.     
  26.     2. Core Language        
  27.      
  28.        2.1  Objects                 
  29.        2.2  Expressions     
  30.        2.3  Declarations 
  31.        2.4  Statements      
  32.        2.5  Top-Level Commands 
  33.     
  34.     3. Debugging 
  35.     
  36.     4. Built-in Routines       
  37.       
  38.        4.1  Predefined Types         
  39.        4.2  Sequence Manipulation   
  40.        4.3  Searching and Sorting  
  41.        4.4  Math            
  42.        4.5  File and Device I/O 
  43.        4.6  Mouse Support   
  44.        4.7  Operating System 
  45.        4.8  Debugging       
  46.        4.9  Graphics & Sound 
  47.        4.10 Machine Level Interface 
  48.  
  49.  
  50.  
  51.     
  52.             1. Introduction
  53.             ===============
  54.         
  55.  Euphoria is a new programming language with the following advantages over 
  56.  conventional languages:
  57.  
  58.  o      a remarkably simple, flexible, powerful language
  59.     definition that is extremely easy to learn and use.  
  60.  
  61.  o      dynamic storage allocation. Variables grow or shrink
  62.     without the programmer having to worry about allocating
  63.     and freeing chunks of memory.  Objects of any size can be 
  64.     assigned to an element of a Euphoria sequence (array).
  65.  
  66.  o      lightning-fast pre-compilation. Your program is checked
  67.     for syntax and converted into an efficient internal form at
  68.     over 12,000 lines per second on a 486-50. 
  69.  
  70.  o      a high-performance, state-of-the-art interpreter that is
  71.     10 to 20 times faster than conventional interpreters such as
  72.     Microsoft QBasic.  
  73.  
  74.  o      extensive run-time checking for: out-of-bounds subscripts,
  75.     uninitialized variables, bad parameter values for built-in
  76.     functions, illegal value assigned to a variable and many 
  77.     more.  There are no mysterious machine exceptions -- you 
  78.     will always get a full English description of any problem 
  79.     that occurs with your program at run-time, along with a 
  80.     call-stack trace-back and a dump of all of your variable
  81.     values.  Programs can be debugged quickly, easily and
  82.     more thoroughly.
  83.  
  84.  o      features of the underlying hardware are completely
  85.     hidden. Programs are not aware of word-lengths,
  86.     underlying bit-level representation of values, byte-order
  87.     etc. Euphoria programs are therefore highly portable from
  88.     one machine to another.
  89.  
  90.  o      a full-screen source debugger and an execution profiler
  91.     are included, along with a full-screen editor. On a color 
  92.     monitor, the editor displays Euphoria programs in 
  93.     multiple colors, to highlight comments, reserved words, 
  94.     built-in functions, strings, and level of nesting of brackets. 
  95.     It optionally performs auto-completion of statements, 
  96.     saving you typing effort and reducing syntax errors. This 
  97.     editor is written in Euphoria, and the source code is 
  98.     provided to you without restrictions. You are free to
  99.     modify it, add features, and redistribute it as you wish. 
  100.  
  101.  o      Euphoria programs run under MS-DOS (or Windows), but are not
  102.     subject to any 64K or 640K memory limitations. You can
  103.     create programs that use the full multi-megabyte memory
  104.     of your computer. You can even set up a swap file for
  105.     programs that need more memory than exists on your machine. 
  106.     A least-recently-used paging algorithm will automatically 
  107.     shuffle pages of virtual memory in and out as your program 
  108.     executes.
  109.  
  110.  o      Euphoria routines are naturally generic. The example
  111.     program below shows a single routine that will sort any
  112.     type of data -- integers, floating-point numbers, strings
  113.     etc. Euphoria is not an "Object-Oriented" language in the 
  114.     usual sense, yet it achieves many of the benefits of these
  115.     languages in a much simpler way. 
  116.  
  117.  
  118.  1.1 Example Program 
  119.  -------------------
  120.  The following is an example of a complete Euphoria program.
  121.  
  122.  
  123. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  124.  
  125.  sequence list, sorted_list
  126.  
  127.  function merge_sort(sequence x)
  128.  -- put x into ascending order using a recursive merge sort
  129.      integer n, mid
  130.      sequence merged, x1, x2
  131.  
  132.      n = length(x)
  133.      if n = 0 or n = 1 then
  134.      return x  -- trivial case
  135.      end if
  136.  
  137.      mid = floor(n/2)
  138.      x1 = merge_sort(x[1..mid])       -- sort first half of x 
  139.      x2 = merge_sort(x[mid+1..n])     -- sort second half of x
  140.  
  141.      -- merge the two sorted halves into one
  142.      merged = {}
  143.      while length(x1) > 0 and length(x2) > 0 do
  144.      if compare(x1[1], x2[1]) < 0 then
  145.          merged = append(merged, x1[1])
  146.          x1 = x1[2..length(x1)]
  147.      else
  148.          merged = append(merged, x2[1])
  149.          x2 = x2[2..length(x2)]
  150.      end if
  151.      end while
  152.      return merged & x1 & x2  -- merged data plus leftovers
  153.  end function
  154.  
  155.  procedure print_sorted_list()
  156.  -- generate sorted_list from list 
  157.      list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
  158.      sorted_list = merge_sort(list)
  159.      ? sorted_list   
  160.  end procedure
  161.  
  162.  print_sorted_list()     -- this command starts the program 
  163.  
  164.  
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166.  
  167.  
  168.  The above example contains 4 separate commands that are processed in order.
  169.  The first declares two variables: list and sorted_list to be sequences. 
  170.  The second defines a function merge_sort(). The third defines a procedure 
  171.  print_sorted_list(). The final command calls procedure print_sorted_list().
  172.  
  173.  The output from the program will be:
  174.   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. 
  175.  
  176.  merge_sort() will just as easily sort {1.5, -9, 1e6, 100} or
  177.  {"oranges", "apples", "bananas"} .
  178.  
  179.  This example is stored as euphoria\demo\example.ex. This is not the fastest 
  180.  way to sort in Euphoria. Go into the euphoria\demo directory and type 
  181.  "ex allsorts" to see timings on several different sorting algorithms for 
  182.  increasing numbers of objects. For a quick tutorial on Euphoria programming
  183.  see euphoria\demo\bench\filesort.ex.
  184.  
  185.  
  186.  1.2 Installation 
  187.  ----------------
  188.  To install Euphoria on your machine, first read the file install.doc. 
  189.  Installation simply involves copying the euphoria files to your hard disk 
  190.  under a directory named "EUPHORIA", and then modifying your autoexec.bat file
  191.  so that EUPHORIA\BIN is on your search path, and the environment variable 
  192.  EUDIR is set to the EUPHORIA directory. An automatic install program, 
  193.  "install.bat" is provided for this purpose. For the latest details, please 
  194.  read the instructions in install.doc before you run install.bat.
  195.  
  196.  When installed, the euphoria directory will look something like this: 
  197.  
  198.     euphoria 
  199.         readme.doc
  200.         \bin        
  201.             ex.exe, dos4gw.exe, ed.bat, other utilities
  202.         \include
  203.             standard include files, e.g. graphics.e
  204.         \doc
  205.             ed.doc, refman.doc etc.
  206.         \demo   
  207.             demo programs, e.g. ttt.ex, mset.ex, plot3d.ex
  208.             \learn
  209.                test your knowledge of Euphoria
  210.             \langwar
  211.                language war game, lw.ex
  212.             \bench  
  213.                benchmark programs
  214.            
  215.  
  216.  1.3 How to Run a Program
  217.  ------------------------
  218.  Euphoria programs are executed by typing "ex", followed by the name of the 
  219.  main (or only) file. By convention, main Euphoria files have an extension of 
  220.  ".ex".  Other Euphoria files, that are meant to be included in a larger
  221.  program, end in ".e". To save typing, you can leave off the ".ex", and 
  222.  the ex command will supply it for you automatically. If the file can't be 
  223.  found in the current directory, your PATH will be searched. There are no 
  224.  command-line options for ex itself, but your program can call the built-in 
  225.  function command_line() to read the ex command-line. You can redirect 
  226.  standard input and standard output when you run a Euphoria program, 
  227.  for example:
  228.  
  229.     ex filesort.ex < raw > sorted
  230.  
  231.        or simply,
  232.  
  233.     ex filesort < raw > sorted
  234.  
  235.  For frequently-used programs you might want to make a small .bat file 
  236.  containing something like:
  237.      
  238.       @echo off
  239.       ex myprog.ex %1 %2
  240.  
  241.  where myprog.ex expects two command-line arguments. This will save you 
  242.  from typing ex all the time. 
  243.  
  244.  ex.exe is in the euphoria\bin directory which must be on your search path. 
  245.  The file dos4gw.exe must also be present in the bin directory. Some 
  246.  Euphoria programs expect the environment variable EUDIR to be set to the 
  247.  main Euphoria directory.
  248.  
  249.  Running Under Windows
  250.  ---------------------
  251.  You can run Euphoria programs directly from the Windows environment, or from 
  252.  a DOS shell that you have opened from Windows. By "associating" .ex files
  253.  with ex.exe, you can simply double-click on a .ex file to run it. It
  254.  is possible to have several Euphoria programs active in different windows.
  255.  You can resize these windows, move them around, change to a different font, 
  256.  run things in the background, copy and paste between windows etc. See your 
  257.  Windows manual. The Euphoria editor is available. You might want to 
  258.  associate .e, .pro and other text files with ed.bat. Also, the File-menu/
  259.  Run-command will let you type in ex or ed command lines. 
  260.  
  261.  Use of a swap file
  262.  ------------------
  263.  If your program requires more memory than is physically present on your 
  264.  machine, you can easily set up a swap file to provide extra "virtual" 
  265.  memory under DOS. All you have to do is define the environment variable 
  266.  DOS4GVM. The MS-DOS command:  set DOS4GVM=1  will allow a 16 megabyte swap 
  267.  file to be created, which will be used as virtual memory. Portions of your 
  268.  program and data that have not been recently used will be copied out to this 
  269.  file to create room in physical memory for actively-used information. If you
  270.  can't afford the default 16 megabytes of disk space, you could: 
  271.  set DOS4GVM=virtualsize#8192  to get an 8 megabyte swap file, or specify a 
  272.  smaller number if this is still too much. To turn off virtual memory, 
  273.  you can:  set DOS4GVM=  after your program has finished executing. The swap 
  274.  file can be deleted after execution, but leaving it in place will let your 
  275.  next program start up faster.  
  276.  
  277.  When you run under Windows, virtual memory swapping will be performed 
  278.  by Windows itself, and you may actually get more memory than DOS (without
  279.  swap file) would provide. 
  280.  
  281.  
  282.  1.4 How to Edit a Program 
  283.  -------------------------
  284.  
  285.  You can use any text editor to edit a Euphoria program. However, Euphoria 
  286.  comes with its own special editor which is written entirely in Euphoria. 
  287.  Type ed followed by the complete name of the file you wish to edit (the 
  288.  .ex extension is not assumed). You can use this editor to edit any kind of 
  289.  text file. When you edit a .e or .ex file some extra features, such as color 
  290.  syntax highlighting and auto-completion of certain statements, are available 
  291.  to make your job easier.
  292.  
  293.  Whenever you run a Euphoria program and get an error message, during 
  294.  compilation or execution, you can simply type ed with no file name and you 
  295.  will be automatically positioned in the file containing the error, at 
  296.  the correct line and column, and with the error message displayed at the 
  297.  top of the screen. 
  298.  
  299.  Under Windows you can associate ed.bat with various kinds of text files 
  300.  that you want to edit.
  301.  
  302.  Most keys that you type are inserted into the file at the cursor position. 
  303.  Hit the Esc key once to get a menu bar of special commands. The arrow keys, 
  304.  and the Insert/Delete/Home/End/PageUp/PageDown keys are also active. See 
  305.  the file euphoria\doc\ed.doc for a complete description of the editing 
  306.  commands. Esc h (help) will let you view ed.doc from your editing session.
  307.  
  308.  If you need to understand or modify any detail of the editor's operation, 
  309.  you can edit the file ed.ex in euphoria\bin (be sure to make a backup 
  310.  copy so you don't lose your ability to edit).  If the name ed conflicts 
  311.  with some other command on your system, simply rename the file 
  312.  euphoria\bin\ed.bat to something else.  Because this editor is written 
  313.  in Euphoria, it is remarkably concise and easy to understand. The same 
  314.  functionality implemented in a language like C, would take far more 
  315.  lines of code.
  316.  
  317.  
  318.  1.5 How To Distribute A Program
  319.  -------------------------------
  320.  Your customer needs to have the 2 files: ex.exe and dos4gw.exe somewhere 
  321.  on the search path. You are free to supply anyone with the Public Domain
  322.  Edition of ex.exe, as well as dos4gw.exe to support it.
  323.  
  324.  Your program can be distributed in source form or in shrouded form. In source
  325.  form you supply your Euphoria files plus any standard include files that are
  326.  required. To deliver a program in shrouded form, you run the Euphoria source 
  327.  code shrouder, bin\shroud.ex, against your main Euphoria file. The shrouder 
  328.  pulls together all included files into a single compact file that is 
  329.  virtually unreadable. You then ship this one file plus a copy of ex.exe and 
  330.  dos4gw.exe. One copy of ex.exe and dos4gw.exe on a machine is sufficient to 
  331.  run any number of Euphoria programs. Comments in bin\shroud.ex tell you how 
  332.  to run it, and what it does to obscure or "shroud" your source. 
  333.  
  334.  
  335.             2. The Core Language
  336.             ====================
  337.  
  338.  2.1 Objects
  339.  -----------
  340.  
  341.  All data objects in Euphoria are either atoms or sequences.  An atom is a 
  342.  single numeric value. A sequence is an ordered list of data objects.  
  343.  The objects contained in a sequence can be an arbitrary mix of atoms or 
  344.  sequences. A sequence is represented by a list of objects in brace brackets,
  345.  separated by commas. Atoms can have any double-precision floating point value.
  346.  This is approximately -1e300 to +1e300 with 15 decimal digits of accuracy.
  347.  Here are some Euphoria objects:
  348.  
  349.     -- examples of atoms:
  350.     0
  351.     1000
  352.     98.6
  353.     -1e6
  354.  
  355.     -- examples of sequences:
  356.     {2, 3, 5, 7, 11, 13, 17, 19}        -- 8-element sequence
  357.     {1, 2, {3, 3, 3}, 4, {5, {6}}}      -- 5-element sequence
  358.     {{"jon", "smith"}, 52389, 97.25}    -- 3-element sequence
  359.     {}                                  -- 0-element sequence
  360.  
  361.  Numbers can also be entered in hexadecimal. For example:
  362.     #FE             -- 254
  363.     #A000           -- 40960
  364.     #FFFF00008      -- 68718428168
  365.     -#10            -- -16
  366.  
  367.  Sequences can be nested to any depth.  Brace brackets are used to construct 
  368.  sequences out of a list of expressions.  These expressions are evaluated at 
  369.  run-time. e.g.
  370.  
  371.     {x+6, 9, y*w+2, sin(0.5)}
  372.  
  373.  
  374.  Performance Note: Although atoms can have any double-precision value,
  375.  integer atoms are generally stored and manipulated as machine integers
  376.  to save time and space. 
  377.  
  378.  
  379.  Character Strings
  380.  -----------------
  381.  Character strings may be entered using quotes e.g.
  382.  
  383.     "ABCDEFG"
  384.  
  385.  Strings are just sequences of characters, and may be manipulated and 
  386.  operated upon just like any other sequences. For example the above 
  387.  string is equivalent to the sequence
  388.  
  389.     {65, 66, 67, 68, 69, 70, 71}
  390.  
  391.  which contains the corresponding ASCII codes. Individual characters may be 
  392.  entered using single quotes if it is desired that they be treated as 
  393.  individual numbers (atoms) and not length-1 sequences. e.g.
  394.  
  395.      'B'   -- equivalent to the atom 66
  396.      "B"   -- equivalent to the sequence {66}
  397.  
  398.  Note that an atom is not equivalent to a one-element sequence containing 
  399.  the same value, although there are a few built-in routines that choose 
  400.  to treat them similarly.
  401.  
  402.  Special characters may be entered using a back-slash:
  403.  
  404.     \n        newline
  405.     \r        carriage return
  406.     \t        tab
  407.     \\        backslash
  408.     \"        double quote
  409.     \'        single quote
  410.  
  411.  For example, "Hello, World!\n", or '\\'. The Euphoria editor displays 
  412.  character strings in brown. 
  413.  
  414.  Comments
  415.  --------
  416.  Comments are started by two dashes and extend to the end of the current line.
  417.  e.g.
  418.  
  419.      -- this is a comment
  420.  
  421.  Comments are ignored by the compiler and have no effect on execution speed. 
  422.  The editor displays comments in red. In this manual we use italics.
  423.  
  424.  
  425.  2.2 Expressions
  426.  ---------------
  427.  
  428.  Objects can be combined into expressions using binary and unary operators as 
  429.  well as built-in and user-defined functions. For example,
  430.  
  431.     {1,2,3} + 5
  432.  
  433.  is an expression that adds the sequence {1,2,3} and the atom 5 to get the 
  434.  resulting sequence {6,7,8}. Besides + there are many other operators. The 
  435.  precedence of operators is as follows:
  436.  
  437.     highest precedence:     function/type calls
  438.  
  439.                 unary -  not 
  440.  
  441.                 *  /
  442.  
  443.                 +  -
  444.  
  445.                 &
  446.  
  447.                 <  >  <=  >=  =  !=
  448.  
  449.      lowest precedence:     and, or
  450.  
  451.  Thus 2+6*3 means 2+(6*3), not (2+6)*3. Operators on the same line above have 
  452.  equal precedence and are evaluated left to right.
  453.  
  454.  
  455.  Relational & Logical Operators
  456.  ------------------------------
  457.  The relational operators, <, >, <=, >=, = , != each produce a 1 (true) or a 
  458.  0 (false) result. These results can be used by the logical operators 'and', 
  459.  'or', and 'not' to determine an overall truth value. e.g.
  460.  
  461.     b > 0 and b != 100 or not (c <= 5) 
  462.  
  463.  where b and c are the names of variables.  
  464.  
  465.  
  466.  Subscripting of Sequences
  467.  -------------------------
  468.  A single element of a sequence may be selected by giving the element number 
  469.  in square brackets. Element numbers start at 1. Non-integer subscripts are 
  470.  rounded down to an integer. 
  471.  
  472.  For example, if x contains {5, 7, 9, 11, 13} then x[2] is 7. Suppose we 
  473.  assign something different to x[2]:
  474.  
  475.     x[2] = {11,22,33}
  476.  
  477.  Then x becomes: {5, {11,22,33}, 9, 11, 13}. Now if we ask for x[2] we  get 
  478.  {11,22,33} and if we ask for x[2][3] we get the atom 33. If you try to 
  479.  subscript with a number that is outside of the range 1 to the number of 
  480.  elements, you will get a subscript error. For example x[0],  x[-99] or 
  481.  x[6] will cause errors. So will x[1][3] since x[1] is not a sequence. There 
  482.  is no limit to the number of subscripts that may follow a variable, but 
  483.  the variable must contain sequences that are nested deeply enough. The 
  484.  two dimensional array, common in other languages, can be easily simulated 
  485.  with a sequence of sequences:
  486.  
  487.      { {5, 6, 7, 8, 9},
  488.        {1, 2, 3, 4, 5},
  489.        {0, 1, 0, 1, 0} }
  490.  
  491.  An expression of the form x[i][j] can be used to access any element. The two 
  492.  dimensions are not symmetric however, since an entire "row" can be selected 
  493.  with x[i], but there is no simple expression to select an entire column. 
  494.  Other logical structures, such as n-dimensional arrays, arrays of strings, 
  495.  arrays of structures etc. can also be handled easily and flexibly.
  496.     
  497.  Note that expressions in general may not be subscripted, just variables. For 
  498.  example: {5,6,7,8}[3] is not supported. 
  499.  
  500.  
  501.  Slicing of Sequences
  502.  --------------------
  503.  A sequence of consecutive elements may be selected by giving the starting and 
  504.  ending element numbers. For example if x is {1, 1, 2, 2, 2, 1, 1, 1} then 
  505.  x[3..5] is the sequence {2, 2, 2}. x[3..3] is the sequence {2}. x[3..2] is 
  506.  also allowed. It evaluates to the length-0 sequence {}.  If y has the value: 
  507.  {"fred", "george", "mary"} then y[1..2] is {"fred",  "george"}.
  508.  
  509.  We can also use slices for overwriting portions of variables. After x[3..5] = 
  510.  {9, 9, 9} x would be {1, 1, 9, 9, 9, 1, 1, 1}. We could also have said 
  511.  x[3..5] = 9 with the same effect. Suppose y is {0, "Euphoria", 1, 1}. 
  512.  Then y[2][1..4] is "Euph". If we say y[2][1..4]="ABCD" then y will 
  513.  become {0, "ABCDoria", 1, 1}.
  514.  
  515.  We need to be a bit more precise in defining the rules for empty slices. 
  516.  Consider a slice s[i..j] where s is of length n. A slice from i to j, 
  517.  where  j = i-1  and i >= 1 produces the empty sequence, even if i = n+1. 
  518.  Thus 1..0  and n+1..n and everything in between are legal (empty) slices. 
  519.  Empty slices are quite useful in many algorithms. A slice from i to j where 
  520.  j < i - 1 is illegal , i.e. "reverse" slices such as s[5..3] are not allowed. 
  521.  
  522.  
  523.  Concatenation of Sequences and Atoms
  524.  ------------------------------------
  525.  Any two objects may be concatenated using the & operator. The result is a 
  526.  sequence with a length equal to the sum of the lengths of the concatenated 
  527.  objects (where atoms are considered to have length 1). e.g.
  528.  
  529.     {1, 2, 3} & 4   -- result is {1, 2, 3, 4}
  530.  
  531.     4 & 5           -- result is {4, 5}
  532.  
  533.     {{1, 1}, 2, 3} & {4, 5}   -- result is {{1, 1}, 2, 3, 4, 5}
  534.  
  535.     x = {}  
  536.     y = {1, 2}
  537.     y = y & x       -- y is still {1, 2}
  538.  
  539.  
  540.  Arithmetic Operations on Sequences
  541.  ----------------------------------
  542.  Any binary or unary arithmetic operation, including any of the built-in 
  543.  math routines, may be applied to entire sequences as well as to single 
  544.  numbers.
  545.  
  546.  When applied to a sequence, a unary operator is actually applied to each 
  547.  element in the sequence to yield a sequence of results of the same length. 
  548.  If one of these elements is itself a sequence then the same rule is applied 
  549.  recursively. e.g.
  550.  
  551.     x = -{1, 2, 3, {4, 5}}   -- x is {-1, -2, -3, {-4, -5}}
  552.  
  553.  If a binary operator has operands which are both sequences then the two 
  554.  sequences must be of the same length. The binary operation is then applied 
  555.  to corresponding elements taken from the two sequences to get a sequence of 
  556.  results. e.g.
  557.  
  558.     x = {5, 6, 7 {1, 1}} + {10, 10, 20, 100}
  559.     -- x is {15, 16, 27, {101, 101}}
  560.  
  561.  If a binary operator has one operand which is a sequence while the other is a 
  562.  single number (atom) then the single number is effectively repeated to 
  563.  form a sequence of equal length to the sequence operand. The rules for 
  564.  operating on two sequences then apply. Some examples:
  565.  
  566.     y = {4, 5, 6}
  567.  
  568.     w = 5 * y  -- w is {20, 25, 30}
  569.  
  570.     x = {1, 2, 3}
  571.     
  572.     z = x + y  -- z is {5, 7, 9}
  573.  
  574.     z = x < y  -- z is {1, 1, 1}
  575.  
  576.     w = {{1, 2}, {3, 4}, {5}}
  577.    
  578.     w = w * y  -- w is {{4, 8}, {15, 20}, {30}}
  579.  
  580.  
  581.  Comparison of Euphoria Objects with Other Languages
  582.  ---------------------------------------------------
  583.  By basing Euphoria on this one, simple, general, recursive data structure, 
  584.  a tremendous amount of the complexity normally found in programming languages
  585.  has been avoided. The arrays, record structures, unions, arrays of records, 
  586.  multidimensional arrays, etc. of other languages can all be easily 
  587.  simulated in Euphoria with sequences. So can higher-level structures such
  588.  as lists, stacks, queues, trees etc. 
  589.  
  590.  Furthermore, in Euphoria you can have sequences of mixed type; you can 
  591.  assign any object to an element of a sequence; and sequences easily grow or 
  592.  shrink in length without your having to worry about storage allocation issues.
  593.  The exact layout of a data structure does not have to be declared in advance,
  594.  and can change dynamically as required. It is easy to write generic code,
  595.  where, for instance, you push or pop a mix of various kinds of data 
  596.  objects using a single stack. 
  597.  
  598.  Data structure manipulations are very efficient since Euphoria will point to 
  599.  large data objects rather than copy them. 
  600.  
  601.  Programming in Euphoria is based entirely on creating and manipulating 
  602.  flexible, dynamic sequences of data. Sequences are it - there are no
  603.  other data structures to learn. You operate in a simple, safe, elastic world 
  604.  of *values*, that is far removed from the rigid, tedious, dangerous world
  605.  of bits, bytes, pointers and machine crashes. 
  606.  
  607.  Unlike other languages such as LISP and Smalltalk, Euphoria's 
  608.  "garbage collection" of unused storage is a continuous process that never 
  609.  causes random delays in execution of a program, and does not pre-allocate 
  610.  huge regions of memory.   
  611.  
  612.  The language definitions of conventional languages such as C, C++, Ada, etc.
  613.  are very complex. Most programmers become fluent in only a subset of the 
  614.  language. The ANSI standards for these languages read like complex legal 
  615.  documents. 
  616.  
  617.  You are forced to write different code for different data types simply to 
  618.  copy the data, ask for its current length, concatenate it, compare it etc. 
  619.  The manuals for those languages are packed with routines such as "strcpy",
  620.  "strncpy", "memcpy", "strcat",  "strlen", "strcmp", "memcmp", etc. that 
  621.  each only work on one of the many types of data.
  622.  
  623.  Much of the complexity surrounds issues of data type. How do you define 
  624.  new types? Which types of data can be mixed? How do you convert one type 
  625.  into another in a way that will keep the compiler happy? When you need to 
  626.  do something requiring flexibility at runtime, you frequently find yourself 
  627.  trying to fake out the compiler.
  628.  
  629.  In these languages the numeric value 4 (for example) can have a different 
  630.  meaning depending on whether it is an int, a char, a short, a double, an  
  631.  int * etc.. In Euphoria, 4 is the atom 4, period. Euphoria has something 
  632.  called types as we shall see later, but it is a much simpler concept.
  633.  
  634.  Issues of dynamic storage allocation and deallocation consume a great deal 
  635.  of programmer coding time and debugging time in these other languages, and 
  636.  make the resulting programs much harder to understand. 
  637.  
  638.  Pointer variables are extensively used. The pointer has been called the 
  639.  "go to" of data structures. It forces programmers to think of data as 
  640.  being bound to a fixed memory location where it can be manipulated in all 
  641.  sorts of low-level non-portable, tricky ways. A picture of the actual 
  642.  hardware that your program will run on is never far from your mind. Euphoria
  643.  does not have pointers and does not need them.
  644.  
  645.  
  646.  2.3 Declarations
  647.  ----------------
  648.  
  649.  Identifiers
  650.  -----------
  651.  Variable names and other user-defined symbols (identifiers) may be of any 
  652.  length. Upper and lower case are distinct. Identifiers must start with a 
  653.  letter and then be followed by letters, digits or underscores. The 
  654.  following reserved words have special meaning in Euphoria and may not be 
  655.  used as identifiers:
  656.  
  657.  and            end             include         then
  658.  by             exit            not             to
  659.  constant       for             or              type
  660.  do             function        procedure       while
  661.  else           global          profile         with
  662.  elsif          if              return          without 
  663.  
  664.  The Euphoria editor displays these words in blue. In this manual we use 
  665.  boldface.
  666.  
  667.  The following kinds of user-defined symbols may be declared in a program:
  668.  
  669.      o  procedures 
  670.     These perform some computation and may have a list of parameters, 
  671.     e.g.
  672.    
  673.     procedure empty()
  674.     end procedure
  675.  
  676.     procedure plot(integer x, integer y)
  677.         position(x, y)
  678.         puts(1, '*')
  679.     end procedure
  680.     
  681.     There are a fixed number of named parameters, but this is not 
  682.     restrictive since any parameter could be a variable-length sequence 
  683.     of arbitrary objects. In many languages variable-length parameter 
  684.     lists are impossible.  In C, you must set up strange mechanisms that
  685.     are complex enough that the average programmer cannot do it without
  686.     consulting a manual or a local guru. 
  687.  
  688.     A copy of the value of each argument is passed in. The formal 
  689.     parameter variables may be modified inside the procedure but this does
  690.     not affect the value of the arguments.
  691.  
  692.     Performance Note: The interpreter does not copy sequences unless it 
  693.     becomes necessary. For example,
  694.         y = {1,2,3,4,5,6,7}
  695.         x = y
  696.     The statement x = y does not cause the value of y to be copied.
  697.     Both x and y will simply "point" to the same data.
  698.     If we later perform x[3] = 9, then x will be given its own separate 
  699.     copy. The same thing applies to "copies" of arguments passed in to
  700.     subroutines.
  701.     
  702.      o  functions 
  703.     These are just like procedures, but they return a value, and can be
  704.     used in an expression, e.g.
  705.  
  706.     function max(atom a, atom b)
  707.         if a >= b then
  708.         return a
  709.         else
  710.         return b
  711.         end if
  712.     end function
  713.  
  714.     Any Euphoria object can be returned.  You can, in effect, have 
  715.     multiple return values, by returning a sequence of objects. e.g.
  716.     
  717.     return {quotient, remainder}
  718.  
  719.     We will use the general term "subroutine", or simply "routine" when a
  720.     remark is applicable to both procedures and functions.
  721.  
  722.      o  types 
  723.     These are special functions that may be used in declaring the allowed
  724.     values for a variable. A type must have exactly one parameter and 
  725.     should return an atom that is either TRUE (non-zero) or FALSE (zero).
  726.     Types can also be called just like other functions. They are discussed
  727.     in more detail below.
  728.  
  729.      o  variables 
  730.     These may be assigned values during execution e.g.
  731.  
  732.     integer x
  733.     x = 25
  734.  
  735.     object a, b, c
  736.     a = {}
  737.     b = a
  738.     c = 0
  739.  
  740.      o  constants
  741.     These are variables that are assigned an initial value that can 
  742.     never change e.g.
  743.       
  744.     constant MAX = 100
  745.     constant upper = MAX - 10, lower = 5
  746.  
  747.     The result of any expression can be assigned to a constant,
  748.     even one involving calls to previously defined functions, but once
  749.     the assignment is made the value of the constant variable is 
  750.     "locked in".
  751.  
  752.  
  753.  Scope
  754.  -----
  755.  Every symbol must be declared before it is used. This is restrictive, but it
  756.  has benefits. It means you always know in which direction to look for the 
  757.  definition of a subroutine or variable that is used at some point in the 
  758.  program. When looking at a subroutine definition, you know that there could 
  759.  not be a call to this routine from any routine defined earlier. In general, 
  760.  it forces you to organize your program into a hierarchy where there are 
  761.  distinct,  "layers" of  low-level , followed by higher-level routines. You 
  762.  can replace a layer without disrupting any lower layers.
  763.  
  764.  A symbol is defined from the point where it is declared to the end of its 
  765.  scope. The scope of a variable declared inside a procedure or function (a 
  766.  private variable) ends at the end of the procedure or function.  The scope 
  767.  of all other constants, procedures, functions and variables ends at the end
  768.  of the source file in which they are declared and they are referred to as 
  769.  local, unless the word global precedes their declaration, in which case their 
  770.  scope extends indefinitely. Procedures and functions can call themselves 
  771.  recursively.
  772.  
  773.  Constant declarations must be outside of any function or procedure.
  774.  
  775.  A special case is that of the controlling variable used in a for-loop. It is 
  776.  automatically declared at the beginning of the loop, and its scope ends at 
  777.  the end of the for loop. If the loop is inside a function or procedure, the 
  778.  loop variable is a private variable and may not have the same name as any 
  779.  other private variable. When the loop is at the top level, outside of any  
  780.  function or procedure, the loop variable is a local variable and may not have 
  781.  the same name as any other global or local variable in that file. You do not 
  782.  declare loop variables as you would other variables. The range of values 
  783.  specified in the for statement defines the legal values of the loop variable 
  784.  - specifying a type would be redundant and is not allowed..
  785.  
  786.  
  787.  Specifying the type of a variable
  788.  ---------------------------------
  789.  
  790.  Variable declarations consist of a type name followed by a list of
  791.  the variables being declared. For example,
  792.  
  793.     global integer x, y, z 
  794.  
  795.     object a 
  796.  
  797.     procedure fred(sequence q, sequence r)
  798.  
  799.  In a parameter list like the one above, the type name may only be followed by 
  800.  a single variable name. 
  801.  
  802.  The types: object, sequence, atom and integer are predefined. Variables of 
  803.  type object may take on any value. Those declared with type sequence must be 
  804.  always be sequences. Those declared with type atom must always be atoms. Those
  805.  declared with type integer must be atoms with integer values from -1073709056 
  806.  to +1073709055 inclusive. You can perform exact calculations on larger integer
  807.  values, up to about 15 decimal digits, but declare them as atom, rather than 
  808.  integer.
  809.  
  810.  Performance Note: Calculations using integer variables will usually be 
  811.  somewhat faster than calculations involving atom variables. If your
  812.  machine has floating-point hardware, Euphoria will use it to manipulate  
  813.  atoms that aren't representable as integers, otherwise floating-point
  814.  emulation routines contained in ex.exe are used.
  815.  
  816.  To augment the predefined types, you can create new types. All you have to 
  817.  do is define a single-parameter function, but declare it with  
  818.  type ... end type instead of function ... end function. For example,
  819.  
  820.  
  821.     type hour(integer x)
  822.          return x >= 0 and x <= 23
  823.     end type
  824.  
  825.     hour h1, h2
  826.  
  827.  This guarantees that variables h1 and h2 can only be assigned integer values 
  828.  in the range 0 to 23 inclusive. After an assignment to h1 or h2 the 
  829.  interpreter will call "hour()", passing the new value.  The parameter x will 
  830.  first be checked to see if it is an integer. If it is, the return statement 
  831.  will be executed to test the value of x (i.e. the new value of h1 or h2). 
  832.  If "hour" returns true, execution continues normally. If "hour" returns false
  833.  then the program is aborted with a suitable diagnostic message. 
  834.  
  835.     procedure set_time(hour h)
  836.  
  837.  set_time() above can only be called with a reasonable value for parameter h.
  838.  
  839.  A variable's type will be checked after each assignment to the variable 
  840.  (except where the compiler can predetermine that a check will not be 
  841.  necessary), and the program will terminate immediately if the type function 
  842.  returns false.  Subroutine parameter types are checked when the subroutine 
  843.  is called. This checking guarantees that a variable can never have a value 
  844.  that does not belong to the type of that variable.
  845.  
  846.  Unlike other languages, the type of a variable does not affect any 
  847.  calculations on the variable. Only the value of the variable matters in an 
  848.  expression. The type just serves as an error check to prevent any "corruption"
  849.  of the variable. 
  850.  
  851.  Type checking can be turned off or on in between subroutines using the 
  852.  with type_check or without type_check commands. It is initially on by default.
  853.  
  854.  Note to Benchmarkers: When comparing the speed of Euphoria programs against 
  855.  programs written in other languages, you should specify  without type_check 
  856.  at the top of the file, unless the other language provides a comparable 
  857.  amount of run-time checking. This gives Euphoria permission to skip runtime 
  858.  type checks, thereby saving some execution time. When type_check is off, all 
  859.  other checks are still performed, e.g. subscript checking, uninitialized 
  860.  variable checking etc. Even when you turn off type checking, Euphoria 
  861.  reserves the right to make checks at strategic places, since this can 
  862.  actually allow it to run your program faster in many cases. So you may 
  863.  still get a type check failure even when you have turned off type checking. 
  864.  With or without type_check, you will never get a machine-level exception. 
  865.  You will always get a meaningful message from Euphoria when something goes 
  866.  wrong.
  867.  
  868.  Euphoria's method of defining types is much simpler than what you will find 
  869.  in other languages, yet Euphoria provides the programmer with greater 
  870.  flexibility in defining the legal values for a type of data. Any algorithm 
  871.  can be used to include or exclude values. You can even declare a variable 
  872.  to be of type object which will allow it to take on any value. Routines can 
  873.  be written to work with very specific types, or very general types.
  874.  
  875.  Strict type definitions can greatly aid the process of debugging.  Logic 
  876.  errors are caught close to their source and are not allowed to propagate in 
  877.  subtle ways through the rest of the program. Furthermore, it is much easier 
  878.  to reason about the misbehavior of a section of code when you are guaranteed 
  879.  that the variables involved always had a legal value, if not the desired 
  880.  value.
  881.  
  882.  Types also provide meaningful, machine-checkable documentation about your 
  883.  program, making it easier for you or others to understand your code at a 
  884.  later date. Combined with the subscript checking, uninitialized variable 
  885.  checking, and other checking that is always present, strict run-time type 
  886.  checking makes debugging much easier in Euphoria than in most other 
  887.  languages. It also increases the reliability of the final program since 
  888.  many latent bugs that would have survived the testing phase in other 
  889.  languages will have been caught by Euphoria.
  890.  
  891.  Anecdote 1: In porting a large C program to Euphoria, a number
  892.  of latent bugs were discovered. Although this C program was believed to be 
  893.  totally "correct", we found: a situation where an uninitialized variable 
  894.  was being read; a place where element number "-1" of an array was routinely 
  895.  written and read; and a situation where something was written just off the 
  896.  screen. These problems resulted in errors that weren't easily visible to a 
  897.  casual observer, so they had survived testing of the C code. 
  898.  
  899.  Anecdote 2: The Quick Sort algorithm presented on page 117 of Writing 
  900.  Efficient Programs by Jon Bentley has a subscript error! The algorithm will 
  901.  sometimes read the element just before the beginning of the array to be 
  902.  sorted, and will sometimes read the element just after the end of the array. 
  903.  Whatever garbage is read, the algorithm will still work - this is probably 
  904.  why the bug was never caught. But what if there isn't any (virtual) memory 
  905.  just before or just after the array? Bentley later modifies the algorithm 
  906.  such that this bug goes away -- but he presented this version as being 
  907.  correct. Even the experts need subscript checking!
  908.  
  909.  Performance Note: When typical user-defined types are used extensively, type 
  910.  checking adds only 20 to 40 percent to execution time. Leave it on unless 
  911.  you really need the extra speed. You might also consider turning it off for
  912.  just a few heavily-executed routines. Profiling can help with this decision.
  913.  
  914.  
  915.  2.4 Statements
  916.  --------------
  917.  
  918.  The following kinds of executable statements are available:
  919.  
  920.     o   assignment statement
  921.  
  922.     o   procedure call 
  923.  
  924.     o   if statement
  925.  
  926.     o   while statement
  927.  
  928.     o   for statement 
  929.  
  930.     o   return statement
  931.  
  932.     o   exit statement
  933.  
  934.  Semicolons are not used in Euphoria, but you are free to put as many 
  935.  statements as you like on one line, or to split a single statement across 
  936.  many lines. You may not split a statement in the middle of a variable name, 
  937.  string, number or keyword. 
  938.  
  939.  An assignment statement assigns the value of an expression to a simple 
  940.  variable, or to a subscript or slice of a variable. e.g. 
  941.     
  942.     x = a + b
  943.  
  944.     y[i] = y[i] + 1
  945.  
  946.     y[i..j] = {1, 2, 3}
  947.  
  948.  The previous value of the variable, or element(s) of the subscripted or 
  949.  sliced variable are discarded.  For example, suppose x was a 1000-element 
  950.  sequence that we had initialized with:
  951.  
  952.     object x
  953.  
  954.     x = repeat(0, 1000)  -- repeat 0, 1000 times
  955.  
  956.  and then later we assigned an atom to x with:
  957.  
  958.     x = 7
  959.  
  960.  This is perfectly legal since x is declared as an object. The previous value 
  961.  of x, namely the 1000-element sequence, would simply disappear. Actually, 
  962.  the space consumed by the 1000-element sequence will be automatically 
  963.  recycled due to Euphoria's dynamic storage allocation.
  964.  
  965.  A procedure call starts execution of a procedure, passing it an optional list 
  966.  of argument values. e.g.
  967.  
  968.     plot(x, 23)
  969.  
  970.  An if statement tests an expression to see if it is 0 (false) or non-zero 
  971.  (true) and then executes the appropriate series of statements.  There may 
  972.  be optional elsif and else clauses. e.g.
  973.  
  974.     if a < b then
  975.         x = 1
  976.     end if
  977.  
  978.  
  979.     if a = 9 then
  980.         x = 4
  981.         y = 5
  982.     else
  983.         z = 8
  984.     end if
  985.  
  986.  
  987.     if char = 'a' then
  988.         x = 1
  989.     elsif char = 'b' then
  990.         x = 2
  991.     elsif char = 'c' then
  992.         x = 3
  993.     else
  994.         x = -1
  995.     end if
  996.  
  997.  A while statement tests an expression to see if it is non-zero (true), 
  998.  and while it is true a loop is executed. e.g.
  999.  
  1000.     while x > 0 do
  1001.         a = a * 2
  1002.         x = x - 1
  1003.     end while
  1004.  
  1005.  A for statement sets up a special loop with a controlling loop variable 
  1006.  that runs from an initial value up or down to some final value. e.g.
  1007.  
  1008.     for i = 1 to 10 do
  1009.         print(1, i)
  1010.     end for
  1011.  
  1012.     for i = 10 to 20 by 2 do
  1013.         for j = 20 to 10 by -2 do
  1014.         print(1, {i, j})
  1015.         end for
  1016.     end for
  1017.  
  1018.  The loop variable is declared automatically and exists until the end of the 
  1019.  loop. Outside of the loop the variable has no value and is not even declared.
  1020.  If you need its final value, copy it into another variable before leaving 
  1021.  the loop. The compiler will not allow any assignments to a loop variable. The
  1022.  initial value, loop limit and increment must all be atoms. If no increment 
  1023.  is specified then +1 is assumed. The limit and increment values are 
  1024.  established when the loop is entered, and are not affected by anything that 
  1025.  happens during the execution of the loop. 
  1026.  
  1027.  A return statement returns from a subroutine. If the subroutine is a function 
  1028.  or type then a value must also be returned. e.g.
  1029.  
  1030.     return
  1031.  
  1032.     return {50, "FRED", {}}
  1033.  
  1034.  An exit statement may appear inside a while loop or a for loop. It causes 
  1035.  immediate termination of the loop, with control passing to the first statement
  1036.  after the loop. e.g.
  1037.  
  1038.     for i = 1 to 100 do
  1039.         if a[i] = x then
  1040.         location = i
  1041.         exit
  1042.         end if
  1043.     end for
  1044.  
  1045.  It is also quite common to see something like this:
  1046.  
  1047.     while TRUE do
  1048.          ...
  1049.          if some_condition then
  1050.             exit
  1051.          end if
  1052.           ...
  1053.     end while
  1054.  
  1055.  i.e. an "infinite" while loop that actually terminates via an exit statement 
  1056.  at some arbitrary point in the body of the loop.
  1057.  
  1058.  
  1059.  2.5 Top-Level Commands
  1060.  ----------------------
  1061.  
  1062.  Euphoria processes your .ex file in one pass, starting at the first line and 
  1063.  proceeding through to the last line. When a procedure or function definition 
  1064.  is encountered, the routine is checked for syntax and converted into an 
  1065.  internal form, but no execution takes place. When a statement that is outside 
  1066.  of any routine is encountered, it is checked for syntax, converted into an 
  1067.  internal form and then immediately executed.  If your .ex file contains only 
  1068.  routine definitions, but no immediate execution statements, then nothing will 
  1069.  happen when you try to run it (other than syntax checking). You need to have 
  1070.  an immediate statement to call your main routine (see the example program in 
  1071.  section 1.1). It is quite possible to have a .ex file with nothing but 
  1072.  immediate statements, for example you might want to use Euphoria as a 
  1073.  desk calculator, typing in just one print (or ? see below) statement into a 
  1074.  file, and then executing it. The langwar demo program 
  1075.  (euphoria\demo\langwar\lw.ex) quickly reads in and displays a file on the 
  1076.  screen, before the rest of the program is compiled (on a 486 this makes 
  1077.  little difference as the compiler takes less than a second to finish 
  1078.  compiling the whole program). Another common practice is to immediately 
  1079.  initialize a global variable, just after its declaration.
  1080.  
  1081.  The following special commands may only appear at the top level i.e. 
  1082.  outside of any function or procedure. As we have seen, it is also 
  1083.  possible to use any Euphoria statement, including for loops, while loops, 
  1084.  if statements etc. (but not return), at the top level.
  1085.  
  1086.  include filename - reads in (compiles) a Euphoria source file in the presence 
  1087.            of any global symbols that have already been defined. 
  1088.            Global symbols defined in the included file remain visible
  1089.            in the remainder of the program. If an absolute pathname 
  1090.            is given, Euphoria will use it. When a relative pathname 
  1091.            is given, Euphoria will first look for filename in the 
  1092.            same directory as the main file given on the ex command 
  1093.            line. If it's not there, it will look in %EUDIR%\include, 
  1094.            where EUDIR is the environment variable that must be set 
  1095.            when using Euphoria. This directory contains the standard
  1096.            Euphoria include files.
  1097.  
  1098.  profile         - outputs an execution profile showing the number of times
  1099.            each statement was executed. Only statements compiled  
  1100.            with profile will be shown. The output is placed in the 
  1101.            file ex.pro. View this file with the Euphoria editor to 
  1102.            see a color display.
  1103.  
  1104.  with            - turns on one of the compile options: profile, trace, 
  1105.            warning or type_check. Options warning and type_check are
  1106.            initially on, while profile and trace are initially off. 
  1107.            These are global options. For example if you have:
  1108.  
  1109.             without type_check
  1110.             include graphics.e 
  1111.  
  1112.            then type checking will be turned off inside graphics.e as 
  1113.            well as in the current file. 
  1114.  
  1115.  without         - turns off one of the above options. Note that each of 
  1116.            these options may be turned on or off between subroutines 
  1117.            but not inside of a subroutine.
  1118.  
  1119.  
  1120.  Redirecting Standard Input and Standard Output
  1121.  ----------------------------------------------
  1122.  Routines such as gets() and puts() can use standard input (file #0), 
  1123.  standard output (file #1), and standard error output (file #2). Standard 
  1124.  input and output can then be redirected as in:
  1125.  
  1126.     ex myprog < myinput  > myoutput
  1127.  
  1128.  See section 4.5 for more details.
  1129.  
  1130.  
  1131.             3. Debugging
  1132.             ============
  1133.  
  1134.  
  1135.  Debugging in Euphoria is much easier than in most other programming languages.
  1136.  The extensive runtime checking provided at all times by Euphoria automatically
  1137.  catches many bugs that in other languages might take hours of your time to 
  1138.  track down. When Euphoria catches an error, you will always get a brief 
  1139.  report on your screen, and a detailed report in a file called "ex.err". 
  1140.  These reports always include a full English description of what happened, 
  1141.  along with a call-stack traceback. The file ex.err will also have a dump of 
  1142.  all variable values, and optionally a list of the most recently executed 
  1143.  statements. For extremely large sequences, only a partial dump is shown.
  1144.  
  1145.  In addition, you are able to create user-defined types that precisely 
  1146.  determine the set of legal values for each of your variables. An error 
  1147.  report will occur the moment that one your variables is assigned an illegal 
  1148.  value.
  1149.  
  1150.  Sometimes a program will misbehave without failing any runtime checks. In 
  1151.  any programming language it may be a good idea to simply study the source 
  1152.  code and rethink the algorithm that you have coded. It may also be useful 
  1153.  to insert print statements at strategic locations in order to monitor the 
  1154.  internal logic of the program. This approach is particularly convenient in 
  1155.  an interpreted language like Euphoria since you can simply edit the source
  1156.  and rerun the program without waiting for a recompile/relink.  
  1157.  
  1158.  Euphoria provides you with additional powerful tools for debugging. You 
  1159.  can trace the execution of your program source code on one screen while 
  1160.  you witness the output of your program on another. with trace / without trace 
  1161.  commands select the subroutines in your program that are available for tracing.
  1162.  Often you will simply insert a with trace command at the very beginning of 
  1163.  your source code to make it all traceable. Sometimes it is better to place 
  1164.  the first with trace after all of your user-defined types, so you don't 
  1165.  trace into these routines after each assignment to a variable. At other times,
  1166.  you may know exactly which routine or routines you are interested in tracing, 
  1167.  and you will want to select only these ones. Of course, once you are in the 
  1168.  trace window you can interactively skip over the execution of any routine by 
  1169.  pressing down-arrow on the keyboard rather than Enter. 
  1170.  
  1171.  Only traceable lines can appear in ex.err as "most-recently-executed lines" 
  1172.  should a runtime error occur. If you want this information and didn't get it, 
  1173.  you should insert a with trace and then rerun your program. Execution will 
  1174.  be a bit slower when lines compiled with trace are executed.
  1175.  
  1176.  After you have predetermined the lines that are traceable, your program must 
  1177.  then dynamically cause the trace facility to be activated by executing a 
  1178.  trace(1) statement. Again, you could simply say:
  1179.  
  1180.     with trace
  1181.     trace(1)
  1182.  
  1183.  at the top of your program, so you can start tracing from the beginning of 
  1184.  execution. More commonly, you will want to trigger tracing when a certain 
  1185.  routine is entered, or when some condition arises. e.g.
  1186.     
  1187.     if  x < 0 then
  1188.         trace(1)
  1189.     end if
  1190.  
  1191.  You can turn off tracing by executing a trace(0) statement. You can also 
  1192.  turn it off interactively by typing 'q' to quit tracing. Remember that 
  1193.  with trace must appear outside of any routine, whereas trace(1) and 
  1194.  trace(0) can appear inside a routine or outside.
  1195.  
  1196.  You might want to turn on tracing from within a type. Suppose you run 
  1197.  your program and it fails, with the ex.err file showing that one of your 
  1198.  variables has been set to a strange, although not illegal value, and you 
  1199.  wonder how it could have happened. Simply create a type for that variable 
  1200.  that executes trace(1) if the value being assigned to the variable is the 
  1201.  strange one that you are interested in.
  1202.  e.g.
  1203.     type positive_int(integer x)
  1204.         if x = 99 then
  1205.         trace(1) -- how can this be???
  1206.         return 1 -- keep going
  1207.         else
  1208.         return x > 0
  1209.         end if
  1210.     end type
  1211.  
  1212.  You will then be able to see the exact statement that caused your variable 
  1213.  to be set to the strange value, and you will be able to check the values 
  1214.  of other variables. You will also be able to check the output screen to 
  1215.  see what has been happening up to this precise moment. If you make your 
  1216.  special type return 0 for the strange value instead of 1, you can force a 
  1217.  dump into ex.err.
  1218.  
  1219.  The Trace Screen
  1220.  ----------------
  1221.  When a trace(1) statement is executed, your main output screen is saved and 
  1222.  a trace screen appears. It shows a view of your program with the statement 
  1223.  that will be executed next highlighted, and several statements before and 
  1224.  after showing as well. Several lines at the bottom of the screen are 
  1225.  reserved for displaying variable names and values. The top line shows the 
  1226.  commands that you can enter at this point:
  1227.  
  1228.  F1 - display main output screen - take a look at your program's output so far
  1229.  
  1230.  F2 - redisplay trace screen. Press this key while viewing the main output 
  1231.       screen to return to the trace display.
  1232.  
  1233.  Enter - execute the currently-highlighted statement only
  1234.  
  1235.  down-arrow - continue execution and break when any statement coming after 
  1236.           this one in the source listing is executed. This lets you skip 
  1237.           over subroutine calls. It also lets you force your way out of 
  1238.           repetitive loops.
  1239.  
  1240.  ? - display the value of a variable. Many variables are displayed 
  1241.      automatically as they are assigned a value, but sometimes you will have 
  1242.      to explicitly ask for one that is not on display. After hitting ?
  1243.      you will be prompted for the name of the variable. Variables that are 
  1244.      not defined at this point cannot be shown. Variables that have not yet 
  1245.      been initialized will have <NO VALUE> beside their name.
  1246.  
  1247.  q - quit tracing and resume normal execution. Tracing will start again when 
  1248.      the next trace(1) is executed.
  1249.  
  1250.  ! - this will abort execution of your program. A traceback and dump of 
  1251.      variable values will go to ex.err.
  1252.  
  1253.  As you trace your program, variable names and values appear automatically in 
  1254.  the bottom portion of the screen. Whenever a variable is assigned-to you will 
  1255.  see its name and new value appear at the bottom. This value is always kept 
  1256.  up-to-date. Private variables are automatically cleared from the screen 
  1257.  when their routine returns. When the variable display area is full, 
  1258.  least-recently referenced variables will be discarded to make room for 
  1259.  new variables. 
  1260.  
  1261.  The trace screen adopts the same graphics mode as the main output screen. 
  1262.  This makes flipping between them quicker and easier.
  1263.  
  1264.  When a traced program requests keyboard input, the main output screen will 
  1265.  appear, to let you type your input as you normally would. This works fine for 
  1266.  gets() input. When get_key() (quickly samples the keyboard) is called you 
  1267.  will be given 10 seconds to type a character otherwise it is assumed that 
  1268.  there is no input for this call to get_key(). This allows you to test the 
  1269.  case of input and also the case of no input for get_key().
  1270.  
  1271.  
  1272.  
  1273.  
  1274.             4. Built-in Routines
  1275.             ====================
  1276.  
  1277.  
  1278.  Many built-in procedures and functions are provided. The names of these 
  1279.  routines are not reserved. If a user-defined symbol has the same name, it 
  1280.  will override the built-in routine until the end of scope of the 
  1281.  user-defined symbol (you will get a suppressible warning about this). Some 
  1282.  routines are written in Euphoria and you must include one of the .e files in 
  1283.  euphoria\include to use them. Where this is the case, the appropriate include 
  1284.  file is noted. The editor displays in magenta those routines that are part 
  1285.  of the interpreter, ex.exe, and require no include file.
  1286.  
  1287.  To indicate what kind of object may be passed in and returned, the following 
  1288.  prefixes are used:
  1289.  
  1290.   fn - an integer used as a file number
  1291.    a - an atom
  1292.    i - an integer 
  1293.    s - a sequence
  1294.   st - a string sequence, or single-character atom
  1295.    x - a general object (atom or sequence)
  1296.  
  1297.  An error will result if an illegal argument value is passed to any of these 
  1298.  routines.
  1299.  
  1300.  
  1301.  4.1 Predefined Types
  1302.  --------------------
  1303.  
  1304.  As well as declaring variables with these types, you can also call them 
  1305.  just like ordinary functions, in order to test if a value is a certain type.
  1306.  
  1307.  
  1308.  i = integer(x)
  1309.     Return 1 if x is an integer in the range -1073709056 to +1073709055. 
  1310.     Otherwise return 0.  
  1311.  
  1312.  
  1313.  i = atom(x)
  1314.     Return 1 if x is an atom else return 0.
  1315.  
  1316.  
  1317.  i = sequence(x)
  1318.     Return 1 if x is a sequence else return 0.
  1319.  
  1320.  
  1321.  4.2 Sequence Manipulating Routines
  1322.  ----------------------------------
  1323.  
  1324.  i = length(s)
  1325.     Return the length of s. s must be a sequence. e.g.
  1326.  
  1327.         length({{1,2}, {3,4}, {5,6}}) -- 3
  1328.         length("")              -- 0
  1329.         length({})              -- 0
  1330.  
  1331.     Notice that {} and "" both represent the same empty sequence.
  1332.     As a matter of style, use "" when you are thinking of it as an 
  1333.     empty string of characters. Use {} when it is an empty sequence
  1334.     in general.
  1335.  
  1336.  
  1337.  s = repeat(x, a)
  1338.     Create a sequence of length a where each element is x.
  1339.     e.g.
  1340.         repeat(0, 10)      -- {0,0,0,0,0,0,0,0,0,0}
  1341.         repeat("JOHN", 4)  -- {"JOHN", "JOHN", "JOHN", "JOHN"}
  1342.  
  1343.  
  1344.  s2 = append(s1, x)
  1345.     Append x to the end of sequence s1. The length of s2 will be 
  1346.     length(s1) + 1. If x is an atom this is the same as 
  1347.     s2 = s1 & x. If x is a sequence it is definitely not the same. 
  1348.     You can use append to dynamically grow a sequence e.g.
  1349.     
  1350.         x = {}
  1351.         for i = 1 to 10 do
  1352.                x = append(x, i)
  1353.         end for
  1354.         -- x is now {1,2,3,4,5,6,7,8,9,10}
  1355.  
  1356.     The necessary storage is allocated automatically (and quite 
  1357.     efficiently) with Euphoria's dynamic storage allocation. See also 
  1358.     the gets() example in section 4.5.
  1359.  
  1360.  
  1361.  s2 = prepend(s1, x)
  1362.     Prepend x to the start of sequence s1. The length of s2 will be 
  1363.     length(s1) + 1. If x is an atom this is the same as s2 = x & s1. 
  1364.     If x is a sequence it is definitely not the same. e.g.
  1365.     
  1366.         prepend({1,2,3}, {0,0})    -- {{0,0}, 1, 2, 3}
  1367.         {0,0} & {1,2,3}        -- {0, 0, 1, 2, 3}
  1368.  
  1369.  
  1370.  4.3 Searching and Sorting 
  1371.  ------------------------- 
  1372.  
  1373.  i = compare(x1, x2)
  1374.     Return 0 if objects x1 and x2 are identical, 1 if x1 is greater 
  1375.     than x2, -1 if x1 is less than x2. Atoms are considered to be less 
  1376.     than sequences. Sequences are compared "alphabetically" starting 
  1377.     with the first element until a difference is found. e.g.
  1378.  
  1379.         compare("ABC", "ABCD")    -- -1
  1380.  
  1381.  
  1382.  i = find(x, s)
  1383.     Find x as an element of s. If successful, return the first element 
  1384.     number of s where there is a match. If unsuccessful return 0. 
  1385.     Example:
  1386.  
  1387.         location = find(11, {5, 8, 11, 2, 3})
  1388.         -- location is set to 3
  1389.  
  1390.         names = {"fred", "rob", "george", "mary", ""}
  1391.         location = find("mary", names)
  1392.         -- location is set to 4
  1393.  
  1394.  
  1395.  i = match(s1, s2)
  1396.     Try to match s1 against some slice of s2. If successful, return 
  1397.     the element number of s2 where the (first) matching slice begins, 
  1398.     else return 0. Example:
  1399.  
  1400.         location = match("pho", "Euphoria")
  1401.         -- location is set to 3
  1402.  
  1403.  
  1404.  include sort.e
  1405.  x2 = sort(x1)
  1406.     Sort x into ascending order using a fast sorting algorithm. By 
  1407.     defining your own compare function to override the built-in 
  1408.     compare(), you can change the ordering of values from sort(), and 
  1409.     perhaps choose a field or element number on which to base the sort.
  1410.     Define your compare() as a global function before including sort.e.
  1411.     Example:
  1412.         x = 0 & sort({7,5,3,8}) & 0
  1413.         -- x is set to {0, 3, 5, 7, 8, 0}
  1414.  
  1415.  
  1416.  4.4 Math
  1417.  --------
  1418.  These routines can be applied to individual atoms or to sequences of values. 
  1419.  See "Arithmetic Operations on Sequences" in chapter 2.
  1420.  
  1421.  x2 = sqrt(x1)   
  1422.     Calculate the square root of x1.
  1423.  
  1424.  x2 = rand(x1) 
  1425.     Return a random integer from 1 to x1 where x1 is from 1 to 32767.
  1426.  
  1427.  x2 = sin(x1)
  1428.     Return the sin of x1, where x1 is in radians.
  1429.  
  1430.  x2 = cos(x1)
  1431.     Return the cos of x1, where x1 is in radians.
  1432.  
  1433.  x2 = tan(x1) 
  1434.     Return the tan of x1, where x1 is in radians.
  1435.  
  1436.  x2 = log(x1)
  1437.     Return the natural log of x1
  1438.  
  1439.  x2 = floor(x1)
  1440.     Return the greatest integer less than or equal to x1.
  1441.  
  1442.  x3 = remainder(x1, x2)
  1443.     Compute the remainder after dividing x1 by x2. The result will have 
  1444.     the same sign as x1, and the magnitude of the result will be less 
  1445.     than the magnitude of x2.
  1446.  
  1447.  x3 = power(x1, x2)
  1448.     Raise x1 to the power x2
  1449.  
  1450.  Examples:
  1451.     sin_x = sin({.5, .9, .11})         -- {.479, .783, .110} 
  1452.  
  1453.      ? power({5, 4, 3.5}, {2, 1, -0.5})  -- {25, 4, 0.534522}
  1454.  
  1455.      ? remainder({81, -3.5, -9, 5.5}, {8, -1.7, 2, -4})
  1456.                          -- {1, -0.1, -1, 1.5}
  1457.  
  1458.  
  1459.  4.5 File and Device I/O
  1460.  -----------------------
  1461.  
  1462.  To do input or output on a file or device you must first open the file or 
  1463.  device, then use the routines below to read or write to it, then close 
  1464.  the file or device. open() will give you a file number to use as the first 
  1465.  argument of the other I/O routines. Certain files/devices are opened for you 
  1466.  automatically:
  1467.  
  1468.          0 - standard input
  1469.          1 - standard output
  1470.          2 - standard error
  1471.  
  1472.  Unless you redirect them on the command line, standard input comes from 
  1473.  the keyboard, standard output and standard error go to the screen. When 
  1474.  you write something to the screen it is written immediately without 
  1475.  buffering. If you write to a file, your characters are put into a buffer 
  1476.  until there are enough of them to write out efficiently. When you close 
  1477.  the file or device, any remaining characters are written out. When your 
  1478.  program terminates, any files that are still open will be closed for you 
  1479.  automatically.
  1480.  
  1481.  fn = open(s1, s2)
  1482.     Open a file or device, to get the file number. -1 is returned if the 
  1483.     open fails. s1 is the path name of the file or device.  s2 is the 
  1484.     mode in which the file is to be opened. Possible modes are:
  1485.       "r"  - open text file for reading
  1486.       "rb" - open binary file for reading
  1487.       "w"  - create text file for writing
  1488.       "wb" - create binary file for writing
  1489.       "u"  - open text file for update (reading and writing)
  1490.       "ub" - open binary file for update
  1491.       "a"  - open text file for appending
  1492.       "ab" - open binary file for appending
  1493.  
  1494.     Files opened for read or update must already exist. Files opened
  1495.     for write or append will be created if necessary. A file opened 
  1496.     for write will be set to 0 bytes. Output to a file opened for
  1497.     append will start at the end of file.
  1498.  
  1499.     Output to text files will have carriage-return characters 
  1500.     automatically added before linefeed characters. On input, these 
  1501.     carriage-return characters are removed. Null characters (0) are 
  1502.     removed from output. I/O to binary files is not modified in any way.
  1503.  
  1504.     Some typical devices that you can open are:
  1505.       "CON"    the console (screen)
  1506.       "AUX"    the serial auxiliary port 
  1507.       "COM1"   serial port 1
  1508.       "COM2"   serial port 2
  1509.       "PRN"    the printer on the parallel port
  1510.       "NUL"    a non-existent device that accepts and discards output 
  1511.  
  1512.  
  1513.  close(fn)
  1514.     Close a file or device and flush out any still-buffered characters.
  1515.  
  1516.  
  1517.  print(fn, x)
  1518.     Print an object x with braces { , , , } to show the structure.
  1519.     If you want to see a string of characters, rather than just the 
  1520.     ASCII codes, you need to use puts or printf below. e.g.
  1521.  
  1522.         print(1, "ABC")  -- output is:  {65, 66, 67}
  1523.          puts(1, "ABC")  -- output is:  ABC
  1524.  
  1525.  ? x    This is just a shorthand way of saying print(1, x), i.e. printing
  1526.     the value of an expression to the standard output. For example 
  1527.     ? {1, 2} + {3, 4}  would display {4, 6}. ? adds new-lines to 
  1528.     make the output more readable on your screen (or standard output).  
  1529.  
  1530.  
  1531.  printf(fn, st, x)
  1532.     Print x using format string st. If x is an atom then a single 
  1533.     value will be printed. If x is a sequence, then formats from st 
  1534.     are applied to successive elements of x. Thus printf always takes 
  1535.     exactly 3 arguments. Only the length of the last argument, 
  1536.     containing the values to be printed, will vary. The basic formats 
  1537.     are: 
  1538.       %d - print an atom as a decimal integer
  1539.       %x - print an atom as a hexadecimal integer
  1540.       %o - print an atom as an octal integer
  1541.       %s - print a sequence as a string of characters
  1542.       %e - print an atom as a floating point number with exponential 
  1543.            notation
  1544.       %f - print an atom as a floating-point number with a decimal point
  1545.            but no exponent
  1546.       %g - print an atom as a floating point number using either
  1547.          the %f or %e format, whichever seems more appropriate
  1548.       %% - print '%' character
  1549.  
  1550.     Field widths can be added to the basic formats, e.g. %5d, or %8.2f. 
  1551.     The number before the decimal point is the minimum field width to be 
  1552.     used. The number after the decimal point is the precision to be used.
  1553.  
  1554.     If the field width is negative, e.g. %-5d then the value will be 
  1555.     left-justified within the field. Normally it will be right-justified.
  1556.     If the field width starts with a leading 0 e.g. %08d then leading
  1557.     zeros will be supplied to fill up the field. If the field width
  1558.     starts with a '+' e.g. %+7d then a plus sign will be printed for
  1559.     positive values. 
  1560.  
  1561.     Examples:
  1562.          rate = 7.75
  1563.          printf(myfile, "The interest rate is: %8.2f\n", rate)
  1564.  
  1565.          name="John Smith"
  1566.          score=97
  1567.          printf(1, "%15s, %5d\n", {name, score})
  1568.  
  1569.     Watch out for the following common mistake:
  1570.  
  1571.          printf(1, "%15s", name)
  1572.  
  1573.     This will print only the first character of name, as each element 
  1574.     of name is taken to be a separate value to be formatted. You must 
  1575.     say this instead:
  1576.  
  1577.          printf(1, "%15s", {name})
  1578.          
  1579.  
  1580.  puts(fn, x)
  1581.     Print a single character (atom) or sequence of characters as bytes 
  1582.     of text. e.g.
  1583.  
  1584.     puts(screen, "Enter your first name: ")
  1585.  
  1586.     puts(output, 'A')  -- the single byte 65 will be sent to output                 
  1587.  
  1588.  
  1589.  i = getc(fn)
  1590.     Get the next character (byte) from file fn.  -1 is returned at end 
  1591.     of file
  1592.  
  1593.  
  1594.  x = gets(fn)
  1595.     Get a sequence (one line, including \n) of characters from text 
  1596.     file fn. The atom -1 is returned on end of file. Example:
  1597.  
  1598.         -- read a text file into a sequence
  1599.         buffer = {}
  1600.         while 1 do
  1601.                  line = gets(0)
  1602.                  if atom(line) then
  1603.                 exit   -- end of file
  1604.                  end if
  1605.                  buffer = append(buffer, line)
  1606.            end while
  1607.  
  1608.  
  1609.  i = get_key()
  1610.     Return the key that was pressed by the user, without waiting for 
  1611.     carriage return, or return -1 if no key was pressed
  1612.  
  1613.  
  1614.  include get.e
  1615.  s = get(fn)
  1616.     Read the next representation of a Euphoria object from file fn, and 
  1617.      convert it into the value of that object. s will be a 2-element 
  1618.     sequence {error status, value}. Error status values are:
  1619.  
  1620.         GET_SUCCESS  -- object was read successfully
  1621.         GET_EOF      -- end of file before object was read
  1622.         GET_FAIL     -- object is not syntactically correct
  1623.  
  1624.     As a simple example, suppose your program asks the user to enter 
  1625.     a number from the keyboard. If he types 77.5, get(0) will return 
  1626.     an atom with numeric value 77.5. gets(0) would return the string 
  1627.     "77.5\n". 
  1628.  
  1629.     get() can read arbitrarily complicated Euphoria objects. You could 
  1630.     have a long sequence of values in braces and separated by commas, 
  1631.     e.g. {23, {49, 57}, 0.5, -1, 99, 'A', "john"}. A single call to 
  1632.     get() will read in this entire sequence and return it's value as 
  1633.     a result. The combination of print() and get() can be used to 
  1634.     save any Euphoria object to disk and later read it back. This 
  1635.     technique could be used to implement a database as one or more 
  1636.     large Euphoria sequences stored in disk files. The sequences 
  1637.     could be read into memory, updated and then written back to disk
  1638.     after each series of transactions is complete. See demo\mydata.ex. 
  1639.  
  1640.     Each call to get() picks up where the previous call left off. For 
  1641.     instance, a series of 5 calls to get() would be needed to read in: 
  1642.     99 5.2 {1,2,3} "Hello" -1.
  1643.  
  1644.  include file.e for the following:
  1645.  i1 = seek(fn, i2)
  1646.     Seek (move) to any byte position in the file fn or to the end of file
  1647.     if i2 is -1. For each open file there is a current byte position
  1648.     that is updated as a result of I/O operations on the file. The 
  1649.     initial file position is 0 for files opened for read, write or update.
  1650.     The initial position is the end of file for files opened for append. 
  1651.     The value returned by seek() is 0 if the seek was successful, and 
  1652.     non-zero if it was unsuccessful. It is possible to seek past the
  1653.     end of a file. In this case undefined bytes will be added to the file
  1654.     to make it long enough for the seek.
  1655.     
  1656.  i = where(fn)
  1657.     This function returns the current byte position in the file fn.
  1658.     This position is updated by reads, writes and seeks on the file.
  1659.     It is the place in the file where the next byte will be read from, 
  1660.     or written to.
  1661.  
  1662.  s = current_dir()
  1663.     Return the name of the current working directory.
  1664.  
  1665.  x = dir(st)
  1666.     Return directory information for the file or directory named by st.
  1667.     If there is no file or directory with this name then -1 is returned.
  1668.     This information is similar to what you would get from the DOS dir
  1669.     command. A sequence is returned where each element is a sequence 
  1670.     that describes one file or subdirectory. For example:
  1671.     {
  1672.        {".",    "d",     0  1994, 1, 18,  9, 30, 02},
  1673.        {"..",   "d",     0  1994, 1, 18,  9, 20, 14},
  1674.        {"fred", "ra", 2350, 1994, 1, 22, 17, 22, 40},
  1675.        {"sub",  "d" ,    0, 1993, 9, 20,  8, 50, 12}
  1676.     } 
  1677.     If st names a directory you will have entries for "." and "..", just 
  1678.     as with the DOS dir command. If st names a file then x will have just 
  1679.     one entry, i.e. length(x) will be 1. Each entry contains the name, 
  1680.     attributes and file size as well as the year, month, day, hour, minute
  1681.     and second of the last modification. The attributes element is a 
  1682.     string sequence containing characters chosen from:
  1683.  
  1684.         d - directory
  1685.         r - read only file
  1686.         h - hidden file
  1687.         s - system file
  1688.         v - volume-id entry
  1689.         a - archive file
  1690.  
  1691.     A normal file without special attributes would just have an empty
  1692.     string, "", in this field. See bin\walkdir.ex for an example that 
  1693.     uses the dir() function.
  1694.         
  1695.  
  1696.  4.6 Mouse Support
  1697.  -----------------
  1698.  
  1699.  include mouse.e  -- required for these routines
  1700.  
  1701.  x = get_mouse()
  1702.     Return the last mouse event in the form {event, x, y}, or return -1 
  1703.     if there has not been a mouse event since the last time get_mouse() 
  1704.     was called. 
  1705.  
  1706.     Constants have been defined in mouse.e for the possible mouse events. 
  1707.     x and y are the coordinates of the mouse pointer at the time that 
  1708.     the event occurred. e.g. {2, 100, 50} would indicate that the 
  1709.     left button was pressed down while the mouse pointer was at position 
  1710.     x=100, y=50 on the screen. get_mouse() returns immediately with 
  1711.     either a -1 or a mouse event. It does not wait for an event to occur.
  1712.     You must check it frequently enough to avoid missing an event. When 
  1713.     the next event occurs, the current event will be lost, if you haven't 
  1714.     read it. In practice it is not hard to catch almost all events, and
  1715.     the ones that are lost are usually lost at a lower level in the 
  1716.     system, beyond the control of your program. Losing a MOVE event is 
  1717.     generally not too serious, as the next MOVE will tell you where the 
  1718.     mouse pointer is. 
  1719.  
  1720.     You can use get_mouse() in most text and graphics modes. (The SVGA 
  1721.     modes may not work fully under MS-DOS).
  1722.  
  1723.  
  1724.  mouse_events(i) 
  1725.     Use this procedure to select the mouse events that you want 
  1726.     get_mouse() to report. For example, mouse_events(LEFT_DOWN+LEFT_UP+
  1727.     RIGHT_DOWN) will restrict get_mouse() to reporting the left button 
  1728.     being pressed down or released, and the right button being pressed 
  1729.     down. All other events will be ignored. By default, get_mouse()
  1730.     will report all events. It is good practice to ignore events that 
  1731.     you are not interested in, particularly the very frequent MOVE event, 
  1732.     in order to reduce the chance that you will miss a significant event.
  1733.     mouse_events() can be called at various stages of the execution of 
  1734.     your program, as the need to detect events changes.
  1735.  
  1736.  
  1737.  mouse_pointer(i)
  1738.     If i is 0 hide the mouse pointer, otherwise turn on the mouse pointer.
  1739.     It may be necessary to hide the mouse pointer temporarily when you
  1740.     update the screen. Multiple calls to hide the pointer will require
  1741.     multiple calls to turn it back on. The first call to either get_mouse()
  1742.     or mouse_events() above, will also turn the pointer on (once).
  1743.  
  1744.  
  1745.  4.7 Operating System 
  1746.  -------------------- 
  1747.  
  1748.  a = time()
  1749.     Return the number of seconds since some fixed point in the past. The 
  1750.     resolution on MS-DOS is about 0.05 seconds. 
  1751.  
  1752.  
  1753.  s = date()
  1754.     Return a sequence with the following information:
  1755.          { year (since 1900),
  1756.            month (January = 1),
  1757.            day (day of month, starting at 1),
  1758.            hour (0 to 23),
  1759.            minute (0 to 59),
  1760.            second (0 to 59),
  1761.            day of  the week (Sunday = 1),
  1762.            day of the year (January 1 = 1) }
  1763.  
  1764.  
  1765.  s = command_line() 
  1766.     Return a sequence of strings, where each string is a word from the 
  1767.     ex command line that started Euphoria. The first word will be the 
  1768.     path to the Euphoria executable. The next word is the name of 
  1769.     your Euphoria .ex file. After that will come any extra words typed 
  1770.     by the user. You can use these words in your program. Euphoria 
  1771.     does not have any command line options.
  1772.  
  1773.  
  1774.  x = getenv(s)
  1775.     Return the value of an environment variable. If the variable is 
  1776.     undefined return -1. e.g.
  1777.  
  1778.         getenv("EUDIR")  -- returns "C:\EUPHORIA" -- or D: etc.
  1779.  
  1780.  
  1781.  system(s, a)
  1782.     Pass a command string s to the operating system command interpreter
  1783.     for execution. The argument a indicates the manner in which to 
  1784.     return from the system call. 
  1785.         value of a      return action
  1786.              0      - restore previous graphics mode
  1787.                   (clears the screen)
  1788.              1      - make a beep sound, wait for a
  1789.                   key press, then restore the
  1790.                   graphics mode
  1791.              2      - do not restore graphics mode
  1792.  
  1793.     Action 2 should only be used when it is known that the system call 
  1794.     will not change the graphics mode. 
  1795.  
  1796.  
  1797.  abort(a)       
  1798.     Abort execution of the program. The argument a is an integer status 
  1799.     value to be returned to the operating system. A value of 0 indicates
  1800.     successful completion of the program. Other values can indicate 
  1801.     various kinds of errors. Euphoria for MS-DOS currently ignores this
  1802.     argument and always returns 0. abort() is useful when a program is 
  1803.     many levels deep in subroutine calls, and execution must end 
  1804.     immediately, perhaps due to a severe error that has been detected.
  1805.  
  1806.  
  1807.  Machine Dependent Routines
  1808.  --------------------------
  1809.  x = machine_func(a, x)
  1810.  machine_proc(a, x) 
  1811.     These routines perform machine-specific operations such as graphics
  1812.     and sound effects. They are meant to be called indirectly via one of
  1813.     the support routines, written in Euphoria. A direct call can cause 
  1814.     a machine exception if done incorrectly. Calls to these routines 
  1815.     may not be portable across all machines and operating systems that
  1816.     Euphoria is implemented on. 
  1817.  
  1818.  
  1819.  
  1820.  4.8 Debugging
  1821.  -------------
  1822.  
  1823.  trace(x) 
  1824.     If x is 1 then turn on full-screen statement tracing. If x is 0 then
  1825.     turn off tracing. Tracing only occurs in subroutines that were 
  1826.     compiled with trace. See the section on Debugging.
  1827.  
  1828.  
  1829.  
  1830.  4.9 Graphics & Sound  
  1831.  --------------------
  1832.  
  1833.  clear_screen()
  1834.     Clear the screen using the current background color. 
  1835.  
  1836.  
  1837.  position(a1, a2)
  1838.     Set the cursor to line a1, column a2, where the top left corner 
  1839.     is position(1,1). 
  1840.  
  1841.  
  1842.  include graphics.e    -- for the following routines:
  1843.  
  1844.  i1 = graphics_mode(i2)
  1845.     Select graphics mode i2. See graphics.e for a list of valid 
  1846.     graphics modes. If successful, i1 is set to 0, otherwise i1
  1847.     is set to 1.
  1848.  
  1849.  
  1850.  s = video_config()
  1851.     Return a sequence of values describing the current video
  1852.     configuration:
  1853.     {color monitor?, graphics mode,  text rows, text columns,
  1854.     xpixels, ypixels, number of colors}
  1855.  
  1856.  
  1857.  scroll(i)
  1858.     Scroll the screen up (i positive) or down (i negative) by i lines.
  1859.  
  1860.  
  1861.  wrap(i)
  1862.     Allow text to wrap at right margin (i = 1) or get truncated (i = 0).
  1863.  
  1864.  
  1865.  cursor(i)
  1866.     Select a style of cursor. See graphics.e for some choices.
  1867.  
  1868.  
  1869.  text_color(i)
  1870.     Set the foreground text color. Add 16 to get blinking text
  1871.     in some modes. See graphics.e for a list of possible colors.
  1872.  
  1873.  
  1874.  bk_color(i)
  1875.     Set the background color - text or graphics modes.
  1876.  
  1877.  
  1878.  x = palette(i, s)
  1879.     Change the color for color number i to s, where s is a sequence of 
  1880.     color intensities: {red, green, blue}. Each value in s can be from 
  1881.     0 to 63. If successful, a 3-element sequence containing the 
  1882.     previous color for i will be returned, and all pixels on the screen
  1883.     with value i will be set to the new color. If unsuccessful, the 
  1884.     atom -1 will be returned. 
  1885.  
  1886.  
  1887.  i2 = text_rows(i1)
  1888.     Set the number of text rows on the screen to i1 if possible.
  1889.     i2 will be set to the actual new number of rows.
  1890.  
  1891.  
  1892.  pixel(i, s) 
  1893.     Set a pixel using color i at point s, where s is a 2-element screen 
  1894.     coordinate {x, y}.
  1895.  
  1896.  
  1897.  i = get_pixel(s)
  1898.     Read the color number for a pixel on the screen at point s,
  1899.     where s is a 2-element screen coordinate {x, y}. -1 is returned
  1900.     for points that are off the screen.
  1901.  
  1902.  
  1903.  draw_line(i, s)
  1904.     Draw a line connecting the points in s, using color i. 
  1905.     Example:
  1906.  
  1907.     draw_line(7, {{100, 100}, {200, 200}, {900, 700}}) 
  1908.  
  1909.     would connect the three points in the sequence using a thin white 
  1910.     line, i.e. a line would be drawn from {100, 100} to {200, 200} and 
  1911.     another line would be drawn from {200, 200} to {900, 700}.
  1912.  
  1913.  
  1914.  polygon(i1, i2, s)
  1915.     Draw a polygon with 3 or more vertices given in s, using a certain 
  1916.     color i1. Fill the area if i2 is 1. Don't fill if i2 is 0. Example:
  1917.  
  1918.     polygon(7, 1, {{100, 100}, {200, 200}, {900, 700}})
  1919.  
  1920.     would make a solid white triangle.
  1921.  
  1922.  
  1923.  ellipse(i1, i2, s1, s2)
  1924.     Draw an ellipse with color i1. The ellipse neatly fits inside
  1925.     the rectangle defined by diagonal points s1 {x1, y1} and s2 
  1926.     {x2, y2}. If the rectangle is a square then the ellipse will be a
  1927.     circle. Fill the ellipse when i2 is 1. Don't fill when i2 is 0. 
  1928.     Example:
  1929.  
  1930.     ellipse(5, 0, {10, 10}, {20, 20})
  1931.  
  1932.     would make a magenta colored circle just fitting inside the square
  1933.     {10, 10}, {10, 20}, {20, 20}, {20, 10}.
  1934.  
  1935.  
  1936.  sound(i)
  1937.     Turn on the PC speaker at the specified frequency. If i is 0 
  1938.     the speaker will be turned off.
  1939.  
  1940.  
  1941.  4.10 Machine Level Interface 
  1942.  ----------------------------
  1943.  
  1944.  With this low-level machine interface you can read and write to memory. You
  1945.  can also set up your own 386+ machine language routines and call them. The 
  1946.  usual guarantee that Euphoria will protect you from machine-level errors 
  1947.  does *not* apply when you use these routines. There are only some simple 
  1948.  checks to catch the use of memory addresses that are negative or zero.
  1949.  (Theoretically address 0 is acceptable, but in practice it is usually an 
  1950.  error so we catch it.)
  1951.  
  1952.  Most Euphoria programmers will never use this interface, but it is 
  1953.  important because it makes it possible to get into every nook and cranny  
  1954.  of the hardware or operating system. For those with very specialized
  1955.  applications this could be crucial. 
  1956.  
  1957.  Machine code routines can be written by hand, or taken from the disassembled
  1958.  output of a compiler for C or some other language. Remember that your
  1959.  machine code will be running in 32-bit protected mode. See demo\callmach.ex 
  1960.  for an example.
  1961.  
  1962.  i = peek(a)
  1963.     Read a single byte value in the range 0 to 255 from machine address a.
  1964.  
  1965.  poke(a, i)
  1966.     Write a single byte value, i, to memory address a. remainder(i, 256)
  1967.     is actually written.
  1968.  
  1969.     Writing to the screen memory with poke() can be much faster than using
  1970.      puts() or printf(), but the programming is more difficult and less 
  1971.     portable. In most cases the speed is not needed. For example, the 
  1972.     Euphoria editor    never uses poke().
  1973.  
  1974.  call(a)
  1975.     Call a machine language routine that starts at location a. This
  1976.     routine must execute a RET instruction #C3 to return control
  1977.     to Euphoria. The routine should save and restore any registers 
  1978.     that it uses. You can allocate a block of memory for the routine
  1979.     and then poke in the bytes of machine code. You might allocate
  1980.     other blocks of memory for data and parameters that the machine
  1981.     code can operate on. The addresses of these blocks could be poked
  1982.     into the machine code. 
  1983.  
  1984.  include machine.e
  1985.  
  1986.  a = allocate(i)
  1987.     Allocate i contiguous bytes of memory. Return the address of the
  1988.     block of memory, or return 0 if the memory can't be allocated.
  1989.  
  1990.  free(a)
  1991.     Free up a previously allocated block of memory by specifying the
  1992.     address of the start of the block, i.e. the address that was
  1993.     returned by allocate(). 
  1994.  
  1995.  s = int_to_bytes(a)
  1996.     Convert an integer into a sequence of 4 bytes. These bytes are in
  1997.     the order expected on the 386+, i.e. least significant byte first.
  1998.     You would use this routine prior to poking the bytes into memory.
  1999.  
  2000.  a = bytes_to_int(s)
  2001.     Convert a sequence of 4 bytes into an integer value. The result could
  2002.     be greater than the integer type allows, so assign it to an atom.
  2003.  
  2004.  
  2005.  
  2006.              --- THE END ---
  2007.  
  2008.