Skip to content

Instantly share code, notes, and snippets.

@PkmX
Last active January 24, 2019 09:13
Show Gist options
  • Select an option

  • Save PkmX/1a78f337f3f82153755847e16e2bea94 to your computer and use it in GitHub Desktop.

Select an option

Save PkmX/1a78f337f3f82153755847e16e2bea94 to your computer and use it in GitHub Desktop.

Revisions

  1. PkmX revised this gist Jan 24, 2019. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion Interpolate.hs
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ plugin = defaultPlugin
    parsedPlugin :: [CommandLineOption] -> ModSummary -> HsParsedModule -> Hsc HsParsedModule
    parsedPlugin _ _ HsParsedModule{..} = do
    let hpm_module' = SYB.mkT transform `SYB.everywhere` hpm_module
    return $ HsParsedModule{ hpm_module = hpm_module', ..}
    return $ HsParsedModule{ hpm_module = hpm_module', .. }

    transform :: HsExpr GhcPs -> HsExpr GhcPs
    transform (HsLit _ (HsString _ fs)) = HsSpliceE NoExt $ mkHsQuasiQuote (mkVarUnqual "i") noSrcSpan fs
    2 changes: 1 addition & 1 deletion Main.hs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    {-# OPTIONS -fplugin=Interpolate #-}
    {-# OPTIONS_GHC -fplugin=Interpolate #-}

    module Main where

  2. PkmX revised this gist Jan 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Main.hs
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,4 @@ s :: String
    s = "#{a} * #{a} = #{a * a}"

    main :: IO ()
    main = putStrLn s
    main = putStrLn s -- 42 * 42 = 1764
  3. PkmX created this gist Jan 24, 2019.
    22 changes: 22 additions & 0 deletions Interpolate.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE RecordWildCards #-}

    module Interpolate (plugin) where

    import GhcPlugins
    import HsSyn
    import qualified Data.Generics as SYB

    plugin :: Plugin
    plugin = defaultPlugin
    { parsedResultAction = parsedPlugin
    }

    parsedPlugin :: [CommandLineOption] -> ModSummary -> HsParsedModule -> Hsc HsParsedModule
    parsedPlugin _ _ HsParsedModule{..} = do
    let hpm_module' = SYB.mkT transform `SYB.everywhere` hpm_module
    return $ HsParsedModule{ hpm_module = hpm_module', ..}

    transform :: HsExpr GhcPs -> HsExpr GhcPs
    transform (HsLit _ (HsString _ fs)) = HsSpliceE NoExt $ mkHsQuasiQuote (mkVarUnqual "i") noSrcSpan fs
    transform expr = expr
    14 changes: 14 additions & 0 deletions Main.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    {-# OPTIONS -fplugin=Interpolate #-}

    module Main where

    import Data.String.Interpolate

    a :: Int
    a = 42

    s :: String
    s = "#{a} * #{a} = #{a * a}"

    main :: IO ()
    main = putStrLn s