Boolean Data Type in freebasic
Re: Boolean Data Type in freebasic
The compiler has to be rebuilt with itself (so it can use the new boolean-capable rtlib); then the tests should work.
Re: Boolean Data Type in freebasic
Could you give some more details why this is needed?dkl wrote:The compiler has to be rebuilt with itself (so it can use the new boolean-capable rtlib); then the tests should work.
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)?
Re: Boolean Data Type in freebasic
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:
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
Re: Boolean Data Type in freebasic
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.
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.
Re: Boolean Data Type in freebasic
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:
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
Re: Boolean Data Type in freebasic
Yes, boolean support is now in FB 1.04.0. The new data type is called "boolean", see also:
http://www.freebasic.net/forum/viewtopi ... 17&t=23786
http://www.freebasic.net/forum/viewtopi ... 17&t=23786
Re: Boolean Data Type in freebasic
Thanks dkl, I'll try it straight away ...
Re: Boolean Data Type in freebasic
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):
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
Re: Boolean Data Type in freebasic
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:
or
Code: Select all
Sub AND_Gate (In() As Boolean, ByRef res As Boolean)
res = In(1) And In(2)
End Sub
Code: Select all
if In(1) AndAlso In(2) then res = true else res = false
Re: Boolean Data Type in freebasic
Now that boolean support is in FBC, does FBC know how to mangle G++ symbols that use C++ bool types in their signature?
Re: Boolean Data Type in freebasic
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).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.
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.
Re: Boolean Data Type in freebasic
Yep, it does.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?
Re: Boolean Data Type in freebasic
@caseih
optimal code wasn't on my mind, at all.
You're right of course, but as a first run, I wanted to stress-test as much as possible ...caseih wrote:Code: Select all
Sub AND_Gate (In() As Boolean, ByRef res As Boolean) res = In(1) And In(2) End Sub
optimal code wasn't on my mind, at all.
Re: Boolean Data Type in freebasic
IMHO, the result of the last line is not logical:
[edit]
This problem is fixed now with version 331 from http://users.freebasic-portal.de/stw/builds/
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
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.
Re: Boolean Data Type in freebasic
This weird behavior is due to 'Const As Bolean' type (as are defined False and True):
[edit]
This problem is fixed now with version 331 from http://users.freebasic-portal.de/stw/builds/
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
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.