From 7db3c0405a7fc2895095e91abe910626aee2bc5d Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Fri, 16 Aug 2024 23:40:18 +0530 Subject: Add lexer implementation for ch2 Add lexer implementation Add reference for base implementation of the lexer --- README.md | 3 ++- src/cljcc/lexer.clj | 10 +++++++++- src/cljcc/token.clj | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 012f4a1..625cb9c 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,5 @@ bb run-main "path/to/file.c" Some talks / projects which helped in implementation. * [What's So Hard About Writing A Compiler, Anyway? Oh - Ramsey Nasser](https://www.youtube.com/watch?v=_7sncBhluXI) - +* [Clojure Lexer implementation by github.com/Vikasg7](https://github.com/ThePrimeagen/ts-rust-zig-deez/tree/master/clj) + diff --git a/src/cljcc/lexer.clj b/src/cljcc/lexer.clj index 10742f0..9922887 100644 --- a/src/cljcc/lexer.clj +++ b/src/cljcc/lexer.clj @@ -9,10 +9,12 @@ :line 1 :col 1}) +(str \1 nil) + (defn lex ([source] (lex source 0 (lexer-ctx))) - ([[ch :as source] pos {:keys [line col] :as ctx}] + ([[ch pk :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 +22,12 @@ (-> ctx (update :line inc) (update :col (fn [_] 1)))) + (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) diff --git a/src/cljcc/token.clj b/src/cljcc/token.clj index 6df4f43..63c1eda 100644 --- a/src/cljcc/token.clj +++ b/src/cljcc/token.clj @@ -47,7 +47,8 @@ "++" :increment \; :semicolon \+ :plus - \- :minus + \- :hyphen + \~ :complement \* :multiply \% :remainder \/ :divide}) -- cgit v1.2.3