Skip to content

Unless macro example #43

@maximvl

Description

@maximvl

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 Nones 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 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions