aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-01-05 15:23:18 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2018-01-05 15:23:18 -0800
commit87b93f4ababeae84ea29d573d98f9ca129f2a126 (patch)
tree6e1fe0dd46a8c16fd2f2df2a83f7b5a8c076cf02
parentde668ce174f07e5b448703b10000437a552245d8 (diff)
Added nomsu:assert()
-rw-r--r--nomsu.lua26
-rwxr-xr-xnomsu.moon22
2 files changed, 30 insertions, 18 deletions
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 <nomsu code>, <is safe for inline use>
- 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 <lua code for value>, <additional lua code>
- 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!"