From 00a5a94c607bb904c6534925e985638f816e1fd9 Mon Sep 17 00:00:00 2001 From: hellish Date: Thu, 26 May 2005 17:49:10 +0000 Subject: [PATCH] Added mkHsValues helper function I was using this function in a project of mine, and I think it's generally useful. --- docs/hs-plugins.tex | 15 ++++++++++++++- src/plugins/System/Eval/Haskell.hs | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/hs-plugins.tex b/docs/hs-plugins.tex index 2c0a3f1..f3772a9 100644 --- a/docs/hs-plugins.tex +++ b/docs/hs-plugins.tex @@ -781,7 +781,20 @@ main = do fn <- unsafeEval "(\\(x::Int) -> (x,x))" [] :: IO (Maybe (Int -> (Int, when (isJust fn) $ putStrLn $ show $ (fromJust fn) 7 } \end{quote} - +\subsection{Utilities for use with eval} +\code{hs-plugins} proves the following utilities for use with \code{eval}: +\begin{itemize} +\item +\code{mkHsValues} is a helper function for converting \code{Data.Map}s +of names and values into Haskell code. It relies on the assumption +that the passed values' Show instances produce valid Haskell +literals (this is true for all prelude types). It's type is as follows: +\begin{quote} +\scm{ +mkHsValues :: (Show a) => Data.Map String a -> String +} +\end{quote} +\end{itemize} \subsection{Foreign Eval} A preliminary binding to \code{eval} has been implemented to allow C diff --git a/src/plugins/System/Eval/Haskell.hs b/src/plugins/System/Eval/Haskell.hs index 4b3d5d8..e46b98f 100644 --- a/src/plugins/System/Eval/Haskell.hs +++ b/src/plugins/System/Eval/Haskell.hs @@ -28,6 +28,7 @@ module System.Eval.Haskell ( unsafeEval, unsafeEval_, typeOf, + mkHsValues, hs_eval_b, -- return a Bool hs_eval_c, -- return a CChar @@ -46,6 +47,7 @@ import AltData.Dynamic ( Dynamic ) import AltData.Typeable ( Typeable ) import Data.Either +import Data.Map as Map import System.IO import System.Directory @@ -151,7 +153,15 @@ unsafeEval_ src mods args ldflags incs = do MakeFailure err -> return $ Left err makeCleaner tmpf return e_rsrc - +------------------------------------------------------------------------ +-- +-- Convenience function for use with eval (and friends). Returns a +-- string of Haskell code with the Data.Map passed as values. +-- +mkHsValues :: (Show a) => Map.Map String a -> String +mkHsValues values = concat $ elems $ Map.mapWithKey convertToHs values + where convertToHs :: (Show a) => String -> a -> String + convertToHs name value = name ++ " = " ++ show value ++ "\n" ------------------------------------------------------------------------ -- -- return a compiled value's type, by using Dynamic to get a