home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / basic / fastp12.zip / FASTPARS.DOC < prev    next >
Text File  |  1993-04-21  |  15KB  |  364 lines

  1. =======================================================================
  2.               Fast Parser v1.2
  3.          Copyright (C) 1992, 1993 by Daniel Corbier
  4.             All rights reserved.
  5. =======================================================================
  6.  
  7.  
  8. Introduction
  9. ============
  10.  
  11. Fast Parser is a PowerBASIC library which allows math expressions to be
  12. created and evaluated during runtime.  It supports a number of built-in
  13. functions, operators, numerical notations, and modes.  Fast Parser is
  14. especially designed to run fast inside loops.  Expressions such as the
  15. following can be evaluated:
  16.  
  17. 5.19 + 2.3E+3 * x/(15.8792 + sin(pi)^2-length) + x mod 3
  18.  
  19. (16 xor 27) and 255
  20.  
  21. #h1fff + #haef and #b1010111
  22.  
  23.  
  24. Table of Symbols
  25. ================
  26.  
  27. Symbol   Equivalent   Description                       Example
  28. ------   ----------   -----------                       -------
  29. ( )                   Prioritizes an expression         5*(1+1) = 10
  30.  
  31. ^        **           Raised to the power of            4  ^  5 = 1024
  32. *                     Multiply by                       3  *  6 = 18
  33. /                     Divide by                         9  /  2 = 4.5
  34. %        MOD          Modulo                            7  %  4 = 3
  35. +                     Add                               1  +  1 = 2
  36. -                     Subtract                          9  -  5 = 4
  37.  
  38. >                     Greater than                      9  >  2 = 1
  39. <                     Less than                         7  <  4 = 0
  40. =        ==          Equal test                        5  =  4 = 0
  41. >=       =>          Greater or equal                  3  >= 3 = 1
  42. <=       =<          Less or equal                   $3E  <= 9 = 0
  43. <>       !=          Not Equal                   #b10101 <> 20 = 1
  44.  
  45. NOT                   Bitwise 'not'                     not(15) = -16
  46. AND       &          Bitwise 'and'              #b101 and #h1E = 4
  47. OR        |          Bitwise 'or'                     13 or 6  = 15
  48. XOR              Bitwise 'exclusive or'           9 xor 3  = 10
  49. EQV              Bitwise 'equivalence'            6 eqv 9  = -16
  50. IMP              Bitwise 'implication'            1 imp 5  = -1
  51.  
  52. SIN                   Sine                              sin(pi) = 0
  53. COS                   Cosine                            cos(pi) = -1
  54. TAN                   Tangent                           tan(pi) = 0
  55. ASIN                  Arcsine                           asin(1) = 1.57079632
  56. ACOS                  Arccosine                        acos(-1) = 3.14159265
  57. ATAN      ATN         Arctangent                        atan(0) = 0
  58.  
  59. SINH                  Hyperbolic sine                  sinh(16) = 4443055.26
  60. COSH                  Hyperbolic cosine                cosh(11) = 29937.0708
  61. TANH                  Hyperbolic tangent                tanh(1) = 0.76159415
  62. COTH                  Hyperbolic cotangent              coth(1) = 1.31303528
  63. SECH                  Hyperbolic secant                sech(14) = 1.66305E-6
  64. CSCH                  Hyperbolic cosecant              csch(34) = 3.4278E-15
  65. ASINH                 Hyperbolic arcsine               asinh(2) = 1.44363547
  66. ACOSH                 Hyperbolic arccosine             acosh(9) = 2.88727095
  67. ATANH                 Hyperbolic arctangent           atanh(.5) = 1.09861228
  68. ACOTH                 Hyperbolic arccotangent          acoth(7) = 0.14384103
  69. ASECH                 Hyperbolic arcsecant            asech(.3) = 1.87382024
  70. ACSCH                 Hyperbolic arccosecant           acsch(2) = 0.48121182
  71.  
  72. ABS                   Absolute value                    abs(-8) = 8
  73. EXP                   e to the power of                  exp(3) = 20.0855369
  74. RND                   Random number                      rnd(1) = .968620060
  75. INT                   Truncate to an integer           int(6.8) = 6
  76. EXP2                  2 to the xth power               exp2(17) = 131072
  77. EXP10                 10 to the xth power              exp10(3) = 1000
  78. FACT                  Factorial                        fact(11) = 39916800
  79. LOG2                  Log base 2                       log2(19) = 4.24792751
  80. LOG10                 Log base 10                      log10(7) = .845098040
  81. LOG        LN         Natural log                       log(16) = 2.77258872
  82. SGN        SIGN       Sign of expression                sgn(-9) = -1
  83. SQR        SQRT       Square root                       sqr(64) = 8
  84.  
  85.  
  86. Order of Precedence
  87. ===================
  88.  
  89. Here is the precedence list from highest to lowest priority:
  90.  
  91. Anything inside parenthesis is performed first  ( )
  92. Exponentiation                    ^
  93. Multiplication, division                        *, /
  94. Modulo                                          MOD
  95. Addition, subtraction                           +, -
  96. Relational operators                            <, >, >=, <=, =, <>
  97. AND
  98. OR, XOR (exclusive or)
  99. EQV (equivalence)
  100. IMP (implication)
  101.  
  102.  
  103. Numerical Notations
  104. ===================
  105.  
  106. The default numerical type is DECIMAL.  Binary, octal, and
  107. hexadecimal number types are also supported.  The latter types
  108. must be preceded by the  #  (number sign) symbol, and one of
  109. the letters "h", "b", or "o", for hexadecimal, binary, or octal
  110. in that respective order.  The $ sign for hexadecimal notation
  111. can be used as a shortcut.
  112.  
  113. Exponential notation is also supported.  These are numbers followed
  114. by the letter E, and an exponent number.  For instance:  3.4E+5
  115. translates to 3.4*10^5, and 3.4E-5 translates to 3.4*10^(-5).
  116.  
  117. Examples:
  118. ---------
  119.  
  120. #b110101110,   #o656,   #h1AE,   $1AE,   430,   4.3E2
  121. are all the same number.
  122.  
  123. #b10101^2 * 5/$1EF + sin(5.8+2)*cos(#o302)-7E6  is a valid expression.
  124.  
  125.  
  126. Library Elements
  127. ================
  128.  
  129. Eval##( Expr$ )        Function
  130.     This evaluates an expression in one step.  It may be preferable
  131.     to use Evaluate##() for speed when inside a time-critical loop
  132.     where a variable in the expression changes values.  See the
  133.     Technical Notes for details on speed differences.
  134.  
  135. VarName$()        Array
  136.     This array stores the names of variables you want Fast Parser to
  137.     recognize.  The variable names should follow the PB convention for
  138.     naming variables (except they do not need to be followed by numeric
  139.     type identifiers).  You may have up to 200 unique variable names
  140.     (subscripts 0 to 199).  It is not case-sensitive.  When assigning
  141.     variable names to the array VarName$(), make sure you start with
  142.     subscript 0, and add names consecutively.
  143.  
  144. VarValue##()        Array
  145.     This array stores the values that correspond to the variable
  146.     names in VarName$().  VarValue##(0) matches VarName$(0),
  147.     VarValue##(1) matches VarName$(1), etc...  These values can
  148.     be changed at any time.
  149.  
  150. EqParse%( Expr$ )    Function
  151.     This function parses the expression in Expr$, and returns a
  152.     pointer for that expression.  The returned value is the parameter
  153.     you should pass to the Evaluate##() function.  This method is more
  154.     efficient in time-critical loops where variables are being changed.
  155.     EqParse%() is usually outside the loop, and Evaluate##() inside the
  156.     loop.  Ex: Ptr% = EqParse%( "3+x^2" )
  157.  
  158. Evaluate##( Ptr% )    Function
  159.     This returns the value for the parsed expression represented
  160.     by Ptr%.  It is generally preceded by changing some values in
  161.     VarValue##(), while inside a loop.  The difference between
  162.     Evaluate##() and Eval##() is mainly in speed.  Eval##() normally
  163.     parses an expression every time it is invoked, whereas Evaluate##()
  164.     evaluates an expression which is already parsed, making it much
  165.     faster in a repetitious situation.
  166.  
  167. EqError%        Variable
  168.     This variable stores one of the following error numbers each
  169.     time Eval##() or EqParse%() is invoked:
  170.           0    No error
  171.           1    Undefined function
  172.           2    Mismatched parenthesis
  173.           3    Undefined variable
  174.           4    Invalid binary number
  175.           5    Invalid octal number
  176.           6    Invalid hexadecimal number
  177.           7    Factorial overflow
  178.           8    (not available)
  179.           9    Invalid expression
  180.       10   Function definition space is full
  181.  
  182. TrigMode( mode )    Sub
  183.     This sub changes the angle mode for trigonometric operations.
  184.       1    Radian  (default)
  185.       2    Degree
  186.           3    Gradient
  187.  
  188. UserFunction( Eq$ )    Sub
  189.      This let's you define functions during runtime.  Only one
  190.      parameter is allowed, and that parameter must be "x".  For
  191.      instance, if you enter the following lines, then the cot()
  192.      and the inv() functions will be recognized in subsequent
  193.      expressions just like any other built-in function:
  194.  
  195.      Call UserFunction( "cot(x) = cos(x) / sin(x)" )
  196.      MyEquation$ = "inv(x) = 1/x"
  197.      Call UserFunction( MyEquation$ )
  198.  
  199. ReleaseLastEq()        Sub
  200.     Each time EqParse%() or UserFunction() is invoked, it adds some
  201.     instructions to a list which may eventually get filled.  Use this
  202.     function to remove the last user function or parsed expression
  203.     from memory.
  204.  
  205. ReleaseEq()        Sub
  206.     When invoked, all functions defined with UserFunction() and
  207.     expressions parsed with EqParse%() are freed from memory.
  208.     This may be useful if EqError% returns 10.
  209.  
  210. Bin##(n$), Oct##(n$), Hex##(n$)        Functions
  211.     These convert n$ from a binary, octal, or hexadecimal number into
  212.     a decimal number.
  213.  
  214. MatchParenth%( parenthesis, text$ )    Function
  215.     This finds a matching parenthesis.  For instance, if
  216.     text$ = "3+(5/(4+8-(1+.5)*3+2.87)+7)-3"
  217.           ^                 ^
  218.     MatchParenth%(6,text$)        returns 24
  219.  
  220.  
  221. Technical Notes
  222. ===============
  223.  
  224. o  The hex, binary, and octal notations are slightly different than in
  225.    PB.  Instead of the "&" symbol, Fast Parser recognizes the "#" symbol.
  226.    For instance, #b1011 is recognized instead of &b1011.
  227.  
  228. o  Relational operators return a 1 (instead of -1) for true expressions,
  229.    and 0 for false ones.  For instance,  5 > 3 = 1, and 5 < 3 = 0.
  230.  
  231. o  There are two .PBU files.  CALC21.PBU will work with PowerBASIC 2.1,
  232.    and CALC30.PBU will work with PB 3.0.  Change the $LINK statement in
  233.    CALC.INC to set the appropriate .PBU file.
  234.  
  235. o  In order for it to run faster, compile your program with either
  236.    the floating point Procedure option, or NPX option, instead of
  237.    the Emulate option.
  238.  
  239. o  When inside a loop, where a variable in your expression is changing
  240.    values, the program will run faster if you Use EqParse%() along with
  241.    Evaluate##(), instead of Eval##().  However, if you are only dealing
  242.    with one expression inside the loop, the Eval##() function will run
  243.    almost as fast, because the parsed expression is still in memory.
  244.  
  245. o  There is a limit of 128 instructions (like +, -, *, etc...) which can
  246.    be used for defining functions & parsing expressions.  Every time
  247.    UserFunction() or EqParse%() is called, part of the instruction list
  248.    is filled.  If this happens, you can use ReleaseEq to release all
  249.    parsed expressions and user functions from memory.
  250.  
  251.  
  252. Distribution
  253. ============
  254.  
  255. Fast Parser can be freely distributed through BBSs, online services
  256. and other shareware collections.  No charge can be made for Fast Parser.
  257. However, you can charge a modest fee for the media it is stored in and
  258. shipment, as long as it is made clear that the user is not buying Fast
  259. Parser by paying this fee, and it is not placed in a public domain
  260. category.  The following files must all be present and unmodified
  261. when distributed:
  262.  
  263. FASTPARS DOC         Documentation for Fast Parser
  264. CALC     INC         $INCLUDE file for Fast Parser.
  265. CALC21   PBU         Unit file compatible with PowerBASIC 2.1
  266. CALC30   PBU         Unit file compatible with PB 3.0
  267. EVAL     BAS         A sample expression evaluator using Eval##()
  268. XYPLOT   BAS         Plots the 2-D graph of equations given at runtime
  269. 3DPLOT   BAS         Plots 3-D surfaces of equations given at runtime
  270.  
  271.  
  272. Registration
  273. ============
  274.  
  275. Fast Parser v1.2 is free for non-commercial personal use, and for
  276. writing shareware and freeware (excluding public domain) programs,
  277. as long as due credit is given (citing the use of Fast Parser by
  278. Daniel Corbier).  If you want to encourage me to continue writing
  279. shareware & freeware programs, a gift of $10 or any amount would
  280. be greatly appreciated.  It should be a check or money order in US
  281. funds (since it might cost more than $10 to change foreign currency).
  282. You may also mail cash in US dollars.
  283.  
  284. IMPORTANT!!
  285. -----------
  286. Please be advised that the registration options from the previous
  287. version of Fast Parser are no longer supported, and the source code
  288. can no longer be purchased.  Also, please keep in mind that although
  289. Fast Parser v1.2 can be used free of charge, it is NOT public domain.
  290.  
  291.  
  292. Disclaimer
  293. ==========
  294.  
  295. The Fast Parser library is provided on an "as is" basis without warranty
  296. of any kind, expressed or implied.  The person using it bears all risk
  297. as to the quality and performance of the library.  The author will not
  298. be held liable for any special, incidental, consequential, direct or
  299. indirect damages due to any problem caused by this software.
  300.  
  301.  
  302. My Address
  303. ==========
  304.  
  305. Since Fast Parser can be used free of charge, I do NOT plan on
  306. supporting it in any special way.  This simply means that I'm not
  307. obliged to assist anyone in the use of Fast Parser.  However, I would
  308. like to hear suggestions, and know what kind of programs it is used
  309. in.  I can be reached at either of the following addresses:
  310.  
  311. US Mail:  (note that this address is different from last year)
  312.     Daniel Corbier
  313.     571 NE 110th Terrace
  314.     Miami, FL  33161
  315.  
  316. Internet:
  317.     corbier@andrews.edu
  318.  
  319. Fidonet:
  320.     Daniel Corbier at 1:135/110 or 1:135/23
  321.  
  322. Compuserve:
  323.     INTERNET:  corbier@andrews.edu
  324.  
  325. I frequent the following BBSs quite often:
  326.  
  327. Telcom Central   305-828-7909
  328. MACC             305-596-1854
  329. SOX              305-821-3317
  330.  
  331. I occasionally frequent the PowerBASIC BBS.
  332.  
  333. I read the following conferences regularly:
  334.  
  335. Fido:  Shareware, ECPROG, Q-BASIC and more
  336. Internet:  alt.lang.basic, and various msdos & pc newsgroups
  337.  
  338.  
  339. New features in this version include:
  340. =====================================
  341.  
  342. - Functions can be defined at runtime
  343. - Unary + and - are now supported
  344. - More trig functions
  345. - Hyperbolic functions
  346. - Relational & bitwise operators
  347. - Up to 200 variables can be defined
  348. - Exponential notation is accepted (like 5.3E+25)
  349. - Hex, binary, and octal notations
  350. - Support for PB 3.0
  351. - Extended precision
  352.  
  353.  
  354. Ultimate Calculator
  355. ===================
  356.  
  357. If you are interested in seeing a program which uses the Fast Parser
  358. library, please get a copy of Ucalc (Ultimate Calculator) from a BBS
  359. near you.  It has more examples, and will give you a better idea of
  360. how Fast Parser can be used.  The file name is UCALCxx.??? (where
  361. xx is the version, and ??? is ZIP, ARJ, SDN or another extention).
  362. In addition to the features in this library, Ucalc can also solve
  363. equations, and do numerical integrations & summations.
  364.