aboutsummaryrefslogtreecommitdiff
path: root/lib/collections.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-10-12 14:39:03 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-10-12 14:39:03 -0700
commit454bb76e2f889f13072445b57204d3e49c42e28c (patch)
treed4f65f98b9c773df2b7c6850c0ff757593e693ad /lib/collections.nom
parent4e5445e6bcd2007a6308942237523eef7c94953f (diff)
Added chain and "with".
Diffstat (limited to 'lib/collections.nom')
-rw-r--r--lib/collections.nom14
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/collections.nom b/lib/collections.nom
index 91b7941..022c1dc 100644
--- a/lib/collections.nom
+++ b/lib/collections.nom
@@ -133,10 +133,22 @@ rule [%items sorted by %key] =:
sort %copy by %key
%copy
+# Metatable stuff
+compile [counter] to: "setmetatable({}, {__index=function() return 0; end})"
+rule [chain %dict to %fallback] =:
+ when (type of %fallback) == ?:
+ * "table":
+ lua expr "setmetatable(\(%dict), \(%fallback))"
+ * "function":
+ lua expr "setmetatable(\(%dict), {__index=function(self, key) return (\(%fallback))(nomsu, {['']=key, self=self}); end})"
+ * else:
+ lua expr "setmetatable(\(%dict), {__index=function(self, key) return (\(%fallback)); end})"
+
+
# TODO: maybe make a generator/coroutine?
#.. Dict comprehensions can be accomplished okay by doing:
- dict ([new_key using (%'s "key"), new_value using (%'s "value")] for all (entries in %dict))
+ dict ([%'s "key", %'s "value"] for all (entries in %dict))
or something similar
# TODO: fix compiler bugs
pass