aboutsummaryrefslogtreecommitdiff
path: root/utils/CC/Types.hs
diff options
context:
space:
mode:
Diffstat (limited to 'utils/CC/Types.hs')
-rw-r--r--utils/CC/Types.hs44
1 files changed, 44 insertions, 0 deletions
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