aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2017-09-15 04:03:42 +0000
committerBruce Hill <bruce@bruce-hill.com>2017-09-15 04:03:42 +0000
commitbf0188827929051f7d0e116c2148986e7ccc2a8b (patch)
tree8f7ebc8ef9c95d4bb9c5b9cb04669d94619ede9e
parent13490a34c872f5cde6b2f094d9dda045dec066d4 (diff)
Switched to use more flexible write instead of print.
-rw-r--r--core.nom12
-rw-r--r--nomsu.lua44
-rwxr-xr-xnomsu.moon45
3 files changed, 53 insertions, 48 deletions
diff --git a/core.nom b/core.nom
index 2dfa2d9..1376990 100644
--- a/core.nom
+++ b/core.nom
@@ -147,7 +147,7 @@ rule "%a != %b":
lua expr "((vars.a ~= vars.b) or not utils.equivalent(vars.a, vars.b))"
macro "say %str":
- ".."|print(utils.repr(\%str as lua expr\))
+ ".."|compiler:writeln(utils.repr(\%str as lua expr\))
# Control flow
rule "do %action":
@@ -331,7 +331,7 @@ rule "restrict %fn to within %whitelist":
| if fn_info == nil then
| compiler:error("Undefined function: "..tostring(fn))
| elseif not compiler:check_permission(fn) then
- | print("You do not have permission to restrict function: "..tostring(fn))
+ | compiler:writeln("You do not have permission to restrict function: "..tostring(fn))
| else
| compiler.defs[fn].whiteset = whiteset
| end
@@ -351,9 +351,9 @@ rule "allow %whitelist to use %fn":
| if fn_info == nil then
| compiler:error("Undefined function: "..tostring(fn))
| elseif fn_info.whiteset == nil then
- | print("Function is already allowed by everyone: "..tostring(fn))
+ | compiler:writeln("Function is already allowed by everyone: "..tostring(fn))
| elseif not compiler:check_permission(fn) then
- | print("You do not have permission to grant permissions for function: "..tostring(fn))
+ | compiler:writeln("You do not have permission to grant permissions for function: "..tostring(fn))
| else
| for _,w in ipairs(whitelist) do
| fn_info.whiteset[w] = true
@@ -375,9 +375,9 @@ rule "forbid %blacklist to use %fn":
| if fn_info == nil then
| compiler:error("Undefined function: "..tostring(fn))
| elseif fn_info.whiteset == nil then
- | print("Cannot remove items from a whitelist when there is no whitelist on function: "..tostring(fn))
+ | compiler:writeln("Cannot remove items from a whitelist when there is no whitelist on function: "..tostring(fn))
| elseif not compiler:check_permission(fn) then
- | print("You do not have permission to restrict function: "..tostring(fn))
+ | compiler:writeln("You do not have permission to restrict function: "..tostring(fn))
| else
| for _,b in ipairs(blacklist) do fn_info.whiteset[b] = nil end
| end
diff --git a/nomsu.lua b/nomsu.lua
index 3a49af6..4dc8b51 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -136,6 +136,10 @@ local NomsuCompiler
do
local _class_0
local _base_0 = {
+ writeln = function(self, ...)
+ self:write(...)
+ return self:write("\n")
+ end,
call = function(self, fn_name, ...)
local fn_info = self.defs[fn_name]
if fn_info == nil then
@@ -159,7 +163,7 @@ do
args = _tbl_0
end
if self.debug then
- print("Calling " .. tostring(fn_name) .. " with args: " .. tostring(utils.repr(args)))
+ self:writeln("Calling " .. tostring(fn_name) .. " with args: " .. tostring(utils.repr(args)))
end
local ret = fn(self, args)
table.remove(self.callstack)
@@ -184,7 +188,7 @@ do
end,
def = function(self, spec, fn)
if self.debug then
- print("Defining rule: " .. tostring(spec))
+ self:writeln("Defining rule: " .. tostring(spec))
end
local invocations, arg_names = self:get_invocations(spec)
local fn_info = {
@@ -247,17 +251,17 @@ do
end,
run = function(self, text)
if self.debug then
- print("RUNNING TEXT:\n" .. tostring(text))
+ self:writeln("RUNNING TEXT:\n" .. tostring(text))
end
local code, retval = self:compile(text)
if self.debug then
- print("\nGENERATED LUA CODE:\n" .. tostring(code))
+ self:writeln("\nGENERATED LUA CODE:\n" .. tostring(code))
end
return retval
end,
parse = function(self, str)
if self.debug then
- print("PARSING:\n" .. tostring(str))
+ self:writeln("PARSING:\n" .. tostring(str))
end
local lingo = [=[ file <- ({ {| %blank_line* {:body: block :} %blank_line* (errors)? |} }) -> File
errors <- (({.+}) => error_handler)
@@ -320,7 +324,7 @@ do
lingo = make_parser(lingo)
local tree = lingo:match(str:gsub("\r", "") .. "\n")
if self.debug then
- print("\nPARSE TREE:")
+ self:writeln("\nPARSE TREE:")
self:print_tree(tree)
end
assert(tree, "Failed to parse: " .. tostring(str))
@@ -637,7 +641,7 @@ do
for line in coroutine.wrap(function()
return self:_yield_tree(tree)
end) do
- print(line)
+ self:writeln(line)
end
end,
stringify_tree = function(self, tree)
@@ -654,7 +658,7 @@ do
output_file = nil
end
if self.debug then
- print("COMPILING:\n" .. tostring(src))
+ self:writeln("COMPILING:\n" .. tostring(src))
end
local tree = self:parse(src)
assert(tree, "Tree failed to compile: " .. tostring(src))
@@ -666,13 +670,13 @@ do
return code, retval
end,
error = function(self, ...)
- print("ERROR!")
- print(...)
- print("Callstack:")
+ self:writeln("ERROR!")
+ self:writeln(...)
+ self:writeln("Callstack:")
for i = #self.callstack, 1, -1 do
- print(" " .. tostring(self.callstack[i]))
+ self:writeln(" " .. tostring(self.callstack[i]))
end
- print(" <top level>")
+ self:writeln(" <top level>")
self.callstack = { }
return error()
end,
@@ -756,7 +760,10 @@ do
})
self.callstack = { }
self.debug = false
- return self:initialize_core()
+ self:initialize_core()
+ self.write = function(self, ...)
+ return io.write(...)
+ end
end,
__base = _base_0,
__name = "NomsuCompiler"
@@ -796,18 +803,15 @@ end
if arg and arg[1] then
local c = NomsuCompiler()
local input = io.open(arg[1]):read("*a")
- local _print = print
- local _io_write = io.write
+ local _write = c.write
if arg[2] == "-" then
- local nop
- nop = function() end
- print, io.write = nop, nop
+ c.write = function() end
end
local code, retval = c:compile(input)
+ c.write = _write
if arg[2] then
local output
if arg[2] == "-" then
- print, io.write = _print, _io_write
output = io.output()
else
output = io.open(arg[2], 'w')
diff --git a/nomsu.moon b/nomsu.moon
index 0c788c0..513f8e8 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -96,6 +96,11 @@ class NomsuCompiler
@callstack = {}
@debug = false
@initialize_core!
+ @write = (...)=> io.write(...)
+
+ writeln:(...)=>
+ @write(...)
+ @write("\n")
call: (fn_name,...)=>
fn_info = @defs[fn_name]
@@ -109,7 +114,7 @@ class NomsuCompiler
{:fn, :arg_names} = fn_info
args = {name, select(i,...) for i,name in ipairs(arg_names[fn_name])}
if @debug
- print "Calling #{fn_name} with args: #{utils.repr(args)}"
+ @writeln "Calling #{fn_name} with args: #{utils.repr(args)}"
ret = fn(self, args)
table.remove @callstack
return ret
@@ -126,7 +131,7 @@ class NomsuCompiler
def: (spec, fn)=>
if @debug
- print "Defining rule: #{spec}"
+ @writeln "Defining rule: #{spec}"
invocations,arg_names = @get_invocations spec
fn_info = {:fn, :arg_names, :invocations, is_macro:false}
for invocation in *invocations
@@ -156,16 +161,16 @@ class NomsuCompiler
run: (text)=>
if @debug
- print "RUNNING TEXT:\n#{text}"
+ @writeln "RUNNING TEXT:\n#{text}"
-- This will execute each chunk as it goes along
code, retval = @compile(text)
if @debug
- print "\nGENERATED LUA CODE:\n#{code}"
+ @writeln "\nGENERATED LUA CODE:\n#{code}"
return retval
parse: (str)=>
if @debug
- print("PARSING:\n#{str}")
+ @writeln("PARSING:\n#{str}")
lingo = [=[
file <- ({ {| %blank_line* {:body: block :} %blank_line* (errors)? |} }) -> File
errors <- (({.+}) => error_handler)
@@ -229,7 +234,7 @@ class NomsuCompiler
tree = lingo\match(str\gsub("\r","").."\n")
if @debug
- print("\nPARSE TREE:")
+ @writeln("\nPARSE TREE:")
@print_tree(tree)
assert tree, "Failed to parse: #{str}"
return tree
@@ -470,7 +475,7 @@ class NomsuCompiler
print_tree:(tree)=>
for line in coroutine.wrap(-> @_yield_tree(tree))
- print(line)
+ @writeln(line)
stringify_tree:(tree)=>
result = {}
@@ -480,7 +485,7 @@ class NomsuCompiler
compile: (src, output_file=nil)=>
if @debug
- print "COMPILING:\n#{src}"
+ @writeln "COMPILING:\n#{src}"
tree = @parse(src)
assert tree, "Tree failed to compile: #{src}"
code, retval = @tree_to_lua(tree)
@@ -490,12 +495,12 @@ class NomsuCompiler
return code, retval
error: (...)=>
- print "ERROR!"
- print(...)
- print("Callstack:")
+ @writeln "ERROR!"
+ @writeln(...)
+ @writeln("Callstack:")
for i=#@callstack,1,-1
- print " #{@callstack[i]}"
- print " <top level>"
+ @writeln " #{@callstack[i]}"
+ @writeln " <top level>"
@callstack = {}
error!
@@ -567,19 +572,15 @@ if arg and arg[1]
c = NomsuCompiler()
--c.debug = true
input = io.open(arg[1])\read("*a")
- -- Kinda hacky, if run via "./nomsu.moon file.nom -", then silence print and io.write
- -- during execution and re-enable them to print out the generated source code
- _print = print
- _io_write = io.write
+ -- If run via "./nomsu.moon file.nom -", then silence output and print generated
+ -- source code instead.
+ _write = c.write
if arg[2] == "-"
- export print
- nop = ->
- print, io.write = nop, nop
+ c.write = ->
code, retval = c\compile(input)
+ c.write = _write -- put it back
if arg[2]
output = if arg[2] == "-"
- export print
- print, io.write = _print, _io_write
io.output()
else io.open(arg[2], 'w')