gtBASIC

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
at67
Posts: 384
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

You probably can't use comments in compound initialisers, I'd have to check the parser. What's most likely happening is that the first comment breaks the parser and the initialiser uses the last valid field to fill in the rest of the array, (which is a feature).

So in your case 0x00 is the last valid value, so the entire array gets filled with 0x00.

This should work:

Code: Select all

DIM PHRASETAB1(129) = {0x00,
                       0x05,0x06,0x02 OR EOP,
                       0x05,0x06,0x03 OR EOP,
                       0x04,0x02 OR EOP,
                       0x04,0x03 OR EOP,
                       0x06,0x07,0x02 OR EOP}
P.S. EOP has to be a constant.
wbushby
Posts: 48
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

Ok, Thank you for that. I was pretty sure there was no alternative. I have a few hundred lines to modify the descriptions on now :)
at67
Posts: 384
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

I just checked the parser and it is broken for both comments and operators in compound initialisers, (so you can't use the OR statement). I have fixed it my end in my current version of the compiler, but that it isn't going to be released for a while.

If you need comments and operators within compound initialisers and are willing to make a couple of gtBASIC source code changes and do a re-compile, then I can show you which files to modify here.

This is for Ver 1.0.9R, (Release, runtime 0104), which is the current version in the main gigatron repo under Contrib/at67.

Comment out line 2529 in keywords.cpp, so it should look like this:

Code: Select all

            std::string initText = codeLine._code.substr(equalsPos + 1);
            //Expression::stripWhitespace(initText);
            std::vector<std::string> initTokens = Expression::tokenise(initText, ',', true);
Modify lines 333 to 346 in compiler.cpp, so that it changes from this:

Code: Select all

            std::getline(infile, lineToken);
            Input inp = {true, lineToken};

            // Compound statement must span multiple lines
            size_t lbra, rbra;
            lineToken = Expression::removeCommentsNotInStrings(lineToken);
            if(Expression::findMatchingBrackets(lineToken, 0, lbra, rbra, '{'))
            {
                fprintf(stderr, "Compiler::readInputFile() : '%s:%d' : compound statement must span multiple lines\n", filename.c_str(), numLines+1);
                return false;
            }

            // Append lines into a compound statement
            int compoundLength = 0;
To this:

Code: Select all

            // Compound statement must span multiple lines
            size_t lbra, rbra;
            std::getline(infile, lineToken);
            lineToken = Expression::removeCommentsNotInStrings(lineToken);
            if(Expression::findMatchingBrackets(lineToken, 0, lbra, rbra, '{'))
            {
                fprintf(stderr, "Compiler::readInputFile() : '%s:%d' : compound statement must span multiple lines\n", filename.c_str(), numLines+1);
                return false;
            }

            // Append lines into a compound statement
            int compoundLength = 0;
            Input inp = {true, lineToken};
Then recompile.
wbushby
Posts: 48
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

I will modify it. Thank you for such a prompt and positive response
wbushby
Posts: 48
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

That worked perfectly. Thank you
wbushby
Posts: 48
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

I have another issue. I have some code in a subroutine where there is a GOTO another subroutine2 but when the subroutine2 hits RETURN it processes the next line as after the GOTO instead of the return from subroutine1. Looks like GOTO might be putting a return address on the call stack and it shouldn't ?

EXAMPLE:
X = 0x0F ' Remove title line and return
Y = 0x80
A = 7
GOTO MODDLST
PRINT "TITLE3" <----Came here after the RETURN in the subroutine MODDLST
at67
Posts: 384
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

wbushby wrote: 05 Sep 2021, 16:39 I have another issue. I have some code in a subroutine where there is a GOTO another subroutine2 but when the subroutine2 hits RETURN it processes the next line as after the GOTO instead of the return from subroutine1. Looks like GOTO might be putting a return address on the call stack and it shouldn't ?

EXAMPLE:
X = 0x0F ' Remove title line and return
Y = 0x80
A = 7
GOTO MODDLST
PRINT "TITLE3" <----Came here after the RETURN in the subroutine MODDLST
Here's some example code:

Code: Select all

_runtimePath_ "../runtime"
_runtimeStart_ &h7FFF
_codeRomType_ ROMvX0


print "TITLE0"
gosub EXAMPLE
print "TITLE4" 
end

EXAMPLE:
    print "TITLE1" 
    goto MODDLST

print "TITLE2" 
end

MODDLST:
    print "TITLE3" 
return
The compiler's output, (this is what you expect):

Code: Select all

TITLE0
TITLE1
TITLE3
TITLE4
Here is some more example code:

Code: Select all

_runtimePath_ "../runtime"
_runtimeStart_ &h7FFF
_codeRomType_ ROMvX0


print "TITLE0"
gosub EXAMPLE
gosub MODDLST
print "TITLE4" 
end

EXAMPLE:
    print "TITLE1" 
    goto MODDLST

print "TITLE2" 
end

MODDLST:
    print "TITLE3" 
return
The compiler's output, (this is what you get):

Code: Select all

TITLE0
TITLE1
TITLE3
TITLE2
The difference is that in the second example the MODDLST label has been marked as a subroutine by the compiler, because somewhere in the code you called it with a GOSUB. That label now has the PUSH instruction as it's very first instruction, do you see the problem?

You should never GOTO a label that has been marked as a subroutine, (i.e. called by a GOSUB somewhere else in your code), unless you know exactly what you are doing.

What do you think would happen if you wrote a function in a modern language and then somewhere in the code did a GOTO to it, (assuming a language that could do this)?

P.S. I can only guess as to what your code is doing as you didn't provide enough of it, but I am 89.63721% sure that this is what the issue is.
wbushby
Posts: 48
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

OK. I got it. You are 100% correct about what I was asking. I have translated an assembler program into basic. The assembler program uses a lot of techniques to reduce memory usage, this being one of them. I am working through the program flow now from start to end. I can't remember if I ever used goto and gosub with the same label when I programmed in basic or with assembler. I thought it would follow the same methodology as assembler. I can work with these rules.
I hope you don't mind these questions as I go through this exercise.
Last edited by wbushby on 05 Sep 2021, 19:51, edited 1 time in total.
at67
Posts: 384
Joined: 14 May 2018, 08:29

Re: gtBASIC

Post by at67 »

wbushby wrote: 05 Sep 2021, 19:40 OK. I got it. I have translated an assembler program into basic. The assembler program uses a lot of techniques to reduce memory usage this being one of them. I am working through the program flow now from start to end. I can't remember if I ever used goto and gosub with the same label when I programmed in basic or with assembler. I thought it would follow the same methodology as assembler. I can work with these rules.
I hope you don't mind these questions as I go through this exercise.
You can do this to save code/memory space in gtBASIC as well, but I don't recommend it, it becomes confusing very fast and can lead to brain twisting bugs:

Code: Select all

_runtimePath_ "../runtime"
_runtimeStart_ &h7FFF
_codeRomType_ ROMvX0


print "TITLE0"
gosub EXAMPLE
gosub MODDLST
print "TITLE4" 
end

EXAMPLE:
    print "TITLE1" 
    goto MODDLST1

print "TITLE2" 
end

MODDLST:
    asm
        LSLW	; dummy instruction to keep compiler happy, (do NOT use PUSH)
    endasm
MODDLST1:
    print "TITLE3" 
return
Output:

Code: Select all

TITLE0
TITLE1
TITLE3
TITLE3
TITLE4
P.S. I edited the code, don't use PUSH as the compiler already adds PUSH to the MODDLST label.
wbushby
Posts: 48
Joined: 16 Jul 2021, 10:59

Re: gtBASIC

Post by wbushby »

You are absolutely correct about the brain twisting. I spent quite a bit of time trying to work out why the code was not working as intended with this subroutine call. After simple translation from assembler to basic I now have a 8400 line basic program I am running through step by step. Hopefully when it is finished it will run at an acceptable level. I have 2710 bytes of memory spare on 64k so will have to watch code bloat.
Post Reply