From 454bb76e2f889f13072445b57204d3e49c42e28c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 12 Oct 2017 14:39:03 -0700 Subject: Added chain and "with". --- lib/collections.nom | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib/collections.nom') 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 -- cgit v1.2.3