Files
EMPTYHEAD/src/cljs/emptyhead/newlib/math/arithmetic.cljs
2026-01-23 11:08:52 +01:00

30 lines
1.2 KiB
Clojure

(ns emptyhead.newlib.math.arithmetic
"Airthmetic operators."
(:require [emptyhead.thought.define :as def]
[emptyhead.thought.crud :as thought]
[emptyhead.newlib.message :as msg]))
;; Initial addition, e.g. =5 +=. Returns partially applied pointer.
(def/define! [:EH :PRIM [:EH :PRIM :NUM] :+]
(fn [thought parent]
(let [to (:to (thought/data parent))
pointer (-> (str "PART<" to "+_>.") gensym keyword)
th (thought/register-thought! [:EH :PRIM :PARTIAL :+] :transient false :data to)]
(msg/register-impl pointer [:EH :PRIM :NUM] th)
[parent pointer])))
;; Final addition, e.g. =(5 +) 5=. Returns a thunk.
(def/define! [:EH :PRIM :PARTIAL :+]
(fn [thought parent]
(let [from (:from (thought/data parent))
to (thought/data thought)
pointer (-> (str "LAZY<" from "+" to">.") gensym keyword)
lazy (thought/register-thought! [:EH :PRIM :LAZY :+] :transient true :data [from to])]
(msg/register-impl pointer [:EH :PRIM :REIFY] lazy)
[parent pointer])))
(def/define! [:EH :PRIM :LAZY :+]
(fn [thought parent]
(let [[from to] (thought/data thought)]
[parent (+ from to)])))