Skip to content

Commit

Permalink
enable comments
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkesy committed Mar 17, 2024
1 parent cb8875a commit 59ff3a3
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 11 deletions.
9 changes: 9 additions & 0 deletions examples/comments.mmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// comment
println("Hello, World!"); // comment
// comment

/* This is a comment
that spans multiple lines */

println( "Hello, World!" /* inline comment */ );
//comment
2 changes: 2 additions & 0 deletions examples/control.mmm
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// if
int i = 0;
if 1 < 2 {
println("true");
Expand All @@ -8,6 +9,7 @@ if 1 < 2 {
}
println(str(i));

// while
int j = 0;
println(str(j));
while j < 10 {
Expand Down
4 changes: 2 additions & 2 deletions examples/structs.mmm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ println(str(n.x))


T n1;
n1 = n;
n1 = n; // copy

println(str(n1.x))

Expand All @@ -17,7 +17,7 @@ n1.x = 3;
println(str(n.x))
println(str(n1.x))


// embedded struct

struct E {
int x;
Expand Down
2 changes: 0 additions & 2 deletions src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ data Variable = Variable VariableDeclaration Expression
data Assignment = Assignment Name Expression
deriving (Show, Eq)

type Comment = String

data Type
= IntType
| FloatType
Expand Down
7 changes: 5 additions & 2 deletions src/Parser/Comment.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 =
Expand Down
6 changes: 3 additions & 3 deletions src/Parser/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/Space.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Text.Parsec.String
space' :: Parser ()
space' =
void space
-- <|> consumeComment -- TODO: enable comments
<|> consumeComment
<|> void newline
<|> void tab

Expand Down
1 change: 1 addition & 0 deletions test/E2E/Interpreter/examples/check_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/E2E/Interpreter/examples/comments.mmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// comment
print("A"); // comment
// comment

/* This is a comment
that spans multiple lines */

print( "B" /* inline comment */ );
//comment
2 changes: 1 addition & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 59ff3a3

Please sign in to comment.