aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/metaprogramming.nom8
-rw-r--r--nomsu.lua40
-rwxr-xr-xnomsu.moon40
3 files changed, 37 insertions, 51 deletions
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom
index 49e20ee..f80c144 100644
--- a/lib/metaprogramming.nom
+++ b/lib/metaprogramming.nom
@@ -149,9 +149,9 @@ action [help %action]
lua> ".."
local metadata = \(action %action metadata);
if not metadata then
- nomsu:writeln("Action not found: "..repr(\%action));
+ print("Action not found: "..repr(\%action));
else
- nomsu:writeln(metadata.src or "<unknown source code>");
+ print(metadata.src or "<unknown source code>");
end
# Compiler tools
@@ -163,9 +163,9 @@ immediately
compile [say %message] to
lua> ".."
if \%message.type == "Text" then
- return {statements="nomsu:writeln("..\(%message as lua expr)..");"};
+ return {statements="print("..\(%message as lua expr)..");"};
else
- return {statements="nomsu:writeln(stringify("..\(%message as lua expr).."));"};
+ return {statements="print(stringify("..\(%message as lua expr).."));"};
end
# Return
diff --git a/nomsu.lua b/nomsu.lua
index f1d6e5b..9881a5c 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -184,13 +184,9 @@ do
local _class_0
local line_counter, stub_defs, stub_pattern, var_pattern
local _base_0 = {
- writeln = function(self, ...)
- self:write(...)
- return self:write("\n")
- end,
define_action = function(self, signature, source, fn)
if self.debug then
- self:writeln(tostring(colored.bright("DEFINING ACTION:")) .. " " .. tostring(colored.green(repr(signature))))
+ print(tostring(colored.bright("DEFINING ACTION:")) .. " " .. tostring(colored.green(repr(signature))))
end
if type(fn) ~= 'function' then
error('function', "Bad fn: " .. tostring(repr(fn)))
@@ -220,7 +216,7 @@ do
for sig_i = 1, #stubs do
local stub, args = stubs[sig_i], stub_args[sig_i]
if self.debug then
- self:writeln(tostring(colored.bright("ALIAS:")) .. " " .. tostring(colored.underscore(colored.magenta(repr(stub)))) .. " " .. tostring(colored.bright("WITH ARGS")) .. " " .. tostring(colored.dim(repr(args))) .. " ON: " .. tostring(self.environment.ACTIONS))
+ print(tostring(colored.bright("ALIAS:")) .. " " .. tostring(colored.underscore(colored.magenta(repr(stub)))) .. " " .. tostring(colored.bright("WITH ARGS")) .. " " .. tostring(colored.dim(repr(args))) .. " ON: " .. tostring(self.environment.ACTIONS))
end
self.environment.ACTIONS[stub] = fn
if not (fn_info.isvararg) then
@@ -312,7 +308,7 @@ do
parse = function(self, nomsu_code, filename)
assert(type(filename) == "string", "Bad filename type: " .. tostring(type(filename)))
if self.debug then
- self:writeln(tostring(colored.bright("PARSING:")) .. "\n" .. tostring(colored.yellow(nomsu_code)))
+ print(tostring(colored.bright("PARSING:")) .. "\n" .. tostring(colored.yellow(nomsu_code)))
end
local userdata
do
@@ -345,7 +341,7 @@ do
lpeg.userdata = old_userdata
assert(tree, "In file " .. tostring(colored.blue(filename)) .. " failed to parse:\n" .. tostring(colored.onyellow(colored.black(nomsu_code))))
if self.debug then
- self:writeln("PARSE TREE:")
+ print("PARSE TREE:")
self:print_tree(tree, " ")
end
return tree
@@ -423,7 +419,7 @@ do
run_lua = function(self, lua_code)
local run_lua_fn, err = load(lua_code, nil, nil, self.environment)
if self.debug then
- self:writeln(tostring(colored.bright("RUNNING LUA:")) .. "\n" .. tostring(colored.blue(colored.bright(lua_code))))
+ print(tostring(colored.bright("RUNNING LUA:")) .. "\n" .. tostring(colored.blue(colored.bright(lua_code))))
end
if not run_lua_fn then
local n = 1
@@ -443,7 +439,7 @@ do
end
local code = "return " .. tostring(self:tree_to_lua(tree).expr) .. ";"
if self.debug then
- self:writeln(tostring(colored.bright("RUNNING LUA TO GET VALUE:")) .. "\n" .. tostring(colored.blue(colored.bright(code))))
+ print(tostring(colored.bright("RUNNING LUA TO GET VALUE:")) .. "\n" .. tostring(colored.blue(colored.bright(code))))
end
local lua_thunk, err = load(code, nil, nil, self.environment)
if not lua_thunk then
@@ -954,8 +950,8 @@ do
args = new_args
end
if self.debug then
- self:write(tostring(colored.bright("RUNNING MACRO")) .. " " .. tostring(colored.underscore(colored.magenta(tree.stub))) .. " ")
- self:writeln(tostring(colored.bright("WITH ARGS:")) .. " " .. tostring(colored.dim(repr((function()
+ print(tostring(colored.bright("RUNNING MACRO")) .. " " .. tostring(colored.underscore(colored.magenta(tree.stub))) .. " ")
+ print(tostring(colored.bright("WITH ARGS:")) .. " " .. tostring(colored.dim(repr((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #args do
@@ -1044,9 +1040,9 @@ do
end
local lua = self:tree_to_lua(bit)
if self.debug then
- self:writeln((colored.bright("INTERP:")))
+ print(colored.bright("INTERP:"))
self:print_tree(bit)
- self:writeln(tostring(colored.bright("EXPR:")) .. " " .. tostring(lua.expr) .. ", " .. tostring(colored.bright("STATEMENT:")) .. " " .. tostring(lua.statements))
+ print(tostring(colored.bright("EXPR:")) .. " " .. tostring(lua.expr) .. ", " .. tostring(colored.bright("STATEMENT:")) .. " " .. tostring(lua.statements))
end
assert(lua.expr, "Cannot use [[" .. tostring(bit:get_src()) .. "]] as a string interpolation value, since it's not an expression.")
insert(concat_parts, "stringify(" .. tostring(lua.expr) .. ")")
@@ -1152,17 +1148,17 @@ do
return nil
end,
print_tree = function(self, tree)
- self:write(colors.bright .. colors.green)
+ local buff = colors.bright .. colors.green
for node, depth in coroutine.wrap(function()
return self:walk_tree(tree)
end) do
if type(node) ~= 'table' or not node.type then
- self:writeln((" "):rep(depth) .. repr(node))
+ buff = buff + (((" "):rep(depth) .. repr(node)) .. "\n")
else
- self:writeln(tostring((" "):rep(depth)) .. tostring(node.type) .. ":")
+ buff = buff + ((tostring((" "):rep(depth)) .. tostring(node.type) .. ":") .. "\n")
end
end
- return self:write(colors.reset)
+ return print(buff .. colors.reset)
end,
tree_to_str = function(self, tree)
local bits = { }
@@ -1360,9 +1356,6 @@ do
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self)
- self.write = function(self, ...)
- return io.write(...)
- end
local NaN_surrogate = { }
local nil_surrogate = { }
self.ids = setmetatable({ }, {
@@ -1515,8 +1508,7 @@ if arg then
end
local compiled_output = nil
if args.flags["-p"] then
- local _write = nomsu.write
- nomsu.write = function() end
+ nomsu.environment.print = function() end
compiled_output = io.output()
elseif args.output then
compiled_output = io.open(args.output, 'w')
@@ -1537,7 +1529,7 @@ if arg then
end
end
if args.flags["-p"] then
- nomsu.write = _write
+ nomsu.environment.print = print
end
end
if args.flags["-i"] then
diff --git a/nomsu.moon b/nomsu.moon
index 6fcfcc7..f64b3c9 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -39,7 +39,7 @@ do
-- Add compiler options for optimization level (compile-fast vs. run-fast, etc.)
-- Do a pass on all actions to enforce parameters-are-nouns heuristic
-- Maybe do some sort of lazy definitions of actions that defer until they're used in code
--- Remove nomsu:write and nomsu:writeln and just use print() instead.
+-- Change nomsu:tree_to_lua() to use a table lookup instead of a switch statement
lpeg.setmaxstack 10000 -- whoa
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
@@ -147,7 +147,6 @@ NOMSU = do
class NomsuCompiler
@def_number: 0
new:()=>
- @write = (...)=> io.write(...)
-- Weak-key mapping from objects to randomly generated unique IDs
NaN_surrogate = {}
nil_surrogate = {}
@@ -181,13 +180,9 @@ class NomsuCompiler
@environment.LOADED = {}
@initialize_core!
- writeln:(...)=>
- @write(...)
- @write("\n")
-
define_action: (signature, source, fn)=>
if @debug
- @writeln "#{colored.bright "DEFINING ACTION:"} #{colored.green repr(signature)}"
+ print "#{colored.bright "DEFINING ACTION:"} #{colored.green repr(signature)}"
if type(fn) != 'function'
error 'function', "Bad fn: #{repr fn}"
if type(signature) == 'string'
@@ -206,7 +201,7 @@ class NomsuCompiler
for sig_i=1,#stubs
stub, args = stubs[sig_i], stub_args[sig_i]
if @debug
- @writeln "#{colored.bright "ALIAS:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(args)} ON: #{@environment.ACTIONS}"
+ print "#{colored.bright "ALIAS:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(args)} ON: #{@environment.ACTIONS}"
-- TODO: use debug.getupvalue instead of @environment.ACTIONS?
@environment.ACTIONS[stub] = fn
unless fn_info.isvararg
@@ -256,7 +251,7 @@ class NomsuCompiler
parse: (nomsu_code, filename)=>
assert type(filename) == "string", "Bad filename type: #{type filename}"
if @debug
- @writeln("#{colored.bright "PARSING:"}\n#{colored.yellow nomsu_code}")
+ print "#{colored.bright "PARSING:"}\n#{colored.yellow nomsu_code}"
userdata = with {source_code:nomsu_code, :filename, indent_stack: {0}}
.get_src = => nomsu_code\sub(@start, @stop-1)
@@ -275,7 +270,7 @@ class NomsuCompiler
assert tree, "In file #{colored.blue filename} failed to parse:\n#{colored.onyellow colored.black nomsu_code}"
if @debug
- @writeln "PARSE TREE:"
+ print "PARSE TREE:"
@print_tree tree, " "
return tree
@@ -333,7 +328,7 @@ class NomsuCompiler
run_lua: (lua_code)=>
run_lua_fn, err = load(lua_code, nil, nil, @environment)
if @debug
- @writeln "#{colored.bright "RUNNING LUA:"}\n#{colored.blue colored.bright(lua_code)}"
+ print "#{colored.bright "RUNNING LUA:"}\n#{colored.blue colored.bright(lua_code)}"
if not run_lua_fn
n = 1
fn = ->
@@ -349,7 +344,7 @@ class NomsuCompiler
return tree.value[1]
code = "return #{@tree_to_lua(tree).expr};"
if @debug
- @writeln "#{colored.bright "RUNNING LUA TO GET VALUE:"}\n#{colored.blue colored.bright(code)}"
+ print "#{colored.bright "RUNNING LUA TO GET VALUE:"}\n#{colored.blue colored.bright(code)}"
lua_thunk, err = load(code, nil, nil, @environment)
if not lua_thunk
error("Failed to compile generated code:\n#{colored.bright colored.blue colored.onblack code}\n\n#{colored.red err}", 0)
@@ -660,8 +655,8 @@ class NomsuCompiler
new_args = [args[p] for p in *metadata.arg_orders[tree.stub]]
args = new_args
if @debug
- @write "#{colored.bright "RUNNING MACRO"} #{colored.underscore colored.magenta(tree.stub)} "
- @writeln "#{colored.bright "WITH ARGS:"} #{colored.dim repr [(repr a)\sub(1,50) for a in *args]}"
+ print "#{colored.bright "RUNNING MACRO"} #{colored.underscore colored.magenta(tree.stub)} "
+ print "#{colored.bright "WITH ARGS:"} #{colored.dim repr [(repr a)\sub(1,50) for a in *args]}"
lua = fn(unpack(args))
remove @compilestack
return lua
@@ -707,9 +702,9 @@ class NomsuCompiler
string_buffer = ""
lua = @tree_to_lua bit
if @debug
- @writeln (colored.bright "INTERP:")
+ print(colored.bright "INTERP:")
@print_tree bit
- @writeln "#{colored.bright "EXPR:"} #{lua.expr}, #{colored.bright "STATEMENT:"} #{lua.statements}"
+ print "#{colored.bright "EXPR:"} #{lua.expr}, #{colored.bright "STATEMENT:"} #{lua.statements}"
assert lua.expr,
"Cannot use [[#{bit\get_src!}]] as a string interpolation value, since it's not an expression."
insert concat_parts, "stringify(#{lua.expr})"
@@ -778,13 +773,13 @@ class NomsuCompiler
return nil
print_tree: (tree)=>
- @write colors.bright..colors.green
+ buff = colors.bright..colors.green
for node,depth in coroutine.wrap(-> @walk_tree tree)
if type(node) != 'table' or not node.type
- @writeln((" ")\rep(depth)..repr(node))
+ buff += ((" ")\rep(depth)..repr(node)).."\n"
else
- @writeln("#{(" ")\rep(depth)}#{node.type}:")
- @write colors.reset
+ buff += ("#{(" ")\rep(depth)}#{node.type}:").."\n"
+ print(buff..colors.reset)
tree_to_str: (tree)=>
bits = {}
@@ -964,8 +959,7 @@ if arg
args.output = args.input\gsub("%.nom", ".lua")
compiled_output = nil
if args.flags["-p"]
- _write = nomsu.write
- nomsu.write = ->
+ nomsu.environment.print = ->
compiled_output = io.output()
elseif args.output
compiled_output = io.open(args.output, 'w')
@@ -982,7 +976,7 @@ if arg
compiled_output\write(code)
if args.flags["-p"]
- nomsu.write = _write
+ nomsu.environment.print = print
if args.flags["-i"]
-- REPL