aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/lexer.clj
diff options
context:
space:
mode:
authorShagun Agrawal <agrawalshagun07@gmail.com>2024-12-28 23:27:58 +0530
committerShagun Agrawal <agrawalshagun07@gmail.com>2024-12-28 23:27:58 +0530
commit6a94b6abab645269c596154ce6812cbeb3811ec5 (patch)
tree1fc78a0a10221fa22b40739821f7aa3cfca58338 /src/cljcc/lexer.clj
parent8f0864f8d28a9c0693c1d89e24e6a69110d6625a (diff)
Add lexing stage for floating point numbers
Diffstat (limited to 'src/cljcc/lexer.clj')
-rw-r--r--src/cljcc/lexer.clj36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/cljcc/lexer.clj b/src/cljcc/lexer.clj
index 3651d16..d4e20d3 100644
--- a/src/cljcc/lexer.clj
+++ b/src/cljcc/lexer.clj
@@ -1,6 +1,6 @@
(ns cljcc.lexer
(:require
- [cljcc.util :refer [newline? whitespace? read-number digit? letter-digit? letter?]]
+ [cljcc.util :refer [newline? whitespace? read-number digit? letter-digit? letter? letter-digit-period?]]
[cljcc.exception :as exc]
[cljcc.token :as t]))
@@ -9,6 +9,8 @@
:line 1
:col 1})
+(set! *warn-on-reflection* true)
+
(defn lex
([source]
(lex source (lexer-ctx)))
@@ -19,6 +21,9 @@
(-> ctx
(update :line inc)
(update :col (fn [_] 1))))
+ (whitespace? ch) (recur (next source)
+ (-> ctx
+ (update :col inc)))
(contains?
t/chrs-kind-map (str ch pk th)) (recur (next (next (next source)))
(-> ctx
@@ -34,17 +39,13 @@
(-> ctx
(update :col inc)
(update :tokens #(conj % (t/create (get t/chrs-kind-map ch) line col)))))
- (whitespace? ch) (recur (next source)
- (-> 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)
- token (t/create :number line col number)]
- (recur (apply str rst)
- (-> ctx
- (update :col #(+ % cnt))
- (update :tokens #(conj % token)))))
+ (or (= \. ch) (digit? ch)) (let [[number rst] (read-number (apply str source) line col)
+ cnt (count number)
+ token (t/create :number line col number)]
+ (recur rst
+ (-> ctx
+ (update :col #(+ % cnt))
+ (update :tokens #(conj % token)))))
(letter? ch) (let [[chrs rst] (split-with letter-digit? source)
lexeme (apply str chrs)
cnt (count chrs)
@@ -59,10 +60,19 @@
(comment
+ (-> "./test-programs/example.c"
+ slurp)
+
+ (-> "./test-programs/example.c"
+ slurp
+ lex)
+
+ (lex "int x = 100;")
+
(lex
"
int main(void) {
- return 42;
+ return 2- -1;
}
")