git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
17 files changed+353−185
since splitsnapshots
unifiedsplit
.gitignore+1−2 README-go.md+3−5 README.md+2−2 app/Main.hs+32−11 default.nix+23 package.yaml+6−6 psgo.cabal+145 src/CodeGen/IL.hs+9−47 src/CodeGen/IL/Common.hs+37−18 src/CodeGen/IL/Optimizer.hs+14−1 src/CodeGen/IL/Optimizer/Inliner.hs+13−4 src/CodeGen/IL/Optimizer/TCO.hs+5−5 src/CodeGen/IL/Printer.hs+22−12 src/Tests.hs+6−4 stack.nix+18−3 stack.yaml+15−65 support/ffi-loader.go+2
.gitignore+1−2
@@ -12,8 +12,6 @@ cabal.sandbox.config
1212.test_modules/
1313bower_components/
1414node_modules
15−pscpp.cabal
16−psgo.cabal
1715tmp/
1816.stack-work/
1917stack.yaml.lock
@@ -22,3 +20,4 @@ examples/docs/docs/
2220core-tests/full-core-docs.md
2321.psc-ide-port
2422.vscode/
23+result
README-go.md+3−5
@@ -38,12 +38,10 @@ This assumes you are running macOS (OS X) or a Unix-like system (Linux, *BSD, et
3838
39391. Initialize your project with `spago init`, and install any dependencies with `spago install` (please see their instructions if you haven't used it before).
4040
41+1. Add the `backend = "psgo"` option to your `spago.dhall` file. That way `spago` will use the psgo backend when compiling.
42+
41431. You should now be ready to build a PureScript program:
42− * As stated above, place your source file(s) in the working directory's `src` subdirectory and execute
43−
44− ```sh
45− spago build -- -g corefn && psgo
46− ```
44+ * As stated above, place your source file(s) in the working directory's `src` subdirectory and execute `spago build`.
4745
4846 * This will generate the Go source tree for your program and then build an executable binary. The resulting executable will be in your working directory and will be named `Main`. The generated source files will be under `output/`.
4947
README.md+2−2
@@ -2,7 +2,7 @@
22
33This is a platform-native compiler backend for [PureScript](https://github.com/purescript/purescript). It attempts to generate "sane", debuggable, and portable [C++11](https://isocpp.org/wiki/faq/cpp11) (or later) or [Go](https://golang.org/) code as an intermediate language, which is then compiled to a native executable binary. This also enables easy interoperability with existing C/C++ or Go frameworks and libraries on a number of platforms.
44
5−There are two utilities in the purescript-native "suite": `pscpp` and `psgo`. The source code in this branch is for `psgo`, the purescript-to-Go transpiler. For the source code of the `pscpp` purescript-to-C++ transpiler / build tool, please see the [cpp branch](https://github.com/andyarvanitis/purescript-native/tree/cpp).
5+There are two utilities in the purescript-native "suite": `pscpp` and `psgo`. The source code in this branch is for `psgo`, the purescript-to-Go transpiler. For the source code of the `pscpp` purescript-to-C++ transpiler, please see the [cpp branch](https://github.com/andyarvanitis/purescript-native/tree/cpp).
66
77#### Platforms
88* Although purescript-native should work on any platform supporting PureScript and modern C++ or Go, the generated code and resulting binary executables have been tested on:
@@ -34,7 +34,7 @@ There are two utilities in the purescript-native "suite": `pscpp` and `psgo`. Th
3434
3535
3636#### For more information and a Getting Started guide, please see
37−* [`pscpp`](https://github.com/andyarvanitis/purescript-native/blob/master/README-cpp.md)
37+* [`pscpp`](https://github.com/andyarvanitis/purescript-native/blob/cpp/README-cpp.md)
3838* [`psgo`](https://github.com/andyarvanitis/purescript-native/blob/golang/README-go.md)
3939
4040
app/Main.hs+32−11
@@ -43,7 +43,7 @@ import CodeGen.IL.Printer
4343
4444import Tests
4545
46−data Command = Build | Run
46+data Command = Build | Run (Maybe String)
4747
4848parseJson :: Text -> Value
4949parseJson text
@@ -54,12 +54,32 @@ jsonToModule :: Value -> Module Ann
5454jsonToModule value =
5555 case parse moduleFromJSON value of
5656 Success (_, r) -> r
57− _ -> error "failed"
57+ _ -> error "Failed parsing corefn. Psgo currently supports purescript <= 0.13.8"
58+
59+splitArgs :: [String] -> ([String], Maybe String, [String])
60+splitArgs args =
61+ let (opts, files) = partition (isPrefixOf "--") args
62+ opts' = (map . map) toLower opts
63+ (moduleName, files') =
64+ if null files
65+ then (Nothing, files)
66+ else
67+ if "--run" `elem` opts' && length (files) >= 1
68+ then do
69+ let file0 = head files
70+ files' = tail files
71+ case file0 of
72+ "Main.main" -> (Just "Main", files')
73+ "Test.Main.main" -> (Just "Test.Main", files')
74+ _ -> (Nothing, files)
75+ else (Nothing, files)
76+ in (opts, moduleName, files')
5877
5978main :: IO ()
6079main = do
6180 args <- getArgs
62− let (opts, files) = partition (isPrefixOf "--") args
81+ -- let (opts, files) = partition (isPrefixOf "--") args
82+ let (opts, moduleName, files) = splitArgs args
6383 opts' = (map . map) toLower opts
6484 if "--tests" `elem` opts'
6585 then runTests
@@ -81,7 +101,7 @@ main = do
81101 else
82102 processFiles opts' files
83103 if "--run" `elem` opts
84− then runBuildTool Run
104+ then runBuildTool $ Run moduleName
85105 else when ("--no-build" `notElem` opts) $
86106 runBuildTool Build
87107 return ()
@@ -109,11 +129,12 @@ main = do
109129 return ()
110130 runBuildTool :: Command -> IO ()
111131 runBuildTool cmd = do
112− let command = case cmd of
113− Build -> "build"
114− Run -> "run"
115132 project <- projectEnv
116− callProcess "go" [command, T.unpack project </> modPrefix' </> "Main"]
133+ case cmd of
134+ Build -> do
135+ callProcess "go" ["build", T.unpack project </> modPrefix' </> "Main"]
136+ Run moduleName -> do
137+ callProcess "go" ["run", T.unpack project </> modPrefix' </> fromMaybe "Main" moduleName]
117138 return ()
118139
119140generateCode :: [String] -> FilePath -> FilePath -> IO ()
@@ -164,16 +185,16 @@ writeSupportFiles baseOutpath = do
164185 let goModSource = path </> "go.mod"
165186 goModSourceExists <- doesFileExist goModSource
166187 when (not goModSourceExists) $ do
167− currentDir <- getCurrentDirectory
168188 project <- projectEnv
169189 let project' = T.unpack project
170190 outputdir' = tail $ T.unpack outputdir
171191 modText' = T.replace "$PROJECT" project $ T.decodeUtf8 modText
172− replaceLoader = project' </> ffiLoader' <> "=" <> currentDir </> subdir
173− replaceOutput = project' </> modPrefix' <> "=" <> currentDir </> outputdir'
192+ replaceLoader = project' </> ffiLoader' <> "=." </> subdir
193+ replaceOutput = project' </> modPrefix' <> "=." </> outputdir'
174194 B.writeFile goModSource $ T.encodeUtf8 modText'
175195 callProcess "go" ["mod", "edit", "-replace", replaceLoader]
176196 callProcess "go" ["mod", "edit", "-replace", replaceOutput]
197+ callProcess "go" ["clean", "-modcache"]
177198 writeLoaderFile :: FilePath -> B.ByteString -> IO ()
178199 writeLoaderFile ffiOutpath loaderText = do
179200 let loaderSource = ffiOutpath </> "ffi_loader.go"
default.nixnew file+23
@@ -0,0 +1,23 @@
1+let
2+ haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
3+ nixpkgsSrc = haskellNix.sources.nixpkgs-1903; # has hsc2hs-0.68.4
4+
5+ hsc2hsOverlay = self: super: {
6+ buildPackages = super.buildPackages // {
7+ hsc2hs = super.haskellPackages.hsc2hs;
8+ };
9+ };
10+
11+ nixpkgsArgs = haskellNix.nixpkgsArgs // {
12+ overlays = haskellNix.nixpkgsArgs.overlays ++ [ hsc2hsOverlay ];
13+ };
14+in
15+{ pkgs ? import nixpkgsSrc nixpkgsArgs
16+}:
17+
18+pkgs.haskell-nix.stackProject {
19+ src = pkgs.haskell-nix.haskellLib.cleanGit {
20+ name = "purescript-native";
21+ src = ./.;
22+ };
23+}
package.yaml+6−6
@@ -24,7 +24,7 @@ description: Please see the README on Github at <https://github.com/andy
2424dependencies:
2525- base >= 4.7 && < 5
2626- bytestring >= 0.10.8.2 && < 0.11
27−- purescript -any
27+- purescript
2828- base-compat >=0.6.0
2929- protolude >=0.1.6
3030- text
@@ -36,11 +36,11 @@ dependencies:
3636- transformers >=0.3.0 && <0.6
3737- mtl >=2.1.0 && <2.3.0
3838- aeson >=1.0 && <1.5
39−- directory -any
40−- process -any
41−- file-embed -any
42−- filemanip -any
43−- gitrev -any
39+- directory
40+- process
41+- file-embed
42+- filemanip
43+- gitrev
4444
4545library:
4646 source-dirs: src
psgo.cabalnew file+145
@@ -0,0 +1,145 @@
1+cabal-version: 1.12
2+
3+-- This file has been generated from package.yaml by hpack version 0.35.0.
4+--
5+-- see: https://github.com/sol/hpack
6+
7+name: psgo
8+version: 0.1.0.0
9+description: Please see the README on Github at <https://github.com/andyarvanitis/purescript-native#readme>
10+homepage: https://github.com/andyarvanitis/purescript-native#readme
11+bug-reports: https://github.com/andyarvanitis/purescript-native/issues
12+author: Andy Arvanitis
13+maintainer: example@example.com
14+copyright: 2018 Andy Arvanitis
15+license: BSD3
16+license-file: LICENSE
17+build-type: Simple
18+extra-source-files:
19+ README.md
20+ ChangeLog.md
21+ stack.yaml
22+ support/ffi-loader.go
23+ support/go.mod.ffi-loader
24+ support/go.mod.output
25+ support/go.mod.working
26+
27+source-repository head
28+ type: git
29+ location: https://github.com/andyarvanitis/purescript-native
30+
31+library
32+ exposed-modules:
33+ CodeGen.IL
34+ CodeGen.IL.Common
35+ CodeGen.IL.Optimizer
36+ CodeGen.IL.Optimizer.Common
37+ CodeGen.IL.Optimizer.Inliner
38+ CodeGen.IL.Optimizer.MagicDo
39+ CodeGen.IL.Optimizer.TCO
40+ CodeGen.IL.Printer
41+ Tests
42+ other-modules:
43+ Paths_psgo
44+ hs-source-dirs:
45+ src
46+ default-extensions:
47+ ConstraintKinds
48+ DataKinds
49+ DeriveFunctor
50+ EmptyDataDecls
51+ FlexibleContexts
52+ KindSignatures
53+ LambdaCase
54+ MultiParamTypeClasses
55+ NoImplicitPrelude
56+ PatternGuards
57+ PatternSynonyms
58+ RankNTypes
59+ RecordWildCards
60+ OverloadedStrings
61+ ScopedTypeVariables
62+ TupleSections
63+ ViewPatterns
64+ build-depends:
65+ aeson >=1.0 && <1.5
66+ , base >=4.7 && <5
67+ , base-compat >=0.6.0
68+ , bytestring >=0.10.8.2 && <0.11
69+ , containers
70+ , directory
71+ , file-embed
72+ , filemanip
73+ , filepath
74+ , gitrev
75+ , monad-parallel >=0.7.2.2 && <0.8
76+ , mtl >=2.1.0 && <2.3.0
77+ , pattern-arrows >=0.0.2 && <0.1
78+ , process
79+ , protolude >=0.1.6
80+ , purescript
81+ , safe >=0.3.9 && <0.4
82+ , text
83+ , transformers >=0.3.0 && <0.6
84+ default-language: Haskell2010
85+
86+executable psgo
87+ main-is: Main.hs
88+ other-modules:
89+ Paths_psgo
90+ hs-source-dirs:
91+ app
92+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
93+ build-depends:
94+ aeson >=1.0 && <1.5
95+ , base >=4.7 && <5
96+ , base-compat >=0.6.0
97+ , bytestring >=0.10.8.2 && <0.11
98+ , containers
99+ , directory
100+ , file-embed
101+ , filemanip
102+ , filepath
103+ , gitrev
104+ , monad-parallel >=0.7.2.2 && <0.8
105+ , mtl >=2.1.0 && <2.3.0
106+ , pattern-arrows >=0.0.2 && <0.1
107+ , process
108+ , protolude >=0.1.6
109+ , psgo
110+ , purescript
111+ , safe >=0.3.9 && <0.4
112+ , text
113+ , transformers >=0.3.0 && <0.6
114+ default-language: Haskell2010
115+
116+test-suite psgo-test
117+ type: exitcode-stdio-1.0
118+ main-is: Spec.hs
119+ other-modules:
120+ Paths_psgo
121+ hs-source-dirs:
122+ test
123+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
124+ build-depends:
125+ aeson >=1.0 && <1.5
126+ , base >=4.7 && <5
127+ , base-compat >=0.6.0
128+ , bytestring >=0.10.8.2 && <0.11
129+ , containers
130+ , directory
131+ , file-embed
132+ , filemanip
133+ , filepath
134+ , gitrev
135+ , monad-parallel >=0.7.2.2 && <0.8
136+ , mtl >=2.1.0 && <2.3.0
137+ , pattern-arrows >=0.0.2 && <0.1
138+ , process
139+ , protolude >=0.1.6
140+ , psgo
141+ , purescript
142+ , safe >=0.3.9 && <0.4
143+ , text
144+ , transformers >=0.3.0 && <0.6
145+ default-language: Haskell2010
src/CodeGen/IL.hs+9−47
@@ -52,10 +52,7 @@ moduleToIL
5252 -> m (Text, [Text], [AST], Text, Text)
5353moduleToIL (Module _ coms mn _ imps _ foreigns decls) project =
5454 do
55− let usedNames = concatMap getNames decls
56− let mnLookup = renameImports usedNames imps
57− let decls' = renameModules mnLookup decls
58− ilDecls <- mapM (bindToIL ModuleDecl) decls'
55+ ilDecls <- mapM (bindToIL ModuleDecl) decls
5956 optimized <- traverse (traverse (optimize modName')) ilDecls
6057 let optimized' = concat optimized
6158 values = annotValue <$> optimized'
@@ -80,54 +77,16 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) project =
8077 getNames (NonRec _ ident _) = [ident]
8178 getNames (Rec vals) = map (snd . fst) vals
8279
83− -- | Creates alternative names for each module to ensure they don't collide
84− -- with declaration names.
85− renameImports :: [Ident] -> [(Ann, ModuleName)] -> M.Map ModuleName (Ann, ModuleName)
86− renameImports = go M.empty
87− where
88− go :: M.Map ModuleName (Ann, ModuleName) -> [Ident] -> [(Ann, ModuleName)] -> M.Map ModuleName (Ann, ModuleName)
89− go acc used ((ann, mn') : mns') =
90− let mni = Ident $ runModuleName mn'
91− in if mn' /= mn && mni `elem` used
92− then let newName = freshModuleName 1 mn' used
93− in go (M.insert mn' (ann, newName) acc) (Ident (runModuleName newName) : used) mns'
94− else go (M.insert mn' (ann, mn') acc) used mns'
95− go acc _ [] = acc
96−
97− freshModuleName :: Integer -> ModuleName -> [Ident] -> ModuleName
98− freshModuleName i mn'@(ModuleName pns) used =
99− let newName = ModuleName $ init pns ++ [ProperName $ runProperName (last pns) <> "_" <> T.pack (show i)]
100− in if Ident (runModuleName newName) `elem` used
101− then freshModuleName (i + 1) mn' used
102− else newName
103−
10480 -- | Generates IL code for a module import
10581 --
10682 importToIL :: AST -> [Text]
10783 importToIL = AST.everything (++) modRef
10884 where
10985 modRef (AST.Indexer _ (AST.Var _ _) (AST.Var _ mname))
110− | not $ T.null mname = [mname]
86+ | not $ T.null mname = [extract mname]
11187 modRef _ = []
112−
113− -- | Replaces the `ModuleName`s in the AST so that the generated code refers to
114− -- the collision-avoiding renamed module imports.
115− renameModules :: M.Map ModuleName (Ann, ModuleName) -> [Bind Ann] -> [Bind Ann]
116− renameModules mnLookup binds =
117− let (f, _, _) = everywhereOnValues id ilExpr goBinder
118− in map f binds
119− where
120− ilExpr :: Expr a -> Expr a
121− ilExpr (Var ann q) = Var ann (renameQual q)
122− ilExpr e = e
123− goBinder :: Binder a -> Binder a
124− goBinder (ConstructorBinder ann q1 q2 bs) = ConstructorBinder ann (renameQual q1) (renameQual q2) bs
125− goBinder b = b
126− renameQual :: Qualified a -> Qualified a
127− renameQual (Qualified (Just mn') a) =
128− let (_,mnSafe) = fromMaybe (internalError "Missing value in mnLookup") $ M.lookup mn' mnLookup
129− in Qualified (Just mnSafe) a
130− renameQual q = q
88+ extract :: Text -> Text
89+ extract = T.replace "_" "." . T.dropWhileEnd (=='_')
13190
13291 -- |
13392 -- Generate code in the simplified intermediate representation for a declaration
@@ -178,7 +137,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) project =
178137 updates <- mapM (sndM valueToIL) ps
179138 obj' <- freshName'
180139 let objVar = AST.Var Nothing obj'
181− copy = AST.VariableIntroduction Nothing obj' (Just $ AST.App Nothing (AST.StringLiteral Nothing (mkString dictType)) [obj])
140+ copy = AST.VariableIntroduction Nothing obj' (Just $ copyDict obj)
182141 assign (k, v) = AST.Assignment Nothing (accessorString k objVar) v
183142 sts = copy : (assign <$> updates) ++ [AST.Return Nothing objVar]
184143 return $ AST.App Nothing (AST.Function Nothing Nothing [] (AST.Block Nothing sts)) []
@@ -242,7 +201,7 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) project =
242201 -- | Generate code in the simplified intermediate representation for a reference to a
243202 -- variable that may have a qualified name.
244203 qualifiedToIL :: (a -> Ident) -> Qualified a -> AST
245− qualifiedToIL f (Qualified (Just (ModuleName [ProperName mn'])) a) | mn' == C.prim = AST.Var Nothing . moduleIdentToIL $ f a
204+ qualifiedToIL f (Qualified (Just (ModuleName mn')) a) | mn' == C.prim = AST.Var Nothing . moduleIdentToIL $ f a
246205 qualifiedToIL f (Qualified (Just mn') a) | mn /= mn' = AST.Indexer Nothing (AST.Var Nothing . moduleIdentToIL $ f a) (AST.Var Nothing (moduleNameToIL mn'))
247206 qualifiedToIL f (Qualified _ a) = AST.Indexer Nothing (AST.Var Nothing . moduleIdentToIL $ f a) (AST.Var Nothing "")
248207
@@ -357,3 +316,6 @@ emptyAnn = (SourceSpan "" (SourcePos 0 0) (SourcePos 0 0), [], Nothing, Nothing)
357316arrayLength :: AST -> AST
358317arrayLength a = AST.App Nothing (AST.Var Nothing arrayLengthFn) [a]
359318-- arrayLength a = AST.Var Nothing (arrayLengthFn <> "(" <> prettyPrintIL1 a <> ")")
319+
320+copyDict :: AST -> AST
321+copyDict a = AST.App Nothing (AST.Var Nothing copyDictFn) [a]
src/CodeGen/IL/Common.hs+37−18
@@ -16,10 +16,12 @@ import Language.PureScript.Names
1616import qualified Language.PureScript.Constants as C
1717
1818moduleNameToIL :: ModuleName -> Text
19−moduleNameToIL (ModuleName pns) = T.intercalate "_" (runProperName `map` pns)
19+moduleNameToIL (ModuleName mn) =
20+ let name = T.replace "." "_" mn
21+ in if nameIsILBuiltIn name then (name <> moduleRenamerMarker) else name
2022
2123moduleNameToIL' :: ModuleName -> Text
22−moduleNameToIL' (ModuleName pns) = T.intercalate "." (runProperName `map` pns)
24+moduleNameToIL' (ModuleName mn) = T.replace "_" "." mn
2325
2426-- | Convert an 'Ident' into a valid IL identifier:
2527--
@@ -48,16 +50,17 @@ unusedName = "_"
4850
4951properToIL :: Text -> Text
5052properToIL name
51− | nameIsILReserved name || nameIsILBuiltIn name || prefixIsReserved name = name <> "ˉ"
53+ | nameIsILReserved name = "ˉ" <> name
5254 | otherwise = T.concatMap identCharToText name
5355
5456-- | Attempts to find a human-readable name for a symbol, if none has been specified returns the
5557-- ordinal value.
5658identCharToText :: Char -> Text
59+identCharToText 'ṩ' = "_ṩ"
5760identCharToText c | isAlphaNum c = T.singleton c
5861identCharToText '_' = "_"
59−identCharToText '\'' = "ʹ"
60−identCharToText c = "ᵤ" <> T.pack (show (ord c))
62+identCharToText '\'' = "ꞌ" -- lowercase saltillo
63+identCharToText c = error (show (ord c)) -- TODO: should never occur now(?)
6164
6265moduleProperToIL :: Text -> Text
6366moduleProperToIL = T.concatMap identCharToText
@@ -71,7 +74,21 @@ nameIsILReserved name =
7174nameIsILBuiltIn :: Text -> Bool
7275nameIsILBuiltIn name =
7376 name `elem`
74− [
77+ [ "Any"
78+ , "Apply"
79+ , "Contains"
80+ , "CopyDict"
81+ , "Dict"
82+ , "EffFn"
83+ , "Fn"
84+ , "Foreign"
85+ , "Get"
86+ , "Index"
87+ , "Is"
88+ , "Length"
89+ , "Once"
90+ , "Run"
91+ , "Undefined"
7592 ]
7693
7794ilAnyReserved :: [Text]
@@ -110,16 +127,6 @@ ilKeywords =
110127 , "var"
111128 ]
112129
113−-- moduleIsReserved :: Text -> Bool
114−-- moduleIsReserved "Main" = False
115−-- moduleIsReserved name = not $ T.isInfixOf "_" name
116−
117−prefixIsReserved :: Text -> Bool
118−prefixIsReserved name =
119− any (`T.isPrefixOf` name)
120− [
121− ]
122−
123130ilLiterals :: [Text]
124131ilLiterals =
125132 [ "init"
@@ -145,7 +152,7 @@ ilLiterals =
145152 ]
146153
147154withPrefix :: Text -> Text
148−withPrefix s = "PS__" <> s
155+withPrefix s = "Ꞌ" <> s -- uppercase saltillo
149156
150157anyType :: Text
151158anyType = "Any"
@@ -156,6 +163,9 @@ dictType = "Dict"
156163arrayType :: Text
157164arrayType = "[]" <> anyType
158165
166+uncurriedFnType :: Int -> Text
167+uncurriedFnType i = "Fn" <> (T.pack . show $ i)
168+
159169int :: Text
160170int = "int"
161171
@@ -171,7 +181,16 @@ string = "string"
171181arrayLengthFn :: Text
172182arrayLengthFn = "Length"
173183
184+indexFn :: Text
185+indexFn = "Index"
186+
187+copyDictFn :: Text
188+copyDictFn = "CopyDict"
189+
174190freshName' :: MonadSupply m => m Text
175191freshName' = do
176192 name <- freshName
177− return $ T.replace "$" "ᵗ" name
193+ return $ T.replace "$" "ṩ" name
194+
195+moduleRenamerMarker :: Text
196+moduleRenamerMarker = "__"
src/CodeGen/IL/Optimizer.hs+14−1
@@ -22,10 +22,11 @@ import qualified Language.PureScript.Constants as C
2222optimize :: MonadSupply m => AST -> AST -> m AST
2323optimize mn il = do
2424 il' <- untilFixedPoint (inlineFnComposition . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp) il
25− untilFixedPoint (return . ignoreUnusedResults . inlineCommonValues . inlineCommonOperators . tidyUp) . tco mn . inlineST
25+ il'' <- untilFixedPoint (return . ignoreUnusedResults . inlineCommonValues . inlineCommonOperators . tidyUp) . tco mn . inlineST
2626 =<< untilFixedPoint (return . magicDoST)
2727 =<< untilFixedPoint (return . magicDoEff)
2828 =<< untilFixedPoint (return . magicDoEffect) il'
29+ untilFixedPoint (return . collapseVarDeclarations) il''
2930 where
3031 tidyUp :: AST -> AST
3132 tidyUp = applyAll
@@ -54,6 +55,18 @@ collapseIfChecks = everywhere collapse where
5455 | prop == "otherwise" && val == "Data_Boolean" = exprs
5556 collapse exprs = exprs
5657
58+collapseVarDeclarations :: AST -> AST
59+collapseVarDeclarations = everywhere collapse where
60+ collapse :: AST -> AST
61+ collapse (Block ss sts) = Block ss (go Nothing sts)
62+ collapse s = s
63+
64+ go :: Maybe Text -> [AST] -> [AST]
65+ go _ [] = []
66+ go Nothing (decl@(VariableIntroduction _ var (Just _)) : sts) = decl : go (Just var) (go Nothing sts)
67+ go (Just var') ((VariableIntroduction ss var (Just val)) : sts)
68+ | var == var' = (Assignment ss (Var Nothing var) val) : go (Just var') sts
69+ go var' (s:sts) = s : go var' sts
5770
5871ignoreUnusedResults :: AST -> AST
5972ignoreUnusedResults = everywhere $ removeFromBlock go
src/CodeGen/IL/Optimizer/Inliner.hs+13−4
@@ -117,12 +117,11 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
117117 , binary' C.dataIntBits C.xor BitwiseXor
118118 , binary' C.dataIntBits C.shl ShiftLeft
119119 , binary' C.dataIntBits C.shr ShiftRight
120− , binary' C.dataIntBits C.zshr ZeroFillShiftRight
121120 , unary' C.dataIntBits C.complement BitwiseNot
122121
123122 , inlineNonClassFunction (isModFn (C.dataFunction, C.apply)) $ \f x -> App Nothing f [x]
124123 , inlineNonClassFunction (isModFn (C.dataFunction, C.applyFlipped)) $ \x f -> App Nothing f [x]
125− , inlineNonClassFunction (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
124+ , inlineUnsafeIndex (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
126125 ] ++
127126 [ fn | i <- [0..10], fn <- [ mkFn i, runFn i ] ] ++
128127 [ fn | i <- [0..10], fn <- [ mkEffFn C.controlMonadEffUncurried C.mkEffFn i, runEffFn C.controlMonadEffUncurried C.runEffFn i ] ] ++
@@ -186,13 +185,14 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
186185 runFn :: Int -> AST -> AST
187186 runFn = runFn' C.dataFunctionUncurried C.runFn (\ss f args ->
188187 let len = length args
189− typ = mkString $ "Fn" <> (T.pack . show $ len) in
188+ typ = mkString $ uncurriedFnType len in
190189 if len > 0
191190 then App ss (App Nothing (StringLiteral Nothing typ) [f]) args
192191 else App ss f args)
193192 runEffFn :: Text -> Text -> Int -> AST -> AST
194193 runEffFn modName fnName = runFn' modName fnName $ \ss fn acc ->
195− Function ss Nothing [] (Block ss [Return ss (App ss fn acc)])
194+ let typ = mkString . uncurriedFnType $ length acc in
195+ Function ss Nothing [] (Block ss [Return ss (App ss (App Nothing (StringLiteral Nothing typ) [fn]) acc)])
196196
197197 runFn' :: Text -> Text -> (Maybe SourceSpan -> AST -> [AST] -> AST) -> Int -> AST -> AST
198198 runFn' modName runFnName res n = convert where
@@ -211,6 +211,15 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
211211 convert (App _ (App _ op' [x]) [y]) | p op' = f x y
212212 convert other = other
213213
214+ inlineUnsafeIndex :: (AST -> Bool) -> (AST -> AST -> AST) -> AST -> AST
215+ inlineUnsafeIndex p _ = convert where
216+ convert :: AST -> AST
217+ convert (App _ (App ss op' [x]) [y@NumericLiteral{}])
218+ | p op' = Indexer ss y x
219+ convert (App _ (App ss op' [x]) [y])
220+ | p op' = App ss (Var Nothing indexFn) [x, y]
221+ convert other = other
222+
214223 isModFn :: (Text, PSString) -> AST -> Bool
215224 isModFn (m, op) (Indexer _ (Var _ op') (Var _ m')) =
216225 m == m' && decodeString op == Just op'
src/CodeGen/IL/Optimizer/TCO.hs+5−5
@@ -12,21 +12,21 @@ import CodeGen.IL.Common
1212import qualified Language.PureScript.Constants as C
1313
1414tcoLoop :: Text
15−tcoLoop = "__tco_loop"
15+tcoLoop = "ṩtco_loop"
1616
1717tco :: AST -> AST -> AST
1818tco mn = everywhere convert where
1919 tcoVar :: Text -> Text
20− tcoVar arg = "__tco_var_" <> arg
20+ tcoVar arg = "ṩtco_var_" <> arg
2121
2222 copyVar :: Text -> Text
23− copyVar arg = "__copy_" <> arg
23+ copyVar arg = "ṩcopy_" <> arg
2424
2525 tcoDone :: Text
26− tcoDone = "__tco_done"
26+ tcoDone = "ṩtco_done"
2727
2828 tcoResult :: Text
29− tcoResult = "__tco_result"
29+ tcoResult = "ṩtco_result"
3030
3131 convert :: AST -> AST
3232 convert fn@(Function ss (Just name) args _)
src/CodeGen/IL/Printer.hs+22−12
@@ -106,6 +106,12 @@ literals = mkPattern' match'
106106 , prettyPrintIL' arg
107107 , return $ emit ")"
108108 ]
109+ match (App _ (Var _ fn) args) | fn == indexFn = mconcat <$> sequence
110+ [ return $ emit fn
111+ , return $ emit "("
112+ , intercalate (emit ", ") <$> forM args prettyPrintIL'
113+ , return $ emit ")"
114+ ]
109115 match (App _ (Var _ fn) args) | fn == tcoLoop = mconcat <$> sequence
110116 [ return $ emit fn
111117 , return $ emit "("
@@ -228,7 +234,7 @@ literals = mkPattern' match'
228234 , return $ emit "]"
229235 ]
230236 match (InstanceOf _ val ty) = mconcat <$> sequence
231− [ return $ emit "Contains("
237+ [ return $ emit "Is("
232238 , prettyPrintIL' val
233239 , return $ emit ", "
234240 , prettyPrintIL' ty
@@ -401,8 +407,7 @@ prettyPrintIL' = A.runKleisli $ runPattern matchValue
401407 , [ binary Add "+"
402408 , binary Subtract "-" ]
403409 , [ binary ShiftLeft "<<"
404− , binary ShiftRight ">>"
405− , binary ZeroFillShiftRight ">>>" ]
410+ , binary ShiftRight ">>" ]
406411 , [ binary LessThan "<"
407412 , binary LessThanOrEqualTo "<="
408413 , binary GreaterThan ">"
@@ -442,19 +447,24 @@ interfaceSource _ _ _ = ""
442447
443448implHeaderSource :: Text -> [Text] -> Text -> Text
444449implHeaderSource mn imports otherPrefix =
445− let imports' = T.replace "_" "." <$> imports in
446450 "// Code generated by psgo. DO NOT EDIT.\n\n" <>
447− "package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
451+ "package " <> (if mn == "Main" || mn == "Test_Main" then "main" else mn) <> "\n\n" <>
448452 "import . \"" <> runtime <> "\"\n" <>
449− (if mn == "Main"
453+ (if mn == "Main" || mn == "Test_Main"
450454 then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
451455 else "\n") <>
452456 "import (\n" <>
453− (T.concat $ formatImport <$> imports') <> ")\n\n" <>
457+ (T.concat $ formatImport <$> imports) <> ")\n\n" <>
454458 "type _ = Any\n\n"
455459 where
456460 formatImport :: Text -> Text
457− formatImport s = " \"" <> otherPrefix <> "/" <> modPrefix <> "/" <> s <> "\"\n"
461+ formatImport s =
462+ "\t\"" <> otherPrefix <> "/" <> modPrefix <> "/" <> moduleRenamer s <> "\"\n"
463+ moduleRenamer :: Text -> Text
464+ moduleRenamer s
465+ | (s', sfx) <- T.breakOn moduleRenamerMarker s,
466+ not $ T.null sfx = s'
467+ moduleRenamer s = s
458468
459469implFooterSource :: Text -> [Ident] -> Text
460470implFooterSource mn foreigns =
@@ -477,12 +487,12 @@ implFooterSource mn foreigns =
477487 " })\n" <>
478488 " return " <> valueName name <> "\n" <>
479489 "}\n\n") <$> foreigns))) <>
480− if mn == "Main" then mainSource else "\n"
490+ if mn == "Main" || mn == "Test.Main" then mainSource else "\n"
481491 where
482492 mainSource :: Text
483493 mainSource = "\
484494 \func main() {\n\
485− \ Run(PS__main())\n\
495+ \ Run(Ꞌmain())\n\
486496 \}\n\n\
487497 \"
488498
@@ -511,7 +521,7 @@ ffiLoader :: Text
511521ffiLoader = modLabel <> "/ffi-loader"
512522
513523initName :: Text -> Text
514−initName s = "ₒ" <> s
524+initName s = "ᛌ" <> s <> "_once"
515525
516526valueName :: Text -> Text
517−valueName s = "ₐ" <> s
527+valueName s = "ᛧ" <> s
src/Tests.hs+6−4
@@ -31,7 +31,7 @@ runTests = do
3131 let srcDir = outputDir </> "src"
3232 createDirectory srcDir
3333
34− callProcess "git" ["clone", "--branch", "v0.13.0", "--depth", "1", "https://github.com/purescript/purescript.git"]
34+ callProcess "git" ["clone", "--branch", "v0.13.8", "--depth", "1", "https://github.com/purescript/purescript.git"]
3535 let passingDir = baseDir </> "purescript" </> "tests" </> "purs" </> "passing"
3636 passingTestCases <- sort . filter (".purs" `isSuffixOf`) <$> getDirectoryContents passingDir
3737
@@ -62,10 +62,9 @@ runTests = do
6262
6363 callProcess "rm" ["-rf", "output"]
6464 callProcess "rm" ["-rf", "Main"]
65− callProcess "spago" ["build", "--", "--codegen", "corefn"]
66− callProcess "psgo" []
65+ callProcess "spago" ["build"]
6766 --
68− -- Run C++ files
67+ -- Run executable file
6968 --
7069 outputFile <- openFile (tmp </> logpath </> logfile) AppendMode
7170 hPutStrLn outputFile ("\n" ++ inputFile ++ ":")
@@ -114,6 +113,9 @@ fetchPackages :: IO ()
114113-------------------------------------------------------------------------------
115114fetchPackages = do
116115 callProcess "spago" ["init"]
116+ callCommand "sed '$s/\\(}\\)/, backend = \"psgo\"\\1/' < spago.dhall | dhall format > spago.tmp"
117+ callCommand "mv spago.tmp spago.dhall"
118+
117119 mapM (callProcess "spago" . (\p -> ["install", p])) packages
118120 return ()
119121
stack.nix+18−3
@@ -1,8 +1,23 @@
1−{ pkgs ? import <nixpkgs> {}, ghc ? pkgs.ghc }:
1+#
2+# You may need to manually run `unset STACK_IN_NIX_SHELL`
3+# (doesn't work from a `shellHook`; see: https://github.com/commercialhaskell/stack/issues/5000
4+#
5+#
6+# You may need to set LC_ALL when building, e.g.
7+# LC_ALL=en_US.iso88591 stack --nix build; see e.g. https://github.com/koalaman/shellcheck/issues/324
8+#
9+#
10+{ pkgs ?
11+ import (builtins.fetchTarball {
12+ name = "nixos-release-19.03";
13+ url = https://github.com/nixos/nixpkgs/archive/34c7eb7545d155cc5b6f499b23a7cb1c96ab4d59.tar.gz;
14+ sha256 = "11z6ajj108fy2q5g8y4higlcaqncrbjm3dnv17pvif6avagw4mcb";
15+ }) {}
16+}:
217
318pkgs.haskell.lib.buildStackProject {
419 name = "default-stack-shell";
5− inherit ghc;
6− buildInputs = with pkgs; [git git-lfs gmp zlib];
20+ ghc = pkgs.haskell.compiler.ghc865;
21+ buildInputs = with pkgs; [git git-lfs gmp ncurses zlib];
722 LANG = "en_US.UTF-8";
823}
stack.yaml+15−65
@@ -1,74 +1,24 @@
1−# This file was automatically generated by 'stack init'
2−#
3−# Some commonly used options have been documented as comments in this file.
4−# For advanced use and comprehensive documentation of the format, please see:
5−# https://docs.haskellstack.org/en/stable/yaml_configuration/
1+resolver: lts-13.26
62
7−# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8−# A snapshot resolver dictates the compiler version and the set of packages
9−# to be used for project dependencies. For example:
10−#
11−# resolver: lts-3.5
12−# resolver: nightly-2015-09-21
13−# resolver: ghc-7.10.2
14−# resolver: ghcjs-0.1.0_ghc-7.10.2
15−# resolver:
16−# name: custom-snapshot
17−# location: "./custom-snapshot.yaml"
18−resolver: lts-13.12
19−
20−# User packages to be built.
21−# Various formats can be used as shown in the example below.
22−#
23−# packages:
24−# - some-directory
25−# - https://example.com/foo/bar/baz-0.0.2.tar.gz
26−# - location:
27−# git: https://github.com/commercialhaskell/stack.git
28−# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
29−# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
30−# extra-dep: true
31−# subdirs:
32−# - auto-update
33−# - wai
34−#
35−# A package marked 'extra-dep: true' will only be built if demanded by a
36−# non-dependency (i.e. a user package), and its test suites and benchmarks
37−# will not be run. This is useful for tweaking upstream packages.
383packages:
394- .
40−# Dependency packages to be pulled from upstream that are not in the resolver
41−# (e.g., acme-missiles-0.3)
5+
426extra-deps:
43−- git: https://github.com/purescript/purescript
44− commit: 738aaa2f21097c7f18aa8100b16ae52316addb04
7+- git: https://github.com/purescript/purescript
8+ commit: 9cad73ed8ea7df3011032ddbd2f3de5a0c08629c
9+- happy-1.19.9
10+- language-javascript-0.7.0.0
4511- network-3.0.1.1
46−- pipes-http-1.0.5
47−
48−# Override default flag values for local packages and extra-deps
49−# flags: {}
50−
51−# Extra package databases containing global packages
52−# extra-package-dbs: []
53−
54−# Control whether we use the GHC we find on the path
55−# system-ghc: true
56−#
57−# Require a specific version of stack, using version ranges
58−# require-stack-version: -any # Default
59−# require-stack-version: ">=1.6"
60−#
61−# Override the architecture used by stack, especially useful on Windows
62−# arch: i386
63−# arch: x86_64
64−#
65−# Extra directories used by stack for building
66−# extra-include-dirs: [/path/to/dir]
67−# extra-lib-dirs: [/path/to/dir]
68−#
69−# Allow a newer minor version of GHC than the snapshot specifies
70−# compiler-check: newer-minor
12+- these-1.0.1
13+- semialign-1
7114
7215nix:
16+ enable: false
7317 shell-file: stack.nix
7418
19+flags:
20+ aeson-pretty:
21+ lib-only: true
22+ these:
23+ assoc: false
24+ quickcheck: false
support/ffi-loader.go+2
@@ -8,11 +8,13 @@ import (
88 _ "github.com/purescript-native/go-ffi/purescript-assert"
99 _ "github.com/purescript-native/go-ffi/purescript-console"
1010 _ "github.com/purescript-native/go-ffi/purescript-effect"
11+ _ "github.com/purescript-native/go-ffi/purescript-enums"
1112 _ "github.com/purescript-native/go-ffi/purescript-exceptions"
1213 _ "github.com/purescript-native/go-ffi/purescript-foldable-traversable"
1314 _ "github.com/purescript-native/go-ffi/purescript-functions"
1415 _ "github.com/purescript-native/go-ffi/purescript-globals"
1516 _ "github.com/purescript-native/go-ffi/purescript-integers"
17+ _ "github.com/purescript-native/go-ffi/purescript-lazy"
1618 _ "github.com/purescript-native/go-ffi/purescript-math"
1719 _ "github.com/purescript-native/go-ffi/purescript-partial"
1820 _ "github.com/purescript-native/go-ffi/purescript-prelude"