Skip to content

Commit

Permalink
fix variable and assigment not working together
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkesy committed Mar 6, 2024
1 parent 97c3d04 commit 6a1e203
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
1 change: 0 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Text.Parsec (parse)

developProgram :: String
developProgram =
-- "int i = 1; int j = 2; int l = 3 + 4; int k = i + j + l;"
"int i = 1; int j = 2; int l = 3 + 4; int k = i + j + l; k = k * 0;"

main :: IO ()
Expand Down
1 change: 1 addition & 0 deletions peter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ test-suite peter-test
Unit.Parser.Assignment
Unit.Parser.Comment
Unit.Parser.Expression
Unit.Parser.Program
Unit.Parser.Statement
Paths_peter
hs-source-dirs:
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/Statement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Text.Parsec.String

parseStatement :: Parser Statement
parseStatement =
( (VariableStatement <$> (spaces' *> (try parseVariable)))
<|> (AssignmentStatement <$> (spaces' *> (try parseAssignment)))
( (VariableStatement <$> try (spaces' *> (try parseVariable)))
<|> (AssignmentStatement <$> try (spaces' *> (try parseAssignment)))
)
<* endOfStatement

Expand Down
2 changes: 2 additions & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Test.HUnit (Test (TestList), runTestTTAndExit)
import Unit.Parser.Assignment
import Unit.Parser.Comment
import Unit.Parser.Expression
import Unit.Parser.Program
import Unit.Parser.Statement

main :: IO ()
Expand All @@ -12,6 +13,7 @@ main =
( Unit.Parser.Assignment.allTests
++ Unit.Parser.Comment.allTests
++ Unit.Parser.Expression.allTests
++ Unit.Parser.Program.allTests
++ Unit.Parser.Statement.allTests
++ E2E.Placeholder.allTests
)
Expand Down
86 changes: 86 additions & 0 deletions test/Unit/Parser/Program.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module Unit.Parser.Program (allTests) where

import AST
import Data.Either (fromRight, isRight)
import Parser.Program
import Test.HUnit
import Text.Parsec (parse)

allTests :: [Test]
allTests =
[ TestLabel "simple" testSimple
]

emptyProgram :: Program
emptyProgram = Program []

testSimple :: Test
testSimple = TestCase $ do
assertEqual
"empty"
True
(isRight (parse parseProgram "" ""))
assertEqual
"Single variable statement"
( Program
[ VariableStatement
( Variable
"k"
IntType
( AtomicExpression (LiteralAtomic (IntLiteral 1))
)
)
]
)
(fromRight emptyProgram (parse parseProgram "" "int k = 1;"))
assertEqual
"Multiple variable statements"
( Program
[ VariableStatement
( Variable
"k"
IntType
( AtomicExpression (LiteralAtomic (IntLiteral 1))
)
),
VariableStatement
( Variable
"j"
IntType
( AtomicExpression (LiteralAtomic (IntLiteral 2))
)
)
]
)
(fromRight emptyProgram (parse parseProgram "" "int k = 1; int j = 2;"))
assertEqual
"Single assignment statement"
( Program
[ AssignmentStatement
( Assignment
"k"
( AtomicExpression (LiteralAtomic (IntLiteral 1))
)
)
]
)
(fromRight emptyProgram (parse parseProgram "" "k = 1;"))
assertEqual
"variable statement and assignment statement"
( Program
[ VariableStatement
( Variable
"k"
IntType
( AtomicExpression (LiteralAtomic (IntLiteral 1))
)
),
AssignmentStatement
( Assignment
"j"
( AtomicExpression (LiteralAtomic (IntLiteral 2))
)
)
]
)
(fromRight emptyProgram (parse parseProgram "" "int k = 1; j = 2;"))

0 comments on commit 6a1e203

Please sign in to comment.