mirror of
https://github.com/macocianradu/RosettaHaskellCompiler.git
synced 2026-03-18 21:10:07 +00:00
Added haddock documentation
This commit is contained in:
@@ -3,6 +3,7 @@ module Semantic.TypeChecker where
|
||||
import Model.Type
|
||||
import Data.Either
|
||||
|
||||
-- |A datatype for the different types of type check errors
|
||||
data TypeCheckError =
|
||||
UndefinedType String
|
||||
| IfConditionNotBoolean
|
||||
@@ -13,6 +14,7 @@ data TypeCheckError =
|
||||
| TypeMismatch String String
|
||||
deriving (Show)
|
||||
|
||||
-- |Checks whether a data type is valid
|
||||
checkType :: [Type] -> Type -> Either [TypeCheckError] Type
|
||||
checkType definedTypes (MakeType name super desc attr)
|
||||
| null (lefts checkedAttr) = Right $ MakeType name super desc (rights checkedAttr)
|
||||
@@ -20,6 +22,8 @@ checkType definedTypes (MakeType name super desc attr)
|
||||
where checkedAttr = checkAttributes definedTypes attr
|
||||
checkType _ (BasicType b) = Right (BasicType b)
|
||||
|
||||
|
||||
-- |Checks whether all the types of the attributes of a data type are already defined
|
||||
checkAttributes :: [Type] -> [TypeAttribute] -> [Either TypeCheckError TypeAttribute]
|
||||
checkAttributes _ [] = []
|
||||
checkAttributes definedTypes ((MakeTypeAttribute name typ crd desc):as)
|
||||
@@ -27,6 +31,7 @@ checkAttributes definedTypes ((MakeTypeAttribute name typ crd desc):as)
|
||||
| otherwise = Left (fromLeftUnsafe checked) : checkAttributes definedTypes as
|
||||
where checked = checkAttributeType definedTypes typ
|
||||
|
||||
-- |Checks whether a type is predefined or in the symbol table
|
||||
checkAttributeType :: [Type] -> Type -> Either TypeCheckError Type
|
||||
checkAttributeType _ (MakeType "int" _ _ _) = Right $ BasicType "Integer"
|
||||
checkAttributeType _ (MakeType "string" _ _ _) = Right $ BasicType "String"
|
||||
@@ -37,16 +42,21 @@ checkAttributeType definedTypes name
|
||||
| name `elem` definedTypes = Right name
|
||||
| otherwise = Left $ UndefinedType (typeName name)
|
||||
|
||||
-- |Add a list of defined types to the symbol table
|
||||
addDefinedTypes :: [Type] -> [Type] -> [Type]
|
||||
addDefinedTypes l [] = l
|
||||
addDefinedTypes l (BasicType _ : ts) = addDefinedTypes l ts
|
||||
addDefinedTypes l (t:ts) = t : addDefinedTypes l ts
|
||||
|
||||
-- |Auxiliary function to get the right value from an either that stops with an error if the value is left
|
||||
-- used when it is certain that the value will be right
|
||||
fromRightUnsafe :: (Show a) => Either a b -> b
|
||||
fromRightUnsafe x = case x of
|
||||
Left a -> error ("Value is Left" ++ show a)
|
||||
Right b -> b
|
||||
|
||||
-- |Auxiliary function to get the left value from an either that stops with an error if the value is right
|
||||
-- used when it is certain that the value will be left
|
||||
fromLeftUnsafe :: Either a b -> a
|
||||
fromLeftUnsafe x = case x of
|
||||
Left a -> a
|
||||
|
||||
Reference in New Issue
Block a user