home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / bc-1.02-bin.lha / man / cat1 / bc.0
Encoding:
Text File  |  1993-12-07  |  41.4 KB  |  1,057 lines

  1.  
  2.  
  3.  
  4. bc(1)                                                       bc(1)
  5.  
  6.  
  7. NNAAMMEE
  8.        bc - An arbitrary precision calculator language
  9.  
  10. SSYYNNTTAAXX
  11.        bbcc [ --llwwss ] [  _f_i_l_e _._._. ]
  12.  
  13. VVEERRSSIIOONN
  14.        This man page documents GNU bc version 1.02.
  15.  
  16. DDEESSCCRRIIPPTTIIOONN
  17.        bbcc is a language that supports arbitrary precision numbers
  18.        with interactive execution of statements.  There are  some
  19.        similarities  in the syntax to the C programming language.
  20.        A standard math  library  is  available  by  command  line
  21.        option.   If requested, the math library is defined before
  22.        processing any files.  bbcc starts by processing  code  from
  23.        all  the  files  listed  on  the command line in the order
  24.        listed.  After all files have  been  processed,  bbcc  reads
  25.        from  the  standard  input.  All code is executed as it is
  26.        read.  (If a file contains a command to halt  the  proces-
  27.        sor, bbcc will never read from the standard input.)
  28.  
  29.        This version of bbcc contains several extensions beyond tra-
  30.        ditional bbcc implementations and the POSIX draft  standard.
  31.        Command line options can cause these extensions to print a
  32.        warning or to be rejected.  This  document  describes  the
  33.        language  accepted  by this processor.  Extensions will be
  34.        identified as such.
  35.  
  36.    OOPPTTIIOONNSS
  37.        -l     Define the standard math library.
  38.  
  39.        -w     Give warnings for extensions to POSIX bbcc.
  40.  
  41.        -s     Process exactly the POSIX bbcc language.
  42.  
  43.    NNUUMMBBEERRSS
  44.        The most basic element in bbcc is the number.   Numbers  are
  45.        arbitrary  precision  numbers.   This precision is both in
  46.        the integer part and the fractional part.  All numbers are
  47.        represented  internally  in decimal and all computation is
  48.        done in decimal.  (This  version  truncates  results  from
  49.        divide and multiply operations.)  There are two attributes
  50.        of numbers, the length and the scale.  The length  is  the
  51.        total number of significant decimal digits in a number and
  52.        the scale is the total number of decimal digits after  the
  53.        decimal point.  For example:
  54.                .000001 has a length of 6 and scale of 6.
  55.                1935.000 has a length of 7 and a scale of 3.
  56.  
  57.    VVAARRIIAABBLLEESS
  58.        Numbers are stored in two types of variables, simple vari-
  59.        ables and arrays.  Both simple variables and  array  vari-
  60.        ables  are  named.   Names begin with a letter followed by
  61.  
  62.  
  63.  
  64.                                 .                               1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. bc(1)                                                       bc(1)
  71.  
  72.  
  73.        any number of letters, digits and underscores.   All  let-
  74.        ters must be lower case.  (Full alpha-numeric names are an
  75.        extension. In POSIX bbcc all names are a single  lower  case
  76.        letter.)   The  type  of  variable is clear by the context
  77.        because all array  variable  names  will  be  followed  by
  78.        brackets ([]).
  79.  
  80.        There are four special variables, ssccaallee,, iibbaassee,, oobbaassee,, and
  81.        llaasstt.  ssccaallee defines how some operations use digits  after
  82.        the decimal point.  The default value of ssccaallee is 0. iibbaassee
  83.        and oobbaassee define the conversion base for input and  output
  84.        numbers.   The  default  for both input and output is base
  85.        10.  llaasstt (an extension) is a variable that has the  value
  86.        of  the  last  printed number.  These will be discussed in
  87.        further detail where appropriate.  All of these  variables
  88.        may  have  values  assigned  to  them  as  well as used in
  89.        expressions.
  90.  
  91.    CCOOMMMMEENNTTSS
  92.        Comments in bbcc start with the characters //** and  end  with
  93.        the characters **//.  Comments may start anywhere and appear
  94.        as a single space in the input.  (This causes comments  to
  95.        delimit other input items.  For example, a comment can not
  96.        be found in the middle  of  a  variable  name.)   Comments
  97.        include  any  newlines (end of line) between the start and
  98.        the end of the comment.
  99.  
  100.    EEXXPPRREESSSSIIOONNSS
  101.        The numbers are manipulated by expressions and statements.
  102.        Since  the language was designed to be interactive, state-
  103.        ments and expressions are executed as  soon  as  possible.
  104.        There  is no "main" program.  Instead, code is executed as
  105.        it is encountered.  (Functions, discussed in detail later,
  106.        are defined when encountered.)
  107.  
  108.        A  simple  expression is just a constant. bbcc converts con-
  109.        stants into internal decimal  numbers  using  the  current
  110.        input  base, specified by the variable iibbaassee. (There is an
  111.        exception in functions.)  The legal values of iibbaassee are  2
  112.        through  16  (F).  Assigning a value outside this range to
  113.        iibbaassee will result in a value of 2 or  16.   Input  numbers
  114.        may  contain  the characters 0-9 and A-F. (Note: They must
  115.        be capitals.  Lower  case  letters  are  variable  names.)
  116.        Single  digit  numbers  always have the value of the digit
  117.        regardless of the value of iibbaassee.  (i.e.  A  =  10.)   For
  118.        multi-digit  numbers,  bbcc changes all input digits greater
  119.        or equal to ibase to the value of iibbaassee-1.  This makes the
  120.        number  FFFFFF  always  be  the largest 3 digit number of the
  121.        input base.
  122.  
  123.        Full expressions are similar to many other high level lan-
  124.        guages.  Since there is only one kind of number, there are
  125.        no rules for mixing types.  Instead, there  are  rules  on
  126.        the  scale  of expressions.  Every expression has a scale.
  127.  
  128.  
  129.  
  130.                                 .                               2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. bc(1)                                                       bc(1)
  137.  
  138.  
  139.        This is derived from the scale of  original  numbers,  the
  140.        operation  performed  and  in many cases, the value of the
  141.        variable ssccaallee. Legal values of the variable ssccaallee  are  0
  142.        to the maximum number representable by a C integer.
  143.  
  144.        In the following descriptions of legal expressions, "expr"
  145.        refers to a complete expression and "var" refers to a sim-
  146.        ple or an array variable.  A simple variable is just a
  147.               _n_a_m_e
  148.        and an array variable is specified as
  149.               _n_a_m_e[_e_x_p_r]
  150.        Unless  specifically  mentioned the scale of the result is
  151.        the maximum scale of the expressions involved.
  152.  
  153.        - expr The result is the negation of the expression.
  154.  
  155.        ++ var The variable is incremented  by  one  and  the  new
  156.               value is the result of the expression.
  157.  
  158.        -- var The  variable  is  decremented  by  one and the new
  159.               value is the result of the expression.
  160.  
  161.        var ++  The result of the expression is the value  of  the
  162.               variable  and  then  the variable is incremented by
  163.               one.
  164.  
  165.        var -- The result of the expression is the  value  of  the
  166.               variable  and  then  the variable is decremented by
  167.               one.
  168.  
  169.        expr + expr
  170.               The result of the expression is the sum of the  two
  171.               expressions.
  172.  
  173.        expr - expr
  174.               The  result  of the expression is the difference of
  175.               the two expressions.
  176.  
  177.        expr * expr
  178.               The result of the expression is the product of  the
  179.               two expressions.
  180.  
  181.        expr / expr
  182.               The result of the expression is the quotient of the
  183.               two expressions.  The scale of the  result  is  the
  184.               value of the variable ssccaallee.
  185.  
  186.        expr % expr
  187.               The result of the expression is the "remainder" and
  188.               it is computed in the following  way.   To  compute
  189.               a%b,  first  a/b is computed to ssccaallee digits.  That
  190.               result is used to compute a-(a/b)*b to the scale of
  191.               the  maximum  of  ssccaallee+scale(b)  and scale(a).  If
  192.               ssccaallee is set  to  zero  and  both  expressions  are
  193.  
  194.  
  195.  
  196.                                 .                               3
  197.  
  198.  
  199.  
  200.  
  201.  
  202. bc(1)                                                       bc(1)
  203.  
  204.  
  205.               integers  this  expression is the integer remainder
  206.               function.
  207.  
  208.        expr ^ expr
  209.               The result of the expression is the  value  of  the
  210.               first  raised  to the second. The second expression
  211.               must be an integer.  (If the second  expression  is
  212.               not  an  integer,  a  warning  is generated and the
  213.               expression is truncated to get an  integer  value.)
  214.               The scale of the result is ssccaallee if the exponent is
  215.               negative.  If the exponent is positive the scale of
  216.               the result is the minimum of the scale of the first
  217.               expression times the value of the exponent and  the
  218.               maximum of ssccaallee and the scale of the first expres-
  219.               sion.   (e.g.  scale(a^b)  =  min(scale(a)*b,  max(
  220.               ssccaallee,, scale(a))).)  It should be noted that expr^0
  221.               will always return the value of 1.
  222.  
  223.        ( expr )
  224.               This alters the standard precedence  to  force  the
  225.               evaluation of the expression.
  226.  
  227.        var = expr
  228.               The  variable  is assigned the value of the expres-
  229.               sion.
  230.  
  231.        var <op>= expr
  232.               This is equivalent to "var = var  <op>  expr"  with
  233.               the exception that the "var" part is evaluated only
  234.               once.  This can make a difference if  "var"  is  an
  235.               array.
  236.  
  237.         Relational  expressions  are a special kind of expression
  238.        that always evaluate to 0 or 1, 0 if the relation is false
  239.        and  1  if  the relation is true.  These may appear in any
  240.        legal expression.   (POSIX  bc  requires  that  relational
  241.        expressions are used only in if, while, and for statements
  242.        and that only one relational test may be  done  in  them.)
  243.        The relational operators are
  244.  
  245.        expr1 < expr2
  246.               The  result  is  1  if  expr1 is strictly less than
  247.               expr2.
  248.  
  249.        expr1 <= expr2
  250.               The result is 1 if expr1 is less than or  equal  to
  251.               expr2.
  252.  
  253.        expr1 > expr2
  254.               The  result  is 1 if expr1 is strictly greater than
  255.               expr2.
  256.  
  257.        expr1 >= expr2
  258.               The result is 1 if expr1 is greater than  or  equal
  259.  
  260.  
  261.  
  262.                                 .                               4
  263.  
  264.  
  265.  
  266.  
  267.  
  268. bc(1)                                                       bc(1)
  269.  
  270.  
  271.               to expr2.
  272.  
  273.        expr1 == expr2
  274.               The result is 1 if expr1 is equal to expr2.
  275.  
  276.        expr1 != expr2
  277.               The result is 1 if expr1 is not equal to expr2.
  278.  
  279.        Boolean  operations  are  also  legal.  (POSIX bbcc does NOT
  280.        have boolean operations). The result of all boolean opera-
  281.        tions  are  0  and 1 (for false and true) as in relational
  282.        expressions.  The boolean operators are:
  283.  
  284.        !expr  The result is 1 if expr is 0.
  285.  
  286.        expr && expr
  287.               The result is 1 if both expressions are non-zero.
  288.  
  289.        expr || expr
  290.               The result is 1 if either expression is non-zero.
  291.  
  292.        The expression precedence is as follows: (lowest to  high-
  293.        est)
  294.               || operator, left associative
  295.               && operator, left associative
  296.               ! operator, nonassociative
  297.               Relational operators, left associative
  298.               Assignment operator, right associative
  299.               + and - operators, left associative
  300.               *, / and % operators, left associative
  301.               ^ operator, right associative
  302.               unary - operator, nonassociative
  303.               ++ and -- operators, nonassociative
  304.  
  305.        This precedence was chosen so that POSIX compliant bbcc pro-
  306.        grams will run correctly. This will cause the use  of  the
  307.        relational  and  logical  operators  to  have some unusual
  308.        behavior when used with assignment expressions.   Consider
  309.        the expression:
  310.               a = 3 < 5
  311.  
  312.        Most  C  programmers  would  assume  this would assign the
  313.        result of "3 < 5" (the value 1) to the variable "a".  What
  314.        this  does in bbcc is assign the value 3 to the variable "a"
  315.        and then compare 3 to 5.  It is best  to  use  parenthesis
  316.        when  using  relational  and  logical  operators  with the
  317.        assignment operators.
  318.  
  319.        There are a few more special expressions that are provided
  320.        in  bbcc.   These have to do with user defined functions and
  321.        standard    functions.      They     all     appear     as
  322.        "_n_a_m_e((_p_a_r_a_m_e_t_e_r_s))".  See the section on functions for user
  323.        defined functions.  The standard functions are:
  324.  
  325.  
  326.  
  327.  
  328.                                 .                               5
  329.  
  330.  
  331.  
  332.  
  333.  
  334. bc(1)                                                       bc(1)
  335.  
  336.  
  337.        length ( expression )
  338.               The value of the length function is the  number  of
  339.               significant digits in the expression.
  340.  
  341.        read ( )
  342.               The read function (an extension) will read a number
  343.               from the standard input, regardless  of  where  the
  344.               function  occurs.   Beware, this can cause problems
  345.               with the mixing of data and program in the standard
  346.               input.  The best use for this function is in a pre-
  347.               viously written program that needs input  from  the
  348.               user,  but  never  allows  program code to be input
  349.               from the user.  The value of the read  function  is
  350.               the  number  read from the standard input using the
  351.               current value of the variable iibbaassee for the conver-
  352.               sion base.
  353.  
  354.        scale ( expression )
  355.               The  value  of  the scale function is the number of
  356.               digits after the decimal point in the expression.
  357.  
  358.        sqrt ( expression )
  359.               The value of the sqrt function is the  square  root
  360.               of  the expression.  If the expression is negative,
  361.               a run time error is generated.
  362.  
  363.    SSTTAATTEEMMEENNTTSS
  364.        Statements (as in most algebraic  languages)  provide  the
  365.        sequencing of expression evaluation.  In bbcc statements are
  366.        executed "as soon as possible."  Execution happens when  a
  367.        newline  in  encountered and there is one or more complete
  368.        statements.  Due to this immediate execution, newlines are
  369.        very important in bbcc. In fact, both a semicolon and a new-
  370.        line are used  as  statement  separators.   An  improperly
  371.        placed  newline  will  cause a syntax error.  Because new-
  372.        lines are statement separators, it is possible to  hide  a
  373.        newline  by  using  the backslash character.  The sequence
  374.        "\<nl>", where <nl>  is  the  newline  appears  to  bbcc  as
  375.        whitespace  instead  of  a newline.  A statement list is a
  376.        series of statements separated by semicolons and newlines.
  377.        The following is a list of bbcc statements and what they do:
  378.        (Things enclosed in brackets ([]) are  optional  parts  of
  379.        the statement.)
  380.  
  381.        expression
  382.               This  statement  does  one  of  two things.  If the
  383.               expression  starts  with  "<variable>  <assignment>
  384.               ...",  it  is considered to be an assignment state-
  385.               ment.  If  the  expression  is  not  an  assignment
  386.               statement,  the expression is evaluated and printed
  387.               to the output.  After the number is printed, a new-
  388.               line  is printed.  For example, "a=1" is an assign-
  389.               ment statement and "(a=1)" is  an  expression  that
  390.               has  an  embedded assignment.  All numbers that are
  391.  
  392.  
  393.  
  394.                                 .                               6
  395.  
  396.  
  397.  
  398.  
  399.  
  400. bc(1)                                                       bc(1)
  401.  
  402.  
  403.               printed are printed in the base  specified  by  the
  404.               variable  oobbaassee.  The  legal values for oobbaassee are 2
  405.               through BC_BASE_MAX.   (See  the  section  LIMITS.)
  406.               For bases 2 through 16, the usual method of writing
  407.               numbers is used.  For bases  greater  than  16,  bbcc
  408.               uses a multi-character digit method of printing the
  409.               numbers where each higher base digit is printed  as
  410.               a  base  10 number.  The multi-character digits are
  411.               separated by spaces.  Each digit contains the  num-
  412.               ber  of  characters  required to represent the base
  413.               ten value of "obase-1".  Since numbers are of arbi-
  414.               trary  precision, some numbers may not be printable
  415.               on a single output line.  These long  numbers  will
  416.               be  split  across  lines  using the "\" as the last
  417.               character on a line.  The maximum number of charac-
  418.               ters  printed  per line is 70.  Due to the interac-
  419.               tive nature of bbcc printing a number cause the  side
  420.               effect  of assigning the printed value the the spe-
  421.               cial variable llaasstt. This allows the user to recover
  422.               the last value printed without having to retype the
  423.               expression that printed the number.   Assigning  to
  424.               llaasstt  is  legal and will overwrite the last printed
  425.               value with the assigned value.  The newly  assigned
  426.               value  will remain until the next number is printed
  427.               or another value is assigned to llaasstt.
  428.  
  429.        string The string is printed to the output.  Strings start
  430.               with a double quote character and contain all char-
  431.               acters until the next double quote character.   All
  432.               characters  are  take literally, including any new-
  433.               line.  No newline character is  printed  after  the
  434.               string.
  435.  
  436.        pprriinntt list
  437.               The print statement (an extension) provides another
  438.               method of output.  The "list" is a list of  strings
  439.               and  expressions  separated by commas.  Each string
  440.               or expression is printed in the order of the  list.
  441.               No terminating newline is printed.  Expressions are
  442.               evaluated and their value is printed  and  assigned
  443.               the  the variable llaasstt. Strings in the print state-
  444.               ment are printed to the output and may contain spe-
  445.               cial characters.  Special characters start with the
  446.               backslash character (\).   The  special  characters
  447.               recognized  by  bbcc are "b" (bell), "f" (form feed),
  448.               "n" (newline), "r" (carriage  return),  "t"  (tab),
  449.               and "\" (backslash).  Any other character following
  450.               the backslash will be ignored.  This still does not
  451.               allow  the double quote character to be part of any
  452.               string.
  453.  
  454.        { statement_list }
  455.               This is the compound statement.  It allows multiple
  456.               statements to be grouped together for execution.
  457.  
  458.  
  459.  
  460.                                 .                               7
  461.  
  462.  
  463.  
  464.  
  465.  
  466. bc(1)                                                       bc(1)
  467.  
  468.  
  469.        iiff ( expression ) tthheenn statement1 [eellssee statement2]
  470.               The  if statement evaluates the expression and exe-
  471.               cutes statement1 or  statement2  depending  on  the
  472.               value of the expression.  If the expression is non-
  473.               zero, statement1 is  executed.   If  statement2  is
  474.               present  and the value of the expression is 0, then
  475.               statement2 is executed.  (The  else  clause  is  an
  476.               extension.)
  477.  
  478.        wwhhiillee ( expression ) statement
  479.               The  while  statement  will  execute  the statement
  480.               while the expression is non-zero.  It evaluates the
  481.               expression  before each execution of the statement.
  482.               Termination of the loop is caused by a zero expres-
  483.               sion value or the execution of a break statement.
  484.  
  485.        ffoorr ( [expression1] ; [expression2] ; [expression3] )
  486.               statement
  487.               The for statement controls  repeated  execution  of
  488.               the statement.  Expression1 is evaluated before the
  489.               loop.  Expression2 is evaluated before each  execu-
  490.               tion  of  the  statement.   If  it is non-zero, the
  491.               statement is evaluated.  If it is zero, the loop is
  492.               terminated.  After each execution of the statement,
  493.               expression3 is evaluated before the reevaluation of
  494.               expression2.   If  expression1  or  expression3 are
  495.               missing, nothing is evaluated  at  the  point  they
  496.               would  be evaluated.  If expression2 is missing, it
  497.               is the same as substituting the value 1 for expres-
  498.               sion2.  (The optional expressions are an extension.
  499.               POSIX bbcc requires all three expressions.)  The fol-
  500.               lowing is equivalent code for the for statement:
  501.               expression1;
  502.               while (expression2) {
  503.                  statement;
  504.                  expression3;
  505.               }
  506.  
  507.        bbrreeaakk  This  statement  causes  a  forced exit of the most
  508.               recent enclosing while statement or for  statement.
  509.  
  510.        ccoonnttiinnuuee
  511.               The  continue  statement (an extension)  causes the
  512.               most recent enclosing for statement  to  start  the
  513.               next iteration.
  514.  
  515.        hhaalltt   The  halt  statement  (an extension) is an executed
  516.               statement that causes the bbcc processor to quit only
  517.               when  it  is  executed.   For example, "if (0 == 1)
  518.               halt" will not cause bbcc to  terminate  because  the
  519.               halt is not executed.
  520.  
  521.        rreettuurrnn Return  the value 0 from a function.  (See the sec-
  522.               tion on functions.)
  523.  
  524.  
  525.  
  526.                                 .                               8
  527.  
  528.  
  529.  
  530.  
  531.  
  532. bc(1)                                                       bc(1)
  533.  
  534.  
  535.        rreettuurrnn ( expression )
  536.               Return the value of the expression from a function.
  537.               (See the section on functions.)
  538.  
  539.    PPSSEEUUDDOO SSTTAATTEEMMEENNTTSS
  540.        These  statements  are  not  statements in the traditional
  541.        sense.  They are not executed statements.  Their  function
  542.        is performed at "compile" time.
  543.  
  544.        lliimmiittss Print  the  local limits enforced by the local ver-
  545.               sion of bbcc.  This is an extension.
  546.  
  547.        qquuiitt   When the quit statement is read, the  bbcc  processor
  548.               is  terminated, regardless of where the quit state-
  549.               ment is found.  For example, "if  (0  ==  1)  quit"
  550.               will cause bbcc to terminate.
  551.  
  552.        wwaarrrraannttyy
  553.               Print  a longer warranty notice.  This is an exten-
  554.               sion.
  555.  
  556.    FFUUNNCCTTIIOONNSS
  557.        Functions provide a method of defining a computation  that
  558.        can  be  executed later.  Functions in bbcc always compute a
  559.        value and return it to the caller.   Function  definitions
  560.        are  "dynamic"  in  the sense that a function is undefined
  561.        until a definition is encountered in the input.  That def-
  562.        inition is then used until another definition function for
  563.        the same name is encountered.   The  new  definition  then
  564.        replaces  the  older definition.  A function is defined as
  565.        follows:
  566.               ddeeffiinnee _n_a_m_e (( _p_a_r_a_m_e_t_e_r_s )) {{ _n_e_w_l_i_n_e
  567.                   _a_u_t_o___l_i_s_t   _s_t_a_t_e_m_e_n_t___l_i_s_t }}
  568.        A  function  call  is  just  an  expression  of  the  form
  569.        "_n_a_m_e((_p_a_r_a_m_e_t_e_r_s))".
  570.  
  571.        Parameters  are  numbers or arrays (an extension).  In the
  572.        function definition, zero or more parameters  are  defined
  573.        by  listing  their names separated by commas.  Numbers are
  574.        only call by value parameters.  Arrays are  only  call  by
  575.        variable.   Arrays  are specified in the parameter defini-
  576.        tion by the notation "_n_a_m_e[[]]".    In  the  function  call,
  577.        actual  parameters are full expressions for number parame-
  578.        ters.  The same notation is used for passing arrays as for
  579.        defining  array  parameters.  The named array is passed by
  580.        variable to the function.  Since function definitions  are
  581.        dynamic,  parameter  numbers  and types are checked when a
  582.        function is called.  Any mismatch in number  or  types  of
  583.        parameters  will  cause  a runtime error.  A runtime error
  584.        will also occur for the call to an undefined function.
  585.  
  586.        The _a_u_t_o___l_i_s_t_i_s _a_n _o_p_t_i_o_n_a_l _l_i_s_t _o_f _v_a_r_i_a_b_l_e_s _t_h_a_t _a_r_e _f_o_r
  587.        _"_l_o_c_a_l_"  _u_s_e_.  _T_h_e _s_y_n_t_a_x _o_f _t_h_e _a_u_t_o _l_i_s_t _(_i_f _p_r_e_s_e_n_t_) _i_s
  588.        _"aauuttoo _n_a_m_e, ... ;".  (The semicolon  is  optional.)   Each
  589.  
  590.  
  591.  
  592.                                 .                               9
  593.  
  594.  
  595.  
  596.  
  597.  
  598. bc(1)                                                       bc(1)
  599.  
  600.  
  601.        _n_a_m_e is the name of an auto variable.  Arrays may be spec-
  602.        ified by using the same notation as  used  in  parameters.
  603.        These  variables  have their values pushed onto a stack at
  604.        the start of the function.  The variables  are  then  ini-
  605.        tialized  to zero and used throughout the execution of the
  606.        function.  At function exit, these variables are popped so
  607.        that the original value (at the time of the function call)
  608.        of these  variables  are  restored.   The  parameters  are
  609.        really auto variables that are initialized to a value pro-
  610.        vided in the function call.  Auto variables are  different
  611.        than traditional local variables in the fact that if func-
  612.        tion A calls function B, B may access  function  A's  auto
  613.        variables  by  just using the same name, unless function B
  614.        has called them auto variables.  Due to the fact that auto
  615.        variables  and parameters are pushed onto a stack, bbcc sup-
  616.        ports recursive functions.
  617.  
  618.        The function body is a  list  of  bbcc  statements.   Again,
  619.        statements   are  separated  by  semicolons  or  newlines.
  620.        Return statements cause the termination of a function  and
  621.        the  return  of  a  value.   There are two versions of the
  622.        return statement.  The first form, "rreettuurrnn",  returns  the
  623.        value  0  to  the  calling  expression.   The second form,
  624.        "rreettuurrnn (( _e_x_p_r_e_s_s_i_o_n ))", computes the value of the expres-
  625.        sion  and  returns  that  value to the calling expression.
  626.        There is an implied "rreettuurrnn ((00))" at the end of every func-
  627.        tion.   This  allows  a function to terminate and return 0
  628.        without an explicit return statement.
  629.  
  630.        Functions also change the usage  of  the  variable  iibbaassee.
  631.        All constants in the function body will be converted using
  632.        the value of iibbaassee at  the  time  of  the  function  call.
  633.        Changes  of  iibbaassee will be ignored during the execution of
  634.        the function except for the standard function rreeaadd,  which
  635.        will  always use the current value of iibbaassee for conversion
  636.        of numbers.
  637.  
  638.    MMAATTHH LLIIBBRRAARRYY
  639.        If bbcc is invoked with the --ll option,  a  math  library  is
  640.        preloaded  and  the default scale is set to 20.   The math
  641.        functions will calculate their results to the scale set at
  642.        the time of their call.  The math library defines the fol-
  643.        lowing functions:
  644.  
  645.        s (_x)  The sine of x in radians.
  646.  
  647.        c (_x)  The cosine of x in radians.
  648.  
  649.        a (_x)  The arctangent of x.
  650.  
  651.        l (_x)  The natural logarithm of x.
  652.  
  653.        e (_x)  The exponential function of raising e to the  value
  654.               x.
  655.  
  656.  
  657.  
  658.                                 .                              10
  659.  
  660.  
  661.  
  662.  
  663.  
  664. bc(1)                                                       bc(1)
  665.  
  666.  
  667.        j (_n_,_x)
  668.               The bessel function of integer order n of x.
  669.  
  670.    EEXXAAMMPPLLEESS
  671.        In  /bin/sh,   the following will assign the value of "pi"
  672.        to the shell variable ppii.
  673.  
  674.               ppii==$$((eecchhoo ""ssccaallee==1100;; 44**aa((11))"" || bbcc --ll))
  675.  
  676.  
  677.        The following is the definition of the  exponential  func-
  678.        tion  used  in the math library.  This function is written
  679.        in POSIX bbcc.
  680.  
  681.               ssccaallee == 2200
  682.  
  683.               //** UUsseess tthhee ffaacctt tthhaatt ee^^xx == ((ee^^((xx//22))))^^22
  684.                  WWhheenn xx iiss ssmmaallll eennoouugghh,, wwee uussee tthhee sseerriieess::
  685.                    ee^^xx == 11 ++ xx ++ xx^^22//22!! ++ xx^^33//33!! ++ ......
  686.               **//
  687.  
  688.               ddeeffiinnee ee((xx)) {{
  689.                 aauuttoo  aa,, dd,, ee,, ff,, ii,, mm,, vv,, zz
  690.  
  691.                 //** CChheecckk tthhee ssiiggnn ooff xx.. **//
  692.                 iiff ((xx<<00)) {{
  693.                   mm == 11
  694.                   xx == --xx
  695.                 }}
  696.  
  697.                 //** PPrreeccoonnddiittiioonn xx.. **//
  698.                 zz == ssccaallee;;
  699.                 ssccaallee == 44 ++ zz ++ ..4444**xx;;
  700.                 wwhhiillee ((xx >> 11)) {{
  701.                   ff ++== 11;;
  702.                   xx //== 22;;
  703.                 }}
  704.  
  705.                 //** IInniittiiaalliizzee tthhee vvaarriiaabblleess.. **//
  706.                 vv == 11++xx
  707.                 aa == xx
  708.                 dd == 11
  709.  
  710.                 ffoorr ((ii==22;; 11;; ii++++)) {{
  711.                   ee == ((aa **== xx)) // ((dd **== ii))
  712.                   iiff ((ee ==== 00)) {{
  713.                     iiff ((ff>>00)) wwhhiillee ((ff----))  vv == vv**vv;;
  714.                     ssccaallee == zz
  715.                     iiff ((mm)) rreettuurrnn ((11//vv));;
  716.                     rreettuurrnn ((vv//11));;
  717.                   }}
  718.                   vv ++== ee
  719.                 }}
  720.               }}
  721.  
  722.  
  723.  
  724.                                 .                              11
  725.  
  726.  
  727.  
  728.  
  729.  
  730. bc(1)                                                       bc(1)
  731.  
  732.  
  733.        The following is code that uses the extended  features  of
  734.        bbcc to implement a simple program for calculating checkbook
  735.        balances.  This program is best kept in a file so that  it
  736.        can  be  used  many  times  without having to retype it at
  737.        every use.
  738.  
  739.               ssccaallee==22
  740.               pprriinntt ""\\nnCChheecckk bbooookk pprrooggrraamm!!\\nn""
  741.               pprriinntt ""  RReemmeemmbbeerr,, ddeeppoossiittss aarree nneeggaattiivvee ttrraannssaaccttiioonnss..\\nn""
  742.               pprriinntt ""  EExxiitt bbyy aa 00 ttrraannssaaccttiioonn..\\nn\\nn""
  743.  
  744.               pprriinntt ""IInniittiiaall bbaallaannccee?? "";; bbaall == rreeaadd(())
  745.               bbaall //== 11
  746.               pprriinntt ""\\nn""
  747.               wwhhiillee ((11)) {{
  748.                 ""ccuurrrreenntt bbaallaannccee == "";; bbaall
  749.                 ""ttrraannssaaccttiioonn?? "";; ttrraannss == rreeaadd(())
  750.                 iiff ((ttrraannss ==== 00)) bbrreeaakk;;
  751.                 bbaall --== ttrraannss
  752.                 bbaall //== 11
  753.               }}
  754.               qquuiitt
  755.  
  756.  
  757.        The following is the definition of the recursive factorial
  758.        function.
  759.  
  760.               ddeeffiinnee ff ((xx)) {{
  761.                 iiff ((xx <<== 11)) rreettuurrnn ((11));;
  762.                 rreettuurrnn ((ff((xx--11)) ** xx));;
  763.               }}
  764.  
  765.  
  766.    DDIIFFFFEERREENNCCEESS
  767.        This   version  of  bbcc  was  implemented  from  the  POSIX
  768.        P1003.2/D11 draft and  contains  several  differences  and
  769.        extensions relative to the draft and traditional implemen-
  770.        tations.  It is not implemented  in  the  traditional  way
  771.        using  _d_c_(_1_)_.   This  version  is  a  single process which
  772.        parses and runs a byte code translation  of  the  program.
  773.        There  is  an  "undocumented"  option (-c) that causes the
  774.        program to output the byte code  to  the  standard  output
  775.        instead  of  running it.  It was mainly used for debugging
  776.        the parser and preparing the math library.
  777.  
  778.        A major source of differences is extensions, where a  fea-
  779.        ture  is extended to add more functionality and additions,
  780.        where new features are added.  The following is  the  list
  781.        of differences and extensions.
  782.  
  783.        LANG   This version does not conform to the POSIX standard
  784.               in the processing of the LANG environment  variable
  785.               and all environment variables starting with LC_.
  786.  
  787.  
  788.  
  789.  
  790.                                 .                              12
  791.  
  792.  
  793.  
  794.  
  795.  
  796. bc(1)                                                       bc(1)
  797.  
  798.  
  799.        names  Traditional  and  POSIX bbcc have single letter names
  800.               for functions, variables  and  arrays.   They  have
  801.               been  extended  to  be  multi-character  names that
  802.               start with a letter and may contain  letters,  num-
  803.               bers and the underscore character.
  804.  
  805.        Strings
  806.               Strings  are not allowed to contain NUL characters.
  807.               POSIX says  all  characters  must  be  included  in
  808.               strings.
  809.  
  810.        last   POSIX  bbcc  does  not  have  a  llaasstt variable.  Some
  811.               implementations of bbcc use the period (.) in a simi-
  812.               lar way.
  813.  
  814.        comparisons
  815.               POSIX  bbcc  allows comparisons only in the if state-
  816.               ment, the while statement, and the  second  expres-
  817.               sion  of  the  for statement.  Also, only one rela-
  818.               tional operation is allowed in each of those state-
  819.               ments.
  820.  
  821.        if statement, else clause
  822.               POSIX bbcc does not have an else clause.
  823.  
  824.        for statement
  825.               POSIX  bbcc requires all expressions to be present in
  826.               the for statement.
  827.  
  828.        &&, ||, !
  829.               POSIX bbcc does not have the logical operators.
  830.  
  831.        read function
  832.               POSIX bbcc does not have a read function.
  833.  
  834.        print statement
  835.               POSIX bbcc does not have a print statement .
  836.  
  837.        continue statement
  838.               POSIX bbcc does not have a continue statement.
  839.  
  840.        array parameters
  841.               POSIX bbcc does not  have  array  parameters.   Other
  842.               implementations  of bbcc may have call by value array
  843.               parameters.
  844.  
  845.        =+, =-, =*, =/, =%, =^
  846.               POSIX bbcc does not require these "old style" assign-
  847.               ment  operators  to  be  defined.  This version may
  848.               allow these "old style" assignments.  Use the  lim-
  849.               its  statement to see if the installed version sup-
  850.               ports them.  If it does  support  the  "old  style"
  851.               assignment  operators,  the statement "a =- 1" will
  852.               decrement aa by 1 instead of setting aa to the  value
  853.  
  854.  
  855.  
  856.                                 .                              13
  857.  
  858.  
  859.  
  860.  
  861.  
  862. bc(1)                                                       bc(1)
  863.  
  864.  
  865.               -1.
  866.  
  867.        spaces in numbers
  868.               Other  implementations  of  bbcc allow spaces in num-
  869.               bers.  For example, "x=1 3" would assign the  value
  870.               13  to  the  variable  x.  The same statement would
  871.               cause a syntax error in this version of bbcc.
  872.  
  873.        errors and execution
  874.               This implementation varies from  other  implementa-
  875.               tions  in  terms of what code will be executed when
  876.               syntax and other errors are found in  the  program.
  877.               If  a  syntax  error is found in a function defini-
  878.               tion, error recovery tries to find the beginning of
  879.               a  statement  and  continue  to parse the function.
  880.               Once a syntax error is found in the  function,  the
  881.               function  will  not  be  callable and becomes unde-
  882.               fined.  Syntax errors in the interactive  execution
  883.               code  will  invalidate the current execution block.
  884.               The execution block is terminated by an end of line
  885.               that  appears  after  a complete sequence of state-
  886.               ments.  For example,
  887.               a = 1
  888.               b = 2
  889.        has two execution blocks and
  890.               { a = 1
  891.                 b = 2 }
  892.        has one execution block.  Any runtime error will terminate
  893.        the  execution  of the current execution block.  A runtime
  894.        warning will not terminate the current execution block.
  895.  
  896.        Interrupts
  897.               During an interactive session,  the  SIGINT  signal
  898.               (usually  generated by the control-C character from
  899.               the terminal) will cause execution of  the  current
  900.               execution block to be interrupted.  It will display
  901.               a "runtime" error  indicating  which  function  was
  902.               interrupted.   After  all  runtime  structures have
  903.               been cleaned up,  a  message  will  be  printed  to
  904.               notify  the  user  that bbcc is ready for more input.
  905.               All previously defined functions remain defined and
  906.               the  value  of all non-auto variables are the value
  907.               at the point of interruption.  All  auto  variables
  908.               and  function  parameters  are  removed  during the
  909.               clean up process.  During  a  non-interactive  ses-
  910.               sion,  the  SIGINT signal will terminate the entire
  911.               run of bbcc.
  912.  
  913.    LLIIMMIITTSS
  914.        The following are the limits currently in place  for  this
  915.        bbcc  processor.   Some  of them may have been changed by an
  916.        installation.  Use the limits statement to see the  actual
  917.        values.
  918.  
  919.  
  920.  
  921.  
  922.                                 .                              14
  923.  
  924.  
  925.  
  926.  
  927.  
  928. bc(1)                                                       bc(1)
  929.  
  930.  
  931.        BC_BASE_MAX
  932.               The  maximum  output  base is currently set at 999.
  933.               The maximum input base is 16.
  934.  
  935.        BC_DIM_MAX
  936.               This is currently an arbitrary limit  of  65535  as
  937.               distributed.  Your installation may be different.
  938.  
  939.        BC_SCALE_MAX
  940.               The  number  of  digits  after the decimal point is
  941.               limited to INT_MAX digits.   Also,  the  number  of
  942.               digits  before  the  decimal  point  is  limited to
  943.               INT_MAX digits.
  944.  
  945.        BC_STRING_MAX
  946.               The limit on the number of characters in  a  string
  947.               is INT_MAX characters.
  948.  
  949.        exponent
  950.               The  value  of  the exponent in the raise operation
  951.               (^) is limited to LONG_MAX.
  952.  
  953.        multiply
  954.               The multiply routine may yield incorrect results if
  955.               a  number has more than LONG_MAX / 90 total digits.
  956.               For 32 bit longs, this number is 23,860,929 digits.
  957.  
  958.        code size
  959.               Each function and the "main" program are limited to
  960.               10240 bytes of compiled byte code each.  This limit
  961.               (BC_MAX_SEGS)  can  be  easily changed to have more
  962.               than 10 segments of 1024 bytes.
  963.  
  964.        variable names
  965.               The current limit on the number of unique names  is
  966.               32767  for  each  of  simple  variables, arrays and
  967.               functions.
  968.  
  969. FFIILLEESS
  970.        In most installations, bbcc  is  completely  self-contained.
  971.        Where  executable  size is of importance or the C compiler
  972.        does not deal with very long strings,  bbcc  will  read  the
  973.        standard      math      library      from     the     file
  974.        /usr/local/lib/libmath.b.  (The actual location may  vary.
  975.        It may be /lib/libmath.b.)
  976.  
  977. DDIIAAGGNNOOSSTTIICCSS
  978.        If any file on the command line can not be opened, bbcc will
  979.        report that the file is unavailable and terminate.   Also,
  980.        there  are compile and run time diagnostics that should be
  981.        self-explanatory.
  982.  
  983. BBUUGGSS
  984.        Error recovery is not very good yet.
  985.  
  986.  
  987.  
  988.                                 .                              15
  989.  
  990.  
  991.  
  992.  
  993.  
  994. bc(1)                                                       bc(1)
  995.  
  996.  
  997. AAUUTTHHOORR
  998.        Philip A. Nelson
  999.        phil@cs.wwu.edu
  1000.  
  1001. AACCKKNNOOWWLLEEDDGGEEMMEENNTTSS
  1002.        The   author   would   like   to   thank   Steve   Sommars
  1003.        (sesv@iwtsf.att.com) for his extensive help in testing the
  1004.        implementation.  Many great suggestions were given.   This
  1005.        is a much better product due to his involvement.
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.                                 .                              16
  1055.  
  1056.  
  1057.