Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AST implement typeable and data #131

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/Front/Ast.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DeriveDataTypeable #-}
-- | Sslang abstract syntax tree.
module Front.Ast where

Expand All @@ -6,42 +7,43 @@ import Common.Identifiers ( Identifiable(..)
)
import Common.Pretty

import Data.Generics
import Data.List ( intersperse )
import Data.Maybe ( mapMaybe )

-- | A complete program: a list of top-level definitions.
newtype Program = Program [TopDef]
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | A top-level definition.
data TopDef
= TopDef Definition -- ^ Bind a (data) value to a variable
| TopType TypeDef -- ^ Define an algebraic data type
| TopCDefs String -- ^ Inlined block of C definitions
| TopExtern ExternDecl -- ^ Declare external symbol for FFI
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | Associate a type with a symbol
data ExternDecl = ExternDecl Identifier Typ
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | An algebraic data type definition.
data TypeDef = TypeDef
{ typeName :: Identifier -- ^ The name of the type, e.g., @Option@
, typeParams :: [Identifier] -- ^ List of type parameters, e.g., @a@
, typeVariants :: [TypeVariant] -- ^ List of variants, e.g., @Some@, @None@
}
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | A type variant, i.e., a data constructor.
data TypeVariant = VariantUnnamed Identifier [Typ]
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | A value definition.
data Definition
= DefFn Identifier [Pat] TypFn Expr
| DefPat Pat Expr
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | A pattern appearing on the LHS of a definition or match arm
data Pat
Expand All @@ -52,14 +54,14 @@ data Pat
| PatTup [Pat] -- ^ Match on a tuple, e.g., @(<pat>, <pat>)@
| PatApp [Pat] -- ^ Match on multiple patterns, e.g., @Some a@
| PatAnn Typ Pat -- ^ Match with type annotation, e.g., @<pat>: Type@
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | Function type annotation
data TypFn
= TypReturn TypAnn
| TypProper TypAnn
| TypNone
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | TODO: type classes
type TypAnn = Typ
Expand All @@ -71,7 +73,7 @@ data Typ
| TTuple [Typ]
| TArrow Typ Typ
-- TODO type variables
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | An expression
data Expr
Expand All @@ -96,7 +98,7 @@ data Expr
| CQuote String
| CCall Identifier [Expr]
| ListExpr [Expr]
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

{- | An operator region: a flat list of alternating expressions and operators
that is initially parsed flat but will be restructured into a tree by
Expand All @@ -105,7 +107,7 @@ the operator precedence parser.
data OpRegion
= NextOp Identifier Expr OpRegion
| EOR
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | A literal
data Literal
Expand All @@ -114,7 +116,7 @@ data Literal
| LitRat Rational
| LitChar Char
| LitEvent
deriving (Eq, Show)
deriving (Eq, Show, Typeable, Data)

-- | Fixity declaration for binary operators.
data Fixity = Infixl Int Identifier
Expand Down