Skip to content

Commit

Permalink
WIP Shorthand syntax for frame and label lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 23, 2024
1 parent 2aceb75 commit 8d747e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
23 changes: 20 additions & 3 deletions src/bean/functions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
(let [[expression-type] ast]
(= expression-type :CellRef)))

(defn quoted-string? [[_ ast]]
(let [[expression-type] ast]
(= expression-type :QuotedString)))

;; Functions don't work for matrices,
;; they need the thing as apply-op does
(defn bean-concat [_sheet args]
Expand Down Expand Up @@ -112,7 +116,8 @@
(bean-get* sheet args asts :left))

(defn bean-frame [sheet args asts]
(if (cell-ref? (first asts))
(cond
(cell-ref? (first asts))
(let [[_ [_ a n]] (first asts)
address (util/a1->rc a (js/parseInt n))
frame-name (frames/cell-frame address sheet)
Expand All @@ -126,8 +131,20 @@
(select-keys frame [:start :end]))
:selection-dirn nil}}
(errors/undefined-frame-at (str a n))))
(errors/invalid-frame-args
(str (:scalar (first args))))))
(quoted-string? (first asts))
(let [[_ [_ frame-name]] (first asts)
frame (frames/get-frame sheet frame-name)]
(if frame
{:matrix (interpreter/eval-matrix (:start frame)
(:end frame)
(:grid sheet))
:frame {:name frame-name
:selection (area/area->addresses
(select-keys frame [:start :end]))
:selection-dirn nil}}
(errors/undefined-frame-at frame-name)))
:else (errors/invalid-frame-args
(str (:scalar (first args))))))

(defn bean-error [_sheet args]
(let [str-err (str (:error (first args)))]
Expand Down
17 changes: 15 additions & 2 deletions src/bean/interpreter.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,22 @@
:FunctionInvocation (apply-f sheet
(eval-sub-ast arg)
(rest args))
:FunctionChain (let [[expr [_ fn-name & fn-args]] args]
:FrameLookup (let [[_ frame-name] arg]
(eval-sub-ast
[:FunctionInvocation
[:Name "frame"]
[:Expression [:QuotedString frame-name]]]))
:FunctionChain (let [[expr [node-type*
[_ name* :as name-node]
& fn-args]] args]
(eval-sub-ast
(concat [:FunctionInvocation fn-name expr] fn-args)))
(case node-type*
:LabelLookup
[:FunctionInvocation
[:Name "get"] expr
[:Expression [:Value [:QuotedString name*]]]]
:FunctionInvocation
(concat [:FunctionInvocation name-node expr] fn-args))))
:Expression (if (util/is-expression? arg)
(let [[left op right] args]
(apply-op (eval-sub-ast op)
Expand Down
11 changes: 8 additions & 3 deletions src/bean/parser/parser.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
MatrixRef = CellRef <':'> CellRef
Operation = '+' | '*' | '=' | '<' | '>'
Expression = Value | CellRef | MatrixRef | FunctionChain | Expression Operation Expression | FunctionInvocation | FunctionDefinition | Name
Expression = (Value | CellRef | MatrixRef | FunctionChain |
Expression Operation Expression | FunctionInvocation |
FunctionDefinition | FrameLookup) / Name
FunctionInvocation = (FunctionDefinition | Name) <'('> [Expression {<' '> Expression}] <')'>
FunctionDefinition = <'{'> Expression <'}'>
FunctionChain = Expression <'.'> FunctionInvocation
FunctionChain = Expression [<'.'> (FunctionInvocation | LabelLookup)]
Name = #'[a-z]+'
Name = #'[a-zA-Z0-9 ]+(?<! )'
FrameLookup = <'$'> Name
LabelLookup = Name
Value = Integer / <'\"'> QuotedString <'\"'>
QuotedString = #'[^\"]+'
Expand Down

0 comments on commit 8d747e6

Please sign in to comment.