diff options
| author | Your Name <agrawalshagun07@gmail.com> | 2025-03-16 18:03:26 +0530 |
|---|---|---|
| committer | Your Name <agrawalshagun07@gmail.com> | 2025-03-16 18:03:26 +0530 |
| commit | 32499638cef3c49ff686b19b5708d6b08712c526 (patch) | |
| tree | 31f09287602d243b22660fe455e5174bdcc280b4 /cljcc-compiler/src/cljcc/cljcc.clj | |
| parent | 277319fa392f5ee9f21eedf2c4d224739f045690 (diff) | |
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.
Diffstat (limited to 'cljcc-compiler/src/cljcc/cljcc.clj')
| -rw-r--r-- | cljcc-compiler/src/cljcc/cljcc.clj | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/cljcc-compiler/src/cljcc/cljcc.clj b/cljcc-compiler/src/cljcc/cljcc.clj new file mode 100644 index 0000000..fc088fc --- /dev/null +++ b/cljcc-compiler/src/cljcc/cljcc.clj @@ -0,0 +1,49 @@ +(ns cljcc.cljcc + (:require + [cljcc.lexer :as lexer] + [cljcc.parser :as parser] + [cljcc.tacky :as tacky] + [cljcc.config :as config] + [cljcc.analyze.core :as analyzer] + [cljcc.compiler :as codegen] + [cljcc.emit :as emit]) + (:gen-class)) + +(def valid-os-targets #{:mac :linux}) +(def valid-stages #{:lex :parse :validate :tacky :codegen :emit}) + +(defn run + "Compiles source input using specified compiler options. + + Parameters: + source - Source C file. + options - Map of compiler configuration options. + + Options map: + :target - Map of target platform settings + :os - Target OS #{:linux :mac} + :stage - Which stage to generate #{:lex :parse :validate :tacky :codegen :emit} + + Returns generated AST for specified stage." + [source & {:keys [options] :or {options {}}}] + (let [default-options {:target {:os :linux} + :stage :emit} + merged-options (merge default-options options) + stage (:stage merged-options) + target-os (:os (:target merged-options)) + _ (assert (stage valid-stages) "Invalid stage for compilation.") + _ (assert (target-os valid-os-targets) "Invalid operating system.") + _ (config/set-os target-os) + stages [lexer/lex parser/parse + analyzer/validate tacky/tacky-generate + codegen/assembly emit/emit] + stage-idx (condp = stage + :lex 1 + :parse 2 + :validate 3 + :tacky 4 + :codegen 5 + :emit 6) + stages-to-run (vec (take stage-idx stages))] + (reduce (fn [acc f] (f acc)) source stages-to-run))) + |
