aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/cljcc.clj
blob: 2c9643e33b16a60a34c78624390345959149f83a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(ns cljcc.cljcc
  (:require
   [clojure.tools.cli :refer [parse-opts]]
   [clojure.string :as string]
   [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 "--lex" "Runs lexer. Does not emit any files."]
   [nil "--parse" "Runs parser. Does not emit any files."]
   [nil "--codegen" "Runs compiler. Does not emit any files."]
   [nil "--tacky" "Runs tacky generation. 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 [{: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)
        (exit 0 "Successfully executed.")
        (catch Exception e
          (exit 1 (ex-message e) e))))))