Suggestions from code review

This commit is contained in:
craigmc08 2021-08-05 10:05:42 -04:00 committed by Craig McIlwrath
parent 6f178115dc
commit 96450aa31d
7 changed files with 10 additions and 9 deletions

View File

@ -15,7 +15,7 @@ data Expr
| DoubleLiteral Double
| BoolLiteral Bool
| ExtImport ExtImportName String
| Identifier Identifier
| Var Identifier
| Quoter Identifier String
deriving (Eq, Show)

View File

@ -76,7 +76,7 @@ Expr :: { Expr }
| double { DoubleLiteral $1 }
| true { BoolLiteral True }
| false { BoolLiteral False }
| ident { Identifier $1 }
| ident { Var $1 }
Dict :: { Expr }
: '{' DictEntries '}' { Dict $2 }

View File

@ -79,7 +79,7 @@ inferExprType (P.IntegerLiteral i) = return $ IntegerLiteral i
inferExprType (P.DoubleLiteral d) = return $ DoubleLiteral d
inferExprType (P.BoolLiteral b) = return $ BoolLiteral b
inferExprType (P.ExtImport n s) = return $ ExtImport n s
inferExprType (P.Identifier ident) =
inferExprType (P.Var ident) =
lookupType ident >>= \case
Nothing -> throw $ UndefinedIdentifier ident
Just typ -> return $ Var ident typ

View File

@ -2,13 +2,14 @@
module Analyzer.TypeChecker.Monad
( TypeChecker,
Bindings,
lookupType,
setType,
throw,
lookupDeclType,
runWithBound,
run,
-- Exported for testing
Bindings,
runWithBound,
)
where

View File

@ -30,7 +30,7 @@ spec_Parser = do
("real", DoubleLiteral 3.14),
("yes", BoolLiteral True),
("no", BoolLiteral False),
("ident", Identifier "Wasp")
("ident", Var "Wasp")
]
]
parse source `shouldBe` Right ast

View File

@ -99,11 +99,11 @@ spec_Internal = do
it "Types identifier as the type in the bindings" $ do
forAll chooseType $ \typ ->
let bindings = H.singleton "var" typ
actual = exprType <$> inferExprType' bindings (P.Identifier "var")
actual = exprType <$> inferExprType' bindings (P.Var "var")
in actual == Right typ
it "Fails to type check identifiers not given a type in the bindings" $ do
let bindings = H.empty
let actual = exprType <$> inferExprType' bindings (P.Identifier "pi")
let actual = exprType <$> inferExprType' bindings (P.Var "pi")
let expected = Left $ UndefinedIdentifier "pi"
actual `shouldBe` expected

View File

@ -60,7 +60,7 @@ spec_TypeChecker = do
let actual = typeCheck typeDefs ast
actual `shouldSatisfy` isRight
it "Type checks an existing enum value" $ do
let ast = P.AST [P.Decl "food" "Cucumber" (P.Identifier "Dill")]
let ast = P.AST [P.Decl "food" "Cucumber" (P.Var "Dill")]
let typeDefs =
TD.TypeDefinitions
{ TD.declTypes = H.singleton "food" (TD.DeclType "food" (EnumType "flavor")),