aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nomsu.lua6
-rwxr-xr-xnomsu.moon6
-rwxr-xr-xtools/autoformat.nom17
-rwxr-xr-xtools/find.nom42
-rwxr-xr-xtools/parse.nom2
-rwxr-xr-xtools/test.nom6
-rwxr-xr-xtools/upgrade.nom11
7 files changed, 55 insertions, 35 deletions
diff --git a/nomsu.lua b/nomsu.lua
index 1b5a73e..266a4a4 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -108,7 +108,7 @@ local parser = re.compile([[ args <- {| (flag %sep)*
/ "-m" %sep (!("--" %sep) {[^%sep]+} %sep)* ("--" %sep)?
/ {[^%sep]+} %sep
/ {~ '' %sep? -> 'nomsu://tools/repl.nom' ~}) |} :})
- {:nomsu_args: {| (nomsu_flag %sep)* {:extra_args: {| ({[^%sep]+} %sep)* |} :} |} :}
+ {:nomsu_args: {| (nomsu_flag %sep)* {:extras: {| ({[^%sep]+} %sep)* |} :} |} :}
|} !.
flag <-
{:optimization: "-O" (%sep? %number)? :}
@@ -138,8 +138,8 @@ for _index_0 = 1, #_list_0 do
local argpair = _list_0[_index_0]
nomsu_args[argpair.key] = argpair.value
end
-nomsu_args.extra_args = List(args.nomsu_args.extra_args or { })
-nomsu_environment.command_line_args = nomsu_args
+nomsu_args.extras = List(args.nomsu_args.extras or { })
+nomsu_environment.COMMAND_LINE_ARGS = nomsu_args
nomsu_environment.OPTIMIZATION = tonumber(args.optimization or 1)
local run
run = function()
diff --git a/nomsu.moon b/nomsu.moon
index d9a7d18..b8b64a7 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -67,7 +67,7 @@ parser = re.compile([[
/ "-m" %sep (!("--" %sep) {[^%sep]+} %sep)* ("--" %sep)?
/ {[^%sep]+} %sep
/ {~ '' %sep? -> 'nomsu://tools/repl.nom' ~}) |} :})
- {:nomsu_args: {| (nomsu_flag %sep)* {:extra_args: {| ({[^%sep]+} %sep)* |} :} |} :}
+ {:nomsu_args: {| (nomsu_flag %sep)* {:extras: {| ({[^%sep]+} %sep)* |} :} |} :}
|} !.
flag <-
{:optimization: "-O" (%sep? %number)? :}
@@ -91,8 +91,8 @@ if not args or args.help
nomsu_args = Dict{}
for argpair in *args.nomsu_args
nomsu_args[argpair.key] = argpair.value
-nomsu_args.extra_args = List(args.nomsu_args.extra_args or {})
-nomsu_environment.command_line_args = nomsu_args
+nomsu_args.extras = List(args.nomsu_args.extras or {})
+nomsu_environment.COMMAND_LINE_ARGS = nomsu_args
nomsu_environment.OPTIMIZATION = tonumber(args.optimization or 1)
run = ->
diff --git a/tools/autoformat.nom b/tools/autoformat.nom
index 76d1d27..d878a02 100755
--- a/tools/autoformat.nom
+++ b/tools/autoformat.nom
@@ -1,16 +1,21 @@
#!/usr/bin/env nomsu -V5.12.12.8
#
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.
+ nomsu -t autoformat [-i] file1 file2...
+
+ If the "-i" flag is used, the file will be edited in-place.
+ If no files are passed in, this will read from stdin.
use "lib/os.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-$args = (command line args)
-for $filename in $args.extra_args:
+$filenames = $(COMMAND LINE ARGS).extras
+if ((#$filenames) == 0):
+ say "Warning: reading from stdin (ctrl-d to abort). To avoid this message, use nomsu -t autoformat -"
+ $filenames = ["stdin"]
+
+for $filename in $filenames:
$file = (read file $filename)
unless $file:
barf "File does not exist: \$filename"
@@ -20,7 +25,7 @@ for $filename in $args.extra_args:
$formatted = "
\$leading_indent\((($tree as nomsu)|text)|with "\n" -> "\n\$leading_indent")"
- if $args."-i":
+ if $(COMMAND LINE ARGS)."-i":
write $formatted to file $filename
..else:
say $formatted inline
diff --git a/tools/find.nom b/tools/find.nom
index 84f69b4..aeb535d 100755
--- a/tools/find.nom
+++ b/tools/find.nom
@@ -1,6 +1,9 @@
#!/usr/bin/env nomsu -V5.12.12.8
#
- This is a tool to find syntax trees matching a pattern. ("*" is a wildcard)
+ This is a tool to find syntax trees matching a pattern. "*" is a wildcard
+ that will match any subtree, and "**" is a wildcard that will match any
+ 0 or more subtrees. "**" is greedy, so extra arguments after it will
+ not match.
nomsu -t find [flags] "* squared" file1 file2...
@@ -18,12 +21,21 @@ use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-$wildcard = ((command line args)."--wildcard" or "%*")
-$pattern = ((command line args).extra_args.1|with $wildcard -> "$wildcard")
+$wildcard = ($(COMMAND LINE ARGS)."--wildcard" or "%*")
+$pattern = $(COMMAND LINE ARGS).extras.1
+if (any of [not $pattern, $pattern == "*", $pattern == "**"]):
+ barf "Usage: nomsu -t find [-l] [--wildcard=<wildcard>] <pattern>, where <pattern> is valid Nomsu code"
+
+$pattern = ($pattern|with "\$wildcard\$wildcard" -> "$multi_wildcard")
+$pattern = ($pattern|with $wildcard -> "$wildcard")
$pattern_tree = ($pattern parsed)
+say $pattern_tree
($tree matches $patt) means:
when:
+ (not ($tree is syntax tree)):
+ return (no)
+
(($patt.type == "Var") and ($patt.1 == "wildcard")):
return (yes)
@@ -33,25 +45,29 @@ $pattern_tree = ($pattern parsed)
($tree.type == "Action"):
if (($tree|get stub) != ($patt|get stub)):
return (no)
- ..else:
- if ((#$tree) < (#$patt)):
- return (no)
-
- ((#$tree) != (#$patt)):
- return (no)
for $ in 1 to (#$patt):
if ($patt.$ is syntax tree):
+ if ($patt.$ == \$multi_wildcard):
+ return (yes)
+
unless ($tree.$ matches $patt.$):
return (no)
..else:
unless ($tree.$ == $patt.$):
return (no)
+ if ((#$tree) != (#$patt)):
+ return (no)
+
return (yes)
-for $ in 2 to (size of (command line args).extra_args):
- $filename = (command line args).extra_args.$
+$filenames = ($(COMMAND LINE ARGS).extras|from 2 to -1)
+if ((#$filenames) == 0):
+ say "Warning: searching stdin (ctrl-d to abort). To avoid this message, use nomsu -t find -"
+ $filenames = ["stdin"]
+
+for $filename in ($filenames):
$file = (read file $filename)
unless $file:
barf "File does not exist: \$filename"
@@ -63,7 +79,7 @@ for $ in 2 to (size of (command line args).extra_args):
$tree = (nil)
unless $tree:
- do next $
+ do next $filename
$results = []
for $t in recursive $tree:
@@ -76,7 +92,7 @@ for $ in 2 to (size of (command line args).extra_args):
if ($sub is syntax tree):
recurse $t on $sub
- if ((command line args)."-l"):
+ if ($(COMMAND LINE ARGS)."-l"):
if ((#$results) > 0):
say $filename
..else:
diff --git a/tools/parse.nom b/tools/parse.nom
index e6768a6..1005983 100755
--- a/tools/parse.nom
+++ b/tools/parse.nom
@@ -36,7 +36,7 @@ externally (print tree $t at indent $indent) means:
else:
say "\$indent \(quote $arg)"
-for $filename in (command line args).extra_args:
+for $filename in $(COMMAND LINE ARGS).extras:
$file = (read file $filename)
unless $file:
barf "File does not exist: \$filename"
diff --git a/tools/test.nom b/tools/test.nom
index 5b36010..7b5842c 100755
--- a/tools/test.nom
+++ b/tools/test.nom
@@ -9,9 +9,9 @@ use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Make sure all the files get run
-for $filename in (command line args).extra_args: use $filename
-$tests = {: for $s = $t in (tests): add (=lua "Source:from_string(\$s)") = $t}
-for $filename in (command line args).extra_args:
+for $filename in $(COMMAND LINE ARGS).extras: use $filename
+$tests = {: for $s = $t in $TESTS: add (=lua "Source:from_string(\$s)") = $t}
+for $filename in $(COMMAND LINE ARGS).extras:
$file = (read file $filename)
$version = ($file|matching "#![^\n]* nomsu %-V[ ]*([^\n]*)")
$file_tests = []
diff --git a/tools/upgrade.nom b/tools/upgrade.nom
index 9244203..96a1312 100755
--- a/tools/upgrade.nom
+++ b/tools/upgrade.nom
@@ -11,12 +11,11 @@ use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-$args = (command line args)
-$inplace = ($args."-i" or $args."--inplace")
-$start_version = $args."--upgrade-from"
-$version = ($args."--upgrade-to" or (Nomsu version))
-$test = ($args."-t" or $args."--test")
-for $filename in $args.extra_args:
+$inplace = ($(COMMAND LINE ARGS)."-i" or $(COMMAND LINE ARGS)."--inplace")
+$start_version = $(COMMAND LINE ARGS)."--upgrade-from"
+$version = ($(COMMAND LINE ARGS)."--upgrade-to" or (Nomsu version))
+$test = ($(COMMAND LINE ARGS)."-t" or $(COMMAND LINE ARGS)."--test")
+for $filename in $(COMMAND LINE ARGS).extras:
$file = (read file $filename)
unless $file:
barf "File does not exist: \$filename"