Add ternary (or elegant conditional)
AviFS opened this issue · comments
Eg. decide how to print the state in a brainfuck program, so it looks like this, with a *
for the ptr & _
for the 0s:
The BF should print:
+>>++++++++++++++++>-<
1 _ 16* 255
<
1 0* 16 255
The basic idea is:
.{tape[i]?(tape[i]:'0'):>3}{ptr[i]?(' ':'*')} ..
We've already lost much of the elegance that I was hoping this lang would have, but tbh it was much easier to write than the corresponding code in python. It might have to be one of the easy-to-write-impossible-to-read langs. Is there a way around this? Do we care to change it if that is the case? I imagine it would just involve making the language less terse, but that I doubt is something we want to do.
Just for experiment's sake, here's how it'd look if more pythonic and verbose:
.{tape[i] if tape[i] else '0'):>3}{'*' if ptr[i] else ' '} ..
Alternatively:
.{if tape[i] then tape[i] else '0'):>3}{if ptr[i] then '*' else ' '} ..
That may actually be worth considering. This is also about as complicated as they should ever be getting with this markup, though. And syntax highlighting should help a lot! We all got used to f-strings and and all those '%s'.format()
, didn't we?
Come up with other conditional-requiring line-based things to print and play around with syntaxes!
EDIT: I almost forgot. This!
nzero = tape[i]?tape[i]:'0'
pointer = ptr[i]?' ':'*'
.{zero:>3}{pointer}..
Implementing a construct where you can declare vars (and sneakily declare functions of i
& j
as simple vars, too) like that might just be the way to go! It doesn't even matter if/how terse the ternaries are if one can do this!
I wasn't totally sure with the options before, but now that I've edited that in I'm definitely leaning towards the last option. I forgot I'd had that idea a few nights ago. I'll make a new issue for the var/func declaring piece but leave this #design-choice open for other conditional ideas & thoughts on the ternary syntax!