aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/cljcc.clj
diff options
context:
space:
mode:
authorShagun Agrawal <agrawalshagun07@gmail.com>2024-07-26 00:03:57 +0530
committerShagun Agrawal <agrawalshagun07@gmail.com>2024-07-26 00:03:57 +0530
commitb6de8db464151fa1300c5a47e508e9e792d034d0 (patch)
treefcff2e637d27de228d869f884f9aa33d8cbabbb4 /src/cljcc/cljcc.clj
parent264c71e3a7666bc234a5dd62b1bcc36c89d1949d (diff)
Add cli options for pasing and codegen
Add cli options specific for only parsing and codegen add logger for help in debugging
Diffstat (limited to 'src/cljcc/cljcc.clj')
-rw-r--r--src/cljcc/cljcc.clj40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/cljcc/cljcc.clj b/src/cljcc/cljcc.clj
index e687ae9..6708173 100644
--- a/src/cljcc/cljcc.clj
+++ b/src/cljcc/cljcc.clj
@@ -1,18 +1,42 @@
(ns cljcc.cljcc
(:require
+ [clojure.tools.cli :refer [parse-opts]]
+ [clojure.string :as string]
+ [cljcc.log :as log]
+ [cljcc.util :refer [exit]]
[cljcc.driver :as d])
(:gen-class))
(set! *warn-on-reflection* true)
+(defn usage [options-summary]
+ (->>
+ ["Usage: ./cljcc path/to/file.c [options]"
+ ""
+ "Options:"
+ options-summary]
+ (string/join \newline)))
+
+(def cli-options
+ [[nil "--parse" "Runs parser. Does not emit any files."]
+ [nil "--codegen" "Runs compiler. Does not emit any files."]
+ ["-h" "--help"]])
+
+(defn validate-args [args]
+ (let [{:keys [options arguments summary]} (parse-opts args cli-options)]
+ (cond
+ (:help options) {:exit-message (usage summary) :ok? true}
+ (= 1 (count arguments)) {:file-path (first arguments)
+ :options options}
+ :else {:exit-message (usage summary)})))
+
(defn -main
"Main entrypoint for cljcc compiler."
[& args]
- (let [input-file-path (first args)]
- (try
- (d/run input-file-path)
- (catch Exception e
- (println "Error: " (.getMessage e))
- (System/exit 1))
- (finally
- (System/exit 0)))))
+ (let [{:keys [file-path exit-message ok? options]} (validate-args args)]
+ (if exit-message
+ (exit (if ok? 0 1) exit-message)
+ (try
+ (d/run file-path options)
+ (catch Exception e (exit 1 (.getMessage e)))
+ (finally (exit 0 "Succesfully ran compiler."))))))