aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/lexer.clj
diff options
context:
space:
mode:
authorShagun Agrawal <agrawalshagun07@gmail.com>2024-12-24 00:05:11 +0530
committerShagun Agrawal <agrawalshagun07@gmail.com>2024-12-24 00:05:11 +0530
commit382c19861608e9ab9903c78f1e5c02bc061cc7c8 (patch)
treecbf2d16132a71b21e0f0e81f6b724685dcb242aa /src/cljcc/lexer.clj
parentb50b3552de7e0e6bf71d78e59adec5e305d7618b (diff)
Add driver changes and float regex
Diffstat (limited to 'src/cljcc/lexer.clj')
-rw-r--r--src/cljcc/lexer.clj24
1 files changed, 8 insertions, 16 deletions
diff --git a/src/cljcc/lexer.clj b/src/cljcc/lexer.clj
index 61022d9..3651d16 100644
--- a/src/cljcc/lexer.clj
+++ b/src/cljcc/lexer.clj
@@ -11,44 +11,37 @@
(defn lex
([source]
- (lex source 0 (lexer-ctx)))
- ([[ch pk th :as source] pos {:keys [line col] :as ctx}]
+ (lex source (lexer-ctx)))
+ ([[ch pk th :as source] {:keys [line col] :as ctx}]
(cond
(empty? source) (update ctx :tokens #(conj % (t/create :eof line col)))
(newline? ch) (recur (next source)
- (+ pos 1)
(-> ctx
(update :line inc)
(update :col (fn [_] 1))))
(contains?
t/chrs-kind-map (str ch pk th)) (recur (next (next (next source)))
- (+ pos 3)
(-> ctx
(update :col #(+ % 3))
(update :tokens #(conj % (t/create (get t/chrs-kind-map (str ch pk th)) line col)))))
(contains?
t/chrs-kind-map (str ch pk)) (recur (next (next source))
- (+ pos 2)
(-> ctx
(update :col #(+ % 2))
(update :tokens #(conj % (t/create (get t/chrs-kind-map (str ch pk)) line col)))))
(contains?
t/chrs-kind-map ch) (recur (next source)
- (+ pos 1)
(-> ctx
(update :col inc)
(update :tokens #(conj % (t/create (get t/chrs-kind-map ch) line col)))))
(whitespace? ch) (recur (next source)
- (+ pos 1)
(-> ctx
(update :col inc)))
(digit? ch) (let [[chrs rst] (split-with letter-digit? source)
number (read-number (apply str chrs) line col)
cnt (count chrs)
- npos (+ pos cnt)
token (t/create :number line col number)]
(recur (apply str rst)
- npos
(-> ctx
(update :col #(+ % cnt))
(update :tokens #(conj % token)))))
@@ -58,19 +51,18 @@
kind (t/identifier->kind lexeme)
token (if (= :identifier kind)
(t/create kind line col lexeme)
- (t/create kind line col))
- npos (+ pos cnt)]
- (recur (apply str rst) npos (-> ctx
- (update :col #(+ % cnt))
- (update :tokens #(conj % token)))))
+ (t/create kind line col))]
+ (recur (apply str rst) (-> ctx
+ (update :col #(+ % cnt))
+ (update :tokens #(conj % token)))))
:else (exc/lex-error {:line line :col col}))))
(comment
(lex
"
-int main() {
- long unsigned a = 110lu;
+int main(void) {
+ return 42;
}
")