aboutsummaryrefslogtreecommitdiff
path: root/lib/os.nom
blob: c165e5a803e0c4f2082d1cf2fea7419c28124eaf (plain)
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
#!/usr/bin/env nomsu -V4.10.12.7
#
    This file defines some actions that interact with the operating system and filesystem.
    
test:
    path of Nomsu file "lib/os.nom"

externally (path of Nomsu file %filename) means:
    lua> "for i,f in Files.walk(\%filename) do return f end"
    barf "Could not find file: \%filename"

externally (sh> %cmd) means:
    lua> "\
        ..local result = io.popen(\%cmd)
        local contents = result:read("*a")
        result:close()
        return contents"

test:
    read file "lib/os.nom"

externally (read file %filename) means (=lua "Files.read(\%filename)")
test:
    for file %f in "core": do nothing

(for file %f in %path %body) compiles to "\
    ..for i,\(%f as lua expr) in Files.walk(\(%path as lua expr)) do
        \(%body as lua)
        \(what (===next %f ===) compiles to)
    end
    \(what (===stop %f ===) compiles to)"

(%expr for file %f in %path) compiles to "\
    ..(function()
        local ret = List{}
        for i,\(%f as lua expr) in Files.walk(\(%path as lua expr)) do
            ret[#ret+1] = \(%expr as lua)
        end
        return ret
    end)()"

externally [..]
    write to file %filename %text, to file %filename write %text
    write %text to file %filename
..all mean:
    assume (%filename != "stdin") or barf "Cannot write to stdin"
    lua> "\
        ..local file = io.open(\%filename, 'w')
        file:write(\%text)
        file:close()"

test:
    assume (line number of 3 in "x\ny") == 2

externally (line number of %pos in %str) means (..)
    =lua "Files.get_line_number(\%str, \%pos)"

test:
    assume (line 2 in "one\ntwo\nthree") == "two"

externally (line %line_num in %str) means (..)
    =lua "Files.get_line(\%str, \%line_num)"

test:
    assume (source lines of \(this))

externally (source lines of %tree) means:
    %source = (%tree.source if (%tree is syntax tree) else %tree)
    %file = (read file %source.filename)
    return (..)
        [..]
            :
                for % in (line number of %source.start in %file) to (..)
                    line number of %source.stop in %file
                ..: add (line % in %file)
        ..::joined with "\n"

externally (spoof file %text) means (%Files.spoof %text)
externally (spoof file %filename = %text) means (%Files.spoof %filename %text)