The Programming Language DINO: Program Next Previous Contents

8. Program

A Dino program is simply a sequence of statements. There is a special declaration useful for writing programs consisting of several files or for making Dino packages. This is an include-declaration. Before execution of any statements all include-declarations are replaced by files whose base names are given by the strings. It is made recursively, i.e. the files themselves can contain other include-declarations. There should be no infinite recursion in this. If + is present in the include-declaration, the file is inserted in any case. Without + the file is inserted only if it has been yet not inserted into the block of the declaration.

          Program = StmtList

          IncludeDeclaration = include ["+"] STRING ";"

Examples:

The following program outputs the first 24 Fibonachi numbers:

          // Recursive function to compute Fibonacci numbers
          fun fibonacci (n) {
               if (n <= 1) return 1;
               return (fibonacci(n-1) + fibonacci(n-2));
          }
          
          var i, fibnum;
          
          fibnum = 0;
          for (i = 0; i <= 24; i++) {
              fibnum = fibonacci(i);
              putln (i @ " " @ fibnum); 
          }

The following program outputs the number of prime numbers less than 8190:

          var i, prime, k, count, flags;
          var final SieveSize = 8190;

          flags = [SieveSize + 1 : 0];
          count = 0;
          for (i = 0; i <= SieveSize; i++)
            flags[i] = 1;
          for (i = 0; i <= SieveSize; i++)
            if (flags[i]) {
                prime = i + i + 3;
                k = i + prime;
                for (;1;) {
                      if (k > SieveSize)
                        break;
                    flags[k] = 0;
                    k += prime;
                }
                count++;
            }
          println (count);

The following program outputs the number of occurrences of different numbers and identifiers in stdin:

          var i, key, voc = tab[];
          for (;;)
            try {
              var ln, a;

              ln = getln ();
              if (ln == "")
                continue;
              a = split (ln, "[^[:alnum:]]");
              for (i = 0; i < #a; i++)
                voc {a[i]} = (a[i] in voc ? voc [a[i]] + 1 : 1);
            } catch (eof) {
              break;
            }
          fun comp (el1, el2) {
            return cmpv (tolower (el1), tolower (el2));
          }
          key = sort (keys (voc), comp);
          for (i = 0; i < #key; i++)
            putln (key[i], " : ", voc[key[i]]);

The following program uses the Dino package socket:

          include "socket";
          var s, cl, str, l = 0;
          s = sockets.stream_server (10003, 4);
          cl = s.accept ();
          try {
            for (;;) {
               str = cl.read (64); l += #str; cl.write (str);
            }
          } catch (sockets.socket_eof_except) {
            putln ("i got ", l, " bytes");
          }


Next Previous Contents