EnumerationBinary

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

EnumerationBinary

Post by StarBootics »

Hello everyone,

I think the compiler should raise a warning if the EnumerationBinary has more than 63 constants. The values will exceed the quad capacity pass the 63rd constants anyway. Maybe this can be raise if we get 128 bit integers in the future but for now 63 constants seems fair enough.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: EnumerationBinary

Post by Michael Vogel »

A lot of additional warnings would be helpful, what I am doing some and when is a combination StartVectorDrawing(...) and StopDrawing() which works with no error but also with no output :shock:...

Maybe something like that two variants could help in critical situations?

Code: Select all

#EnumerationMaximum=10

Macro C1(zzz)
	zzz
	CompilerIf #PB_Compiler_EnumerationValue>#EnumerationMaximum : EndEnumeration : Debug "PANIC" : Enumeration : CompilerEndIf
EndMacro
Macro C2()
	: CompilerIf #PB_Compiler_EnumerationValue>#EnumerationMaximum : EndEnumeration : Debug "PANIC"
	Enumeration 
	CompilerEndIf
EndMacro

EnumerationBinary
	C1(#A)
	C1(#B)
	C1(#C)
	#D	C2()
	#E_	C2()
	#F	C2()
EndEnumeration

Debug #a
Debug #b
Debug #c
Debug #d
Debug #e_
Debug #f
breeze4me
Enthusiast
Enthusiast
Posts: 511
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: EnumerationBinary

Post by breeze4me »

The following code is a kind of hack using #PB_Compiler_Line.

Caution !
1. Nothing should be added between "BinEnum" and "EndBinEnum" other than the constants. No empty lines !
2. The ":" character for the command concatenation should not be used between "BinEnum" and "EndBinEnum".
3. The "=" character to alter a value should not be used.

Code: Select all

Macro BinEnum
  CompilerIf #PB_Compiler_Version = 572 ;And #PB_Compiler_OS = #PB_OS_Windows
    #___BinEnumLineLimit = 65
  CompilerEndIf
  #___BinEnumLineStart_#MacroExpandedCount = #PB_Compiler_Line
  EnumerationBinary
EndMacro

Macro EndBinEnum
  EndEnumeration
  #___BinEnumLineEnd_#MacroExpandedCount = #PB_Compiler_Line
  CompilerIf #___BinEnumLineEnd_#MacroExpandedCount - #___BinEnumLineStart_#MacroExpandedCount > #___BinEnumLineLimit
    CompilerError "Exceed EnumerationBinary limitation !"
  CompilerEndIf
EndMacro

BinEnum
#a1
#a2
#a3
#a4
#a5
#a6
#a7
#a8
#a9
#a10
#a11
#a12
#a13
#a14
#a15
#a16
#a17
#a18
#a19
#a20
#a21
#a22
#a23
#a24
#a25
#a26
#a27
#a28
#a29
#a30
#a31
#a32
#a33
#a34
#a35
#a36
#a37
#a38
#a39
#a40
#a41
#a42
#a43
#a44
#a45
#a46
#a47
#a48
#a49
#a50
#a51
#a52
#a53
#a54
#a55
#a56
#a57
#a58
#a59
#a60
#a61
#a62
#a63
#a64
EndBinEnum

BinEnum
#b1
#b2
#b3
#b4
#b5
#b6
#b7
#b8
#b9
#b10
#b11
#b12
#b13
#b14
#b15
#b16
#b17
#b18
#b19
#b20
#b21
#b22
#b23
#b24
#b25
#b26
#b27
#b28
#b29
#b30
#b31
#b32
#b33
#b34
#b35
#b36
#b37
#b38
#b39
#b40
#b41
#b42
#b43
#b44
#b45
#b46
#b47
#b48
#b49
#b50
#b51
#b52
#b53
#b54
#b55
#b56
#b57
#b58
#b59
#b60
#b61
#b62
#b63
#b64
EndBinEnum

Debug CountString(Bin(#a64), "0")
Debug CountString(Bin(#b64), "0")
[/size]
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: EnumerationBinary

Post by StarBootics »

Hello everyone,

Personally I prefer to have an enumeration already filled with 63 constants and comment the unused ones. Like that :

Code: Select all

EnumerationBinary
  
  #CONSTANT_NAME_00
  #CONSTANT_NAME_01
  #CONSTANT_NAME_02
  #CONSTANT_NAME_03
  #CONSTANT_NAME_04
  #CONSTANT_NAME_05
  #CONSTANT_NAME_06
  #CONSTANT_NAME_07
  #CONSTANT_NAME_08
  #CONSTANT_NAME_09
  ;   #CONSTANT_NAME_10
  ;   #CONSTANT_NAME_11
  ;   #CONSTANT_NAME_12
  ;   #CONSTANT_NAME_13
  ;   #CONSTANT_NAME_14
  ;   #CONSTANT_NAME_15
  ;   #CONSTANT_NAME_16
  ;   #CONSTANT_NAME_17
  ;   #CONSTANT_NAME_18
  ;   #CONSTANT_NAME_19
  ;   #CONSTANT_NAME_20
  ;   #CONSTANT_NAME_21
  ;   #CONSTANT_NAME_22
  ;   #CONSTANT_NAME_23
  ;   #CONSTANT_NAME_24
  ;   #CONSTANT_NAME_25
  ;   #CONSTANT_NAME_26
  ;   #CONSTANT_NAME_27
  ;   #CONSTANT_NAME_28
  ;   #CONSTANT_NAME_29
  ;   #CONSTANT_NAME_30
  ;   #CONSTANT_NAME_31
  ;   #CONSTANT_NAME_32
  ;   #CONSTANT_NAME_33
  ;   #CONSTANT_NAME_34
  ;   #CONSTANT_NAME_35
  ;   #CONSTANT_NAME_36
  ;   #CONSTANT_NAME_37
  ;   #CONSTANT_NAME_38
  ;   #CONSTANT_NAME_39
  ;   #CONSTANT_NAME_40
  ;   #CONSTANT_NAME_41
  ;   #CONSTANT_NAME_42
  ;   #CONSTANT_NAME_43
  ;   #CONSTANT_NAME_44
  ;   #CONSTANT_NAME_45
  ;   #CONSTANT_NAME_46
  ;   #CONSTANT_NAME_47
  ;   #CONSTANT_NAME_48
  ;   #CONSTANT_NAME_49
  ;   #CONSTANT_NAME_50
  ;   #CONSTANT_NAME_51
  ;   #CONSTANT_NAME_52
  ;   #CONSTANT_NAME_53
  ;   #CONSTANT_NAME_54
  ;   #CONSTANT_NAME_55
  ;   #CONSTANT_NAME_56
  ;   #CONSTANT_NAME_57
  ;   #CONSTANT_NAME_58
  ;   #CONSTANT_NAME_59
  ;   #CONSTANT_NAME_60
  ;   #CONSTANT_NAME_61
  ;   #CONSTANT_NAME_62
  
EndEnumeration
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: EnumerationBinary

Post by Michael Vogel »

Cool idea! Not sure, but couldn't it made without compiler lines also?

Code: Select all

Macro BinEnum;(zzz=1)
	#___BinEnumLineStart_#MacroExpandedCount = 1;zzz
	EnumerationBinary
	EndMacro

	Macro EndBinEnum
		#___BinEnumLineEnd_#MacroExpandedCount = #PB_Compiler_EnumerationValue
	EndEnumeration
	Debug "$"+Hex(#___BinEnumLineEnd_#MacroExpandedCount)
	CompilerIf Bool(#___BinEnumLineEnd_#MacroExpandedCount>=0 And #___BinEnumLineEnd_#MacroExpandedCount<#___BinEnumLineStart_#MacroExpandedCount)
		CompilerError "Exceed EnumerationBinary limitation !"
	CompilerEndIf
EndMacro
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: EnumerationBinary

Post by Josh »

It always sends shivers down my spine when I see such constructs of macros. The code is hardly comprehensible and I don't even want to think about program maintenance after a few years. Why not use simple solutions like StarBootics or the following:

Code: Select all

EnumerationBinary
  #CONSTANT_00
  #CONSTANT_01
  #CONSTANT_02
  #CONSTANT_03
  #CONSTANT_04
  #CONSTANT_05
  #CONSTANT_06
  #CONSTANT_07
  #CONSTANT_08
  #CONSTANT_09
  #CONSTANT_10
  #CONSTANT_11
  #CONSTANT_12
  #CONSTANT_13
  #CONSTANT_14
  #CONSTANT_15
  #CONSTANT_16
  #CONSTANT_17
  #CONSTANT_18
  #CONSTANT_19
  #CONSTANT_20
  #CONSTANT_21
  #CONSTANT_22
  #CONSTANT_23
  #CONSTANT_24
  #CONSTANT_25
  #CONSTANT_26
  #CONSTANT_27
  #CONSTANT_28
  #CONSTANT_29
  #CONSTANT_30
  #CONSTANT_31
  #CONSTANT_32
  #CONSTANT_33
  #CONSTANT_34
  #CONSTANT_35
  #CONSTANT_36
  #CONSTANT_37
  #CONSTANT_38
  #CONSTANT_39
  #CONSTANT_40
  #CONSTANT_41
  #CONSTANT_42
  #CONSTANT_43
  #CONSTANT_44
  #CONSTANT_45
  #CONSTANT_46
  #CONSTANT_47
  #CONSTANT_48
  #CONSTANT_49
  #CONSTANT_50
  #CONSTANT_51
  #CONSTANT_52
  #CONSTANT_53
  #CONSTANT_54
  #CONSTANT_55
  #CONSTANT_56
  #CONSTANT_57
  #CONSTANT_58
  #CONSTANT_59
  #CONSTANT_60
  #CONSTANT_61
  #CONSTANT_62
  #__CONSTANT_LAST__
EndEnumeration

If #__CONSTANT_LAST__ <= 0
  CallDebugger
EndIf
sorry for my bad english
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: EnumerationBinary

Post by kenmo »

I like Josh's idea, in fact I'll go one step simpler :lol:

Code: Select all

EnumerationBinary
  #Flag1
  #Flag2
  #Flag3
  #Flag4
  ;#Flag5 ; uncomment this to trigger error
  
  CompilerIf (#PB_Compiler_EnumerationValue > (1 << 4)) ; only allow 4 binary constants
    CompilerWarning "Too many binary constants!"
  CompilerEndIf
EndEnumeration
EDIT - Ah, I see the issue with negative overflow when going too far with this method...


+1 I like the feature request. Compiler Warning (not error) if >64 EnumerationBinary value.

I was going to say, maybe a warning of >32 when compiling 32-bit mode, but no, I think keep it consistent and 64-bits. Besides, you can use 64-bit quads in 32-bit compile mode.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: EnumerationBinary

Post by StarBootics »

Josh wrote:Why not use simple solutions like StarBootics...
Simple solution are always the best. That being said, I still recommend to have a compiler warning this shouldn't difficult to add since the compiler generate the constants. Fred is the boss so I will let him decide about this.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: EnumerationBinary

Post by BarryG »

StarBootics wrote:the compiler should raise a warning if the EnumerationBinary has more than 63 constants
+1
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: EnumerationBinary

Post by Michael Vogel »

And what about another compiler tweak? Of course too late, would cause incompatibility issues :wink:
Do you see the different results of the following two snippets without giving it a try?

C O D E 1

Code: Select all

EnumerationBinary 
	#MessageLowPriority=	#Null
	#MessageHighPriority
	#MessageTopPriority
	#MessageDrawWindow
	#MessagePriorityFilter=	#MessageDrawWindow-1
EndEnumeration
C O D E 2

Code: Select all

EnumerationBinary 
	#MessageLowPriority=	#Null
	#MessageHighPriority=	1
	#MessageTopPriority
	#MessageDrawWindow
	#MessagePriorityFilter=	#MessageDrawWindow-1
EndEnumeration
Yes, it's not a typical one - especially the last line (which I kept to make it more difficult)...
...but I'd have liked code number one acting exactly as number two and three :mrgreen:

C O D E 3

Code: Select all

#MessageLowPriority=	#Null
EnumerationBinary 
	#MessageHighPriority
	#MessageTopPriority
	#MessageDrawWindow
	#MessagePriorityFilter=	#MessageDrawWindow-1
EndEnumeration
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: EnumerationBinary

Post by Josh »

All three codes give exactly the result I would have expected. 0*2 is 0 and remains 0, so what other result should come out? Each other result would be wrong.

I think you should use the PB stuff the way it is meant to be used and not try to find any bugs by any useless constructs. EnumerationBinary works perfect for what it is made for. The only thing that could made better by EnumerationBinary is that assignments that are not a power of 2 should not be allowed. Then such questions could not come up at all.
sorry for my bad english
Post Reply