0

I am making my own programming language using JS but when I attempt to put multiple words where it is expected to be an error, it has no errors, I don't know why .includes only gets one string and skips the others, could I get any help on the subject??

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>HyperAnimator</title>
    </head>
    <body>
        <h1>HyperAnimator:</h1>
        <br>
        <b>Dark mode: <button onclick="DarkMode()">Toggle dark mode</button>
        <br>
        <br>
        <b>Input code:</b>
        <br>
        <!--<input type="text" id="Code">-->
        <textarea id="Code">
Main Anim[] class=>

<=ClassEnd</textarea>
        <br>
        <button onclick="Compile()">Compile</button>
        <br>
        <br>
        <div id="Compiled">

        </div>
    </body>
</html>

CSS:

h1 {
font-size: 25px
}
#Code {
border: 5px solid black;
font-family: Arial, Helvetica, sans-serif;
font-weight: bolder;
height: 200px;
width: 400px;
overflow: hidden;
text-align: left;
color: black;
}
body {
background-color: white;
color: black;
font-size: 25px;
}
.dark-mode {
background-color: black;
color: white;
}
button {
background-color: white;
border: 5px solid black;
font-weight: bolder;
}
button:hover {
background-color: lightgrey;
}
b {
font-size: 20px;
}
#Compiled {
    border: 5px solid black;
    height: 200px;
    width: 400px;
    background-color: white;
    color: black;
}
textarea {
    max-width: 400px;
    max-height: 200px;
}

JS:

let ClassTagAmount = 0;
let CurrentClassSelected = "none";
let Code = document.getElementById("Code").value;
function Show(code){
    document.getElementById("Compiled").innerHTML = code;
}
function DarkMode() {
    var element = document.body;
    element.classList.toggle("dark-mode");
}
function Compile() {
    ClassTagAmount = 0;
    let Code = document.getElementById("Code").value;
    if (Code.includes("Main Anim[] class=>") == true){
        ClassTagAmount += 1;
    }
    if (Code.includes("<=ClassEnd") == true){
        ClassTagAmount -= 1;
    }
    if (ClassTagAmount == 0){
        console.log(Code)
        console.log(ClassTagAmount)
    }else{
        Show("Syntax Error: Unexpected missing starting or ending class token")
        console.log(ClassTagAmount)
    }
}

Whenever I put multiple of the same string, it should count both of the same strings and make the ClassTagAmount variable become -1 or 1 but instead it is 0, when it is only one string it works but with multiple strings it just becomes 0.

EG: Test A(

Input:

Main Anim[] class=>
    
<=ClassEnd

Output:

Main Anim[] class=>
    
<=ClassEnd
0

)

Test B(

Input:

Main Anim[] class=>

Output:

Syntax Error: Unexpected missing starting or ending class token
1

)

but...

Test C(

Input:

Main Anim[] class=>
Main Anim[] class=>
<=ClassEnd

Error Output:

Main Anim[] class=>
Main Anim[] class=> //this line is being forgotten.
<=ClassEnd
0

Expected Output:

Syntax Error: Unexpected missing starting or ending class token
1

)

I was expecting so there is an equal amount of the two tags, Main Anim[] class=> and <=ClassEnd but when I put these two not equally then the program thinks it is an equal amount, I expected an error as it is not equal to eachother as either there is two start tags and one end tag or one start tag and two end tags, thus is should output 1 and the error but instead it shows 0 as it seemed to skip all other text except of the closest one.

  • Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Dec 21 '22 at 10:25
  • Nothing is being ignored or forgotten. [`includes`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes) checks if a substring is included in a string. `"xy"` includes `"x"`. `"xxxxxxxxy"` also includes `"x"`. You never _count_ the number of substrings. Learn how to utilize [formal grammars](//en.wikipedia.org/wiki/Formal_grammar) and [abstract syntax trees](//en.wikipedia.org/wiki/Abstract_syntax_tree), otherwise your journey in making a programming language is not going to fare well. – Sebastian Simon Dec 21 '22 at 10:27
  • @SebastianSimon I know but I need to know how much of the same string there is so I can find out if the syntax is wrong because I am trying to interpret from programming languages like C# which has the default class. – GMD Gooodsoups Dec 21 '22 at 10:29

0 Answers0