From 382c19861608e9ab9903c78f1e5c02bc061cc7c8 Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Tue, 24 Dec 2024 00:05:11 +0530 Subject: Add driver changes and float regex --- src/cljcc/lexer.clj | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'src/cljcc/lexer.clj') 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; } ") -- cgit v1.2.3