2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
{-# language Strict #-}
|
|
|
|
|
2020-06-04 13:48:04 +04:00
|
|
|
{- | /The/ scope resolution system.
|
2020-05-21 23:28:26 +04:00
|
|
|
-}
|
|
|
|
|
2020-06-04 13:48:04 +04:00
|
|
|
module AST.Scope
|
2020-07-03 20:35:58 +04:00
|
|
|
-- ( HasLocalScope (..)
|
|
|
|
-- , addLocalScopes
|
|
|
|
-- , lookupEnv
|
|
|
|
-- , Kind (..)
|
|
|
|
-- , ScopedDecl (..)
|
|
|
|
-- )
|
2020-06-04 13:48:04 +04:00
|
|
|
where
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-07-01 16:56:21 +04:00
|
|
|
import Control.Arrow (second)
|
|
|
|
import Control.Monad.State
|
|
|
|
import Control.Monad.Writer.Strict hiding (Alt, Product)
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
import Data.Function
|
2020-07-01 16:56:21 +04:00
|
|
|
import qualified Data.List as List
|
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import Data.Maybe (fromJust, listToMaybe)
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as Text
|
|
|
|
|
|
|
|
import AST.Parser
|
|
|
|
import AST.Types
|
|
|
|
import Comment
|
|
|
|
import Lattice
|
|
|
|
import Parser
|
|
|
|
import Pretty
|
|
|
|
import Product
|
|
|
|
import Range
|
|
|
|
import Tree
|
|
|
|
|
|
|
|
import Debug.Trace
|
|
|
|
|
|
|
|
-- | Ability to contain a list of declarations.
|
2020-06-17 22:05:44 +04:00
|
|
|
class HasLocalScope x where
|
|
|
|
getLocalScope :: x -> [ScopedDecl]
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance Contains [ScopedDecl] xs => HasLocalScope (Product xs) where
|
|
|
|
getLocalScope = getElem
|
2020-06-04 17:40:38 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
type CollectM = State (Product [FullEnv, [Range]])
|
2020-06-10 22:37:02 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
type AddRefsM = State FullEnv
|
2020-06-10 22:37:02 +04:00
|
|
|
|
2020-07-08 20:31:42 +04:00
|
|
|
data FullEnv = FullEnv
|
|
|
|
{ vars :: Env
|
|
|
|
, types :: Env
|
|
|
|
}
|
|
|
|
|
|
|
|
data Category = Variable | Type
|
|
|
|
|
|
|
|
emptyEnv = FullEnv Map.empty Map.empty
|
|
|
|
|
|
|
|
with Variable (FullEnv vs ts) f = FullEnv (f vs) ts
|
|
|
|
with Type (FullEnv vs ts) f = FullEnv vs (f ts)
|
|
|
|
|
|
|
|
grab Variable (FullEnv vs ts) = vs
|
|
|
|
grab Type (FullEnv vs ts) = ts
|
|
|
|
|
|
|
|
type Env = Map Range [ScopedDecl]
|
|
|
|
|
|
|
|
ofCategory Variable ScopedDecl { _sdType = Just (Right Star) } = False
|
|
|
|
ofCategory Variable _ = True
|
|
|
|
ofCategory Type ScopedDecl { _sdType = Just (Right Star) } = True
|
|
|
|
ofCategory _ _ = False
|
2020-06-09 15:56:11 +04:00
|
|
|
|
2020-07-01 16:56:21 +04:00
|
|
|
-- | Calculate scopes and attach to all tree points declarations that are
|
|
|
|
-- visible there.
|
|
|
|
--
|
2020-06-17 22:05:44 +04:00
|
|
|
addLocalScopes
|
2020-07-08 20:31:42 +04:00
|
|
|
:: Contains Range xs
|
2020-06-17 22:05:44 +04:00
|
|
|
=> Pascal (Product xs)
|
2020-07-08 20:31:42 +04:00
|
|
|
-> Pascal (Product ([ScopedDecl] : Maybe Category : xs))
|
2020-06-17 22:05:44 +04:00
|
|
|
addLocalScopes tree =
|
2020-07-08 20:31:42 +04:00
|
|
|
fmap (\xs -> Cons (fullEnvAt envWithREfs (getRange xs)) xs) tree1
|
2020-07-03 20:35:58 +04:00
|
|
|
where
|
2020-07-08 20:31:42 +04:00
|
|
|
tree1 = addNameCategories tree
|
2020-07-03 20:35:58 +04:00
|
|
|
envWithREfs = getEnvTree tree
|
|
|
|
|
2020-07-08 20:31:42 +04:00
|
|
|
addNameCategories
|
|
|
|
:: Contains Range xs
|
|
|
|
=> Pascal (Product xs)
|
|
|
|
-> Pascal (Product (Maybe Category : xs))
|
|
|
|
addNameCategories tree = flip evalState emptyEnv do
|
|
|
|
traverseMany
|
|
|
|
[ Visit \r (Name t) -> do
|
|
|
|
modify $ getRange r `addRef` (Variable, t)
|
|
|
|
return $ (Cons (Just Variable) r, Name t)
|
|
|
|
|
|
|
|
, Visit \r (TypeName t) -> do
|
|
|
|
modify $ getRange r `addRef` (Type, t)
|
|
|
|
return $ (Cons (Just Type) r, TypeName t)
|
|
|
|
]
|
|
|
|
(Cons Nothing)
|
|
|
|
tree
|
|
|
|
|
2020-07-03 20:35:58 +04:00
|
|
|
getEnvTree tree = envWithREfs
|
2020-06-17 22:05:44 +04:00
|
|
|
where
|
|
|
|
envWithREfs = flip execState env do
|
2020-07-08 20:31:42 +04:00
|
|
|
traverseMany
|
|
|
|
[ Visit \r (Name t) -> do
|
|
|
|
modify $ getRange r `addRef` (Variable, t)
|
|
|
|
return $ (r, Name t)
|
|
|
|
|
|
|
|
, Visit \r (TypeName t) -> do
|
|
|
|
modify $ getRange r `addRef` (Type, t)
|
|
|
|
return $ (r, TypeName t)
|
|
|
|
]
|
|
|
|
id
|
|
|
|
tree
|
2020-06-17 22:05:44 +04:00
|
|
|
|
|
|
|
env
|
|
|
|
= execCollectM
|
|
|
|
$ traverseTree pure tree
|
|
|
|
|
2020-07-08 20:31:42 +04:00
|
|
|
fullEnvAt :: FullEnv -> Range -> [ScopedDecl]
|
|
|
|
fullEnvAt fe r = envAt (grab Type fe) r <> envAt (grab Variable fe) r
|
|
|
|
|
|
|
|
envAt :: Env -> Range -> [ScopedDecl]
|
2020-06-17 22:05:44 +04:00
|
|
|
envAt env pos =
|
|
|
|
Map.elems scopes
|
|
|
|
where
|
|
|
|
ranges = List.sortBy partOrder $ filter isCovering $ Map.keys env
|
|
|
|
scopes = Map.unions $ (map.foldMap) toScopeMap $ map (env Map.!) ranges
|
2020-06-10 22:37:02 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
isCovering = (pos <?)
|
|
|
|
toScopeMap sd@ScopedDecl {_sdName} = Map.singleton (ppToText _sdName) sd
|
2020-06-10 22:37:02 +04:00
|
|
|
|
2020-07-08 20:31:42 +04:00
|
|
|
addRef :: Range -> (Category, Text) -> FullEnv -> FullEnv
|
|
|
|
addRef r (cat, n) env =
|
|
|
|
with cat env \slice ->
|
|
|
|
Map.union
|
|
|
|
(go slice $ range slice)
|
|
|
|
slice
|
2020-06-17 22:05:44 +04:00
|
|
|
where
|
2020-07-08 20:31:42 +04:00
|
|
|
go slice (r' : rest) =
|
|
|
|
let decls = slice Map.! r'
|
2020-06-17 22:05:44 +04:00
|
|
|
in
|
|
|
|
case updateOnly n r addRefToDecl decls of
|
|
|
|
(True, decls) -> Map.singleton r' decls
|
2020-07-08 20:31:42 +04:00
|
|
|
(False, decls) -> Map.insert r' decls (go slice rest)
|
|
|
|
go _ [] = Map.empty
|
2020-06-17 22:05:44 +04:00
|
|
|
|
2020-07-08 20:31:42 +04:00
|
|
|
range slice
|
2020-06-17 22:05:44 +04:00
|
|
|
= List.sortBy partOrder
|
|
|
|
$ filter (r <?)
|
2020-07-08 20:31:42 +04:00
|
|
|
$ Map.keys slice
|
2020-06-17 22:05:44 +04:00
|
|
|
|
|
|
|
addRefToDecl sd = sd
|
|
|
|
{ _sdRefs = r : _sdRefs sd
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOnly
|
|
|
|
:: Text
|
|
|
|
-> Range
|
|
|
|
-> (ScopedDecl -> ScopedDecl)
|
|
|
|
-> [ScopedDecl]
|
|
|
|
-> (Bool, [ScopedDecl])
|
|
|
|
updateOnly name r f = go
|
|
|
|
where
|
|
|
|
go = \case
|
|
|
|
d : ds
|
|
|
|
| ppToText (_sdName d) == name ->
|
|
|
|
if r == _sdOrigin d
|
|
|
|
then (True, d : ds)
|
|
|
|
else (True, f d : ds)
|
|
|
|
| otherwise -> second (d :) (go ds)
|
|
|
|
|
|
|
|
[] -> (False, [])
|
|
|
|
|
|
|
|
enter :: Range -> CollectM ()
|
|
|
|
enter r = do
|
|
|
|
modify $ modElem (r :)
|
|
|
|
|
2020-07-08 20:31:42 +04:00
|
|
|
define :: Category -> ScopedDecl -> CollectM ()
|
|
|
|
define cat sd = do
|
2020-06-17 22:05:44 +04:00
|
|
|
r <- gets (head . getElem)
|
|
|
|
modify
|
2020-07-08 20:31:42 +04:00
|
|
|
$ modElem @FullEnv \env ->
|
|
|
|
with cat env
|
|
|
|
$ Map.insertWith (++) r [sd]
|
2020-06-17 22:05:44 +04:00
|
|
|
|
|
|
|
leave :: CollectM ()
|
|
|
|
leave = modify $ modElem @[Range] tail
|
|
|
|
|
|
|
|
-- | Run the computation with scope starting from empty scope.
|
|
|
|
execCollectM :: CollectM a -> FullEnv
|
2020-07-08 20:31:42 +04:00
|
|
|
execCollectM action = getElem $ execState action $ Cons emptyEnv (Cons [] Nil)
|
2020-06-17 22:05:44 +04:00
|
|
|
|
|
|
|
instance {-# OVERLAPS #-} Pretty FullEnv where
|
2020-07-08 20:31:42 +04:00
|
|
|
pp = block . map aux . Map.toList . mergeFE
|
2020-06-17 22:05:44 +04:00
|
|
|
where
|
2020-07-08 20:31:42 +04:00
|
|
|
aux (r, fe) =
|
|
|
|
pp r `indent` block fe
|
|
|
|
|
|
|
|
mergeFE (FullEnv a b) = a <> b
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-04 17:40:38 +04:00
|
|
|
-- | The type/value declaration.
|
2020-05-21 23:28:26 +04:00
|
|
|
data ScopedDecl = ScopedDecl
|
2020-06-17 22:05:44 +04:00
|
|
|
{ _sdName :: Pascal ()
|
|
|
|
, _sdOrigin :: Range
|
|
|
|
, _sdBody :: Maybe Range
|
|
|
|
, _sdType :: Maybe (Either (Pascal ()) Kind)
|
|
|
|
, _sdRefs :: [Range]
|
2020-05-21 23:28:26 +04:00
|
|
|
}
|
2020-06-10 22:37:02 +04:00
|
|
|
deriving Show via PP ScopedDecl
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-09 15:56:11 +04:00
|
|
|
instance Pretty ScopedDecl where
|
2020-06-17 22:05:44 +04:00
|
|
|
pp (ScopedDecl n o b t refs) = color 3 (pp n) <+> pp o <+> ":" <+> color 4 (maybe "?" (either pp pp) t) <+> "=" <+> pp refs
|
2020-06-09 15:56:11 +04:00
|
|
|
|
2020-06-04 17:40:38 +04:00
|
|
|
-- | The kind.
|
2020-05-21 23:28:26 +04:00
|
|
|
data Kind = Star
|
2020-06-09 15:56:11 +04:00
|
|
|
deriving Show via PP Kind
|
|
|
|
|
|
|
|
instance Pretty Kind where
|
2020-06-17 22:05:44 +04:00
|
|
|
pp _ = "TYPE"
|
|
|
|
|
2020-07-01 16:56:21 +04:00
|
|
|
-- | Search for a name inside a local scope.
|
2020-06-17 22:05:44 +04:00
|
|
|
lookupEnv :: Text -> [ScopedDecl] -> Maybe ScopedDecl
|
|
|
|
lookupEnv name = listToMaybe . filter ((name ==) . ppToText . _sdName)
|
|
|
|
|
2020-06-04 17:40:38 +04:00
|
|
|
-- | Add a type declaration to the current scope.
|
2020-06-17 22:05:44 +04:00
|
|
|
defType :: HasRange a => Pascal a -> Kind -> Pascal a -> CollectM ()
|
2020-06-01 18:17:33 +04:00
|
|
|
defType name kind body = do
|
2020-07-08 20:31:42 +04:00
|
|
|
define Type
|
2020-06-10 22:37:02 +04:00
|
|
|
$ ScopedDecl
|
2020-06-17 22:05:44 +04:00
|
|
|
(void name)
|
2020-06-10 22:37:02 +04:00
|
|
|
(getRange $ infoOf name)
|
|
|
|
(Just $ getRange $ infoOf body)
|
|
|
|
(Just (Right kind))
|
|
|
|
[]
|
|
|
|
|
2020-07-01 16:56:21 +04:00
|
|
|
-- observe :: Pretty i => Pretty res => Text -> i -> res -> res
|
|
|
|
-- observe msg i res
|
|
|
|
-- = traceShow (pp msg, "INPUT", pp i)
|
|
|
|
-- $ traceShow (pp msg, "OUTPUT", pp res)
|
|
|
|
-- $ res
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-04 17:40:38 +04:00
|
|
|
-- | Add a value declaration to the current scope.
|
2020-05-21 23:28:26 +04:00
|
|
|
def
|
2020-06-01 18:17:33 +04:00
|
|
|
:: HasRange a
|
|
|
|
=> Pascal a
|
|
|
|
-> Maybe (Pascal a)
|
|
|
|
-> Maybe (Pascal a)
|
2020-06-17 22:05:44 +04:00
|
|
|
-> CollectM ()
|
2020-06-01 18:17:33 +04:00
|
|
|
def name ty body = do
|
2020-07-08 20:31:42 +04:00
|
|
|
define Variable
|
2020-06-10 22:37:02 +04:00
|
|
|
$ ScopedDecl
|
2020-06-17 22:05:44 +04:00
|
|
|
(void name)
|
2020-06-10 22:37:02 +04:00
|
|
|
(getRange $ infoOf name)
|
|
|
|
((getRange . infoOf) <$> body)
|
|
|
|
((Left . void) <$> ty)
|
|
|
|
[]
|
2020-06-01 18:17:33 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance UpdateOver CollectM Contract (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
before r _ = enter r
|
2020-06-17 22:05:44 +04:00
|
|
|
after _ _ = skip
|
2020-06-01 18:17:33 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance HasRange a => UpdateOver CollectM Declaration (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
before _ = \case
|
2020-06-01 18:17:33 +04:00
|
|
|
TypeDecl ty body -> defType ty Star body
|
|
|
|
_ -> skip
|
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance HasRange a => UpdateOver CollectM Binding (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
before r = \case
|
2020-06-04 17:16:04 +04:00
|
|
|
Function recur name _args ty body -> do
|
2020-05-21 23:28:26 +04:00
|
|
|
when recur do
|
2020-06-01 18:17:33 +04:00
|
|
|
def name (Just ty) (Just body)
|
2020-06-10 22:37:02 +04:00
|
|
|
enter r
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-10 22:37:02 +04:00
|
|
|
_ -> enter r
|
2020-05-21 23:28:26 +04:00
|
|
|
|
2020-06-10 22:37:02 +04:00
|
|
|
after _ = \case
|
2020-06-01 18:17:33 +04:00
|
|
|
Irrefutable name body -> do leave; def name Nothing (Just body)
|
|
|
|
Var name ty body -> do leave; def name (Just ty) (Just body)
|
|
|
|
Const name ty body -> do leave; def name (Just ty) (Just body)
|
2020-06-04 17:16:04 +04:00
|
|
|
Function recur name _args ty body -> do
|
2020-06-01 18:17:33 +04:00
|
|
|
leave
|
2020-05-21 23:28:26 +04:00
|
|
|
unless recur do
|
2020-06-01 18:17:33 +04:00
|
|
|
def name (Just ty) (Just body)
|
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance HasRange a => UpdateOver CollectM VarDecl (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
after _ (Decl _ name ty) = def name (Just ty) Nothing
|
2020-06-01 18:17:33 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance UpdateOver CollectM Mutable (Pascal a)
|
|
|
|
instance UpdateOver CollectM Type (Pascal a)
|
|
|
|
instance UpdateOver CollectM Variant (Pascal a)
|
|
|
|
instance UpdateOver CollectM TField (Pascal a)
|
2020-06-01 18:17:33 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance HasRange a => UpdateOver CollectM Expr (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
before r = \case
|
|
|
|
Let {} -> enter r
|
|
|
|
Lambda {} -> enter r
|
2020-06-01 18:17:33 +04:00
|
|
|
ForLoop k _ _ _ -> do
|
2020-06-10 22:37:02 +04:00
|
|
|
enter r
|
2020-06-01 18:17:33 +04:00
|
|
|
def k Nothing Nothing
|
|
|
|
|
|
|
|
ForBox k mv _ _ _ -> do
|
2020-06-10 22:37:02 +04:00
|
|
|
enter r
|
2020-06-01 18:17:33 +04:00
|
|
|
def k Nothing Nothing
|
|
|
|
maybe skip (\v -> def v Nothing Nothing) mv
|
|
|
|
|
|
|
|
_ -> skip
|
|
|
|
|
2020-06-10 22:37:02 +04:00
|
|
|
after _ = \case
|
2020-06-01 18:17:33 +04:00
|
|
|
Let {} -> leave
|
|
|
|
Lambda {} -> leave
|
|
|
|
ForLoop {} -> leave
|
|
|
|
ForBox {} -> leave
|
|
|
|
_ -> skip
|
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance HasRange a => UpdateOver CollectM Alt (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
before r _ = enter r
|
|
|
|
after _ _ = leave
|
2020-06-01 18:17:33 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance UpdateOver CollectM LHS (Pascal a)
|
|
|
|
instance UpdateOver CollectM MapBinding (Pascal a)
|
|
|
|
instance UpdateOver CollectM Assignment (Pascal a)
|
|
|
|
instance UpdateOver CollectM FieldAssignment (Pascal a)
|
|
|
|
instance UpdateOver CollectM Constant (Pascal a)
|
2020-06-01 18:17:33 +04:00
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance HasRange a => UpdateOver CollectM Pattern (Pascal a) where
|
2020-06-10 22:37:02 +04:00
|
|
|
before _ = \case
|
2020-06-01 18:17:33 +04:00
|
|
|
IsVar n -> def n Nothing Nothing
|
|
|
|
_ -> skip
|
|
|
|
|
2020-06-17 22:05:44 +04:00
|
|
|
instance UpdateOver CollectM QualifiedName (Pascal a)
|
|
|
|
instance UpdateOver CollectM Path (Pascal a)
|
2020-07-08 20:31:42 +04:00
|
|
|
instance UpdateOver CollectM Name (Pascal a)
|
|
|
|
instance UpdateOver CollectM TypeName (Pascal a)
|
|
|
|
instance UpdateOver CollectM FieldName (Pascal a)
|