on c0554e0 help sign in
48 lines 2.2 kB history raw
1--parse type and data constructors
2module TutorialD.Interpreter.Types where
3import ProjectM36.Base
4import Text.Parsec.String
5import Text.Parsec
6import TutorialD.Interpreter.Base
7import ProjectM36.DataTypes.Primitive
8import Control.Monad (liftM)
9import qualified Data.Text as T
11-- | Upper case names are type names while lower case names are polymorphic typeconstructor arguments.
12-- data *Either a b* = Left a | Right b
13typeConstructorDefP :: Parser TypeConstructorDef
14typeConstructorDefP = ADTypeConstructorDef <$> liftM T.pack capitalizedIdentifier <*> typeVarNamesP
16typeVarNamesP :: Parser [TypeVarName]
17typeVarNamesP = do
18 vars <- many uncapitalizedIdentifier
19 pure (map T.pack vars)
21-- data Either a b = *Left a* | *Right b*
22dataConstructorDefP :: Parser DataConstructorDef
23dataConstructorDefP = DataConstructorDef <$> liftM T.pack identifier <*> many dataConstructorDefArgP
25-- data *Either a b* = Left *a* | Right *b*
26dataConstructorDefArgP :: Parser DataConstructorDefArg
27dataConstructorDefArgP = parens (DataConstructorDefTypeConstructorArg <$> typeConstructorP) <|>
28 DataConstructorDefTypeVarNameArg <$> liftM T.pack uncapitalizedIdentifier
30--built-in, nullary type constructors
31-- Int, Text, etc.
32primitiveTypeConstructorP :: Parser TypeConstructor
33primitiveTypeConstructorP = choice (map (\(PrimitiveTypeConstructorDef name typ, _) -> do
34 tName <- liftM T.pack (symbol (T.unpack name))
35 pure $ PrimitiveTypeConstructor tName typ)
36 primitiveTypeConstructorMapping)
38-- *Either Int Text*, *Int*
39typeConstructorP :: Parser TypeConstructor
40typeConstructorP = primitiveTypeConstructorP <|>
41 ADTypeConstructor <$> liftM T.pack identifier <*> many typeConstructorArgP
43typeConstructorArgP :: Parser TypeConstructorArg
44typeConstructorArgP = TypeConstructorArg <$> typeConstructorP <|>
45 TypeConstructorTypeVarArg <$> liftM T.pack uncapitalizedIdentifier