diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/collections.nom | 1 | ||||
| -rw-r--r-- | core/control_flow.nom | 27 | ||||
| -rw-r--r-- | core/errors.nom | 123 | ||||
| -rw-r--r-- | core/metaprogramming.nom | 10 | ||||
| -rw-r--r-- | core/operators.nom | 1 |
5 files changed, 79 insertions, 83 deletions
diff --git a/core/collections.nom b/core/collections.nom index 69685b8..6aac861 100644 --- a/core/collections.nom +++ b/core/collections.nom @@ -37,7 +37,6 @@ test: test: $dict = {.x = 1, .y = 2, .z = 3} assume (size of $dict) == 3 - assume [: for $ in {.x = 1}: add $] == [{.key = "x", .value = 1}] assume [: for $k = $v in {.x = 1}: add {.key = $k, .value = $v}] == [{.key = "x", .value = 1}] assume ({.x = 1, .y = 1} + {.y = 10, .z = 10}) == {.x = 1, .y = 11, .z = 10} diff --git a/core/control_flow.nom b/core/control_flow.nom index 16f8537..f066767 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -5,7 +5,6 @@ use "core/metaprogramming.nom" use "core/operators.nom" -use "core/errors.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -449,32 +448,6 @@ test: ") test: - $d = {} - try: - do: - $d.x = "bad" - barf - ..then always: - $d.x = "good" - assume ($d.x == "good") - -(do $action then always $final_action) compiles to: - define mangler - return - Lua (" - do - local \(mangle "fell_through") = false - local \(mangle "ok"), \(mangle "ret") = pcall(function() - \($action as lua) - \(mangle "fell_through") = true - end) - \($final_action as lua) - if not \(mangle "ok") then error(ret, 0) end - if not \(mangle "fell_through") then return ret end - end - ") - -test: assume ((result of: return 99) == 99) # Inline thunk: diff --git a/core/errors.nom b/core/errors.nom index 6779922..0b63b5b 100644 --- a/core/errors.nom +++ b/core/errors.nom @@ -3,11 +3,12 @@ This file contains basic error reporting code use "core/metaprogramming.nom" +use "core/operators.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -(barf $msg) compiles to - "error(\(=lua "\$msg and \($msg as lua expr) or 'nil'"), 0);" +(fail $msg) compiles to + "error(\(($msg as lua expr) if $msg else "nil"), 0);" (assume $condition) compiles to: lua> (" @@ -37,62 +38,86 @@ use "core/metaprogramming.nom" end ") -(assume $condition or barf $message) compiles to (" - if not \($condition as lua expr) then - error(\($message as lua expr), 0) - end -") - test: - try (barf) and if it succeeds: - barf "try failed." + try: fail $worked = (no) - try (barf) and if it barfs: + try: fail + ..if it fails: $worked = (yes) - assume $worked or barf "try/catch failed" - $x = 1 - try: - $x = 2 - do (barf) then always: $x = 3 - ..and if it barfs: - do nothing - assume ($x == 3) or barf "do/then always failed" - + ..if it succeeds: + fail "'try' incorrectly ran success case." + + unless $worked: + fail "'try' failed to recover from failure" # Try/except [ - try $action and if it succeeds $success or if it barfs $msg $fallback - try $action and if it barfs $msg $fallback or if it succeeds $success -] all compile to (" - do - local fell_through = false - local err, erred = nil, false - local ok, ret = xpcall(function() - \($action as lua) - fell_through = true - end, function(\(=lua "\$fallback and \($msg as lua expr) or ''")) - local ok, ret = pcall(function() - \((=lua "\$fallback or \$msg") as lua) - end) - if not ok then err, erred = ret, true end - end) - if ok then - \($success as lua) - if not fell_through then - return ret + try $action if it succeeds $success if it fails $fallback + try $action if it fails $fallback if it succeeds $success +] all compile to: + $success_lua = ($success as lua) + if ((#"\$success_lua") > 0): $success_lua, add "\n" + $success_lua, prepend "-- Success:\n" + $success_lua, add "if not _fell_through then return table.unpack(_result, 2) end" + $fallback_lua = ($fallback as lua) + if ((#"\$fallback_lua") > 0): + $fallback_lua, prepend "\nlocal function failure() return _result[2] end\n" + $fallback_lua, prepend "-- Failure:" + return + Lua (" + do + local _fell_through = false + local _result = {pcall(function() + \($action as lua) + _fell_through = true + end)} + if _result[1] then + \$success_lua + else + \$fallback_lua + end end - elseif erred then - error(err, 0) - end - end -") + ") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (try $action) parses as - try $action and if it succeeds (do nothing) or if it barfs (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 succeeds (do nothing) if it fails $msg $fallback + +(try $action if it succeeds $success) parses as + 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 succeeds $success if it fails $fallback + +test: + $success = (no) + try: + do: fail + ..then always: + $success = (yes) + ..if it succeeds: + fail "'try ... then always ...' didn't propagate failure" + + unless $success: + fail "'try ... then always ...' didn't execute the 'always' code" + +(do $action then always $final_action) compiles to (" + do -- do/then always + local _fell_through = false + local _results = {pcall(function() + \($action as lua) + _fell_through = true + end)} + \($final_action as lua) + if not _results[1] then error(_results[2], 0) end + if not _fell_through then return table.unpack(_results, 2) end + end +") -(try $action and if it barfs $msg $fallback) parses as - try $action and if it succeeds (do nothing) or if it barfs $msg $fallback +~~~ -(try $action and if it succeeds $success) parses as - try $action and if it succeeds $success or if it barfs (do nothing) +(barf $) parses as (fail $) +(assume $1 or barf $2) parses as (unless $1: fail $2) diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 2e1bf6b..b4297dd 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -308,11 +308,11 @@ externally ($ is $kind syntax tree) means ($tree with $t -> $replacement) compiles to (" \($tree as lua expr):map(function(\($t as lua expr)) \( - =lua (" - \$replacement.type == 'Block' and \($replacement as lua) or 'return '..\ - ..\($replacement as lua expr) - ") - ) + =lua (" + \$replacement.type == 'Block' and \($replacement as lua) or 'return '..\ + ..\($replacement as lua expr) + ") + ) end) ") diff --git a/core/operators.nom b/core/operators.nom index 8d646e9..6c089a6 100644 --- a/core/operators.nom +++ b/core/operators.nom @@ -3,7 +3,6 @@ This file contains definitions of operators like "+" and "and". use "core/metaprogramming.nom" -use "core/errors.nom" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
