home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / MINIMAN.DOC < prev    next >
Text File  |  1993-07-06  |  13KB  |  318 lines

  1.             --------------------------
  2.             A Mini Manual for Euphoria
  3.             --------------------------
  4.  1. Introduction
  5.  ===============
  6.  The purpose of this mini manual is to give you a very brief summary of
  7.  the Euphoria language so you can start writing small programs.
  8.  You will receive a detailed, 48-page printed Euphoria Reference Manual when
  9.  you purchase the Complete Edition of Euphoria from Rapid Deployment Software.
  10.  
  11.  Besides this mini manual, also take a look at the many demo programs
  12.  written in Euphoria, in the demo directory. The sanity.ex program contains
  13.  a very large variety of statements in Euphoria. There is also the Euphoria
  14.  editor (ed.ex) in the bin directory and others such as grep.ex, lines.ex, etc.
  15.  The file help.e in this directory has a brief syntax summary. To test your
  16.  knowledge about Euphoria, try the learn.ex program in demo\learn.
  17.  
  18.  Once you have installed Euphoria (see install.doc) you will be able to
  19.  use the Euphoria editor, ed, to view or modify files with color syntax
  20.  highlighting. Type:   ed <filename.ex>  (see ed.doc).
  21.  
  22.  To run a Euphoria program type "ex" followed by the name of the main (.ex) 
  23.  file. If the file is not found in the current directory, Euphoria will search 
  24.  your PATH. There are no command line options for ex. You can redirect input 
  25.  and output if the program is written that way.
  26.  
  27.  
  28.  2. The Core Language
  29.  ====================
  30.  2.1 Objects
  31.  -----------
  32.  All data objects in Euphoria are either atoms or sequences. An atom is a
  33.  single numeric value. A sequence is an ordered list of data objects. The
  34.  objects in a sequence can be an arbitrary mix of atoms or sequences.
  35.  Here are some Euphoria objects:
  36.  
  37.      0
  38.      1000
  39.      98.6
  40.      -1e6
  41.      {2, 3, 5, 7, 11, 13, 17, 19}
  42.      {1, 2, {3, 3, 3}, 4, {5, {6}}}
  43.      {{"john", "smith"}, 5552389, 97.25}
  44.      {} -- length 0 sequence
  45.  
  46.  A string such as "ABCD" is just another way of writing {65, 66, 67, 68},
  47.  i.e. the sequence of ASCII codes.
  48.  
  49.  2.2 Expressions
  50.  ---------------
  51.  The following operators can be used in building expressions. Each expression
  52.  results in a Euphoria object as a result:
  53.  
  54.       highest precedence:  function/type calls
  55.                unary- not
  56.                * /
  57.                + -
  58.                &
  59.                < > <= >= = !=
  60.        lowest precedence:  and or
  61.  
  62.  e.g. {1, 2, 3} + 5 results in {6, 7, 8}
  63.  
  64.  Subscripting of Sequences: A single element of a sequence can be selected by
  65.  giving the element number in square brackets. Element numbers start at 1.
  66.  For example, if variable x contains the value {5, 7, 9, 11, 13} then
  67.  x[2] is 7. If we reassign x[2] = {11, 22, 33} then x becomes:
  68.  {5, {11, 22, 33}, 9, 11, 13}
  69.  
  70.  Multiple subscripts can appear on the left or right of the assignment.
  71.  If x is {{1,2,3}, {5, 7, 9}} then x[2][3] is 9.
  72.  
  73.  Slicing of Sequences: Euphoria lets you select a "slice" from a sequence.
  74.  A slice is a sequence of consecutive elements. e.g. if x is
  75.  {1, 1, 2, 2, 2, 1, 1} then x[3..5] is {2, 2, 2}. x[3..3] is {2} and
  76.  x[3..2] is {}.
  77.  
  78.  Concatenation of Sequences: The & operator will concatenate two sequences
  79.  into a longer sequence. e.g. {1, 2, 3} & {4, 5, 6} is {1, 2, 3, 4, 5, 6}.
  80.  Atoms can also be concatenated: 6.5 & 9 is {6.5, 9}. {1, 2, 3} & 4 is
  81.  {1, 2, 3, 4}.
  82.  
  83.  Arithmetic Operations on Sequences: Any binary or unary arithmetic operation
  84.  including any of the built-in math routines can be applied to entire
  85.  sequences as well as to single numbers.
  86.  e.g. {1, 2, 3} + {4, 5, 6} is {5, 7, 9}
  87.       {{1, 2}, {3, 4}, {5}} * {4, 5, 6} is {{4, 8}, {15, 20}, {30}}
  88.  
  89.  
  90.  2.3 Declarations
  91.  ----------------
  92.  Variables are declared by listing a type-name followed by a list of
  93.  variables names. e.g.
  94.  
  95.      integer x, y, z
  96.      atom a
  97.      sequence s1, s2
  98.      object fred, george
  99.  
  100.  The types: integer, atom, sequence and object are predefined. Other types
  101.  can be created by the user by defining a one-parameter type function that
  102.  returns true when a value belongs to the type and false when it does not.
  103.  Variables declared as object can take on any value, (atom or sequence).
  104.  Those declared as atom must be atoms. Those declared as sequence must
  105.  be assigned sequences. Those declared as integer must be assigned atoms that
  106.  are integers in the range of roughly +/- one billion.
  107.  
  108.  Subroutines come in 3 flavours: procedures, functions and types. Procedures
  109.  do not return a value. Functions and types do. Types are just
  110.  one-parameter functions, used in defining new user-defined types for declaring
  111.  variables.
  112.  
  113.  2.4 Statements
  114.  --------------
  115.      The following kinds of statements are provided. Euphoria does
  116.      not use semicolons, but you are free to have one statement per line, 
  117.      many statements per line, or many lines per statement. 
  118.  
  119.      assignment statement:
  120.     Assign a value to a variable, subscripted variable,
  121.     or sliced variable, e.g.
  122.         x[2][4][3..5] = {9, 8, "Hello"}
  123.  
  124.      procedure call:
  125.     Call a procedure, passing arguments by value. e.g.
  126.         foo(x, y, z+2)
  127.     A routine can call itself.
  128.  
  129.      if statement:
  130.     An example of the most general form of if statement is:
  131.         if char = 'a' then
  132.             x = 1
  133.         elsif char = 'b' then
  134.             x = 2
  135.             y = 0
  136.         elsif char = 'c' then
  137.             x = 3
  138.         else
  139.             x = -1
  140.         end if
  141.  
  142.      while statement:
  143.     An example of a while statement is:
  144.         while x > 0 do
  145.             a = a * 2
  146.             x = x - 1
  147.         end while
  148.  
  149.      for statement:
  150.     A for statement sets up a loop with a controlling loop variable
  151.     that runs from an initial value up or down to some final value, e.g.
  152.         for i = 10 to 20 do
  153.             for j = 15 to 1 by -2 do
  154.             ? {i, j}
  155.             end for
  156.         end for
  157.     The loop variable is declared automatically.
  158.  
  159.      return statement:
  160.     A return statement returns from a subroutine. If the subroutine is
  161.     a function or type then a value must also be returned. e.g.
  162.         return {50, "FRED", {}}
  163.  
  164.      exit statement:
  165.     An exit statement exits from a while loop or for loop e.g.
  166.         while 1 do
  167.             line = gets(0)
  168.             if atom(line) then
  169.             exit   -- end of file
  170.             end if
  171.             buffer = append(buffer, line)
  172.         end while
  173.  
  174.      A couple of abbreviations are used. For example, ? 2+2 is short for
  175.      print(1, 2+2), and ! dir is short for system("dir", 0).
  176.  
  177.  2.5 Top-Level Commands
  178.  ----------------------
  179.  Top Level commands are commands that are outside of any subroutine. They
  180.  are immediately processed when encountered in the source file. They are:
  181.  
  182.     include filename - reads in another Euphoria source file at this point
  183.     profile - outputs an execution profile for statements compiled
  184.          "with profile" - see the file ex.pro
  185.     with/without - turns on/off one of the options: profile, trace,
  186.                warning, type_check
  187.  
  188.  In addition, any Euphoria statements or declarations can appear at the top
  189.  level, except for a return statement. They will be immediately executed.
  190.  If you have a main routine, you must explicitly call it to start your program
  191.  running.
  192.  
  193.  3. Debugging
  194.  ============
  195.  As with any language, you can debug by inserting print statements, and this
  196.  is easy with an interpreted language like Euphoria where you don't have to
  197.  recompile/relink. On top of this, Euphoria provides a debug/trace facility
  198.  that is activated by the trace(1) command. First you select the statements
  199.  that you want to be traceable, by placing a "with trace" command in your
  200.  code (outside of any subroutine). You could simply put it at the start of
  201.  your file so everything is traceable. Then you insert a trace(1) statement
  202.  into your code at the point where you want tracing to begin. When the trace(1)
  203.  is executed, your screen is saved and a trace screen appears. Hit Enter to
  204.  single-step through your program, and watch as variable values are updated
  205.  at the bottom of the screen. Hit q to quit tracing. Press F1 then F2 to flip
  206.  to the main screen to view your program's output. Down-Arrow will skip over
  207.  subroutine calls and break out of repetitive loops. ? will let you display a
  208.  particular variable's value. Control-c will abort the program.
  209.  
  210.  4. Built-in Routines
  211.  ====================
  212.  Numerous built-in routines are provided. Some of them are written partially
  213.  or entirely in Euphoria, and you have to include get.e, graphics.e or
  214.  sort.e to use them. The editor shows built-ins in purple (magenta) unless
  215.  they are written in Euphoria. For examples of their usage see sanity.ex or
  216.  the other Euphoria files. Complete details are available in the Euphoria
  217.  Reference Manual.
  218.  
  219.  To indicate what may be passed in and returned the following prefixes
  220.  are used:
  221.     x - a general object (sequence or atom)
  222.     a - an atom
  223.     s - a sequence
  224.     i - an integer
  225.     fn- an integer file number
  226.     st- a string sequence or single-character atom
  227.  
  228.  Math:
  229.     x2 = sqrt(x1)          -- square root
  230.     x2 = rand(x1)          -- random number
  231.     x2 = sin(x1)           -- trig function
  232.     x2 = cos(x1)           -- trig function
  233.     x2 = tan(x1)           -- trig function
  234.     x2 = log(x1)           -- natural log
  235.     x2 = floor(x1)         -- round down to an integer
  236.     x2 = remainder(x1, x2) -- remainder when x1 is divided by x2
  237.     x2 = power(x1, x2)     -- x1 to the power x2
  238.  
  239.  Types:
  240.     i = integer(x)         -- is x an integer?
  241.     i = atom(x)            -- is x an atom?
  242.     i = sequence(x)        -- is x a sequence?
  243.  
  244.  Operations on Sequences:
  245.     i = length(s)          -- length of sequence
  246.     s = repeat(x, a)       -- repeat x a times
  247.     s2 = append(s1, x)     -- append x to end of s1
  248.     s2 = prepend(s1, x)    -- prepend x at beginning of s1
  249.  
  250.  Searching & Sorting:
  251.     i = compare(x1, x2)    -- compare x1 to x2, return +1, -1 or 0
  252.     i = find(x, s)         -- find position of x within s
  253.     i = match(s1, s2)      -- find position of s1 as a slice within s2
  254.     x2 = sort(s)           -- sort a sequence of arbitrary objects
  255.                   [include sort.e and see comments in sort.e]
  256.  
  257.  File I/O:
  258.     fn = open(st1, st2)    -- open file name st1 with mode st2 e.g. "r"
  259.     Some files are opened automatically for you:
  260.         0 standard input
  261.         1 standard output
  262.         2 standard error
  263.     close(fn)              -- close file number fn
  264.     print(fn, x)           -- print value of x to file fn
  265.     printf(fn, st, x)      -- formatted print. st contains %d, %f, %s etc.
  266.                   x is a sequence of values to be formatted
  267.     puts(fn, st)           -- output string st to file fn
  268.     i = getc(fn)           -- get next character from file fn (-1 is EOF)
  269.     x = gets(fn)           -- get next line from file fn (-1 is EOF)
  270.     i = get_key()          -- get key pressed, or return -1
  271.     s = get(fn)            -- read next Euphoria object representation from
  272.                   file fn  [include get.e and see comments 
  273.                   in get.e]
  274.  Mouse Support
  275.     [include mouse.e]
  276.     x = get_mouse()           -- return the last mouse event as {event#, x, y}
  277.                    -- or return -1 if there has not been a
  278.                    -- mouse event since last time you checked
  279.     mouse_events(i)           -- specify which mouse events you want get_mouse
  280.                    -- to report    
  281.  
  282.  Operating System
  283.     a = time()             -- time in seconds since a fixed point
  284.     s = date()             -- {year, month, day, hour, minute, second,
  285.                    day of week, day of year}
  286.     s = command_line()     -- sequence of words from ex command line
  287.     system(s, i)           -- execute a DOS command s, 
  288.                   i = 0 => restore graphics mode
  289.                   i = 1 => beep, wait for key press, then
  290.                        restore graphics mode
  291.                   i = 2 => don't restore graphics mode
  292.     s2 = getenv(s1)        -- return value of an environment variable
  293.  
  294.  Debugging:
  295.     trace(a)               -- turn tracing on (1) or off (0)
  296.  
  297.  Graphics&Sound:
  298.     clear_screen()         -- clear the screen
  299.     position(a1, a2)       -- move cursor to line a1, column a2
  300.  
  301.       for the following, include graphics.e and read the comments and
  302.       code in graphics.e for more details
  303.     graphics_mode(i)       -- choose a graphics mode (see graphics.e)
  304.     s = video_config()     -- {color monitor?, mode, rows, columns,
  305.                    xpixels, ypixels, #colors}
  306.     scroll(i)              -- scroll screen up or down
  307.     wrap(i)                -- wrap or not at right margin
  308.     cursor(i)              -- cursor style
  309.     text_color(i)          -- foreground text color
  310.     bk_color(i)            -- background text or graphics color
  311.     s = palette(i, s)      -- select {red, green, blue} intensity for i
  312.     i2 = text_rows(i1)     -- set number of text rows
  313.     pixel(i, s)            -- set a pixel to color i, s is {x, y}
  314.     draw_line(i1, i2, s)   -- draw a line, s is {{x1, y1}, {x2, y2}}
  315.     polygon(i1, i2, i3, s) -- draw a polygon, s has vertices
  316.     sound(i)               -- turn on speaker, 0 means turn off
  317.  
  318.