Skip to content
Browse files

added ghci-lib, starting moving things there

  • Loading branch information...
1 parent bfb0af8 commit d63bb473c2ef59cad44f57620cf8af7043ba73e9 @gibiansky gibiansky committed Feb 6, 2014
View
20 ghci-lib/LICENSE
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+
+Copyright (c) 2013 Andrew Gibiansky
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
76 ghci-lib/Language/Haskell/GHC/Interpret.hs
@@ -0,0 +1,76 @@
+module Language.Haskell.GHC.Interpret (
+ -- Initialize GHC API.
+ initGhci,
+
+ -- Evaluation
+ {-
+ evalStatements,
+ evalExpression,
+ -}
+ evalImport,
+ {-
+ evalDeclarations,
+ setExtension,
+ setFlag,
+ getType,
+ loadFile,
+ -}
+ ) where
+
+import InteractiveEval
+import GHC
+import DynFlags
+import GhcMonad
+import HsImpExp
+import HscTypes
+import RdrName
+
+import Data.Function (on)
+import Control.Monad (void)
+
+-- | Initialize the GHC API. Run this as the first thing in the `runGhc`.
+initGhci :: GhcMonad m => m ()
+initGhci = do
+ -- Initialize dyn flags.
+ -- Start with -XExtendedDefaultRules and -XNoMonomorphismRestriction.
+ originalFlags <- getSessionDynFlags
+ let flag = flip xopt_set
+ unflag = flip xopt_unset
+ dflags = flag Opt_ExtendedDefaultRules . unflag Opt_MonomorphismRestriction $ originalFlags
+
+ void $ setSessionDynFlags $ dflags { hscTarget = HscInterpreted,
+ ghcLink = LinkInMemory,
+ pprCols = 300 }
+
+evalImport :: GhcMonad m => String -> m ()
+evalImport imports = do
+ importDecl <- parseImportDecl imports
+ context <- getContext
+
+ -- If we've imported this implicitly, remove the old import.
+ let noImplicit = filter (not . implicitImportOf importDecl) context
+
+ -- If this is a `hiding` import, remove previous non-`hiding` imports.
+ oldImps = if isHiddenImport importDecl
+ then filter (not . importOf importDecl) context
+ else noImplicit
+
+ -- Replace the context.
+ setContext $ IIDecl importDecl : oldImps
+
+ where
+ -- Check whether an import is the same as another import (same module).
+ importOf :: ImportDecl RdrName -> InteractiveImport -> Bool
+ importOf _ (IIModule _) = False
+ importOf imp (IIDecl decl) = ((==) `on` (unLoc . ideclName)) decl imp
+
+ -- Check whether an import is an *implicit* import of something.
+ implicitImportOf :: ImportDecl RdrName -> InteractiveImport -> Bool
+ implicitImportOf _ (IIModule _) = False
+ implicitImportOf imp (IIDecl decl) = ideclImplicit decl && imp `importOf` IIDecl decl
+
+ -- Check whether an import is hidden.
+ isHiddenImport :: ImportDecl RdrName -> Bool
+ isHiddenImport imp = case ideclHiding imp of
+ Just (True, _) -> True
+ _ -> False
View
2 ghci-lib/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
View
26 ghci-lib/ghci-lib.cabal
@@ -0,0 +1,26 @@
+-- Initial ghci-lib.cabal generated by cabal init. For further
+-- documentation, see http://haskell.org/cabal/users-guide/
+
+name: ghci-lib
+version: 0.1.0.0
+synopsis: A library for interactively evaluating Haskell code.
+-- description:
+homepage: http://github.com/gibiansky/IHaskell
+license: MIT
+license-file: LICENSE
+author: Andrew Gibiansky
+maintainer: andrew.gibiansky@gmail.com
+-- copyright:
+category: Language
+build-type: Simple
+-- extra-source-files:
+cabal-version: >=1.10
+
+library
+ exposed-modules: Language.Haskell.GHC.Interpret
+ -- other-modules:
+ -- other-extensions:
+ build-depends: base >=4.6 && <4.7,
+ ghc==7.6.*
+ -- hs-source-dirs:
+ default-language: Haskell2010
View
7 ihaskell.cabal
@@ -65,6 +65,7 @@ library
filepath -any,
ghc ==7.6.*,
ghc-parser >=0.1.1,
+ ghci-lib >=0.1,
ghc-paths ==0.1.*,
haskeline -any,
here -any,
@@ -141,7 +142,8 @@ executable IHaskell
directory -any,
filepath -any,
ghc ==7.6.*,
- ghc-parser -any,
+ ghc-parser >=0.1.1,
+ ghci-lib >=0.1,
ghc-paths ==0.1.*,
haskeline -any,
here -any,
@@ -183,7 +185,8 @@ Test-Suite hspec
directory -any,
filepath -any,
ghc ==7.6.*,
- ghc-parser -any,
+ ghc-parser >=0.1.1,
+ ghci-lib >=0.1,
ghc-paths ==0.1.*,
haskeline -any,
here -any,
View
525 notebooks/IHaskell.ipynb
465 additions, 60 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2 src/IHaskell/Eval/Evaluate.hs
@@ -104,8 +104,6 @@ globalImports =
, "import qualified System.IO as IHaskellSysIO"
]
-
-
-- | Run an interpreting action. This is effectively runGhc with
-- initialization and importing. First argument indicates whether `stdin`
-- is handled specially, which cannot be done in a testing environment.

0 comments on commit d63bb47

Please sign in to comment.
Something went wrong with that request. Please try again.