Boolean Type for FB

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Boolean Type for FB

Post by Imortis »

Call this BoolLib.bas

Code: Select all

'-------------------------------------------------------------
'|BoolLib                                                    |
'|By Imortis Inglorian                                       |
'|v0.3                                                       |
'|                                                           |
'|07-19-07                                                   |
'|-First Draft Completed                                     |
'-------------------------------------------------------------

Enum Bool
	FALSE
	TRUE = Not(FALSE)
End Enum

Type Boolean 
	TorF As Bool
	Declare Operator Let (ByRef rhs As Bool)
	Declare Operator Let (ByRef rhs As Boolean)
	Declare Operator Cast () As String
End Type

Operator Boolean.Cast() As String
	If this.TorF = TRUE Then Return "True"
	If this.TorF = FALSE Then Return "False"
End Operator

Operator Boolean.Let(ByRef rhs As Bool)
	If rhs = TRUE Then 
		this.TorF = TRUE
	Else
		this.TorF = FALSE
	End if
End Operator

Operator Boolean.Let(ByRef rhs As Boolean)
	this.TorF = rhs.TorF
End Operator

Operator = (ByRef lhs As Boolean, ByRef rhs As Boolean) As Byte
	Dim Ret  As Byte
	If lhs.TorF = rhs.TorF Then 
		Ret = -1
	Else
		Ret = 0
	End if
	Return Ret
End Operator

Operator = (ByRef lhs As Boolean, ByRef rhs As Bool) As Byte
	Dim Ret  As Byte
	If lhs.TorF = rhs Then 
		Ret = -1
	Else
		Ret = 0
	End If
	Return Ret
End Operator

Operator = (ByRef lhs As Bool, Byref rhs As Boolean) As Byte
	Dim Ret  As Byte
	If lhs = rhs.TorF Then 
		Ret = -1
	Else
		Ret = 0
	End If
	Return Ret
End Operator

Operator Or (ByRef lhs As Boolean, ByRef rhs As Boolean) As Boolean
	Dim Ret As Boolean
	If (lhs.TorF = TRUE) Then 
		Ret.TorF = TRUE
	elseIf (rhs.TorF = TRUE) Then 
		Ret.TorF = TRUE
	Else
		Ret.TorF = FALSE
	End if
	Return Ret
End Operator

Operator Or (ByRef lhs As Boolean, ByRef rhs As Bool) As Boolean
	Dim Ret As Boolean
	If (lhs.TorF = TRUE) Then 
		Ret.TorF = TRUE
	elseIf (rhs = TRUE) Then 
		Ret.TorF = TRUE
	Else
		Ret.TorF = FALSE
	End if
	Return Ret
End Operator

Operator Or (ByRef lhs As Bool, ByRef rhs As Bool) As Boolean
	Dim Ret As Boolean
	If (lhs = TRUE) Then 
		Ret.TorF = TRUE
	elseIf (rhs = TRUE) Then 
		Ret.TorF = TRUE
	Else
		Ret.TorF = FALSE
	End if
	Return Ret
End Operator

Operator Or (ByRef lhs As Bool, ByRef rhs As Boolean) As Boolean
	Dim Ret As Boolean
	If (lhs = TRUE) Then 
		Ret.TorF = TRUE
	elseIf (rhs.TorF = TRUE) Then 
		Ret.TorF = TRUE
	Else
		Ret.TorF = FALSE
	End if
	Return Ret
End Operator

Operator And (ByRef lhs As Boolean, ByRef rhs As Boolean) As Boolean
	Dim Ret As Boolean
	If (lhs.TorF = TRUE) Then 
		If (rhs.TorF = TRUE) Then
			Ret.TorF = TRUE
		Else
			Ret.TorF = FALSE
		EndIf
	Else
		Ret.TorF = FALSE
	EndIf
	Return Ret
End Operator

Operator And (ByRef lhs As Boolean, ByRef rhs As Bool) As Boolean
	Dim Ret As Boolean
	If (lhs.TorF = TRUE) Then 
		If (rhs = TRUE) Then
			Ret.TorF = TRUE
		Else
			Ret.TorF = FALSE
		EndIf
	Else
		Ret.TorF = FALSE
	EndIf
	Return Ret
End Operator

Operator And (ByRef lhs As Bool, ByRef rhs As Boolean) As Boolean
	Dim Ret As Boolean
	If (lhs = TRUE) Then 
		If (rhs.TorF = TRUE) Then
			Ret.TorF = TRUE
		Else
			Ret.TorF = FALSE
		EndIf
	Else
		Ret.TorF = FALSE
	EndIf
	Return Ret
End Operator

Operator And (ByRef lhs As Bool, ByRef rhs As Bool) As Boolean
	Dim Ret As Boolean
	If (lhs = TRUE) Then 
		If (rhs = TRUE) Then
			Ret.TorF = TRUE
		Else
			Ret.TorF = FALSE
		EndIf
	Else
		Ret.TorF = FALSE
	EndIf
	Return Ret
End Operator

Operator Not(ByRef rhs As Boolean) As Boolean
	Dim Ret As Boolean
	If rhs.TorF = TRUE Then 
		Ret.TorF = FALSE
	Else
		Ret.TorF = TRUE
	EndIf
	Return Ret
End Operator

Operator Not(ByRef rhs As Bool) As Boolean
	Dim Ret As Boolean
	If rhs = TRUE Then 
		Ret.TorF = FALSE
	Else
		Ret.TorF = TRUE
	EndIf
	Return Ret
End Operator
And as a test:

Code: Select all

#Include "BoolLib.bas"

Dim Test1 As Boolean
Dim Test2 As Boolean

Print "Test1 = TRUE"
Test1 = TRUE

Print "Test2 = FALSE"
Test2 = FALSE

Print
Print "Test1 Or Test2  = ", Test1 Or Test2
Print "Test1 And Test2 = ", Test1 And Test2
Print
Print "Not(Test1 Or Test2)  = ", Not(Test1 Or Test2)
Print "Not(Test1 And Test2) = ", Not(Test1 And Test2)
Print
Print "Test1 And TRUE = ", Test1 And TRUE
Print "TRUE And Test1 = ", TRUE And Test1
Print
Print "Test2 Or FALSE = ", Test2 Or FALSE
Print "FALSE Or Test2 = ", FALSE Or Test2
Print
Print "FALSE Or FALSE = ", FALSE Or FALSE
Print "TRUE And TRUE  = ", TRUE And TRUE

Print 
Print "Test1 = Test2?",
If (Test1 = Test2) Then 
	Print "Yes!"
Else
	Print "No!"
End If

Print "Test1 = TRUE?  ",
If (Test1 = TRUE) Then
	Print "Yes!"
Else 
	Print "No!"
EndIf

Print "TRUE = TRUE?   ",
If (TRUE = TRUE) Then
	Print "Yes!"
Else
	Print "No!"
EndIf

sleep
If anything else needs to be added or if bugs are found, just let me know.
Last edited by Imortis on Jul 19, 2007 23:43, edited 1 time in total.
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Post by Deleter »

Just a note that I think its more proper or whatever to do this:

Code: Select all

enum bool
    false
    true=not(false)
end enum
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Yeah, TRUE in freebasic is -1, not 1.

When you do tests, test against FALSE, not TRUE. '3' would evaluate as TRUE, simply because it isn't FALSE ;)
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Post by Imortis »

I did this to match up against other Booleans, not the Bitwise operators.

Edit: I just realized that I forgot to do the = operator. I'll get it done.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Regardless, other booleans test against FALSE, just saying.

if( 2 )
printf( "hi!" );

will print hi in C.
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Post by Imortis »

Oh, yeah... Duh. Sorry.
My bad.
Fixing...

EDIT: Updated first post.
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Post by Imortis »

Sorry for the double post, but...

Will anyone really use this? I have seen many people asking for Booleans in FB so I thought, "Finally some thing I can do!"

In retrospect, it seems a little bit overkill...
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

No reason not to post it. Heheh. I think this is very useful :D However...

http://www.freebasic.net/forum/viewtopic.php?t=8707
Imortis
Moderator
Posts: 1966
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Post by Imortis »

I can't believe I missed that one... Oh well. Too late, as always.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

I always just use 0, !0 as false, true. I don't need any complex classes or functions when a simple comparison is enough.
Post Reply