Skip to content

Commit

Permalink
feat: adding conditionals and booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
BRonen committed Mar 31, 2024
1 parent 0edbeb8 commit e73cfc4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/bronen/kekwisp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@
[token tokens]
[(conj token {:value (apply str (:value token))}) tokens])

(defn parse-bool
[token tokens]
(case (:value token)
[\t \r \u \e] [{:token "boolean" :value true} tokens]
[\f \a \l \s \e] [{:token "boolean" :value false} tokens]
(parse-literal token tokens)))

(defn parse-number
[token tokens]
(if (= (:token token) "number")
[(conj token {:value (Integer/parseInt (apply str (:value token)))}) tokens]
(parse-literal token tokens)))
(parse-bool token tokens)))

(defn parse-string
[token tokens]
Expand Down Expand Up @@ -83,10 +90,11 @@
; Evaluation

(def default-context
{"print" (fn [v] {:token "print" :value v})
{"print" (fn [v] {:token "print" :value v})
"def" (fn [v] {:token "definition" :value v})
"fn" (fn [v] {:token "function" :value v})
"do" (fn [v] {:token "do" :value v})
"if" (fn [v] {:token "conditional" :value v})
"+" (fn [v] {:token "sum" :value v})
"-" (fn [v] {:token "subtraction" :value v})
"*" (fn [v] {:token "multiplication" :value v})
Expand All @@ -112,6 +120,7 @@
"division" (apply / (map #(evaluate % ctx) (drop 1 values)))
"print" (let [result (doall (map #(evaluate % ctx) (drop 1 values)))] (println result) result)
"do" (last (doall (map #(evaluate % ctx) (drop 1 values))))
"conditional" (if (evaluate (nth values 1)) (evaluate (nth values 2)) (evaluate (nth values 3)))
"definition" (do (swap! ctx #(conj % {(:value (nth values 1))
(evaluate (nth values 2))}))
(when (nth values 3 false) (evaluate (nth values 3) ctx)))
Expand All @@ -131,4 +140,5 @@
"list" (eval-list value ctx)
"string" value
"number" value
"boolean" value
"literal" (eval-literal {:token token :value value} ctx))))
42 changes: 40 additions & 2 deletions test/bronen/kekwisp_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
[{:token "string" :value '(\l \o \r \e \m)}]))
(is (= (lexer "println")
[{:token "literal" :value '(\p \r \i \n \t \l \n)}]))
(is (= (lexer "true")
[{:token "literal" :value '(\t \r \u \e)}]))
(is (= (lexer "false")
[{:token "literal" :value '(\f \a \l \s \e)}]))
(is (= (lexer "()")
[{:token "lbraces"} {:token "rbraces"}]))
(is (= (lexer ")(")
Expand All @@ -36,6 +40,10 @@
[{:token "number" :value 5} nil]))
(is (= (parse [{:token "string" :value '(\l \o \r \e \m \space \i \p \s \u \m)}])
[{:token "string" :value "lorem ipsum"} nil]))
(is (= (parse [{:token "literal" :value '(\t \r \u \e)}])
[{:token "boolean" :value true} nil]))
(is (= (parse [{:token "literal" :value '(\f \a \l \s \e)}])
[{:token "boolean" :value false} nil]))
(is (= (parse [{:token "lbraces"}
{:token "literal" :value '(\p \r \i \n \t \l \n)}
{:token "number" :value '(\5)}
Expand Down Expand Up @@ -67,7 +75,9 @@
(testing "Should evaluate a syntax tree and return a value"
(is (= (evaluate {:token "literal" :value "example"} (atom {"example" 123})) 123))
(is (= (evaluate {:token "number" :value 5} (atom {})) 5))
(is (= (evaluate {:token "string" :value "lorem ipsum"} (atom {})) "lorem ipsum")))
(is (= (evaluate {:token "string" :value "lorem ipsum"} (atom {})) "lorem ipsum"))
(is (= (evaluate {:token "boolean" :value true} (atom {})) true))
(is (= (evaluate {:token "boolean" :value false} (atom {})) false)))
(testing "Should evaluate a syntax tree and mutate the original context"
(let [ctx (atom {"example" 123})]
(evaluate
Expand All @@ -86,6 +96,20 @@
(first)
(#(evaluate % (atom {"example" 123}))))
123))
(let [ctx (atom {})]
(is (= (-> "true"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
true)))
(let [ctx (atom {})]
(is (= (-> "false"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
false)))
(is (= (-> "(+ 1 2 3)"
(lexer)
(parse)
Expand Down Expand Up @@ -137,4 +161,18 @@
(parse)
(first)
(#(evaluate % ctx)))
67)))))
67)))
(let [ctx (atom {})]
(is (= (-> "(if true 123 444)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
123)))
(let [ctx (atom {})]
(is (= (-> "(if false 123 444)"
(lexer)
(parse)
(first)
(#(evaluate % ctx)))
444)))))

0 comments on commit e73cfc4

Please sign in to comment.