From 276b0c200e5159b1d099ff85aab544480c2ac757 Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Fri, 30 Aug 2024 18:49:17 +0530 Subject: Add compound assignment operators Added compound assignment operators ( >>==, += etc ) Pass chapter 5 extra credit tests --- src/cljcc/lexer.clj | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/cljcc/lexer.clj') diff --git a/src/cljcc/lexer.clj b/src/cljcc/lexer.clj index 8525441..f074ab2 100644 --- a/src/cljcc/lexer.clj +++ b/src/cljcc/lexer.clj @@ -12,7 +12,7 @@ (defn lex ([source] (lex source 0 (lexer-ctx))) - ([[ch pk :as source] pos {:keys [line col] :as ctx}] + ([[ch pk th :as source] pos {:keys [line col] :as ctx}] (cond (empty? source) (update ctx :tokens #(conj % (t/create :eof line col))) (newline? ch) (recur (next source) @@ -20,6 +20,12 @@ (-> 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) @@ -66,6 +72,6 @@ }" (pp/pprint - (lex "int main(void) {return 2;}")) + (lex "int main(void) {return int a = 2; a <<= 2;}")) ()) -- cgit v1.2.3