witam zacząłem tworzyc zmienne i same zmienne udało mi sie stworzyc tylko mam problem z uzyciem write do wypisania zmiennej udało mi sie podzielic na 2 1 to z "" a 2 to bez "" ale ogólnie gdy wypisuje zawartosc obiektu to undefined choc wszystko niby sie zgadza
if (matchW) {
console.log(Variable);
console.log(matchW);
let text = matchW[1];
let varB = matchW[2];
if (text){
consol.innerHTML += text + '<br>';
}
else if (varB){
console.log(varB)
consol.innerHTML += Variable[varB] + '<br>';
}
}
czy ktos pomoze ?
dodatkowe dane:
- Object
- "hej ": "hello"
- [[Prototype]]: Object
SCRIPT.js:86
- Array(3)
- 0: "write: hej"
- 1: undefined
- 2: "hej"
- groups: undefined
- index: 0
- input: "write: hej;"
- length: 3
- [[Prototype]]: Array(0)
SCRIPT.js:94hej
SCRIPT.js:96undefined
a w edytor wpisuje nemo hej = "hello";
write: hej; a to cały kod oprócz funkcji jak show menu import i export
function runCode() {
const code = document.getElementById('editor').innerText;
const consol = document.getElementById('console');
consol.innerHTML=' ';
interpret(code, consol);
}
function interpret(code, consol) {
let lines = code.split('\n');
let error = false;
const Variable = {};
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
let Wrm = /write\s*:\s*(?:"([^"]*)"|(\w+))/;
let sRm = /sum:\s*((\d+)(?:\s*,\s*\d+)*)\s*;/;
let Erm = /equal:\s*(\d+)\s*,\s*(\d+)\s*\[([+\-*\/])\];/;
let Nrm = /nemo\s+([\w\s]+)\s*=\s*"(.*?)";/;
let matchW = line.match(Wrm);
let matchS = line.match(sRm);
let matchE = line.match(Erm);
let matchN = line.match(Nrm);
if (matchW) {
console.log(Variable);
console.log(matchW);
let text = matchW[1];
let varB = matchW[2];
if (text){
consol.innerHTML += text + '<br>';
}
else if (varB){
console.log(varB);
consol.innerHTML += Variable[varB] + '<br>';
}
} else if (matchS) {
let numer = matchS[1].split(',').map(Number);
let sum = 0;
for (let j = 0; j < numer.length; j++) {
sum += numer[j];
}
consol.innerHTML += `${sum} <br>`;
} else if (matchE) {
let vl1 = parseFloat(matchE[1]);
let vl2 = parseFloat(matchE[2]);
let pseudooperator = matchE[3];
let result;
switch (pseudooperator) {
case '+':
result = vl1 + vl2;
break;
case '-':
result = vl1 - vl2;
break;
case '*':
result = vl1 * vl2;
break;
case '/':
result = vl1 / vl2;
break;
default:
error = true;
break;
}
consol.innerHTML += `${result}<br>`;
} else if (matchN) {
let variableName = matchN[1];
let variableValue = matchN[2];
Variable[variableName] = variableValue;
}
else {
error = true;
}
}
if (error) {
consol.innerHTML = 'Error!';
}
}