diff options
Diffstat (limited to 'lib/core')
| -rw-r--r-- | lib/core/control_flow.nom | 64 | ||||
| -rw-r--r-- | lib/core/errors.nom | 56 | ||||
| -rw-r--r-- | lib/core/metaprogramming.nom | 30 |
3 files changed, 84 insertions, 66 deletions
diff --git a/lib/core/control_flow.nom b/lib/core/control_flow.nom index 715630b..c4c1770 100644 --- a/lib/core/control_flow.nom +++ b/lib/core/control_flow.nom @@ -328,29 +328,34 @@ test: $clause = "if" $else_allowed = (yes) unless ($body.type is "Block"): - compile error at $body "'if' expected a Block, but got a \($body.type)." - "Perhaps you forgot to put a ':' after 'if'?" + at $body fail (" + Compile error: 'if' expected a Block, but got a \($body.type). + Hint: Perhaps you forgot to put a ':' after 'if'? + ") for $line in $body: unless (($line.type is "Action") and ((size of $line) >= 2)) and $line.(size of $line) is "Block" syntax tree ..: - compile error at $line "Invalid line for the body of an 'if' block." (" - Each line should contain one or more conditional expressions followed by a block, or "else"\ - .. followed by a block. + at $line fail (" + Compile error: Invalid line for the body of an 'if' block. + Hint: Each line should contain one or more conditional expressions followed \ + ..by a block, or "else" followed by a block. ") $action = $line.(size of $line) if (($line.1 is "else") and ((size of $line) == 2)): unless $else_allowed: - compile error at $line "You can't have two 'else' blocks." - "Merge all of the 'else' blocks together." + at $line fail (" + Compile error: You can't have two 'else' blocks. + Hint: Merge all of the 'else' blocks together. + ") unless ((size of "\$code") > 0): - compile error at $line - .."You can't have an 'else' block without a preceding condition" (" - If you want the code in this block to always execute, you don't need a conditional block \ - ..around it. Otherwise, make sure the 'else' block comes last. + at $line fail (" + Compile error: You can't have an 'else' block without a preceding condition. + Hint: If you want the code in this block to always execute, you don't need a \ + ..conditional block around it. Otherwise, make sure the 'else' block comes last. ") $code, add "\nelse\n " ($action as lua) @@ -365,8 +370,10 @@ test: $clause = "\nelseif" if ((size of "\$code") == 0): - compile error at $body "'if' block has an empty body." - "This means nothing would happen, so the 'if' block should be deleted." + at $body fail (" + Compile error: 'if' block has an empty body. + Hint: This means nothing would happen, so the 'if' block should be deleted. + ") $code, add "\nend --when" return $code @@ -392,28 +399,33 @@ test: $else_allowed = (yes) define mangler unless ($body.type is "Block"): - compile error at $body "'if' expected a Block, but got a \($body.type)" - "Perhaps you forgot to put a ':' after the 'is'?" + at $body fail (" + Compile error: 'if' expected a Block, but got a \($body.type). + Hint: Perhaps you forgot to put a ':' after the 'is'? + ") for $line in $body: unless (($line.type is "Action") and ((size of $line) >= 2)) and $line.(size of $line) is "Block" syntax tree ..: - compile error at $line "Invalid line for 'if' block." (" - Each line should contain expressions followed by a block, or "else" followed by a block + at $line fail (" + Compile error: Invalid line for 'if' block. + Hint: Each line should contain expressions followed by a block, or "else" followed by a block. ") $action = $line.(size of $line) if (($line.1 is "else") and ((size of $line) == 2)): unless $else_allowed: - compile error at $line "You can't have two 'else' blocks." - "Merge all of the 'else' blocks together." + at $line fail (" + Compile error: You can't have two 'else' blocks. + Hint: Merge all of the 'else' blocks together. + ") unless ((size of "\$code") > 0): - compile error at $line - .."You can't have an 'else' block without a preceding condition" (" - If you want the code in this block to always execute, you don't need a conditional block \ - ..around it. Otherwise, make sure the 'else' block comes last. + at $line fail (" + Compile error: You can't have an 'else' block without a preceding condition. + Hint: If you want the code in this block to always execute, you don't need \ + ..a conditional block around it. Otherwise, make sure the 'else' block comes last. ") $code, add "\nelse\n " ($action as lua) @@ -428,8 +440,10 @@ test: $clause = "\nelseif" if ((size of "\$code") == 0): - compile error at $body "'if' block has an empty body." - "This means nothing would happen, so the 'if' block should be deleted." + at $body fail (" + Compile error: 'if' block has an empty body. + Hint: This means nothing would happen, so the 'if' block should be deleted. + ") $code, add "\nend --when" return diff --git a/lib/core/errors.nom b/lib/core/errors.nom index 9a2cc9b..0b67400 100644 --- a/lib/core/errors.nom +++ b/lib/core/errors.nom @@ -8,32 +8,36 @@ use "core/control_flow" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -(fail $msg) compiles to "error(\(($msg as lua expr) if $msg else "nil"), 0);" -(assume $condition) compiles to: - lua> (" - local \$assumption = 'Assumption failed: '..tostring((\$condition):get_source_code()) - ") - - return - Lua (" - if not \($condition as lua expr) then - error(\(quote "\$assumption"), 0) - end - ") +(fail $msg) compiles to + LuaCode + "at_1_fail(\(quote $msg.source), 'Failure: '..\($msg as lua expr))" + ..if $msg else "error('Failure', 0)" -(assume $a == $b) compiles to: - lua> "local \$assumption = 'Assumption failed: '..tostring(\(\($a == $b) as nomsu))" - define mangler - return - Lua (" - do - local \(mangle "a"), \(mangle "b") = \($a as lua expr), \($b as lua expr) - if \(mangle "a") ~= \(mangle "b") then - error(\(quote "\$assumption").."\\n"..tostring(\(mangle "a")).." != "..tostring(\ - ..\(mangle "b")), 0) - end - end - ") +(assume $condition) compiles to (" + if not \($condition as lua expr) then + at_1_fail(\(quote "\($condition.source)"), "Assumption failed: This is not true.") + end +") + +(assume $a == $b) compiles to (" + do + local _a, _b = \($a as lua expr), \($b as lua expr) + if _a ~= _b then + at_1_fail(\(quote "\($a.source)"), + "Assumption failed: This value is "..tostring(_a)..", but it was supposed to be "..tostring(_b)..".") + end + end +") + +(assume $a != $b) compiles to (" + do + local _a, _b = \($a as lua expr), \($b as lua expr) + if _a == _b then + at_1_fail(\(quote "\($a.source)"), + "Assumption failed: This value is "..tostring(_a)..", but it wasn't supposed to be.") + end + end +") test: try: fail @@ -44,7 +48,7 @@ test: $worked = (yes) ..if it succeeds: fail "'try' incorrectly ran success case." - assume $failure == "xx" + assume ($failure, matches "xx") unless $worked: fail "'try' failed to recover from failure" diff --git a/lib/core/metaprogramming.nom b/lib/core/metaprogramming.nom index 9b3d5c4..a17a33c 100644 --- a/lib/core/metaprogramming.nom +++ b/lib/core/metaprogramming.nom @@ -34,14 +34,15 @@ lua> (" local arg_lua = SyntaxTree:is_instance(arg) and \(nomsu environment):compile(arg):text() or arg if arg_lua == "..." then if i < #\$args then - compile_error_at(SyntaxTree:is_instance(arg) and arg or nil, - "Extra arguments must come last.", "Try removing any arguments after \ - ..(*extra arguments*)") + at_1_fail(SyntaxTree:is_instance(arg) and arg or nil, + "Compile error: Extra arguments must come last. ".. + "Hint: Try removing any arguments after (*extra arguments*)") end elseif not arg_lua:is_lua_id() then - compile_error_at(SyntaxTree:is_instance(arg) and arg or nil, - "This does not compile to a Lua identifier, so it can't be used as a function argument.", - "This should probably be a Nomsu variable instead (like $x).") + at_1_fail(SyntaxTree:is_instance(arg) and arg or nil, + "Compile error: This does not compile to a Lua identifier, so it ".. + "can't be used as a function argument.".. + "Hint: This should probably be a Nomsu variable instead (like $x).") end lua:add(i > 1 and ", " or "", arg_lua) body_lua:remove_free_vars({arg_lua}) @@ -98,7 +99,7 @@ lua> (" ($actions all compile to $body) compiles to: lua> (" if \$actions.type ~= "List" then - compile_error(\$actions, "This should be a list of actions.") + at_1_fail(\$actions, "Compile error: This should be a list of actions.") end local lua = \(\($actions.1 compiles to $body) as lua) local \$args = List{"\(nomsu environment)", unpack(\$actions[1]:get_args())} @@ -151,7 +152,7 @@ test: lua:add(\$action:get_stub():as_lua_id()) lua:add_free_vars({\$action:get_stub():as_lua_id()}) else - compile_error_at(\$action, "Expected an action or method call here") + at_1_fail(\$action, "Compile error: Expected an action or method call here") end lua:add(" = ", \(\($action -> $body) as lua), ";") return lua @@ -241,7 +242,7 @@ test: lua> (" local replacements = {} if \$actions.type ~= "List" then - compile_error(\$actions, "This should be a list.") + at_1_fail(\$actions, "Compile error: This should be a list.") end for i,arg in ipairs(\$actions[1]:get_args()) do replacements[arg[1]] = \(nomsu environment):compile(arg):text() @@ -293,9 +294,8 @@ external: if \$tree.type == 'Block' then tree_lua = LuaCode:from(\$tree.source, '(function()\\n ', tree_lua, '\\nend)()') elseif \$tree.type == 'MethodCall' and #\$tree > 2 then - compile_error_at(\$tree, "This must be a single value instead of "..(#\$tree - 1).."\ - .. method calls.", - "Replace this with a single method call.") + at_1_fail(\$tree, "Compile error: This must be a single value instead of ".. + (#\$tree - 1).." method calls. Hint: Replace this with a single method call.") end return tree_lua ") @@ -311,9 +311,9 @@ external: lua> (" local lua = \($var as lua) if not lua:text():is_a_lua_id() then - compile_error(\$var, - "This is supposed to be something that compiles to a valid Lua identifier.", - "This should probably be a variable.") + at_1_fail(\$var, "Compile error: ".. + "This is supposed to be something that compiles to a valid Lua identifier. ".. + "Hint: This should probably be a variable.") end return lua ") |
