JavaScript "engine"

Admin User, erstellt 25. Aug. 2023
         
/**
* Modern Albufeira Prolog Interpreter
*
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {set_stage, set_cursor, clear, get_stage,
consult_async, post, Compound, init, show
} from "../../../01_ciao/lib/dogelog.mjs";
/****************************************************************/
/* Notebook Handlers */
/****************************************************************/
let dirty = false;
/**
* Disable/enable all the buttons.
*
* @param flag Enable if true, other disable.
*/
function shield_elems(flag) {
let num = 0;
let elem = document.getElementById("launch"+num);
while (elem !== null) {
elem.disabled = flag;
elem.style.color = (flag ? "lightgray" : "black");
num++;
elem = document.getElementById("launch"+num);
}
}
/**
* Rollback the Prolog interpreter to a stage.
*
* @param num The desired stage.
*/
function reset(num) {
if (dirty) {
document.getElementById("demo"+get_stage()).innerHTML = "";
clear();
dirty = false;
}
while (num < get_stage()) {
set_stage(get_stage()-1);
document.getElementById("demo"+get_stage()).innerHTML = "";
clear();
}
}
/**
* Asynchronously run or abort the Prolog interpreter.
*
* @param num The desired stage.
*/
async function main_async(num) {
let elem = document.getElementById("launch"+num);
if (elem.innerText === "⏵") {
shield_elems(true);
elem.innerText = "⏹";
elem.style.color = "red";
elem.disabled = false;
reset(num);
try {
while (num >= get_stage()) {
let elem2 = document.getElementById("demo"+get_stage());
set_cursor(elem2);
let text = document.getElementById("text" + get_stage()).innerText;
await consult_async(text);
set_stage(get_stage()+1);
}
} catch (err) {
show(err);
dirty = true;
}
elem.innerText = "⏵";
shield_elems(false);
} else {
elem.disabled = true;
post(new Compound("system_error", ["user_abort"]));
}
}
/****************************************************************/
/* Notebook Creators */
/****************************************************************/
/**
* Setup the engine.
*/
export function setup_engine() {
let elem3 = document.getElementById("demo"+0);
set_cursor(elem3);
init();
}
/**
* Link the buttons with an asynchronous main.
*/
export function link_cells_async() {
let num = 0;
let elem = document.getElementById("launch"+num);
while (elem !== null) {
{
let i = num;
elem.addEventListener("click", async function () {await main_async(i)});
}
num++;
elem = document.getElementById("launch"+num);
}
}