Skip to content

Commit de379d0

Browse files
committed
Add a --pretty-thunks flag
1 parent 6ea964a commit de379d0

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

src/Fay.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,20 @@ compileToModule filepath config raw with hscode = do
138138
, state
139139
)
140140
where
141-
pw = execPrinter (runtime <> printer <> main) pr
141+
pw = execPrinter (runtime <> aliases <> printer <> main) pr
142142
runtime = whenP (configExportRuntime config) $
143143
write raw
144+
aliases = whenP (configPrettyThunks config) $
145+
write . unlines $ [ "var $ = Fay$$$;"
146+
, "var _ = Fay$$_;"
147+
, "var __ = Fay$$__;"
148+
]
144149
main = whenP (not $ configLibrary config) $
145150
write $ "Fay$$_(" ++ modulename ++ ".main, true);\n"
146-
pr = defaultPrintReader { prPretty = configPrettyPrint config }
151+
pr = defaultPrintReader
152+
{ prPrettyThunks = configPrettyThunks config
153+
, prPretty = configPrettyPrint config
154+
}
147155

148156
-- | Convert a Haskell filename to a JS filename.
149157
toJsName :: String -> String

src/Fay/Compiler/Print.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ printJSString x = concat . reverse . pwOutput $ execPrinter (printJS x) defaultP
3636

3737
-- | Print the JS to a pretty string.
3838
printJSPretty :: Printable a => a -> String
39-
printJSPretty x = concat . reverse . pwOutput $ execPrinter (printJS x) defaultPrintReader { prPretty = True }
39+
printJSPretty x = concat . reverse . pwOutput $ execPrinter (printJS x) defaultPrintReader{ prPretty = True }
4040

4141
-- | Print literals. These need some special encoding for
4242
-- JS-format literals. Could use the Text.JSON library.
@@ -218,15 +218,17 @@ instance Printable JsName where
218218
case name of
219219
JsNameVar qname -> printJS qname
220220
JsThis -> write "this"
221-
JsThunk -> write "Fay$$$"
222-
JsForce -> write "Fay$$_"
223-
JsApply -> write "Fay$$__"
221+
JsThunk -> writeThunkish "Fay$$$" "$"
222+
JsForce -> writeThunkish "Fay$$_" "_"
223+
JsApply -> writeThunkish "Fay$$__" "__"
224224
JsParam i -> write ("$p" ++ show i)
225225
JsTmp i -> write ("$tmp" ++ show i)
226226
JsConstructor qname -> printCons qname
227227
JsBuiltIn qname -> "Fay$$" +> printJS qname
228228
JsParametrizedType -> write "type"
229229
JsModuleName (ModuleName _ m) -> write m
230+
where writeThunkish ugly pretty = askP $ \pr ->
231+
write $ if prPrettyThunks pr then pretty else ugly
230232

231233
-- | Print a constructor name given a QName.
232234
printCons :: N.QName -> Printer

src/Fay/Config.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Fay.Config
2323
, configTypecheckOnly
2424
, configRuntimePath
2525
, configOptimizeNewtypes
26+
, configPrettyThunks
2627
)
2728
, defaultConfig
2829
, defaultConfigWithSandbox
@@ -72,6 +73,7 @@ data Config = Config
7273
, configTypecheckOnly :: Bool -- ^ Only invoke GHC for typechecking, don't produce any output
7374
, configRuntimePath :: Maybe FilePath
7475
, configOptimizeNewtypes :: Bool -- ^ Optimize away newtype constructors?
76+
, configPrettyThunks :: Bool -- ^ Use pretty thunk names?
7577
} deriving (Show)
7678

7779

@@ -101,6 +103,7 @@ defaultConfig = addConfigPackage "fay-base"
101103
, configRuntimePath = Nothing
102104
, configSourceMap = False
103105
, configOptimizeNewtypes = True
106+
, configPrettyThunks = False
104107
}
105108

106109
defaultConfigWithSandbox :: IO Config

src/Fay/Types.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ liftModuleT = Compile . lift . lift
126126
-- | Global options of the printer
127127
data PrintReader = PrintReader
128128
{ prPretty :: Bool -- ^ Are we to pretty print?
129+
, prPrettyThunks :: Bool -- ^ Use pretty thunk names?
129130
}
130131

131132
defaultPrintReader :: PrintReader
132-
defaultPrintReader = PrintReader False
133+
defaultPrintReader = PrintReader False False
133134

134135

135136
-- | Output of printer

src/main/Main.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ data FayCompilerOptions = FayCompilerOptions
4242
, optSourceMap :: Bool
4343
, optFiles :: [String]
4444
, optNoOptimizeNewtypes :: Bool
45+
, optPrettyThunks :: Bool
4546
}
4647

4748
-- | Main entry point.
@@ -70,6 +71,7 @@ main = do
7071
, configRuntimePath = optRuntimePath opts
7172
, configSourceMap = optSourceMap opts
7273
, configOptimizeNewtypes = not $ optNoOptimizeNewtypes opts
74+
, configPrettyThunks = optPrettyThunks opts
7375
}
7476
if optVersion opts
7577
then runCommandVersion
@@ -121,6 +123,7 @@ options = FayCompilerOptions
121123
<*> switch (long "sourcemap" <> help "Produce a source map in <outfile>.map")
122124
<*> many (argument (ReadM ask) (metavar "<hs-file>..."))
123125
<*> switch (long "no-optimized-newtypes" <> help "Remove optimizations for newtypes, treating them as normal data types")
126+
<*> switch (long "pretty-thunks" <> help "Use pretty thunk names")
124127
where
125128
strsOption :: Mod OptionFields [String] -> Parser [String]
126129
strsOption m = option (ReadM . fmap (wordsBy (== ',')) $ ask) (m <> value [])

0 commit comments

Comments
 (0)