diff options
| author | Shagun Agrawal <agrawalshagun07@gmail.com> | 2024-07-24 22:59:02 +0530 |
|---|---|---|
| committer | Shagun Agrawal <agrawalshagun07@gmail.com> | 2024-07-24 22:59:02 +0530 |
| commit | b17a2610bd3d7293d1ae59c5531121e78859e7ea (patch) | |
| tree | 8f115e357a0e949177aff699c50cbb6c948bdf11 /src/cljcc | |
| parent | e81e86e937584a7f2345df43dc93a13793d10dc2 (diff) | |
throw exception in case of parsing failure
passed chapter 1 tests
Diffstat (limited to 'src/cljcc')
| -rw-r--r-- | src/cljcc/cljcc.clj | 1 | ||||
| -rw-r--r-- | src/cljcc/driver.clj | 31 | ||||
| -rw-r--r-- | src/cljcc/parser.clj | 3 |
3 files changed, 23 insertions, 12 deletions
diff --git a/src/cljcc/cljcc.clj b/src/cljcc/cljcc.clj index bfecc3b..e687ae9 100644 --- a/src/cljcc/cljcc.clj +++ b/src/cljcc/cljcc.clj @@ -11,7 +11,6 @@ (let [input-file-path (first args)] (try (d/run input-file-path) - (println "success") (catch Exception e (println "Error: " (.getMessage e)) (System/exit 1)) diff --git a/src/cljcc/driver.clj b/src/cljcc/driver.clj index 0cd3f9f..dede930 100644 --- a/src/cljcc/driver.clj +++ b/src/cljcc/driver.clj @@ -2,7 +2,8 @@ (:require [clojure.java.shell :refer [sh]] [clojure.java.io :as io] [cljcc.compiler :as c] - [cljcc.util :refer [get-os handle-sh mac-aarch64?]])) + [cljcc.util :refer [get-os handle-sh mac-aarch64?]] + [cljcc.parser :as p])) (defn make-file-name ([filename ext] @@ -46,6 +47,9 @@ (let [preprocessed-file-path (make-file-name directory (remove-extension filename) "i") file (io/file preprocessed-file-path) source (slurp file) + _ (if (p/parseable? (p/parse source)) + (println "Parsing successfull.") + (throw (Exception. "Failed during parsing"))) assembled-source (c/run-compile source) out-file-path (make-file-name directory (remove-extension filename) "s")] (spit out-file-path assembled-source) @@ -53,26 +57,31 @@ (defn cleanup [directory filename] (let [file-without-ext (remove-extension filename)] - (sh "rm" (make-file-name directory file-without-ext "i")) - (sh "rm" (make-file-name directory file-without-ext "s")))) + (io/delete-file (make-file-name directory file-without-ext "i") true) + (io/delete-file (make-file-name directory file-without-ext "s") true))) (defn run "Runs the compiler driver with the given input source file." [^String file-path] (let [file (io/file ^String file-path) - _ (println file) filename (.getName file) directory (.getParent file)] - (handle-os) - (preprocess directory filename) - (run-compile directory filename) - (assemble directory filename) - (cleanup directory filename) - (println "Successfully created executable at " directory " for filename " filename))) + (try + (handle-os) + (preprocess directory filename) + (run-compile directory filename) + (assemble directory filename) + (println "Successfully created executable at " directory " for filename " filename) + (catch Exception e + (println "Caught exception: " (.getMessage e)) + (cleanup directory filename) + (throw (Exception. "Failed to run compiler."))) + (finally + (cleanup directory filename))))) (comment - (run "/Users/shagunagrawal/Development/c_tests/ex2.c") + (run "/Users/shagunagrawal/Development/c_tests/ex3.c") (assemble "/Users/shagunagrawal/Development/c_tests" "ex2.c") diff --git a/src/cljcc/parser.clj b/src/cljcc/parser.clj index 3c54012..860fcd5 100644 --- a/src/cljcc/parser.clj +++ b/src/cljcc/parser.clj @@ -17,6 +17,9 @@ keyword = #'int\\b' | #'return\\b' | #'void\\b'" :auto-whitespace whitespace)) +(defn parseable? [result] + (not (insta/failure? result))) + (defn parse [source] (c-parser source)) |
