Skip to content

Commit

Permalink
refactor: renaming parser
Browse files Browse the repository at this point in the history
  • Loading branch information
BRonen committed Mar 30, 2024
1 parent 47a4d91 commit e4282f6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/bronen/kekwisp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

(defn quote? [s] (not= s \"))

; Lexer

(defn lexer-literal
"Tokenize a literal that begins with a non-digit until finds whitespace and returns the rest"
[chars]
Expand Down Expand Up @@ -36,6 +38,8 @@
:else (let [[rst token] (lexer-literal chars)]
(recur rst (conj acc token))))))

; Parser

(defn parse-literal
[token _]
(conj token {:value (apply str (:value token))}))
Expand All @@ -52,20 +56,22 @@
(conj token {:value (apply str (:value token))})
(parse-number token tokens)))

(declare parse-expression)
(declare parse)

(defn parse-list
[token tokens]
(if (= (:token token) "lbraces")
(let [elems (take-while #(not= (:token %) "rbraces") tokens)]
{:token "list" :value (map #(parse-expression [%]) elems)})
{:token "list" :value (map #(parse [%]) elems)})
(parse-string token tokens)))

(defn parse-expression
(defn parse
"Parses tokens to syntax tree"
[[token & tokens]]
(parse-list token tokens))

; Evaluation

(def default-context
{"def" (fn [v] {:token "definition" :value v})
"fn" (fn [v] {:token "function" :value v})
Expand Down
28 changes: 14 additions & 14 deletions test/bronen/kekwisp_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns bronen.kekwisp-test
(:require [clojure.test :refer [deftest testing is]]
[bronen.kekwisp :refer [lexer parse-expression, evaluate]]))
[bronen.kekwisp :refer [lexer parse evaluate]]))

(deftest lexer-test
(testing "Should tokenize a string into valid tokens"
Expand Down Expand Up @@ -30,17 +30,17 @@

(deftest parser-test
(testing "Should parse syntax tokens into a valid syntax tree"
(is (= (parse-expression [{:token "literal" :value '(\p \r \i \n \t \l \n)}])
(is (= (parse [{:token "literal" :value '(\p \r \i \n \t \l \n)}])
{:token "literal" :value "println"}))
(is (= (parse-expression [{:token "number" :value '(\5)}])
(is (= (parse [{:token "number" :value '(\5)}])
{:token "number" :value 5}))
(is (= (parse-expression [{:token "string" :value '(\l \o \r \e \m \space \i \p \s \u \m)}])
(is (= (parse [{:token "string" :value '(\l \o \r \e \m \space \i \p \s \u \m)}])
{:token "string" :value "lorem ipsum"}))
(is (= (parse-expression [{:token "lbraces"}
{:token "literal" :value '(\p \r \i \n \t \l \n)}
{:token "number" :value '(\5)}
{:token "string" :value '(\l \o \r \e \m \space \i \p \s \u \m)}
{:token "rbraces"}])
(is (= (parse [{:token "lbraces"}
{:token "literal" :value '(\p \r \i \n \t \l \n)}
{:token "number" :value '(\5)}
{:token "string" :value '(\l \o \r \e \m \space \i \p \s \u \m)}
{:token "rbraces"}])
{:token "list"
:value [{:token "literal" :value "println"}
{:token "number" :value 5}
Expand All @@ -65,34 +65,34 @@
(testing "Should evaluate a string and return the result"
(is (= (-> "example"
(lexer)
(parse-expression)
(parse)
(#(evaluate % (atom {"example" 123}))))
123))
(is (= (-> "(+ 1 2 3)"
(lexer)
(parse-expression)
(parse)
(evaluate))
6)))
(testing "Should evaluate a string, return the result and mutate the context"
(let [ctx (atom {})]
(is (= (-> "(def wasd 123 444)"
(lexer)
(parse-expression)
(parse)
(#(evaluate % ctx)))
444))
(is (= @ctx
{"wasd" 123})))))
;(let [ctx (atom {})]
;(is (= (-> "(def wasd 123 (def ddd 444))"
;(lexer)
;(parse-expression)
;(parse)
;(#(evaluate % ctx)))
;nil))
;(is (= @ctx
;{"wasd" 123
;"ddd" 444})))))

;(is (= (parse-expression [{:token "lbraces"}
;(is (= (parse [{:token "lbraces"}
; {:token "string" :value "lorem ipsum"}
; {:token "lbraces"}
; {:token "lbraces"}
Expand Down

0 comments on commit e4282f6

Please sign in to comment.