From ec2e54a2977ff30e51eee15f464e52dc1e8390ab Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Wed, 31 Jul 2024 23:48:08 +0530 Subject: Add multiple statements to function --- src/cljcc/parser.clj | 23 +++++++++++++++++++---- 1 file 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 " = 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 + = 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))))); + }") ,) -- cgit v1.2.3