simplification
Download
101 lines 4.3 KiBNomsuModified 2026-11-08executable raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
#!/usr/bin/env nomsu -V8.0.0
###
    This is a tool to replace syntax trees with something new.

    Usage:
        nomsu -t replace [-i] [-f] [-q] [--literal="$v1 $v2..."] <pattern> <replacement> file1 file2...

    Example:
        nomsu -t replace "($1 and $2) and $3" "all of {$1, $2, $3}" my_file.nom

    Patterns work like they do for the "find" tool, and the replacement can use the
    pattern's variables, or "**" for what "**" matched.
    If the "-i" flag is used, the file(s) will be edited in-place.
    When editing in-place, if the "-f" flag is not used, each change will be
        run past the user first.
    If the "-q" flag is used and a file fails to parse, the original file
        contents will be output.
    If no files are passed in, this will read from stdin.

use "filesystem"
use "consolecolor"
use "commandline"
use "tree_search"

command line program with $args:
    if (#$args.extras < 2):
        fail ("
            Usage: nomsu -t replace [--literal="$v1 $v2..."] <pattern> <replacement> file1 file2...
        ")
    $pattern = (pattern $args.extras.1 with literals $args.literal)
    $replacement_tree = (($args.extras.2, with "%*%*" -> "$wildcard_rest") parsed)

    ### Put what the pattern's variables matched into the replacement
    ($t filled with $values) means:
        if (($t.type == "Var") and ($values.($t, as var) != (nil))):
            return $values.($t, as var)
        $new = ($t.type tree from $t.source)
        for $child in $t:
            if (($child is syntax tree) and ($child == \$wildcard_rest)):
                for $c in $values.wildcard_rest: $new, add $c
            ..else:
                $new, add (($child filled with $values) if ($child is syntax tree) else $child)
        return $new

    for $filename in (files to search ($args.extras from 3 to -1)):
        {$file, $tree, $error} = (read and parse $filename)
        if $error:
            if $args.q:
                unless $args.i: say $file
            ..else:
                say $error
            do next $filename

        $replaced = {}
        $matched = {}
        $user_answers = {}
        ($tree with replacements) means
            $tree, with
                $t ->:
                    $values = ($t matches $pattern)
                    if $values:
                        $matched.$t = (yes)
                        for ($k = $v) in $values:
                            if ($k == "wildcard_rest"):
                                $values.$k = (
                                    (($ with replacements) if ($ is syntax tree) else $) for $ in $v
                                )
                            ..else:
                                $values.$k = ($v with replacements)
                        $ret = ($replacement_tree filled with $values)
                        if ($args.i and (not $args.f)):
                            if ($user_answers.$t == (nil)):
                                if ((number of keys in $user_answers) > 0): say ""
                                $user_answers.$t = "n"
                                say "\(bright)Should this:"
                                say ("
                                        \(bright)\(yellow)\("\(($t with replacements) as nomsu)", with "\n" -> "\n    ")
                                ....\(reset color)
                                ")
                                say "\(bright)..be replaced with:"
                                say ("
                                        \(bright)\(blue)\("\($ret as nomsu)", with "\n" -> "\n    ")\(reset color)
                                ")
                                $user_answers.$t = (ask "\(bright)..? [Y/n]\(reset color) ")
                            if ($user_answers.$t == "n"): return (nil)
                        $replaced.$t = (yes)
                        return $ret

        $tree2 = ($tree with replacements)
        if $args.i:
            if ((number of keys in $user_answers) > 0): say ""
            $num_replaced = (number of keys in $replaced)
            say ("
                \$num_replaced/\(number of keys in $matched) replacement
            ....\("" if ($num_replaced == 1) else "s") in \$filename
            ")

            if ($num_replaced > 0):
                write "\($tree2 as nomsu)\n" to file $filename
        ..else:
            say ($tree2 as nomsu)