home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / flink11.zip / Demo.fpl < prev    next >
Text File  |  1995-05-19  |  11KB  |  393 lines

  1. /***************************************************************************
  2.  
  3.  **   $Filename: demo.FPL $
  4.  **   $Release: 9.0 $
  5.  **   $Date: 94/08/06 $
  6.  **
  7.  **   (C) Copyright 1992 - 1994 by FrexxWare
  8.  **       All Rights Reserved
  9.  
  10. ****************************************************************************
  11.  
  12.      >>>>>> Frexx Programming Language (FPL) demonstration program <<<<<<
  13.                Author: Daniel Stenberg.
  14.  
  15. ****************************************************************************
  16.  
  17.  FPL is an interpreting, library based programming language designed to be
  18.  flexible and easy to insert in any software that needs some kind of
  19.  batch/macro control.
  20.  
  21.  FPL is a complete programming language very similar to C. (Just look at
  22.  this program example!)
  23.  
  24.  FPL.library lets the software programmer define functions that FPL should
  25.  accept. The library will call a function in the software whenever any of
  26.  these functions are discovered.
  27.  
  28.  In this example, the printf() function is a such function. To be able to
  29.  run this example you must use the "src/SFPL" program. (Look at the code in
  30.  caller.c and see how simple it is to implement.) This example is working with
  31.  FPL version 9+.
  32.  
  33.  FPL is freely distributable for non-commercial purposes only, including
  34.  extensive documentation and source code.
  35.  
  36.  ***************************************************************************
  37.  *
  38.  * Prototype all function we'll use:
  39.  */
  40.  
  41. #pragma cache /* pragma using little example! */
  42.  
  43. void strings();
  44. void integers();
  45. void mix_them();
  46. void arrays();
  47. int expressions();
  48. void conditions();
  49. void loops();
  50. void constants();
  51. void locals();
  52. void recursive();
  53. void functions();
  54. export void arg_functions(); /* make this global system-wide */
  55.  
  56. typedef long LONG; /* good old Commodore habit! */
  57.  
  58. LONG label(int);
  59. LONG test(int);
  60.  
  61. LONG setvar(int *);
  62. LONG printint(int);
  63. LONG double2(int a) /* declare before use instead of prototype! */
  64. {
  65.   return a*2;  // with or without parentheses!
  66. }
  67.  
  68. int ninja=1;
  69. export int ninjax=0;
  70. auto loop;
  71.  
  72. /***************************************************************************
  73.  *
  74.  * Main program part coming up:
  75.  */
  76.  
  77. do {
  78.  
  79.   strings();    // How to use strings.
  80.   integers();   // How to use integers.
  81.   mix_them();   // How to mix strings and integers.
  82.   arrays(); // How to use arrays.
  83.   //expressions();// How to write expressions.
  84.   conditions(); // How to check conditions.
  85.   loops();  // How to loop.
  86.   constants();  // How to write constants.
  87.   locals(); // How to use local variables.
  88.   recursive();  // How to recurse.
  89.   functions();  // How to use the functions.
  90.   arg_functions(); // How to send parameters to your own functions.
  91. } while(loop++<10);
  92. exit;
  93.  
  94. /***************************************************************************
  95.  *
  96.  * Here follows a subroutine featuring some of the string expression
  97.  * facilities that FPL includes:
  98.  */
  99.  
  100. void strings()
  101. {
  102.   string a;             /* declaration of a string   */
  103.   string b="Written ";          /* declaration and assign    */
  104.   string c[2];              /* string array declaration  */
  105.   string d="\x65\157";          /* hexadecimal + octal ASCII */
  106.   a="bb";               /* string variable assign    */
  107.   a[1]='y';             /* single character access!  */
  108.   b+=a;                 /* string appending          */
  109.   c[0]={"Daniel",
  110.         "Stenberg"};            /* array assigning           */
  111.  
  112.   printf("%s %s %s ", b, c[0], c[1]);   /* display result            */
  113. }
  114.  
  115. /***************************************************************************
  116.  *
  117.  * Here follows a subroutine featuring some of the integer handlings that
  118.  * can be done:
  119.  */
  120.  
  121. void integers()
  122. {
  123.   int a;                /* integer declaration      */
  124.   int b=200;                /* declaration assign       */
  125.   int c[2]={-25, 1234};         /* array declaration and assign */
  126.   a=10;                 /* integer variable assign      */
  127.   c[0]+={2020, 758};            /* compound array assign    */
  128.  
  129.   printf("%d - %d!\n", c[1], c[0]); /* display the contents of c[0-1] */
  130. }
  131.  
  132. /***************************************************************************
  133.  *
  134.  * Numerical expressions can be converted to strings by using ltostr() or
  135.  * itoa().
  136.  */
  137.  
  138. void mix_them()
  139. {
  140.   string a="Copyright ", c;     /* two string variables */
  141.   int b;                /* one integer variable */
  142.  
  143.   b=a[0];               /* get ASCII code of column 2 */
  144.  
  145.   printf("%s%s by ", a, ltostr(b*97+32+022, 16));  /* display result */
  146. }
  147.  
  148. /***************************************************************************
  149.  *
  150.  * FPL supports multi dimensional arrays (max 40 dimensions though):
  151.  */
  152.  
  153. void arrays()
  154. {
  155.   int c[5][2]={
  156.     {2, 0},
  157.     {3, 1}
  158.   };
  159.   int a[25]={0, 1, 2, 3, 4, 5, 6};  /* declaration assign    */
  160.   string b[4];
  161.  
  162.   a[12]^={3, 2, 1, 0};          /* array compound assign */
  163.  
  164.   resize a[10];             /* array resizing        */
  165.  
  166.   printf(b[0]={"FPL", "is", "Copyright by", "FrexxWare!"});
  167.   printf("\n");
  168. }
  169.  
  170. /***************************************************************************
  171.  *
  172.  * Expressions are very extensible in FPL. It features all ANSI C operators
  173.  * and has 100% operator precedence compatibility:
  174.  */
  175.  
  176. int expressions()
  177. {
  178.   int a=128, b=3, c, d;
  179.   string out;
  180.  
  181.   c=~(a>>b)+2*b-a++/50 & (d=a*2-1);     /* complex formula */
  182.  
  183.   c^= (2^3&3+a)%c;          /* compound assign */
  184.  
  185.   printf("%d",d = c---a-b*10);      /* printf integer! */
  186.   printf("%% ");                /* printf string! */
  187.   return d;
  188. }
  189.  
  190. /***************************************************************************
  191.  *
  192.  * Conditions are used just as i C.
  193.  */
  194.  
  195. void conditions()
  196. {
  197.   int a=11;
  198.   switch( expressions() ) {
  199.     case 99:
  200.       printf("Turbo pascal");
  201.       break;
  202.  
  203.     case 100:
  204.       printf("ANSI C");
  205.  
  206.     default:
  207.       printf(" operator ");
  208.   }
  209.   if(a==11)
  210.     printf("compliance!\n");
  211.   else
  212.     printf("failure!\n");
  213. }
  214.  
  215.  
  216. /***************************************************************************
  217.  *
  218.  * Loops are used just as in C. (While has been improved with else support
  219.  * and break with a level option!):
  220.  */
  221.  
  222. void loops()
  223. {
  224.   int a, b;
  225.   for(a=0; a<10; a++) {
  226.     int Bninja;
  227.     printf("-");
  228.     b=a;
  229.     while(b<5)
  230.       b++;
  231.     else {
  232.       printf("Very "        /* Strings in         */
  233.          "easy"     /* several parts      */
  234.          " implemented, "); /* just as in C!      */
  235.     /* This upper thing is a 5.3+ feature! */
  236.       break;            /* break out of loop! */
  237.     }
  238.   }
  239.   do {
  240.     if(++b<10)
  241.       continue;
  242.     break;
  243.   } while(a);
  244.  
  245.   while(1)
  246.     while(1)
  247.       while(1)
  248.         while(1)
  249.           while(1) {
  250.         printf("very similar to C.\n");
  251.         break 5;/* break out of multiple loops! */
  252.       }
  253. }
  254.  
  255. /***************************************************************************
  256.  *
  257.  * Constants can be written in several different ways in FPL:
  258.  */
  259.  
  260. void constants()
  261. {
  262.   int a= 010 +    /* octal number       */
  263.          0b100 +  /* binary number      */
  264.          0xdead - /* hexadecimal number     */
  265.          211 +    /* common decimal number  */
  266.          'A'+     /* ASCII code         */
  267.          '\t';    /* ASCII code of control char */
  268. }
  269.  
  270. /***************************************************************************
  271.  *
  272.  * Variables always exist in their declared level and in the following ones
  273.  * execpt when new variables are declared there with the same name:
  274.  */
  275.  
  276. void locals()
  277. {
  278.   int a=2, b=11;    /* declare a and b here */
  279.   {
  280.     int a=3;        /* declare a here */
  281.     {
  282.       int a=4;      /* declare a here */
  283.       {
  284.         int a=5;    /* declare a here */
  285.         test(b);    /* jump to test and come back */
  286.       }         /* this removes one a variable */
  287.     }           /* this removes another a variable */
  288.   }         /* this removes yet another a variable */
  289.   printf("%d kinds of comments, ", a);  /* this is the original variable */
  290. }
  291.  
  292. int test(int b)
  293. {
  294.   printf("FPL features %d internal functions\n",  b);
  295. }
  296.  
  297. /***************************************************************************
  298.  *
  299.  * FPL is fully recursive:
  300.  */
  301.  
  302. void recursive()/* This example shows the recursiveness by using a lot of */
  303.         /* unnecessary braces and from within the innermost brace */
  304.         /* level call itself. */
  305. {
  306.   int b;
  307.   label(0);
  308. }
  309.  
  310. int label(int b)
  311. {
  312.   int a;
  313.   if(++b<5) {
  314.     while(1) {
  315.       {
  316.         {
  317.           {
  318.             {
  319.               {
  320.                 label(b);
  321.                 break;
  322.           }
  323.             }
  324.           }
  325.         }
  326.       }
  327.     }
  328.   } else
  329.     printf("Finally we reached this point when b equals %d.\n", b);
  330. }
  331.  
  332. /***************************************************************************
  333.  *
  334.  * FPL supplies eight internal functions:
  335.  */
  336.  
  337. void functions()
  338. {
  339.   int a, b=2, c;
  340.   string str1="hello", str2="world", str3="l", num="12";
  341.  
  342.   a=strcmp(str1, str2);     /* compare two strings */
  343.   a=strncmp(str1, str2, b); /* compare two strings a certain length */
  344.  
  345.   a=strstr(str2, str3);     /* search for substring within a big string */
  346.  
  347.   str3=substr(str1, 0, 3);  /* get substring */
  348.  
  349.   a=atoi(num);          /* decimal string to numerical */
  350.  
  351.   a=strtol(num, 16);        /* any-base string to numerical */
  352.  
  353.   str3=ltostr(a, 14);       /* integer to string, base 14 */
  354.  
  355.   num+="-19";           /* append to string */
  356.  
  357.   c=eval(num);          /* calculate a string!!! */
  358.  
  359.   a=strlen(str2);       /* length of a string */
  360.  
  361.                 /* absolute value of integer: */
  362.   printf("abs(%s) equals %d.\n", num, abs(c));
  363. }
  364.  
  365. void arg_functions()
  366. {
  367.   long a;  // supports `long', which is the same as `int'
  368.   for(a=0; a<5; a++)
  369.     printint(double2(a)); // `inside' functions used as any other function!
  370.  
  371.   setvar(&a);   // assign variable a!
  372.   printint(a);  // to prove it really changed!
  373.   printint((++ninjax, ninja++)); /* Using comma separator in functions that
  374.                     only wants one parameter! */
  375.   printf("\n");
  376. }
  377.  
  378. int setvar(int *variable) // integer variable as argument
  379. {
  380.   *variable=100; // assigns the received variable!
  381. }
  382.  
  383. int printint(int x) // integer as argument
  384. {
  385.   printf("|%d|", x); // printf the numer received!
  386. }
  387.  
  388. /***************************************************************************
  389.  
  390.             >Copyright (C) 1992 - 1995 by FrexxWare<
  391.  
  392. ****************************************************************************/
  393.