-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathReify.hs
More file actions
63 lines (48 loc) · 2.32 KB
/
Reify.hs
File metadata and controls
63 lines (48 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{-# LANGUAGE FlexibleInstances #-}
-- | Module Reify provides a typeclass and instances for turning internal compiler types and data into
-- corresponding representations in the Carp language.
module Reify where
import Info
import Obj
import System.FilePath
import Types
-- | The Reifiable class ranges over internal Carp compiler types that
-- may have corresponding representations in Carp itself.
class Reifiable a where
reify :: a -> XObj
symbol :: Show a => a -> XObj
symbol x = XObj (Sym (SymPath [] (show x)) Symbol) Nothing Nothing
-- Show on strings results in a symbol that includes quotes ""
-- This function is the same as symbol, for string literals.
literal :: String -> XObj
literal x = XObj (Sym (SymPath [] x) Symbol) Nothing Nothing
array :: (Reifiable a) => [a] -> XObj
array x = XObj (Arr (map reify x)) Nothing Nothing
lifetime :: Show a => a -> XObj
lifetime x = literal ("<" ++ show x ++ ">")
-- Types
instance Reifiable Kind where
reify k = symbol k
instance Reifiable Ty where
reify (StructTy t []) = reify t
reify (StructTy t vs) = XObj (Lst (reify t : map reify vs)) Nothing (Just TypeTy)
reify (RefTy t lt) = XObj (Lst [literal "Ref", reify t, lifetime lt]) Nothing (Just TypeTy)
reify (PointerTy t) = XObj (Lst [literal "Ptr", reify t]) Nothing (Just TypeTy)
reify (FuncTy ats rt lt) = XObj (Lst [literal "Fn", array ats, reify rt, lifetime lt]) Nothing (Just TypeTy)
reify TypeTy = XObj (Sym (SymPath [] (show TypeTy)) Symbol) Nothing (Just Universe)
reify UnitTy = XObj (Sym (SymPath [] "Unit") Symbol) Nothing (Just TypeTy)
reify (ConcreteNameTy path) = XObj (Sym path Symbol) Nothing (Just TypeTy)
reify t = XObj (Sym (SymPath [] (show t)) Symbol) Nothing (Just TypeTy)
instance Reifiable String where
reify s = XObj (Str s) Nothing (Just StringTy)
instance Reifiable Int where
reify i = XObj (Num IntTy (fromIntegral i)) Nothing (Just IntTy)
getInfoAsXObj :: (Reifiable a) => (Info -> a) -> Maybe Info -> Maybe XObj
getInfoAsXObj f = fmap (reify . f)
getFileAsXObj :: FilePathPrintLength -> Maybe Info -> Maybe XObj
getFileAsXObj FullPath = getInfoAsXObj infoFile
getFileAsXObj ShortPath = getInfoAsXObj (takeFileName . infoFile)
getLineAsXObj :: Maybe Info -> Maybe XObj
getLineAsXObj = getInfoAsXObj infoLine
getColumnAsXObj :: Maybe Info -> Maybe XObj
getColumnAsXObj = getInfoAsXObj infoColumn