CodeWalrus

Development => Web => Topic started by: c4ooo on October 26, 2015, 02:33:15 am

Title: [ti-83+/84+]Finding missing ends in tibasic [web][ti-basic] [javascript]
Post by: c4ooo on October 26, 2015, 02:33:15 am
On par with @123outerme 's request, i wrote a small program in JS to count up the ends in and indent ti-basic source code, and say something if there are too many, or an extra one is needed. You can try/use it at https://jsfiddle.net/th4hnpmh/ (Changed to latest version)
All the code is here:
Spoiler: ShowHide


<!DOCTYPE html>
<head>
    <script>
        function test() {
            var text = document.getElementById('text').value;
            var i = 0;
            var line = 0;
            for (var x = 0; x < text.length; x++) {
   var substring = text.substring(x).toLowerCase();
                if (text.charAt(x) == "\n") {
                    line++;
                    while(text.charAt(x+1) == ' '){
                     text = text.slice(0,x+1) + text.slice(x+2);
                    }
                substring = text.substring(x).toLowerCase();
                    if(substring.substring(1).startsWith("end")) {i--;}
var count=i;
if(substring.substring (1).startsWith("else")) {count--;}
                    for (var insert = 0; insert < count; insert++) {
                      text=text.slice(0,x+1)+" "+text.slice(x+1);
                    }
                   document.getElementById('text').value = text;
                }
                if (substring.startsWith("for")) {
                    i++;
                }
                if (substring.startsWith("then")) {
                    i++;
                }
                if (substring.startsWith("while")) {
                    i++;
                }
                if (substring.startsWith("repeat")) {
                    i++;
                }
                if (i == -1) {
                    alert("To many 'end's!\n" + "Line: " + line);
                    setCaretPosition("text", x);
                    insertAtCaret("text", "REMOVE thiS->")
                    break;
                }
                // alert(i);
            }
            if (i > 0) {
                alert("Missing an End!!!")
            }
        }

        function setCaretPosition(elemId, caretPos) {
            var elem = document.getElementById(elemId);

            if (elem != null) {
                if (elem.createTextRange) {
                    var range = elem.createTextRange();
                        range.move('character', caretPos);
                    range.select();
                } else {
                    if (elem.selectionStart) {
                        elem.focus();
                        elem.setSelectionRange(caretPos, caretPos);
                    } else elem.focus();
                }
            }
        }

        function insertAtCaret(areaId, text) {
            var txtarea = document.getElementById(areaId);
            var scrollPos = txtarea.scrollTop;
            var caretPos = txtarea.selectionStart;

            var front = (txtarea.value).substring(0, caretPos);
            var back = (txtarea.value).substring(txtarea.selectionEnd, txtarea.value.length);
            txtarea.value = front + text + back;
            caretPos = caretPos + text.length;
            txtarea.selectionStart = caretPos;
            txtarea.selectionEnd = caretPos;
            txtarea.focus();
            txtarea.scrollTop = scrollPos;
        }
    </script>
</head>
<body>
    <textarea rows="40" cols="50" id="text">type here</textarea>
   

    <button type="button" onClick="test()">Count!</button>
</body>

</html>

Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on October 26, 2015, 06:07:06 am
The best way to count missing ends is to indent your code, but sadly that is not possible when programming on-calc (it would actually make it harder to read the code, because the screen is so small, plus it would take too much RAM space). I think that such tool was suggested long ago, but I don't remember if it was ever implemented. The only closest thing I can remember was a variable usage detection program, which told you if a variable was in use in your BASIC program.


Also, sometimes loops will not stop prematurely due to missing End instructions, but rather due to a conditional error, such as the > and < signs being inverted as a typo, or variable conflicts.
Title: Re: Finding missing ends in tibasic
Post by: Ivoah on February 24, 2016, 01:58:44 am
Quote from: DJ Omnimaga on October 26, 2015, 06:07:06 am
The best way to count missing ends is to indent your code, but sadly that is not possible when programming on-calc (it would actually make it harder to read the code, because the screen is so small, plus it would take too much RAM space). I think that such tool was suggested long ago, but I don't remember if it was ever implemented. The only closest thing I can remember was a variable usage detection program, which told you if a variable was in use in your BASIC program.


Also, sometimes loops will not stop prematurely due to missing End instructions, but rather due to a conditional error, such as the > and < signs being inverted as a typo, or variable conflicts.

Necro bump  >:D
You can indent you code with ':', but as you mentioned it wastes space and slows it down.
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on February 24, 2016, 02:50:12 am
Nah using : instead of a linebreak actually takes the same amount of space. I don't know if it slows execution down, but it considerably speeds up the time it takes to scroll through the code on color models.
Title: Re: Finding missing ends in tibasic
Post by: c4ooo on March 03, 2016, 08:10:47 pm
Quote from: DJ Omnimaga on February 24, 2016, 02:50:12 am
Nah using : instead of a linebreak actually takes the same amount of space. I don't know if it slows execution down, but it considerably speeds up the time it takes to scroll through the code on color models.

The ":" does not work with return statement though, ive had this problem in the Lazer I.
Ex:

If A=1
Then
Blah
Blah
Return
End
If A=2
Then
Blah
Blah
Return
End
If A=3
Then
Blah
Blah
Return
End

works fine but

If A=1:Then:Blah:Blah:Return:End:If A=2:Then:Blah:Blah:Return:End:If A=3:Then:Blah:Blah:Return:End

wont work correctly >_>
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 03, 2016, 08:13:15 pm
Really? I never noticed that before. I thought that : only failed with the Lbl command... ???

Is that a newly introduced OS bug? Can anyone else reproduce? You should make a screenshot
Title: Re: Finding missing ends in tibasic
Post by: c4ooo on March 03, 2016, 08:19:49 pm
Quote from: DJ Omnimaga on March 03, 2016, 08:13:15 pm
Really? I never noticed that before. I thought that : only failed with the Lbl command... ???

Is that a newly introduced OS bug? Can anyone else reproduce? You should make a screenshot

I know its present in 2.55MP, dont know about earlier OSs.
Also there was a cemetech post about this exact bug.
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 13, 2016, 06:39:02 am
I'll try to remember to try reproducing the bug on various OS'es when I have soms time.

OS 1.12 had that weird glitch where :Then with an extra : caused a syntax error, but it was fixed afterwards.

The Omni topic might mention it. It has plenty of ancient bugs.
Title: Re: [ti-83+/84+]Finding missing ends in tibasic [web][ti-basic] [javascript]
Post by: c4ooo on February 05, 2017, 07:08:06 pm
Update time: your code will now be auto indented!
https://jsfiddle.net/L33gnLk9/

Code:
Spoiler: ShowHide

<!DOCTYPE html>
<head>
    <script>
        function test() {
            var text = document.getElementById('text').value;
            var i = 0;
            var line = 0;
            for (var x = 0; x < text.length; x++) {
                if (text.charAt(x) == "\n") {
                    line++;
                    while(text.charAt(x+1) == ' '){
                     text = text.slice(0,x+1) + text.slice(x+2);
                    }
                var substring = text.substring(x).toLowerCase();
                    if(substring.substring(1).startsWith("end")) {i--;}
                    for (var insert = 0; insert < i; insert++) {
                      text=text.slice(0,x+1)+" "+text.slice(x+1);
                    }
                   document.getElementById('text').value = text;
                }
                if (substring.startsWith("for")) {
                    i++;
                }
                if (substring.startsWith("then")) {
                    i++;
                }
                if (substring.startsWith("while")) {
                    i++;
                }
                if (substring.startsWith("repeat")) {
                    i++;
                }
                if (substring.startsWith("end")) {
                   // i--;
                }
                if (i == -1) {
                    alert("To many 'end's!\n" + "Line: " + line);
                    setCaretPosition("text", x);
                    insertAtCaret("text", "REMOVE thiS->")
                    break;
                }
                // alert(i);
            }
            if (i > 0) {
                alert("Missing an End!!!")
            }
        }

        function setCaretPosition(elemId, caretPos) {
            var elem = document.getElementById(elemId);

            if (elem != null) {
                if (elem.createTextRange) {
                    var range = elem.createTextRange();
                        range.move('character', caretPos);
                    range.select();
                } else {
                    if (elem.selectionStart) {
                        elem.focus();
                        elem.setSelectionRange(caretPos, caretPos);
                    } else elem.focus();
                }
            }
        }

        function insertAtCaret(areaId, text) {
            var txtarea = document.getElementById(areaId);
            var scrollPos = txtarea.scrollTop;
            var caretPos = txtarea.selectionStart;

            var front = (txtarea.value).substring(0, caretPos);
            var back = (txtarea.value).substring(txtarea.selectionEnd, txtarea.value.length);
            txtarea.value = front + text + back;
            caretPos = caretPos + text.length;
            txtarea.selectionStart = caretPos;
            txtarea.selectionEnd = caretPos;
            txtarea.focus();
            txtarea.scrollTop = scrollPos;
        }
    </script>
</head>
<body>
    <textarea rows="40" cols="50" id="text">type here</textarea>
   

    <button type="button" onClick="test()">Count!</button>
</body>

</html>
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on February 05, 2017, 07:22:19 pm
I typed While 1: Goto 0:End with line breaks and then clicked Count and nothing happened. I am using Opera 42
Title: Re: [ti-83+/84+]Finding missing ends in tibasic [web][ti-basic] [javascript]
Post by: c4ooo on February 05, 2017, 07:47:35 pm
Looks like code wasn't updated.
This should work: https://jsfiddle.net/th4hnpmh/
BTW colens might act weird.
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 11, 2017, 12:39:30 am
By the way @c4ooo I tried the second link and it still does nothing.
Title: Re: Finding missing ends in tibasic
Post by: c4ooo on March 11, 2017, 01:03:27 am
Quote from: DJ Omnimaga on March 11, 2017, 12:39:30 am
By the way @c4ooo I tried the second link and it still does nothing.

Are you sure? Its working for me  :(
This is the latest link: https://jsfiddle.net/th4hnpmh/
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 11, 2017, 06:17:37 am
Yeah it does nothing for me. I think cross-browser compatibility is the problem. In which browser have you tested this?
Title: Re: [ti-83+/84+]Finding missing ends in tibasic [web][ti-basic] [javascript]
Post by: p2 on March 11, 2017, 09:04:00 pm
it loads tons of external scripts like from google. some of us might want to recheck the filter settings as well ;)
Title: Re: Finding missing ends in tibasic
Post by: c4ooo on March 12, 2017, 02:42:42 pm
Quote from: DJ Omnimaga on March 11, 2017, 06:17:37 am
Yeah it does nothing for me. I think cross-browser compatibility is the problem. In which browser have you tested this?

Works in chrome on windows and android. ;)
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 12, 2017, 03:53:49 pm
Yea I think either I'm not using it properly or it's an Opera-specific issue.
Title: Re: [ti-83+/84+]Finding missing ends in tibasic [web][ti-basic] [javascript]
Post by: p2 on March 21, 2017, 12:04:34 am
you're probably the last member using an opera browser here  dj O.O
I thought Opera was long dead xD
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 22, 2017, 04:48:46 pm
The original Opera is dead. The new one using Chromium engine is well alive, though.
Title: Re: [ti-83+/84+]Finding missing ends in tibasic [web][ti-basic] [javascript]
Post by: p2 on March 23, 2017, 10:14:56 am
wut? So it's basically Chromium (which is google chrome without google) with an Opera User Interface stickd on top of it...?
I didnt know such a thing existed xD
This sounds surprisingly stupid and creative at the same time...  :ninja:
Title: Re: Finding missing ends in tibasic
Post by: DJ Omnimaga on March 23, 2017, 04:00:09 pm
Yeah a lot of long time users were pissed at first because the engine change also resulted into the loss of some features.