summary refs log tree commit diff
path: root/lib/proto.fnl
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2021-04-18 09:57:30 -0500
committerequa <equaa@protonmail.com>2021-04-18 09:57:30 -0500
commit0c8a8cf8d861bc4ef3162e45e8b58d2f0173d2f7 (patch)
tree5fe4e826b92173f33abf8a1f2391ccab706db230 /lib/proto.fnl
nanpa wan
commit of the last two days: working cellular automata and rendering,
prototype system, etc
Diffstat (limited to 'lib/proto.fnl')
-rw-r--r--lib/proto.fnl78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/proto.fnl b/lib/proto.fnl
new file mode 100644
index 0000000..13b65e9
--- /dev/null
+++ b/lib/proto.fnl
@@ -0,0 +1,78 @@
+;; function set in the prototype via its identity, i.e.
+;; (local blah (meta-fn :blah)
+;; (blah (setmetadata {} {blah (fn [] 4)})) ;; -> 4
+(fn meta-fn [name]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] ((. (getmetatable obj) x) ...))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+(fn meta-method [name]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] ((. (getmetatable obj) x) obj ...))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+;; function set in the prototype via its identity, i.e.
+;; (local blah (meta-fn :blah)
+;; however, these functions are optional, and nop if left out
+;; (blah (setmetadata {} {})) ;; -> nil
+(fn meta-fn-opt [name fallback]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] (if (and (getmetatable obj)
+                                      (. (getmetatable obj) x))
+                                 ((. (getmetatable obj) x) ...)
+                                 fallback
+                                 (fallback ...)
+                                 nil))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+(fn meta-method-opt [name fallback]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] (if (and (getmetatable obj)
+                                      (. (getmetatable obj) x))
+                                 ((. (getmetatable obj) x) obj ...)
+                                 fallback
+                                 (fallback obj ...)
+                                 nil))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+;; value set in the table via its identity, i.e.
+;; (local blah (table-value :blah))
+;; (blah {blah 4}) ;; -> (. {blah 4} blah) -> 4
+(fn table-value [name]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] (. obj x))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+(fn table-fn [name]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] ((. obj x) ...))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+;; methods have an extra "self" param
+(fn table-method [name]
+  (local x {})
+  (setmetatable
+    x
+    {:__call (fn [_ obj ...] ((. obj x) obj ...))
+     :__name name
+     :__fennelview (fn [] [name])}))
+
+{: meta-fn : meta-fn-opt : meta-method : meta-method-opt : table-value : table-fn : table-method}