-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.hs
200 lines (167 loc) · 6.49 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
module Main where
import System.Exit
import System.Environment
import System.Directory (createDirectoryIfMissing, getCurrentDirectory)
import System.FilePath ((</>), joinPath, takeFileName)
import Text.Printf (printf)
import Data.Aeson hiding (Options)
import Data.Aeson.Types hiding (Options)
import qualified Data.List as List
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as L
import qualified Data.Text.Lazy.Encoding as L
import qualified Data.ByteString as B
import qualified Data.Set as S
import qualified Language.PureScript.AST.SourcePos as SP
import Language.PureScript.CoreImp.AST ( AST
, withSourceSpan
, getSourceSpan
, everywhere
)
import Language.PureScript.CoreFn
import Language.PureScript.CoreFn.FromJSON
import Language.PureScript.Errors (MultipleErrors)
import Language.PureScript.Options (Options(..))
import qualified Language.PureScript as P
import Language.PureScript.Names ( moduleNameFromString
, isBuiltinModuleName )
import Language.PureScript.CodeGen.Py (moduleToJS)
import Language.PureScript.CodeGen.Py.Printer (Py, bindSExpr)
import Language.PureScript.CodeGen.Py.Eval (finally)
import Language.PureScript.CodeGen.Py.Common (escape)
import Control.Monad.Supply
import Data.Text.Prettyprint.Doc.Render.Text (renderStrict)
import Data.Text.Prettyprint.Doc (Doc, layoutPretty, defaultLayoutOptions)
import Control.Monad.Reader (MonadReader(..))
import qualified Control.Monad.State as State
import Monads.STEither
instance MonadReader Options (STEither Options MultipleErrors) where
ask = STEither State.get
local r (STEither m) = STEither (State.modify r >> m)
defaultOpts :: Options
defaultOpts =
Options { optionsVerboseErrors = True
, optionsNoComments = False
, optionsCodegenTargets = S.empty
}
main :: IO ()
main = do
opts <- getArgs
case opts of
[ "--out-python"
, baseOutDir
, "--corefn-entry"
, moduleParts
, "--out-ffi-dep"
, ffiDepPath] -> do
ffiDeps <-
fixPointCG
baseOutDir
S.empty
(S.empty, [moduleNameFromString $ T.pack moduleParts])
T.writeFile ffiDepPath (T.unlines $ map T.pack ffiDeps)
_ -> putStrLn "Malformed options, expect form --out-python <dir0> --corefn-entry <A.B.C> --out-ffi-dep <xxx>." >> exitFailure
fixPointCG :: FilePath -> S.Set FilePath -> (S.Set P.ModuleName, [P.ModuleName]) -> IO [FilePath]
fixPointCG baseOutDir ffiPathReferred (importedModules, moduleImportDeque) =
case moduleImportDeque of
[] -> return $ S.toList ffiPathReferred
m:ms
| m `S.member` importedModules || isBuiltinModuleName m ->
fixPointCG baseOutDir ffiPathReferred (importedModules, ms)
| otherwise -> do
(newModsToImport, newFFIReferred) <- cg baseOutDir m
fixPointCG
baseOutDir
(S.union ffiPathReferred newFFIReferred)
(S.insert m importedModules, newModsToImport ++ ms)
-- code generation for each module
cg :: FilePath -> P.ModuleName -> IO ([P.ModuleName], S.Set FilePath)
cg baseOutDir coreFnMN = do
pwd <- getCurrentDirectory
-- TODO: customizable `output` directory
let jsonFile = joinPath
[ pwd
, "output"
, runModuleName [] [] coreFnMN
, "corefn.json"
]
jsonText <- T.decodeUtf8 <$> B.readFile jsonFile
let module' = jsonToModule $ parseJson jsonText
-- getting the module name
mn = moduleName module'
-- getting the module path
mp = modulePath module'
-- name of the generated python package
package = takeFileName baseOutDir
hasForeign <- case flip State.runStateT defaultOpts . runSTEither .runSupplyT 5 $
moduleToJS module' (T.pack package) of
Left e -> print (e :: MultipleErrors) >> exitFailure
Right (((hasForeign, ast), _), _) -> do
let implCode =
legalizedCodeGen
(runModuleName [package] ["pure"] mn)
(joinPath [pwd, mp])
(finally $ everywhere (astSSToAbsPath pwd) ast)
outDir = runToModulePath [pwd, baseOutDir] [] mn
implSrcPath = outDir </> "pure.src.py"
implLoaderPath = outDir </> "pure.py"
createDirectoryIfMissing True outDir
T.writeFile implSrcPath implCode
T.writeFile implLoaderPath loaderCode
return hasForeign
let newModsToImport = map snd (moduleImports module')
let newFModToAdd = S.fromList [mp | hasForeign]
return (newModsToImport, newFModToAdd)
runToModulePath :: [FilePath] -> [FilePath] -> P.ModuleName -> String
runToModulePath prefix suffix (P.ModuleName pns) =
joinPath .
(prefix++) . (++suffix) .
map T.unpack $ (P.runProperName <$> pns)
runModuleName :: [FilePath] -> [FilePath] -> P.ModuleName -> String
runModuleName prefix suffix (P.ModuleName pns) =
List.intercalate "." .
(prefix++) . (++suffix) .
map T.unpack $ (P.runProperName <$> pns)
parseJson :: Text -> Value
parseJson text
| Just fileJson <- decode . L.encodeUtf8 $ L.fromStrict text = fileJson
| otherwise = error "Bad json"
jsonToModule :: Value -> Module Ann
jsonToModule value =
case parse moduleFromJSON value of
Success (_, r) -> r
_ -> error "Bad corefn"
astSSToAbsPath :: FilePath -> AST -> AST
astSSToAbsPath pwd n =
case getSourceSpan n of
Nothing -> n
Just ss ->
withSourceSpan (ss {P.spanName = joinPath [pwd, SP.spanName ss]}) n
doc2Text :: Doc ann -> T.Text
doc2Text = renderStrict . layoutPretty defaultLayoutOptions
legalizedCodeGen :: String -> FilePath -> Doc Py -> Text
legalizedCodeGen mName mPath sexpr =
T.unlines
[
T.pack "from py_sexpr.terms import *"
, T.pack "from py_sexpr.stack_vm.emit import module_code"
, doc2Text (bindSExpr "res" sexpr)
, T.pack $
printf
"res = module_code(res, filename=%s, name=%s)"
(escape mPath)
(escape mName)
]
loaderCode :: Text
loaderCode =
T.unlines . map T.pack $
[
"from purescripto import LoadPureScript"
, "__py__ = globals()"
, "__ps__ = LoadPureScript(__file__, __name__)"
, "__all__ = list(__ps__)"
, "__py__.update(__ps__)"
]