JavaScript flow control

I have over 190 changes to the basic JavaScript logic. Flow control by far gives the greatest advantage over conventional programming methods. It completely changes the dynamics of software development. Context within a function is maintained far better by putting logic into well organized units. Segregation of context has huge ramifications in logic design and in program capabilities, by simplifying and reducing context to its absolute minimum. Context is the "array of values" that a function requires. The flow control changes nothing... current compilers simply limit your ability to control flow. The Jane compiler allows you to have 100% control over your programming environment. Flow control is delegation of responsibility. "Mary requests an apple pie, I need apples, John go to the store, while your gone I will clean the kitchen, Mary are you sure you want Apple pie?".   Mary now wants cherry pie, I call john and request cherries and almond extract, I have the crust already baking, good, I change the recipe. This is normal, it is not top down logic, but it must be this simple. Programming should be at the 3rd grade level of logic.

I was forced to rewrite Jane's compiler logic so flow control is a basic function of the language. This eliminated about eight hundred lines of code, so it actual helped and makes the compiler much more powerful.

Part of this effort was absolutely required because I had to overcome the asynchronous limitation of JavaScript. Without a "wait" in all browsers I was unable to attach the database using web sockets to the compiler, and I was unable to implement unsolicited server events.  So now I can say "A = B;" where B is a file on the server, or B is a database Table. I can now change the contents of "A" and it be reflected back on the server.  With parallel statement execution I can now implement Big Data logic in a few characters. "A = B + C"; where B is a table on server X, and C is a table on Server Y, plus the assignment can now wait for both B and C to complete in parallel.

current:  A calls B, B returns to A without waiting
(I know "await", I hate "promises" and it is very browser dependent and limited)

now: A calls B,  B can pass control back to A with some values,  A can pass control back to B with more values, or A can exit without returning control to B. This is a two (or more) way communication between functions.  All synchronously or asynchronously, blocking or not blocking other events. Both A and B retain context. Plus, with the built-in database and communication A and B can be on different machines.

Flow Control,  events: 

wait here until an event, block all other events A = read.wait(); 

return here after an event, allow all other events,  A = read.return(); 

continue execution to next statement handled = read.continue(); 
must be in a loop, always returns twice: once before event, once after event -- parallel execution

event function to handle asynchronous function calls, that satisfy wait/return/continue flow statements

entry a callable entry point of the caller of an event or function, (only callable from the function itself, of any called functions)

exit  from anywhere to the top level, cancel all levels of function calls and statements (except finally)

refresh exit to the top level to allow screen refresh, and then return to the next statement;

reenter permits the function that called an entry point to be reentered at the statement after the call or to one of its own entry points.  The reenter can be used with exception handing to recover back to the point of failure, or to a passed entry point.


Flow control allows far better definition of context, and therefore much simpler logic. My expectation is that all program logic will be reduced by 50%, which is directly proportional to the number of line of code.  Security will also be greatly increased since now security can be at the instruction level without affecting logic.

example: "A = B" B requires login, user is asked to login, exit if fail, B's security is that it cannot be assigned to another value, fail or recover to another alternate logic, or login again to higher security.

A function's "context" is programming. We spend 99% of our effort in getting things into context. With Jane and its compiler, database, web server, and data format handling all context, software development will be reduced by 99%.  If I tie speech to text events to the compiler, context switching will be greatly improved. "now talk about sex"... this is context handling and flow control.  Using entry points context can now be switched back and forth.


Flow between two or more functions using "entry" points and the "reenter" statement. Separation of logic, B is only concerned with getting data, A and C only concerned with format. A and C can alter flow at any time. We should be able to reduce conditionals to the very minimum, which is the reduction of logic permutations.

figure 1:

01	A();
02	C();
04 function A()
05 {
06	B();
07	alert(value);
08	return;
10	entry begin();   value = 'A test of entry points\n'; reenter;
11	entry add(text); value += text + "\n"; reenter;
12	entry finish();  value += 'done'; reenter;
13 }
15 function C()
16 {
17	B();
18	alert(value);
19	return;
21	entry begin();   first = true; value = 'values = ['; reenter;
22	entry add(text); if (! first) value += ','; first = false; value += text; reenter;
23	entry finish();   value += ']'; reenter;
24 }
26 function B()
27 {
28	count = 0;
29	begin();
30	while (++count < 10) add(count);
31	finish();
32 }

Better flow control with event handling. Recovery from the throw statement by passing a recovery reentry point.
figure 2:

01 function A()
02 {
03	try { B(10); }
04	catch (e,recover)
06	{
07		alert(e);
08		if (recover) reenter recover(220);
09	}
10 }
12 function B(n)
13 {
14	entry onFail(n)
15	if (n < 100) throw("number must be greater than 100",onFail);
16	alert(n);
17 }

figure 3:

01	for (i=0; i <= 100; ++i);
02	{
03		setProgressBar(i of 100);
04		refresh;
05		wait(2 seconds);
06	}

The "exit" statement cancels all operations, (no alert is issued)

figure 4:

01	a()
02	alert('executed');
04	function a()
05	{
06		exit;
07	}

An "event" is an asynchronous function used to satisfy control flow (wait/return/continue)
it also permits values to to returned from the event

	figure 5:

01	x = setTimeout.wait(myEvent,10 seconds); // blocked
02	alert("Hello " + x);
04	event myEvent() { return "World"; }

A caller's "entry" point to handle alternate flow control, (entry points of the caller are first in the search path)
entry points do not have to return control to the caller, but are continuation of the calling function. Entry point statements are not executable, they allow called functions and events the ability of pass values, and to change the order of statement execution

figure 6:

01 function Apple()
02 {
03 = false;
04	text = doSomething()
05	alert(text);
06	return;
08	entry loginRequired(message);
09	if (! confirm(message)) { alert('user canceled operation'); return; }
10	reenter;
11 }
12 //=========================================================
13 //			doSomething
14 //=========================================================
15 function doSomething()
16 {
17	if (! loginRequired('Do you wish to login');
18	return "Hello World - Wecome";	
19 }

Parallel Processing Events

Asynchronous Event Handling. The keyword "event" is a sub-function of "read".  If a function is called with a wait qualifier without any "event" sub-function, the call is immediately satisfied, otherwise the call waits for any event to be called. An event can issue a "continue waiting;" statement if the event is not finished.
figure 7:

01 //==============================================================
02 //		read (with an event handler)
03 //==============================================================
04 function read(url)
05 {
06	http = new XMLHttpRequest();
07	http.onload = onload;
08'get', url, true);
09	http.send();
11	event onload()
12	{
13	    if (this.status == 200) return this.responseText;
14	    onFailure(this.status,this.responseText);
15	}
16 }

Sequential Processing, read three files wait for each to be read
figure 8:

01	names =['apple','pear','banana'];
02	alert(readFiles(names));
04 //============================================================================
05 //	readFiles (".wait" sequential blocked, or unblocked ".return")
06 //=============================================================================
07 function readFiles(names)
08 {
09	for (index in names)
10	{
11		text += read.wait("./data/" + names[index] + ".txt");
12	}
13	return text;
14 }

Parallel Processing, read three files in parallel, when all three are read, return merged files
figure 9:

01	names =['apple','pear','banana'];
02	text = readParallel(names);
04 //=============================================================
05 //            readParallel
06 //=============================================================
07 function readParallel(names)
08 {
09	text = "";
10	temp = new Array();
11	for (index in names)
12	{
13		handled = read.continue(index,"./data/" + names[index] + ".txt");
14		if (handled) { temp[handled.arguments[0]] += handled.result; break; }
15	}
16	continue waiting;   // block all other events (or "return" for unblocked execution)
17	return temp.join();
18 }


Line 13 the first parameter "index" is never passed to the "read" function all of the arguments of "continue" are kept in the "handled" return value

Line 14 "handled.result" is the returned value of the "read" function

The "handled" object keeps the starting and ending times and duration of the call (not shown)