File:  [gforth] / gforth / vmgen-ex / test.mini
Revision 1.1: download - view: text, annotated - select for diffs
Sun Apr 29 11:28:24 2001 UTC (18 years, 6 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, v0-6-2, v0-6-1, v0-6-0, HEAD
added vmgen-ex

    1: func operators()
    2:   print 3 = 3;
    3:   print (3+5) = 8;
    4:   print (5-3) = 2;
    5:   print (3*5) = 15;
    6:   print (3&5) = 1;
    7:   print (3|5) = 7;
    8:   print (3<5) = 1;
    9:   print (5<3) = 0;
   10:   print (3=5) = 0;
   11:   print (5=5) = 1;
   12:   print (!3) = 0;
   13:   print (!0) = 1;
   14:   print (-3) = (0-3);
   15:   return 0;
   16: end func;
   17: 
   18: func params(a, b, c)
   19:   print a = 3;
   20:   print b = 5;
   21:   print c = 7;
   22:   return 9;
   23: end func;
   24: 
   25: func locals(a)
   26:   var b;
   27:   var c;
   28:   b:=a+1;
   29:   c:=b+1;
   30:   a:=c+1;
   31:   return a;
   32: end func;
   33: 
   34: func inc(x)
   35:   return x+1;
   36: end func;
   37: 
   38: func sign(n)
   39:   var r;
   40:   if (n<0) then
   41:     r:=-1;
   42:   else
   43:     if (0<n) then
   44:       r:=1;
   45:     else
   46:       r:=0;
   47:     end if;
   48:   end if;
   49:   return r;
   50: end func;
   51: 
   52: func recfac(n)
   53:   var r;
   54:   if (n<1) then
   55:     r:=1;
   56:   else
   57:     r:=recfac(n-1)*n;
   58:   end if;
   59:   return r;
   60: end func;
   61: 
   62: func itfac(n)
   63:   var r;
   64:   r:=1;
   65:   while (0<n) do
   66:     r:=r*n;
   67:     n:=n-1;
   68:   end while;
   69:   return r;
   70: end func;
   71: 
   72: func testfac()
   73:   var i;
   74:   i:=0;
   75:   while (i<10) do
   76:     print itfac(i) = recfac(i);
   77:     i:=i+1;
   78:   end while;
   79:   return 0;
   80: end func;
   81: 
   82: func main()
   83:   operators();
   84:   print params(3,5,7) = 9;
   85:   print locals(3) = 6;
   86:   print (inc(1)+inc(inc(inc(3))))=8;
   87:   print sign(5) = 1;
   88:   print sign(0) = 0;
   89:   print sign(-5) = (-1);
   90:   print itfac(5) = 120;
   91:   testfac();
   92:   return 0;
   93: end func;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>