|
Post by vovchik on Jun 30, 2014 21:13:44 GMT 1
Dear Peter, Thanks... With kind regards, vovchik
|
|
|
Post by bitvast on Jul 1, 2014 7:22:34 GMT 1
Thanks Peter. @ vovchik, I wasn't sure whether it was a bug either, but as Peter pointed out, for completeness it seemed that multiple string declarations/assignments should work if long/double assignments did, but they didn't.
|
|
|
Post by vovchik on Jul 1, 2014 8:00:52 GMT 1
Dear Alex, Different but looks good to me. Almost like a gtk popup...with some nice frills. With kind regards, vovchik
|
|
|
Post by bitvast on Jul 2, 2014 15:23:57 GMT 1
Peter, perhaps not a bug, but an unintended feature? The equivalence operator on strings works ok in this example: x$ = "1" y$ = "1"
IF x$ == y$ THEN PRINT "x$ = y$" END IF but not here: REM equivalence operator should REM assign 1 to z% in each case.
REM NUMBER x% = 1 y% = 1
z% = x% == y%
PRINT z%: 'ok
REM FLOATING x# = 1 y# = 1
z% = x# == y#
PRINT z%: 'ok
REM STRING x$ = "1" y$ = "1"
z% = x$ == y$
PRINT z%: 'fail baconshell.bac.c: In function 'main': baconshell.bac.c:84:43: warning: comparison between pointer and integer [enabled by default] z__b2c__long_var=(long)x__b2c__string_var == y__b2c__string_var;
|
|
|
Post by Pjot on Jul 2, 2014 19:42:15 GMT 1
Hi bitvast, Thanks, but I wonder if this should be considered a bug. In fact, it makes sense the error occurs, because you mix a numeric variable with a string. And of course, integer and float can be mixed as they both are numeric. z% = x% == y%
This means that z% gets the value assigned from x%, and then is compared with y%. When we do a print of z% the result is '1'. Similarly for floats. So for strings, we must convert them to numeric type first, using VAL: z% = VAL(x$) == VAL(y$)
This works and delivers the same result. BR Peter
|
|
|
Post by bitvast on Jul 2, 2014 21:06:54 GMT 1
In fact, it makes sense the error occurs, because you mix a numeric variable with a string. And of course, integer and float can be mixed as they both are numeric. z% = x% == y%
This means that z% gets the value assigned from x%, and then is compared with y%. When we do a print of z% the result is '1'. Similarly for floats. So for strings, we must convert them to numeric type first, using VAL: z% = VAL(x$) == VAL(y$)
This works and delivers the same result. BR Peter Hi Peter, I thought that relational operators (in C anyway) always return a 1 for TRUE and a 0 for FALSE. Loops and IF statements evaluate the condition which reduces to either 1 or 0, so this code would result in an infinite loop: WHILE 1 DO 'do stuff WEND But am I mixing a numeric value with a string? I assumed that the equivalence operation would be done first, then the result assigned to z%. i.e. x$ == y$ results in a value of 1, which is then assigned to z%, so there should be no problem mixing a numeric with a string. Interestingly, using brackets no longer generates an error, but the result is 0, not 1. x$ = "1" y$ = "1"
z% = (x$ == y$)
PRINT z% 0
|
|
|
Post by bitvast on Jul 2, 2014 21:30:30 GMT 1
In the manual, the equations table says:
=, == Equal to String, numeric So shouldn't the first PRINT result in 1 since == applies to both numeric and string? :
x$ = "1" y$ = "1"
PRINT (x$ == y$)
PRINT EQUAL(x$, y$) 0 1
|
|
|
Post by bitvast on Jul 2, 2014 21:59:41 GMT 1
This is ok
z = (1 != 2)
PRINT z 1
but not this
z = (1 <> 2)
PRINT z baconshell.bac.c: In function 'main': baconshell.bac.c:60:13: error: expected expression before '>' token z=(long)(1 <> 2); ^ make: *** [baconshell.bac.o] Error 1
|
|
|
Post by vovchik on Jul 2, 2014 22:29:09 GMT 1
And this is OK
z = (1 NE 2) PRINT z
|
|
|
Post by bitvast on Jul 3, 2014 8:58:46 GMT 1
Weird... Using a literal value == works, but if using variables the output is the opposite to what it should be: <> always generates an error.
x$ = "a" y$ = "a"
'-----------------------------
z = (x$ == y$): 'z should be 1 but is 0
PRINT z '-----------------------------
z = (x$ != y$): 'z should be 0 but is 1
PRINT z '-----------------------------
z = ("a" == "a"): 'z should be 1
PRINT z '-----------------------------
z = ("a" != "a"): 'z should be 0
PRINT z
0 1 1 0
|
|
|
Post by alexfish on Jul 3, 2014 10:45:12 GMT 1
Hi bitvast
not sure on this one, think it could be a Doc err , since can not remember any operator in c that = '<>'
or possible that in bacon it be a parsing err.
BR Alex
|
|
|
Post by Pjot on Jul 3, 2014 20:54:30 GMT 1
Hij bitvast,
Currently I am traveling and I do not carry my laptop. I will be back next week Saturday, I will look into it then...
BR Peter
|
|
|
Post by bitvast on Jul 3, 2014 22:04:11 GMT 1
Ok, thanks Peter.
|
|
|
Post by Pjot on Jul 9, 2014 19:21:35 GMT 1
x$ = "1" y$ = "1"
z% = (x$ == y$)
PRINT z This indeed does not work, but I can explain why. For strings, the '==' and '<>' comparisons are only evaluated to valid C string comparisons when they occur within a IF, REPEAT or a WHILE.
But they are not evaluated when they occur in an assignment (like in the above code). And because of that, the resulting C code simply compares the character pointer 'x$' with the character pointer 'y$'. But these pointers point to different memory areas and hence, the result is '0'.
This is the same when applied in plain C code. In fact, there are no such things as string variables in C. These are pointers to memory areas which contain the array of separate ASCII values of the string. If you want to compare two memory areas you have to use the 'strcmp' function, for example. (In fact, when the '==' or '<>' comparisons occur and BaCon detects that this is done for strings, it will convert the comparison to the 'strcmp' function.)
However, from BASIC point of view this looks somewhat confusing, of course. I am not sure what to do about this now, I'll see if there is an elegant solution.
Thanks for reporting, Peter
|
|
|
Post by bitvast on Jul 10, 2014 7:07:40 GMT 1
Hi Peter, Thanks for the explanation. Perhaps it's not worth the time and trouble to "fix" this, being a limitation of C. And you can always use the EQUAL function.
|
|