![]() |
Forum Index : Microcontroller and PC projects : MMBasic 4.5b bug with loops
Author | Message | ||||
rave Newbie ![]() Joined: 24/02/2018 Location: United StatesPosts: 28 |
The following program fails with "Error: LOOP without a matching DO" ![]() Test "+", 1 Sub Test(a$, k) Do While IsMatch(a$, k, "+") a$ = "" Loop ' this line is where the error is reported End Sub Function IsMatch(a$, k, bS) Do While Mid$(a$, k, 1) = " ": k = k + 1: Loop IsMatch = Mid$(a$, k) = b$ End Function Changing the first statement to the following: Test "", 1 gives the error "Error: No function call to return to" at the end of the function IsMatch. ![]() Changing this program to the following semantically equivalent version without 'While' works fine: Test "+", 1 Sub Test(a$, k) Do If Not IsMatch(a$, k, "+") Then Exit ' use exit instead of While a$ = "" Loop End Sub Function IsMatch(a$, k, bS) Do While Mid$(a$, k, 1) = " ": k = k + 1: Loop IsMatch = Mid$(a$, k) = b$ End Function This is a simplified program from a much larger program. It took me a bit of time to create this minimized version that shows this bug. Hopefully this is useful for future MMBasic 4.x updates or to list this as an issue with MMBasic 4.5. ![]() - Rob |
||||
flip Senior Member ![]() Joined: 18/07/2016 Location: AustraliaPosts: 114 |
Good day Rob and welcome. Is it a typo?...should be b$ maybe?? Function IsMatch(a$, k, bS) I do stuff like this all the time... ![]() Sometimes Jim's MMEDIT Program Report Variable Usage can pinpoint the problem. Regards, Phil |
||||
Azure![]() Guru ![]() Joined: 09/11/2017 Location: AustraliaPosts: 446 |
These will not solve your problem but are problems in the code. In both examples you define the function variable as bS and reference it as b$. So you are not testing the value passed. For testing/debugging purposes I would add a print statement to the Test Sub and IsMatch function to print the values passed. That way you can see if the error is on the first pass or after some number of iterations. @flip: your post beat me. I've still left my post. |
||||
rave Newbie ![]() Joined: 24/02/2018 Location: United StatesPosts: 28 |
Here is the correct IsMarch(), I made a typo when copying this over manually from my CMM: Function IsMatch(a$, k, b$) Do While Mid$(a$, k, 1) = " ": k = k + 1: Loop IsMatch = Mid$(a$, k) = b$ End Function Still, the problem is not with this function. The interpreter chokes on the DO-WHILE-LOOP or does not recognize the end of the function. Probably a stack issue deeper in the interpreter ![]() - Rob |
||||
Azure![]() Guru ![]() Joined: 09/11/2017 Location: AustraliaPosts: 446 |
That is why I suggested adding the print while debugging it will show if it is a stack issue or possibly some other clue. You could also add a nesting counter to print for each recursion level. Happy offer a suggestion added to your code if you want. |
||||
Azure![]() Guru ![]() Joined: 09/11/2017 Location: AustraliaPosts: 446 |
You can add the prints something like this (sorry might be typing mistakes it's late). Test "+", 1 Sub Test(a$, k) Do While IsMatch(a$, k, "+") a$ = "" Loop ' this line is where the error is reported End Sub Function IsMatch(a$, k, b$) Static IMNest = 0 IMNest = IMNest + 1 print "Entering IsMatch"; IMNest; " Times a$: "; a$; "k: "; k; "b$: "; b$ Do While Mid$(a$, k, 1) = " ": k = k + 1: Loop IsMatch = Mid$(a$, k) = b$ print "Exiting IsMatch"; IMNest; " Times a$: "; a$; "k: "; k; "b$: "; b$ IMNest = IMNest - 1 End Function |
||||
rave Newbie ![]() Joined: 24/02/2018 Location: United StatesPosts: 28 |
I've made two test cases as you can see in my code: we pass "+" or "". Both fail, but with different MMBasic 4.5b interpreter errors. The first fails after one iteration of the loop, affecting the closing LOOP which is no longer recognized by the interpreter (LOOP without a matching DO). The second fails immediately (no loop iterations) and fails in the IsMatch function where MMBasic 4.5b reports "No function call to return to". I made the code deliberately simple, just one iteration or none in the Test sub. My original program has hundreds of lines when I ran into this problem. I found a work-around by replacing WHILE X with IF NOT X THEN EXIT. Unfortunately, my program still produces the same error in some cases, so I'll have to tweak it further to work around this limitation. Again, I am pretty sure this is a bug of the MMBasic 4.5b interpreter. Perhaps its internal state/stack gets smashed. Thanks for your help! - Rob |
||||
rave Newbie ![]() Joined: 24/02/2018 Location: United StatesPosts: 28 |
Hi all, I am 100% sure this is a MMBasic 4.5b bug ![]() I managed to make progress with my larger program by working around this problem as follows: I removed several WHILE and EXIT DO statements and replaced them with FOR-NEXT and EXIT SUB when applicable (the latter when the loop is the last statement in a SUB). This fixes the MMBasic errors. The program runs now but the result is some darn ugly code ![]() ![]() ![]() ![]() I love to recreate some of my Apple ][ programs and some other SW+HW stuff I've done since the 80s on 8-bit computers, but I don't want to go back to the days we had to use GOTO and GOSUB ![]() Have a great weekend! ![]() ![]() - Rob |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Not a true solution but this might be easier to implement: z = IsMatch(a$, k, "+") ' get the first test condition DO WHILE z z = IsMatch(a$, k, "+") ' get the next test condition ... LOOP Full test program: PRINT MM.VER Test "+", 1 Test "", 1 Test "fred", 1 Test " +12", 1 SUB Test(a$, k) z = IsMatch(a$, k, "+") 'get the first test condition PRINT "Initial ",z,x ' debug x = x + 1 ' debug DO WHILE z PRINT "Internal",z,x ' debug x = x + 1 ' debug z = IsMatch(a$, k, "+") ' get the next test condition a$ = "" LOOP ' this line is where the error is reported END SUB FUNCTION IsMatch(a$, k, b$) DO WHILE MID$(a$, k, 1) = " ": k = k + 1: LOOP IsMatch = MID$(a$, k) = b$ END FUNCTION > Maximite BASIC Version 4.5C Copyright 2011-2018 Geoff Graham > RUN 4.0503 Initial 1 0 Internal 1 1 Internal 1 2 Initial 0 3 Initial 0 4 Initial 0 5 > I agree that resorting to GOTOs would be a backward step. Jim VK7JH MMedit |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1593 |
EDIT AFAIK are there known issues to leave a FOR...NEXT loop with EXIT too. The list of reported bugs for MMBasic (Maximite). causality ≠ correlation ≠ coincidence |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |