Skip to content

Commit

Permalink
Move from :value -> :scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
neenaoffline committed Nov 21, 2023
1 parent f6dfbc0 commit 5ffcf77
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 207 deletions.
4 changes: 2 additions & 2 deletions src/bean/functions.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns bean.functions)

(defn bean-concat [params]
(let [concated-str (reduce (fn [c {:keys [value]}] (str c value))
(let [concated-str (reduce (fn [c {:keys [scalar]}] (str c scalar))
""
params)]
{:value concated-str
{:scalar concated-str
:representation concated-str}))
6 changes: 3 additions & 3 deletions src/bean/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(defn- clear-spilled-cell [cell address]
(-> cell
errors/reset
(dissoc :value :spilled-from)
(dissoc :scalar :spilled-from)
(update :interested-spillers disj address)
(assoc :representation "")))

Expand Down Expand Up @@ -51,7 +51,7 @@
:spilled-from address
:error (:error %2)
:representation (:representation %2)
:value (:value %2)}
:scalar (:scalar %2)}
(= %1 [0 0]) (merge {:matrix matrix
:content (:content cell)
:ast (:ast cell)})))
Expand Down Expand Up @@ -254,7 +254,7 @@
:ast nil

;; Evaluation results
:value nil
:scalar nil
:representation nil
:error nil
:matrix nil
Expand Down
32 changes: 16 additions & 16 deletions src/bean/interpreter.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
(defn- ast-result [error-or-val]
(if-let [error (:error error-or-val)]
(errors/stringified-error error)
{:value error-or-val
{:scalar error-or-val
:representation (str error-or-val)}))

(defn- fn-result [f]
{:value f
{:scalar f
:representation ""})

(defn- cell->ast-result [cell]
(select-keys cell [:value :error :representation]))
(select-keys cell [:scalar :error :representation]))

(defn- ast-result->cell [{:keys [error matrix] :as ast-result} cell]
(merge
{:content (:content cell)
:ast (:ast cell)
:value (:value ast-result)
:scalar (:scalar ast-result)
:representation (:representation ast-result)}
(when matrix {:matrix matrix})
(when error {:error error})))
Expand All @@ -34,15 +34,15 @@
#(util/get-cell grid %)
(util/addresses-matrix start-address end-address)))

(defn- apply-values
(defn- apply-results
([f ast-results]
(if-let [referenced-error (first-error ast-results)]
referenced-error
(ast-result (apply f (map :value ast-results)))))
(ast-result (apply f (map :scalar ast-results)))))
([f ast-results matrix]
{:matrix
(util/map-on-matrix
#(apply-values f (conj ast-results %))
#(apply-results f (conj ast-results %))
matrix)}))

(defn- dim [matrix]
Expand All @@ -53,7 +53,7 @@
{:matrix
(mapv (partial mapv
(fn [l-el r-el]
(apply-values
(apply-results
#(apply %1 [%2 %3])
[op l-el r-el])))
lmatrix
Expand All @@ -65,18 +65,18 @@
rmatrix (:matrix right)]
(cond
(and lmatrix rmatrix) (matrix-op-matrix lmatrix op rmatrix)
lmatrix (apply-values #(apply %1 [%2 %3]) [op right] lmatrix)
rmatrix (apply-values #(apply %1 [%3 %2]) [op left] rmatrix)
:else (apply-values #(apply %1 [%2 %3]) [op left right]))))
lmatrix (apply-results #(apply %1 [%2 %3]) [op right] lmatrix)
rmatrix (apply-results #(apply %1 [%3 %2]) [op left] rmatrix)
:else (apply-results #(apply %1 [%2 %3]) [op left right]))))

(def global-ctx
{"concat" {:value functions/bean-concat
{"concat" {:scalar functions/bean-concat
:representation "f"}})

(declare apply-f)

(defn eval-ast [ast {:keys [grid bindings] :as sheet}]
; ast goes down, value or an error comes up
; ast goes down, result or an error comes up
(let [[node-type & [arg :as args]] ast
eval-sub-ast #(eval-ast % sheet)
eval-matrix* #(eval-matrix %1 %2 grid)]
Expand Down Expand Up @@ -115,9 +115,9 @@
"*" operators/bean-op-*)))))

(defn- apply-f [sheet f params]
(if (fn? (:value f))
((:value f) params)
(eval-ast (:value f)
(if (fn? (:scalar f))
((:scalar f) params)
(eval-ast (:scalar f)
(update-in sheet
[:bindings]
#(merge % (into {} (map vector ["x" "y" "z"] params)))))))
Expand Down
34 changes: 17 additions & 17 deletions src/bean/provenance.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
[bean.interpreter :as interpreter]
[bean.errors :as errors]))

(defn- value-proof [v & args]
(defn- scalar-proof [v & args]
(->> args
(concat [:value v])
(concat [:scalar v])
(into [])))

(defn- self-evident [value]
(value-proof value :self-evident))
(defn- self-evident [scalar]
(scalar-proof scalar :self-evident))

(defn combine-op-proof [ast-result lproof op rproof]
(let [value (:value ast-result)
(let [scalar (:scalar ast-result)
matrix (:matrix ast-result)
lmatrix (:matrix lproof)
rmatrix (:matrix rproof)]
(cond
(and lmatrix rmatrix) {:matrix
(util/map-on-matrix-addressed
#(value-proof (:value %2) (get-in lmatrix %1) op (get-in rmatrix %1))
#(scalar-proof (:scalar %2) (get-in lmatrix %1) op (get-in rmatrix %1))
matrix)}
lmatrix {:matrix
(util/map-on-matrix-addressed
#(value-proof (get-in matrix (conj %1 :value)) %2 op rproof)
#(scalar-proof (get-in matrix (conj %1 :scalar)) %2 op rproof)
lmatrix)}
rmatrix {:matrix (util/map-on-matrix-addressed
#(value-proof (get-in matrix (conj %1 :value)) lproof op %2)
#(scalar-proof (get-in matrix (conj %1 :scalar)) lproof op %2)
rmatrix)}
:else (value-proof value lproof op rproof))))
:else (scalar-proof scalar lproof op rproof))))

(declare cell-proof)

(defn ast-proof [ast sheet]
;; ast goes down, proof come up
(let [[node-type & [arg :as args]] ast
sub-ast-proof #(ast-proof % sheet)
ast-result #(interpreter/eval-ast ast sheet)
ast-value #(:value (ast-result))]
value #(interpreter/eval-ast ast sheet)
scalar #(:scalar (value))]
(case node-type
:CellContents (if arg
(sub-ast-proof arg) [])
Expand All @@ -51,15 +51,15 @@
:Expression (if (util/is-expression? arg)
(let [[left op right] args]
(combine-op-proof
(ast-result)
(value)
(sub-ast-proof left)
(sub-ast-proof op)
(sub-ast-proof right)))
(sub-ast-proof arg))
:Value (sub-ast-proof arg)
:Integer (self-evident (ast-value))
:String (self-evident (ast-value))
:QuotedString (self-evident (ast-value))
:Integer (self-evident (scalar))
:String (self-evident (scalar))
:QuotedString (self-evident (scalar))
:Operation (self-evident arg))))

(defn- spilled-cell-proof [address cell {:keys [grid] :as sheet}]
Expand All @@ -69,7 +69,7 @@
{:spilled-from (:spilled-from cell)
:content (:content spilled-from)
:address address
:value (:value cell)
:scalar (:scalar cell)
:relative-address (:relative-address cell)}]
(get-in
(:matrix (ast-proof (:ast spilled-from) sheet))
Expand All @@ -87,5 +87,5 @@
[:cell-ref
{:address address
:content (:content cell)
:value (:value cell)}
:scalar (:scalar cell)}
(ast-proof (:ast cell) sheet)]))))
2 changes: 1 addition & 1 deletion src/bean/ui/sheet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
:on-blur (fn [e]
(set-mode [row col] :view)
(let [input (.-textContent (.-target e))]
(set! (.-innerHTML (.-target e)) (:value cell))
(set! (.-innerHTML (.-target e)) (:scalar cell))
(update-cell [row col] input)))
:class (cs :bean-cell
(when (= mode :edit) :edit-mode)
Expand Down
6 changes: 3 additions & 3 deletions test/bean/deps_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(get-in grid [0 1])
{:content "=A1"
:ast [:CellContents [:Expression [:CellRef "A" "1"]]]
:value 1
:scalar 1
:representation "1"})))))

(testing "Adds an edge to the depgraph to a node that already has a dependency"
Expand All @@ -25,7 +25,7 @@
(get-in grid [0 1])
{:content "=A1"
:ast [:CellContents [:Expression [:CellRef "A" "1"]]]
:value 1
:scalar 1
:representation "1"})))))

(testing "Removes a node from the depgraph if it has no edges after update"
Expand All @@ -37,5 +37,5 @@
(get-in grid [0 1])
{:content "2"
:ast [:CellContents [:Integer "2"]]
:value 2
:scalar 2
:representation "2"}))))))
Loading

0 comments on commit 5ffcf77

Please sign in to comment.