diff --git a/nomsu.lua b/nomsu.lua index e24cdb5..bb38efd 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -205,7 +205,7 @@ do elseif type(signature) == 'table' and type(signature[1]) == 'string' then signature = self:get_stubs(signature) end - assert(type(thunk) == 'function', "Bad thunk: " .. tostring(repr(thunk))) + self:assert(type(thunk) == 'function', "Bad thunk: " .. tostring(repr(thunk))) local canonical_args = nil local canonical_escaped_args = nil local aliases = { } @@ -223,7 +223,7 @@ do local _des_0 = signature[_index_0] local stub, arg_names, escaped_args stub, arg_names, escaped_args = _des_0[1], _des_0[2], _des_0[3] - assert(stub, "NO STUB FOUND: " .. tostring(repr(signature))) + self:assert(stub, "NO STUB FOUND: " .. tostring(repr(signature))) if self.debug then self:writeln(tostring(colored.bright("DEFINING RULE:")) .. " " .. tostring(colored.underscore(colored.magenta(repr(stub)))) .. " " .. tostring(colored.bright("WITH ARGS")) .. " " .. tostring(colored.dim(repr(arg_names)))) end @@ -235,12 +235,12 @@ do end end if canonical_args then - assert(equivalent(set(arg_names), canonical_args), "Mismatched args") + self:assert(equivalent(set(arg_names), canonical_args), "Mismatched args") else canonical_args = set(arg_names) end if canonical_escaped_args then - assert(equivalent(escaped_args, canonical_escaped_args), "Mismatched escaped args") + self:assert(equivalent(escaped_args, canonical_escaped_args), "Mismatched escaped args") else canonical_escaped_args = escaped_args def.escaped_args = escaped_args @@ -496,7 +496,7 @@ do end str = str:gsub("\r", "") local tree = parse(str, filename) - assert(tree, "In file " .. tostring(colored.blue(filename)) .. " failed to parse:\n" .. tostring(colored.onyellow(colored.black(str)))) + self:assert(tree, "In file " .. tostring(colored.blue(filename)) .. " failed to parse:\n" .. tostring(colored.onyellow(colored.black(str)))) if self.debug then self:writeln("PARSE TREE:") self:print_tree(tree, " ") @@ -525,8 +525,8 @@ do debug.sethook(timeout, "", max_operations) end local tree = self:parse(src, filename) - assert(tree, "Tree failed to compile: " .. tostring(src)) - assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type)) + self:assert(tree, "Tree failed to compile: " .. tostring(src)) + self:assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type)) local buffer = { } local return_value = nil local _list_0 = tree.value @@ -611,7 +611,7 @@ end);]]):format(concat(buffer, "\n")) if force_inline == nil then force_inline = false end - assert(tree, "No tree provided.") + self:assert(tree, "No tree provided.") if not tree.type then self:errorln(debug.traceback()) self:error("Invalid tree: " .. tostring(repr(tree))) @@ -801,7 +801,7 @@ end);]]):format(concat(buffer, "\n")) end end, tree_to_lua = function(self, tree, filename) - assert(tree, "No tree provided.") + self:assert(tree, "No tree provided.") if not tree.type then self:errorln(debug.traceback()) self:error("Invalid tree: " .. tostring(repr(tree))) @@ -1199,6 +1199,14 @@ end)]]):format(concat(lua_bits, "\n")) end end)) end, + assert = function(self, condition, msg) + if msg == nil then + msg = '' + end + if not condition then + return self:error(msg) + end + end, error = function(self, msg) local error_msg = colored.red("ERROR!") if msg then diff --git a/nomsu.moon b/nomsu.moon index 896cd4b..13ff129 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -169,7 +169,7 @@ class NomsuCompiler signature = @get_stubs {signature} elseif type(signature) == 'table' and type(signature[1]) == 'string' signature = @get_stubs signature - assert type(thunk) == 'function', "Bad thunk: #{repr thunk}" + @assert type(thunk) == 'function', "Bad thunk: #{repr thunk}" canonical_args = nil canonical_escaped_args = nil aliases = {} @@ -177,15 +177,15 @@ class NomsuCompiler def = {:thunk, :src, :is_macro, aliases:{}, def_number:@@def_number, defs:@defs} where_defs_go = (getmetatable(@defs) or {}).__newindex or @defs for {stub, arg_names, escaped_args} in *signature - assert stub, "NO STUB FOUND: #{repr signature}" + @assert stub, "NO STUB FOUND: #{repr signature}" if @debug then @writeln "#{colored.bright "DEFINING RULE:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(arg_names)}" for i=1,#arg_names-1 do for j=i+1,#arg_names if arg_names[i] == arg_names[j] then @error "Duplicate argument in function #{stub}: '#{arg_names[i]}'" if canonical_args - assert equivalent(set(arg_names), canonical_args), "Mismatched args" + @assert equivalent(set(arg_names), canonical_args), "Mismatched args" else canonical_args = set(arg_names) if canonical_escaped_args - assert equivalent(escaped_args, canonical_escaped_args), "Mismatched escaped args" + @assert equivalent(escaped_args, canonical_escaped_args), "Mismatched escaped args" else canonical_escaped_args = escaped_args def.escaped_args = escaped_args @@ -324,7 +324,7 @@ class NomsuCompiler @writeln("#{colored.bright "PARSING:"}\n#{colored.yellow str}") str = str\gsub("\r","") tree = parse(str, filename) - assert tree, "In file #{colored.blue filename} failed to parse:\n#{colored.onyellow colored.black str}" + @assert tree, "In file #{colored.blue filename} failed to parse:\n#{colored.onyellow colored.black str}" if @debug @writeln "PARSE TREE:" @print_tree tree, " " @@ -338,8 +338,8 @@ class NomsuCompiler @error "Execution quota exceeded. Your code took too long." debug.sethook timeout, "", max_operations tree = @parse(src, filename) - assert tree, "Tree failed to compile: #{src}" - assert tree.type == "File", "Attempt to run non-file: #{tree.type}" + @assert tree, "Tree failed to compile: #{src}" + @assert tree.type == "File", "Attempt to run non-file: #{tree.type}" buffer = {} return_value = nil @@ -405,7 +405,7 @@ end);]])\format(concat(buffer, "\n")) tree_to_nomsu: (tree, force_inline=false)=> -- Return , - assert tree, "No tree provided." + @assert tree, "No tree provided." if not tree.type @errorln debug.traceback() @error "Invalid tree: #{repr(tree)}" @@ -535,7 +535,7 @@ end);]])\format(concat(buffer, "\n")) tree_to_lua: (tree, filename)=> -- Return , - assert tree, "No tree provided." + @assert tree, "No tree provided." if not tree.type @errorln debug.traceback() @error "Invalid tree: #{repr(tree)}" @@ -786,6 +786,10 @@ end)]])\format(concat(lua_bits, "\n")) var = var.value (var\gsub "%W", (verboten)-> if verboten == "_" then "__" else ("_%x")\format(verboten\byte!)) + + assert: (condition, msg='')=> + if not condition + @error(msg) error: (msg)=> error_msg = colored.red "ERROR!"