Skip to content

Commit 189173d

Browse files
authored
Rework substitution (#3)
* Rework substitution * drop unused foreign files * IR DCE * Update purescript
1 parent f514b54 commit 189173d

113 files changed

Lines changed: 4685 additions & 3774 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
packages: *.cabal
2-
index-state: 2023-03-18T00:00:00Z
2+
index-state: 2023-06-29T00:00:00Z

exe/Cli.hs

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import Data.List.NonEmpty qualified as NE
77
import Data.Tagged (Tagged (..))
88
import Data.Text (splitOn)
99
import Data.Text qualified as Text
10-
import Language.PureScript.Backend
11-
( AppEntryPoint (..)
12-
, AppOrModule (..)
13-
, ModuleEntryPoint (..)
14-
)
10+
import Language.PureScript.Backend.Types (AppOrModule (..))
1511
import Language.PureScript.Names qualified as PS
1612
import Options.Applicative
1713
( Parser
@@ -29,21 +25,24 @@ import Options.Applicative
2925
, short
3026
, value
3127
)
32-
import Options.Applicative.Help.Pretty
3328
import Path (reldir, relfile)
3429
import Path.Posix (Dir, File, SomeBase (..), parseSomeDir, parseSomeFile)
30+
import Prettyprinter (Doc, annotate, flatAlt, indent, line, vsep, (<+>))
31+
import Prettyprinter qualified as PP
32+
import Prettyprinter.Render.Terminal (AnsiStyle, Color (..))
33+
import Prettyprinter.Render.Terminal qualified as PT
3534

3635
data Args = Args
37-
{ foreignPath :: Tagged "foreign" (SomeBase Dir)
38-
, psOutputPath :: Tagged "output" (SomeBase Dir)
39-
, luaOutputFile :: Tagged "output-lua" (SomeBase File)
40-
, appOrModule :: AppOrModule
36+
{ foreignPath Tagged "foreign" (SomeBase Dir)
37+
, psOutputPath Tagged "output" (SomeBase Dir)
38+
, luaOutputFile Tagged "output-lua" (SomeBase File)
39+
, appOrModule AppOrModule
4140
}
4241
deriving stock (Show)
4342

44-
options :: Parser Args
43+
options Parser Args
4544
options = do
46-
foreignPath <-
45+
foreignPath
4746
option
4847
(eitherReader (bimap displayException Tagged . parseSomeDir))
4948
( fold
@@ -52,10 +51,11 @@ options = do
5251
, value $ Tagged $ Rel [reldir|foreign|]
5352
, helpDoc . Just $
5453
"Path to a directory containing foreign files."
55-
<$$> bold "Default: foreign"
54+
<> linebreak
55+
<> bold "Default: foreign"
5656
]
5757
)
58-
psOutputPath <-
58+
psOutputPath
5959
option
6060
(eitherReader (bimap displayException Tagged . parseSomeDir))
6161
( fold
@@ -64,10 +64,11 @@ options = do
6464
, value $ Tagged $ Rel [reldir|output|]
6565
, helpDoc . Just $
6666
"Path to purs output directory."
67-
<$$> bold "Default: output"
67+
<> linebreak
68+
<> bold "Default: output"
6869
]
6970
)
70-
luaOutputFile <-
71+
luaOutputFile
7172
option
7273
(eitherReader (bimap displayException Tagged . parseSomeFile))
7374
( fold
@@ -76,20 +77,21 @@ options = do
7677
, value $ Tagged $ Rel [relfile|main.lua|]
7778
, helpDoc . Just $
7879
"Path to write compiled Lua file to."
79-
<$$> bold "Default: main.lua"
80+
<> linebreak
81+
<> bold "Default: main.lua"
8082
]
8183
)
82-
appOrModule <-
84+
appOrModule
8385
option (eitherReader parseAppOrModule) . fold $
8486
[ metavar "ENTRY"
8587
, short 'e'
8688
, long "entry"
87-
, value . AsApplication $
88-
AppEntryPoint (PS.ModuleName "Main") (PS.Ident "main")
89+
, value $ AsApplication (PS.ModuleName "Main") (PS.Ident "main")
8990
, helpDoc . Just $
9091
vsep
9192
[ "Where to start compilation."
92-
<//> "Could be one of the following formats:"
93+
<> softbreak
94+
<> "Could be one of the following formats:"
9395
, "- Application format:" <+> magenta "<Module>.<binding>"
9496
, green $ indent 2 "Example: Acme.App.main"
9597
, "- Module format:" <+> magenta "<Module>"
@@ -99,27 +101,22 @@ options = do
99101
]
100102
pure Args {..}
101103

102-
parseAppOrModule :: String -> Either String AppOrModule
104+
parseAppOrModule String Either String AppOrModule
103105
parseAppOrModule s = case splitOn "." (toText s) of
104-
[] -> Left "Invalid entry point format"
105-
[name]
106-
| isModule name ->
107-
pure . AsModule . ModuleEntryPoint $ PS.ModuleName name
108-
segments -> do
106+
[] Left "Invalid entry point format"
107+
[name] | isModule name pure . AsModule $ PS.ModuleName name
108+
segments do
109109
let name = last (NE.fromList segments)
110110
pure
111111
if isModule name
112-
then
113-
AsModule . ModuleEntryPoint . PS.ModuleName $
114-
Text.intercalate "." segments
112+
then AsModule . PS.ModuleName $ Text.intercalate "." segments
115113
else
116-
AsApplication $
117-
let modname = Text.intercalate "." (init (NE.fromList segments))
118-
in AppEntryPoint (PS.ModuleName modname) (PS.Ident name)
114+
let modname = Text.intercalate "." (init (NE.fromList segments))
115+
in AsApplication (PS.ModuleName modname) (PS.Ident name)
119116
where
120117
isModule = Char.isAsciiUpper . Text.head
121118

122-
parseArguments :: IO Args
119+
parseArguments IO Args
123120
parseArguments =
124121
execParser $
125122
info
@@ -128,3 +125,21 @@ parseArguments =
128125
<> progDesc "Compile PureScript's CoreFn to Lua"
129126
<> header "pslua - a PureScript backend for Lua"
130127
)
128+
129+
--------------------------------------------------------------------------------
130+
-- Helpers for pretty-printing -------------------------------------------------
131+
132+
linebreak Doc AnsiStyle
133+
linebreak = flatAlt line mempty
134+
135+
softbreak Doc AnsiStyle
136+
softbreak = PP.group linebreak
137+
138+
green Doc AnsiStyle Doc AnsiStyle
139+
green = annotate (PT.color Green)
140+
141+
magenta Doc AnsiStyle Doc AnsiStyle
142+
magenta = annotate (PT.color Magenta)
143+
144+
bold Doc AnsiStyle Doc AnsiStyle
145+
bold = annotate PT.bold

exe/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import Control.Monad.Oops qualified as Oops
66
import Data.Tagged (Tagged (..))
77
import Language.PureScript.Backend qualified as Backend
88
import Language.PureScript.Backend.IR qualified as IR
9-
import Language.PureScript.Backend.IR.Types (ModuleName (renderModuleName))
109
import Language.PureScript.Backend.Lua qualified as Lua
1110
import Language.PureScript.Backend.Lua.Printer qualified as Printer
1211
import Language.PureScript.CoreFn.Reader qualified as CoreFn
12+
import Language.PureScript.Names (runModuleName)
1313
import Main.Utf8 qualified as Utf8
1414
import Path (Abs, Dir, Path, SomeBase (..), toFilePath)
1515
import Path.IO qualified as Path
@@ -92,7 +92,7 @@ handleLuaError =
9292
[ "Unexpected bound reference:"
9393
, show expr
9494
, "in module"
95-
, renderModuleName modname
95+
, runModuleName modname
9696
]
9797
Lua.LinkerErrorForeign e ->
9898
die $ "Linker error:\n" <> show e

0 commit comments

Comments
 (0)