Nachricht für neue Nutzer.
Nachricht für engagierte Nutzer.
Widget:KnowHowComputer: Unterschied zwischen den Versionen
Aus ZUM-Unterrichten
KKeine Bearbeitungszusammenfassung |
KKeine Bearbeitungszusammenfassung |
||
| Zeile 8: | Zeile 8: | ||
class KnowHowComputerElement extends HTMLElement { | class KnowHowComputerElement extends HTMLElement { | ||
static observedAttributes = ['no-help']; | static observedAttributes = ['no-help']; | ||
/** | |||
* @typedef {object} MemoryConfiguration | |||
* @property {string} name | |||
* @property {Record<int,string>} program | |||
*/ | |||
/** @type {MemoryConfiguration[]} */ | |||
#memoryConfigurations = []; | |||
/** @type {?MutationObserver} */ | |||
#observer; | |||
/** @type {int} */ | |||
#selectedMemory; | |||
constructor() { | constructor() { | ||
| Zeile 227: | Zeile 241: | ||
refreshMemoryConfigurations() { | refreshMemoryConfigurations() { | ||
this.memoryConfigurations = [ | this.#memoryConfigurations = [ | ||
...this.parseMemoryElements() | ...this.parseMemoryElements() | ||
]; | ]; | ||
this.refreshMemorySelector(this.memoryConfigurations); | this.refreshMemorySelector(this.#memoryConfigurations); | ||
if(!this.#selectedMemory){ | |||
if (this.khc && this.#memoryConfigurations[0] | |||
) { | |||
this.khc.loadProgram(this.#memoryConfigurations[0].program); | |||
} | |||
} | |||
} | } | ||
| Zeile 239: | Zeile 259: | ||
} | } | ||
attributeChangedCallback( | // noinspection JSUnusedGlobalSymbols | ||
attributeChangedCallback(_name, _oldValue, _newValue) { | |||
this.refreshHelp() | this.refreshHelp() | ||
} | } | ||
// Lifecycle callbacks | // Lifecycle callbacks | ||
// noinspection JSUnusedGlobalSymbols | |||
connectedCallback() { | connectedCallback() { | ||
// Component is now in the DOM | // Component is now in the DOM | ||
| Zeile 249: | Zeile 271: | ||
// Parse khc-memory child elements | // Parse khc-memory child elements | ||
this.refreshMemoryConfigurations(); | this.refreshMemoryConfigurations(); | ||
this.observer = new MutationObserver( | this.#observer = new MutationObserver(_ => { | ||
this.refreshMemoryConfigurations(); | this.refreshMemoryConfigurations(); | ||
}); | }); | ||
this.observer.observe(this, {childList: true}); | this.#observer.observe(this, {childList: true}); | ||
| Zeile 261: | Zeile 283: | ||
// Load program from khc-memory elements | // Load program from khc-memory elements | ||
const index = parseInt(selectedValue); | const index = parseInt(selectedValue); | ||
if (index >= 0 && index < this.memoryConfigurations.length) { | if (index >= 0 && index < this.#memoryConfigurations.length) { | ||
this.loadMemoryConfiguration(this.memoryConfigurations[index]); | this.#selectedMemory = index; | ||
this.loadMemoryConfiguration(this.#memoryConfigurations[index]); | |||
} | } | ||
e.preventDefault(); | e.preventDefault(); | ||
| Zeile 270: | Zeile 293: | ||
this.refreshHelp(); | this.refreshHelp(); | ||
} | } | ||
// noinspection JSUnusedGlobalSymbols | |||
disconnectedCallback() { | disconnectedCallback() { | ||
// Component is removed from the DOM | // Component is removed from the DOM | ||
// Clean up any event listeners if needed | // Clean up any event listeners if needed | ||
this.#observer?.disconnect(); | |||
} | } | ||
| Zeile 353: | Zeile 374: | ||
// State variables | // State variables | ||
/** @type {string[]} */ | |||
this.mem = new Array(this.speicheranzahl + 1); // mem[0] existiert, wird aber nicht benutzt | this.mem = new Array(this.speicheranzahl + 1); // mem[0] existiert, wird aber nicht benutzt | ||
this.pz = 1; // Programmzähler | this.pz = 1; // Programmzähler | ||
| Zeile 358: | Zeile 380: | ||
// Constants and utility variables | // Constants and utility variables | ||
this.ziffern = "0123456789"; | this.ziffern = new Set("0123456789".split('')); | ||
this.lueckenzeichen = " \r\n\t" | this.lueckenzeichen = new Set(" \r\n\t".split('')); | ||
this.doppelpunkt = new Set(":".split('')); | |||
this.doppelpunkt = ":"; | this.kommentarzeichen = new Set(":".split('')); | ||
this.kommentarzeichen = " | this.khcbefehle = new Set(["isz", "jmp", "inc", "dec", "stp"]); | ||
this.khcbefehle = new | this.khcbuchstaben = new Set(this.khcbefehle.values().flatMap(s => s.split(''))); | ||
// Lexer/parser state | // Lexer/parser state | ||
this.symtypen = | /** @type {string[]} */ | ||
this.symwerte = | this.symtypen = []; | ||
/** @type {string[]} */ | |||
this.symwerte = []; | |||
this.symindex = -1; | this.symindex = -1; | ||
// Error messages | // Error messages | ||
this.fehlermeldungen = | this.fehlermeldungen = []; | ||
this.fehlermeldungen[0] = "Keinen Fehler gefunden"; | this.fehlermeldungen[0] = "Keinen Fehler gefunden"; | ||
this.fehlermeldungen[1] = "Unerlaubtes Zeichen"; | this.fehlermeldungen[1] = "Unerlaubtes Zeichen"; | ||
| Zeile 418: | Zeile 443: | ||
} | } | ||
return test; | return test; | ||
} | } | ||
| Zeile 431: | Zeile 452: | ||
// Lexer and parser | // Lexer and parser | ||
khclexer(eingabe) { | khclexer(eingabe) { | ||
let z; // einzelnes Zeichen aus eingabe | |||
let i; | |||
this.symtypen = this.arrayleeren(this.symtypen); | this.symtypen = this.arrayleeren(this.symtypen); | ||
this.symwerte = this.arrayleeren(this.symwerte); | this.symwerte = this.arrayleeren(this.symwerte); | ||
this.symindex = -1; | this.symindex = -1; | ||
let symwert = ""; | |||
let symtyp = "l"; // z =zahl, b =buchstabenkette, l =luecke, : =doppelpunkt | |||
eingabe = " " + eingabe + " "; | eingabe = " " + eingabe + " "; | ||
let ztyp = "l"; | |||
for (i = 0; i < eingabe.length; i++) { | for (i = 0; i < eingabe.length; i++) { | ||
z = eingabe.charAt(i); | z = eingabe.charAt(i); | ||
if (this. | if (this.lueckenzeichen.has(z)) ztyp = "l"; // z ist ein Lückenzeichen | ||
else if (this. | else if (this.ziffern.has(z)) ztyp = "z"; // z ist eine Ziffer | ||
else if (this. | else if (this.doppelpunkt.has(z)) ztyp = ":"; // z ist ein Doppelpunkt | ||
else if (this. | else if (this.khcbuchstaben.has(z)) ztyp = "b"; // z kommt in einem der KHC-Befehle vor | ||
else if (this. | else if (this.kommentarzeichen.has(z)) ztyp = ";"; // z ist Kommentarzeichen | ||
else return false; | else return false; | ||
if (symtyp == ztyp) { // aktueller symtyp stimmt mit dem ztyp des gerade gelesenen Zeichens z überein | if (symtyp === ztyp) { // aktueller symtyp stimmt mit dem ztyp des gerade gelesenen Zeichens z überein | ||
symwert = symwert + z; | symwert = symwert + z; | ||
} else { // neues Symbol: | } else { // neues Symbol: | ||
if (symtyp != "l") { // bisheriges Symbol in Arrays speichern | if (symtyp !== "l") { // bisheriges Symbol in Arrays speichern | ||
this.symindex++; | this.symindex++; | ||
this.symwerte[this.symindex] = symwert; | this.symwerte[this.symindex] = symwert; | ||
this.symtypen[this.symindex] = symtyp; | this.symtypen[this.symindex] = symtyp; | ||
} | } | ||
if (ztyp != ";") { | if (ztyp !== ";") { | ||
symtyp = ztyp; // Anfang des neuen Symbols in sym-Vars speichern | symtyp = ztyp; // Anfang des neuen Symbols in sym-Vars speichern | ||
symwert = z; | symwert = z; | ||
| Zeile 470: | Zeile 491: | ||
istkhcbefehl(s) { | istkhcbefehl(s) { | ||
return this.khcbefehle.has(s); | |||
} | } | ||
| Zeile 492: | Zeile 507: | ||
// Rückgabewert: fehlerindex | // Rückgabewert: fehlerindex | ||
var ok = this.khclexer(zeile); | var ok = this.khclexer(zeile); | ||
if (this.symwerte.join("").length == 0) return -1; // Leerzeile liefert keine Fehlermeldung | if (this.symwerte.join("").length === 0) return -1; // Leerzeile liefert keine Fehlermeldung | ||
if (!ok) return 1; // "Unerlaubtes Zeichen" | if (!ok) return 1; // "Unerlaubtes Zeichen" | ||
if (this.symtypen[0] != "z") return 2; // "Syntaxfehler - Zahl erwartet" | if (this.symtypen[0] !== "z") return 2; // "Syntaxfehler - Zahl erwartet" | ||
if (!this.istgueltigezahl(this.symwerte[0], 1, this.speicheranzahl)) return 3; // "Adresse nicht im gültigen Bereich" | if (!this.istgueltigezahl(this.symwerte[0], 1, this.speicheranzahl)) return 3; // "Adresse nicht im gültigen Bereich" | ||
if (this.symtypen[1] != ":") return 6; // "Syntaxfehler - Doppelpunkt erwartet" | if (this.symtypen[1] !== ":") return 6; // "Syntaxfehler - Doppelpunkt erwartet" | ||
if (this.symtypen[2] != "z" && this.symtypen[2] != "b") return 7; // "Syntaxfehler - Zahl oder Befehl erwartet" | if (this.symtypen[2] !== "z" && this.symtypen[2] !== "b") return 7; // "Syntaxfehler - Zahl oder Befehl erwartet" | ||
if (this.symtypen[2] == "z") { | if (this.symtypen[2] === "z") { | ||
if (!this.istgueltigezahl(this.symwerte[2], 0, this.maxwert)) return 4; // "Wert nicht im gültigen Bereich" | if (!this.istgueltigezahl(this.symwerte[2], 0, this.maxwert)) return 4; // "Wert nicht im gültigen Bereich" | ||
} | } | ||
if (this.symtypen[2] == "b") { | if (this.symtypen[2] === "b") { | ||
if (!this.istkhcbefehl(this.symwerte[2])) return 5; // "Ungültiger Befehl" | if (!this.istkhcbefehl(this.symwerte[2])) return 5; // "Ungültiger Befehl" | ||
if (this.symwerte[2] == "stp" && this.symtypen.length > 3) return 8; // "Syntaxfehler" | if (this.symwerte[2] === "stp" && this.symtypen.length > 3) return 8; // "Syntaxfehler" | ||
if (this.symwerte[2] != "stp") { | if (this.symwerte[2] !== "stp") { | ||
if (this.symtypen[3] != "z") return 9; // "Syntaxfehler" | if (this.symtypen[3] !== "z") return 9; // "Syntaxfehler" | ||
if (!this.istgueltigezahl(this.symwerte[3], 1, this.speicheranzahl)) return 3; // "Adresse nicht im gültigen Bereich" | if (!this.istgueltigezahl(this.symwerte[3], 1, this.speicheranzahl)) return 3; // "Adresse nicht im gültigen Bereich" | ||
} | } | ||
| Zeile 591: | Zeile 606: | ||
registeranzeigefeldaktualisieren() { | registeranzeigefeldaktualisieren() { | ||
this.container.getElementById("programmzaehleranzeigefeld").value = this.rechtsbuendig(this.pz) + ": " + this.stringmitleerzeichenauffuellen(this.mem[this.pz], this.spaltenanzahlinspeicheranzeigefeld - 3); | |||
} | } | ||
| Zeile 621: | Zeile 635: | ||
var befehlscode = this.br.substr(0, 3).toLowerCase(); | var befehlscode = this.br.substr(0, 3).toLowerCase(); | ||
// Bei stp ist keine Adresse erforderlich und es wird auch nichts gemacht: | // Bei stp ist keine Adresse erforderlich und es wird auch nichts gemacht: | ||
if (befehlscode == "stp") return 0; | if (befehlscode === "stp") return 0; | ||
// sonst muss eine ganze Zahl als Adresse folgen: | // sonst muss eine ganze Zahl als Adresse folgen: | ||
var adresse = parseInt(this.br.substr(4)); // ab Pos. 4 bis zum Ende ausschneiden | var adresse = parseInt(this.br.substr(4)); // ab Pos. 4 bis zum Ende ausschneiden | ||
| Zeile 631: | Zeile 645: | ||
break; | break; | ||
case "isz": | case "isz": | ||
if (this.mem[adresse] | if (this.isZero(this.mem[adresse])) this.pz = this.pz + 2; else this.pz = this.pz + 1; | ||
break; | break; | ||
case "inc": | case "inc": | ||
| Zeile 657: | Zeile 671: | ||
this.zeileinspeicheranzeigefeldmarkieren(this.pz); | this.zeileinspeicheranzeigefeldmarkieren(this.pz); | ||
return fehlerindex; | return fehlerindex; | ||
} | |||
isZero(zeile){ | |||
return 0 === parseInt(zeile); | |||
} | } | ||
| Zeile 663: | Zeile 681: | ||
var fehlerindex = 0; // vorläufig | var fehlerindex = 0; // vorläufig | ||
fehlerindex = this.khcparser(zeile); | fehlerindex = this.khcparser(zeile); | ||
if (fehlerindex == 0) { | if (fehlerindex === 0) { | ||
var pz = parseInt(this.symwerte[0]); | var pz = parseInt(this.symwerte[0]); | ||
var befehlszeile = this.symwerte[2]; | var befehlszeile = this.symwerte[2]; | ||
if (this.symwerte.length > 3) befehlszeile = befehlszeile + " " + this.symwerte[3]; | if (this.symwerte.length > 3) befehlszeile = befehlszeile + " " + this.symwerte[3]; | ||
this.mem[pz] = befehlszeile; | this.mem[pz] = befehlszeile; | ||
} else { | } else { | ||
| Zeile 689: | Zeile 706: | ||
domElem.selectionStart = posStart; | domElem.selectionStart = posStart; | ||
domElem.selectionEnd = posEnde; | domElem.selectionEnd = posEnde; | ||
} | } | ||
} | } | ||
| Zeile 704: | Zeile 714: | ||
let endepos = startpos + this.mem[pz].length + 4; | let endepos = startpos + this.mem[pz].length + 4; | ||
this.setzeMarkierungInTextFeld("#speicheranzeigefeld", startpos, endepos); | this.setzeMarkierungInTextFeld("#speicheranzeigefeld", startpos, endepos); | ||
} | } | ||
} | } | ||
| Zeile 741: | Zeile 731: | ||
// Lifecycle callbacks | // Lifecycle callbacks | ||
// noinspection JSUnusedGlobalSymbols | |||
connectedCallback() { | connectedCallback() { | ||
// Hide the element by default as it's just a data container | // Hide the element by default as it's just a data container | ||
Version vom 9. Juni 2025, 10:33 Uhr
<noinclude>{{#tag:pre|{{msgnw:Widget:KnowHowComputer}}}}</noinclude><includeonly><script type="text/JavaScript">
(function () {
const STYLESHEET_HREFS = Array.from(document.styleSheets).filter(cssss => cssss.href).map(cssss => cssss.href);
if (!customElements.get('know-how-computer')) {
// Define the KnowHowComputer Web Component
class KnowHowComputerElement extends HTMLElement {
static observedAttributes = ['no-help'];
/**
* @typedef {object} MemoryConfiguration
* @property {string} name
* @property {Record<int,string>} program
*/
/** @type {MemoryConfiguration[]} */
#memoryConfigurations = [];
/** @type {?MutationObserver} */
#observer;
/** @type {int} */
#selectedMemory;
constructor() {
super();
this.attachShadow({mode: 'open'});
// Add the HTML template and styles to the shadow DOM
this.shadowRoot.innerHTML = `<style>
:host {
display: block;
margin-block:1rem;
}
.no-help .khc-help {
display: none;
}
:host > div {
display: grid;
gap: 1rem;
grid-template-areas:
"nav nav "
"main side ";
grid-template-columns: 30ch auto;
}
#memory-selector:not(:has(> button)) {
display: none;
}
code {
font-weight: bolder;
}
nav {
grid-area: nav;
display: flex;
gap: 0.5em;
> input {
max-height: 2rem;
}
}
main {
grid-area: main;
display: flex;
flex-direction: column;
fieldset {
display: flex;
flex-direction: column;
gap:0.5rem;
}
}
aside {
grid-area: side;
}
details {
font-size: 0.95rem;
}
details, details > summary {
display: revert;
}
details > summary {
font-weight: bolder;
font-size: 1.15rem;
}
@media (max-width: 600px) {
:host > div {
display: flex;
flex-direction: column;
grid-template-areas: "title" "nav" "main" "side";
}
nav {
flex-direction: column;
}
}
</style>
<div>
<nav class="steueritem">
<button class="mw-ui-button"
type="button"
id="neustartschalter"
aria-describedby="descr-neustartschalter">Neustart F8</button>
<button class="mw-ui-button"
type="button"
id="ausfuehrenschalter"
aria-describedby="descr-ausfuehrenschalter">Ausführen F9</button>
</nav>
<main>
<label for="programmzaehleranzeigefeld">Programmzähler</label>
<input type="text"
id="programmzaehleranzeigefeld"
aria-describedby="descr-programmzaehleranzeigefeld"
readonly
disabled/>
<label for="speicheranzeigefeld">Hauptspeicher</label>
<textarea id="speicheranzeigefeld"
aria-describedby="descr-speicheranzeigefeld">
</textarea>
<fieldset id="memory-selector">
<legend>Speicherkonfiguration laden:</legend>
</fieldset>
</main>
<aside class="khc-help">
<details open>
<summary><span is="h2">Bedeutung der KHC Befehle</span></summary>
<dl>
<dt><code>inc x</code></dt>
<dd>Erhöhe den Wert in Zelle x um 1 <br/>
und erhöhe den Programmzähler um 1
</dd>
<dt><code>dec x</code></dt>
<dd>Verringere den Wert in Zelle x um 1 <br/>
und erhöhe den Programmzähler um 1
</dd>
<dt><code> isz x</code></dt>
<dd>Wenn Zelle x den Wert 0 (<span lang="en">zero</span>) enthält,<br/>
dann erhöhe den Programmzähler um 2,<br/>
sonst erhöhe den Programmzähler um 1
</dd>
<dt><code>jmp x</code></dt>
<dd>Setze den Programmzähler auf den Wert x</dd>
<dt><code>stp </code></dt>
<dd>Beende das Programm</dd>
</dl>
</details>
<details>
<summary>Über den Know-How-Computer</summary>
<p>Die Idee zum Know-How-Computer wurde im Jahr 1983 von Wolfgang Back (WDR) und Ulrich Rohde (PC-Magazin)
entwickelt. Veröffentlicht wurde das Konzept des KHC u.a. in den Computerzeitschriften MC und
PC-Magazin. Weitere Informationen findet man auf <a
href="https://de.wikipedia.org/wiki/Know-how-Computer"
target="_blank">de.wikipedia.org/wiki/Know-how-Computer</a>
</p>
<p>Eine leicht abgewandelte Form des Know-How-Computers ist der so genannte Murmelrechner, der auf der Seite
<a
href="https://www.inf-schule.de/rechner/bonsai/murmelrechner" target="_blank">
www.inf-schule.de/rechner/bonsai/murmelrechner</a> vorgestellt wird.
</p>
<p>Diese JavaScript-Implementierung des Know-How-Computers ist auch für Menschen mit Seheinschränkungen
barrierefrei zugänglich. Sie wurde 2025 von Ulrich Kalina erstellt.
</p>
</details>
<details>
<summary>Kurzanleitung zu dieser JavaScript Implementierung des KHC</summary>
<p>Die Bedienoberfläche des KHC besteht in dieser Implementierung aus folgenden fünf
Elementen, die mit TAB bzw. UMSCHALT+TAB fokussiert (angewählt) werden können.</p>
<h3>Mehrzeiliges Textfeld Hauptspeicher</h3>
<p id="descr-speicheranzeigefeld">bildet den Hauptspeicher des Know-How-Computers ab. Jede Zeile des
Textfeldes enthält
die
Nummer (Adresse) einer Speicherzelle und deren Inhalt. Dabei gehört die Adresse (Zahl vor dem
Doppelpunkt) selbst nicht zum Inhalt der Speicherzelle, sondern dient lediglich ihrer eindeutigen
Bezeichnung. Die Zelleninhalte (hinter dem Doppelpunkt) können entweder KHC-Befehle oder Daten
(Zahlenwerte von 0 bis 99) sein.<br>
Der gesamte Inhalt des Textfeldes kann über Tastatureingaben verändert werden. Dabei muss
in jeder
Zeile die Abfolge Adresse, Doppelpunkt, Befehl oder Datenwert eingehalten werden. Da es sich um ein
normales
Textfeld handelt, kann sein gesamter Inhalt, also ein ganzes KHC-Programm einfach durch Kopieren und
Einfügen
("Drag and Drop") aus einem Texteditor in den KHC-Speicher importiert oder umgekehrt in einen Editor
exportiert und in einer Textdatei gespeichert werden.<br>Der importierte Programmtext darf
Kommentare
enthalten. Diese beginnen mit einem Semikolon ; und reichen bis zum Zeilenende. Die Kommentare
werden
automatisch entfernt, wenn der Fokus das mehrzeilige Textfeld verlässt.<br>
Wenn in diesem Textfeld Programmzeilen verändert wurden, sollte vor der nächsten
Programmausführung
ein Neustart mit dem Neustart-Schalter (F8) durchgeführt werden.
</p>
<h3>Einzeiliges Textfeld Programmzähler</h3>
<p id="descr-programmzaehleranzeigefeld">enthält eine Kombination aus Programmzähler und
Befehlsregister. Im
Befehlsregister
(hinter dem Doppelpunkt) steht immer der aktuelle Befehl, der im nächsten Schritt ausgeführt
werden soll. Der Programmzähler (vor dem Doppelpunkt) gibt die Adresse der Speicherzelle an,
von der
dieser Befehl ins Befehlsregister geladen wurde.
</p>
<h3>Neustart-Schalter (F8)</h3>
<p id="descr-neustartschalter">setzt den Programmzähler auf den Wert 1 zurück und lädt
den Befehl aus
der Zelle
1 des Hauptspeichers in das Befehlsregister. Der Inhalt des Hauptspeichers wird durch einen Neustart
nicht
verändert. Der Neustart-Schalter kann auch mit der F8-Taste aktiviert werden.
</p>
<h3>Ausführen-Schalter (F9)</h3>
<p id="descr-ausfuehrenschalter">führt den aktuellen Befehl im Befehlsregisters aus und lädt
anschließend
den nächsten
Befehl aus dem Hauptspeicher dorthin. Der Ausführen-Schalter kann auch mit der F9-Taste
aktiviert
werden.
</p>
</details>
</aside>
</div>`;
STYLESHEET_HREFS.map(function (href) {
const styleElem = document.createElement("link");
styleElem.setAttribute("href", href);
styleElem.setAttribute("rel", "stylesheet");
return styleElem;
}).forEach(linkElem => this.shadowRoot.prepend(linkElem));
// Create the KnowHowComputer instance
this.khc = new KnowHowComputer(this.shadowRoot);
}
refreshMemoryConfigurations() {
this.#memoryConfigurations = [
...this.parseMemoryElements()
];
this.refreshMemorySelector(this.#memoryConfigurations);
if(!this.#selectedMemory){
if (this.khc && this.#memoryConfigurations[0]
) {
this.khc.loadProgram(this.#memoryConfigurations[0].program);
}
}
}
refreshHelp() {
this.shadowRoot.querySelector(':host > div')
.classList
.toggle('no-help', this.hasAttribute('no-help'));
}
// noinspection JSUnusedGlobalSymbols
attributeChangedCallback(_name, _oldValue, _newValue) {
this.refreshHelp()
}
// Lifecycle callbacks
// noinspection JSUnusedGlobalSymbols
connectedCallback() {
// Component is now in the DOM
// Parse khc-memory child elements
this.refreshMemoryConfigurations();
this.#observer = new MutationObserver(_ => {
this.refreshMemoryConfigurations();
});
this.#observer.observe(this, {childList: true});
// Add event listener for memory selector
const memorySelector = this.shadowRoot.getElementById('memory-selector');
memorySelector.addEventListener('click', (e) => {
const selectedValue = e.target.value;
// Load program from khc-memory elements
const index = parseInt(selectedValue);
if (index >= 0 && index < this.#memoryConfigurations.length) {
this.#selectedMemory = index;
this.loadMemoryConfiguration(this.#memoryConfigurations[index]);
}
e.preventDefault();
});
this.refreshMemoryConfigurations();
this.refreshHelp();
}
// noinspection JSUnusedGlobalSymbols
disconnectedCallback() {
// Component is removed from the DOM
// Clean up any event listeners if needed
this.#observer?.disconnect();
}
// Parse khc-memory child elements
parseMemoryElements() {
// Use Array.from to convert HTMLCollection to Array
return Array.from(this.querySelectorAll('khc-memory'))
.map((element, idx) => {
const programText = element.textContent.trim();
const [name, program] = this.parseProgram(programText);
return {
name: name ?? `Program ${idx + 1}`,
program: program
};
});
}
// Parse program text into a program object
parseProgram(programText) {
const lines = programText.split('\n')
.map(line => line.trim())
.filter(line => line !== '');
let name =
lines.slice(0, 1)
.filter(l => l.startsWith('#'))
.map(l => l.replace(/^#*/, ''))
.map(l => l.trim())
.find(l => l !== '') ?? null;
return [
name,
Object.fromEntries(
lines
.filter(l => !l.startsWith('#')) // filter out comments
.map(l => l.split(':', 2).map(s => s.trim())) // split and trim parts
.filter(([address, instruction]) => (address && instruction))) // filter incomplete lines;
];
}
// Populate memory selector dropdown
refreshMemorySelector(memoryConfigurations) {
const memorySelector = this.shadowRoot.getElementById('memory-selector');
memorySelector.querySelectorAll('button').forEach(button => memorySelector.removeChild(button));
// Add options for each memory configuration
memoryConfigurations.forEach((config, index) => {
const button = document.createElement('button');
button.value = (index).toString();
button.textContent = config.name;
button.type = 'button';
button.classList.add('mw-ui-button');
memorySelector.appendChild(button);
});
}
// Load memory configuration
loadMemoryConfiguration(config) {
if (this.khc) {
this.khc.loadProgram(config.program);
}
}
}
// KnowHowComputer class that handles the emulator logic
class KnowHowComputer {
constructor(containerNode) {
// Store the container node
this.container = containerNode;
// Configuration variables
this.speicheranzahl = 25; // Standardwert ist 25 für Adr. 1-25, kann aber geändert werden. Adr. 0 wird nicht benutzt
this.maxwert = 99;
this.spaltenanzahlinspeicheranzeigefeld = 10;
this.zeilenanzahlinspeicheranzeigefeld = this.speicheranzahl; // kann auch abweichend von speicheranzahl gewählt werden
// State variables
/** @type {string[]} */
this.mem = new Array(this.speicheranzahl + 1); // mem[0] existiert, wird aber nicht benutzt
this.pz = 1; // Programmzähler
this.br = ""; // Befehlsregister
// Constants and utility variables
this.ziffern = new Set("0123456789".split(''));
this.lueckenzeichen = new Set(" \r\n\t".split(''));
this.doppelpunkt = new Set(":".split(''));
this.kommentarzeichen = new Set(":".split(''));
this.khcbefehle = new Set(["isz", "jmp", "inc", "dec", "stp"]);
this.khcbuchstaben = new Set(this.khcbefehle.values().flatMap(s => s.split('')));
// Lexer/parser state
/** @type {string[]} */
this.symtypen = [];
/** @type {string[]} */
this.symwerte = [];
this.symindex = -1;
// Error messages
this.fehlermeldungen = [];
this.fehlermeldungen[0] = "Keinen Fehler gefunden";
this.fehlermeldungen[1] = "Unerlaubtes Zeichen";
this.fehlermeldungen[2] = "Zahl erwartet";
this.fehlermeldungen[3] = "Adresse nicht im gültigen Bereich";
this.fehlermeldungen[4] = "Wert nicht im gültigen Bereich";
this.fehlermeldungen[5] = "Ungültiger Befehl";
this.fehlermeldungen[6] = "Syntaxfehler: Doppelpunkt erwartet";
this.fehlermeldungen[7] = "Syntaxfehler: Zahl oder Befehl erwartet";
this.fehlermeldungen[8] = "Syntaxfehler: stp ohne Operandenadresse ";
this.fehlermeldungen[9] = "Syntaxfehler: Operandenadresse erwartet";
this.fehlermeldungen[10] = "Laufzeitfehler: Kein ausführbarer Befehl";
this.fehlermeldungen[11] = "Laufzeitfehler: Operand würde zu klein";
this.fehlermeldungen[12] = "Laufzeitfehler: Operand würde zu groß";
// Initialize the emulator
this.init();
}
// Helper methods
meldung(index) {
alert(this.fehlermeldungen[index]);
}
stringmitleerzeichenauffuellen(str, bislaenge) { // neu 24.01.2024
return str.trimEnd().padEnd(bislaenge);
}
rechtsbuendig(s) {
return String(s).trimStart().padStart(2)
}
zahltrimmen(zahlstr, min, max) {
var test = parseInt(zahlstr);
if (isNaN(test)) {
test = min;
} else {
if (test < min) {
test = min;
this.meldung(11);
}
if (test > max) {
test = max;
this.meldung(12);
}
}
return test;
}
arrayleeren(a) {
a.splice(0)
return a;
}
// Lexer and parser
khclexer(eingabe) {
let z; // einzelnes Zeichen aus eingabe
let i;
this.symtypen = this.arrayleeren(this.symtypen);
this.symwerte = this.arrayleeren(this.symwerte);
this.symindex = -1;
let symwert = "";
let symtyp = "l"; // z =zahl, b =buchstabenkette, l =luecke, : =doppelpunkt
eingabe = " " + eingabe + " ";
let ztyp = "l";
for (i = 0; i < eingabe.length; i++) {
z = eingabe.charAt(i);
if (this.lueckenzeichen.has(z)) ztyp = "l"; // z ist ein Lückenzeichen
else if (this.ziffern.has(z)) ztyp = "z"; // z ist eine Ziffer
else if (this.doppelpunkt.has(z)) ztyp = ":"; // z ist ein Doppelpunkt
else if (this.khcbuchstaben.has(z)) ztyp = "b"; // z kommt in einem der KHC-Befehle vor
else if (this.kommentarzeichen.has(z)) ztyp = ";"; // z ist Kommentarzeichen
else return false;
if (symtyp === ztyp) { // aktueller symtyp stimmt mit dem ztyp des gerade gelesenen Zeichens z überein
symwert = symwert + z;
} else { // neues Symbol:
if (symtyp !== "l") { // bisheriges Symbol in Arrays speichern
this.symindex++;
this.symwerte[this.symindex] = symwert;
this.symtypen[this.symindex] = symtyp;
}
if (ztyp !== ";") {
symtyp = ztyp; // Anfang des neuen Symbols in sym-Vars speichern
symwert = z;
} else { // Kommentarzeichen, also aufhören:
return true;
}
}
}
return true;
}
istkhcbefehl(s) {
return this.khcbefehle.has(s);
}
istgueltigezahl(zahlstr, min, max) {
var ok = true;
var test = parseInt(zahlstr);
if (isNaN(test)) ok = false;
else {
if (test < min || test > max) ok = false;
}
return ok;
}
khcparser(zeile) {
// Rückgabewert: fehlerindex
var ok = this.khclexer(zeile);
if (this.symwerte.join("").length === 0) return -1; // Leerzeile liefert keine Fehlermeldung
if (!ok) return 1; // "Unerlaubtes Zeichen"
if (this.symtypen[0] !== "z") return 2; // "Syntaxfehler - Zahl erwartet"
if (!this.istgueltigezahl(this.symwerte[0], 1, this.speicheranzahl)) return 3; // "Adresse nicht im gültigen Bereich"
if (this.symtypen[1] !== ":") return 6; // "Syntaxfehler - Doppelpunkt erwartet"
if (this.symtypen[2] !== "z" && this.symtypen[2] !== "b") return 7; // "Syntaxfehler - Zahl oder Befehl erwartet"
if (this.symtypen[2] === "z") {
if (!this.istgueltigezahl(this.symwerte[2], 0, this.maxwert)) return 4; // "Wert nicht im gültigen Bereich"
}
if (this.symtypen[2] === "b") {
if (!this.istkhcbefehl(this.symwerte[2])) return 5; // "Ungültiger Befehl"
if (this.symwerte[2] === "stp" && this.symtypen.length > 3) return 8; // "Syntaxfehler"
if (this.symwerte[2] !== "stp") {
if (this.symtypen[3] !== "z") return 9; // "Syntaxfehler"
if (!this.istgueltigezahl(this.symwerte[3], 1, this.speicheranzahl)) return 3; // "Adresse nicht im gültigen Bereich"
}
}
return 0; // keinen Fehler gefunden
}
// Initialization and control
init() {
for (let i = 0; i < this.speicheranzahl + 1; i++) {
this.mem[i] = "0";
}
/**@type HTMLInputElement */
const registeranzeigefeld = this.container.getElementById("programmzaehleranzeigefeld");
/**@type HTMLTextAreaElement */
const speicheranzeigefeld = this.container.getElementById("speicheranzeigefeld");
speicheranzeigefeld.rows = this.zeilenanzahlinspeicheranzeigefeld;
speicheranzeigefeld.cols = this.spaltenanzahlinspeicheranzeigefeld;
registeranzeigefeld.size = this.spaltenanzahlinspeicheranzeigefeld;
this.neustart();
// Set up event listeners
this.setupEventListeners();
}
setupEventListeners() {
const self = this;
this.container.getElementById('ausfuehrenschalter').addEventListener('click', function () {
self.befehlausfuehren();
});
this.container.getElementById('neustartschalter').addEventListener('click', function () {
self.neustart();
});
this.container.getElementById('speicheranzeigefeld').addEventListener('change', function () {
self.speicherfeldanzeigeinspeicheruebernehmen();
self.neustart();
});
this.container.getElementById('speicheranzeigefeld').addEventListener('focus', function () {
self.zeileinspeicheranzeigefeldmarkieren(self.pz);
});
this.container.addEventListener('keydown', function (event) {
if (event.keyCode === 120) { // F9
self.registeranzeigefeldaktualisieren();
self.befehlausfuehren();
} else if (event.keyCode === 119) { // F8
self.speicherfeldanzeigeinspeicheruebernehmen();
self.neustart(); // 24.01.2024
}
});
}
// Load a program from a program object
loadProgram(program) {
// Reset memory
for (let i = 0; i < this.speicheranzahl + 1; i++) {
this.mem[i] = "0";
}
this.mem[0] = "nicht verwendet";
// Load program into memory
for (const [address, instruction] of Object.entries(program)) {
if (parseInt(address) >= 1 && parseInt(address) <= this.speicheranzahl) {
this.mem[address] = instruction;
}
}
// Restart the emulator
this.neustart();
}
neustart() {
this.pz = 1; // Programmzähler
this.br = this.mem[this.pz]; // Befehlsregister
this.anzeigeaktualisieren();
this.container.getElementById("speicheranzeigefeld").focus();
}
registeranzeigefeldaktualisieren() {
this.container.getElementById("programmzaehleranzeigefeld").value = this.rechtsbuendig(this.pz) + ": " + this.stringmitleerzeichenauffuellen(this.mem[this.pz], this.spaltenanzahlinspeicheranzeigefeld - 3);
}
anzeigeaktualisieren() {
// Anzeige des gesamten Speichers aktualisieren:
const speicherfeld = this.container.getElementById("speicheranzeigefeld");
speicherfeld.value = this.mem.slice(1).map((speicher, idx) => this.rechtsbuendig(idx + 1) + ": " + speicher).join('\n');
this.registeranzeigefeldaktualisieren();
this.zeileinspeicheranzeigefeldmarkieren(this.pz);
}
befehlausfuehren() {
var fehlerindex = 0;
this.speicherfeldanzeigeinspeicheruebernehmen();
console.log("mem[pz] = " + this.mem[this.pz]);
this.br = this.mem[this.pz];
var z = parseInt(this.br);
console.log("Start befehlausfuehren mit z = parseint(br): " + z);
console.log("in befehlausfuehren vor Test: pz = " + this.pz + " br = " + this.br);
// Test: Ist Inhalt von br nur eine ganze Zahl, also ein Operand? Dann keine Ausführung möglich ...
if (!isNaN(z)) {
fehlerindex = 10;
this.meldung(fehlerindex);
} else {
// sonst gehen wir von einem Befehl aus:
var befehlscode = this.br.substr(0, 3).toLowerCase();
// Bei stp ist keine Adresse erforderlich und es wird auch nichts gemacht:
if (befehlscode === "stp") return 0;
// sonst muss eine ganze Zahl als Adresse folgen:
var adresse = parseInt(this.br.substr(4)); // ab Pos. 4 bis zum Ende ausschneiden
if (isNaN(adresse)) return 2;
this.pz = parseInt(this.pz);
switch (befehlscode) {
case "jmp":
this.pz = adresse;
break;
case "isz":
if (this.isZero(this.mem[adresse])) this.pz = this.pz + 2; else this.pz = this.pz + 1;
break;
case "inc":
let wert = this.mem[adresse];
wert = parseInt(wert) + 1;
wert = this.zahltrimmen(wert, 0, this.maxwert);
this.mem[adresse] = wert;
this.pz = this.pz + 1;
break;
case "dec":
let wert2 = this.mem[adresse];
wert2 = parseInt(wert2) - 1;
wert2 = this.zahltrimmen(wert2, 0, this.maxwert);
this.mem[adresse] = wert2;
this.pz = this.pz + 1;
break;
default:
fehlerindex = 3;
}
this.pz = this.zahltrimmen(this.pz, 1, this.speicheranzahl);
this.br = this.mem[this.pz];
this.anzeigeaktualisieren();
}
this.container.getElementById("speicheranzeigefeld").focus();
this.zeileinspeicheranzeigefeldmarkieren(this.pz);
return fehlerindex;
}
isZero(zeile){
return 0 === parseInt(zeile);
}
einezeileinspeicheruebernehmen(zeile) {
zeile = zeile.toLowerCase();
var fehlerindex = 0; // vorläufig
fehlerindex = this.khcparser(zeile);
if (fehlerindex === 0) {
var pz = parseInt(this.symwerte[0]);
var befehlszeile = this.symwerte[2];
if (this.symwerte.length > 3) befehlszeile = befehlszeile + " " + this.symwerte[3];
this.mem[pz] = befehlszeile;
} else {
if (fehlerindex > 0) { // -1 wäre Leerzeile
alert(this.fehlermeldungen[fehlerindex] + " in\n" + zeile);
}
}
}
speicherfeldanzeigeinspeicheruebernehmen() {
var zeilen = this.container.getElementById("speicheranzeigefeld").value.split("\n");
for (let i = 0; i < zeilen.length; i++) {
this.einezeileinspeicheruebernehmen(zeilen[i]);
}
this.anzeigeaktualisieren();
}
setzeMarkierungInTextFeld(domElemSelector, posStart, posEnde) {
const domElem = this.container.querySelector(domElemSelector);
if ('selectionStart' in domElem) {
domElem.selectionStart = posStart;
domElem.selectionEnd = posEnde;
}
}
zeileinspeicheranzeigefeldmarkieren(pz) {
let pzstring = this.rechtsbuendig(pz) + ": ";
let startpos = this.container.getElementById("speicheranzeigefeld").value.indexOf(pzstring);
let endepos = startpos + this.mem[pz].length + 4;
this.setzeMarkierungInTextFeld("#speicheranzeigefeld", startpos, endepos);
}
}
// Register the custom elements
customElements.define('know-how-computer', KnowHowComputerElement);
}
if (!customElements.get('khc-memory')) {
// Define the KhcMemoryElement custom element
class KhcMemoryElement extends HTMLPreElement {
constructor() {
super();
// No need for shadow DOM as this is just a data container
}
// Lifecycle callbacks
// noinspection JSUnusedGlobalSymbols
connectedCallback() {
// Hide the element by default as it's just a data container
this.style.display = 'none';
}
}
customElements.define('khc-memory', KhcMemoryElement, {extends: 'pre'});
}
// Initialize when the DOM is loaded
document.addEventListener('DOMContentLoaded', function () {
// For MediaWiki gadget, this would be the initialization code
// No need to do anything here as the custom element will initialize itself when added to the DOM
});
})();
</script>
<know-how-computer
<!--{if $noHelp|validate:'boolean'|default:'false' == 'true'}-->
no-help
<!--{/if}-->
>
<!--{if $Speicherkonfiguration1|default:'' != ''}-->
<khc-memory><!--{$Speicherkonfiguration1|escape:'htmlall'}--></khc-memory>
<!--{/if}-->
<!--{if $Speicherkonfiguration2|default:'' != ''}-->
<khc-memory><!--{$Speicherkonfiguration2|escape:'htmlall'}--></khc-memory>
<!--{/if}-->
<!--{if $Speicherkonfiguration3|default:'' != ''}-->
<khc-memory><!--{$Speicherkonfiguration3|escape:'htmlall'}--></khc-memory>
<!--{/if}-->
<!--{if $Speicherkonfiguration4|default:'' != ''}-->
<khc-memory><!--{$Speicherkonfiguration4|escape:'htmlall'}--></khc-memory>
<!--{/if}-->
<!--{if $Speicherkonfiguration5|default:'' != ''}-->
<khc-memory><!--{$Speicherkonfiguration5|escape:'htmlall'}--></khc-memory>
<!--{/if}-->
</know-how-computer>
</includeonly>