• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

question-closed moja zmora - interpretowanie wielu lini

42 Warsaw Coding Academy
0 głosów
142 wizyt
pytanie zadane 11 czerwca 2024 w JavaScript przez niezalogowany
zamknięte 15 czerwca 2024

mecze sie z tym od paru dni ale no 

 


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*(?:"([^"]*)"|([^;]+));/;
        let Drm = /del:\s+(\w+);/;
        let Frm = /^for\((\d+),\s*(\w+)\):\[(?:"([^"]*)"|([^"\]]*))\];/;
        let NSrm = /nemos\s+(\w+)\s*=\s*\[([^\]]*?)\];/;
        let Crm = /clear:\s+(\w+);/;

////////////////////////////////////////////////////

        const MTrm = /(\w+)\s*=\s*(\w+)\[\]/;

        
///////////////////////////////////////////////////
        
        let matchW = line.match(Wrm);
        let matchS = line.match(sRm);
        let matchE = line.match(Erm);
        let matchN = line.match(Nrm);
        let matchD = line.match(Drm);
        let matchF = line.match(Frm);
        let matchNS = line.match(NSrm);
        let matchC = line.match(Crm);

//////////////////////////////////////////////////

        const MTmatch = line.match(MTrm);
        
//////////////////////////////////////////////////
    
           if (matchW) {
             
                let text = matchW[1];
                let varB = matchW[2];
        
                if (text) {
                    consol.innerHTML += text + '<br>';
                } else if (Variable.hasOwnProperty(varB)) {
                    consol.innerHTML += Variable[varB] + '<br>';
                } else {
                    error = true;
                }
            }
        
        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) {
            if (MTmatch) {
                let TimeName = MTmatch[1];
                let TimeType = MTmatch[2];
                let DATETIMES = new Date(); 
        
                switch (TimeType) {
                    case 'TimeDate':
                        Variable[TimeName] = `${DATETIMES.getHours()}.${DATETIMES.getMinutes()}.${DATETIMES.getSeconds()}|${DATETIMES.getDate()}.${DATETIMES.getMonth() + 1}.${DATETIMES.getFullYear()}`;
                        break;
                    case 'Time':
                        Variable[TimeName] = `${DATETIMES.getHours()}.${DATETIMES.getMinutes()}.${DATETIMES.getSeconds()}`;
                        break;
                    case 'Date':
                        Variable[TimeName] = `${DATETIMES.getDate()}.${DATETIMES.getMonth() + 1}.${DATETIMES.getFullYear()}`;
                        break;
                    default:
                        error = true;
                        break;
                }
            } else {
                let variableName = matchN[1];
                let variableValue = matchN[2];
                Variable[variableName] = variableValue;
        }
    }      else if (matchD) {
            let varDB = matchD[1];
            if (Variable.hasOwnProperty(varDB)) {
                delete Variable[varDB];
            }
        }
          else if (matchF){
              console.log(matchF);
              let li = matchF[1];
              let opr = matchF[2];
              let warc = matchF[3];
              let warcs = matchF[4];

            for (let i = 1; i <= li; i++) {

                switch (opr) {
                    case 'write':

                        if (warc) {
                            consol.innerHTML += `${warc}<br>`;
                        } else if (Variable.hasOwnProperty(warcs)) {
                            consol.innerHTML += `${Variable[warcs]}<br>`;
                        } else {
                            error = true;
                        }
                        break;
                    default:
                        error = true;
                        break;
                }

            }
        }
          else if (matchNS){
            let arrayname = matchNS[1];
              const elm = matchNS[2].split(',').map(element => element.trim());


              Variable[arrayname] = elm;
          }
            else if(matchC){
                
            }
          
          else {
            error = true;
        }
    }

    if (error) {
        consol.innerHTML = 'Error!';
    }
}

 

wiem ten kod jets nie do koncony i nie działa dokładnie jeszce ale no 

gdy kod jest podzielony na linie a kazda linia interpretowana osobno to co zrobic by interpretowało komendy wieloliniowe 

komentarz zamknięcia: na szczescie znam juz odpowiedz
komentarz 11 czerwca 2024 przez niezalogowany
sam nie mam pomysłu ani nie wiem

Podobne pytania

0 głosów
2 odpowiedzi 256 wizyt
pytanie zadane 8 czerwca 2024 w JavaScript przez niezalogowany
0 głosów
0 odpowiedzi 166 wizyt
pytanie zadane 7 czerwca 2024 w JavaScript przez niezalogowany
–1 głos
1 odpowiedź 251 wizyt
pytanie zadane 30 czerwca 2024 w JavaScript przez niezalogowany

93,395 zapytań

142,389 odpowiedzi

322,569 komentarzy

62,753 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

VMware Cloud PRO - przenieś swoją infrastrukturę IT do chmury
...