home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / max130.zip / INTRO.DOC < prev    next >
Text File  |  1988-10-30  |  22KB  |  581 lines

  1.     
  2.             Max Version 1.30 - Reference Manual
  3.     
  4.     
  5.     1.0    Introduction
  6.     
  7.          Why should you bother to read about another programming
  8.     language? After all there are hundreds of programming languages.
  9.     Some are general purpose. Some are specialized for a particular
  10.     field or application. You may have even found a favorite, perfect
  11.     in every respect. If not, then here are some reasons for taking
  12.     a closer look at Max:
  13.  
  14.         o  No existing programming language has quite
  15.            captured your approach to programming.
  16.  
  17.         o  You are looking for a simple, block structured
  18.            language with a good interpreter.
  19.  
  20.         o  You want to be able to write powerful programs
  21.            quickly and easily.
  22.  
  23.         o  You don't think that a language needs to
  24.            have a complicated syntax to be powerful.
  25.         
  26.         o  You are looking for a language that can be
  27.            extended and defined by the programmer.
  28.  
  29.         o  You are tired of a language, or its
  30.            creators, telling you how to program.
  31.  
  32.         o  You are interested in programming languages for
  33.            their own sake.
  34.  
  35.         o  You want to design your own language    and incorporate
  36.            into it the best features of other languages.
  37.  
  38.          And there are dozens of other reasons to take a good 
  39.     look at Max. Remember, the source to the Max interpreter is
  40.     available. If there is something you don't like, you can 
  41.     always change it. Now, back to the introduction...
  42.  
  43.          Max is a general purpose programming language that is
  44.     based on C, LISP and the spirit of BASIC. It is not intended
  45.     to replace any of those languages. Instead it is designed to make
  46.     people think about programming languages and to consider how the
  47.     design of the language affects the design and implementation of
  48.     programs written in the language.
  49.     
  50.          Max was developed out of frustration with the syntactic
  51.     inconsistencies and restrictions of modern programming languages.
  52.     It provides the framework for a language and invites extension
  53.     and customization using the basic syntactic element of the function,
  54.     as well as the builtin preprocessor. 
  55.     
  56.          Kernighan and Ritchie, in their guide to the C programming
  57.     language, jokingly suggest that you use the C preprocessor to
  58.     define BEGIN and END and use them instead of curly brackets. In
  59.     Max you are encouraged to do this and anything else that will make
  60.     you more comfortable with the language.
  61.     
  62.          Max is extensible. This means that new flow of control
  63.     constructs can be defined and used just like the builtin ones.
  64.     If you want to create an optimizing case statement, where the
  65.     most frequently occuring cases are dynamically measured and
  66.     tested first, you are free to do so.
  67.     
  68.          Max is not designed to shape programming style. It is
  69.     designed to be shaped by it. An objective of Max is to promote
  70.     programmer creativity and expressiveness through a minimum of
  71.     restrictions.
  72.     
  73.          Max is not designed to perform fast Whetstones or long
  74.     scientific calculations. And this version of Max does have some
  75.     limitations, such as the lack of infix and postfix notation. Max
  76.     is not a dead language. It is evolving. New features will be added
  77.     in a way that is consistent with the framework of the language so
  78.     that there will be a minimum of incompatibility.
  79.         
  80.          Most of all, Max is designed to be powerful, fun and easy
  81.     to use.
  82.     
  83.          This overview of Max assumes that you have some familiarity
  84.     with another programming language, such as C, LISP or BASIC. Max
  85.     combines some of the useful features of each of these languages
  86.     with its own unique characteristics. 
  87.  
  88.          Consider the following Max program. It will give you a
  89.     feeling for Max that will be helpful throughout the remainder
  90.     of the reference manual.
  91.  
  92.         do {
  93.             put ("What is your name? ")
  94.             get (name)
  95.             put ("Hello " name ", the time is " time ())
  96.         } do
  97.  
  98.     The symbols do, put, get and time are functions. The quoted
  99.     words "What is your name? ", "Hello" and ", the time is " are
  100.     literals. The symbols { and ( are argument list initiators.
  101.     The symbols } and ) are argument list terminators. The use of
  102.     parenthesis in put (...), get (...) and time (...) constitutes
  103.     simple block closure. The use of curly brackets in do {...} do
  104.     constitutes named block closure. The symbols and quoted words
  105.     are collectively called tokens.
  106.     
  107.     2.0    Tokens
  108.     
  109.          Tokens in Max can be broken down into six classes. These
  110.     basic classes are functions, argument list initiators, argument
  111.     list terminators, variables, literals and comments. The space, tab,
  112.     comma and newline characters serve as delimiters and are otherwise
  113.     ignored. Parentheses, square brackets, curly brackets and double
  114.     quotes are also delimiters.
  115.     
  116.     2.1    Functions
  117.     
  118.          Every statement in Max is a function. Functions are used
  119.     for arithmetic, input and output, and flow-of-control. There is a
  120.     large library of builtin functions and programs are written by
  121.     defining new functions, called user defined functions. The
  122.     language is completely extensible.
  123.     
  124.          Functions are passed values via an argument list and may
  125.     return a result as a function value. Unlike the C programming
  126.     language, arguments are not evaluated until they are referenced
  127.     in the function. This allows user defined functions to implement
  128.     flow-of-control constructs. The argument list to a flow-of-control
  129.     function constitutes a block. The block may be delimited by three
  130.     different sets of symbols to support different degress of block
  131.     closure.
  132.  
  133.     2.2    Argument List Initiators
  134.     
  135.          A function name is immediately followed by an argument
  136.     list, which may be empty. The argument list is delimited on the
  137.     left by an argument list initiator. The argument list initiator
  138.     is used by Max to decide whether or not the preceding token is a
  139.     function. There are three types of argument list initiators: left
  140.     parenthesis, left square bracket and left curly bracket. See
  141.     section 3.0 on Block Closure for information on the use of
  142.     argument list initiators.
  143.     
  144.     2.3    Argument List Terminators
  145.     
  146.          An argument list is delimited on the right by an
  147.     argument list terminator. There are three types of argument list
  148.     terminators:  right parenthesis, right square bracket and right
  149.     curly bracket. See section 3.0 on Block Closure for information
  150.     on the use of argument list terminators.
  151.     
  152.     2.4    Variables
  153.     
  154.          Values of any elemental type may be stored in a variable.
  155.     This includes character strings, integers and floating point 
  156.     numbers. Aggregate types such as lists and arrays are referenced
  157.     through an aggregate descriptor which may be stored in a variable.
  158.     Variables are defined the first time that a value is stored in
  159.     them. Until then, they return their name as their value, but
  160.     runtime parameters may be selected that cause an error to be
  161.     generated when an uninitialized variable is referenced.
  162.     
  163.     2.5    Literals
  164.     
  165.          A sequence of zero or more characters surrounded by double
  166.     quotes is considered a literal. All imbedded blanks are part of
  167.     the literal. The double quotes that delimit the literal are not
  168.     part of it. Non-printing characters may be included by using the
  169.     backslash (\) escape convention. Valid escape sequences include:
  170.     
  171.         \\ - backslash
  172.         \b - backspace
  173.         \  - continuation (at end of line)
  174.         \f - formfeed
  175.         \n - newline
  176.         \" - quote
  177.         \r - return
  178.         \t - tab
  179.     
  180.          An arbitrary ASCII code may be specified by using \0xnn
  181.     where nn is the hexadecimal value of the ASCII code. Numbers are
  182.     automatically classified as literals and do not need to be
  183.     surrounded by double quotes.
  184.     
  185.     2.6    Comments
  186.     
  187.          A semicolon introduces a comment in Max. The semicolon and
  188.     anything else on the line are ignored. Semicolons in literals are
  189.     not considered to be comment introducers.
  190.     
  191.     3.0    Block Closure
  192.     
  193.          Max provides support for three different levels of block
  194.     closure. See section 10 on the Preprocessor for a list of text
  195.     replacement definitions that may be used to write clear block
  196.     structured programs. 
  197.     
  198.     3.0.1    Simple Block Closure
  199.     
  200.          Simple block closure uses parentheses to delimit the
  201.     argument list or block. They pair just as they do in conventional
  202.     programming languages such as C. An if-then-else construct could
  203.     be written as:
  204.     
  205.         if (boolean-expression
  206.             then (
  207.                 then-function-list
  208.             )
  209.             else (
  210.                 else-function-list
  211.             )
  212.         )
  213.     
  214.          A disadvantage to this type of closure is that it becomes
  215.     difficult for the language processor (or programmer) to accurately
  216.     identify the location of a missing parenthesis.
  217.     
  218.     3.0.2    Named Block Closure
  219.     
  220.          To overcome this problem, Max provides "named block closure"
  221.     which uses curly brackets to delimit the argument list. The right
  222.     curly bracket is followed by the function name that precedes the
  223.     corresponding left bracket. The above example can be rewritten as:
  224.     
  225.         if {boolean-expression
  226.             then (
  227.                 then-function-list
  228.             )
  229.             else (
  230.                 else-function-list
  231.             )
  232.         } if
  233.     
  234.          In this example, the "if" block uses named block closure
  235.     and the "then" and "else" blocks use simple block closure. The
  236.     type of closure used is entirely up to the programmer. The Max
  237.     language processor can detect block structure errors (i.e. missing
  238.     parenthesis) within named blocks delimited by curly brackets.
  239.     There is a tradeoff between the improved ability to detect
  240.     errors using named block closure and the readability of simple
  241.     block closure.
  242.     
  243.     3.0.3    Multiple Block Closure
  244.     
  245.          Max also provides a shortcut to writing all of the right
  246.     parenthesis or right curly brackets. It is called "multiple block
  247.     closure" and is specified using square brackets:
  248.     
  249.         function-name [
  250.             function-list
  251.         ] function-name
  252.     
  253.          A right square bracket, function name sequence pairs with
  254.     the most recent function name, left square bracket sequence of
  255.     the same name and closes all intervening blocks. An example of
  256.     this is:
  257.     
  258.         if [boolean-expression
  259.             then (
  260.                 true-function-list
  261.         ] if
  262.     
  263.          The "then" block is closed by the multiple block closure
  264.     of the "if" block. Both simple and named blocks may be closed by
  265.     right square brackets.
  266.     
  267.     4.0    Data Types
  268.     
  269.          Max supports elemental and aggregate data types.
  270.     
  271.     4.1    Elemental
  272.     
  273.          Elemental data types consist of strings and numbers and
  274.     can be stored in a Max variable.
  275.     
  276.     4.1.1    Strings
  277.     
  278.          A character string consists of a sequence of characters.
  279.     Each character occupies one byte and any combination of eight
  280.     bits is valid. There are numerous builtin functions that
  281.     manipulate character strings. Character strings are automatically
  282.     converted to numbers for use in numeric builtin functions. When
  283.     converting character strings to numbers, the first non-numeric
  284.     character terminates the conversion. If the character string is
  285.     not a number then the result of the conversion is zero.
  286.     
  287.     4.1.2    Numbers
  288.     
  289.          Max supports integer and real numbers and automatically
  290.     converts between them when necessary. The maximum precision for
  291.     integers is approximately 32 bits and the maximum precision for
  292.     real numbers is 52 bits in the mantissa and 11 bits in the
  293.     exponent. Max converts numbers to strings when necessary for
  294.     string builtin functions.
  295.     
  296.     4.2    Aggregate Data
  297.     
  298.          Max supports arrays and lists. They are known as aggregate
  299.     data types and are referenced using an aggregate descriptor. The
  300.     aggregate descriptor is returned by the builtin function that
  301.     allocates the data structure and is then used for all other
  302.     builtin functions that manipulate the data. Aggregate descriptors
  303.     are small integers and may be stored in element variables. Arrays
  304.     and lists can be mapped together so that the data can be
  305.     referenced using either array or list functions.
  306.     
  307.     4.2.1    Arrays
  308.     
  309.          Arrays are explicitly allocated and freed, and are accessed
  310.     by an array descriptor. The size of the array as well as optional
  311.     initializers is specified on allocation. There are two types of
  312.     arrays. Indexed arrays are similar to arrays in other programming
  313.     languages. The elements of an indexed array are referenced by an
  314.     integer index. The other type of array is a keyed array. Elements
  315.     of a keyed array are referenced by a character string key. Keyed
  316.     arrays form a simple associative memory.
  317.     
  318.     4.2.2    Lists
  319.     
  320.          Lists are explicitly allocated and freed and are accessed
  321.     by a list descriptor. Elements may be pushed and popped on the
  322.     head or tail of the list, at the current location, or on a sub
  323.     node. The current location may be set to the head or tail and
  324.     updated by moving it to the previous, next or sub node. Lists
  325.     with sub nodes form binary trees. Lists may be sorted, optionally
  326.     by a user supplied compare function.
  327.     
  328.     4.3    Patterns
  329.     
  330.          A large library of pattern matching functions allows text
  331.     to be searched for patterns. Matches can be assigned to variables
  332.     for subsequent processing. Patterns are explicitly allocated and
  333.     freed and are accessed by a pattern descriptor.
  334.     
  335.     5.0    Storage Types
  336.     
  337.          Elemental data (variables) may be allocated automatic or
  338.     static storage. Aggregates are explicitly allocated static storage.
  339.     Aggregates must be explicitly freed when they are no longer needed.
  340.     
  341.     5.1    Automatic
  342.     
  343.          Unless otherwise defined, all variables in user defined
  344.     functions are automatic. They are allocated storage from a stack
  345.     when the function is entered and the storage is freed when the
  346.     function returns. Storage may also be explicitly allocated using
  347.     the auto builtin function. Storage allocated via auto may be
  348.     explicitly freed. It is automatically freed when the function
  349.     returns. 
  350.     
  351.     5.2    Static
  352.     
  353.          Static storage may be used when a variable must retain
  354.     its contents between calls to a function. It must be explicitly
  355.     allocated using the "static" builtin function. Static storage
  356.     must be explicitly freed when it is no longer needed.
  357.     
  358.     5.3    Shared
  359.     
  360.          Two builtin functions, export and import, may be used to
  361.     share access to automatic and static storage between user defined
  362.     functions. These functions specify the name of the variable and
  363.     optionally the name of the function to export to or import from.
  364.  
  365.     5.4    Transient
  366.  
  367.          Functions are stored in an area of memory called the
  368.     transient area. General purpose functions can be locked in
  369.     the transient area using the tset builtin function. Transient
  370.     functions can be cleared from memory using the tclear builtin
  371.     function. The transient area itself can be subdivided into
  372.     frames using the tpush and tpop builtin functions. Each
  373.     frame can have a locked area and a transient area.
  374.     
  375.     6.0    Identifiers
  376.     
  377.          Function and variable names are known collectively as
  378.     identifiers. There are no reserved keywords, although parameters
  379.     may be set that prevent redefinition of builtin function names
  380.     as user defined functions. 
  381.     
  382.     6.1    Naming Rules
  383.     
  384.          Names may not begin with a digit, a minus sign or a
  385.     decimal point. They may not contain spaces, tabs, commas,
  386.     newlines, parentheses, square brackets, curly brackets or 
  387.     semicolons. 
  388.     
  389.     6.2    Scope
  390.     
  391.          There are two types of user defined functions: external
  392.     and internal. External functions may be called from other external
  393.     functions and can be invoked as commands. Internal user defined
  394.     functions are only visible within the function that defines them.
  395.     
  396.          Variables in user defined functions are only visible within
  397.     the function that defines them and any internal functions. Variables
  398.     defined in internal user defined functions are not visible in the
  399.     containing function.
  400.     
  401.     7.0    Recursion
  402.     
  403.          All external user defined functions in Max may be invoked
  404.     recursively. Internal user defined functions do not support
  405.     recursion.
  406.     
  407.     8.0    Error and Control-C Handling
  408.     
  409.          Errors may be trapped and handled by user defined
  410.     functions. A system error level may also be set. Errors with a
  411.     severity greater than the error level cause a message to be
  412.     printed. The severity of errors is divided into three classes:
  413.     warning, error and fatal.
  414.  
  415.          Control-C may also be trapped and handled by a user 
  416.     defined function. The default action is to terminate the 
  417.     currently executing function and print the step number that
  418.     was interrupted.
  419.     
  420.     8.1    Warnings
  421.     
  422.          Warnings are severity level 0. An error message is printed
  423.     and the function returns -1 to indicate an error occured. The
  424.     containing user defined function, if any, may check the return
  425.     code and is free to continue executing.
  426.     
  427.     8.2    Errors
  428.     
  429.          Errors are severity level 1. An error message is printed
  430.     and the function returns -1 to indicate an error occured. The
  431.     containing user defined function, if any, is terminated.
  432.     
  433.     8.3    Fatal
  434.     
  435.          Fatals are severity level 2. An error message is printed
  436.     and the interpreter is terminated. This type of error cannot be
  437.     caught by an error handler.
  438.     
  439.     9.0    Debugging
  440.     
  441.          Max provides support for step tracing, single step
  442.     execution, stack history display and stack relative variable
  443.     contents display. Only step tracing is available during pattern
  444.     matching. Any Max command may be entered after step completion
  445.     during single step execution.
  446.     
  447.     10.0    Preprocessor
  448.     
  449.          Max uses a source preprocessor that supports text replacement
  450.     and include files. The preprocessor may be disabled. It may also
  451.     be used in an optimized mode where only tokens that begin with
  452.     characters in a specific range, such as A to Z (capitalized),
  453.     are checked for replacement.
  454.  
  455.     10.1    Text Replacement
  456.     
  457.     A preprocessor text replacement statement takes the form:
  458.     
  459.         $replace ("old" "new")
  460.     
  461.     where any subsequent occurence of "old" is replaced by "new".
  462.     Please note that both the "old" and "new" text should be enclosed
  463.     in quotes.
  464.     
  465.          The text replacement feature may be used to define
  466.     structured flow-of-control names to make Max programs more
  467.     readable. The following definitions are useful in writing 
  468.     block structured programs:
  469.     
  470.         $replace ("Function"    "define [")
  471.         $replace ("EndFunction" "] define")
  472.     
  473.         $replace ("Begin"       "(")
  474.         $replace ("End"        ")")
  475.     
  476.         $replace ("While"       "while {")
  477.         $replace ("EndWhile"    "} while")
  478.     
  479.         $replace ("Until"       "until {")
  480.         $replace ("EndUntil"    "} until")
  481.     
  482.         $replace ("For"         "for {")
  483.         $replace ("EndFor"      "} for")
  484.     
  485.         $replace ("Loop"    "loop {")
  486.         $replace ("EndLoop"    "} loop")
  487.      
  488.         $replace ("If"          "if [")
  489.         $replace ("EndIf"       "] if")
  490.     
  491.         $replace ("Then"        "then {")
  492.         $replace ("EndThen"     "} then")
  493.     
  494.         $replace ("Else"        "else {")
  495.         $replace ("EndElse"     "} else")
  496.     
  497.         $replace ("Case"    "case {")
  498.         $replace ("EndCase"    "} case")
  499.     
  500.         $replace ("Select"    "select {")
  501.         $replace ("EndSelect"    "} select")
  502.     
  503.         $replace ("When"    "when {")
  504.         $replace ("EndWhen"    "} when")
  505.     
  506.         $replace ("Default"    "default {")
  507.         $replace ("EndDefault"    "} default")
  508.     
  509.         $replace ("Do"        "do {")
  510.         $replace ("EndDo"    "} do")
  511.     
  512.          These preprocessor replacement specifications may be found
  513.     in the file named block.max in the standard distribution.
  514.  
  515.     10.2    Replacement Redefinition
  516.  
  517.          A symbol may be redefined by subsequent preprocessor replace
  518.     statements. Each one hides the definition of the previous one. The
  519.     previous definition may be restored by using the nreplace directive.
  520.     The syntax for nreplace is:
  521.  
  522.         $nreplace ("Name")
  523.  
  524.     where "Name" is the name of a symbol from a previous replace
  525.     statement. The most recent definition of "Name" is removed and
  526.     any previous definition is visible.
  527.  
  528.     10.3    Text Inclusion
  529.  
  530.          The include preprocessor directive may be used to add text
  531.     from other files to a source file. It is used as follows:
  532.     
  533.         $include ("pathname")
  534.     
  535.     Where "pathname" is the name of the file that you want to include.
  536.     
  537.     11.0    Configuration
  538.     
  539.          The maximum length of function names, variable names, and
  540.     quoted literals may be set as a runtime parameter. The default
  541.     is 128 bytes. The maximum amount of storage allocated for variables
  542.     as well as the maximum size of many other internal data structures
  543.     may be specified by runtime parameters.
  544.     
  545.     11.1    Startup File
  546.     
  547.          When started, Max searches the directories in the PATH
  548.     environment variable for a file called config.max. This file may
  549.     contain values that override defaults for the amount of storage
  550.     allocated to internal data structures. Each line in the file is
  551.     in the same format as a command line parameter. The file may also
  552.     contain the name of Max source files to evaluate during
  553.     initialization. This allows Max to load libraries of user defined
  554.     functions.
  555.     
  556.     11.2    Command Line
  557.     
  558.          Command line parameters may be specified to override values
  559.     in the configuration file. 
  560.     
  561.     12.0    Execution Environment
  562.     
  563.          A choice of integrated or interactive execution environments
  564.     is available with Max.
  565.     
  566.     12.1    Integrated
  567.     
  568.          A multiwindow integrated environment called MaxEdit is
  569.     available. This allows program development to occur in one window
  570.     while program execution and output is monitored in another window
  571.     (though not multitasked under DOS). Any number of tiled (adjacent)
  572.     or piled (overlapping) windows may be created.
  573.     
  574.     12.2    Interactive
  575.     
  576.          If the integrated environment is not used, Max enters an
  577.     interactive mode where lines are read from the keyboard until a     latest replacement definition may be removed and the previous
  578.     complete command function reference is entered, whereupon
  579.     evaluation of the function begins and output is displayed on
  580.     the console.
  581.