rev: ebafc720a90fa0562f276f82113a43e8e7c3ee88 scopes/testing/test_loop.sc -rw-r--r-- 2.7 KiB View raw Log this file
ebafc720a90f — Leonard Ritter * merged default branch 7 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using import testing

# default form
let x =
    do
        loop (i k = 0 64)
            # last line in loop is fed back into loop
            # breaks must be explicit
            if (i < k)
                _ (i + 1) k
            else
                break i
test (x == 64)

# while loop
local i = 10
while (i != 0)
    i = i - 1
    if (i == 3)
        continue;
    print i

# infinite loop
do
    local x = 0
    loop ()
        if (x < 100)
            x += 1
            repeat;
        else
            break;

fn test_2d_loops ()
    let w h = 4 4

    # method 1: two nested loops using generators
    for y in (range h)
        for x in (range w)
            print x y

    print;

    # method 2: two basic nested loops (permitting immutable state changes)
    loop (y = 0)
        if (y < h)
            do
                loop (x = 0)
                    if (x < w)
                        print x y
                        repeat (x + 1)
                    else
                        break;
            repeat (y + 1)
        else
            break;

    print;

    # method 3: single loop, two counters
    if (h <= 0)
        return;
    let y1 = (h - 1)
    loop (x y = 0 0)
        if (x < w)
            print x y
            repeat (x + 1) y
        elseif (y < y1)
            repeat 0 (y + 1)
        else
            break;

    print;

    # method 4: single loop, one counter
    let size = (w * h)
    loop (i = 0)
        if (i < size)
            let x y = (i % w) (i // h)
            print x y
            repeat (i + 1)
        else
            break;

test_2d_loops;

# returning from an inner loop
fn looper ()
    for i in (range 32)
        if (i == 5)
            return false
    true

test (not (looper))

test-compiler-error
    embed
        # loop that never breaks
        loop (i = 0)
            repeat (i + 1)
        true

do
    # for-in/else form

    fn contains? (x)
        for i in (range 10)
            if (i == x)
                break true
        else
            print "not found"
            false

    assert (contains? 3)
    assert (not (contains? 13))

do
    let u v w =
        fold (a b c = 1 2 3) for i in (range 3)
            print a b c
            if true
                repeat 4 5 6
            _ 1 2 3
    test (u == 4)
    test (v == 5)
    test (w == 6)

do
    # loop in fold must use standard repeat
    fold (a b c = 1 2 3) for i in (range 10)
        unlet repeat
        unlet break
        loop (a b = 1 2)
            if (a < 5)
                repeat (a + 1) (b + 1)
            else
                break a b
        _ a b c

do
    # reverse loops
    for i in (rrange 0 10)
        test ((i >= 0) & (i < 10))
    # unsigned values
    for i in (rrange 0:u32 10:u32)
        test ((i >= 0:u32) & (i < 10:u32))

true