Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unless macro example #43

Open
maximvl opened this issue Dec 19, 2015 · 1 comment
Open

Unless macro example #43

maximvl opened this issue Dec 19, 2015 · 1 comment

Comments

@maximvl
Copy link

maximvl commented Dec 19, 2015

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 }
@sustrik
Copy link
Owner

sustrik commented Dec 22, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants