From 32499638cef3c49ff686b19b5708d6b08712c526 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 16 Mar 2025 18:03:26 +0530 Subject: Refactor into cli and cljcc-compiler folders Pass all tests. Fix babashka tasks and build setup. Repl is behaving in unexpected ways, otherwise working as expected. --- cljcc-compiler/src/cljcc/core/exception.clj | 21 +++++++++++++++++ cljcc-compiler/src/cljcc/core/exception.cljc | 34 ---------------------------- cljcc-compiler/src/cljcc/core/format.clj | 1 + cljcc-compiler/src/cljcc/core/format.cljc | 10 -------- cljcc-compiler/src/cljcc/core/log.clj | 28 +++++++++++++++++++++++ cljcc-compiler/src/cljcc/core/log.cljc | 28 ----------------------- 6 files changed, 50 insertions(+), 72 deletions(-) create mode 100644 cljcc-compiler/src/cljcc/core/exception.clj delete mode 100644 cljcc-compiler/src/cljcc/core/exception.cljc create mode 100644 cljcc-compiler/src/cljcc/core/format.clj delete mode 100644 cljcc-compiler/src/cljcc/core/format.cljc create mode 100644 cljcc-compiler/src/cljcc/core/log.clj delete mode 100644 cljcc-compiler/src/cljcc/core/log.cljc (limited to 'cljcc-compiler/src/cljcc/core') diff --git a/cljcc-compiler/src/cljcc/core/exception.clj b/cljcc-compiler/src/cljcc/core/exception.clj new file mode 100644 index 0000000..412dfff --- /dev/null +++ b/cljcc-compiler/src/cljcc/core/exception.clj @@ -0,0 +1,21 @@ +(ns cljcc.core.exception) + +(defn lex-error [{line :line col :col :as data}] + (throw (ex-info + (format "Invalid token at line: %s, col: %s." line col) + (merge {:error/type :lexer} data)))) + +(defn parser-error [msg data] + (throw (ex-info msg (merge {:error/type :parser} data)))) + +(defn analyzer-error [msg data] + (throw (ex-info msg (merge {:error/type :analyzer} data)))) + +(defn tacky-error [msg data] + (throw (ex-info msg (merge {:error/type :tacky} data)))) + +(defn compiler-error [msg data] + (throw (ex-info msg (merge {:error/type :compiler} data)))) + +(defn emit-error [msg data] + (throw (ex-info msg (merge {:error/type :emit} data)))) diff --git a/cljcc-compiler/src/cljcc/core/exception.cljc b/cljcc-compiler/src/cljcc/core/exception.cljc deleted file mode 100644 index 19245aa..0000000 --- a/cljcc-compiler/src/cljcc/core/exception.cljc +++ /dev/null @@ -1,34 +0,0 @@ -(ns cljcc.core.exception - (:require [cljcc.core.format :refer [safe-format]])) - -(defn try-catch-ex - ([f] - (try - (f) - (catch #?(:clj Throwable :cljs :default) e - [:error e]))) - ([f default] - (try - (f) - (catch #?(:clj Throwable :cljs :default) e - default)))) - -(defn lex-error [{line :line col :col :as data}] - (throw (ex-info - (safe-format "Invalid token at line: %s, col: %s." line col) - (merge {:error/type :lexer} data)))) - -(defn parser-error [msg data] - (throw (ex-info msg (merge {:error/type :parser} data)))) - -(defn analyzer-error [msg data] - (throw (ex-info msg (merge {:error/type :analyzer} data)))) - -(defn tacky-error [msg data] - (throw (ex-info msg (merge {:error/type :tacky} data)))) - -(defn compiler-error [msg data] - (throw (ex-info msg (merge {:error/type :compiler} data)))) - -(defn emit-error [msg data] - (throw (ex-info msg (merge {:error/type :emit} data)))) diff --git a/cljcc-compiler/src/cljcc/core/format.clj b/cljcc-compiler/src/cljcc/core/format.clj new file mode 100644 index 0000000..7b3fc48 --- /dev/null +++ b/cljcc-compiler/src/cljcc/core/format.clj @@ -0,0 +1 @@ +(ns cljcc.core.format) diff --git a/cljcc-compiler/src/cljcc/core/format.cljc b/cljcc-compiler/src/cljcc/core/format.cljc deleted file mode 100644 index 851b6a1..0000000 --- a/cljcc-compiler/src/cljcc/core/format.cljc +++ /dev/null @@ -1,10 +0,0 @@ -(ns cljcc.core.format - #?(:clj (:require [clojure.core :refer [format]]) - :cljs (:require [goog.string :as gstring] - [goog.string.format]))) - -(defn safe-format - "Cross platform format." - [fmt & args] - #?(:clj (apply format fmt args) - :cljs (apply gstring/format fmt args))) diff --git a/cljcc-compiler/src/cljcc/core/log.clj b/cljcc-compiler/src/cljcc/core/log.clj new file mode 100644 index 0000000..bb92b02 --- /dev/null +++ b/cljcc-compiler/src/cljcc/core/log.clj @@ -0,0 +1,28 @@ +(ns cljcc.core.log + (:require [clojure.string :as str])) + +(def ^:private log-colors + {:debug "\u001b[36m" ; Cyan + :info "\u001b[32m" ; Green + :warn "\u001b[33m" ; Yellow + :error "\u001b[31m" ; Red + :reset "\u001b[0m"}) ; Reset color + +(def reset-color (get log-colors :reset)) + +(defn- log-message [level message] + (let [color (get log-colors level) + formatted-message (str color "[" (str/upper-case (name level)) "] " message reset-color)] + (println formatted-message))) + +(defn debug [msg] + (log-message :debug msg)) + +(defn info [msg] + (log-message :info msg)) + +(defn warn [msg] + (log-message :warn msg)) + +(defn error [msg] + (log-message :error msg)) diff --git a/cljcc-compiler/src/cljcc/core/log.cljc b/cljcc-compiler/src/cljcc/core/log.cljc deleted file mode 100644 index bb92b02..0000000 --- a/cljcc-compiler/src/cljcc/core/log.cljc +++ /dev/null @@ -1,28 +0,0 @@ -(ns cljcc.core.log - (:require [clojure.string :as str])) - -(def ^:private log-colors - {:debug "\u001b[36m" ; Cyan - :info "\u001b[32m" ; Green - :warn "\u001b[33m" ; Yellow - :error "\u001b[31m" ; Red - :reset "\u001b[0m"}) ; Reset color - -(def reset-color (get log-colors :reset)) - -(defn- log-message [level message] - (let [color (get log-colors level) - formatted-message (str color "[" (str/upper-case (name level)) "] " message reset-color)] - (println formatted-message))) - -(defn debug [msg] - (log-message :debug msg)) - -(defn info [msg] - (log-message :info msg)) - -(defn warn [msg] - (log-message :warn msg)) - -(defn error [msg] - (log-message :error msg)) -- cgit v1.2.3