Skip to content

Commit

Permalink
Add subtraction and division
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 24, 2024
1 parent f594fa1 commit 2dc3bab
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/bean/errors.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
{:error "Matrices should be same size."
:representation "Matrices should be same size."})

(defn divide-by-zero []
{:error "can't divide by zero"
:representation "can't divide by zero"})

(defn type-mismatch-+-op []
{:error "+ only works for Integers"
:representation "+ only works for Integers"})
Expand Down
4 changes: 3 additions & 1 deletion src/bean/interpreter.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@
(eval-sub-ast right)))
(eval-sub-ast arg))
:Value (eval-sub-ast arg)
:Integer (ast-result (js/parseInt arg))
:Number (ast-result (js/Number.parseFloat arg))
:String (ast-result arg)
:QuotedString (ast-result arg)
:Operation (ast-result (case arg
"+" operators/bean-op-+
"-" operators/bean-op-minus
"/" operators/bean-op-div
"*" operators/bean-op-*
"<" operators/bean-op-<
">" operators/bean-op->
Expand Down
29 changes: 23 additions & 6 deletions src/bean/operators.cljs
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
(ns bean.operators
(:require [bean.errors :as errors]))

;; This is a hack to work around JS's float weirdnesses.
(defn format-number
[num]
(if (.isInteger js/Number num) num (js/Number (.toFixed num 4))))

(defn bean-op-+ [left right]
(if (and (int? left) (int? right))
(if (and (number? left) (number? right))
(+ left right)
(errors/type-mismatch-+-op)))

(defn bean-op-minus [left right]
(if (and (number? left) (number? right))
(format-number (- left right))
(errors/type-mismatch-+-op)))

(defn bean-op-div [left right]
(if (and (number? left) (number? right))
(if (zero? right)
(errors/divide-by-zero)
(format-number (/ left right)))
(errors/type-mismatch-+-op)))

(defn bean-op-< [left right]
(if (and (int? left) (int? right))
(if (and (number? left) (number? right))
(< left right)
(errors/type-mismatch-<-op)))

(defn bean-op-> [left right]
(if (and (int? left) (int? right))
(if (and (number? left) (number? right))
(> left right)
(errors/type-mismatch->-op)))

(defn bean-op-= [left right]
(if (and (or (string? left)
(int? left))
(number? left))
(or (string? right)
(int? right)))
(number? right)))
(= left right)
(errors/type-mismatch-=-op)))

(defn bean-op-* [left right]
(if (and (int? left) (int? right))
(if (and (number? left) (number? right))
(* left right)
(errors/type-mismatch-*-op)))
8 changes: 4 additions & 4 deletions src/bean/parser/parser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
;; TODO: Integers are currently just natural numbers
"
CellContents = <'='> Expression / RawValue / Epsilon
<RawValue> = Integer / String
Integer = #'[0-9]+'
<RawValue> = Number / String
Number = #'[-]?[0-9]+([.][0-9]+)?'
String = #'.*'
CellRef = #'[A-Z]+' #'[1-9][0-9]*'
MatrixRef = CellRef <':'> CellRef
Operation = '+' | '*' | '=' | '<' | '>'
Operation = '+' | '*' | '=' | '<' | '>' | '/' | '-'
Expression = (Value | CellRef | MatrixRef | FunctionChain |
Expression Operation Expression | FunctionInvocation |
FunctionDefinition | FrameLookup) / Name
Expand All @@ -32,7 +32,7 @@
FrameLookup = <'$'> Name
LabelLookup = Name
Value = Integer / <'\"'> QuotedString <'\"'>
Value = Number / <'\"'> QuotedString <'\"'>
QuotedString = #'[^\"]+'
")

Expand Down

0 comments on commit 2dc3bab

Please sign in to comment.