Added nomsu:assert()
This commit is contained in:
parent
de668ce174
commit
87b93f4aba
26
nomsu.lua
26
nomsu.lua
@ -205,7 +205,7 @@ do
|
|||||||
elseif type(signature) == 'table' and type(signature[1]) == 'string' then
|
elseif type(signature) == 'table' and type(signature[1]) == 'string' then
|
||||||
signature = self:get_stubs(signature)
|
signature = self:get_stubs(signature)
|
||||||
end
|
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_args = nil
|
||||||
local canonical_escaped_args = nil
|
local canonical_escaped_args = nil
|
||||||
local aliases = { }
|
local aliases = { }
|
||||||
@ -223,7 +223,7 @@ do
|
|||||||
local _des_0 = signature[_index_0]
|
local _des_0 = signature[_index_0]
|
||||||
local stub, arg_names, escaped_args
|
local stub, arg_names, escaped_args
|
||||||
stub, arg_names, escaped_args = _des_0[1], _des_0[2], _des_0[3]
|
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
|
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))))
|
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
|
end
|
||||||
@ -235,12 +235,12 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if canonical_args then
|
if canonical_args then
|
||||||
assert(equivalent(set(arg_names), canonical_args), "Mismatched args")
|
self:assert(equivalent(set(arg_names), canonical_args), "Mismatched args")
|
||||||
else
|
else
|
||||||
canonical_args = set(arg_names)
|
canonical_args = set(arg_names)
|
||||||
end
|
end
|
||||||
if canonical_escaped_args then
|
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
|
else
|
||||||
canonical_escaped_args = escaped_args
|
canonical_escaped_args = escaped_args
|
||||||
def.escaped_args = escaped_args
|
def.escaped_args = escaped_args
|
||||||
@ -496,7 +496,7 @@ do
|
|||||||
end
|
end
|
||||||
str = str:gsub("\r", "")
|
str = str:gsub("\r", "")
|
||||||
local tree = parse(str, filename)
|
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
|
if self.debug then
|
||||||
self:writeln("PARSE TREE:")
|
self:writeln("PARSE TREE:")
|
||||||
self:print_tree(tree, " ")
|
self:print_tree(tree, " ")
|
||||||
@ -525,8 +525,8 @@ do
|
|||||||
debug.sethook(timeout, "", max_operations)
|
debug.sethook(timeout, "", max_operations)
|
||||||
end
|
end
|
||||||
local tree = self:parse(src, filename)
|
local tree = self:parse(src, filename)
|
||||||
assert(tree, "Tree failed to compile: " .. tostring(src))
|
self:assert(tree, "Tree failed to compile: " .. tostring(src))
|
||||||
assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type))
|
self:assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type))
|
||||||
local buffer = { }
|
local buffer = { }
|
||||||
local return_value = nil
|
local return_value = nil
|
||||||
local _list_0 = tree.value
|
local _list_0 = tree.value
|
||||||
@ -611,7 +611,7 @@ end);]]):format(concat(buffer, "\n"))
|
|||||||
if force_inline == nil then
|
if force_inline == nil then
|
||||||
force_inline = false
|
force_inline = false
|
||||||
end
|
end
|
||||||
assert(tree, "No tree provided.")
|
self:assert(tree, "No tree provided.")
|
||||||
if not tree.type then
|
if not tree.type then
|
||||||
self:errorln(debug.traceback())
|
self:errorln(debug.traceback())
|
||||||
self:error("Invalid tree: " .. tostring(repr(tree)))
|
self:error("Invalid tree: " .. tostring(repr(tree)))
|
||||||
@ -801,7 +801,7 @@ end);]]):format(concat(buffer, "\n"))
|
|||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
tree_to_lua = function(self, tree, filename)
|
tree_to_lua = function(self, tree, filename)
|
||||||
assert(tree, "No tree provided.")
|
self:assert(tree, "No tree provided.")
|
||||||
if not tree.type then
|
if not tree.type then
|
||||||
self:errorln(debug.traceback())
|
self:errorln(debug.traceback())
|
||||||
self:error("Invalid tree: " .. tostring(repr(tree)))
|
self:error("Invalid tree: " .. tostring(repr(tree)))
|
||||||
@ -1199,6 +1199,14 @@ end)]]):format(concat(lua_bits, "\n"))
|
|||||||
end
|
end
|
||||||
end))
|
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)
|
error = function(self, msg)
|
||||||
local error_msg = colored.red("ERROR!")
|
local error_msg = colored.red("ERROR!")
|
||||||
if msg then
|
if msg then
|
||||||
|
22
nomsu.moon
22
nomsu.moon
@ -169,7 +169,7 @@ class NomsuCompiler
|
|||||||
signature = @get_stubs {signature}
|
signature = @get_stubs {signature}
|
||||||
elseif type(signature) == 'table' and type(signature[1]) == 'string'
|
elseif type(signature) == 'table' and type(signature[1]) == 'string'
|
||||||
signature = @get_stubs signature
|
signature = @get_stubs signature
|
||||||
assert type(thunk) == 'function', "Bad thunk: #{repr thunk}"
|
@assert type(thunk) == 'function', "Bad thunk: #{repr thunk}"
|
||||||
canonical_args = nil
|
canonical_args = nil
|
||||||
canonical_escaped_args = nil
|
canonical_escaped_args = nil
|
||||||
aliases = {}
|
aliases = {}
|
||||||
@ -177,15 +177,15 @@ class NomsuCompiler
|
|||||||
def = {:thunk, :src, :is_macro, aliases:{}, def_number:@@def_number, defs:@defs}
|
def = {:thunk, :src, :is_macro, aliases:{}, def_number:@@def_number, defs:@defs}
|
||||||
where_defs_go = (getmetatable(@defs) or {}).__newindex or @defs
|
where_defs_go = (getmetatable(@defs) or {}).__newindex or @defs
|
||||||
for {stub, arg_names, escaped_args} in *signature
|
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)}"
|
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
|
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 arg_names[i] == arg_names[j] then @error "Duplicate argument in function #{stub}: '#{arg_names[i]}'"
|
||||||
if canonical_args
|
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)
|
else canonical_args = set(arg_names)
|
||||||
if canonical_escaped_args
|
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
|
else
|
||||||
canonical_escaped_args = escaped_args
|
canonical_escaped_args = escaped_args
|
||||||
def.escaped_args = escaped_args
|
def.escaped_args = escaped_args
|
||||||
@ -324,7 +324,7 @@ class NomsuCompiler
|
|||||||
@writeln("#{colored.bright "PARSING:"}\n#{colored.yellow str}")
|
@writeln("#{colored.bright "PARSING:"}\n#{colored.yellow str}")
|
||||||
str = str\gsub("\r","")
|
str = str\gsub("\r","")
|
||||||
tree = parse(str, filename)
|
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
|
if @debug
|
||||||
@writeln "PARSE TREE:"
|
@writeln "PARSE TREE:"
|
||||||
@print_tree tree, " "
|
@print_tree tree, " "
|
||||||
@ -338,8 +338,8 @@ class NomsuCompiler
|
|||||||
@error "Execution quota exceeded. Your code took too long."
|
@error "Execution quota exceeded. Your code took too long."
|
||||||
debug.sethook timeout, "", max_operations
|
debug.sethook timeout, "", max_operations
|
||||||
tree = @parse(src, filename)
|
tree = @parse(src, filename)
|
||||||
assert tree, "Tree failed to compile: #{src}"
|
@assert tree, "Tree failed to compile: #{src}"
|
||||||
assert tree.type == "File", "Attempt to run non-file: #{tree.type}"
|
@assert tree.type == "File", "Attempt to run non-file: #{tree.type}"
|
||||||
|
|
||||||
buffer = {}
|
buffer = {}
|
||||||
return_value = nil
|
return_value = nil
|
||||||
@ -405,7 +405,7 @@ end);]])\format(concat(buffer, "\n"))
|
|||||||
|
|
||||||
tree_to_nomsu: (tree, force_inline=false)=>
|
tree_to_nomsu: (tree, force_inline=false)=>
|
||||||
-- Return <nomsu code>, <is safe for inline use>
|
-- Return <nomsu code>, <is safe for inline use>
|
||||||
assert tree, "No tree provided."
|
@assert tree, "No tree provided."
|
||||||
if not tree.type
|
if not tree.type
|
||||||
@errorln debug.traceback()
|
@errorln debug.traceback()
|
||||||
@error "Invalid tree: #{repr(tree)}"
|
@error "Invalid tree: #{repr(tree)}"
|
||||||
@ -535,7 +535,7 @@ end);]])\format(concat(buffer, "\n"))
|
|||||||
|
|
||||||
tree_to_lua: (tree, filename)=>
|
tree_to_lua: (tree, filename)=>
|
||||||
-- Return <lua code for value>, <additional lua code>
|
-- Return <lua code for value>, <additional lua code>
|
||||||
assert tree, "No tree provided."
|
@assert tree, "No tree provided."
|
||||||
if not tree.type
|
if not tree.type
|
||||||
@errorln debug.traceback()
|
@errorln debug.traceback()
|
||||||
@error "Invalid tree: #{repr(tree)}"
|
@error "Invalid tree: #{repr(tree)}"
|
||||||
@ -787,6 +787,10 @@ end)]])\format(concat(lua_bits, "\n"))
|
|||||||
(var\gsub "%W", (verboten)->
|
(var\gsub "%W", (verboten)->
|
||||||
if verboten == "_" then "__" else ("_%x")\format(verboten\byte!))
|
if verboten == "_" then "__" else ("_%x")\format(verboten\byte!))
|
||||||
|
|
||||||
|
assert: (condition, msg='')=>
|
||||||
|
if not condition
|
||||||
|
@error(msg)
|
||||||
|
|
||||||
error: (msg)=>
|
error: (msg)=>
|
||||||
error_msg = colored.red "ERROR!"
|
error_msg = colored.red "ERROR!"
|
||||||
if msg
|
if msg
|
||||||
|
Loading…
Reference in New Issue
Block a user