-- For GHC version 7.10 or higher {-# LANGUAGE GADTs, KindSignatures, DataKinds #-} {-# LANGUAGE LambdaCase #-} {-# OPTIONS_GHC -fno-warn-unused-binds #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-} {-# OPTIONS_GHC -fno-warn-unused-matches #-} {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} {-# OPTIONS_GHC -fno-warn-overlapping-patterns #-} module AbsCpp ( Tree(..) , Program , Def , Arg , Stm , Exp , Type , Id , johnMajorEq , module ComposOpCpp ) where import Prelude ((.), (>), (&&), (==)) import Prelude ((.), (>), (&&), (==)) import qualified Prelude as P import ComposOpCpp data Tag = Program_ | Def_ | Arg_ | Stm_ | Exp_ | Type_ | Id_ type Program = Tree 'Program_ type Def = Tree 'Def_ type Arg = Tree 'Arg_ type Stm = Tree 'Stm_ type Exp = Tree 'Exp_ type Type = Tree 'Type_ type Id = Tree 'Id_ data Tree (a :: Tag) where PDefs :: [Def] -> Tree 'Program_ DFun :: Type -> Id -> [Arg] -> [Stm] -> Tree 'Def_ ADecl :: Type -> Id -> Tree 'Arg_ SBlock :: [Stm] -> Tree 'Stm_ SDecls :: Type -> [Id] -> Tree 'Stm_ SExp :: Exp -> Tree 'Stm_ SIfElse :: Exp -> Stm -> Stm -> Tree 'Stm_ SInit :: Type -> Id -> Exp -> Tree 'Stm_ SReturn :: Exp -> Tree 'Stm_ SReturnVoid :: Tree 'Stm_ SWhile :: Exp -> Stm -> Tree 'Stm_ EAnd :: Exp -> Exp -> Tree 'Exp_ EApp :: Id -> [Exp] -> Tree 'Exp_ EAss :: Exp -> Exp -> Tree 'Exp_ EDecr :: Exp -> Tree 'Exp_ EDiv :: Exp -> Exp -> Tree 'Exp_ EDouble :: P.Double -> Tree 'Exp_ EEq :: Exp -> Exp -> Tree 'Exp_ EFalse :: Tree 'Exp_ EGt :: Exp -> Exp -> Tree 'Exp_ EGtEq :: Exp -> Exp -> Tree 'Exp_ EId :: Id -> Tree 'Exp_ EIncr :: Exp -> Tree 'Exp_ EInt :: P.Integer -> Tree 'Exp_ ELt :: Exp -> Exp -> Tree 'Exp_ ELtEq :: Exp -> Exp -> Tree 'Exp_ EMinus :: Exp -> Exp -> Tree 'Exp_ ENEq :: Exp -> Exp -> Tree 'Exp_ EOr :: Exp -> Exp -> Tree 'Exp_ EPDecr :: Exp -> Tree 'Exp_ EPIncr :: Exp -> Tree 'Exp_ EPlus :: Exp -> Exp -> Tree 'Exp_ EString :: P.String -> Tree 'Exp_ ETimes :: Exp -> Exp -> Tree 'Exp_ ETrue :: Tree 'Exp_ ETyped :: Exp -> Type -> Tree 'Exp_ Type_bool :: Tree 'Type_ Type_double :: Tree 'Type_ Type_int :: Tree 'Type_ Type_string :: Tree 'Type_ Type_void :: Tree 'Type_ Id ::P.String -> Tree 'Id_ instance Compos Tree where compos r a f = \case PDefs defs -> r PDefs `a` P.foldr (\ x z -> r (:) `a` f x `a` z) (r []) defs DFun type_ id args stms -> r DFun `a` f type_ `a` f id `a` P.foldr (\ x z -> r (:) `a` f x `a` z) (r []) args `a` P.foldr (\ x z -> r (:) `a` f x `a` z) (r []) stms ADecl type_ id -> r ADecl `a` f type_ `a` f id SBlock stms -> r SBlock `a` P.foldr (\ x z -> r (:) `a` f x `a` z) (r []) stms SDecls type_ ids -> r SDecls `a` f type_ `a` P.foldr (\ x z -> r (:) `a` f x `a` z) (r []) ids SExp exp -> r SExp `a` f exp SIfElse exp stm1 stm2 -> r SIfElse `a` f exp `a` f stm1 `a` f stm2 SInit type_ id exp -> r SInit `a` f type_ `a` f id `a` f exp SReturn exp -> r SReturn `a` f exp SWhile exp stm -> r SWhile `a` f exp `a` f stm EAnd exp1 exp2 -> r EAnd `a` f exp1 `a` f exp2 EApp id exps -> r EApp `a` f id `a` P.foldr (\ x z -> r (:) `a` f x `a` z) (r []) exps EAss exp1 exp2 -> r EAss `a` f exp1 `a` f exp2 EDecr exp -> r EDecr `a` f exp EDiv exp1 exp2 -> r EDiv `a` f exp1 `a` f exp2 EEq exp1 exp2 -> r EEq `a` f exp1 `a` f exp2 EGt exp1 exp2 -> r EGt `a` f exp1 `a` f exp2 EGtEq exp1 exp2 -> r EGtEq `a` f exp1 `a` f exp2 EId id -> r EId `a` f id EIncr exp -> r EIncr `a` f exp ELt exp1 exp2 -> r ELt `a` f exp1 `a` f exp2 ELtEq exp1 exp2 -> r ELtEq `a` f exp1 `a` f exp2 EMinus exp1 exp2 -> r EMinus `a` f exp1 `a` f exp2 ENEq exp1 exp2 -> r ENEq `a` f exp1 `a` f exp2 EOr exp1 exp2 -> r EOr `a` f exp1 `a` f exp2 EPDecr exp -> r EPDecr `a` f exp EPIncr exp -> r EPIncr `a` f exp EPlus exp1 exp2 -> r EPlus `a` f exp1 `a` f exp2 ETimes exp1 exp2 -> r ETimes `a` f exp1 `a` f exp2 ETyped exp type_ -> r ETyped `a` f exp `a` f type_ t -> r t instance P.Show (Tree c) where showsPrec n = \case Id str -> opar . P.showString "Id" . P.showChar ' ' . P.showsPrec 1 str . cpar PDefs defs -> opar . P.showString "PDefs" . P.showChar ' ' . P.showsPrec 1 defs . cpar DFun type_ id args stms -> opar . P.showString "DFun" . P.showChar ' ' . P.showsPrec 1 type_ . P.showChar ' ' . P.showsPrec 1 id . P.showChar ' ' . P.showsPrec 1 args . P.showChar ' ' . P.showsPrec 1 stms . cpar ADecl type_ id -> opar . P.showString "ADecl" . P.showChar ' ' . P.showsPrec 1 type_ . P.showChar ' ' . P.showsPrec 1 id . cpar SBlock stms -> opar . P.showString "SBlock" . P.showChar ' ' . P.showsPrec 1 stms . cpar SDecls type_ ids -> opar . P.showString "SDecls" . P.showChar ' ' . P.showsPrec 1 type_ . P.showChar ' ' . P.showsPrec 1 ids . cpar SExp exp -> opar . P.showString "SExp" . P.showChar ' ' . P.showsPrec 1 exp . cpar SIfElse exp stm1 stm2 -> opar . P.showString "SIfElse" . P.showChar ' ' . P.showsPrec 1 exp . P.showChar ' ' . P.showsPrec 1 stm1 . P.showChar ' ' . P.showsPrec 1 stm2 . cpar SInit type_ id exp -> opar . P.showString "SInit" . P.showChar ' ' . P.showsPrec 1 type_ . P.showChar ' ' . P.showsPrec 1 id . P.showChar ' ' . P.showsPrec 1 exp . cpar SReturn exp -> opar . P.showString "SReturn" . P.showChar ' ' . P.showsPrec 1 exp . cpar SReturnVoid -> P.showString "SReturnVoid" SWhile exp stm -> opar . P.showString "SWhile" . P.showChar ' ' . P.showsPrec 1 exp . P.showChar ' ' . P.showsPrec 1 stm . cpar EAnd exp1 exp2 -> opar . P.showString "EAnd" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EApp id exps -> opar . P.showString "EApp" . P.showChar ' ' . P.showsPrec 1 id . P.showChar ' ' . P.showsPrec 1 exps . cpar EAss exp1 exp2 -> opar . P.showString "EAss" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EDecr exp -> opar . P.showString "EDecr" . P.showChar ' ' . P.showsPrec 1 exp . cpar EDiv exp1 exp2 -> opar . P.showString "EDiv" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EDouble d -> opar . P.showString "EDouble" . P.showChar ' ' . P.showsPrec 1 d . cpar EEq exp1 exp2 -> opar . P.showString "EEq" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EFalse -> P.showString "EFalse" EGt exp1 exp2 -> opar . P.showString "EGt" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EGtEq exp1 exp2 -> opar . P.showString "EGtEq" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EId id -> opar . P.showString "EId" . P.showChar ' ' . P.showsPrec 1 id . cpar EIncr exp -> opar . P.showString "EIncr" . P.showChar ' ' . P.showsPrec 1 exp . cpar EInt n -> opar . P.showString "EInt" . P.showChar ' ' . P.showsPrec 1 n . cpar ELt exp1 exp2 -> opar . P.showString "ELt" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar ELtEq exp1 exp2 -> opar . P.showString "ELtEq" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EMinus exp1 exp2 -> opar . P.showString "EMinus" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar ENEq exp1 exp2 -> opar . P.showString "ENEq" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EOr exp1 exp2 -> opar . P.showString "EOr" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EPDecr exp -> opar . P.showString "EPDecr" . P.showChar ' ' . P.showsPrec 1 exp . cpar EPIncr exp -> opar . P.showString "EPIncr" . P.showChar ' ' . P.showsPrec 1 exp . cpar EPlus exp1 exp2 -> opar . P.showString "EPlus" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar EString str -> opar . P.showString "EString" . P.showChar ' ' . P.showsPrec 1 str . cpar ETimes exp1 exp2 -> opar . P.showString "ETimes" . P.showChar ' ' . P.showsPrec 1 exp1 . P.showChar ' ' . P.showsPrec 1 exp2 . cpar ETrue -> P.showString "ETrue" ETyped exp type_ -> opar . P.showString "ETyped" . P.showChar ' ' . P.showsPrec 1 exp . P.showChar ' ' . P.showsPrec 1 type_ . cpar Type_bool -> P.showString "Type_bool" Type_double -> P.showString "Type_double" Type_int -> P.showString "Type_int" Type_string -> P.showString "Type_string" Type_void -> P.showString "Type_void" where opar = if n > 0 then P.showChar '(' else P.id cpar = if n > 0 then P.showChar ')' else P.id instance P.Eq (Tree c) where (==) = johnMajorEq instance P.Ord (Tree c) where compare x y = P.compare (index x) (index y) `P.mappend` compareSame x y index :: Tree c -> P.Int index (PDefs _) = 1 index (DFun _ _ _ _) = 2 index (ADecl _ _) = 3 index (SBlock _) = 4 index (SDecls _ _) = 5 index (SExp _) = 6 index (SIfElse _ _ _) = 7 index (SInit _ _ _) = 8 index (SReturn _) = 9 index (SReturnVoid ) = 10 index (SWhile _ _) = 11 index (EAnd _ _) = 12 index (EApp _ _) = 13 index (EAss _ _) = 14 index (EDecr _) = 15 index (EDiv _ _) = 16 index (EDouble _) = 17 index (EEq _ _) = 18 index (EFalse ) = 19 index (EGt _ _) = 20 index (EGtEq _ _) = 21 index (EId _) = 22 index (EIncr _) = 23 index (EInt _) = 24 index (ELt _ _) = 25 index (ELtEq _ _) = 26 index (EMinus _ _) = 27 index (ENEq _ _) = 28 index (EOr _ _) = 29 index (EPDecr _) = 30 index (EPIncr _) = 31 index (EPlus _ _) = 32 index (EString _) = 33 index (ETimes _ _) = 34 index (ETrue ) = 35 index (ETyped _ _) = 36 index (Type_bool ) = 37 index (Type_double ) = 38 index (Type_int ) = 39 index (Type_string ) = 40 index (Type_void ) = 41 index (Id _) = 42 johnMajorEq :: Tree a -> Tree b -> P.Bool johnMajorEq (PDefs defs) (PDefs defs_) = defs == defs_ johnMajorEq (DFun type_ id args stms) (DFun type__ id_ args_ stms_) = type_ == type__ && id == id_ && args == args_ && stms == stms_ johnMajorEq (ADecl type_ id) (ADecl type__ id_) = type_ == type__ && id == id_ johnMajorEq (SBlock stms) (SBlock stms_) = stms == stms_ johnMajorEq (SDecls type_ ids) (SDecls type__ ids_) = type_ == type__ && ids == ids_ johnMajorEq (SExp exp) (SExp exp_) = exp == exp_ johnMajorEq (SIfElse exp stm1 stm2) (SIfElse exp_ stm1_ stm2_) = exp == exp_ && stm1 == stm1_ && stm2 == stm2_ johnMajorEq (SInit type_ id exp) (SInit type__ id_ exp_) = type_ == type__ && id == id_ && exp == exp_ johnMajorEq (SReturn exp) (SReturn exp_) = exp == exp_ johnMajorEq SReturnVoid SReturnVoid = P.True johnMajorEq (SWhile exp stm) (SWhile exp_ stm_) = exp == exp_ && stm == stm_ johnMajorEq (EAnd exp1 exp2) (EAnd exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EApp id exps) (EApp id_ exps_) = id == id_ && exps == exps_ johnMajorEq (EAss exp1 exp2) (EAss exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EDecr exp) (EDecr exp_) = exp == exp_ johnMajorEq (EDiv exp1 exp2) (EDiv exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EDouble d) (EDouble d_) = d == d_ johnMajorEq (EEq exp1 exp2) (EEq exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq EFalse EFalse = P.True johnMajorEq (EGt exp1 exp2) (EGt exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EGtEq exp1 exp2) (EGtEq exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EId id) (EId id_) = id == id_ johnMajorEq (EIncr exp) (EIncr exp_) = exp == exp_ johnMajorEq (EInt n) (EInt n_) = n == n_ johnMajorEq (ELt exp1 exp2) (ELt exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (ELtEq exp1 exp2) (ELtEq exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EMinus exp1 exp2) (EMinus exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (ENEq exp1 exp2) (ENEq exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EOr exp1 exp2) (EOr exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EPDecr exp) (EPDecr exp_) = exp == exp_ johnMajorEq (EPIncr exp) (EPIncr exp_) = exp == exp_ johnMajorEq (EPlus exp1 exp2) (EPlus exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq (EString str) (EString str_) = str == str_ johnMajorEq (ETimes exp1 exp2) (ETimes exp1_ exp2_) = exp1 == exp1_ && exp2 == exp2_ johnMajorEq ETrue ETrue = P.True johnMajorEq (ETyped exp type_) (ETyped exp_ type__) = exp == exp_ && type_ == type__ johnMajorEq Type_bool Type_bool = P.True johnMajorEq Type_double Type_double = P.True johnMajorEq Type_int Type_int = P.True johnMajorEq Type_string Type_string = P.True johnMajorEq Type_void Type_void = P.True johnMajorEq (Id str) (Id str_) = str == str_ johnMajorEq _ _ = P.False compareSame :: Tree c -> Tree c -> P.Ordering compareSame (PDefs defs) (PDefs defs_) = P.compare defs defs_ compareSame (DFun type_ id args stms) (DFun type__ id_ args_ stms_) = P.mappend (P.compare type_ type__) (P.mappend (P.compare id id_) (P.mappend (P.compare args args_) (P.compare stms stms_))) compareSame (ADecl type_ id) (ADecl type__ id_) = P.mappend (P.compare type_ type__) (P.compare id id_) compareSame (SBlock stms) (SBlock stms_) = P.compare stms stms_ compareSame (SDecls type_ ids) (SDecls type__ ids_) = P.mappend (P.compare type_ type__) (P.compare ids ids_) compareSame (SExp exp) (SExp exp_) = P.compare exp exp_ compareSame (SIfElse exp stm1 stm2) (SIfElse exp_ stm1_ stm2_) = P.mappend (P.compare exp exp_) (P.mappend (P.compare stm1 stm1_) (P.compare stm2 stm2_)) compareSame (SInit type_ id exp) (SInit type__ id_ exp_) = P.mappend (P.compare type_ type__) (P.mappend (P.compare id id_) (P.compare exp exp_)) compareSame (SReturn exp) (SReturn exp_) = P.compare exp exp_ compareSame SReturnVoid SReturnVoid = P.EQ compareSame (SWhile exp stm) (SWhile exp_ stm_) = P.mappend (P.compare exp exp_) (P.compare stm stm_) compareSame (EAnd exp1 exp2) (EAnd exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EApp id exps) (EApp id_ exps_) = P.mappend (P.compare id id_) (P.compare exps exps_) compareSame (EAss exp1 exp2) (EAss exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EDecr exp) (EDecr exp_) = P.compare exp exp_ compareSame (EDiv exp1 exp2) (EDiv exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EDouble d) (EDouble d_) = P.compare d d_ compareSame (EEq exp1 exp2) (EEq exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame EFalse EFalse = P.EQ compareSame (EGt exp1 exp2) (EGt exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EGtEq exp1 exp2) (EGtEq exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EId id) (EId id_) = P.compare id id_ compareSame (EIncr exp) (EIncr exp_) = P.compare exp exp_ compareSame (EInt n) (EInt n_) = P.compare n n_ compareSame (ELt exp1 exp2) (ELt exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (ELtEq exp1 exp2) (ELtEq exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EMinus exp1 exp2) (EMinus exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (ENEq exp1 exp2) (ENEq exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EOr exp1 exp2) (EOr exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EPDecr exp) (EPDecr exp_) = P.compare exp exp_ compareSame (EPIncr exp) (EPIncr exp_) = P.compare exp exp_ compareSame (EPlus exp1 exp2) (EPlus exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame (EString str) (EString str_) = P.compare str str_ compareSame (ETimes exp1 exp2) (ETimes exp1_ exp2_) = P.mappend (P.compare exp1 exp1_) (P.compare exp2 exp2_) compareSame ETrue ETrue = P.EQ compareSame (ETyped exp type_) (ETyped exp_ type__) = P.mappend (P.compare exp exp_) (P.compare type_ type__) compareSame Type_bool Type_bool = P.EQ compareSame Type_double Type_double = P.EQ compareSame Type_int Type_int = P.EQ compareSame Type_string Type_string = P.EQ compareSame Type_void Type_void = P.EQ compareSame (Id str) (Id str_) = P.compare str str_ compareSame _ _ = P.error "BNFC error: compareSame"