home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / p / preprss.lbr / BMLP.DZC / BMLP.DOC
Encoding:
Text File  |  1993-10-25  |  12.4 KB  |  320 lines

  1.  
  2. BMLP.DOC
  3.  
  4.        Function
  5.  
  6.          BMLP  expands macro expressions using program resident and/or 
  7.          library file macro definitions.  BMLP  is  written  in  'SSS' 
  8.          language and should be a useful learning tool.  
  9.  
  10.     
  11.        Invocation
  12.  
  13.          Entering 'BMLP' at the DOS prompt will  envoke  the  compiled 
  14.          version  (.EXE)  of BMLP.  The (.BAS) version will have to be 
  15.          run using the interpreter by entering 'BASICA  BMLP'  at  the 
  16.          DOS  prompt.  BMLP  will  then prompt for the input file name 
  17.          and the output file name.  The default  for  the  input  file 
  18.          extension  is '.M',  and the default for the output file name 
  19.          is the input file name with the extension of '.P'.  
  20.  
  21.  
  22.        Formats 
  23.  
  24.             Macro expression format: 
  25.  
  26.                  $macro-name param1,param2,param3 
  27.          
  28.          A leading dollar sign ($) is used to identify  a  macro  name 
  29.          and,  except for spaces and tabs, must be the first character 
  30.          on  the  line.   When  parameters  are  used,  they  must  be 
  31.          separated by a comma, tab, or space;  when there are too many 
  32.          to put on one line,  a double backslash (\\) may be  used  to 
  33.          continue on the next line.  
  34.  
  35.  
  36.             Macro coding format:
  37.  
  38.                  MACRO <macro-name>
  39.                  ENDM
  40.              
  41.          Macros  are  written and maintained with a text editor in one 
  42.          or more library  text  files  and/or  program  source  files.  
  43.          Macro  names may contain any combination of characters except 
  44.          commas,   tabs,   and  spaces,   and   there   is   no   case 
  45.          discrimination.  The  keyword  'MACRO'  is used to identify a 
  46.          macro name and the beginning of code  for  that  macro  name.  
  47.          The  keyword  'ENDM'  identifies  the end of code for a macro 
  48.          name.  Within the macro code a number enclosed with  brackets 
  49.          ([])  identifies  a  parameter request.  A leading semi-colon 
  50.          (;) may be used as a comment character.  Macros may use other 
  51.          macros as  well  as  supporting  subroutines  (more  on  this 
  52.          later).  
  53.  
  54. 
  55.  
  56.        Design
  57.  
  58.  
  59.          Writing macros is easy, fun and can be habit forming once you 
  60.          get  the  hang  of  it.  To demonstrate let's take the simple 
  61.          task of displaying text on the video monitor.  The  following 
  62.          list of operations describes this task in detail: 
  63.          
  64.  
  65.             1. Save the current cursor location.
  66.             2. Turn off and re-position the cursor.
  67.             3. Clear a space the size of the text.
  68.             4. Re-position the cursor.
  69.             5. Display the text.
  70.             6. Restore cursor location and turn it on.
  71.  
  72.  
  73.          BASIC code for this example might be:
  74.  
  75.             100 XR%=CSRLIN:XC%=POS(0)
  76.                 :LOCATE,,0:LOCATE ROW,COL
  77.                 :PRINT SPACE$(LEN(TEXT$));
  78.                 :LOCATE ROW,COL
  79.                 :PRINT TEXT$;
  80.                 :LOCATE XR%,XC%,1
  81.       
  82.          A macro expression for the above example:
  83.       
  84.             $CRT ROW,COL,TEXT$
  85.             /--- -------------\
  86.             Macro Name         Parameter List
  87.       
  88.          Code for the 'CRT' macro could be written like this:
  89.       
  90.             MACRO CRT
  91.                XR%=CSRLIN:XC%=POS(0)
  92.                LOCATE,,0:LOCATE [1],[2],0:PRINT SPACE$(LEN([3]));
  93.                LOCATE [1],[2],0:PRINT [3];:LOCATE XR%,XC%,1
  94.             ENDM
  95. 
  96.  
  97.       
  98.          The  parameters  are  numbered in left to right order as they 
  99.          appear in the list following the 'CRT' macro, e.g.; 
  100.  
  101.  
  102.             [1] = ROW     <parameter one>
  103.             [2] = COL     <parameter two>
  104.             [3] = TEXT$   <parameter three>
  105.  
  106.          Parameters  can  be  passed  using   variables   or   literal 
  107.          expressions: 
  108.  
  109.             $CRT 12,10,"Hello World!"
  110.  
  111.  
  112.  
  113.        Nesting Macros
  114.   
  115.          Because  a  macro  can use other macros (nesting),  the 'CRT' 
  116.          macro can also be written using macros for the  required  six 
  117.          functions.  
  118.  
  119.  
  120.             1. Save the current cursor location. ------+
  121.             |  MACRO SVC                               |
  122.             |      [1]=CSRLIN:[2]=POS(0)               |
  123.             |  ENDM                                    |
  124.             +------------------------------------------+
  125.  
  126.             2. Position the cursor and switch on/off. -+
  127.             |  MACRO XY                                |
  128.             |      LOCATE [1],[2],[3]                  |
  129.             |  ENDM                                    |
  130.             +------------------------------------------+
  131.  
  132.             3. Clear a space the size of the text. ----+
  133.             |  MACRO CLR                               |
  134.             |      PRINT SPACE$([1]);                  |
  135.             |  ENDM                                    |
  136.             +------------------------------------------+
  137.  
  138.             4. Re-position the cursor. ----------------+
  139.             |  (Use the XY macro, (2))                 |
  140.             +------------------------------------------+
  141.  
  142.             5. Display the text. ----------------------+
  143.             |  MACRO SHO                               |
  144.             |      PRINT [1];                          |
  145.             |  ENDM                                    |
  146.             +------------------------------------------+
  147.  
  148.             6. Restore cursor location, turn it on. ---+ 
  149.             |  (Again use the XY macro, (2))           |
  150.             +------------------------------------------+
  151. 
  152.  
  153.          Now the 'CRT' macro can be written using the four new macros:
  154.  
  155.             MACRO CRT                
  156.                 $SVC XR%,XC% 
  157.                 $XY ,,0
  158.                 $XY [1],[2],0
  159.                 $CLR LEN([3])
  160.                 $XY [1],[2],0
  161.                 $SHO [3]
  162.                 $XY XR%,XC%,1
  163.             ENDM
  164.                 
  165.          Writing  macros  for  the  lower level functions of the 'CRT' 
  166.          macro provides  several  benefits,  one  of  them  being  the 
  167.          availability  of  lower  level  macros  to be used in writing 
  168.          other higher level macros.  
  169.  
  170.  
  171.        Conditional Logic
  172.  
  173.             $IF / $ELSE / $END
  174.     
  175.          '$IF-$ELSE-$END' are reserved macro keywords that provide the 
  176.          ability to control the inclusion or expansion of segments  of 
  177.          code  within  a  macro.  The 'CRT' macro can be enhanced with 
  178.          added ability to display the text in reverse video.  This  is 
  179.          easily  implemented  using a fourth parameter and conditional 
  180.          logic to control the inclusion of the "COLOR" statement.  
  181.  
  182.  
  183.             MACRO CRT
  184.                 $SVC XR%,XC%
  185.                 $XY ,,0
  186.                 $XY [1],[2],0
  187.                 $CLR LEN([3])
  188.                 $XY [1],[2],0
  189.                 $IF [4]     <--+  If fourth parameter ([4]) is provided
  190.                 COLOR 0,7      |  then the statement 'COLOR 0,7' will
  191.                 $END        <--+  be included. (Reverse)
  192.                 $SHO [3]
  193.                 $IF [4]     <--+  If fourth parameter ([4]) is provided
  194.                 COLOR 7,0      |  then the statement 'COLOR 7,0' will
  195.                 $END        <--+  be included. (Reset to Normal)
  196.                 $XY XR%,XC%,1
  197.             ENDM
  198. 
  199.  
  200.          Example:
  201.  
  202.             $CRT 12,10,"Hello World!",REV
  203.                                         \..(fourth parameter)
  204.             (Display "Hello World!" at row 12, column 10, in reverse video.)
  205.  
  206.             $CRT 12,10,"Hello World!"
  207.                                         \..(missing fourth parameter)
  208.             (Display "Hello World!" at row 12, column 10, in normal video.)
  209.   
  210.  
  211.  
  212.        Logical Operators
  213.  
  214.          The 'equal-to (=)' and the 'not-equal (#)  or  (<>)'  logical 
  215.          operators  can  be  used  to  test  literal  parameters for a 
  216.          specific value, e.g.; 
  217.       
  218.  
  219.             $IF [1] = 1
  220.             $IF [6] # NOERROR
  221.             $IF [4] = REV
  222.                     \
  223.                     (Operators must to be separated by spaces or tabs.)
  224.  
  225.          'CRT' macro with logical operators: 
  226.  
  227.             MACRO CRT
  228.                 $SVC XR%,XC%
  229.                 $XY ,,0
  230.                 $XY [1],[2],0
  231.                 $CLR LEN([3])
  232.                 $XY [1],[2],0
  233.                 $IF [4] = REV  <--+  If fourth parameter ([4]) EQUALS "REV"
  234.                 COLOR 0,7         |  then the statement 'COLOR 0,7' will
  235.                 $END           <--+  be included. (Reverse)
  236.                 $SHO [3]
  237.                 $IF [4] # NORM <--+  If fourth parameter ([4]) NOT EQUAL "NORM"
  238.                 COLOR 7,0         |  then the statement 'COLOR 7,0' will
  239.                 $END           <--+  be included. (Reset to Normal)
  240.                 $XY XR%,XC%,1
  241.             ENDM
  242.  
  243.          Example:
  244.  
  245.             $CRT 12,10,"Hello World!",REV
  246.                                         \..(fourth parameter)
  247.             (Display "Hello World!" at row 12, column 10, in reverse video.)
  248.  
  249.             $CRT 12,10,"Hello World!",NORM
  250.                                         \..(fourth parameter)
  251.             (Display "Hello World!" at row 12, column 10, in normal video.)
  252. 
  253.  
  254.        Macros and Subroutines
  255.  
  256.          One of the more powerful features of macro programming is the 
  257.          ability to write macros that require supporting  subroutines.  
  258.          This  ability  allows  the use of a macro any number of times 
  259.          within a program,  with only one inclusion of its  supporting 
  260.          subroutine.  Thus, blocks of duplicated code are eliminated.  
  261.  
  262.  
  263.          An example is the operation of stripping leading and trailing 
  264.          space and tab characters from a string of text: 
  265.  
  266.       
  267.          $STRIP TEXT$
  268.       
  269.          The code for 'STRIP' macro:------+
  270.          MACRO STRIP                      |
  271.              X$=[1]:Gosub _Stripit:[1]=X$ |
  272.              $$_STRIP                     |
  273.          ENDM                             |
  274.          ---------------------------------+
  275.        
  276.          The code for '_STRIP' supporting subroutine:----------------+
  277.          MACRO _STRIP                                                |
  278.          proc _Stripit                                               |
  279.              unless LEN(X$)<3                                        |
  280.                  X%=LEN(X$)+1                                        |
  281.                  WHILE(X%>LEN(X$) AND LEN(X$)>2)                     |
  282.                      X%=LEN(X$)                                      |
  283.                      X$=LEFT$(X$,LEN(X$)+(RIGHT$(X$,1)=" "))         |
  284.                      X$=LEFT$(X$,LEN(X$)+(ASC(RIGHT$(X$,1))=9))      |
  285.                      X$=RIGHT$(X$,LEN(X$)+(ASC(X$)=32 OR ASC(X$)=9)) |
  286.                  WEND                                                |
  287.              endu                                                    |
  288.          endp                                                        |
  289.          ENDM                                                        |
  290.          ------------------------------------------------------------+
  291.               
  292.          The  leading  double  dollar sign ($$) identify '_STRIP' as a 
  293.          subroutine which is defined as a procedure (_Stripit) and  it 
  294.          will be included into a program source file only one time.  A 
  295.          subroutine may include and use other subroutines and macros.  
  296.  
  297. 
  298.  
  299.        Developing Libraries
  300.  
  301.          Macros can be written within a  program  source  file,  where 
  302.          they can be easily tested and debugged before being committed 
  303.          to  a  library  file.   Library  documentation  is  extremely 
  304.          important.  After  writing  a  couple  of  dozen  macros,  it 
  305.          becomes  a  little  more  difficult  to remember function and 
  306.          parameter requirements for each macro.  The ability to  share 
  307.          libraries  among  several  programmers  working  on  the same 
  308.          project makes the library documentation essential.  
  309.  
  310.  
  311.        Using Libraries
  312.  
  313.             LIBRARY <file-name.ext>
  314.  
  315.          The keyword 'LIBRARY' is used at the top of a program  source 
  316.          file to identify each macro library to use in processing that 
  317.          source  file.  Libraries can be nested by specifing libraries 
  318.          within libraries.  
  319.  
  320.