From 138b2a782834242adbcaeacd8bb058b94921cd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Hangh=C3=B8j=20Iversen?= Date: Fri, 11 Oct 2019 11:37:11 +0200 Subject: [PATCH] Small refactor --- ruby/test.rb | 12 ++++++++---- src/Rubyhs.hs | 13 +------------ src/Rubyhs/References.hs | 30 +++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/ruby/test.rb b/ruby/test.rb index 0e4a7ef..926c233 100644 --- a/ruby/test.rb +++ b/ruby/test.rb @@ -3,17 +3,21 @@ module M end def g - f(2) h(2, a: 'a') h(2, a: :a) + end + module N + def f + g + end + + def g + end end end -# class C -# end - module K end diff --git a/src/Rubyhs.hs b/src/Rubyhs.hs index cd1c37a..39a8f0f 100644 --- a/src/Rubyhs.hs +++ b/src/Rubyhs.hs @@ -12,11 +12,6 @@ import System.Environment (getArgs) import System.Process (readProcess) import qualified Data.ByteString.Lazy.Char8 as ByteString import Rubyhs.References (references) -import qualified Rubyhs.References as References -import Data.Text.Prettyprint.Doc.Render.Text (renderStrict) -import Data.Text.Prettyprint.Doc (layoutCompact) -import Data.HashMap.Strict (HashMap) -import GHC.Exts (fromList, toList) main :: IO () main = getArgs >>= traverse_ run @@ -25,13 +20,7 @@ run :: FilePath -> IO () run p = do json <- runParser p block <- decodeFail @_ @Block $ ByteString.pack json - ByteString.putStrLn $ encode block - let References.Env{declarations, applications} = references block - printEm declarations - printEm applications - where - go (x, y) = (renderStrict $ layoutCompact $ pretty x, y) - printEm x = ByteString.putStrLn $ encode $ fromList @(HashMap _ _) $ go <$> toList x + ByteString.putStrLn $ encode $ references block decodeFail :: MonadFail m => FromJSON a => ByteString -> m a decodeFail s = case eitherDecode s of diff --git a/src/Rubyhs/References.hs b/src/Rubyhs/References.hs index 7b7f25c..44ec58b 100644 --- a/src/Rubyhs/References.hs +++ b/src/Rubyhs/References.hs @@ -6,6 +6,7 @@ module Rubyhs.References , FQN(..) , references , Env(..) + , Result(..) ) where import Frelude @@ -14,14 +15,19 @@ import Data.Map (Map) import qualified Data.Map as Map import Data.Kind import Control.Monad.State +import Data.Aeson ((.=)) import qualified Data.Aeson as Aeson import Data.List +import Data.HashMap.Strict (HashMap) +import Data.Text.Prettyprint.Doc.Render.Text (renderStrict) +import Data.Text.Prettyprint.Doc (layoutCompact) data Entry a = Entry { node :: a , fqn :: FQN } +-- Names are in reverse order. newtype FQN = FQN ([] Name) deriving stock instance Show FQN @@ -36,7 +42,7 @@ instance IsList FQN where fromList l = FQN $ fromList l toList (FQN l) = toList l instance Pretty FQN where - pretty = pretty . intercalate "::" . fmap go . toList + pretty = pretty . intercalate "::" . fmap go . reverse . toList where go (Name (Aeson.Array [_, _, Aeson.String n])) = convertString n go x = show x @@ -56,6 +62,20 @@ data Env = Env , context :: FQN } +data Result = Result + { declarations :: Map FQN [Name] + , applications :: Map FQN [Name] + } +instance ToJSON Result where + toJSON Result{declarations,applications} = Aeson.object + [ "declarations" .= f declarations + , "applications" .= f applications + ] + where + f :: Map FQN [Name] -> HashMap Text [Name] + f x = fromList $ go <$> toList x + go (x, y) = (renderStrict $ layoutCompact $ pretty x, y) + instance Semigroup Env where Env a0 a1 a2 <> Env b0 b1 b2 = Env (a0 <> b0) (a1 <> b1) (a2 <> b2) @@ -65,9 +85,11 @@ instance Monoid Env where instance MyMonad (State Env) where declaration q n = modify go where + go :: Env -> Env go env@Env{declarations} = env { declarations = Map.insertWith mappend q (pure n) declarations } application q n = modify go where + go :: Env -> Env go env@Env{applications} = env { applications = Map.insertWith mappend q (pure n) applications } getContext = gets context writeContext q = modify go @@ -92,8 +114,10 @@ appendToContext n = updateContext go class References a where entries :: MyMonad m => a -> m () -references :: Block -> Env -references q = execState (entries @_ @(State Env) q) mempty +references :: Block -> Result +references q = Result{declarations, applications} + where + Env{declarations,applications} = execState (entries @_ @(State Env) q) mempty instance References Block where entries :: forall m . MyMonad m => Block -> m ()