From 90b8a837789eb99c44bcea9abee53012f2df71a4 Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Sun, 15 Dec 2024 10:26:36 +0530 Subject: Add lexer for unsigned int / long --- src/cljcc/lexer.clj | 4 ++-- src/cljcc/token.clj | 6 +++++- src/cljcc/util.clj | 10 ++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/cljcc/lexer.clj b/src/cljcc/lexer.clj index d092ea6..61022d9 100644 --- a/src/cljcc/lexer.clj +++ b/src/cljcc/lexer.clj @@ -70,8 +70,8 @@ (lex " int main() { - long a = 110; + long unsigned a = 110lu; } ") - ()) + ()) diff --git a/src/cljcc/token.clj b/src/cljcc/token.clj index 3eaa505..6796c0b 100644 --- a/src/cljcc/token.clj +++ b/src/cljcc/token.clj @@ -53,7 +53,9 @@ :kw-return :kw-int :kw-long - :kw-void}) + :kw-void + :kw-signed + :kw-unsigned}) (def unary-ops #{:logical-not @@ -201,6 +203,8 @@ "continue" :kw-continue "static" :kw-static "extern" :kw-extern + "signed" :kw-signed + "unsigned" :kw-unsigned :identifier)) (defn create diff --git a/src/cljcc/util.clj b/src/cljcc/util.clj index d851a62..f75dd5f 100644 --- a/src/cljcc/util.clj +++ b/src/cljcc/util.clj @@ -91,13 +91,19 @@ (catch Exception _e false))) +(defn- matches-regex [re s] + (not (nil? (re-matches re s)))) + (defn read-number - "Returns number and number type tuple. + "Returns number in string form. Checks whether number is valid long. If no, checks if it valid int. Otherwise error." [s line col] - (if-let [s (valid-long? s)] + (if-let [_ (or (matches-regex #"[0-9]+" s) + (matches-regex #"[0-9]+[lL]" s) + (matches-regex #"[0-9]+[uU]" s) + (matches-regex #"[0-9]+([lL][uU]|[uU][lL])" s))] s (exc/lex-error {:line line :col col}))) -- cgit v1.2.3