aboutsummaryrefslogtreecommitdiff
path: root/lib/os.nom
blob: 329a77f32987e995ae0bf4abfd36716cdd2e88ac (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
#!/usr/bin/env nomsu -V1
#
    This file defines some actions that interact with the operating system and filesystem.

use "core"

action [path of Nomsu file %filename]
    lua> ".."
        for f in files.walk(\%filename) do return f end
    barf "Could not find file: \%filename"

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

action [read file %filename]
    =lua "files.read(\%filename)"

compile [for file %f in %path %body] to
    Lua ".."
        for \(%f as lua expr) in files.walk(\(%path as lua expr)) do
            \(%body as lua statements)
            \(compile as: === next %f ===)
        end
        \(compile as: === stop %f ===)

compile [%expr for file %f in %path] to
    Lua value ".."
        (function()
            local ret = list{}
            for \(%f as lua expr) in files.walk(\(%path as lua expr)) do
                ret[#ret+1] = \(%expr as lua statements)
            end
            return ret
        end)()

action [write to file %filename %text, to file %filename write %text]
    lua> ".."
        local file = io.open(\%filename, 'w')
        file:write(\%text)
        file:close()

action [line number of %pos in %str]
    =lua "files.get_line_number(\%str, \%pos)"
    
action [line %line_num in %str]
    =lua "files.get_line(\%str, \%line_num)"