rev: 08a0786d94a50b3b229836c53a03976f10ab6bd7 scopes/lib/scopes/chaining.sc -rw-r--r-- 1.7 KiB View raw Log this file
08a0786d94a5Shawn Walker-Salas * drop number sections in menu due to readthedocs theme incompatibility 19 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.

""""chaining
    ========

    chaining provides the `-->` operator, which allows the nesting of
    expressions by chaining them in a sequence.

""""Expands a processing chain into nested expressions so that each expression
    is passed as a tailing argument to the following expression.

    `__` can be used as a placeholder token to position the previous expression.

    Example:

        :::scopes
        --> x
            f
            g
            h 2 __
            k

    Expands to:

        :::scopes
        k
            h 2
                g
                    f x
sugar --> (expr ...)
    fn placeholder? (elem)
        (('typeof elem) == Symbol) and (elem as Symbol == '__)

    fold (outp = expr) for expr in ...
        let anchor = ('anchor expr)
        'tag
            match ('typeof expr)
            case list
                let prev-outp = outp
                let expr = (expr as list)
                let outp found =
                    fold (outp found = '() false) for elem in expr
                        if (placeholder? elem)
                            if found
                                hide-traceback;
                                error@ ('anchor elem) "while expanding expression" "duplicate placeholder token"
                            _ (cons prev-outp outp) true
                        else
                            _ (cons elem outp) found
                if found
                    `[('reverse outp)]
                else
                    `[(.. expr (list prev-outp))]
            default
                `[(list expr outp)]
            anchor

do
    let -->

    locals;