Skip to content

Commit

Permalink
Add * operator
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Nov 15, 2023
1 parent 1c22ac1 commit 01bbb4c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
13 changes: 5 additions & 8 deletions src/bean/interpreter.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns bean.interpreter
(:require [bean.functions :as functions]
[bean.util :as util]
[bean.errors :as errors]))
[bean.errors :as errors]
[bean.operators :as operators]))

(defn- ast-result [error-or-val]
(if-let [error (:error error-or-val)]
Expand All @@ -28,11 +29,6 @@
(defn- first-error [ast-results]
(->> ast-results (filter :error) first))

(defn bean-op-+ [left right]
(if (and (int? left) (int? right))
(+ left right)
{:error "Addition only works for Integers"}))

(defn- eval-matrix [start-address end-address grid]
(util/map-on-matrix
#(util/get-cell grid %)
Expand Down Expand Up @@ -117,7 +113,8 @@
:String (ast-result arg)
:QuotedString (ast-result arg)
:Operation (ast-result (case arg
"+" bean-op-+)))))
"+" operators/bean-op-+
"*" operators/bean-op-*)))))

(defn- apply-f [sheet f params]
(if (fn? (:value f))
Expand All @@ -129,4 +126,4 @@

(defn eval-cell [cell sheet]
(-> (eval-ast (:ast cell) sheet)
(ast-result->cell cell)))
(ast-result->cell cell)))
11 changes: 11 additions & 0 deletions src/bean/operators.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns bean.operators)

(defn bean-op-+ [left right]
(if (and (int? left) (int? right))
(+ left right)
{:error "+ only works for Integers"}))

(defn bean-op-* [left right]
(if (and (int? left) (int? right))
(* left right)
{:error "* only works for Integers"}))
4 changes: 2 additions & 2 deletions src/bean/parser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
CellRef = #'[A-Z]+' #'[1-9][0-9]*'
MatrixRef = CellRef <':'> CellRef
Operation = '+'
Operation = '+' | '*'
Expression = Value | CellRef | MatrixRef | Expression Operation Expression | FunctionInvocation | FunctionDefinition | Name
FunctionInvocation = (FunctionDefinition | Name) <'('> [Expression {<' '> Expression}] <')'>
FunctionDefinition = <'{'> Expression <'}'>
Expand Down Expand Up @@ -48,4 +48,4 @@
(defn error [result]
(when (insta/get-failure result)
(let [{:keys [index reason]} result]
(str "Parse Error: idx " index ". " reason))))
(str "Parse Error: idx " index ". " reason))))
6 changes: 3 additions & 3 deletions test/bean/grid_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
[{:content "ABC" :value "ABC" :representation "ABC"}
{:content "=A1000" :value nil :error "Invalid address [999 0]" :representation "Invalid address [999 0]"}
{:content "" :value nil :representation ""}]
[{:content "=A1+A2" :value nil :error "Addition only works for Integers" :representation "Addition only works for Integers"}
[{:content "=A1+A2" :value nil :error "+ only works for Integers" :representation "+ only works for Integers"}
{:content "=B2" :value nil :error "Invalid address [999 0]" :representation "Invalid address [999 0]"}
{:content "" :value nil :representation ""}]
[{:content "=A3+1" :value nil :error "Addition only works for Integers" :representation "Addition only works for Integers"}
[{:content "=A3+1" :value nil :error "+ only works for Integers" :representation "+ only works for Integers"}
{:content "" :value nil :representation ""}
{:content "" :value nil :representation ""}]]))))

Expand Down Expand Up @@ -308,4 +308,4 @@
(as-> (new-sheet [["1" "2"]] "addaone:4") sheet
(eval-sheet sheet)
(eval-address [:cell [0 1]] sheet "=addaone+20")
(get-in sheet [:depgraph]))))))
(get-in sheet [:depgraph]))))))
15 changes: 5 additions & 10 deletions test/bean/interpreter_test.cljs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
(ns bean.interpreter-test
(:require [bean.interpreter :refer [bean-op-+ apply-op]]
(:require [bean.interpreter :refer [apply-op]]
[bean.operators :as operators]
[clojure.test :refer [deftest testing is]]))

(deftest bean-op-+-test
(testing "Adds two numbers"
(is (= (bean-op-+ 2 3) 5)))
(testing "Returns an error if an operand is an invalid data type"
(is (= (bean-op-+ "1" 2) {:error "Addition only works for Integers"}))))

(deftest apply-op-test
(testing "Applies an op to a matrix and a scalar"
(is (= (apply-op {:value bean-op-+} {:value 1}
(is (= (apply-op {:value operators/bean-op-+} {:value 1}
{:matrix [[{:value 1}
{:value 2}
{:value 1}]]})
Expand All @@ -20,7 +15,7 @@
{:value 2 :representation "2"}]]})))

(testing "Applies an op to a scalar and a matrix"
(is (= (apply-op {:value bean-op-+}
(is (= (apply-op {:value operators/bean-op-+}
{:matrix [[{:value 1}
{:value 2}
{:value 1}]]}
Expand All @@ -31,7 +26,7 @@
{:value 2 :representation "2"}]]})))

(testing "Applies an op to a scalar and a matrix"
(is (= (apply-op {:value bean-op-+}
(is (= (apply-op {:value operators/bean-op-+}
{:matrix [[{:value 1}
{:value 2}
{:value 1}]]}
Expand Down
9 changes: 9 additions & 0 deletions test/bean/operators_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns bean.operators-test
(:require [bean.operators :refer [bean-op-+]]
[clojure.test :refer [deftest testing is]]))

(deftest bean-op-+-test
(testing "Adds two numbers"
(is (= (bean-op-+ 2 3) 5)))
(testing "Returns an error if an operand is an invalid data type"
(is (= (bean-op-+ "1" 2) {:error "+ only works for Integers"}))))

0 comments on commit 01bbb4c

Please sign in to comment.