ghc api / calling functions?

Lemmih lemmih at gmail.com
Tue May 16 09:16:08 EDT 2006


On 5/16/06, Maarten <maarten at snowlion.nl> wrote:
> Dear all,
>
> Is there a way to use the ghc api to call functions directly instead of
> evaluating statements (more or less similar to hs-plugins)?
>
> So far, thanks to some info pages, I have been able to load sessions and
> make modules, however there seems to be little or no documentation on
> calling functions directly. Any info/pointers appreciated. Thanks.

Here's some code from hIDE:

compileSymbol :: Session -> String -> IO (HValue, Type)
compileSymbol session symbol
    = do name <- fmap expectOneName (GHC.parseName session symbol)
         Just tything <- GHC.lookupName session name
         let globalId = getGlobalId  tything
         hscEnv <- GHC.sessionHscEnv session
         unlinked <- coreExprToBCOs (hsc_dflags hscEnv) (Var globalId)
         hvalue <- linkExpr hscEnv unlinked
         return (hvalue, idType globalId)
    where getGlobalId (AnId id) | isGlobalId id = id
          getGlobalId (AnId _) = error $ symbol ++ ": not a global id"
          getGlobalId (ADataCon _) = error $ symbol ++ ": is a data constructor"
          getGlobalId (ATyCon _) = error $ symbol ++ ": is a type constructor"
          getGlobalId (AClass _) = error $ symbol ++ ": is a class"
          expectOneName [x] = x
          expectOneName [] = error $ "No such symbol in plugin: " ++ symbol
          expectOneName _ = error $ "Ambiguous symbol in plugin: " ++ symbol

-- 
Friendly,
  Lemmih


More information about the Glasgow-haskell-users mailing list