You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

121 lines
3.1 KiB

{-|
Module: Simple.AST
Description: Defines a AST for a simple subset of the language
Defines a AST for a simple subset of the language
-}
module Simple.AST where
import Types
-- | A type in the language
data Type
= FuncType PN Type Type
| ChrType PN
| StrType PN
| IntType PN
| UsrType PN Identifier
deriving Show
-- | Prints a pretty type signature for a given type.
pretty :: Type -> String
pretty (FuncType _ t1@(FuncType _ _ _) t2) = "(" <> pretty t1 <> ") → " <> pretty t2
pretty (FuncType _ t1 t2) = pretty t1 <> "" <> pretty t2
pretty (ChrType _) = "Char"
pretty (StrType _) = "String"
pretty (IntType _) = ""
pretty (UsrType _ i) = unId i
-- | Only compares type information, ignores position information.
instance Eq Type where
FuncType _ t11 t12 == FuncType _ t21 t22 = t11 == t21 && t12 == t22
ChrType _ == ChrType _ = True
StrType _ == StrType _ = True
IntType _ == IntType _ = True
UsrType _ i1 == UsrType _ i2 = i1 == i2
_ == _ = False
instance Positioned Type where
pos (FuncType p _ _) = p
pos (ChrType p) = p
pos (StrType p) = p
pos (IntType p) = p
pos (UsrType p _) = p
-- | A top level declaration.
data TopLevel
= Def PN Type Identifier [([Pattern], Expr)] -- ^ Variable and function definition
| Dat PN Identifier [(Type, Identifier)] -- ^ Data definition
| Rec PN Identifier [(Type, Identifier)] -- ^ Record definition
deriving Show
instance Positioned TopLevel where
pos (Def p _ _ _) = p
pos (Dat p _ _) = p
pos (Rec p _ _) = p
-- | A pattern to match on.
data Pattern
= Wild PN
| PVar PN Identifier
| PLit Literal
| PApp PN Identifier [Pattern]
deriving Show
instance Positioned Pattern where
pos (Wild p) = p
pos (PVar p _) = p
pos (PLit l) = pos l
pos (PApp p _ _) = p
-- | A literal value of a builtin type.
data Literal
= LInt PN Integer
| LChr PN Char
| LStr PN String
deriving Show
instance Positioned Literal where
pos (LInt p _) = p
pos (LChr p _) = p
pos (LStr p _) = p
-- | A term
data Term
= TLit Literal
| TLambda PN [(Type, Identifier)] Expr
| TLambdaCase PN Type [(Pattern, Expr)]
| TVar PN Identifier
deriving Show
instance Positioned Term where
pos (TLambdaCase p _ _) = p
pos (TLambda p _ _) = p
pos (TLit l) = pos l
pos (TVar p _) = p
-- | An expression which is either explicitly or implicitly typed
data Expr
= ExpExpr Type Expr1
| ImpExpr Expr1
deriving Show
instance Positioned Expr where
pos (ExpExpr _ e) = pos e
pos (ImpExpr e) = pos e
-- | An expression
data Expr1
= Apply PN Expr [Expr]
| Case PN Expr [(Pattern, Expr)]
| Let PN [(Identifier, Expr)] Expr
| Inst PN Identifier [(Identifier, Expr)]
| Term Term
deriving Show
instance Positioned Expr1 where
pos (Apply p _ _) = p
pos (Case p _ _) = p
pos (Let p _ _) = p
pos (Inst p _ _) = p
pos (Term t) = pos t