Boolean Data Type in freebasic

For other topics related to the FreeBASIC project or its community.
dkl
Site Admin
Posts: 3209
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Boolean Data Type in freebasic

Postby dkl » Jul 26, 2015 16:47

The compiler has to be rebuilt with itself (so it can use the new boolean-capable rtlib); then the tests should work.
St_W
Posts: 1468
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Boolean Data Type in freebasic

Postby St_W » Jul 26, 2015 18:44

dkl wrote:The compiler has to be rebuilt with itself (so it can use the new boolean-capable rtlib); then the tests should work.

Could you give some more details why this is needed?
In the current state fbc is compiled with an old fbc version (and thus also linked to the rtlib of that old version). However it should be capable of emitting programs that use new features and the new rtlib (that is linked to compiled programs). If the compiler itself would use new features it would not compile with the older fbc version. What am I missing here that causes the error? Doesn't a new fbc emit the same code if compiled with different fbc versions (rtlib would be the same, as compiled with gcc)?
dkl
Site Admin
Posts: 3209
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Boolean Data Type in freebasic

Postby dkl » Jul 26, 2015 19:47

Allright, I made some changes in Git that should make it unnecessary.

It's not technically needed afterall; it was just implemented that way. It did something like this for compile-time evaluation of str() on boolean constants:

Code: Select all

#ifndef cbool
#define cbool(x) iif((x), -1, 0)
#endif

... = str(cbool(value))
'' if built with non-boolean-capable fbc => str(integer) => 0/-1 => tests fail
'' if built with boolean-capable fbc => str(boolean) => false/true => tests succeed
coderJeff
Site Admin
Posts: 2949
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Boolean Data Type in freebasic

Postby coderJeff » Jul 26, 2015 21:18

Maybe add can it to the TODO list so it is not forgotten for a 1.05 release. In my opinion just putting off the inevitable. Yes, danger in using compiler features that are newly added but in this case it is all isolated to boolean.

Some explanation and more details:

Compiler source code was using a substitute for CBOOL() to bootstrap:
#define cbool(x_) iif((x_),-1,0))
Not fast, does not handle strings arguments, but it has just enough functionality for fbc to compile itself, and to pass most tests. The tests that are failing all have to do with boolean=>[w]str conversion, which only exists in the NEW rtlib. So to get the full functionality of CBOOL(), fbc needs to be compiled again so it can be linked against the new rtlib.

The existing fbc/rtlib are used to build the next version of fbc. fbc needs and uses the rtlib itself. That's important to know because, fbc will, from time to time, need something for a new compiler feature that does not yet exist in the rtlib. Not every version of fbc can compile every other version of fbc. And, this chicken-egg problem has happened before in fbc's history. And it has been solved this way before with a 2-iteration build.

OK, so CBOOL() dependency is removed which allows a 1-step working build.

By doing this, it breaks the pattern of how other conversions are handled by the compiler like CDBL(), CSNG(), etc. So now, compile-time CBOOL() is actually an IIF(), which was meant to be temporary only to bootstrap. And what we have now is compiler using IIF() for boolean conversion, and only user code gets the full/real/fast implementation of CBOOL(). Plus "TRUE"/"FALSE" constants are now multiple places in the code, multiple places in the rtlib. I was trying to keep them in one place.
MrSwiss
Posts: 3184
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 27, 2015 11:52

Hi all,

I'm still not quite "getting" it: in plain text please ...
- Does the 1.04.0 Version (most recent St_W build) now have the booleans y/n?
- What exactly is it called in -lang "fb", is it CBOOL? Anything else?
Thanks for "enlighting" me.

Below AND-Gate.bas which runs on current FBC 1.03.0, but could possibly gain from booleans:

Code: Select all

''
'' AND-Gate logic: In(x) = input, res = output
''
Sub AND_Gate (In() As Long, ByRef res As Long)
   ' implements the AND-Gate logic (0=false, 1=true)
   If In(1) = 1 AndAlso In(2) = 1 Then res = 1
   If In(1) = 0 AndAlso In(2) = 1 Then res = 0
   If In(1) = 1 AndAlso In(2) = 0 Then res = 0
   If In(1) = 0 AndAlso In(2) = 0 Then res = 0
End Sub

' ===== MAIN ===== '
Dim As Long In(1 To 2), res

For i As Long = 0 To 3
   Select Case As Const i
      Case 0
         In(1) = 1 : In(2) = 1   ' set the 2 inputs
         AND_Gate(In(), res)      ' get the result
      Case 1
         In(1) = 0 : In(2) = 1 : AND_Gate(In(), res)
      Case 2
         In(1) = 1 : In(2) = 0 : AND_Gate(In(), res)
      Case 3
         In(1) = 0 : In(2) = 0 : AND_Gate(In(), res)
   End Select
   Print "AND Gate:", "In(1)=" & iif(In(1), "TRUE", "FALSE"), _
      "In(2)=" & iif(In(2), "TRUE", "FALSE"), _
      "Outp=" & iif(res, "TRUE", "FALSE")
Next

Sleep : End 0
   
dkl
Site Admin
Posts: 3209
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Boolean Data Type in freebasic

Postby dkl » Jul 27, 2015 12:40

Yes, boolean support is now in FB 1.04.0. The new data type is called "boolean", see also:
viewtopic.php?f=17&t=23786
MrSwiss
Posts: 3184
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 27, 2015 12:51

Thanks dkl, I'll try it straight away ...
MrSwiss
Posts: 3184
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 27, 2015 13:36

SUCCESS ...

First results with Boolean Type ... it works as expected (WIN32 and WIN64), however:
using UCase(Boolean) throws a "type mismatch compiler error" ATM. Well, a Boolean isn't a String ...

AND-Gate.bas (using Boolean):

Code: Select all

''
'' AND-Gate logic: In(x) = input, res = output
'' FBC Version 1.04.0 >= 2015-07-25 only!!!
''
Sub AND_Gate (In() As Boolean, ByRef res As Boolean)
   ' implements the AND-Gate logic (false, true)
   If In(1) AndAlso In(2) Then res = true
   If Not In(1) AndAlso In(2) Then res = false
   If In(1) AndAlso Not In(2) Then res = false
   If Not In(1) AndAlso Not In(2) Then res = false
End Sub

' ===== MAIN ===== '
Dim As Boolean In(1 To 2), res

For i As Long = 0 To 3
   Select Case As Const i
      Case 0
         In(1) = true : In(2) = true   ' set the 2 inputs
         AND_Gate(In(), res)         ' get the result
      Case 1
         In(1) = false : In(2) = true : AND_Gate(In(), res)
      Case 2
         In(1) = true : In(2) = false : AND_Gate(In(), res)
      Case 3
         In(1) = false : In(2) = false : AND_Gate(In(), res)
   End Select
   Print "AND-Gate:", "In(1)=" & In(1), "In(2)=" & In(2), "Outp=" & res
Next

Sleep : End 0   
caseih
Posts: 1360
Joined: Feb 26, 2007 5:32

Re: Boolean Data Type in freebasic

Postby caseih » Jul 27, 2015 15:59

Just curious but why do you have all the redundant logic in your AND_Gate sub? If you want to test Boolean why not do:

Code: Select all

Sub AND_Gate (In() As Boolean, ByRef res As Boolean)
    res = In(1) And In(2)
End Sub


or

Code: Select all

if In(1) AndAlso In(2) then res = true else res = false
caseih
Posts: 1360
Joined: Feb 26, 2007 5:32

Re: Boolean Data Type in freebasic

Postby caseih » Jul 27, 2015 16:00

Now that boolean support is in FBC, does FBC know how to mangle G++ symbols that use C++ bool types in their signature?
St_W
Posts: 1468
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Boolean Data Type in freebasic

Postby St_W » Jul 27, 2015 16:24

MrSwiss wrote:I'm still not quite "getting" it: in plain text please ...
- Does the 1.04.0 Version (most recent St_W build) now have the booleans y/n?
- What exactly is it called in -lang "fb", is it CBOOL? Anything else?
Thanks for "enlighting" me.

My current builds do contain the boolean feature, BUT there are likely some errors because the builds were done with an older fbc version and before dkl's CBool change (see discussion a few posts above).
Unfortunately no new builds have been uploaded tonight because I've accidentally turned off the build machine remotely (instead of another remote machine) and have not set up a possibility to turn it on remotely :-(
I hope I can get it running again this evening so that new builds will be available tomorrow.
Then I'll also update the fbc used to do the builds with a current build.

@dkl, coderJeff:
Thank You for your explanations why rebuilding fbc with itself was necessary with this change. I did not expect that boolean features were already used by fbc itself.


//edit: The build server is up and running again and I've taken some precautions to prevent such downtimes in the future for even more reliable build-updates. The new builds will be uploaded tonight. New builds will be built with the current fbc build (that was still built with an older fbc). But enough confusion - have fun trying out the new builds :-)
Last edited by St_W on Jul 27, 2015 21:57, edited 1 time in total.
dkl
Site Admin
Posts: 3209
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Boolean Data Type in freebasic

Postby dkl » Jul 27, 2015 17:08

caseih wrote:Now that boolean support is in FBC, does FBC know how to mangle G++ symbols that use C++ bool types in their signature?

Yep, it does.
MrSwiss
Posts: 3184
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Boolean Data Type in freebasic

Postby MrSwiss » Jul 27, 2015 18:20

@caseih
caseih wrote:

Code: Select all

    Sub AND_Gate (In() As Boolean, ByRef res As Boolean)
        res = In(1) And In(2)
    End Sub


You're right of course, but as a first run, I wanted to stress-test as much as possible ...
optimal code wasn't on my mind, at all.
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Jul 27, 2015 19:19

IMHO, the result of the last line is not logical:

Code: Select all

Dim As Boolean b0 = false
Dim As Boolean b1 = true

Print b0, b1
Print Str(b0), Str(b1)
Print false, true
Print Str(false), Str(true)

Code: Select all

false         true
false         true
false         true
0             -1

[edit]
This problem is fixed now with version 331 from http://users.freebasic-portal.de/stw/builds/
Last edited by fxm on Jul 28, 2015 6:05, edited 1 time in total.
fxm
Posts: 9066
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Boolean Data Type in freebasic

Postby fxm » Jul 27, 2015 19:33

This weird behavior is due to 'Const As Bolean' type (as are defined False and True):

Code: Select all

Const As Boolean b0 = false
Const As Boolean b1 = true

Print b0, b1
Print Str(b0), Str(b1)
Print false, true
Print Str(false), Str(true)

Code: Select all

false         true
0             -1
false         true
0             -1

[edit]
This problem is fixed now with version 331 from http://users.freebasic-portal.de/stw/builds/
Last edited by fxm on Jul 28, 2015 6:06, edited 1 time in total.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 10 guests