Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:52 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic 4.5b bug with loops

Author Message
rave
Newbie

Joined: 24/02/2018
Location: United States
Posts: 28
Posted: 01:34am 23 Mar 2018
Copy link to clipboard 
Print this post

The following program fails with "Error: LOOP without a matching DO" , even though it is syntactically perfectly correct:


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: Australia
Posts: 114
Posted: 03:14am 23 Mar 2018
Copy link to clipboard 
Print this post

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
Edited by flip 2018-03-24
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 03:21am 23 Mar 2018
Copy link to clipboard 
Print this post

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.Edited by Azure 2018-03-24
 
rave
Newbie

Joined: 24/02/2018
Location: United States
Posts: 28
Posted: 01:51pm 23 Mar 2018
Copy link to clipboard 
Print this post


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: Australia
Posts: 446
Posted: 02:22pm 23 Mar 2018
Copy link to clipboard 
Print this post

  Azure said  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.


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: Australia
Posts: 446
Posted: 02:34pm 23 Mar 2018
Copy link to clipboard 
Print this post

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 States
Posts: 28
Posted: 02:49pm 23 Mar 2018
Copy link to clipboard 
Print this post

  Azure said  
  Azure said  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.



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 States
Posts: 28
Posted: 12:20am 24 Mar 2018
Copy link to clipboard 
Print this post


Hi all,

I am 100% sure this is a MMBasic 4.5b bug as my test case shows and further analysis reveals.

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 feel it runs on shaky grounds ready to spit "LOOP without DO" errors again at any moment . If that happens I'm ready to this project.

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: Australia
Posts: 6283
Posted: 02:12am 24 Mar 2018
Copy link to clipboard 
Print this post

Not a true solution but this might be easier to implement:
  Quote  
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:
  Quote   ' Do WHILE test
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
PRINT
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: Germany
Posts: 1593
Posted: 10:36am 24 Mar 2018
Copy link to clipboard 
Print this post

EDIT
AFAIK are there known issues to leave a FOR...NEXT loop with EXIT too.

The list of reported bugs for MMBasic (Maximite).Edited by twofingers 2018-03-25
causality ≠ correlation ≠ coincidence
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025