diff --git a/src/cljs/emptyhead/lib/object/core.cljs b/src/cljs/emptyhead/lib/object/core.cljs new file mode 100644 index 0000000..bbc3d00 --- /dev/null +++ b/src/cljs/emptyhead/lib/object/core.cljs @@ -0,0 +1,71 @@ +(ns emptyhead.lib.object.core + (:require [emptyhead.thought.crud :as thought] + [emptyhead.thought.define :as def] + [emptyhead.idea.property :as prop] + [emptyhead.thought.extend :as extend] + [emptyhead.thought.eval :as eval] + )) + +(def/define! [:EH :CORE :RETURN] + (fn [thought parent] + [parent (thought/data thought)])) + +(def/define! [:EH :DEBUG :PRINT-DATA] + (fn [thought parent] + (print (thought/data thought)) + [parent nil])) + +(def/define! [:EH :NOP] (fn [parent _] [parent nil])) + +(def/define! [:EH :CALL] (fn [parent _] [parent nil])) + +(def/define! [:EH :DEBUG :WHOAMI] + (fn [_ parent] + (let [[_ self] (thought/pop-stack parent)] + (println "Hello from " self "!") + (println parent) + [parent nil]))) + +(def/define! [:EH :DEBUG :PRINT-TOS] + (fn [_ parent] + (let [[parent self] (thought/pop-stack parent) + [_ stack] (thought/pop-stack parent) + val (last stack)] + (println val) + [parent nil]))) + +(def sexer (thought/register-thought! [:EH :DEBUG :WHOAMI])) + +;; TODO magic identifiers +(defn create [] + [:EH :OBJ (keyword (gensym "O-"))]) + +(defn extend [object name daughter] + (extend/register-extension! + (thought/register-thought! [:EH :CORE :RETURN] :data daughter) + (conj object name))) + +(defn implement [object thought] + (extend/register-extension! thought (conj object :_CALL))) + +(defn call [object thought] + (eval/execute! + (thought/make-thought [:EH :CALL] + :return [(thought/stack thought) object] + :ext-stages [(conj object :_CALL)]) + thought)) + +(defn val->obj [val] + (let [obj (create) + val-th (thought/register-thought! [:EH :CORE :RETURN] :data val)] + (implement obj val-th) + obj)) + +(defn th->obj [op & [data]] + (let [obj (create) + val-th (thought/register-thought! op :data data)] + (implement obj val-th) + obj)) + +(defn val->obj [val] + (th->obj [:EH :CORE :RETURN] val)) diff --git a/src/cljs/emptyhead/thought/crud.cljs b/src/cljs/emptyhead/thought/crud.cljs index 95399c8..a51eec1 100644 --- a/src/cljs/emptyhead/thought/crud.cljs +++ b/src/cljs/emptyhead/thought/crud.cljs @@ -8,13 +8,14 @@ (defn make-thought "Helper function to make thought object. You may want `register-thought!` instead." - [operator & {:keys [data ext-stages] + [operator & {:keys [data ext-stages return] :or {data {} - ext-stages [[:PRE-EXECUTE] [:EXECUTE] [:POST-EXECUTE]]}}] + ext-stages [[:PRE-EXECUTE] [:EXECUTE] [:POST-EXECUTE]] + return []}}] (hash-map :operator operator :data data :ext-stages ext-stages - :return [])) + :return return)) (defn register-thought! "Create a thought and register it in the state.