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.clj23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/cljcc/parser.clj b/src/cljcc/parser.clj
index 77482f1..d19ddd1 100644
--- a/src/cljcc/parser.clj
+++ b/src/cljcc/parser.clj
@@ -6,12 +6,15 @@
(insta/parser
"whitespace = #'\\s+'"))
+(declare parse)
+
(def c-parser
(insta/parser
"<program> = function+
- function = #'int\\b' identifier <'('> #'void\\b' <')'> <'{'> statement <'}'>
+ function = #'int\\b' identifier <'('> #'void\\b' <')'> <'{'> statement+ <'}'>
statement = #'return\\b' exp <';'>
- exp = constant | unop exp | <'('> exp <')'>
+ exp = exp-prime
+ <exp-prime> = constant | unop exp-prime | <'('> exp-prime <')'>
unop = #'-' | #'~'
identifier = #'[a-zA-Z_]\\w*\\b'
constant = #'[0-9]+\\b'
@@ -25,10 +28,22 @@
(c-parser source))
(comment
+
(parse "int main(void) {return 2;}")
+ (parse "
+ int main(void) {
+ return 2;
+ return 2;
+ }")
+
+ (parse "int main(void) {
+ return -(((((10)))));
+ }")
+
(parse "int main(void) {
-return -(((((10)))));
-}")
+ return --2;
+ return -(((((10)))));
+ }")
,)