aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core.nom3
-rw-r--r--examples/tutorial.nom11
-rw-r--r--nomsu.lua14
-rwxr-xr-xnomsu.moon14
4 files changed, 30 insertions, 12 deletions
diff --git a/core.nom b/core.nom
index d152137..08115fa 100644
--- a/core.nom
+++ b/core.nom
@@ -204,6 +204,9 @@ rule "dict %items":
lua block "vars.dict[vars.pair[1]] = vars.pair[2]"
return %dict
+rule ["capitalize %str", "%str capitalized"]:
+ lua expr ".."|vars.str:gsub("%l", string.upper, 1)
+
rule "restrict %fn to within %whitelist":
lua block ".."
|local fns = compiler:get_invocations(vars.fn)
diff --git a/examples/tutorial.nom b/examples/tutorial.nom
index 59aed28..7106f94 100644
--- a/examples/tutorial.nom
+++ b/examples/tutorial.nom
@@ -45,6 +45,17 @@ rule "get x from %dict":
is the return value.
return (%dict's "x")
+# Functions can have aliases, which may or may not have the arguments in different order
+rule [..]
+ "I hate %worse-things more than %better-things", "I think %worse-things are worse than %better-things"
+ "I like %better-things more than %worse-things"
+..:
+ say ".."|\%better-things capitalized\ rule and \%worse-things\ drool!
+
+I like "dogs" more than "cats"
+I think "chihuahuas" are worse than "corgis"
+
+
#.. Function calls can have parts of the function's name spread throughout.
Everything that's not a literal value is treated as part of the function's name
say both "Hello" and also "again!"
diff --git a/nomsu.lua b/nomsu.lua
index 0bd2752..161deac 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -153,7 +153,7 @@ do
local args
do
local _tbl_0 = { }
- for i, name in ipairs(arg_names) do
+ for i, name in ipairs(arg_names[fn_name]) do
_tbl_0[name] = select(i, ...)
end
args = _tbl_0
@@ -205,7 +205,8 @@ do
}
end
local invocations = { }
- local arg_names
+ local arg_names = { }
+ local prev_arg_names = nil
for _index_0 = 1, #text do
local _text = text[_index_0]
local invocation = _text:gsub("'", " '"):gsub("%%%S+", "%%"):gsub("%s+", " ")
@@ -220,13 +221,14 @@ do
_arg_names = _accum_0
end
table.insert(invocations, invocation)
- if arg_names then
- if not utils.equivalent(utils.set(arg_names), utils.set(_arg_names)) then
+ if prev_arg_names then
+ if not utils.equivalent(utils.set(prev_arg_names), utils.set(_arg_names)) then
self:error("Conflicting argument names " .. tostring(utils.repr(arg_names)) .. " and " .. tostring(utils.repr(_arg_names)) .. " for " .. tostring(utils.repr(text)))
end
else
- arg_names = _arg_names
+ prev_arg_names = _arg_names
end
+ arg_names[invocation] = _arg_names
end
return invocations, arg_names
end,
@@ -544,7 +546,7 @@ do
end
do
local _tbl_0 = { }
- for i, name in ipairs(arg_names) do
+ for i, name in ipairs(arg_names[name]) do
_tbl_0[name] = args[i]
end
args = _tbl_0
diff --git a/nomsu.moon b/nomsu.moon
index 5626b59..2a9a21d 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -108,7 +108,7 @@ class NomsuCompiler
@error "You do not have the authority to call: #{fn_name}"
table.insert @callstack, fn_name
{:fn, :arg_names} = fn_info
- args = {name, select(i,...) for i,name in ipairs(arg_names)}
+ args = {name, select(i,...) for i,name in ipairs(arg_names[fn_name])}
if @debug
print "Calling #{fn_name} with args: #{utils.repr(args)}"
ret = fn(self, args)
@@ -136,15 +136,17 @@ class NomsuCompiler
get_invocations:(text)=>
if type(text) == 'string' then text = {text}
invocations = {}
- local arg_names
+ arg_names = {}
+ prev_arg_names = nil
for _text in *text
invocation = _text\gsub("'"," '")\gsub("%%%S+","%%")\gsub("%s+"," ")
_arg_names = [arg for arg in _text\gmatch("%%(%S[^%s']*)")]
table.insert(invocations, invocation)
- if arg_names
- if not utils.equivalent(utils.set(arg_names), utils.set(_arg_names))
+ if prev_arg_names
+ if not utils.equivalent(utils.set(prev_arg_names), utils.set(_arg_names))
@error("Conflicting argument names #{utils.repr(arg_names)} and #{utils.repr(_arg_names)} for #{utils.repr(text)}")
- else arg_names = _arg_names
+ else prev_arg_names = _arg_names
+ arg_names[invocation] = _arg_names
return invocations, arg_names
defmacro: (spec, lua_gen_fn)=>
@@ -397,7 +399,7 @@ class NomsuCompiler
@error "You do not have the authority to call: #{name}"
{:fn, :arg_names} = @defs[name]
args = [a for a in *tree.value when a.type != "Word"]
- args = {name,args[i] for i,name in ipairs(arg_names)}
+ args = {name,args[i] for i,name in ipairs(arg_names[name])}
table.insert @callstack, name
ret, manual_mode = fn(self, args, kind)
table.remove @callstack