From ab32a441a6aafb29cf615e14dcd284e9f62786ef Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Wed, 24 Jul 2024 18:51:24 +0530 Subject: Add initial compiler implementation --- src/cljcc/parser.clj | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/cljcc/parser.clj (limited to 'src/cljcc/parser.clj') diff --git a/src/cljcc/parser.clj b/src/cljcc/parser.clj new file mode 100644 index 0000000..3c54012 --- /dev/null +++ b/src/cljcc/parser.clj @@ -0,0 +1,25 @@ +(ns cljcc.parser + (:require + [instaparse.core :as insta])) + +(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)) + +(defn parse [source] + (c-parser source)) + +(comment + (parse "int main(void) {return 2;}") + ,) -- cgit v1.2.3