From bc52411ae2ed26cab1d5086ae6df68f23ebbd052 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Wed, 10 Jun 2020 19:59:03 +0200 Subject: Initial state I found the code in --- utils/CC/Types.hs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 utils/CC/Types.hs (limited to 'utils/CC/Types.hs') diff --git a/utils/CC/Types.hs b/utils/CC/Types.hs new file mode 100644 index 0000000..dc1b0e5 --- /dev/null +++ b/utils/CC/Types.hs @@ -0,0 +1,44 @@ +module CC.Types where + +import CC.Pretty + + +-- | Names of variables in the program +type Name = String + +-- | Source metadata for compilation +data Context = Context FilePath + +-- | Position in a source file; `SourcePos line column`, both zero-based +data SourcePos = SourcePos Int Int + deriving (Show, Read, Eq, Ord) + +instance Pretty SourcePos where + pretty (SourcePos line col) = show (line + 1) ++ ":" ++ show (col + 1) + +-- | A range in the original source code (for diagnostics and debug +-- information); [from, to). +data SourceRange = SourceRange SourcePos SourcePos + deriving (Show, Read) + +instance Pretty SourceRange where + pretty (SourceRange from@(SourcePos fromLine fromCol) to@(SourcePos toLine toCol)) + | fromLine == toLine = + show (fromLine + 1) ++ ":" ++ show (fromCol + 1) ++ "-" ++ show (toCol + 1) + | otherwise = + show from ++ "-" ++ show to + +class HasRange a where + range :: a -> SourceRange + +mergeRange :: SourceRange -> SourceRange -> SourceRange +mergeRange (SourceRange p1 p2) (SourceRange q1 q2) = SourceRange (min p1 q1) (max p2 q2) + +-- | A newtype with no-op Read and Show instances +newtype RawString = RawString String + +instance Read RawString where + readsPrec _ s = [(RawString s, "")] + +instance Show RawString where + show (RawString s) = s -- cgit v1.2.3-70-g09d2