aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/parser.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/cljcc/parser.clj')
-rw-r--r--src/cljcc/parser.clj25
1 files changed, 25 insertions, 0 deletions
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
+ "<program> = 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;}")
+ ,)