Summary Files Commits Branches @commit @diff @stats
path: root / src / teaching / upp-2025 / week02 / machine_table.js
diff options
context:
space:
mode:
Commit7e77973c9d8594ee2f36a18b53f5d78d797a6686 (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-10-05 20:31:09 +0300
Committer
 Syndamia <kamen@syndamia.com>
2025-10-05 20:31:09 +0300
Files8fc21d33f95d5a9ea99fe5b436a8bd71b57759c8 /src/teaching/upp-2025/week02/machine_table.js
syndamia.com-7e77973c9d8594ee2f36a18b53f5d78d797a6686.tar
syndamia.com-7e77973c9d8594ee2f36a18b53f5d78d797a6686.tar.gz
syndamia.com-7e77973c9d8594ee2f36a18b53f5d78d797a6686.zip
Parentaf0d8026bad910e98b04f760a8d836ede8b6a1cd (diff)
feat(teaching/upp-2025/w02): Add materials and exercises
This week will be very experimental, since no real material will be taught. We could do some git or Visual Studio drills but those will be boring and limited in useability. Using this fancy table should aid in pointers and thinking about data and operations as part of the same domain.
1 files changed, 339 insertions, 0 deletions
-rw-r--r--src/teaching/upp-2025/week02/machine_table.js339
diff --git a/src/teaching/upp-2025/week02/machine_table.js b/src/teaching/upp-2025/week02/machine_table.js
new file mode 100644
index 0000000..ec67ff8
--- /dev/null
+++ b/src/teaching/upp-2025/week02/machine_table.js
@@ -0,0 +1,339 @@
+// Style //
+
+var machineStyles = document.createElement("style");
+machineStyles.textContent = `
+ /* Reset */
+ #worksheet table,
+ #worksheet tr,
+ #worksheet th,
+ #worksheet td {
+ background-color: initial;
+ border: initial;
+ padding: 0.1em;
+ }
+
+ /* General styling */
+ #worksheet th {
+ font-weight: normal;
+ }
+ #worksheet {
+ color: black;
+ background-color: white;
+ margin: 1em auto;
+
+ /* Thanks https://stackoverflow.com/a/4407335/12036073 */
+
+ -webkit-touch-callout: none; /* iOS Safari */
+ -webkit-user-select: none; /* Safari */
+ -khtml-user-select: none; /* Konqueror HTML */
+ -moz-user-select: none; /* Old versions of Firefox */
+ -ms-user-select: none; /* Internet Explorer/Edge */
+ user-select: none;
+ }
+
+ #worksheet button {
+ border-radius: 0;
+ border-width: 2px;
+ width: 100%;
+ }
+ #worksheet button:hover,
+ #worksheet input[type="radio"]:hover,
+ #worksheet input[type="radio"] ~ label:hover {
+ cursor: pointer;
+ }
+
+ /* Borders and colors */
+ #worksheet td, #worksheet th {
+ border: 1px solid #c0c0c0;
+ }
+ #worksheet tr:first-of-type th,
+ #worksheet td:first-child, #worksheet th:first-child
+ {
+ border-color: #6a6a6a;
+ background-color: #f1f1f1;
+ }
+ #worksheet tr:first-of-type th {
+ border-top: none;
+ }
+ #worksheet td:first-child, #worksheet th:first-child {
+ border-left: none;
+ }
+
+ /* Spacing */
+ #worksheet th:first-child {
+ min-width: 2.82em;
+ padding: 0.3em 0;
+ }
+ #worksheet td {
+ min-width: 8em;
+ padding: 0.3em 0.2em;
+ }
+
+ #worksheet button {
+ margin: calc(-0.3em + 4px) 0;
+ padding: 0.2em;
+ }
+
+ /* Execution */
+ .selected {
+ background-color: yellow !important;
+ }
+ .error {
+ background-color: red !important;
+ }
+`;
+document.head.appendChild(machineStyles);
+
+// Execution //
+
+const INPUT1 = document.getElementById("input1");
+const INPUT2 = document.getElementById("input2");
+const A = document.getElementById("regA");
+const B = document.getElementById("regB");
+
+const errorMsg = document.getElementById("errorMsg");
+
+var speed = 1000;
+var running = false;
+var stop = false;
+
+const memory = document.querySelectorAll("#worksheet tr:not(:first-of-type) > td:first-of-type");
+var memoryLocation = memory.length;
+var parametrisedCommand = null;
+
+function executeInit() {
+ running = true;
+ A.innerText = 0;
+ B.innerText = 0;
+ errorMsg.innerText = '';
+
+ for (let i = 0; i < memory.length; ++i) {
+ memory[i].innerText = memory[i].innerText
+ .trim()
+ .replace(' ', '')
+ .replace('\n', '');
+ memory[i].classList.remove('error');
+ memory[i].classList.remove('selected');
+ }
+}
+
+function executeDeinit() {
+ memoryLocation = memory.length;
+ parametrisedCommand = null;
+ stop = false;
+ running = false;
+ stopresumeBtn.innerText = 'Спри';
+}
+
+function expectedCommand() {
+ if (parametrisedCommand !== null) {
+ error('Очакваше се число, не команда!');
+ return false;
+ }
+ return true;
+}
+
+function expectedNumber() {
+ if (parametrisedCommand === null) {
+ error('Очакваше се команда!');
+ return false;
+ }
+ if (isNaN(toNumber(memory[memoryLocation].innerText))) {
+ error('Очакваше се валидно число!');
+ return false;
+ }
+ return true;
+}
+
+function error(msg) {
+ errorMsg.innerText = msg;
+ memory[memoryLocation].classList.add('error');
+ executeDeinit();
+}
+
+function toNumber(text) {
+ if (text === 'INPUT1') text = INPUT1.innerText;
+ else if (text === 'INPUT2') text = INPUT2.innerText;
+
+ return Number(text);
+}
+
+function execute() {
+ if (stop) {
+ running = false;
+ return;
+ }
+
+ // After cell was set
+ if (memoryLocation < memory.length) {
+ memory[memoryLocation].classList.remove('selected');
+
+ switch (parametrisedCommand) {
+ case null: break;
+ case 'ASUM':
+ if (!expectedNumber()) return;
+ A.innerText = Number(A.innerText) + toNumber(memory[memoryLocation].innerText);
+ break;
+ case 'ASUBTRACT':
+ if (!expectedNumber()) return;
+ A.innerText = Number(A.innerText) - toNumber(memory[memoryLocation].innerText);
+ break;
+ case 'BSUM':
+ if (!expectedNumber()) return;
+ B.innerText = Number(B.innerText) + toNumber(memory[memoryLocation].innerText);
+ break;
+ case 'BSUBTRACT':
+ if (!expectedNumber()) return;
+ B.innerText = Number(B.innerText) - toNumber(memory[memoryLocation].innerText);
+ break;
+ case 'GOTOBZ':
+ if (!expectedNumber()) return;
+ if (Number(B.innerText) === 0)
+ memoryLocation = toNumber(memory[memoryLocation].innerText) - 2;
+ break;
+ case 'GOTOBN':
+ if (!expectedNumber()) return;
+ if (Number(B.innerText) >= 0)
+ memoryLocation = toNumber(memory[memoryLocation].innerText) - 2;
+ break;
+ }
+
+ if (parametrisedCommand === null) {
+ switch (memory[memoryLocation].innerText) {
+ case '':
+ executeDeinit();
+ return;
+ case 'ASHOW':
+ alert(A.innerText);
+ break;
+ case 'ASUM':
+ case 'ASUBTRACT':
+ case 'GOTOBZ':
+ case 'GOTOBN':
+ case 'BSUM':
+ case 'BSUBTRACT':
+ if (!expectedCommand()) return;
+ parametrisedCommand = memory[memoryLocation].innerText;
+ break;
+ default:
+ error('Невалидна команда!');
+ return;
+ }
+ }
+ else parametrisedCommand = null;
+ }
+ // On execution start
+ else {
+ executeInit();
+ }
+
+ memoryLocation = (memoryLocation + 1) % (memory.length + 1);
+ memory[memoryLocation].classList.add('selected');
+
+ if (memoryLocation < memory.length) {
+ setTimeout(execute, speed);
+ }
+}
+
+const execBtn = document.getElementById("execute");
+execBtn.addEventListener('click', function() {
+ if (!running)
+ executeDeinit();
+ if (memoryLocation == memory.length)
+ execute();
+});
+
+const stopresumeBtn = document.getElementById('stopresume');
+stopresumeBtn.addEventListener('click', function() {
+ if (stop && !running) {
+ stop = false;
+ running = true;
+ stopresumeBtn.innerText = 'Спри';
+ execute();
+ }
+ else if (!stop && running) {
+ stop = true;
+ stopresumeBtn.innerText = 'Продължи';
+ }
+});
+
+// Disallow newline
+function preventCharacters(evt) {
+ if (evt.keyCode == 13 || evt.keyCode == 32)
+ evt.preventDefault();
+}
+for (let i = 0; i < memory.length; ++i) {
+ memory[i].addEventListener('keydown', preventCharacters);
+}
+
+// Speed //
+
+const rbFast = document.getElementById('speedChoice1');
+const rbSlow = document.getElementById('speedChoice2');
+
+rbFast.addEventListener('change', function() {
+ speed = rbFast.value;
+});
+if (rbFast.checked) speed = rbFast.value;
+
+rbSlow.addEventListener('change', function() {
+ speed = rbSlow.value;
+});
+if (rbSlow.checked) speed = rbSlow.value;
+
+// Save/load //
+
+const saveBtn = document.getElementById('save');
+const loadBtn = document.getElementById('load');
+
+saveBtn.addEventListener('click', function() {
+ let contents = '';
+ for (let i = 0; i < memory.length; ++i) {
+ contents += memory[i].innerText + '\n';
+ }
+
+ let now = new Date();
+ let datetime =
+ now.getFullYear().toString()
+ + (now.getMonth() < 10 ? '0' : '')
+ + now.getMonth().toString()
+ + (now.getDate() < 10 ? '0' : '')
+ + now.getDate()
+ + "_"
+ + now.getHours()
+ + now.getMinutes();
+
+ // Thanks https://stackoverflow.com/a/79383186
+ const download = document.createElement('a');
+ download.href = URL.createObjectURL(new Blob([contents], { type: 'text/csv' }));
+ download.download = 'Таблица_' + datetime + '.csv';
+ download.click();
+
+ URL.revokeObjectURL(download.href);
+});
+
+function loadMemory(text) {
+ let rows = text.split("\n");
+ for (let i = 0; i < memory.length; ++i) {
+ memory[i].innerText = rows[i].split(',')[0];
+ }
+}
+
+loadBtn.addEventListener('click', function() {
+ const upload = document.createElement('input');
+ upload.type = 'file';
+ upload.accept = 'text/csv';
+ upload.click();
+
+ upload.addEventListener('change', function(e) {
+ let file = e.target.files[0];
+ if (!file) return;
+
+ let reader = new FileReader();
+ reader.readAsText(file);
+
+ reader.addEventListener('load', function(e) {
+ loadMemory(e.target.result);
+ });
+ });
+});