module Language.Qux.Annotated.Syntax (
Annotated(..),
Id(..), Program(..),
Decl(..),
name, parameters, parameterNames, parameterTypes, returnType, stmts,
Stmt(..), Expr(..), Type(..),
BinaryOp(..), UnaryOp(..), Value(..)
) where
import Language.Qux.Syntax (BinaryOp(..), UnaryOp(..), Value(..))
class Annotated node where
ann :: node a -> a
data Id a = Id a String
deriving (Eq, Show)
instance Annotated Id where
ann (Id a _) = a
data Program a = Program a [Decl a]
deriving (Eq, Show)
instance Annotated Program where
ann (Program a _) = a
data Decl a = FunctionDecl a (Id a) [(Type a, Id a)] [Stmt a]
deriving (Eq, Show)
instance Annotated Decl where
ann (FunctionDecl a _ _ _) = a
name :: Decl a -> Id a
name (FunctionDecl _ n _ _) = n
types :: Decl a -> [Type a]
types (FunctionDecl _ _ ps _) = map fst ps
parameters :: Decl a -> [(Type a, Id a)]
parameters (FunctionDecl _ _ ps _) = init ps
parameterNames :: Decl a -> [Id a]
parameterNames = (map snd) . parameters
parameterTypes :: Decl a -> [Type a]
parameterTypes = (map fst) . parameters
returnType :: Decl a -> Type a
returnType (FunctionDecl _ _ ps _) = fst $ last ps
stmts :: Decl a -> [Stmt a]
stmts (FunctionDecl _ _ _ ss) = ss
data Stmt a = IfStmt a (Expr a) [Stmt a] [Stmt a]
| ReturnStmt a (Expr a)
| WhileStmt a (Expr a) [Stmt a]
deriving (Eq, Show)
instance Annotated Stmt where
ann (IfStmt a _ _ _) = a
ann (ReturnStmt a _) = a
ann (WhileStmt a _ _) = a
data Expr a = ApplicationExpr a (Id a) [Expr a]
| BinaryExpr a BinaryOp (Expr a) (Expr a)
| ListExpr a [Expr a]
| UnaryExpr a UnaryOp (Expr a)
| ValueExpr a Value
deriving (Eq, Show)
instance Annotated Expr where
ann (ApplicationExpr a _ _) = a
ann (BinaryExpr a _ _ _) = a
ann (ListExpr a _) = a
ann (UnaryExpr a _ _) = a
ann (ValueExpr a _) = a
data Type a = BoolType a
| IntType a
| ListType a (Type a)
| NilType a
deriving (Eq, Show)
instance Annotated Type where
ann (BoolType a) = a
ann (IntType a) = a
ann (ListType a _) = a
ann (NilType a) = a