From 59ff3a358bb96f2df85dcad8fb999c1a2bc784e3 Mon Sep 17 00:00:00 2001 From: Anton Kesy Date: Sun, 17 Mar 2024 19:06:38 +0100 Subject: [PATCH] enable comments --- examples/comments.mmm | 9 +++++++++ examples/control.mmm | 2 ++ examples/structs.mmm | 4 ++-- src/AST.hs | 2 -- src/Parser/Comment.hs | 7 +++++-- src/Parser/Expression.hs | 6 +++--- src/Parser/Space.hs | 2 +- test/E2E/Interpreter/examples/check_examples.sh | 1 + test/E2E/Interpreter/examples/comments.mmm | 9 +++++++++ test/Spec.hs | 2 +- 10 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 examples/comments.mmm create mode 100644 test/E2E/Interpreter/examples/comments.mmm diff --git a/examples/comments.mmm b/examples/comments.mmm new file mode 100644 index 0000000..61d8355 --- /dev/null +++ b/examples/comments.mmm @@ -0,0 +1,9 @@ +// comment +println("Hello, World!"); // comment +// comment + +/* This is a comment + that spans multiple lines */ + +println( "Hello, World!" /* inline comment */ ); +//comment diff --git a/examples/control.mmm b/examples/control.mmm index d8d0186..23e71a3 100644 --- a/examples/control.mmm +++ b/examples/control.mmm @@ -1,3 +1,4 @@ +// if int i = 0; if 1 < 2 { println("true"); @@ -8,6 +9,7 @@ if 1 < 2 { } println(str(i)); +// while int j = 0; println(str(j)); while j < 10 { diff --git a/examples/structs.mmm b/examples/structs.mmm index 0cf0824..507eb29 100644 --- a/examples/structs.mmm +++ b/examples/structs.mmm @@ -8,7 +8,7 @@ println(str(n.x)) T n1; -n1 = n; +n1 = n; // copy println(str(n1.x)) @@ -17,7 +17,7 @@ n1.x = 3; println(str(n.x)) println(str(n1.x)) - +// embedded struct struct E { int x; diff --git a/src/AST.hs b/src/AST.hs index 0cfe600..8e20945 100644 --- a/src/AST.hs +++ b/src/AST.hs @@ -50,8 +50,6 @@ data Variable = Variable VariableDeclaration Expression data Assignment = Assignment Name Expression deriving (Show, Eq) -type Comment = String - data Type = IntType | FloatType diff --git a/src/Parser/Comment.hs b/src/Parser/Comment.hs index c319dfd..788fc9a 100644 --- a/src/Parser/Comment.hs +++ b/src/Parser/Comment.hs @@ -1,11 +1,12 @@ module Parser.Comment (module Parser.Comment) where -import AST import Control.Monad (void) import Parser.EndOfLine (eol) import Text.Parsec import Text.Parsec.String +type Comment = String + consumeComment :: Parser () consumeComment = void @@ -19,7 +20,9 @@ parseComment = parseSingleLineComment :: Parser Comment parseSingleLineComment = - string "//" *> manyTill anyChar (try eol) + string "//" *> manyTill anyChar eoi + where + eoi = void eol <|> lookAhead eof -- don't consume eof parseMultiLineComment :: Parser Comment parseMultiLineComment = diff --git a/src/Parser/Expression.hs b/src/Parser/Expression.hs index d0c4b0b..e918966 100644 --- a/src/Parser/Expression.hs +++ b/src/Parser/Expression.hs @@ -46,9 +46,9 @@ parseAtomicExpression = parseFunctionCallAtomic :: Parser Atomic parseFunctionCallAtomic = do name <- try parseName - _ <- char '(' - args <- try (parseExpression `sepBy` (spaces' >> char ',' >> spaces')) - _ <- char ')' + _ <- spaces' *> char '(' <* spaces' + args <- try ((spaces *> parseExpression <* spaces') `sepBy` (spaces' >> char ',' >> spaces')) + _ <- spaces' *> char ')' return $ FunctionCallAtomic name args parseAtomic :: Parser Atomic diff --git a/src/Parser/Space.hs b/src/Parser/Space.hs index b138028..4839714 100644 --- a/src/Parser/Space.hs +++ b/src/Parser/Space.hs @@ -8,7 +8,7 @@ import Text.Parsec.String space' :: Parser () space' = void space - -- <|> consumeComment -- TODO: enable comments + <|> consumeComment <|> void newline <|> void tab diff --git a/test/E2E/Interpreter/examples/check_examples.sh b/test/E2E/Interpreter/examples/check_examples.sh index b52c46a..966580d 100755 --- a/test/E2E/Interpreter/examples/check_examples.sh +++ b/test/E2E/Interpreter/examples/check_examples.sh @@ -10,6 +10,7 @@ file_paths=( "short_hello_world.mmm:Hello, World!" "print.mmm:Hello, World!1" "structs.mmm:2223hello2223" + "comments.mmm:AB" ) # Function to run program over file and check stdout diff --git a/test/E2E/Interpreter/examples/comments.mmm b/test/E2E/Interpreter/examples/comments.mmm new file mode 100644 index 0000000..7c3188b --- /dev/null +++ b/test/E2E/Interpreter/examples/comments.mmm @@ -0,0 +1,9 @@ +// comment +print("A"); // comment +// comment + +/* This is a comment + that spans multiple lines */ + +print( "B" /* inline comment */ ); +//comment diff --git a/test/Spec.hs b/test/Spec.hs index 4df52e5..9ab2c8a 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -13,7 +13,7 @@ main = ( TestList ( Unit.Parser.Assignment.allTests ++ Unit.Parser.Atomic.allTests - -- ++ Unit.Parser.Comment.allTests + ++ Unit.Parser.Comment.allTests ++ Unit.Parser.Expression.allTests ++ Unit.Parser.Program.allTests ++ Unit.Parser.Statement.allTests