aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nomsu.lua29
-rwxr-xr-xnomsu.moon27
2 files changed, 34 insertions, 22 deletions
diff --git a/nomsu.lua b/nomsu.lua
index 11dd792..bd04c46 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -205,6 +205,10 @@ do
self:write(...)
return self:write("\n")
end,
+ errorln = function(self, ...)
+ self:write_err(...)
+ return self:write_err("\n")
+ end,
def = function(self, signature, thunk, src, is_macro)
if is_macro == nil then
is_macro = false
@@ -362,7 +366,7 @@ do
end
local ok, expr, statements = pcall(self.tree_to_lua, self, statement)
if not ok then
- self:writeln(tostring(colored.red("Error occurred in statement:")) .. "\n" .. tostring(colored.bright(colored.yellow(statement.src))))
+ self:errorln(tostring(colored.red("Error occurred in statement:")) .. "\n" .. tostring(colored.bright(colored.yellow(statement.src))))
self:error(expr)
end
local code_for_statement = ([[ return (function(nomsu, vars)
@@ -390,8 +394,8 @@ do
return_value = ret
end
if not ok then
- self:writeln(tostring(colored.red("Error occurred in statement:")) .. "\n" .. tostring(colored.yellow(statement.src)))
- self:writeln(debug.traceback())
+ self:errorln(tostring(colored.red("Error occurred in statement:")) .. "\n" .. tostring(colored.yellow(statement.src)))
+ self:errorln(debug.traceback())
self:error(ret)
end
insert(buffer, tostring(statements or '') .. "\n" .. tostring(expr and "ret = " .. tostring(expr) or ''))
@@ -420,7 +424,7 @@ do
tree_to_lua = function(self, tree)
assert(tree, "No tree provided.")
if not tree.type then
- self:writeln(debug.traceback())
+ self:errorln(debug.traceback())
self:error("Invalid tree: " .. tostring(repr(tree)))
end
local _exp_0 = tree.type
@@ -719,16 +723,16 @@ do
end
end))
end,
- error = function(self, ...)
- self:writeln("ERROR!")
- if select(1, ...) then
- self:writeln(...)
+ error = function(self, msg)
+ self:errorln((colored.red("ERROR!")))
+ if msg then
+ self:errorln(colored.bright(colored.yellow(colored.onred(msg))))
end
- self:writeln("Callstack:")
+ self:errorln("Callstack:")
for i = #self.callstack, 1, -1 do
- self:writeln(" " .. tostring(self.callstack[i]))
+ self:errorln(" " .. tostring(self.callstack[i]))
end
- self:writeln(" <top level>")
+ self:errorln(" <top level>")
self.callstack = { }
return error()
end,
@@ -809,6 +813,9 @@ do
self.write = function(self, ...)
return io.write(...)
end
+ self.write_err = function(self, ...)
+ return io.stderr:write(...)
+ end
self.defs = setmetatable({ }, {
__index = parent and parent.defs
})
diff --git a/nomsu.moon b/nomsu.moon
index 1695906..7e45d0d 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -184,6 +184,7 @@ nomsu = re.compile(nomsu, defs)
class NomsuCompiler
new:(parent)=>
@write = (...)=> io.write(...)
+ @write_err = (...)=> io.stderr\write(...)
@defs = setmetatable({}, {__index:parent and parent.defs})
@callstack = {}
@debug = false
@@ -197,6 +198,10 @@ class NomsuCompiler
@write(...)
@write("\n")
+ errorln:(...)=>
+ @write_err(...)
+ @write_err("\n")
+
def: (signature, thunk, src, is_macro=false)=>
assert type(thunk) == 'function', "Bad thunk: #{repr thunk}"
canonical_args = nil
@@ -293,7 +298,7 @@ class NomsuCompiler
@print_tree statement
ok,expr,statements = pcall(@tree_to_lua, self, statement)
if not ok
- @writeln "#{colored.red "Error occurred in statement:"}\n#{colored.bright colored.yellow statement.src}"
+ @errorln "#{colored.red "Error occurred in statement:"}\n#{colored.bright colored.yellow statement.src}"
@error(expr)
code_for_statement = ([[
return (function(nomsu, vars)
@@ -314,8 +319,8 @@ class NomsuCompiler
ok,ret = pcall(run_statement, self, vars)
if expr then return_value = ret
if not ok
- @writeln "#{colored.red "Error occurred in statement:"}\n#{colored.yellow statement.src}"
- @writeln debug.traceback!
+ @errorln "#{colored.red "Error occurred in statement:"}\n#{colored.yellow statement.src}"
+ @errorln debug.traceback!
@error(ret)
insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr}" or ''}"
@@ -342,7 +347,7 @@ class NomsuCompiler
-- Return <lua code for value>, <additional lua code>
assert tree, "No tree provided."
if not tree.type
- @writeln debug.traceback()
+ @errorln debug.traceback()
@error "Invalid tree: #{repr(tree)}"
switch tree.type
when "File"
@@ -537,14 +542,14 @@ class NomsuCompiler
(var\gsub "%W", (verboten)->
if verboten == "_" then "__" else ("_%x")\format(verboten\byte!))
- error: (...)=>
- @writeln "ERROR!"
- if select(1, ...)
- @writeln(...)
- @writeln("Callstack:")
+ error: (msg)=>
+ @errorln (colored.red "ERROR!")
+ if msg
+ @errorln(colored.bright colored.yellow colored.onred msg)
+ @errorln("Callstack:")
for i=#@callstack,1,-1
- @writeln " #{@callstack[i]}"
- @writeln " <top level>"
+ @errorln " #{@callstack[i]}"
+ @errorln " <top level>"
@callstack = {}
error!