mirror of
https://github.com/macocianradu/RosettaHaskellCompiler.git
synced 2026-03-18 21:10:07 +00:00
Added support for multiple namespaces and imports
This commit is contained in:
@@ -8,4 +8,5 @@ import Model.Type
|
||||
data RosettaObject =
|
||||
EnumObject EnumType
|
||||
| TypeObject Type
|
||||
| FunctionObject Function
|
||||
| FunctionObject Function
|
||||
deriving Show
|
||||
@@ -6,31 +6,22 @@ import Model.Header
|
||||
import PrettyPrinter.General
|
||||
import Prettyprinter
|
||||
import Data.Char
|
||||
import Utils.Utils
|
||||
|
||||
-- |Converts a Header into a haskell valid String
|
||||
printHeader :: Header -> String
|
||||
printHeader (MakeHeader name (Just description) _ imports) =
|
||||
show $ vcat ["module" <+> pretty (convertFirst name) <+> "where",
|
||||
show $ vcat ["module" <+> pretty (removePeriods name) <+> "where",
|
||||
enclose "{-" "-}" (pretty description),
|
||||
emptyDoc,
|
||||
vcat (map printImport imports),
|
||||
emptyDoc]
|
||||
printHeader (MakeHeader name Nothing _ imports) =
|
||||
show $ vcat ["module" <+> pretty (convertFirst name) <+> "where",
|
||||
show $ vcat ["module" <+> pretty (removePeriods name) <+> "where",
|
||||
emptyDoc,
|
||||
vcat (map printImport imports),
|
||||
emptyDoc]
|
||||
|
||||
-- |Converts an import name into an import prettyprinter doc
|
||||
printImport :: String -> Doc a
|
||||
printImport name = "import" <+> pretty name
|
||||
|
||||
convertName :: String -> String
|
||||
convertName [] = []
|
||||
convertName (c:cs)
|
||||
| c == '.' = c : convertFirst cs
|
||||
| otherwise = c : convertName cs
|
||||
|
||||
convertFirst :: String -> String
|
||||
convertFirst [] = []
|
||||
convertFirst (c:cs) = toUpper c : convertName cs
|
||||
printImport name = "import" <+> pretty (removePeriods name)
|
||||
@@ -28,7 +28,7 @@ checkType _ (BasicType b) = Right (BasicType b)
|
||||
populateSuper :: [Type] -> [Type] -> Type -> Either TypeCheckError Type
|
||||
populateSuper _ _ (BasicType "Object") = Right (BasicType "Object")
|
||||
populateSuper _ _ (BasicType _) = Left $ UndefinedType "Can't extend basic types"
|
||||
populateSuper _ [] t = error "WTF" --Left $ UndefinedType (typeName t)
|
||||
populateSuper _ [] t = Left $ UndefinedType (typeName t)
|
||||
populateSuper allTypes (currType : types) (MakeType t super d a)
|
||||
| typeName currType == t = case populateSuper allTypes allTypes super of
|
||||
Right superChecked -> Right $ MakeType t superChecked (typeDescription currType) (typeAttributes currType)
|
||||
|
||||
70
src/Utils/Utils.hs
Normal file
70
src/Utils/Utils.hs
Normal file
@@ -0,0 +1,70 @@
|
||||
module Utils.Utils where
|
||||
|
||||
import Data.Either
|
||||
import Data.Char
|
||||
|
||||
-- |Convert a namespace to a filename
|
||||
namespaceToName :: String -> String
|
||||
namespaceToName [] = ".rosetta"
|
||||
namespaceToName ".*" = ".rosetta"
|
||||
namespaceToName (c : cs)
|
||||
| c == '.' = '-' : namespaceToName cs
|
||||
| otherwise = c : namespaceToName cs
|
||||
|
||||
-- |Returns the directory of a file from a path
|
||||
fileDirectory :: String -> String
|
||||
fileDirectory s = take (length s - length (fileName s)) s
|
||||
|
||||
-- |Returns the name of a file from a path
|
||||
fileName :: String -> String
|
||||
fileName path = reverse $ fileName1 $ reverse path
|
||||
|
||||
-- |Auxiliary function for the name of a file from a path
|
||||
fileName1 :: String -> String
|
||||
fileName1 [] = []
|
||||
fileName1 (c : cs)
|
||||
| c == '/' = []
|
||||
| otherwise = c : fileName1 cs
|
||||
|
||||
-- |Create a new haskell filename based on the namespace
|
||||
haskellFileName :: String -> String
|
||||
haskellFileName s = "resources/Generated/" ++ removePeriods s ++".hs"
|
||||
|
||||
-- |Function to remove all the periods from a name, and convert the name to CamelCase
|
||||
removePeriods :: String -> String
|
||||
removePeriods [] = []
|
||||
removePeriods ['*'] = []
|
||||
removePeriods (c:cs) = toUpper c : removePeriods1 cs
|
||||
|
||||
-- |Auxiliary function for converting names
|
||||
removePeriods1 :: String -> String
|
||||
removePeriods1 [] = []
|
||||
removePeriods1 (c:cs)
|
||||
| c == '.' = removePeriods cs
|
||||
| otherwise = c : removePeriods1 cs
|
||||
|
||||
-- |Extract the first elements from a list of tuples
|
||||
fstlst :: [(a, b)] -> [a]
|
||||
fstlst [] = []
|
||||
fstlst ((a,_) : rst) = a : fstlst rst
|
||||
|
||||
-- |Extract the second elements from a list of tuples
|
||||
sndlst :: [(a, b)] -> [b]
|
||||
sndlst [] = []
|
||||
sndlst ((_, b): rst) = b : sndlst rst
|
||||
|
||||
nestedLefts :: [[Either a b]] -> [[a]]
|
||||
nestedLefts = map lefts
|
||||
|
||||
nestedRights :: [[Either a b]] -> [[b]]
|
||||
nestedRights = map rights
|
||||
|
||||
-- |Get the objects from a pair with an either
|
||||
pairLefts :: [(a, [Either b c])] -> [(a, [b])]
|
||||
pairLefts [] = []
|
||||
pairLefts ((a, b) : rst) = (a, lefts b) : pairLefts rst
|
||||
|
||||
-- |Get the objects from a pair with an either
|
||||
pairRights :: [(a, [Either b c])] -> [(a, [c])]
|
||||
pairRights [] = []
|
||||
pairRights ((a, c) : rst) = (a, rights c) : pairRights rst
|
||||
Reference in New Issue
Block a user