home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / bob / doc / Ref / Built_in next >
Encoding:
Text File  |  1994-12-30  |  25.3 KB  |  696 lines

  1. ArmBob v.2.1 Reference                                GCW 29/11/94
  2.  
  3.        ----------------- BUILT IN NAMES ---------------
  4.  
  5. Operators
  6. ---------
  7.  
  8. Armbob has the following operators.
  9.  
  10. Ternary:
  11.  
  12.    c?yes:no     c an integer. If c is nonzero, the expression 
  13.                 evaluates and returns the value of yes (no is 
  14.                 not evaluated), otherwise it evaluates and returns
  15.                 the value of no (yes is not evaluated).
  16.  
  17. Binary:
  18.  
  19.     x || y      x, y integers. If x is nonzero, the expression 
  20.                 evaluates to 1, without evaluating y. Otherwise 
  21.                 it returns 0 if y is zero and 1 otherwise.
  22.  
  23.     x && y      x, y integers. If x is zero the expression returns
  24.                 0 without evaluating y. Otherwise it returns 0 if 
  25.                 y is zero and 1 otherwise.
  26.  
  27.     x | y       x, y integers. Returns the bitwise OR of x and y.
  28.  
  29.     x ^ y       x, y integers. Returns the bitwise XOR of x and y.
  30.  
  31.     x & y       x, y integers. Returns the bitwise AND of x and y.
  32.  
  33.     x == y      x, y both integers, strings or reals. It returns 0
  34.                 if they are different and 1 if they are equal.
  35.  
  36.     x != y      x, y both integers, strings or reals. It returns 1
  37.                 if they are different and 0 if they are equal.
  38.  
  39.     x < y       x, y both integers, strings or reals. It returns 1
  40.                 if x strictly precedes y, otherwise it returns 0.
  41.  
  42.     x <= y      x, y both integers, strings or reals. It returns 1
  43.                 if x precedes or equals y, otherwise it returns 0.
  44.  
  45.     x >= y      x, y both integers, strings or reals. It returns 1
  46.                 if y precedes or equals x, otherwise it returns 0.
  47.  
  48.     x > y       x, y both integers, strings or reals. It returns 1 if
  49.                 if y strictly precedes x, otherwise it returns 0.
  50.  
  51.     x >> y      x, y both integers. It returns x arithmetically shifted
  52.                 right by y bits.
  53.  
  54.     x << y      If x, y are both integers it returns x shifted left by
  55.                 y bits. If x is an iostream and y a string, it outputs
  56.                 y to x and returns the result x.
  57.  
  58.     x + y       If x, y are both integers or both reals it returns 
  59.                 their sum. If x, y are both strings it returns their 
  60.                 concatenation. If either x or y is a string and the 
  61.                 other argument is an integer, the other argument is 
  62.                 converted to the string consisting of the single 
  63.                 character given by the ASCII code of the bottom 8 bits 
  64.                 and the result is the concatenation of the two strings.
  65.  
  66.     x - y       x, y both integers or both reals. The difference is 
  67.                 returned.
  68.  
  69.     x * y       x, y are reals or integers. The product is returned.
  70.                 The type of the result is real if either x or y is a real
  71.                 and integer otherwise.
  72.  
  73.     x % y       x, y both integers. The remainder is returned.
  74.  
  75.     x / y       If x is an integer, then so must y be and an integer
  76.                 quotient is returned. If x is real y may be either an
  77.                 integer or a real and the real quotient is returned.
  78.  
  79. Unary:
  80.  
  81.     -x          x integer or real. Minus x.
  82.  
  83.     !x          x integer. Returns 0 if x is nonzero and 1 otherwise.
  84.  
  85.     ~x          x integer. Returns the bitwise NOT of x.
  86.  
  87. Binary assignment:
  88.  
  89.     x = y       Assign y to x, and return x.
  90.  
  91.     x += y      Assign x+y to x, and return x.
  92.  
  93.     x -= y      Assign x-y to x, and return x.
  94.  
  95.     x *= y      Assign x*y to x, and return x.
  96.  
  97.     x /= y      Assign x/y to x, and return x.
  98.  
  99.     x %= y      Assign x%y to x, and return x.
  100.  
  101.     x &= y      Assign x&y to x, and return x.
  102.  
  103.     x |= y      Assign x|y to x, and return x.
  104.  
  105.     x ^= y      Assign x^y to x, and return x.
  106.  
  107.     x >>= y     Assign x>>y to x, and return x.
  108.  
  109.     x <<= y     Assign x<<y to x, and return x.
  110.  
  111. Unary assignment (integer expressions only):
  112.  
  113.     x++         Evaluate x in current expression, and then assign
  114.                 x+1 to x.
  115.  
  116.     x--         Evaluate x in current expression, and then assign
  117.                 x-1 to x.
  118.  
  119.     ++x         Assign x+1 to x before further evaluation.
  120.  
  121.     --x         Assign x-1 to x before further evaluation.
  122.  
  123.  
  124. Gazette of built-in values and keywords
  125. ---------------------------------------
  126.  
  127.  
  128. Storage and creation
  129. --------------------
  130.  
  131. $     $(x)       The string stored at address x. The string is 
  132.                  terminated by any character with ASCII code less
  133.                  than 32. The address x should be an integer 
  134.                  divisible by 4.
  135.   .........................................................
  136. $$    $$(x,s)    Place string s at address x. The value of s is
  137.                  returned. 
  138.  
  139.           Example   $$(x,$$(y,s)); stores s at both x and y.
  140.   ............................................................
  141. @     @(s)       The address of the first character of the string s.
  142.                  This address is divisible by 4. Strings are stored
  143.                  as a consecutive array of bytes.
  144.  
  145.           Example   @s = @(s = "hello");
  146.  
  147.                  Note that 
  148.  
  149.                          s = $(@("hello")); 
  150.  
  151.                  is equivalent to s = "hello"; .
  152.  
  153.   .............................................................
  154.  
  155. class          Keyword introducing a class definition. Class
  156.                definitions can only appear at the top level.
  157.                That is to say, they cannot appear inside a
  158.                class definition or a function definition.
  159.                Class definitions must precede the definitions
  160.                of any of their instance objects.
  161.  
  162.       Example    class point
  163.                  { q;   /* a vector of coordinates */ }
  164.  
  165.                We say that the class point has the vector q as its
  166.                data.
  167.  
  168.                Methods for a class can be declared inside the class's
  169.                definition but this is optional. They must be defined 
  170.                outside the class definition.
  171.  
  172.                For example, to add a method to point to read its
  173.                first coordinate, we could have
  174.  
  175.      Example     point::x()
  176.                  { return q[0]; }
  177.  
  178. ::             Thus, method definitions are exactly like function
  179.                definitions except that the class name, followed by ::,
  180.                prefixes the method name. The same method name can be
  181.                used in different classes. That is to say, data and
  182. this           method names are local to a class. The variable 'this'
  183.                can be used in a method to refer to the instance object 
  184.                using the method.
  185.  
  186.      Example     point::translate(x,y)
  187.                  {
  188.                   q[0] += x;
  189.                   q[1] += y;
  190.                   return this;
  191.                  }
  192.  
  193.                To create an instance object of a class a method must
  194.                be defined with the same name as the class.
  195.  
  196.      Example    point::point(x,y)
  197.                 {
  198.                  q = newvector(2);
  199.                  q[0] = x;
  200.                  q[1] = y;
  201.                  return this;
  202.                 }
  203.                 
  204.                A particular point is then created by
  205.  
  206.      Example    vertex = new point(3.5,6.0); 
  207.  
  208. new               using the keyword 'new'.
  209.  
  210.                It can then be shifted by using the 'translate' method
  211.  
  212.      Example    vertex = vertex->translate(1.0,-1.0);
  213.  
  214. ->            The -> operator connects an expression for an instance
  215.               object with a method to produce an actual function.
  216.  
  217.      Example    print("The x-coordinate of vertex is ",vertex->x(),"\n");
  218.  
  219.               Each instance object of a class has its own private copy
  220.               of the class's data, which can only be updated or accessed
  221.               using a  method
  222.              
  223.               A class can inherit the data and methods of another
  224.               class.
  225.  
  226.      Example    class particle::point
  227.                 { v; }
  228.  
  229.                 particle::angular_momentum()
  230.                 { return (q[0]*v[1] - q[1]*v[0]);}
  231.  
  232.               The particle class inherits all the data and methods
  233.               of the point class.  
  234.  
  235. static        Data and methods in a class definition can be declared
  236.               to be static, by placing the word 'static' in front
  237.               of their declaration. Static data and methods are
  238.               shared by all instances of a class, and static methods
  239.               may not have the variable 'this' in their definition.                           A typical use of static variables would be a variable
  240.               to count the number of instance objects of a certain kind.
  241.  
  242.      Example    class foo
  243.                 {
  244.                  static total;
  245.                  ...........
  246.                 } 
  247.  
  248.                 foo::foo(...)
  249.                 {
  250.                  if (typeof(total)) total += 1;
  251.                  else total = 1;
  252.                  ..............
  253.                 }
  254.  
  255.                 foo::current_number()
  256.                 { return total; }
  257.  
  258.   ............................................................... 
  259.  
  260. enum    enum { x1 , ... , xn }   This returns the integer n and assigns
  261.                                  the values 0, 1, ... (n-1) to the n
  262.                      variables x1, ... xn in order. It is useful for
  263.                      hiding explicit indexing.
  264.  
  265.         Example      v = newvector(enum {x,y,z});
  266.                      v[x] = v[y] = v[z] = 0.0;
  267.  
  268.                      employee = enum { name,salary,department };                                     fred = newvector(employee);
  269.                      fred[name] = "Fred";
  270.                      fred[salary] = 20000;
  271.                      fred[department] = Widget_Handling;
  272.  
  273.   .................................................................      
  274.  
  275. gc      gc()         Calling this function forces a garbage collection
  276.                      of the heap. The value nil is returned.
  277.  
  278.   ................................................................
  279.  
  280. in .. put { ... }           in b put { x1 ; .... ; xn;}  
  281.  
  282.                       The expression b denotes either a string or an 
  283.                       integer address expression specifying a buffer into
  284.                       which the integers or strings x1 ... xn are to be
  285.                       put. This is useful for defining windows, icons or
  286.                       menus.
  287.  
  288.   ...................................................................
  289.       
  290. new                   This word is used to create a new instance object
  291.                       of a class. See the section on class.
  292.  
  293.   ....................................................................
  294.  
  295. newstring   newstring(n)    Allocates an array of n bytes in the heap,
  296.                             starting on a word boundary, initializes 
  297.                        them to zero, and returns the string which has 
  298.                        this array as its value.
  299.  
  300.   .....................................................................
  301.  
  302. newvector  newvector(n)    Allocates space in the heap for an n-component
  303.                            vector, whose values are all nil, and returns
  304.                         this vector.
  305.  
  306.   ......................................................................
  307.  
  308. nil                    The single value of type NIL. 
  309.  
  310.   .......................................................................
  311.  
  312. put                    See the section in ... put { ..... }
  313.  
  314.   ......................................................................
  315.  
  316. sizeof   sizeof(s)     Returns the number of characters in a string s,
  317.                        or the number of components of a vector s, as
  318.                        an integer.
  319.  
  320.   ......................................................................
  321.  
  322. static                 Introduces static data or method names in
  323.                        class definitions. See the section on class.
  324.  
  325.   .......................................................................
  326.  
  327. typeof  typeof(x)      Returns the integer code describing the type of x.
  328.                        The type of nil is 0, and all other values
  329.                        return a nonzero value. This is useful for coping
  330.                        with undefined vector components.
  331.  
  332.           Example      a = typeof(a)?a:0;
  333.  
  334.   .....................................................................
  335.  
  336. vector   vector { x1 , ... , xn }  Returns a new vector with components
  337.                                    x1, ... , xn. There must be at least
  338.                         one component and not more than 255 for this
  339.                         construction.
  340.  
  341.   ......................................................................
  342.  
  343. `       `(x)           Returns as an integer the contents of the byte at
  344.                        the address x or in the string x. If s is a string
  345.                        `(s) is the same as s[0].
  346.  
  347.   ......................................................................
  348.  
  349. ``      ``(x,c)        Place byte c at address x, and return c.
  350.  
  351.   ......................................................................
  352.  
  353. £        £(x)         Returns as an integer the contents of the word at 
  354.                       the address x or in the string x.
  355.  
  356.   ......................................................................
  357.  
  358. ££       ££(x,n)      Place integer n as a word at address x, and return n.
  359.  
  360.   .................................................................
  361.  
  362. Condition and repetition
  363. ------------------------
  364.  
  365. break        break;      Jump out of the current structure.
  366.                          It only makes sense within a 'switch',
  367.                          'do'/'repeat', 'for' or 'while' structure.
  368.  
  369.   ...........................................................
  370.  
  371. case       case x:       See the section on 'switch'.
  372.  
  373.   ............................................................
  374.  
  375. continue    continue;   Start the next iteration of the smallest 
  376.                         enclosing 'do'/'repeat', 'for' or 'while'
  377.                         structure.
  378.  
  379.   .............................................................
  380.  
  381. default        See the section on 'switch'.
  382.  
  383.   ..............................................................
  384.  
  385. do .. until/while    do .. until (x);
  386.                      do .. while (x);
  387.  
  388.   Looping construct where the body is executed at least once.
  389.  
  390.   ...............................................................
  391.  
  392. else        Optional part of 'if' construct. See the section on 'if'.
  393.  
  394.   ................................................................
  395.  
  396. for   for(<start>;<terminate>;<action>) ...    iteration construct
  397.  
  398.     Example    for(i = 0; i<sizeof(v); i++)
  399.                   print("v[",i,"] = ",v[i],"\n");
  400.  
  401.   ..................................................................
  402.  
  403. if .. else ..     if (<condition>) ....      conditional construct
  404.  
  405.                   if (<condition>) .... else ....
  406.  
  407.     A condition is regarded as false if it is either the integer 0
  408.     or the value nil. Other values count as true. 
  409.  
  410. FALSE       FALSE is a synonym for 0.
  411.  
  412. TRUE        TRUE  is a synonym for 1.
  413.  
  414.   ...................................................................
  415.        
  416. repeat .. until/while    repeat ....... until <condition>
  417.                          repeat ....... while <condition>
  418.  
  419.          'repeat' is a synonym for 'do'. See the section for 'do'.
  420.          See the section for 'if' for a discussion of conditions.
  421.  
  422.   ...................................................................
  423.  
  424. return    return x;         Return x from a function.
  425.  
  426.           return;           is equivalent to      return nil;
  427.  
  428.      Functions with no return statement return nil.
  429.  
  430.   .....................................................................
  431.  
  432. switch .. case .. default
  433.  
  434.         switch (x)                 If the integer or string value
  435.         {                          x matches a value following a 'case'
  436.          case y:                   the corresponding statements are
  437.            .............           executed. Note that unless one wants
  438.          case z:                   to drop through to the next match
  439.            .............           the statements should be terminated
  440.          ...............           by 'break'. If no matches occur, the
  441.          default:                  statements following 'default:' are
  442.            .............           executed. The 'default' part is
  443.          }                         optional.
  444.  
  445.   ...................................................................
  446.  
  447. while   while (x) ........       while construct. 
  448.  
  449.   .....................................................................
  450.  
  451. Random integers
  452. ---------------
  453.  
  454. rnd   rnd()       Returns a random positive integer.
  455.  
  456.       Example       chance = rnd()%n;
  457.  
  458.   ..............................................................
  459.  
  460. seed  seed(n)      Seeds the random number generator with the integer n
  461.                    and returns nil.
  462.  
  463.       Example        seed(time());
  464.  
  465.   ................................................................
  466.  
  467.  
  468. Input/Output
  469. ------------
  470.  
  471. EOF         EOF          End of file value, -1.
  472.  
  473.   ..........................................................
  474.  
  475. fclose     fclose(fp)    Closes the IOSTREAM fp and returns nil.
  476.  
  477.   ............................................................
  478.  
  479. fopen      fopen(filename,mode)    Opens a new IOSTREAM connected to
  480.                                    the file named by 'filename', in
  481.                   mode 'mode' and returns either nil, if it cannot
  482.                   open the channel, or the IOSTREAM. The mode variable 
  483.                   can take the values "r" (read), "w" (write) and 
  484.                   "a" (append).
  485.  
  486.   ..............................................................
  487.  
  488. getarg  getarg(i)      Return the i-th argument on the command line
  489.                        as a string, if there is one, and nil if there
  490.                        is none. The 0-th argument is the command
  491.                        itself (the program's pathname).
  492.  
  493.         Example    for(i = 1; typeof(arg = getarg(i)); i++)
  494.                          process(arg);
  495.  
  496.   ................................................................
  497.  
  498. getc       getc(fp)      Returns a character or EOF from the
  499.                          IOSTREAM fp.
  500.  
  501.   ...............................................................
  502.                           
  503. input   input()        Returns an input string. Leading spaces are
  504.                        not removed. To input a number use the function val.
  505.  
  506.          Example         print("Input a number.\n? ");
  507.                          x = val(input());
  508.  
  509.                        This should not be used in a  wimp program.
  510.  
  511.   ..................................................................
  512.  
  513. print    print(x1, ... ,xn)  Print the items x1, .... xn and return
  514.                              nil. Newlines are not automatically 
  515.                         appended. The digrams \n, \t, \\ can be used 
  516.                         in strings for newline, tab and \, respectively.
  517.  
  518.                         This should not be used in a  wimp program.
  519.  
  520.  The expression
  521.  
  522.                      fp << s
  523.  
  524.  will output a string s to the iostream fp. The value of this expression
  525.  is fp, and << associates to the left, so that
  526.  
  527.                   fp << s1 << s2 .....
  528.  
  529.  is equivalent to
  530.  
  531.                   fp << (s1 + s2 + ...... )
  532.  
  533.   ....................................................................
  534.  
  535. putc       putc(ch,fp)   Writes the character ch to the IOSTREAM fp.
  536.                          Returns nil.
  537.  
  538.   ................................................................
  539.  
  540. stderr      stderr      Built in IOSTREAM constants.
  541. stdin       stdin
  542. stdout      stdout
  543.  
  544.   ..................................................................
  545.  
  546. Conversion
  547. ----------
  548.  
  549. val     val(s)          Converts as many characters of the string s to 
  550.                         a number as it can. Decimal or hexadecimal 
  551.                         numbers (prefixed by &) are recognized. Note
  552.                         that the hexadecimal digits a,b,c,d,e,f are in
  553.                         lower case. Reals are distinguished by a decimal 
  554.                         point.
  555.  
  556.   .......................................................................
  557.  
  558.  
  559. Real arithmetic functions
  560. -------------------------
  561.  
  562. acos    acos(x)      Arc-cosine
  563. asin    asin(x)      Arc-sine
  564. atan    atan(x)      Arc-tangent
  565. cos     cos(x)       Cosine
  566. exp     exp(x)       Exponential
  567. log     log(x)       Natural Logarithm
  568. sin     sin(x)       Sine
  569. sqrt    sqrt(x)      Square root
  570. tan     tan(x)       Tangent
  571.  
  572. pi      pi           Built in constant.
  573.  
  574. System
  575. ------
  576.  
  577. oscli      oscli(s)      Sends a string s to the command line
  578.                          interpreter. The value nil is returned.
  579.  
  580.   ...................................................................
  581.  
  582. quit     quit(s)       Causes the program to exit with a message
  583.                        string s to stderr and returns nil. In wimp
  584.                        programs, wimp_closedown is called.
  585.  
  586.   ...................................................................
  587.  
  588. start_task  start_task(s)  Takes a command in the string s to start
  589.                            up a child task, and returns its task
  590.                 handle. This works both from BobFiles (i.e. from a 
  591.                 command window) or from BobTasks (i.e. from a taskwindow).
  592.                 This function should only be used outside wimp programs 
  593.                 (i.e. before wimp_init or after wimp_closedown). There are
  594.                 other means of starting a child task from within a wimp 
  595.                 program.
  596.  
  597.   ..................................................................
  598.  
  599. swi    swi(x,r)     x is either the SWI integer or the SWI's name
  600.                     as a string. r is a vector of at least 8 integers.
  601.          SWI x is called with register values given by the components 
  602.          r[0], r[1], ... r[7] and the function returns nil with the returned 
  603.          values of the registers in the components of r. 
  604.  
  605.       Example            getenv()
  606.                          {
  607.                           local r;
  608.                           swi("OS_GetEnv",r = newvector(8));
  609.                           return($(r[0]));
  610.                          }
  611.  
  612.       This function to get the command string is superfluous in view
  613.       of the function getarg, but it illustrates the use of swi.
  614.  
  615.       Note that even though a SWI may pass no values in registers, 
  616.       it is necessary for the second argument of swi to be a vector 
  617.       with at least 8 components.
  618.  
  619.   ...................................................................
  620.  
  621.  
  622. sysvar   sysvar(s)     Returns the string value of the string system
  623.                        variable s, or nil if it does not exist.
  624.  
  625.         Example         print(sysvar("Bob$Path"),"\n");
  626.  
  627.   .....................................................................
  628.  
  629. time  time()          Returns the first 32 bits of the time in 
  630.                       centiseconds since the beginning of the century.
  631.  
  632.        Example        start = time();
  633.                       process();
  634.                       end = time();
  635.                       print("That took ",(end-start)*0.01," seconds\n");
  636.  
  637.   .................................................................
  638.  
  639. wimp_closedown      wimp_closedown()      Programs running as wimp 
  640.                                           tasks must use the function
  641.              wimp_closedown before terminating. It returns nil.
  642.  
  643.   .................................................................
  644.  
  645. wimp_init   wimp_init(vn,name,mesg_list)     vn is 100 times the 
  646.                                              version of RISC OS being 
  647.              used, name is the name of the wimp task, and mesg_list is
  648.              a buffer, given as a string or its integer address, 
  649.              containing the messages, stored as words and terminated 
  650.              by 0, which the task wishes to receive. The function 
  651.              returns the task handle as an integer. After wimp_init
  652.              the following function is useful for factoring wimp
  653.              programs.
  654.  
  655.   Example      event_process(maskptr,buffer,action,user)
  656.                {
  657.                 local r, respond, go_on;
  658.                 r = newvector(8);
  659.                 go_on = TRUE;
  660.                 while(go_on)
  661.                 {
  662.                  r[0] = maskptr[0];
  663.                  r[1] = buffer;
  664.                  swi("Wimp_Poll",r);
  665.                  go_on = (typeof(respond = action[r[0]])== BYTECODE)?
  666.                           respond(buffer,user,maskptr):TRUE;
  667.                  }
  668.                 wimp_closedown();
  669.                }
  670.  
  671.        This function event_process takes as arguments:
  672.  
  673.         maskptr    -  a 1-component vector containing the bit mask
  674.                       for Wimp_Poll. Handler functions can update the
  675.                       mask via their third argument.
  676.         buffer     -  a buffer for wimp messages, passed to handler
  677.                       functions as the first argument.
  678.         action     -  a 20-component vector of handler functions expecting
  679.                       3 arguments, that return zero to cause an exit
  680.                       and a nonzero integer to continue.
  681.         user       -  a user variable to pass on to handler functions
  682.                       as the second argument.
  683.  
  684.        The wimp program can now be controlled by the vector, indexed by
  685.        wimp_poll reason codes, of handler functions.
  686.  
  687.   ..................................................................
  688.  
  689. wimp_report     wimp_report(s)     Display string s in a dialogue 
  690.                                    box and return 0 if no key or
  691.                mouse button is clicked, 1 if the OK button is selected,
  692.                and 2 if the CANCEL button is selected.
  693.  
  694.   
  695.                                 
  696.                ------------- END -------------------