rev: release-0.15 scopes/doc/build_reference.sc -rwxr-xr-x 7.7 KiB View raw Log this file
e53de6d7cb89 — Leonard Ritter * win32 build fix 1 year, 5 months 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env scopes
using import Array

let argc argv = (launch-args)
assert (argc >= 2)
let scriptpath =
    string (argv @ 1)
let modulepath module =
    if (argc < 3)
        _ "core.sc" (globals)
    else
        let modulepath =
            string (argv @ 2)
        _ modulepath
            (require-from module-dir modulepath) as Scope

let module = (module as Scope)

fn starts-with-letter (s)
    let c = (s @ 0)
    or
        (c >= (char "a")) and (c <= (char "z"))
        (c >= (char "A")) and (c <= (char "Z"))

let EntryT = (tuple Symbol Value)
local objs : (GrowingArray EntryT)

let used-keys = (Scope)
loop (scope = module)
    if (scope == null)
        break;
    let parent = ('parent scope)
    let used = `true
    for k in ('deleted scope)
        try
            '@ used-keys k
            ;
        except (err)
            'set-symbol used-keys k used
    for k v in scope
        try
            '@ used-keys k
            ;
        except (err)
            'set-symbol used-keys k used
            let T = ('typeof v)
            'append objs
                tupleof k v
            ;
    repeat parent

fn entry-key (x)
    let key entry = (unpack x)
    let T = ('typeof entry)
    let s = (key as string)
    ..
        do
            if (T == Closure) "C"
            elseif (T == Builtin) "E"
            elseif (T == SugarMacro) "D"
            elseif (T == SpiceMacro) "F"
            elseif ('function-pointer? T) "G"
            elseif (T == type) "B"
            else "A"
        if (starts-with-letter s) s
        else
            if ((countof s) < 2:usize)
                .. " 1" s
            else
                .. " 2" s

'sort objs entry-key

fn member-key (x)
    let key entry = (unpack x)
    let T = ('typeof entry)
    let s = (key as string)
    s

fn docstring-is-complete (str)
    (slice str 0 2) == ".."

fn repeat-string (n c)
    loop (i s = 0:usize "")
        if (i == n)
            return s
        _ (i + 1:usize)
            .. s c

fn write-docstring (tab str)
    let c = (countof str)
    if (c > 0)
        io-write! tab
        io-write! "\n"
        io-write! tab
        for i in (range c)
            let s = (slice str i (i + 1))
            io-write! s
            if ((s == "\n") and ((i + 1) != c))
                io-write! tab

fn print-entry (module parent key entry parent-name opts...)
    let print-entry = this-function
    let typemember? = ((typeof parent) == type)
    let sym = key
    let key = (key as string)
    let prefix1 = (slice key 0 1)
    let prefix2 = (slice key 0 2)
    if (prefix1 == "#")
        return;
    elseif
        and (prefix2 == "__")
            not typemember?
                #and typemember?
                    or
                        sym == '__typecall
                        sym == '__call
        return;
    let T = ('typeof entry)
    let docstr =
        static-if typemember?
            'docstring parent sym
        else
            'docstring module sym
            #parent @ (Symbol (.. "#doc:" key))
    let has-docstr = (not (empty? docstr))
    let docstr =
        if has-docstr (docstr as string)
        else ""
    let indent =
        if typemember? "   "
        else ""
    let tab =
        if typemember? "      "
        else "   "
    if (docstring-is-complete docstr)
        if typemember?
            write-docstring indent docstr
        else
            io-write! docstr
            io-write! "\n"
        return;
    if (T == Closure)
        let func = (entry as Closure)
        let docstr2 = ('docstring func)
        let label =
            sc_closure_get_template func
        if (docstring-is-complete docstr2)
            io-write! docstr2
            io-write! "\n"
        else
            io-write! indent
            if (sc_template_is_inline label)
                io-write! ".. inline:: ("
            else
                io-write! ".. fn:: ("
            io-write! key
            let count = (sc_template_parameter_count label)
            for i in (range count)
                let param = (sc_template_parameter label i)
                io-write! " "
                io-write! ((sc_parameter_name param) as string)
            io-write! ")\n"
            if (empty? docstr)
                write-docstring tab docstr2
            else
                write-docstring tab docstr
        return;
    elseif (T == Builtin)
        io-write! indent
        io-write! ".. builtin:: ("
        io-write! key
        io-write! " ...)\n"
    elseif (T == SugarMacro)
        io-write! indent
        io-write! ".. sugar:: ("
        io-write! key
        io-write! " ...)\n"
    elseif (T == SpiceMacro)
        io-write! indent
        io-write! ".. spice:: ("
        io-write! key
        io-write! " ...)\n"
    elseif (T == type)
        if (typemember? and (not has-docstr))
            return;
        let ty = (entry as type)
        io-write! indent
        io-write! ".. type:: "
        io-write! key
        let superT = ('superof ty)
        let ST =
            if ('opaque? ty) Unknown
            else ('storageof ty)
        io-write! "\n\n"
        if has-docstr
            write-docstring tab docstr
            io-write! "\n"
        else
            io-write! tab
            let has-storage? = (ST != Unknown)
            let plain? = ('plain? ty)
            io-write! "A"
            if has-storage?
                if plain?
                    io-write! " plain"
                else
                    io-write! "n unique"
            else
                io-write! "n opaque"
            io-write! " type"
            let tystr = (tostring ty)
            if ((let tystr = (tostring ty)) != key)
                io-write! " labeled ``"
                io-write! (tostring ty)
                io-write! "``"
            if (superT != typename)
                io-write! " of supertype `"
                io-write! (tostring superT)
                io-write! "`"
                if has-storage?
                    io-write! " and"
            if has-storage?
                io-write! " of storage type `"
                io-write! (tostring ST)
                io-write! "`"
            io-write! ".\n\n"
        if (not typemember?)
            local members : (GrowingArray EntryT)
            for k v in ('symbols ty)
                'append members
                    tupleof k v
            'sort members member-key
            for entry in members
                let k v = (unpack entry)
                let k = (dupe (deref k))
                let v = (dupe (deref v))
                print-entry module ty k v key
        return;
    elseif ('function-pointer? T)
        let fntype = ('element@ T 0)
        let params = ('element-count fntype)
        io-write! indent
        io-write! ".. compiledfn:: ("
        io-write! key
        io-write! " ...)\n\n"
        io-write! tab
        io-write! "A"
        if (('kind entry) == value-kind-global)
            io-write! "n external"
        else
            io-write! " compiled"
        io-write! " function of type ``"
        io-write! (tostring fntype)
        io-write! "``.\n"
    elseif (T == Unknown)
        # a dreaded curse!
        return;
    else
        if (typemember? and (not has-docstr))
            return;
        io-write! indent
        io-write! ".. define:: "
        io-write! key
        io-write! "\n"
        if (not has-docstr)
            io-write! "\n"
            io-write! tab
            io-write! "A constant of type `"
            io-write! (tostring T)
            io-write! "`.\n"
    write-docstring tab docstr

let moduledoc = ('docstring module unnamed)
if (not (empty? moduledoc))
    io-write! (moduledoc as string)
    io-write! "\n"

for entry in objs
    let key entry = (unpack entry)
    print-entry module module (deref key) (deref entry) ""