Skip to content

Commit

Permalink
refactor: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brenno Rodrigues committed Apr 13, 2024
1 parent 9327df2 commit 7a66f3d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 67 deletions.
28 changes: 14 additions & 14 deletions src/bronen/kekwisp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,26 @@

(def default-context
"A default map to look for default values"
{"list" {:node :list}
"map" {:node :map}
"get" {:node :get}
"fold" {:node :fold}
"print" {:node :print}
"def" {:node :definition}
"fn" {:node :function}
"do" {:node :do}
"if" {:node :conditional}
"+" {:node :sum}
"-" {:node :subtraction}
"*" {:node :multiplication}
"/" {:node :division}})
{"list" :list
"map" :map
"get" :get
"fold" :fold
"print" :print
"def" :definition
"fn" :function
"do" :do
"if" :conditional
"+" :sum
"-" :subtraction
"*" :multiplication
"/" :division})

(defn eval-literal
[{_ :node value :value} ctx]
(let [default-value (get default-context value)]
(if (nil? default-value)
(get @ctx value)
default-value)))
{:node default-value})))

(declare evaluate)

Expand Down
106 changes: 53 additions & 53 deletions test/bronen/kekwisp_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -190,56 +190,56 @@
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [{:node :list :value [1 2 3]} 4 5 6]})))
(testing "Should evaluate lists calls"
(let [ctx (atom {})]
(is (= (-> "(list 1 2 3)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [1 2 3]})))
(let [ctx (atom {})]
(is (= (-> "(list (list 1 2 3) 4 5 6)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [{:node :list :value [1 2 3]} 4 5 6]}))))
(testing "Should evaluate map calls"
(let [ctx (atom {})]
(is (= (-> "(map (fn (a) 8) (list 1 2 3))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [8 8 8]}))
(is (= (-> "(map (fn (a) (* a 2)) (list 1 2 3))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [2 4 6]}))))
(testing "Should evaluate fold calls"
(let [ctx (atom {})]
(is (= (-> "(fold (fn (acc v) (+ acc v)) 0 (list 1 2 3))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
6)))
(let [ctx (atom {})]
(is (= (-> "(fold (fn (acc v) (+ acc v)) 0 (map (fn (v) (* v 2)) (list 1 2 3)))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
12))))
(testing "Should evaluate get calls"
(let [ctx (atom {})]
(is (= (-> "(get (list 1 2 3) 2)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
3))))))
{:node :list :value [{:node :list :value [1 2 3]} 4 5 6]}))))
(testing "Should evaluate lists calls"
(let [ctx (atom {})]
(is (= (-> "(list 1 2 3)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [1 2 3]})))
(let [ctx (atom {})]
(is (= (-> "(list (list 1 2 3) 4 5 6)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [{:node :list :value [1 2 3]} 4 5 6]}))))
(testing "Should evaluate map calls"
(let [ctx (atom {})]
(is (= (-> "(map (fn (a) 8) (list 1 2 3))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [8 8 8]}))
(is (= (-> "(map (fn (a) (* a 2)) (list 1 2 3))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
{:node :list :value [2 4 6]}))))
(testing "Should evaluate fold calls"
(let [ctx (atom {})]
(is (= (-> "(fold (fn (acc v) (+ acc v)) 0 (list 1 2 3))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
6)))
(let [ctx (atom {})]
(is (= (-> "(fold (fn (acc v) (+ acc v)) 0 (map (fn (v) (* v 2)) (list 1 2 3)))"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
12))))
(testing "Should evaluate get calls"
(let [ctx (atom {})]
(is (= (-> "(get (list 1 2 3) 2)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
3)))))

0 comments on commit 7a66f3d

Please sign in to comment.