From 8fe495a61e4a85be1934e5205a6f036900ee45d0 Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Thu, 19 Sep 2024 22:54:27 +0530 Subject: Add driver option and lexing stage for functions Ch 9 --- src/cljcc/cljcc.clj | 2 ++ src/cljcc/driver.clj | 8 +++++--- src/cljcc/token.clj | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cljcc/cljcc.clj b/src/cljcc/cljcc.clj index f63ebed..3aeea48 100644 --- a/src/cljcc/cljcc.clj +++ b/src/cljcc/cljcc.clj @@ -22,6 +22,8 @@ [nil "--validate" "Runs semantic analyzer. Does not emit any files."] [nil "--tacky" "Runs tacky generation. Does not emit any files."] [nil "--codegen" "Runs compiler. Does not emit any files."] + ["-c" nil "Generate object file." + :id :generate-object-file] ["-h" "--help"]]) (defn validate-args [args] diff --git a/src/cljcc/driver.clj b/src/cljcc/driver.clj index b7b14c1..8485574 100644 --- a/src/cljcc/driver.clj +++ b/src/cljcc/driver.clj @@ -33,7 +33,7 @@ (throw (Exception. ^String (:err output))) (log/info (str "Successfully preprocessed file: " preprocessed-file-path))))) -(defn- assemble-step [directory filename] +(defn- assemble-step [directory filename options] (let [file-without-ext (remove-extension filename) assembly-file (make-file-name directory file-without-ext "s") preprocessed-file-path (make-file-name directory (remove-extension filename) "i") @@ -45,7 +45,9 @@ _ (println assembly-output) _ (spit assembly-out-file-path assembly-output) output-file (str directory "/" file-without-ext) - output (handle-sh "gcc" assembly-file "-o" output-file)] + output (if (:generate-object-file options) + (handle-sh "gcc" "-c" assembly-file "-o" output-file) + (handle-sh "gcc" assembly-file "-o" output-file))] (if (= 1 (:exit output)) (throw (Exception. ^String (:err output))) (log/info (str "Successfully created executable at: " output-file))))) @@ -103,7 +105,7 @@ (partial semantic-analyzer-step directory filename) (partial tacky-step directory filename) (partial compiler-step directory filename) - (partial assemble-step directory filename)]] + (partial assemble-step directory filename options)]] (cond (:lex options) (subvec steps 0 3) (:parse options) (subvec steps 0 4) diff --git a/src/cljcc/token.clj b/src/cljcc/token.clj index a703373..fc62334 100644 --- a/src/cljcc/token.clj +++ b/src/cljcc/token.clj @@ -3,6 +3,7 @@ (def token-kind #{:eof :semicolon + :comma ;; brackets :left-curly @@ -132,6 +133,7 @@ \) :right-paren \? :question \: :colon + \, :comma \{ :left-curly \} :right-curly \= :assignment -- cgit v1.2.3