tock-mirror/pregen/GenNavAST.hs
Adam Sampson 058a3488d9 Generate instances of a Navigable class.
This isn't immediately useful, but I plan to build on it.
2008-05-09 15:46:18 +00:00

75 lines
2.3 KiB
Haskell

{-
Tock: a compiler for parallel languages
Copyright (C) 2008 University of Kent
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
-}
-- | Utilities for metaprogramming.
module GenNavAST where
import Data.Generics
import qualified Data.Set as Set
import PregenUtils
import Utils
header :: [String]
header
= [ "{-# OPTIONS_GHC -Werror -fwarn-overlapping-patterns -fwarn-unused-matches -fwarn-unused-binds -fwarn-incomplete-patterns #-}"
, "-- | Instances that allow the AST to be navigated efficiently."
, "-- NOTE: This file is auto-generated by the GenNavAST program, "
, "-- and should not be edited directly."
, ""
, "module NavAST where"
, ""
, "import qualified AST"
, "import qualified Metadata"
, ""
, "data Navigation = Hit | Through | Miss"
, ""
, "class Navigable f t where"
, " navigate :: f -> t -> Navigation"
, ""
]
instancesFrom :: Data t => t -> [String]
instancesFrom w
= concat [inst c | DataBox c <- justBoxes $ astTypeMap]
where
wName = show $ typeOf w
wKey = typeKey w
containedKeys = Set.fromList [typeKey c
| DataBox c <- justBoxes $ findTypesIn w]
inst c
= [ "instance Navigable (" ++ wName ++ ") (" ++ cName ++ ") where"
, " navigate _ _ = " ++ result
, ""
]
where
cName = show $ typeOf c
cKey = typeKey c
result
| wKey == cKey = "Hit"
| cKey `Set.member` containedKeys = "Through"
| otherwise = "Miss"
main :: IO ()
main = putStr $ unlines $ header ++
concat [instancesFrom w
| DataBox w <- justBoxes $ astTypeMap]