![]() |
Conditional operator? - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Wishlist (https://www.boriel.com/forum/forumdisplay.php?fid=14) +---- Thread: Conditional operator? (/showthread.php?tid=603) |
Conditional operator? - einar - 09-16-2014 Whenever I'm programming anything in ZX BASIC that involves non-trivial calculations, I really miss the conditional operator from C. So much that I finally decided to post a request here... Since ZX BASIC already incorporates many useful operators from C like << (binary shift) and & (binary and), could you please consider incorporating C conditional operator also? What I mean is allowing code like this: Code: IF (a < 30) THEN To be written like this: Code: LET a = a + (a < 30 ? 2 : 1) Those unfamiliar with C programming may not recognize it at first, but after you get used to it, the latter version is a lot easier to read. It's also easier for a compiler to optimize a single expression using conditional operators, instead of multiple IFs. Also this kind of feature would not introduce any problems or ambiguity in the parser. For more complex expressions it makes a huge difference. Here's another example: Code: IF (row > 0) THEN Using conditional operators, all this code above would be simplified to this: Code: LET pos = pos | (row > 0 ? move & UP : TOP) | Makes sense? RE: Conditional operator? - einar - 02-27-2021 Bump! I'm missing this feature a lot. Is there any chance it will be implemented? RE: Conditional operator? - boriel - 02-28-2021 (02-27-2021, 02:40 AM)einar Wrote: Bump! Yes, It's also planned. Indeed, I was thinking of implementing this other one (FreeBasic / VisualBasic compatible): IFF IFF(cond, expr on True, expr on false). The only constraint is that both actions return the same type (both Strings, both numeric) RE: Conditional operator? - einar - 02-28-2021 IFF would be great, thanks!!! RE: Conditional operator? - javier2112 - 10-30-2021 (09-16-2014, 08:57 PM)einar Wrote: In Sinclair BASIC you can use AND and OR expressions for that. In this case you'd need this: LET a = ((a+2) AND (a < 30)) + ((a+1) AND (a >= 30)) Another example to truncate a float number: LET n = -23.4 LET t = INT n + (1 AND SGN n = -1) Unfortunately this doesn't work with Zx Basic ![]() RE: Conditional operator? - boriel - 10-30-2021 Can you explain further, please? What does not work? Are you compiling with —sinclair or —strict-bool options? If not, try it and let me know. RE: Conditional operator? - einar - 10-30-2021 Quote:In Sinclair BASIC you can use AND and OR expressions for that. Yes, but try implementing my second example using AND and OR. It's both illegible and inefficient. RE: Conditional operator? - javier2112 - 11-01-2021 (10-30-2021, 05:57 PM)boriel Wrote: Can you explain further, please? I thought Sinclair option just involved the ATTR, SCREEN$ and POINT features. I discovered this compiler some days ago and to me it's superb. However, I've tested the following program and results aren't as I expected (I compiled it with ZX Basic 1.16.0): Code: 10 let n = 2 These are: With Fuse emulator, a Spectrum +2 prints: 3 0 With Zx Basic and Sinclair option enabled, the Spectrum prints: 1 0 With Zx Basic and strict boolean option enabled, the Spectrum prints: 1 0 With Zx Basic and both Sinclair and strict boolean options enabled, the Spectrum prints: 1 0 Now I've realized that my python version is 3.6.9. Could this be the problem? RE: Conditional operator? - einar - 11-01-2021 More precisely, Sinclair BASIC evaluates "x AND y" as follows: Code: x AND y results x, when y is not zero And Boriel ZX Basic evaluates "x AND y" as follows: Code: x AND y results 1, when both x and y are not zero You only notice the difference when x is neither 1 or 0. |