#!../src/tops  -i -s ../sys  -u ../usr
/*
Program Tops - a stack-based computing environment
Copyright (C) 1999-2006  Dale R. Williamson

Author: Dale R. Williamson <dale.williamson@prodigy.net>

File test/js_equiv  February 2006

Showing operations that are somewhat equivalent to JavaScript, Java
and C++.  

Reference: 
   Flanagan, D., "JavaScript: The Definitive Guide, Fourth Edition, 
   O'Reilly Media, Inc., 2002.

For a good overview of object-oriented JavaScript versus Java and C++,
see discussion in Flanagan, pages 123 - 132.

----------------------------------------------------------------------*/

/* These lines perform similarly to some examples in the Reference,
   pages 106 - 107: Functions as Data. */

   indexbase(0); // match JavaScript's 0 index base

   catmsg(no); // don't show "into catalog" messages

/* Making a function, or word, called square: */
 //function (y) = square(x) { return(x*x); } /* simpler=better */
   function (y) = square(x) { y = x*x; } /* stresses the parser more */

   a = square(4); // a is NUM 16

   macro("square", "b"); // synonym for square called b

   c = b(5); // c is NUM 25

/* Putting a function into the library of another function (Flanagan 
   calls this assigning a function to object properties): */

   library("o"); // making a library word (but any word would do)

   macro("square", localref(o, "square")); // square() into lib of o

/* Extract function square from the library of function o and run it 
   (dot notation A.B extracts item B from lib of A; if B is a function,
   it will execute--see man yank): */
   y = o.square(10); // y is 100 

/* Making an array that includes the ptr to function square: */
   a = [ptr("square") ; 20]; // putting ptr to square() at a[0]

/* Executing ptr at a[0] with data a[1] and putting result into new 
   third element a[2]: */
   a[2] = exe(a[1], @a[0]); // a[2] is 400

   NULL[0] = square(4)-16 + c-25 + y-100 + @a[2]-400;

//----------------------------------------------------------------------

/* These lines perform similarly to the circle class example of section
   8.5.5 of the Reference, pages 126 - 127. */

   catmsg(no);

   constructor("r", "Circle"); // word Circle to make Circle words
   Circle.PI = pi;             // pi into the library of word Circle

   Circle(12, "c1");           // making circle word c1 with r=12
   Circle(20, "c2");           // making circle word c2 with r=20

   function Circle_area(b) { // compute and bank area into library of b
      b.area = b.r*dup*Circle.PI;
   }

   function (R) = Circle_max(a, b) { // return the biggest circle
      if(a.r > b.r) return(a);
      else return(b);
   }

// Compute and bank areas into the libs of these circle words: 
   Circle_area(c1);
   Circle_area(c2);

// Show some libraries:
   nl; wholib("Circle");
   nl; wholib(c1);
   nl; wholib(c2); 

// Extract some radii and areas from the libs of circle words:
   NULL[1] = c1.r*c1.r*pi - c1.area
           + c2.r*c2.r*pi - c2.area
           + true - (c2 == Circle_max(c1, c2))
           + c2.r - yank(Circle_max(c2, c1), "r");

//----------------------------------------------------------------------

<< \ switching to postfix

\ Display a summary (postfix style):
   NULL totals @ 0=
   IF " Ok " ELSE " Error " THEN "JavaScript equivalence" + nl .  nl
   "*" 72 cats . nl 2 idle

   HALT

------------------------------------------------------------------------

   Notes:

   These examples use the program's notion of words and libraries of 
   words to mimic object-oriented ideas.

   The program's ability to make words that themselves make words is 
   behind function constructor(), used above to make "circle" words.  
   The ability of words to store and fetch items from the local li-
   braries of other words is also used.  None of these are new to the
   program.

   Infix symbol A.B refers to the item named B in the library of the 
   word named A, to which the parser applies words yank and bank when 
   transforming infix to postfix.

   Phrases here are written in C-like infix notation, but the symbols
   of infix cannot be interpreted directly to run the machine (see 
   doc/parser.doc).

   A front end parser transforms the infix symbols to postfix phrases,
   putting words in a sequential order that the program can run, as 
   always, step by step (see man parse).  

------------------------------------------------------------------------
