aboutsummaryrefslogtreecommitdiff
path: root/tools/autoformat.nom
blob: 8626c91e0c900037c5d84a25ca10f1cc9e18415d (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
#!/usr/bin/env nomsu -V4.10.12.7
#
    Auto-format Nomsu code. Usage:
        nomsu tools/autoformat.nom [-i] file1 file2 directory1 ...
    If the first argument is "-i", modifications will be performed in-place. Otherwise,
    the formatted code will be printed.
    
use "lib/os.nom"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%args = (command line args)
%inplace = (no)
if (%args.1 is "-i"):
    %inplace = (yes)
    %args::remove index 1

for %path in %args:
    if (%path == "-"): %path = "stdin"
    for file %filename in %path:
        unless ((%filename::matches "%.nom$") or (%filename == "stdin")): do next %filename
        %contents = (read file %filename)
        %code = (NomsuCode from (Source %filename 1 (size of %contents)) %contents)
        %tree = (%code parsed)
        %formatted = ((%tree as nomsu)::text)
        if %inplace:
            write %formatted to file %filename
        ..else:
            say %formatted inline