-- |
-- Module      : Ast
-- Description : Contains all AST elements.
--
-- Contains all AST elements, all of these produced by the [Parser]("Parser.Parser") module.
module Parser.Ast where

import Data.List.NonEmpty (NonEmpty)
import Trees.Common

--------------------------------------------------------Program---------------------------------------------------------

-- * Program

-- | The head of the AST.
newtype Program = Program [Statement]
  deriving (Int -> Program -> ShowS
[Program] -> ShowS
Program -> String
(Int -> Program -> ShowS)
-> (Program -> String) -> ([Program] -> ShowS) -> Show Program
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Program -> ShowS
showsPrec :: Int -> Program -> ShowS
$cshow :: Program -> String
show :: Program -> String
$cshowList :: [Program] -> ShowS
showList :: [Program] -> ShowS
Show, Program -> Program -> Bool
(Program -> Program -> Bool)
-> (Program -> Program -> Bool) -> Eq Program
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Program -> Program -> Bool
== :: Program -> Program -> Bool
$c/= :: Program -> Program -> Bool
/= :: Program -> Program -> Bool
Eq)

-- ** Statements

-- | Statement.
data Statement
  = -- | Declaration statement, see 'Declaration'.
    StmtDecl Declaration
  | -- | Expression statement, see 'Expression'.
    StmtExpr Expression
  deriving (Int -> Statement -> ShowS
[Statement] -> ShowS
Statement -> String
(Int -> Statement -> ShowS)
-> (Statement -> String)
-> ([Statement] -> ShowS)
-> Show Statement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Statement -> ShowS
showsPrec :: Int -> Statement -> ShowS
$cshow :: Statement -> String
show :: Statement -> String
$cshowList :: [Statement] -> ShowS
showList :: [Statement] -> ShowS
Show, Statement -> Statement -> Bool
(Statement -> Statement -> Bool)
-> (Statement -> Statement -> Bool) -> Eq Statement
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Statement -> Statement -> Bool
== :: Statement -> Statement -> Bool
$c/= :: Statement -> Statement -> Bool
/= :: Statement -> Statement -> Bool
Eq)

------------------------------------------------------Declarations------------------------------------------------------

-- * Declarations

-- | Declaration.
data Declaration
  = -- | Variable declaration.
    --
    -- > let x = 5
    DeclVar (Identifier, Maybe Type) Expression
  | -- | Function declaration.
    --
    -- > let f x y = x + y
    --
    -- > let rec f x y = f x 1 + f 1 y
    DeclFun Identifier IsRec Fun
  deriving (Int -> Declaration -> ShowS
[Declaration] -> ShowS
Declaration -> String
(Int -> Declaration -> ShowS)
-> (Declaration -> String)
-> ([Declaration] -> ShowS)
-> Show Declaration
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Declaration -> ShowS
showsPrec :: Int -> Declaration -> ShowS
$cshow :: Declaration -> String
show :: Declaration -> String
$cshowList :: [Declaration] -> ShowS
showList :: [Declaration] -> ShowS
Show, Declaration -> Declaration -> Bool
(Declaration -> Declaration -> Bool)
-> (Declaration -> Declaration -> Bool) -> Eq Declaration
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Declaration -> Declaration -> Bool
== :: Declaration -> Declaration -> Bool
$c/= :: Declaration -> Declaration -> Bool
/= :: Declaration -> Declaration -> Bool
Eq)

------------------------------------------------------Expressions-------------------------------------------------------

-- * Expressions

-- | Expression.
data Expression
  = -- | Identifier expression, see 'Identifier'.
    ExprId Identifier
  | -- | Primitive value expression, see 'PrimitiveValue'.
    ExprPrimVal PrimitiveValue
  | -- | Binary operation, see 'BinaryOperator'.
    ExprBinOp BinaryOperator Expression Expression
  | -- | Unary operation, see 'UnaryOperator'.
    ExprUnOp UnaryOperator Expression
  | -- | Function application expression.
    --
    -- > f 6
    --
    -- > (fun x y = x + y) 5
    ExprApp Expression Expression
  | -- | If-then-else expression.
    --
    -- > if x > 4 then x * 8 else x / 15
    ExprIte Expression Expression Expression
  | -- | Let expression.
    --
    -- > let x = 4 in x * x
    --
    -- > let f x y = x + y in f 4 8
    --
    -- > let rec f x y = f x 1 + f 1 y in f 4 8
    ExprLetIn Declaration Expression
  | -- | Anonymous function, see 'Fun'.
    ExprFun Fun
  deriving (Int -> Expression -> ShowS
[Expression] -> ShowS
Expression -> String
(Int -> Expression -> ShowS)
-> (Expression -> String)
-> ([Expression] -> ShowS)
-> Show Expression
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Expression -> ShowS
showsPrec :: Int -> Expression -> ShowS
$cshow :: Expression -> String
show :: Expression -> String
$cshowList :: [Expression] -> ShowS
showList :: [Expression] -> ShowS
Show, Expression -> Expression -> Bool
(Expression -> Expression -> Bool)
-> (Expression -> Expression -> Bool) -> Eq Expression
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Expression -> Expression -> Bool
== :: Expression -> Expression -> Bool
$c/= :: Expression -> Expression -> Bool
/= :: Expression -> Expression -> Bool
Eq)

-- | Function representation without the name.
--
-- It contains its parameters, returned type and body.
--
-- > fun x -> true
--
-- > fun x y -> x + y
data Fun = Fun (NonEmpty (Identifier, Maybe Type)) (Maybe Type) Expression
  deriving (Int -> Fun -> ShowS
[Fun] -> ShowS
Fun -> String
(Int -> Fun -> ShowS)
-> (Fun -> String) -> ([Fun] -> ShowS) -> Show Fun
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Fun -> ShowS
showsPrec :: Int -> Fun -> ShowS
$cshow :: Fun -> String
show :: Fun -> String
$cshowList :: [Fun] -> ShowS
showList :: [Fun] -> ShowS
Show, Fun -> Fun -> Bool
(Fun -> Fun -> Bool) -> (Fun -> Fun -> Bool) -> Eq Fun
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Fun -> Fun -> Bool
== :: Fun -> Fun -> Bool
$c/= :: Fun -> Fun -> Bool
/= :: Fun -> Fun -> Bool
Eq)