From 0c8a8cf8d861bc4ef3162e45e8b58d2f0173d2f7 Mon Sep 17 00:00:00 2001 From: equa Date: Sun, 18 Apr 2021 09:57:30 -0500 Subject: nanpa wan commit of the last two days: working cellular automata and rendering, prototype system, etc --- lib/proto.fnl | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 lib/proto.fnl (limited to 'lib/proto.fnl') 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} -- cgit 1.3.0-6-gf8a5