Some changes to the error API, a fix for statement block parsing, and

replacing ((foo 1 baz)'s meaning) with $(foo 1 baz).
This commit is contained in:
Bruce Hill 2019-01-01 17:15:51 -08:00
parent e68eb04d69
commit 3e89092833
47 changed files with 252 additions and 184 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <2.3 to Nomsu 2.3 This file defines upgrades from Nomsu <2.3 to Nomsu 2.3

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <2.4 to Nomsu 2.4 This file defines upgrades from Nomsu <2.4 to Nomsu 2.4

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <2.5.5.5 to Nomsu 2.5.5.5 This file defines upgrades from Nomsu <2.5.5.5 to Nomsu 2.5.5.5

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <2.5 to Nomsu 2.5 This file defines upgrades from Nomsu <2.5 to Nomsu 2.5

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu 1 to Nomsu 2 This file defines upgrades from Nomsu 1 to Nomsu 2

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <3.5.5.6 to Nomsu 3.5.5.6 This file defines upgrades from Nomsu <3.5.5.6 to Nomsu 3.5.5.6

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <3.6 to 3.6 This file defines upgrades from Nomsu <3.6 to 3.6

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <3.7 to 3.7 This file defines upgrades from Nomsu <3.7 to 3.7

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <3.8 to 3.8 (Text method changes) This file defines upgrades from Nomsu <3.8 to 3.8 (Text method changes)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <=2 to Nomsu 3 This file defines upgrades from Nomsu <=2 to Nomsu 3

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <4.10.12.7 to 4.10.12.7 This file defines upgrades from Nomsu <4.10.12.7 to 4.10.12.7
use "compatibility/compatibility.nom" use "compatibility/compatibility.nom"
@ -24,8 +24,10 @@ upgrade action ($e for $i in $start to $stop by $step) to "4.10.12.7" as
upgrade action ($e for $i in $start to $stop via $step) to "4.10.12.7" as upgrade action ($e for $i in $start to $stop via $step) to "4.10.12.7" as
[: for $i in $start to $stop by $step: add $e] [: for $i in $start to $stop by $step: add $e]
upgrade action ($k = $v for $ in $items) to "4.10.12.7" as upgrade action ($k = $v for $ in $items) to "4.10.12.7" as {
{: for $ in $items: add $k = $v} : for $ in $items:
add $k = $v
}
upgrade action ($k = $v for $k0 = $v0 in $items) to "4.10.12.7" as upgrade action ($k = $v for $k0 = $v0 in $items) to "4.10.12.7" as
{: for $k0 = $v0 in $items: add $k = $v} {: for $k0 = $v0 in $items: add $k = $v}

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <4.11 to Nomsu 4.11 This file defines upgrades from Nomsu <4.11 to Nomsu 4.11
(overhaul of function literals, deleting (if all of ...), etc. shorthand) (overhaul of function literals, deleting (if all of ...), etc. shorthand)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <4.11 to Nomsu 4.11 This file defines upgrades from Nomsu <4.11 to Nomsu 4.11
(overhaul of function literals, deleting (if all of ...), etc. shorthand) (overhaul of function literals, deleting (if all of ...), etc. shorthand)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <4.8.10 to 4.8.10 (renaming "action" -> "means") This file defines upgrades from Nomsu <4.8.10 to 4.8.10 (renaming "action" -> "means")
use "compatibility/compatibility.nom" use "compatibility/compatibility.nom"

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines upgrades from Nomsu <4.9 to 4.9 This file defines upgrades from Nomsu <4.9 to 4.9
use "compatibility/compatibility.nom" use "compatibility/compatibility.nom"

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains code for defining ways to upgrade code between different versions This file contains code for defining ways to upgrade code between different versions
of Nomsu. of Nomsu.
@ -85,16 +85,16 @@ externally [
$tree with $ ->: $tree with $ ->:
if (($ is "Action" syntax tree) and $ACTION_UPGRADES.$ver.($.stub)): if (($ is "Action" syntax tree) and $ACTION_UPGRADES.$ver.($.stub)):
$with_upgraded_args = { $with_upgraded_args = {
: for $k = $v in $: add $k = ($v upgraded from $start_version to $end_version) : for $k = $v in $:
add $k = ($v upgraded from $start_version to $end_version)
} }
set $with_upgraded_args's metatable to ($'s metatable) set $with_upgraded_args's metatable to ($'s metatable)
return ($ACTION_UPGRADES.$ver.($.stub) $with_upgraded_args $end_version) return ($ACTION_UPGRADES.$ver.($.stub) $with_upgraded_args $end_version)
if $UPGRADES.$ver: if $UPGRADES.$ver:
$with_upgraded_args = { $with_upgraded_args = {
: : for $k = $v in $tree:
for $k = $v in $tree: add $k = ($v upgraded from $start_version to $end_version)
add $k = ($v upgraded from $start_version to $end_version)
} }
set $with_upgraded_args's metatable to ($tree's metatable) set $with_upgraded_args's metatable to ($tree's metatable)
$tree = ($UPGRADES.$ver $with_upgraded_args $end_version) $tree = ($UPGRADES.$ver $with_upgraded_args $end_version)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains code that supports manipulating and using collections like lists This file contains code that supports manipulating and using collections like lists
and dictionaries. and dictionaries.
@ -60,8 +60,10 @@ externally ($lists flattened) means:
test: test:
assume ((entries in {.x = 1}) == [{.key = "x", .value = 1}]) assume ((entries in {.x = 1}) == [{.key = "x", .value = 1}])
(entries in $dict) parses as (entries in $dict) parses as [
[: for $k = $v in $dict: add {.key = $k, .value = $v}] : for $k = $v in $dict:
add {.key = $k, .value = $v}
]
test: test:
assume ((keys in {.x = 1}) == ["x"]) assume ((keys in {.x = 1}) == ["x"])

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains compile-time actions that define basic control flow structures This file contains compile-time actions that define basic control flow structures
like "if" statements and loops. like "if" statements and loops.
@ -16,7 +16,7 @@ test:
# Conditionals # Conditionals
test: test:
if (no): if (no):
barf "conditional fail" fail "conditional fail"
(if $condition $if_body) compiles to (" (if $condition $if_body) compiles to ("
if \($condition as lua expr) then if \($condition as lua expr) then
@ -26,7 +26,7 @@ test:
test: test:
unless (yes): unless (yes):
barf "conditional fail" fail "conditional fail"
(unless $condition $unless_body) parses as (if (not $condition) $unless_body) (unless $condition $unless_body) parses as (if (not $condition) $unless_body)
[ [
@ -134,7 +134,7 @@ test:
$x += 1 $x += 1
if (yes): if (yes):
do next do next
barf "Failed to 'do next'" fail "Failed to 'do next'"
assume ($x == 20) assume ($x == 20)
(repeat while $condition $body) compiles to: (repeat while $condition $body) compiles to:
@ -311,16 +311,16 @@ test:
test: test:
when: when:
(1 == 2) (100 < 0): (1 == 2) (100 < 0):
barf "bad conditional" fail "bad conditional"
(1 == 0) (1 == 1) $not_a_variable.x: do nothing (1 == 0) (1 == 1) $not_a_variable.x: do nothing
(1 == 1): (1 == 1):
barf "bad conditional" fail "bad conditional"
(1 == 2): (1 == 2):
barf "bad conditional" fail "bad conditional"
else: else:
barf "bad conditional" fail "bad conditional"
# Multi-branch conditional (if..elseif..else) # Multi-branch conditional (if..elseif..else)
(when $body) compiles to: (when $body) compiles to:
@ -374,16 +374,16 @@ test:
test: test:
if 5 is: if 5 is:
1 2 3: 1 2 3:
barf "bad switch statement" fail "bad switch statement"
4 5: 4 5:
do nothing do nothing
5 6: 5 6:
barf "bad switch statement" fail "bad switch statement"
else: else:
barf "bad switch statement" fail "bad switch statement"
# Switch statement # Switch statement
[if $branch_value is $body, when $branch_value is $body] all compile to: [if $branch_value is $body, when $branch_value is $body] all compile to:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines the code that creates and manipulates coroutines This file defines the code that creates and manipulates coroutines
@ -18,7 +18,10 @@ test:
$nums = [] $nums = []
for $ in coroutine $co: for $ in coroutine $co:
$nums, add $ $nums, add $
assume ($nums == [4, 5, 6, 6, 6]) or barf "Coroutine iteration failed"
unless ($nums == [4, 5, 6, 6, 6]):
fail "Coroutine iteration failed"
$d = {.x = 0} $d = {.x = 0}
$co2 = $co2 =
coroutine: coroutine:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains basic error reporting code This file contains basic error reporting code
@ -7,9 +7,7 @@ use "core/operators.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(fail $msg) compiles to (fail $msg) compiles to "error(\(($msg as lua expr) if $msg else "nil"), 0);"
"error(\(($msg as lua expr) if $msg else "nil"), 0);"
(assume $condition) compiles to: (assume $condition) compiles to:
lua> (" lua> ("
local \$assumption = 'Assumption failed: '..tostring((\$condition):get_source_code()) local \$assumption = 'Assumption failed: '..tostring((\$condition):get_source_code())
@ -41,26 +39,34 @@ use "core/operators.nom"
test: test:
try: fail try: fail
$worked = (no) $worked = (no)
try: fail try:
..if it fails: fail "xx"
..if it fails with $failure:
$worked = (yes) $worked = (yes)
..if it succeeds: ..if it succeeds:
fail "'try' incorrectly ran success case." fail "'try' incorrectly ran success case."
assume $failure == "xx"
unless $worked: unless $worked:
fail "'try' failed to recover from failure" fail "'try' failed to recover from failure"
# Try/except # Try/except
[ [
try $action if it succeeds $success if it fails $fallback try $action if it succeeds $success if it fails with $msg $fallback
try $action if it fails $fallback if it succeeds $success try $action if it fails with $msg $fallback if it succeeds $success
] all compile to: ] all compile to:
$success_lua = ($success as lua) $success_lua = ($success as lua)
if ((#"\$success_lua") > 0): $success_lua, add "\n" if ((#"\$success_lua") > 0):
$success_lua, add "\n"
$success_lua, prepend "-- Success:\n" $success_lua, prepend "-- Success:\n"
$success_lua, add "if not _fell_through then return table.unpack(_result, 2) end" $success_lua,
add "if not _fell_through then return table.unpack(_result, 2) end"
$fallback_lua = ($fallback as lua) $fallback_lua = ($fallback as lua)
if ((#"\$fallback_lua") > 0): if ((#"\$fallback_lua") > 0):
$fallback_lua, prepend "\nlocal function failure() return _result[2] end\n" $msg_lua = ($msg as lua expr)
if ((#"\$msg_lua") > 0):
$fallback_lua, prepend "\n\$msg_lua = _result[2]\n"
if ($msg_lua, text, is lua id):
$fallback_lua, add free vars [($msg_lua, text)]
$fallback_lua, prepend "-- Failure:" $fallback_lua, prepend "-- Failure:"
return return
Lua (" Lua ("
@ -83,14 +89,20 @@ test:
(try $action) parses as (try $action) parses as
try $action if it succeeds (do nothing) if it fails (do nothing) try $action if it succeeds (do nothing) if it fails (do nothing)
(try $action if it fails $msg $fallback) parses as (try $action if it fails $fallback) parses as
try $action if it succeeds (do nothing) if it fails $msg $fallback try $action if it succeeds (do nothing) if it fails $fallback
(try $action if it fails with $msg $fallback) parses as
try $action if it succeeds (do nothing) if it fails with $msg $fallback
(try $action if it succeeds $success) parses as (try $action if it succeeds $success) parses as
try $action if it succeeds $success if it fails (do nothing) try $action if it succeeds $success if it fails (do nothing)
(try $action if it fails $fallback if it succeeds $success) parses as (try $action if it fails $fallback if it succeeds $success) parses as
try $action if it succeeds $success if it fails $fallback try $action if it fails with (=lua "") $fallback if it succeeds $success
(try $action if it succeeds $success if it fails $fallback) parses as
try $action if it succeeds $success if it fails with (=lua "") $fallback
test: test:
$success = (no) $success = (no)
@ -117,7 +129,7 @@ test:
end end
") ")
~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(barf $) parses as (fail $) (barf $) parses as (fail $)
(assume $1 or barf $2) parses as (unless $1: fail $2) (assume $1 or barf $2) parses as (unless $1: fail $2)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
A simple UUID function based on RFC 4122: http://www.ietf.org/rfc/rfc4122.txt A simple UUID function based on RFC 4122: http://www.ietf.org/rfc/rfc4122.txt

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains basic input/output code This file contains basic input/output code

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines some common math literals and functions This file defines some common math literals and functions
@ -12,10 +12,11 @@ use "core/collections.nom"
# Literals: # Literals:
test: test:
assume (all of [inf, NaN, pi, tau, golden ratio, e]) or barf unless (all of [inf, NaN, pi, tau, golden ratio, e]):
"math constants failed" fail "math constants failed"
$nan = (NaN) $nan = (NaN)
assume ($nan != $nan) or barf "NaN failed" unless ($nan != $nan):
fail "NaN failed"
[infinity, inf] all compile to "math.huge" [infinity, inf] all compile to "math.huge"
[not a number, NaN, nan] all compile to "(0/0)" [not a number, NaN, nan] all compile to "(0/0)"
[pi, Pi, PI] all compile to "math.pi" [pi, Pi, PI] all compile to "math.pi"
@ -26,19 +27,18 @@ test:
# Functions: # Functions:
test: test:
assume (("5" as a number) == 5) assume (("5" as a number) == 5)
(($ as a number)'s meaning) = ((tonumber $)'s meaning) external $($ as a number) = $(tonumber $)
(($ as number)'s meaning) = ((tonumber $)'s meaning) external $($ as number) = $(tonumber $)
test: test:
assume unless
all of [ all of [
abs 5, | 5 |, sqrt 5, √ 5, sine 5, cosine 5, tangent 5, arc sine 5, arc cosine 5 abs 5, | 5 |, sqrt 5, √ 5, sine 5, cosine 5, tangent 5, arc sine 5, arc cosine 5
arc tangent 5, arc tangent 5 / 10, hyperbolic sine 5, hyperbolic cosine 5 arc tangent 5, arc tangent 5 / 10, hyperbolic sine 5, hyperbolic cosine 5
hyperbolic tangent 5, e^ 5, ln 5, log base 2 of 5, floor 5, ceiling 5, round 5 hyperbolic tangent 5, e^ 5, ln 5, log base 2 of 5, floor 5, ceiling 5, round 5
] ]
..or barf "math functions failed" ..:
fail "math functions failed"
[absolute value $, | $ |, abs $] all compile to "math.abs(\($ as lua expr))" [absolute value $, | $ |, abs $] all compile to "math.abs(\($ as lua expr))"
[square root $, square root of $, √ $, sqrt $] all compile to [square root $, square root of $, √ $, sqrt $] all compile to
"math.sqrt(\($ as lua expr))" "math.sqrt(\($ as lua expr))"
[sine $, sin $] all compile to "math.sin(\($ as lua expr))" [sine $, sin $] all compile to "math.sin(\($ as lua expr))"
@ -60,8 +60,8 @@ test:
[ceiling $, ceil $] all compile to "math.ceil(\($ as lua expr))" [ceiling $, ceil $] all compile to "math.ceil(\($ as lua expr))"
[round $, $ rounded] all compile to "math.floor(\($ as lua expr) + .5)" [round $, $ rounded] all compile to "math.floor(\($ as lua expr) + .5)"
test: test:
assume ((463 to the nearest 100) == 500) or barf "rounding failed" unless ((463 to the nearest 100) == 500): fail "rounding failed"
assume ((2.6 to the nearest 0.25) == 2.5) or barf "rounding failed" unless ((2.6 to the nearest 0.25) == 2.5): fail "rounding failed"
externally ($n to the nearest $rounder) means externally ($n to the nearest $rounder) means
=lua "(\$rounder)*math.floor((\$n / \$rounder) + .5)" =lua "(\$rounder)*math.floor((\$n / \$rounder) + .5)"

View File

@ -1,9 +1,9 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This File contains actions for making actions and compile-time actions and some helper This File contains actions for making actions and compile-time actions and some helper
functions to make that easier. functions to make that easier.
lua> "NOMSU_CORE_VERSION = 13" lua> "NOMSU_CORE_VERSION = 14"
lua> "NOMSU_LIB_VERSION = 8" lua> "NOMSU_LIB_VERSION = 8"
lua> (" lua> ("
do do
@ -62,22 +62,27 @@ test:
(five) compiles to "5" (five) compiles to "5"
test: test:
assume ((five) == 5) or barf "Compile to expression failed." unless ((five) == 5):
fail "Compile to expression failed."
(loc x) compiles to "local x = 99;" (loc x) compiles to "local x = 99;"
test: test:
lua> "do" lua> "do"
loc x loc x
assume ($x is 99) or barf "Compile to statements with locals failed." unless ($x is 99):
fail "Compile to statements with locals failed."
lua> "end" lua> "end"
assume ($x is (nil)) or barf "Failed to properly localize a variable." unless ($x is (nil)):
fail "Failed to properly localize a variable."
(asdf) compiles to: (asdf) compiles to:
$tmp = "" $tmp = ""
return (Lua $tmp) return (Lua $tmp)
test: test:
asdf asdf
assume ($tmp is (nil)) or barf "compile to is leaking variables" unless ($tmp is (nil)):
fail "compile to is leaking variables"
lua> (" lua> ("
compile.action["1 compiles to"] = function(compile, \$action, \$body) compile.action["1 compiles to"] = function(compile, \$action, \$body)
@ -121,12 +126,17 @@ lua> ("
test: test:
(foo $x) means "outer" (foo $x) means "outer"
with [(foo $)'s meaning]: with [$(foo $)]:
(foo $x) means: (foo $x) means:
$y = ($x + 1) $y = ($x + 1)
return $y return $y
assume ((foo 10) == 11) or barf "Action didn't work."
assume ($y is (nil)) or barf "Action leaked a local into globals." unless ((foo 10) == 11):
fail "Action didn't work."
unless ($y is (nil)):
fail "Action leaked a local into globals."
(baz $) parses as (foo $) (baz $) parses as (foo $)
assume ((foo 1) == "outer") assume ((foo 1) == "outer")
@ -195,10 +205,6 @@ test:
return lua return lua
") ")
test:
assume (((say $)'s meaning) == (=lua "say"))
($action's meaning) compiles to (Lua ($action, get stub, as lua id))
test: test:
(swap $x and $y) parses as (swap $x and $y) parses as
do: do:
@ -209,12 +215,12 @@ test:
test: test:
[$1, $2] = [1, 2] [$1, $2] = [1, 2]
swap $1 and $2 swap $1 and $2
assume (($1 == 2) and ($2 == 1)) or barf unless (($1 == 2) and ($2 == 1)):
"'parse $ as $' failed on 'swap $ and $'" fail "'parse $ as $' failed on 'swap $ and $'"
[$tmp, $tmp2] = [1, 2] [$tmp, $tmp2] = [1, 2]
swap $tmp and $tmp2 swap $tmp and $tmp2
assume (($tmp == 2) and ($tmp2 == 1)) or barf unless (($tmp == 2) and ($tmp2 == 1)):
"'parse $ as $' variable mangling failed." fail "'parse $ as $' variable mangling failed."
($actions all parse as $body) compiles to: ($actions all parse as $body) compiles to:
lua> (" lua> ("
@ -227,10 +233,10 @@ test:
end end
local function make_tree(t) local function make_tree(t)
if SyntaxTree:is_instance(t) and t.type == "Var" then if SyntaxTree:is_instance(t) and t.type == "Var" then
if replacements[t[1]] then if replacements[t:as_var()] then
return replacements[t[1]] return replacements[t:as_var()]
else else
return "SyntaxTree{mangle("..t[1]:as_lua().."), type="..t.type:as_lua()..", \ return "SyntaxTree{mangle("..t:as_var():as_lua().."), type="..t.type:as_lua()..", \
..source="..tostring(t.source):as_lua().."}" ..source="..tostring(t.source):as_lua().."}"
end end
elseif SyntaxTree:is_instance(t) then elseif SyntaxTree:is_instance(t) then
@ -265,7 +271,6 @@ test:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[$action parses as $body] all parse as ([$action] all parse as $body) [$action parses as $body] all parse as ([$action] all parse as $body)
externally ($tree as lua expr) means: externally ($tree as lua expr) means:
lua> (" lua> ("
local tree_lua = compile(\$tree) local tree_lua = compile(\$tree)
@ -320,7 +325,7 @@ externally ($tree with vars $replacements) means
=lua (" =lua ("
\$tree:map(function(\$t) \$tree:map(function(\$t)
if \$t.type == "Var" then if \$t.type == "Var" then
return \$replacements[\$t[1]] return \$replacements[\$t:as_var()]
end end
end) end)
") ")
@ -328,7 +333,7 @@ externally ($tree with vars $replacements) means
(tree $tree with vars $replacements) compiles to (" (tree $tree with vars $replacements) compiles to ("
\(=lua "(\$tree):as_lua()"):map(function(t) \(=lua "(\$tree):as_lua()"):map(function(t)
if t.type == "Var" then if t.type == "Var" then
return \($replacements as lua expr)[t[1]] return \($replacements as lua expr)[t:as_var()]
end end
end) end)
") ")
@ -344,7 +349,7 @@ externally ($tree with vars $replacements) means
externally (match $tree with $patt) means: externally (match $tree with $patt) means:
lua> (" lua> ("
if \$patt.type == "Var" then return Dict{[\$patt[1]]=\$tree} end if \$patt.type == "Var" then return Dict{[\$patt:as_var()]=\$tree} end
if \$patt.type == "Action" and \$patt:get_stub() ~= \$tree:get_stub() then return nil end if \$patt.type == "Action" and \$patt:get_stub() ~= \$tree:get_stub() then return nil end
if #\$patt ~= #\$tree then return nil end if #\$patt ~= #\$tree then return nil end
local matches = Dict{} local matches = Dict{}

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains definitions of operators like "+" and "and". This file contains definitions of operators like "+" and "and".
@ -24,12 +24,15 @@ test:
$x = 10 $x = 10
assume ($x == 10) assume ($x == 10)
[$x, $y] = [10, 20] [$x, $y] = [10, 20]
assume (($x == 10) and ($y == 20)) or barf "mutli-assignment failed." unless (($x == 10) and ($y == 20)):
fail "mutli-assignment failed."
[$x, $y] = [$y, $x] [$x, $y] = [$y, $x]
assume (($y == 10) and ($x == 20)) or barf "swapping vars failed." unless (($y == 10) and ($x == 20)):
fail "swapping vars failed."
$vals = [4, 5] $vals = [4, 5]
[$x, $y] = (unpack $vals) [$x, $y] = (unpack $vals)
assume (($x == 4) and ($y == 5)) or barf "unpacking failed" unless (($x == 4) and ($y == 5)):
fail "unpacking failed"
# Variable assignment operator # Variable assignment operator
($var = $value) compiles to: ($var = $value) compiles to:
@ -80,7 +83,7 @@ test:
external $foozle = "inner" external $foozle = "inner"
$y = "inner" $y = "inner"
set global x local y set global x local y
assume (($foozle == "inner") and ($y == "outer")) or barf "external failed." unless (($foozle == "inner") and ($y == "outer")): fail "external failed."
(external $var = $value) compiles to "\($var as lua) = \($value as lua)" (external $var = $value) compiles to "\($var as lua) = \($value as lua)"
test: test:
[$foozle, $y] = ["outer", "outer"] [$foozle, $y] = ["outer", "outer"]
@ -89,8 +92,8 @@ test:
$foozle = "inner" $foozle = "inner"
$y = "inner" $y = "inner"
set global x local y set global x local y
assume (($foozle == "inner") and ($y == "outer")) or barf unless (($foozle == "inner") and ($y == "outer")):
"'with external' failed." fail "'with external' failed."
(with external $externs $body) compiles to: (with external $externs $body) compiles to:
$body_lua = ($body as lua) $body_lua = ($body as lua)
@ -104,10 +107,17 @@ test:
with [$z, $x = 999]: with [$z, $x = 999]:
assume $z == (nil) assume $z == (nil)
$z = 999 $z = 999
assume ($z == 999) or barf "'with' failed." unless ($z == 999):
assume ($x == 999) or barf "'with' assignment failed." fail "'with' failed."
assume ($x == 1) or barf "'with' scoping failed"
assume ($z == (nil)) or barf "'with' scoping failed" unless ($x == 999):
fail "'with' assignment failed."
unless ($x == 1):
fail "'with' scoping failed"
unless ($z == (nil)):
fail "'with' scoping failed"
(with $assignments $body) compiles to: (with $assignments $body) compiles to:
lua> (" lua> ("
@ -132,7 +142,8 @@ test:
# Math Operators # Math Operators
test: test:
assume ((5 wrapped around 2) == 1) or barf "mod not working" unless ((5 wrapped around 2) == 1):
fail "mod not working"
[$x wrapped around $y, $x mod $y] all compile to [$x wrapped around $y, $x mod $y] all compile to
"((\($x as lua expr)) % (\($y as lua expr)))" "((\($x as lua expr)) % (\($y as lua expr)))"
@ -144,9 +155,12 @@ test:
(one) means: (one) means:
external $calls = ($calls + 1) external $calls = ($calls + 1)
return 1 return 1
assume (0 <= (one) <= 2) or barf "Three-way chained comparison failed."
assume ($calls == 1) or barf unless (0 <= (one) <= 2):
"Three-way comparison evaluated middle value multiple times" fail "Three-way chained comparison failed."
unless ($calls == 1):
fail "Three-way comparison evaluated middle value multiple times"
($x < $y < $z) parses as ((($a $b $c) -> (($a < $b) and ($b < $c))) $x $y $z) ($x < $y < $z) parses as ((($a $b $c) -> (($a < $b) and ($b < $c))) $x $y $z)
($x <= $y < $z) parses as ((($a $b $c) -> (($a <= $b) and ($b < $c))) $x $y $z) ($x <= $y < $z) parses as ((($a $b $c) -> (($a <= $b) and ($b < $c))) $x $y $z)
($x < $y <= $z) parses as ((($a $b $c) -> (($a < $b) and ($b <= $c))) $x $y $z) ($x < $y <= $z) parses as ((($a $b $c) -> (($a < $b) and ($b <= $c))) $x $y $z)
@ -159,7 +173,7 @@ test:
# TODO: optimize for common case where x,y,z are all either variables or number literals # TODO: optimize for common case where x,y,z are all either variables or number literals
# Boolean Operators # Boolean Operators
test: test:
(barfer) means (barf "short circuiting failed") (barfer) means (fail "short circuiting failed")
assume (((no) and (barfer)) == (no)) assume (((no) and (barfer)) == (no))
assume ((no) or (yes)) assume ((no) or (yes))
assume ((yes) or (barfer)) assume ((yes) or (barfer))
@ -228,11 +242,14 @@ test:
test: test:
$x = 1 $x = 1
$x += 1 $x += 1
assume ($x == 2) or barf "+= failed" unless ($x == 2):
fail "+= failed"
$x *= 2 $x *= 2
assume ($x == 4) or barf "*= failed" unless ($x == 4):
fail "*= failed"
wrap $x around 3 wrap $x around 3
assume ($x == 1) or barf "wrap around failed" unless ($x == 1):
fail "wrap around failed"
($var += $) parses as ($var = (($var or 0) + $)) ($var += $) parses as ($var = (($var or 0) + $))
($var -= $) parses as ($var = (($var or 0) - $)) ($var -= $) parses as ($var = (($var or 0) - $))
($var *= $) parses as ($var = (($var or 1) * $)) ($var *= $) parses as ($var = (($var or 1) * $))

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains some definitions of text escape sequences, including ANSI console This file contains some definitions of text escape sequences, including ANSI console
color codes. color codes.

View File

@ -1,5 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# How do I... # How do I...
# Write a comment? Put a # and go till the end of the line # Write a comment? Put a # and go till the end of the line
# How do I write a multi-line comment? # How do I write a multi-line comment?
@ -250,6 +249,7 @@ say (2 + 3)
# If you need to keep going after an indented region, you can start the next line with ".." # If you need to keep going after an indented region, you can start the next line with ".."
say both "Very very very very long first argument that needs its own line" say both "Very very very very long first argument that needs its own line"
..and also "short second arg" ..and also "short second arg"
(my favorite number) means (21 + 2) (my favorite number) means (21 + 2)
# This can be nested: # This can be nested:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines actions for encoding/decoding base 64, as specified in: This file defines actions for encoding/decoding base 64, as specified in:
https://tools.ietf.org/html/rfc4648 https://tools.ietf.org/html/rfc4648

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines actions for ANSI console color escape codes. This file defines actions for ANSI console color escape codes.

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines some actions for hashing files and looking up files by hash. This file defines some actions for hashing files and looking up files by hash.
@ -10,11 +10,12 @@ use "lib/base64.nom"
lua> "local \$use_sha1, \$hashlib = pcall(require, 'openssl.digest')" lua> "local \$use_sha1, \$hashlib = pcall(require, 'openssl.digest')"
test: test:
assume (hash "hello world") == (hash "hello world") assume (hash "hello world") == (hash "hello world")
assume ((hash "hello world") != (hash "goodbye")) or barf (" if ((hash "hello world") == (hash "goodbye")):
Hash collision: fail ("
(hash "hello world") = \(hash "hello world") Hash collision:
(hash "goodbye") = \(hash "goodbye") (hash "hello world") = \(hash "hello world")
") (hash "goodbye") = \(hash "goodbye")
")
assume assume
( (
@ -24,8 +25,8 @@ test:
") ")
) != "inf" ) != "inf"
assume ((hash "\000") != (hash "\000\000\000\000\000")) or barf if ((hash "\000") == (hash "\000\000\000\000\000")):
"Incorrect hashing of null strings" fail "Incorrect hashing of null strings"
if $use_sha1: if $use_sha1:
assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=") assume ((hash "hello world") == "Kq5sNclPz7QV2+lfQIuc6R7oRu0=")

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file contains the implementation of an Object-Oriented programming system. This file contains the implementation of an Object-Oriented programming system.
@ -37,7 +37,9 @@ test:
assume ($d.genus == "Canus") assume ($d.genus == "Canus")
assume ($d.barks == 3) assume ($d.barks == 3)
$d2 = (Dog {}) $d2 = (Dog {})
assume ($d2.barks == 0) or barf "Default initializer failed" unless ($d2.barks == 0):
fail "Default initializer failed"
with [$d = (Dog {.barks = 1})]: with [$d = (Dog {.barks = 1})]:
assume (($d, bark) == "Bark!") assume (($d, bark) == "Bark!")
@ -50,8 +52,12 @@ test:
$corg = (Corgi {}) $corg = (Corgi {})
assume ($corg.barks == 0) assume ($corg.barks == 0)
with [$d = (Corgi {.barks = 1})]: with [$d = (Corgi {.barks = 1})]:
assume (($d, sploot) == "splooted") or barf "subclass method failed" unless (($d, sploot) == "splooted"):
assume (($d, bark) == "Yip!") or barf "inheritance failed" fail "subclass method failed"
unless (($d, bark) == "Yip!"):
fail "inheritance failed"
assume (($d, woof) == "Yip!") assume (($d, woof) == "Yip!")
with [$d = (Dog {.barks = 2})]: with [$d = (Dog {.barks = 2})]:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines some actions that interact with the operating system and filesystem. This file defines some actions that interact with the operating system and filesystem.
@ -33,7 +33,9 @@ externally [
write to file $filename $text, to file $filename write $text write to file $filename $text, to file $filename write $text
write $text to file $filename write $text to file $filename
] all mean: ] all mean:
assume ($filename != "stdin") or barf "Cannot write to stdin" unless ($filename != "stdin"):
fail "Cannot write to stdin"
lua> (" lua> ("
local file = io.open(\$filename, 'w') local file = io.open(\$filename, 'w')
file:write(\$text) file:write(\$text)
@ -45,10 +47,9 @@ externally (source lines of $tree) means:
$file = (read file $source.filename) $file = (read file $source.filename)
return return
[ [
: : for $ in ($file, line number at $source.start) to
for $ in ($file, line number at $source.start) to $file, line number at $source.stop
$file, line number at $source.stop ..: add ($file, line $)
..: add ($file, line $)
], joined with "\n" ], joined with "\n"
externally (spoof file $text) means ($Files.spoof $text) externally (spoof file $text) means ($Files.spoof $text)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
A library for simple object oriented programming. A library for simple object oriented programming.
@ -30,7 +30,9 @@ test:
assume ($d.genus == "Canus") assume ($d.genus == "Canus")
assume ($d.barks == 3) assume ($d.barks == 3)
$d2 = (a Dog) $d2 = (a Dog)
assume ($d2.barks == 0) or barf "Default initializer failed" unless ($d2.barks == 0):
fail "Default initializer failed"
with [$d = (a Dog with {.barks = 1})]: with [$d = (a Dog with {.barks = 1})]:
assume (($d, bark) == "Bark!") assume (($d, bark) == "Bark!")
@ -47,8 +49,12 @@ test:
assume ($corg.barks == 0) assume ($corg.barks == 0)
assume "\$corg" == "Dogloaf {barks: 0}" assume "\$corg" == "Dogloaf {barks: 0}"
with [$d = (a Corgi with {.barks = 1})]: with [$d = (a Corgi with {.barks = 1})]:
assume (($d, sploot) == "sploooot") or barf "subclass method failed" unless (($d, sploot) == "sploooot"):
assume (($d, bark) == "Yip!") or barf "inheritance failed" fail "subclass method failed"
unless (($d, bark) == "Yip!"):
fail "inheritance failed"
assume (($d, woof) == "Yip!") assume (($d, woof) == "Yip!")
with [$d = (a Dog with {.barks = 2})]: with [$d = (a Dog with {.barks = 2})]:
@ -87,9 +93,8 @@ $METAMETHOD_MAP = {
.size = "__len", .iterate = "__ipairs", ."iterate all" = "__pairs" .size = "__len", .iterate = "__ipairs", ."iterate all" = "__pairs"
} }
(($ as text like a dict)'s meaning) = ({}'s metatable).__tostring $($ as text like a dict) = ({}'s metatable).__tostring
externally (a class named $classname with $members ((initialize $it)'s meaning)) externally (a class named $classname with $members $(initialize $)) means:
..means:
$class = {.__type = $classname} $class = {.__type = $classname}
$class.__index = $class $class.__index = $class
$class.class = $class $class.class = $class
@ -103,7 +108,7 @@ externally (a class named $classname with $members ((initialize $it)'s meaning))
if $members.$key: if $members.$key:
rawset $its $key $value rawset $its $key $value
..else: ..else:
barf "Cannot set \$key, it's not one of the allowed member fields." fail "Cannot set \$key, it's not one of the allowed member fields."
set $class's metatable to { set $class's metatable to {
.__tostring = ($class -> $class.__type) .__tostring = ($class -> $class.__type)
@ -116,7 +121,7 @@ externally (a class named $classname with $members ((initialize $it)'s meaning))
return $initial_values return $initial_values
} }
if ((initialize)'s meaning): if $(initialize $):
initialize $class initialize $class
for $stub = $metamethod in $METAMETHOD_MAP: for $stub = $metamethod in $METAMETHOD_MAP:
if $class.($stub, as lua id): if $class.($stub, as lua id):

View File

@ -99,14 +99,14 @@ indented_index_chain <-
-- Actions need 1 argument and either another argument or a word. -- Actions need 1 argument and either another argument or a word.
inline_action (Action) <- inline_action (Action) <-
!section_division !section_division
( word (ws* arg)* ( word (ws* inline_arg)*
/ inline_expression ((ws* arg)+ / "(" ws* ")")) / inline_expression ((ws* inline_arg)+ / "(" ws* ")"))
inline_arg <- inline_expression / word inline_arg <- inline_expression / word
action (Action) <- action (Action) <-
!section_division !section_division
( word ((linesplit / ws*) arg)* ( word ((linesplit / ws*) arg)*
/ arg (((linesplit / ws*) arg)+ / "(" ws* ")")) / !statement_block arg (((linesplit / ws*) arg)+ / "(" ws* ")"))
arg <- expression / indented_naked_expression / word arg <- expression / indented_naked_expression / word
linesplit <- eol nl_nodent ".." ws* linesplit <- eol nl_nodent ".." ws*

View File

@ -473,12 +473,7 @@ local compile = setmetatable({
elseif "Number" == _exp_0 then elseif "Number" == _exp_0 then
return LuaCode:from(tree.source, tostring(tree[1])) return LuaCode:from(tree.source, tostring(tree[1]))
elseif "Var" == _exp_0 then elseif "Var" == _exp_0 then
if type(tree[1]) == 'string' then return LuaCode:from(tree.source, tree:as_var():as_lua_id())
return LuaCode:from(tree.source, (concat(tree, " ")):as_lua_id())
else
assert(tree[1].type == 'Action')
return LuaCode:from(tree.source, tree[1]:get_stub():as_lua_id())
end
elseif "FileChunks" == _exp_0 then elseif "FileChunks" == _exp_0 then
return error("Can't convert FileChunks to a single block of lua, since each chunk's " .. "compilation depends on the earlier chunks") return error("Can't convert FileChunks to a single block of lua, since each chunk's " .. "compilation depends on the earlier chunks")
elseif "Comment" == _exp_0 then elseif "Comment" == _exp_0 then

View File

@ -370,11 +370,7 @@ compile = setmetatable({
return LuaCode\from(tree.source, tostring(tree[1])) return LuaCode\from(tree.source, tostring(tree[1]))
when "Var" when "Var"
if type(tree[1]) == 'string' return LuaCode\from(tree.source, tree\as_var!\as_lua_id!)
return LuaCode\from(tree.source, (concat(tree, " "))\as_lua_id!)
else
assert(tree[1].type == 'Action')
return LuaCode\from(tree.source, tree[1]\get_stub!\as_lua_id!)
when "FileChunks" when "FileChunks"
error("Can't convert FileChunks to a single block of lua, since each chunk's ".. error("Can't convert FileChunks to a single block of lua, since each chunk's "..

View File

@ -506,7 +506,10 @@ tree_to_nomsu = function(tree)
else else
nomsu:add("\\") nomsu:add("\\")
local interp_nomsu = recurse(bit) local interp_nomsu = recurse(bit)
if not (interp_nomsu:is_multiline()) then if interp_nomsu:is_multiline() then
local curr_indent = nomsu:text():match("\n( *)[^\n]*$") or nomsu:text():match("^( *)")
interp_nomsu = NomsuCode((interp_nomsu:text():gsub("\n", "\n" .. curr_indent)))
else
local space = max_line - nomsu:trailing_line_len() local space = max_line - nomsu:trailing_line_len()
if bit.type == "Var" then if bit.type == "Var" then
local next_str = tree[i + 1] local next_str = tree[i + 1]
@ -524,6 +527,8 @@ tree_to_nomsu = function(tree)
interp_nomsu2 = tree_to_nomsu(bit) interp_nomsu2 = tree_to_nomsu(bit)
end end
if #interp_nomsu2:text():lines() > 3 or #interp_nomsu2:text() >= MAX_LINE * GOLDEN_RATIO then if #interp_nomsu2:text():lines() > 3 or #interp_nomsu2:text() >= MAX_LINE * GOLDEN_RATIO then
local curr_indent = nomsu:text():match("\n( *)[^\n]*$") or nomsu:text():match("^( *)")
interp_nomsu2 = NomsuCode((interp_nomsu2:text():gsub("\n", "\n" .. curr_indent)))
interp_nomsu = interp_nomsu2 interp_nomsu = interp_nomsu2
else else
nomsu:add("\n..\\") nomsu:add("\n..\\")

View File

@ -385,7 +385,10 @@ tree_to_nomsu = (tree)->
else else
nomsu\add "\\" nomsu\add "\\"
interp_nomsu = recurse(bit) interp_nomsu = recurse(bit)
unless interp_nomsu\is_multiline! if interp_nomsu\is_multiline!
curr_indent = nomsu\text!\match("\n( *)[^\n]*$") or nomsu\text!\match("^( *)")
interp_nomsu = NomsuCode((interp_nomsu\text!\gsub("\n", "\n"..curr_indent)))
else
space = max_line - nomsu\trailing_line_len! space = max_line - nomsu\trailing_line_len!
if bit.type == "Var" if bit.type == "Var"
next_str = tree[i+1] next_str = tree[i+1]
@ -400,6 +403,8 @@ tree_to_nomsu = (tree)->
tree_to_nomsu(bit) tree_to_nomsu(bit)
if #interp_nomsu2\text!\lines! > 3 or #interp_nomsu2\text! >= MAX_LINE*GOLDEN_RATIO if #interp_nomsu2\text!\lines! > 3 or #interp_nomsu2\text! >= MAX_LINE*GOLDEN_RATIO
curr_indent = nomsu\text!\match("\n( *)[^\n]*$") or nomsu\text!\match("^( *)")
interp_nomsu2 = NomsuCode((interp_nomsu2\text!\gsub("\n", "\n"..curr_indent)))
interp_nomsu = interp_nomsu2 interp_nomsu = interp_nomsu2
else else
nomsu\add "\n..\\" nomsu\add "\n..\\"

View File

@ -185,6 +185,14 @@ do
stub_bits[#stub_bits] = nil stub_bits[#stub_bits] = nil
end end
return concat(stub_bits, " ") return concat(stub_bits, " ")
end,
as_var = function(self)
assert(self.type == "Var")
if type(self[1]) == 'string' then
return self[1]
else
return self[1]:get_stub()
end
end end
} }
_base_0.__index = _base_0 _base_0.__index = _base_0

View File

@ -99,6 +99,13 @@ class SyntaxTree
stub_bits[#stub_bits] = nil stub_bits[#stub_bits] = nil
return concat stub_bits, " " return concat stub_bits, " "
as_var: =>
assert(@type == "Var")
if type(@[1]) == 'string'
return @[1]
else
return @[1]\get_stub!
@is_instance: (t)=> @is_instance: (t)=>
type(t) == 'table' and getmetatable(t) == @__base type(t) == 'table' and getmetatable(t) == @__base

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
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 that will match any subtree, and "**" is a wildcard that will match any
@ -24,7 +24,7 @@ use "lib/consolecolor.nom"
$wildcard = ($(COMMAND LINE ARGS).wildcard or "%*") $wildcard = ($(COMMAND LINE ARGS).wildcard or "%*")
$pattern = $(COMMAND LINE ARGS).extras.1 $pattern = $(COMMAND LINE ARGS).extras.1
if (any of [not $pattern, $pattern == "*", $pattern == "**"]): if (any of [not $pattern, $pattern == "*", $pattern == "**"]):
barf (" fail ("
Usage: nomsu -t find [-l] [--wildcard=<wildcard>] <pattern>, where <pattern> is valid Nomsu code 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" -> "$multi_wildcard")
@ -57,11 +57,11 @@ if ((#$filenames) == 0):
for $filename in $filenames: for $filename in $filenames:
$file = (read file $filename) $file = (read file $filename)
unless $file: unless $file:
barf "File does not exist: \$filename" fail "File does not exist: \$filename"
$code = (NomsuCode from ($Source $filename 1 (size of $file)) $file) $code = (NomsuCode from ($Source $filename 1 (size of $file)) $file)
try: try:
$tree = ($code parsed) $tree = ($code parsed)
..and if it barfs $msg: ..if it fails $msg:
say say
red (" red ("
\$filename failed to parse: \$filename failed to parse:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
Auto-format Nomsu code. Usage: Auto-format Nomsu code. Usage:
nomsu -t format [-i] file1 file2... nomsu -t format [-i] file1 file2...
@ -21,12 +21,12 @@ if ((#$filenames) == 0):
for $filename in $filenames: for $filename in $filenames:
$file = (read file $filename) $file = (read file $filename)
unless $file: unless $file:
barf "File does not exist: \$filename" fail "File does not exist: \$filename"
$leading_indent = ($file, matching "\n*([ ]*)") $leading_indent = ($file, matching "\n*([ ]*)")
$code = (NomsuCode from ($Source $filename 1 (size of $file)) $file) $code = (NomsuCode from ($Source $filename 1 (size of $file)) $file)
try: try:
$tree = ($code parsed) $tree = ($code parsed)
..and if it barfs $msg: ..if it fails $msg:
if $(COMMAND LINE ARGS).q: if $(COMMAND LINE ARGS).q:
$formatted = $file $formatted = $file
..else: ..else:

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
Tool to print out a parse tree of files in an easy-to-read format. Usage: Tool to print out a parse tree of files in an easy-to-read format. Usage:
nomsu tools/parse.nom file1 file2 directory1 ... nomsu tools/parse.nom file1 file2 directory1 ...
@ -39,7 +39,7 @@ externally (print tree $t at indent $indent) means:
for $filename in $(COMMAND LINE ARGS).extras: for $filename in $(COMMAND LINE ARGS).extras:
$file = (read file $filename) $file = (read file $filename)
unless $file: unless $file:
barf "File does not exist: \$filename" fail "File does not exist: \$filename"
$nomsu = (NomsuCode from (Source $filename 1 (size of $file)) $file) $nomsu = (NomsuCode from (Source $filename 1 (size of $file)) $file)
$tree = ($nomsu parsed) $tree = ($nomsu parsed)
print tree $tree at indent "" print tree $tree at indent ""

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This file defines a Read-Evaluate-Print-Loop (REPL) for Nomsu This file defines a Read-Evaluate-Print-Loop (REPL) for Nomsu
@ -45,7 +45,7 @@ repeat:
spoof file $buff spoof file $buff
try: try:
$tree = ($buff parsed) $tree = ($buff parsed)
..and if it barfs $err: ..if it fails $err:
say $err say $err
do next do next
@ -55,7 +55,7 @@ repeat:
for $chunk in $tree: for $chunk in $tree:
try: try:
$lua = ($chunk as lua) $lua = ($chunk as lua)
..and if it barfs $err: say $err ..if it fails $err: say $err
unless $lua: unless $lua:
do next do next
@ -66,8 +66,8 @@ repeat:
$lua, remove free vars $lua, remove free vars
try: try:
$ret = (run $lua) $ret = (run $lua)
..and if it barfs $err: say $err ..if it fails $err: say $err
..or if it succeeds: ..if it succeeds:
if (type of $ret) is: if (type of $ret) is:
"nil": "nil":
do nothing do nothing

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
This is a tool to replace syntax trees with something new. This is a tool to replace syntax trees with something new.
@ -21,7 +21,7 @@ use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if ((#$(COMMAND LINE ARGS).extras) < 2): if ((#$(COMMAND LINE ARGS).extras) < 2):
barf (" fail ("
Usage: nomsu -t replace [--literal="$v1 $v2..."] <pattern> <replacement> file1 file2... Usage: nomsu -t replace [--literal="$v1 $v2..."] <pattern> <replacement> file1 file2...
") ")
$pattern = $(COMMAND LINE ARGS).extras.1 $pattern = $(COMMAND LINE ARGS).extras.1
@ -34,7 +34,7 @@ if $(COMMAND LINE ARGS).literal:
$literal_vars.$var = (yes) $literal_vars.$var = (yes)
if (($pattern_tree.type == "Var") and (not $literal_vars.($pattern_tree.1))): if (($pattern_tree.type == "Var") and (not $literal_vars.($pattern_tree.1))):
barf "Pattern matches every part of the file." fail "Pattern matches every part of the file."
$pattern_vars = { $pattern_vars = {
: for $ in recursive $pattern_tree: : for $ in recursive $pattern_tree:
@ -77,7 +77,6 @@ $pattern_vars = {
if ((#$tree) != (#$patt)): return (nil) if ((#$tree) != (#$patt)): return (nil)
return $substitution_values return $substitution_values
$filenames = ($(COMMAND LINE ARGS).extras, from 3 to -1) $filenames = ($(COMMAND LINE ARGS).extras, from 3 to -1)
if ((#$filenames) == 0): if ((#$filenames) == 0):
say (" say ("
@ -88,14 +87,13 @@ if ((#$filenames) == 0):
for $filename in $filenames: for $filename in $filenames:
$file = (read file $filename) $file = (read file $filename)
unless $file: unless $file:
barf "File does not exist: \$filename" fail "File does not exist: \$filename"
$code = (NomsuCode from ($Source $filename 1 (size of $file)) $file) $code = (NomsuCode from ($Source $filename 1 (size of $file)) $file)
try: try:
$tree = ($code parsed) $tree = ($code parsed)
..and if it barfs $msg: ..if it fails $msg:
if $(COMMAND LINE ARGS).q: if $(COMMAND LINE ARGS).q:
unless $(COMMAND LINE ARGS).i: unless $(COMMAND LINE ARGS).i: say $code
say $code
..else: ..else:
say $msg say $msg

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
Tool to run all tests in a file (i.e. the code block inside a call to 'test $'). Usage: Tool to run all tests in a file (i.e. the code block inside a call to 'test $'). Usage:
nomsu tools/test.nom file1 file2 directory1 ... nomsu tools/test.nom file1 file2 directory1 ...

View File

@ -1,4 +1,4 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.14
# #
Tool to automatically update code from old versions of Nomsu. Usage: Tool to automatically update code from old versions of Nomsu. Usage:
nomsu tools/upgrade.nom [-i] file1 file2 directory1 ... nomsu tools/upgrade.nom [-i] file1 file2 directory1 ...
@ -18,7 +18,7 @@ $test = ($(COMMAND LINE ARGS).t or $(COMMAND LINE ARGS).test)
for $filename in $(COMMAND LINE ARGS).extras: for $filename in $(COMMAND LINE ARGS).extras:
$file = (read file $filename) $file = (read file $filename)
unless $file: unless $file:
barf "File does not exist: \$filename" fail "File does not exist: \$filename"
$leading_indent = ($file, matching "\n*([ ]*)") $leading_indent = ($file, matching "\n*([ ]*)")
$code = (NomsuCode from (Source $filename 1 (size of $file)) $file) $code = (NomsuCode from (Source $filename 1 (size of $file)) $file)
$tree = ($code parsed $start_version) $tree = ($code parsed $start_version)