From c1c32688a4afc43f6addb99b8b5fa878944a70e3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 14 Jan 2019 15:42:48 -0800 Subject: Overhaul in progress, mostly working. Moved all the nomsu packages into lib/, including core/*. Changes to how nomsu environments and importing work. --- lib/tools/format.nom | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 lib/tools/format.nom (limited to 'lib/tools/format.nom') diff --git a/lib/tools/format.nom b/lib/tools/format.nom new file mode 100755 index 0000000..e60d12e --- /dev/null +++ b/lib/tools/format.nom @@ -0,0 +1,45 @@ +#!/usr/bin/env nomsu -V6.14 +# + 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 (size of $file)) $file) + try: + $tree = ($code parsed) + ..if it fails $msg: + if $args.q: + $formatted = $file + ..else: + say $msg + + if ($tree and (not $formatted)): + $formatted = + "\$leading_indent\($tree as nomsu, text, with "\n" -> "\n\$leading_indent")" + + if $formatted: + if $args.i: + write $formatted to file $filename + ..else: + say $formatted inline -- cgit v1.2.3