(195 lines)
1 #!/usr/bin/env nomsu -V7.0.02 ###3 This file contains basic error reporting code5 use "core/metaprogramming"6 use "core/operators"7 use "core/control_flow"9 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~11 (fail $msg) compiles to ("12 at_1_fail(\(quote (this tree).source),13 \(($msg as lua expr) if $msg else (quote "A failure was triggered here'"))14 )15 ")17 (assume $condition) compiles to:18 if ($condition.type == "IndexChain"):19 return Lua ("20 do -- Assumption:21 local _thing, _key = \($condition.1 as lua expr), \($condition.2.1 as lua expr)22 if not _thing[_key] then23 _key = type_of(_key) == 'Text' and _key:as_lua() or _1_as_text(_key)24 at_1_fail(\(quote "\($condition.source)"),25 "Assumption failed: \($condition.1 as nomsu, text) does not have a value for ".._key..".")26 end27 end28 ")30 if ($condition.type == "Action"):31 when $condition.stub is:32 "1 ==":33 return Lua ("34 do -- Assumption:35 local _a, _b = \($condition.1 as lua expr), \($condition.3 as lua expr)36 if _a ~= _b then37 _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a)38 _b = type_of(_b) == 'Text' and _b:as_lua() or _1_as_text(_b)39 at_1_fail(\(quote "\($condition.1.source)"),40 "Assumption failed: This value was ".._a.." but it was expected to be ".._b..".")41 end42 end43 ")45 "1 !=":46 return Lua ("47 do -- Assumption:48 local _a, _b = \($condition.1 as lua expr), \($condition.3 as lua expr)49 if _a == _b then50 _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a)51 at_1_fail(\(quote "\($condition.1.source)"),52 "Assumption failed: This value was ".._a.." but it wasn't expected to be.")53 end54 end55 ")57 "1 >" "1 <" "1 >=" "1 <=":58 return Lua ("59 do -- Assumption:60 local _a, _b = \($condition.1 as lua expr), \($condition.3 as lua expr)61 if not (_a \($condition.2) _b) then62 _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a)63 _b = type_of(_b) == 'Text' and _b:as_lua() or _1_as_text(_b)64 at_1_fail(\(quote "\($condition.1.source)"),65 "Assumption failed: This value was ".._a..", but it was expected to be \66 ..\($condition.2)".._b..".")67 end68 end69 ")71 "1 is":72 return Lua ("73 do -- Assumption:74 local _a, _b = \($condition.1 as lua expr), \($condition.3 as lua expr)75 if not _1_is(_a, _b) then76 _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a)77 at_1_fail(\(quote "\($condition.1.source)"),78 "Assumption failed: This value (".._a..") was expected to be ".._b..", but wasn't.")79 end80 end81 ")83 "1 isn ' t" "1 is not":84 return Lua ("85 do -- Assumption:86 local _a, _b = \($condition.1 as lua expr), \($condition.(#$condition) as lua expr)87 if _1_is(_a, _b) then88 _a = type_of(_a) == 'Text' and _a:as_lua() or _1_as_text(_a)89 at_1_fail(\(quote "\($condition.1.source)"),90 "Assumption failed: This value (".._a..") was expected to not be ".._b..", but it was.")91 end92 end93 ")95 return Lua ("96 if not \($condition as lua expr) then97 at_1_fail(\(quote "\($condition.source)"), "Assumption failed: This assumption did not hold.")98 end99 ")101 (assume $a == $b) parses as (assume ($a == $b))102 (assume $a != $b) parses as (assume ($a != $b))103 (test that $condition) parses as (assume $condition)104 test:105 try: fail106 $worked = (no)107 try:108 fail "xx"109 ..if it fails with $failure:110 $worked = (yes)111 ..if it succeeds:112 fail "'try' incorrectly ran success case."113 assume ($failure, matches "xx")114 unless $worked:115 fail "'try' failed to recover from failure"117 ### Try/except118 [119 try $action if it succeeds $success if it fails with $msg $fallback120 try $action if it fails with $msg $fallback if it succeeds $success121 ] all compile to:122 $success_lua = ($success as lua)123 if (#"\$success_lua" > 0):124 $success_lua, add "\n"125 $success_lua, prepend "-- Success:\n"126 $success_lua,127 add "if not _fell_through then return table.unpack(_result, 2) end"128 $fallback_lua = ($fallback as lua)129 if (#"\$fallback_lua" > 0):130 $msg_lua = ($msg as lua expr)131 if (#"\$msg_lua" > 0):132 $fallback_lua, prepend "\n\$msg_lua = _result[2]\n"133 if ($msg_lua, text, is lua id):134 $fallback_lua, add free vars [($msg_lua, text)]136 $fallback_lua, prepend "-- Failure:\n"137 return Lua ("138 do139 local _fell_through = false140 local _result = {xpcall(function()141 \($action as lua)142 _fell_through = true143 end, enhance_error)}144 if _result[1] then145 \$success_lua146 else147 \$fallback_lua148 end149 end150 ")152 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~154 (try $action) parses as155 try $action if it succeeds (do nothing) if it fails (do nothing)157 (try $action if it fails $fallback) parses as158 try $action if it succeeds (do nothing) if it fails $fallback160 (try $action if it fails with $msg $fallback) parses as161 try $action if it succeeds (do nothing) if it fails with $msg $fallback163 (try $action if it succeeds $success) parses as164 try $action if it succeeds $success if it fails (do nothing)166 (try $action if it fails $fallback if it succeeds $success) parses as167 try $action if it fails with (=lua "") $fallback if it succeeds $success169 (try $action if it succeeds $success if it fails $fallback) parses as170 try $action if it succeeds $success if it fails with (=lua "") $fallback172 test:173 $success = (no)174 try:175 do: fail176 ..then always:177 $success = (yes)178 ..if it succeeds:179 fail "'try ... then always ...' didn't propagate failure"181 unless $success:182 fail "'try ... then always ...' didn't execute the 'always' code"184 (do $action then always $final_action) compiles to ("185 do -- do/then always186 local _fell_through = false187 local _results = {xpcall(function()188 \($action as lua)189 _fell_through = true190 end, enhance_error)}191 \($final_action as lua)192 if not _results[1] then error(_results[2], 0) end193 if not _fell_through then return table.unpack(_results, 2) end194 end195 ")