ribosome
ribosome copied to clipboard
Unless macro example
Hi! I'm trying to make a simple macro called unless
, which generates if not expr:
line, but it's not quite clear how to do it with ribosome.
My attempt is:
#!/usr/bin/env python
def unless(x, text):
.if not @{x}:
. @{text}
def main():
for i in range(3):
.var@{i} = @{i*10}
unless(
.var0
,
.print var2
)
main()
but it generates:
var0 = 0
var1 = 10
var2 = 20
var0
var2
if not None:
None
So, lines go directly to output, and programmer has no access to them. I checked ribosome code and found that these None
s were returned from dot
method. Changing it to
# Adds newline followed by one . line from the DNA file.
@staticmethod
def dot(line, bind):
Block.stack[-1].append(Block(''))
Block.add(line, bind)
return line
gave me something better:
var0 = 0
var1 = 10
var2 = 20
var0
print var2
if not var0:
print var2
But these lines still go directly to output. So, am I missing something? Is it possible to make unless
with ribosome? Is it that complex to work with tokens, instead of whole lines? Instead of
unless(
.var0
,
.print var2
)
I would prefer something like
.`{unless( .var0 , .print var2 )}
And with blocks:
.`{unless( .var0 ,
.` print 1
.` print 2 }
I think you have a wrong conceptual model of what ribosome is. To make it simple, imagine that every line starting with dot is actually a print statement. Thus:
unless(
.var0
,
.print var2
)
translates to:
unless(print("var0"), print("print var2"))
which in probably not what you want.