Skip to content

Commit

Permalink
WIP lookups for names
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 23, 2024
1 parent 2aceb75 commit ef3e891
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/bean/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
;; Temporary hack until we figure out how to reval
;; frame queries
(defn eval-sheet-a-few-times [sheet]
(prn "revaling")
(let [addresses (->> (:grid sheet)
(util/map-on-matrix-addressed
(fn [address item]
Expand All @@ -244,7 +245,7 @@
(mapcat identity)
(remove nil?))]
(reduce
(fn [sheet* _] (reduce #(eval-cell %2 %1) sheet* addresses)) sheet (range 3))))
(fn [sheet* _] (reduce #(eval-cell %2 %1) sheet* addresses)) sheet (range 1))))

(defn update-cell-content [address sheet content]
(if (= (:content (util/get-cell (:grid sheet) address)) content "")
Expand Down
18 changes: 18 additions & 0 deletions src/bean/interpreter.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@

(declare apply-f)

(defn debug [x] (prn x) x)

(defn eval-ast [ast {:keys [grid bindings] :as sheet}]
; ast goes down, result or an error comes up
(let [[node-type & [arg :as args]] ast
Expand All @@ -91,6 +93,19 @@
(apply eval-matrix*))}
:Name (or (get bindings arg)
(errors/undefined-named-ref arg))
:LookupExpression (eval-sub-ast
(reduce
(fn [sub-ast [_ lookup-name]]
[:FunctionChain sub-ast
[:FunctionInvocation
[:Name "get"]
[:Expression [:Value [:QuotedString lookup-name]]]]])
[:FunctionInvocation
[:Name "frame"]
[:Expression [:CellRef "B" "2"]]] ;; use first lookup
(reverse (drop 1 args))))
;; counter example: =Frame 1.Name.row().Fav color is
;; cant mix function invocations and name lookups
:FunctionDefinition (fn-result arg)
:FunctionInvocation (apply-f sheet
(eval-sub-ast arg)
Expand Down Expand Up @@ -141,5 +156,8 @@
(apply-f-args sheet f args asts)))

(defn eval-cell [cell sheet]
;; (prn (:ast cell))
(-> (eval-ast (:ast cell) sheet)
(ast-result->cell cell)))


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 | Name) / LookupExpression
FunctionInvocation = (FunctionDefinition | Name) <'('> [Expression {<' '> Expression}] <')'>
FunctionDefinition = <'{'> Expression <'}'>
FunctionChain = Expression <'.'> FunctionInvocation
FunctionChain = Expression <'.'> FunctionInvocation
Name = #'[a-z]+'
LookupExpression = LookupName {<'.'> LookupName}
LookupName = #'[a-zA-Z0-9 ]+'
Value = Integer / <'\"'> QuotedString <'\"'>
QuotedString = #'[^\"]+'
Expand Down

0 comments on commit ef3e891

Please sign in to comment.