simplification
Download
40 lines 1.3 KiBNomsuModified 2026-11-08executable raw
12345678910111213141516171819202122232425262728293031323334353637383940
#!/usr/bin/env nomsu -V8.0.0
###
    Auto-format Nomsu code. Usage:
    nomsu -t format [-i] file1 file2...
    
    If the "-i" flag is used, the file will be edited in-place.
    If the "-q" flag is used and an error occurs, the original file will be printed.
    If no files are passed in, this will read from stdin.
    
use "filesystem"
use "commandline"

command line program with $args:
    $filenames = $args.extras
    if (#$filenames == 0):
        say ("
            Warning: reading from stdin (ctrl-d to abort). To avoid this message, use nomsu -t format -
        ")
        $filenames = {"stdin"}
    
    for $filename in $filenames:
        $file = (read file $filename)
        unless $file:
            fail "File does not exist: \$filename"
        $leading_indent = ($file, matching "^\n*([ ]*)")
        $code = (NomsuCode from (Source $filename 1 #$file) $file)
        try:
            $tree = ($code parsed)
            $formatted = ($tree as nomsu, text, indented $leading_indent)
        ..if it fails with $msg:
            if $args.q:
                $formatted = $file
            ..else:
                say $msg
                do next $filename
        
        if $args.i:
            write "\$formatted\n" to file $filename
        ..else:
            say $formatted inline