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

Link loop #418

Closed
bvssvni opened this issue Dec 30, 2016 · 0 comments
Closed

Link loop #418

bvssvni opened this issue Dec 30, 2016 · 0 comments
Assignees

Comments

@bvssvni
Copy link
Member

bvssvni commented Dec 30, 2016

For more information about the link type, see #227.

Dyon has a link loop that makes repeated patterns of code generation or templates easier and faster.

The link items inside loop body is evaluated directly as if it was inside a link { ... } block.

For example:

gen_struct(name, fields: [{}]) = link {
    "pub struct "name" {\n"link i {
    "    pub "fields[i].name": "fields[i].type",\n"
    }"}\n\n"
}

Void expressions, continue and break

You can put expressions that do not return a value inside a link loop, and also use continue and break like in other loops. This is useful for filtering:

fn main() {
    list := sift i 10 {i+1}
    x := link i {
        // The `else` case returns no value, but that is OK.
        if (list[i] > 2) && (list[i] <= 7) {continue}
        list[i]", "
    }
    // prints `1, 2, 8, 9, 10,`
    println(x)
}

The continue keyword skips the rest of the link items for the current iteration, but link items that already are evaluated will be inserted.

The break keyword skips the rest of the link items and exits the loop.

Here is an example where you print out a list without a trailing comma:

fn main() {
    list := sift i 10 {i+1}
    last := len(list)-1
    x := link i {list[i] if i == last {break} ", "}
    // prints `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`
    println(x)
}

When using a link { ... } inside the body, you get all-or-nothing case. Notice how 10 disappeared compared to the last example:

fn main() {
    list := sift i 10 {i+1}
    last := len(list)-1
    x := link i {link {list[i] if i == last {break} ", "}}
    // prints `1, 2, 3, 4, 5, 6, 7, 8, 9, `
    println(x)
}

You can use packed loop notation with the link loop as if it was a flat loop. Here is an advanced example:

fn main() {
    n := 3
    list := sift i n, j n, k n {floor(random()*9)+1}
    last := n-1
    x := link {"["'out: link i, j, k {
        if (i != 0) && (j == 0) && (k == 0) {" "}
        list[i][j][k]
        if (i == last) &&
           (j == last) &&
           (k == last) {break 'out}
        else if (j == last) &&
                (k == last) {";\n"}
        else if (k == last) {", "}
        else {" "}
    }"]"}
    // prints something like:
    // ```
    // [1 6 7, 1 7 7, 6 1 3;
    //  8 1 7, 5 7 9, 7 1 9;
    //  8 6 1, 7 9 8, 8 7 9]
    // ```
    println(x)
}
@bvssvni bvssvni changed the title Add link loop Link loop Dec 31, 2016
@bvssvni bvssvni self-assigned this Dec 31, 2016
@bvssvni bvssvni mentioned this issue Dec 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant