Compiler behavior on Select Case

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Compiler behavior on Select Case

Post by Lunasole »

Hi. Recently I've misspelled on "Case" statement (typed ":" instead of ","), and that caused some silent bug in my program, because case options after first one were never triggered.

It's generally not a bug, rather some kind of (syntax) problem occuring in such case.
There are 3 examples showing that:

1

Code: Select all

; Example 1, compiler doesn't see any problem and #CONST_1 is matching in Select
EnableExplicit

#CONST_1 = 3
#CONST_2 = 4
Define A = #CONST_1

Select A
	Case #CONST_1: #CONST_2:
		Debug A
	Default
		Debug "NO MATCH"
EndSelect
2

Code: Select all

; Example 2, compiler doesn't see any problem, but #CONST_2 is never matching in Select
EnableExplicit

#CONST_1 = 3
#CONST_2 = 4
Define A = #CONST_2

Select A
	Case #CONST_1: #CONST_2:
		Debug A
	Default
		Debug "NO MATCH"
EndSelect
3

Code: Select all

; Finally example 3, here compiler tells "duplicate label" on second Select statement, which points to a source of problem
EnableExplicit

#CONST_1 = 3
#CONST_2 = 4
Define A = #CONST_2

;1
Select A
	Case #CONST_1: #CONST_2:
		Debug A
	Default
		Debug "NO MATCH"
EndSelect

;2
Select A
	Case #CONST_1: #CONST_2:
		Debug A
	Default
		Debug "NO MATCH"
EndSelect
Checked with 5.60
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Compiler behavior on Select Case

Post by TI-994A »

The error seems to be caused by a confusion between constants and labels. For some reason using the hex symbol (#) in a label raises no errors.

Code: Select all

Case #CONST_1: #CONST_2:
In the above expression, the first colon is interpreted as a line concatenator, while the second is seen as a label.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Compiler behavior on Select Case

Post by netmaestro »

I believe the allowing of constants to serve as labels is intended and actually quite useful for creating self-documenting code. I wouldn't like to see it change because someone made a typo in a case structure and caused a bug in their code, no offense intended.
BERESHEIT
Post Reply