aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/metaprogramming.nom12
-rw-r--r--lib/object2.nom4
-rw-r--r--nomsu.lua21
-rwxr-xr-xnomsu.moon18
4 files changed, 32 insertions, 23 deletions
diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom
index 544ae59..b56f0e9 100644
--- a/core/metaprogramming.nom
+++ b/core/metaprogramming.nom
@@ -98,13 +98,13 @@ immediately:
action [remove action %stub]:
lua> ".."
- local fn = ACTION[\%stub];
+ local fn = ACTIONS[\%stub];
local metadata = ACTION_METADATA[fn];
for i=#metadata.aliases,1,-1 do
metadata.arg_orders[metadata.aliases[i]] = nil;
table.remove(metadata.aliases, i);
end
- ACTION[\%stub] = nil;
+ ACTIONS[\%stub] = nil;
immediately:
action [%tree as lua]:
@@ -141,7 +141,7 @@ immediately:
compile [%var as lua identifier] to {expr:"nomsu:var_to_lua_identifier(\(%var as lua expr))"}
action [action %names metadata]:
- =lua "ACTION_METADATA[ACTION[\%names]]"
+ =lua "ACTION_METADATA[ACTIONS[\%names]]"
# Get the source code for a function
action [help %action]:
@@ -160,6 +160,12 @@ immediately:
parse [disable debugging] as: lua> "nomsu.debug = false;"
immediately:
+ compile [show lua %block] to:
+ lua> ".."
+ local \%lua = nomsu:tree_to_lua(\%block);
+ return {statements = "print("..repr(\%lua.statements or \%lua.expr)..");"};
+
+immediately:
compile [say %message] to:
lua> ".."
if \%message.type == "Text" then
diff --git a/lib/object2.nom b/lib/object2.nom
index d9e90cc..d27bbc0 100644
--- a/lib/object2.nom
+++ b/lib/object2.nom
@@ -33,8 +33,8 @@ compile [as %instance %body] to:
statements: ".."
do
local self = \(%instance as lua expr);
- local global_actions = ACTION;
- local ACTION = setmetatable({}, {__index=function(_,key)
+ local global_actions = ACTIONS;
+ local ACTIONS = setmetatable({}, {__index=function(_,key)
local method = self[key];
if method then return (function(...) return method(self, ...); end); end
return global_actions[key];
diff --git a/nomsu.lua b/nomsu.lua
index 60f74ea..82465d1 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -212,9 +212,9 @@ do
for sig_i = 1, #stubs do
local stub, args = stubs[sig_i], stub_args[sig_i]
if self.debug then
- 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.ACTION))
+ 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.ACTION[stub] = fn
+ self.environment.ACTIONS[stub] = fn
if not (fn_info.isvararg) then
local arg_positions
do
@@ -941,7 +941,12 @@ do
}
elseif "FunctionCall" == _exp_0 then
insert(self.compilestack, tree)
- local fn = rawget(self.environment.ACTION, tree.stub)
+ local ok, fn = pcall(function()
+ return self.environment.ACTIONS[tree.stub]
+ end)
+ if not ok then
+ fn = nil
+ end
local metadata = self.environment.ACTION_METADATA[fn]
if metadata and metadata.compile_time then
local args
@@ -1043,7 +1048,7 @@ do
end
remove(self.compilestack)
return {
- expr = self.__class:comma_separated_items("ACTION[" .. tostring(repr(tree.stub)) .. "](", args, ")")
+ expr = self.__class:comma_separated_items("ACTIONS[" .. tostring(repr(tree.stub)) .. "](", args, ")")
}
elseif "Text" == _exp_0 then
local concat_parts = { }
@@ -1369,15 +1374,13 @@ do
}
end)
self:define_action("run file %filename", get_line_no(), function(_filename)
- return {
- expr = "nomsu:run_file(" .. tostring(nomsu:tree_to_lua(filename).expr) .. ")"
- }
+ return nomsu:run_file(_filename)
end)
return self:define_compile_action("use %filename", get_line_no(), function(_filename)
local filename = nomsu:tree_to_value(_filename)
nomsu:use_file(filename)
return {
- expr = "nomsu:use_file(" .. tostring(repr(filename)) .. ");"
+ expr = "nomsu:use_file(" .. tostring(repr(filename)) .. ")"
}
end)
end
@@ -1447,7 +1450,7 @@ do
load = load,
ipairs = ipairs
}
- self.environment.ACTION = setmetatable({ }, {
+ self.environment.ACTIONS = setmetatable({ }, {
__index = function(self, key)
return error("Attempt to run undefined action: " .. tostring(key), 0)
end
diff --git a/nomsu.moon b/nomsu.moon
index 0c58a2d..ce4e5e2 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -169,7 +169,7 @@ class NomsuCompiler
:table, :assert, :dofile, :loadstring, :type, :select, :debug, :math, :io, :pairs,
:load, :ipairs,
}
- @environment.ACTION = setmetatable({}, {__index:(key)=>
+ @environment.ACTIONS = setmetatable({}, {__index:(key)=>
error("Attempt to run undefined action: #{key}", 0)
})
@environment.ACTION_METADATA = setmetatable({}, {__mode:"k"})
@@ -196,9 +196,8 @@ class NomsuCompiler
for sig_i=1,#stubs
stub, args = stubs[sig_i], stub_args[sig_i]
if @debug
- print "#{colored.bright "ALIAS:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(args)} ON: #{@environment.ACTION}"
- -- TODO: use debug.getupvalue instead of @environment.ACTION?
- @environment.ACTION[stub] = fn
+ print "#{colored.bright "ALIAS:"} #{colored.underscore colored.magenta repr(stub)} #{colored.bright "WITH ARGS"} #{colored.dim repr(args)} ON: #{@environment.ACTIONS}"
+ @environment.ACTIONS[stub] = fn
unless fn_info.isvararg
arg_positions = [fn_arg_positions[a] for a in *args]
-- TODO: better error checking?
@@ -658,8 +657,9 @@ class NomsuCompiler
when "FunctionCall"
insert @compilestack, tree
- -- Rawget here to avoid triggering an error for accessing an undefined action
- fn = rawget(@environment.ACTION, tree.stub)
+ ok, fn = pcall(-> @environment.ACTIONS[tree.stub])
+ if not ok then fn = nil
+
metadata = @environment.ACTION_METADATA[fn]
if metadata and metadata.compile_time
args = [arg for arg in *tree.value when arg.type != "Word"]
@@ -700,7 +700,7 @@ class NomsuCompiler
args = new_args
remove @compilestack
- return expr:@@comma_separated_items("ACTION[#{repr tree.stub}](", args, ")")
+ return expr:@@comma_separated_items("ACTIONS[#{repr tree.stub}](", args, ")")
when "Text"
concat_parts = {}
@@ -936,12 +936,12 @@ class NomsuCompiler
return expr: repr("#{tree.filename}:#{tree.start},#{tree.stop}")
@define_action "run file %filename", get_line_no!, (_filename)->
- return expr:"nomsu:run_file(#{nomsu\tree_to_lua(filename).expr})"
+ return nomsu\run_file(_filename)
@define_compile_action "use %filename", get_line_no!, (_filename)->
filename = nomsu\tree_to_value(_filename)
nomsu\use_file(filename)
- return expr:"nomsu:use_file(#{repr filename});"
+ return expr:"nomsu:use_file(#{repr filename})"
-- Only run this code if this file was run directly with command line arguments, and not require()'d:
if arg and debug.getinfo(2).func != require