mirror of
https://github.com/macocianradu/RosettaHaskellCompiler.git
synced 2026-03-18 21:10:07 +00:00
Working Version 0.0.1
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
module Model.Type where
|
||||
|
||||
import Data.Time.LocalTime()
|
||||
|
||||
-- |The representation of a Rosetta data type
|
||||
data Type = MakeType {
|
||||
typeName :: String,
|
||||
|
||||
@@ -29,7 +29,7 @@ printAttributes objName (at : ats) = (printAttribute objName at <> ",") : printA
|
||||
printAttribute :: String -> TypeAttribute -> Doc a
|
||||
printAttribute objName (MakeTypeAttribute name typ crd description) =
|
||||
printDescription description
|
||||
(pretty objName <> pretty (capitalize name) <+> "::" <+> printCardinality (MakeTypeAttribute name typ crd description))
|
||||
(pretty (uncapitalize objName) <> pretty (capitalize name) <+> "::" <+> printCardinality (MakeTypeAttribute name typ crd description))
|
||||
|
||||
-- |Converts a Cardinality into a haskell valid Doc
|
||||
printCardinality :: TypeAttribute -> Doc a
|
||||
|
||||
@@ -18,6 +18,15 @@ data Symbol = Var{
|
||||
argsType :: [(Type, Cardinality)],
|
||||
returnType :: (Type, Cardinality)
|
||||
} deriving (Show)
|
||||
|
||||
instance Eq Symbol where
|
||||
(==) (Var name1 _ _) (Var name2 _ _)
|
||||
| name1 == name2 = True
|
||||
| otherwise = False
|
||||
(==) (Func name1 _ _) (Func name2 _ _)
|
||||
| name1 == name2 = True
|
||||
| otherwise = False
|
||||
(==) _ _ = False
|
||||
|
||||
|
||||
-- |A map of the predefined functions, their arguments and their return type
|
||||
|
||||
@@ -15,6 +15,7 @@ data TypeCheckError =
|
||||
| TypeMismatch String String
|
||||
| CardinalityMismatch Cardinality Cardinality
|
||||
| MultipleDeclarations String
|
||||
| TypeNameReserved String
|
||||
deriving (Show)
|
||||
|
||||
-- |Checks whether a data type is valid
|
||||
@@ -57,9 +58,12 @@ checkAttributeType definedTypes 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)
|
||||
| typeName t `elem` map typeName l = error $ "Multiple declarations of " ++ show t
|
||||
| otherwise = t : addDefinedTypes l ts
|
||||
addDefinedTypes :: [Type] -> [Type] -> Either [TypeCheckError] [Type]
|
||||
addDefinedTypes l [] = Right l
|
||||
addDefinedTypes l (BasicType t : ts) = Left [TypeNameReserved t]
|
||||
addDefinedTypes l (t:ts) =
|
||||
case addDefinedTypes l ts of
|
||||
Left error -> Left error
|
||||
Right types -> if typeName t `elem` map typeName l
|
||||
then Left [MultipleDeclarations $ show t]
|
||||
else Right $ t : types
|
||||
@@ -4,10 +4,14 @@ import Data.Either
|
||||
import Data.Char
|
||||
|
||||
|
||||
-- |Capitalise a string
|
||||
-- |Capitalize a string
|
||||
capitalize :: String -> String
|
||||
capitalize s = toUpper (head s) : tail s
|
||||
|
||||
-- |Uncapitalize a string
|
||||
uncapitalize :: String -> String
|
||||
uncapitalize s = toLower (head s) : tail s
|
||||
|
||||
-- |Convert a namespace to a filename
|
||||
namespaceToName :: String -> String
|
||||
namespaceToName [] = ".rosetta"
|
||||
@@ -75,6 +79,13 @@ pairRights [] = []
|
||||
pairRights ((a, c) : rst) = (a, rights c) : pairRights rst
|
||||
|
||||
|
||||
-- |Check a list for duplicate values. Returns a list with all the values which have duplicates
|
||||
checkDuplicates :: Eq a => [a] -> [a]
|
||||
checkDuplicates [] = []
|
||||
checkDuplicates (a : as)
|
||||
| a `elem` as = a : checkDuplicates as
|
||||
| otherwise = checkDuplicates as
|
||||
|
||||
-- |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 :: Either a b -> b
|
||||
|
||||
Reference in New Issue
Block a user