aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/collections.nom14
-rw-r--r--lib/control_flow.nom9
2 files changed, 22 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
diff --git a/lib/control_flow.nom b/lib/control_flow.nom
index 6287bfb..f0c0714 100644
--- a/lib/control_flow.nom
+++ b/lib/control_flow.nom
@@ -178,3 +178,12 @@ compile [when %branch-value == ? %body] to code:
%result join= "\n::finished_when::;\nend;"
%result
+
+# With statement
+compile [with %thing = %value %action] to code: ".."
+ |do;
+ | local old_value = \(%thing as lua);
+ | \(%thing as lua) = \(%value as lua);
+ | \(%action as lua statements);
+ | \(%thing as lua) = old_value;
+ |end;