From 76041390b66ae1a7e903bfd40b3b176dc895b385 Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Sun, 21 Jul 2024 00:11:14 +0530 Subject: add parser for ch1 --- deps.edn | 3 ++- src/cljcc/cljcc.clj | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/deps.edn b/deps.edn index ddb4e07..f3ab417 100644 --- a/deps.edn +++ b/deps.edn @@ -1,5 +1,6 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.11.3"}} + :deps {org.clojure/clojure {:mvn/version "1.11.3"} + instaparse/instaparse {:mvn/version "1.5.0"}} :aliases {:run-m {:main-opts ["-m" "cljcc.cljcc"]} :run-x {:ns-default cljcc.cljcc diff --git a/src/cljcc/cljcc.clj b/src/cljcc/cljcc.clj index 6493b08..1e102af 100644 --- a/src/cljcc/cljcc.clj +++ b/src/cljcc/cljcc.clj @@ -1,6 +1,28 @@ (ns cljcc.cljcc + (:require + [instaparse.core :as insta] + [clojure.java.io :as io]) (:gen-class)) +(def ex-prg "int main(void) {return 2;}") + +(def whitespace + (insta/parser + "whitespace = #'\\s+'")) + +(def c-parser + (insta/parser + " = function+ + function = #'int\\b' identifier <'('> #'void\\b' <')'> <'{'> statement <'}'> + statement = #'return\\b' exp <';'> + exp = constant + identifier = #'[a-zA-Z_]\\w*\\b' + constant = #'[0-9]+\\b' + keyword = #'int\\b' | #'return\\b' | #'void\\b'" + :auto-whitespace whitespace)) + +(println (c-parser ex-prg)) + (defn greet "Callable entry point to the application." [data] @@ -9,4 +31,5 @@ (defn -main "I don't do a whole lot ... yet." [& args] + (let [input-file-path (first args)]) (greet {:name (first args)})) -- cgit v1.2.3