mirror of
https://github.com/aristanetworks/purescript-backend-optimizer.git
synced 2024-11-22 04:13:32 +03:00
Cleanup node/aff specific utils, add tidyrc (#22)
This commit is contained in:
parent
49003bdd7c
commit
7dfb4fa43d
10
.tidyrc.json
Normal file
10
.tidyrc.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"importSort": "ide",
|
||||
"importWrap": "source",
|
||||
"indent": 2,
|
||||
"operatorsFile": null,
|
||||
"ribbon": 1,
|
||||
"typeArrowPlacement": "first",
|
||||
"unicode": "never",
|
||||
"width": null
|
||||
}
|
@ -35,8 +35,8 @@ import Node.Path (FilePath)
|
||||
import Node.Path as Path
|
||||
import Node.Process as Process
|
||||
import Node.Stream as Stream
|
||||
import PureScript.Backend.Optimizer.Builder.Cli (basicBuildMain, externalDirectivesFromFile)
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript (esCodegenModule, esModulePath)
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript.Builder (basicBuildMain, externalDirectivesFromFile)
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript.Foreign (esForeignSemantics)
|
||||
import PureScript.Backend.Optimizer.CoreFn (Module(..), ModuleName(..))
|
||||
import PureScript.Backend.Optimizer.Semantics.Foreign (coreForeignSemantics)
|
||||
|
@ -29,7 +29,7 @@ import PureScript.Backend.Optimizer.Codegen.EcmaScript.Common (EsStatement(..),
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript.Inline (esInlineMap)
|
||||
import PureScript.Backend.Optimizer.Codegen.Tco (LocalRef, TcoAnalysis(..), TcoExpr(..), TcoPop, TcoRef(..), TcoRole, TcoScope, TcoScopeItem)
|
||||
import PureScript.Backend.Optimizer.Codegen.Tco as Tco
|
||||
import PureScript.Backend.Optimizer.Convert (BackendImplementations, BackendModule, BackendBindingGroup)
|
||||
import PureScript.Backend.Optimizer.Convert (BackendBindingGroup, BackendImplementations, BackendModule)
|
||||
import PureScript.Backend.Optimizer.CoreFn (ConstructorType(..), Ident(..), Literal(..), ModuleName(..), Prop(..), ProperName(..), Qualified(..), propValue, qualifiedModuleName, unQualified)
|
||||
import PureScript.Backend.Optimizer.Semantics (CtorMeta, DataTypeMeta, ExternImpl(..), NeutralExpr)
|
||||
import PureScript.Backend.Optimizer.Syntax (BackendAccessor(..), BackendEffect(..), BackendOperator(..), BackendOperator1(..), BackendOperator2(..), BackendOperatorNum(..), BackendOperatorOrd(..), BackendSyntax(..), Level(..), Pair(..))
|
||||
|
@ -1,28 +1,58 @@
|
||||
module PureScript.Backend.Optimizer.Builder.Cli where
|
||||
module PureScript.Backend.Optimizer.Codegen.EcmaScript.Builder where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Parallel (parTraverse)
|
||||
import Data.Argonaut as Json
|
||||
import Data.Array as Array
|
||||
import Data.Array.NonEmpty (NonEmptyArray)
|
||||
import Data.Array.NonEmpty as NonEmptyArray
|
||||
import Data.Bifunctor (lmap)
|
||||
import Data.Compactable (separate)
|
||||
import Data.Either (Either(..))
|
||||
import Data.Foldable (for_)
|
||||
import Data.List (List)
|
||||
import Data.Map (Map)
|
||||
import Data.Map as Map
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Data.Tuple (Tuple(..))
|
||||
import Effect.Aff (Aff, parallel, sequential)
|
||||
import Effect.Class (liftEffect)
|
||||
import Effect.Class.Console as Console
|
||||
import Node.Encoding (Encoding(..))
|
||||
import Node.FS.Aff as FS
|
||||
import Node.Glob.Basic (expandGlobs)
|
||||
import Node.Path (FilePath)
|
||||
import Node.Process as Process
|
||||
import PureScript.Backend.Optimizer.Builder (BuildEnv, buildModules, coreFnModulesFromOutput)
|
||||
import PureScript.Backend.Optimizer.Builder (BuildEnv, buildModules)
|
||||
import PureScript.Backend.Optimizer.Convert (BackendModule)
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ann, Ident, Module, Qualified)
|
||||
import PureScript.Backend.Optimizer.CoreFn.Json (decodeModule)
|
||||
import PureScript.Backend.Optimizer.CoreFn.Sort (sortModules)
|
||||
import PureScript.Backend.Optimizer.Directives (parseDirectiveFile)
|
||||
import PureScript.Backend.Optimizer.Directives.Defaults as Defaults
|
||||
import PureScript.Backend.Optimizer.Semantics (EvalRef, InlineDirective)
|
||||
import PureScript.Backend.Optimizer.Semantics.Foreign (ForeignEval)
|
||||
import PureScript.CST.Errors (printParseError)
|
||||
|
||||
coreFnModulesFromOutput :: String -> Aff (Either (NonEmptyArray (Tuple FilePath String)) (List (Module Ann)))
|
||||
coreFnModulesFromOutput path = do
|
||||
{ left, right } <- map separate $ expandGlobs path [ "**/corefn.json" ] >>= Array.fromFoldable >>> parTraverse readCoreFnModule
|
||||
case NonEmptyArray.fromArray left of
|
||||
Just errors ->
|
||||
pure $ Left errors
|
||||
Nothing ->
|
||||
pure $ Right $ sortModules right
|
||||
|
||||
readCoreFnModule :: String -> Aff (Either (Tuple FilePath String) (Module Ann))
|
||||
readCoreFnModule filePath = do
|
||||
contents <- FS.readTextFile UTF8 filePath
|
||||
case lmap Json.printJsonDecodeError <<< decodeModule =<< Json.jsonParser contents of
|
||||
Left err -> do
|
||||
pure $ Left $ Tuple filePath err
|
||||
Right mod ->
|
||||
pure $ Right mod
|
||||
|
||||
externalDirectivesFromFile :: FilePath -> Aff (Map EvalRef InlineDirective)
|
||||
externalDirectivesFromFile filePath = do
|
||||
fileContent <- FS.readTextFile UTF8 filePath
|
@ -9,9 +9,9 @@ import Data.Tuple (Tuple(..))
|
||||
import Dodo as Dodo
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript.Common (esApp)
|
||||
import PureScript.Backend.Optimizer.Codegen.Tco (TcoExpr(..))
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ident, Literal(..), Qualified)
|
||||
import PureScript.Backend.Optimizer.Semantics.Foreign (qualified)
|
||||
import PureScript.Backend.Optimizer.Syntax (BackendSyntax(..))
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ident, Literal(..), Qualified)
|
||||
|
||||
type EsInline a = Tuple (Qualified Ident) (EsInlineCall a)
|
||||
type EsInlineCall a = (TcoExpr -> Dodo.Doc a) -> Qualified Ident -> Array TcoExpr -> Maybe (Dodo.Doc a)
|
||||
|
@ -31,8 +31,9 @@ import Node.FS.Aff as FS
|
||||
import Node.Glob.Basic (expandGlobsCwd)
|
||||
import Node.Path as Path
|
||||
import Node.Process as Process
|
||||
import PureScript.Backend.Optimizer.Builder (buildModules, coreFnModulesFromOutput)
|
||||
import PureScript.Backend.Optimizer.Builder (buildModules)
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript (esCodegenModule)
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript.Builder (coreFnModulesFromOutput)
|
||||
import PureScript.Backend.Optimizer.Codegen.EcmaScript.Foreign (esForeignSemantics)
|
||||
import PureScript.Backend.Optimizer.CoreFn (Module(..), ModuleName(..))
|
||||
import PureScript.Backend.Optimizer.Directives (parseDirectiveFile)
|
||||
|
@ -2,5 +2,7 @@
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"format": "purs-tidy format-in-place src backend-es/src backend-es/test/*.purs",
|
||||
"format:check": "purs-tidy check src backend-es/src backend-es/test/*.purs"
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ import Data.Set as Set
|
||||
import Data.String.CodeUnits as SCU
|
||||
import Data.Traversable (foldMap, foldr)
|
||||
import Data.Tuple (Tuple(..), snd)
|
||||
import PureScript.Backend.Optimizer.Syntax (class HasSyntax, BackendAccessor, BackendOperator(..), BackendOperator1(..), BackendSyntax(..), Level, Pair(..), sndPair, syntaxOf)
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ident, Literal(..), Qualified)
|
||||
import PureScript.Backend.Optimizer.Syntax (class HasSyntax, BackendAccessor, BackendOperator(..), BackendOperator1(..), BackendSyntax(..), Level, Pair(..), sndPair, syntaxOf)
|
||||
|
||||
data Capture = CaptureNone | CaptureBranch | CaptureClosure
|
||||
|
||||
|
@ -2,36 +2,19 @@ module PureScript.Backend.Optimizer.Builder
|
||||
( BuildEnv
|
||||
, BuildOptions
|
||||
, buildModules
|
||||
, readCoreFnModule
|
||||
, coreFnModulesFromOutput
|
||||
) where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Parallel (parTraverse)
|
||||
import Data.Argonaut as Json
|
||||
import Data.Array as Array
|
||||
import Data.Array.NonEmpty (NonEmptyArray)
|
||||
import Data.Array.NonEmpty as NonEmptyArray
|
||||
import Data.Bifunctor (lmap)
|
||||
import Data.Compactable (separate)
|
||||
import Data.Either (Either(..))
|
||||
import Data.FoldableWithIndex (foldrWithIndex)
|
||||
import Data.List (List, foldM)
|
||||
import Data.List as List
|
||||
import Data.Map (Map)
|
||||
import Data.Map as Map
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Data.Tuple (Tuple(..))
|
||||
import Effect.Aff (Aff)
|
||||
import Node.Encoding (Encoding(..))
|
||||
import Node.FS.Aff as FS
|
||||
import Node.Glob.Basic (expandGlobs)
|
||||
import Node.Path (FilePath)
|
||||
import Data.Tuple (Tuple)
|
||||
import PureScript.Backend.Optimizer.Analysis (BackendAnalysis)
|
||||
import PureScript.Backend.Optimizer.Convert (BackendModule, toBackendModule)
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ann, Ident, Module(..), Qualified)
|
||||
import PureScript.Backend.Optimizer.CoreFn.Json (decodeModule)
|
||||
import PureScript.Backend.Optimizer.CoreFn.Sort (sortModules)
|
||||
import PureScript.Backend.Optimizer.Semantics (EvalRef, ExternImpl, InlineDirective)
|
||||
import PureScript.Backend.Optimizer.Semantics.Foreign (ForeignEval)
|
||||
@ -42,32 +25,16 @@ type BuildEnv =
|
||||
, moduleIndex :: Int
|
||||
}
|
||||
|
||||
type BuildOptions =
|
||||
type BuildOptions m =
|
||||
{ directives :: Map EvalRef InlineDirective
|
||||
, foreignSemantics :: Map (Qualified Ident) ForeignEval
|
||||
, onPrepareModule :: BuildEnv -> Module Ann -> Aff (Module Ann)
|
||||
, onCodegenModule :: BuildEnv -> Module Ann -> BackendModule -> Aff Unit
|
||||
, onPrepareModule :: BuildEnv -> Module Ann -> m (Module Ann)
|
||||
, onCodegenModule :: BuildEnv -> Module Ann -> BackendModule -> m Unit
|
||||
}
|
||||
|
||||
coreFnModulesFromOutput :: FilePath -> Aff (Either (NonEmptyArray (Tuple FilePath String)) (List (Module Ann)))
|
||||
coreFnModulesFromOutput path = do
|
||||
{ left, right } <- map separate $ expandGlobs path [ "**/corefn.json" ] >>= Array.fromFoldable >>> parTraverse readCoreFnModule
|
||||
case NonEmptyArray.fromArray left of
|
||||
Just errors ->
|
||||
pure $ Left errors
|
||||
Nothing ->
|
||||
pure $ Right $ sortModules right
|
||||
|
||||
readCoreFnModule :: FilePath -> Aff (Either (Tuple FilePath String) (Module Ann))
|
||||
readCoreFnModule filePath = do
|
||||
contents <- FS.readTextFile UTF8 filePath
|
||||
case lmap Json.printJsonDecodeError <<< decodeModule =<< Json.jsonParser contents of
|
||||
Left err -> do
|
||||
pure $ Left $ Tuple filePath err
|
||||
Right mod ->
|
||||
pure $ Right mod
|
||||
|
||||
buildModules :: BuildOptions -> List (Module Ann) -> Aff Unit
|
||||
-- | Builds modules given a _sorted_ list of modules.
|
||||
-- | See `PureScript.Backend.Optimizer.CoreFn.Sort.sortModules`.
|
||||
buildModules :: forall m. Monad m => BuildOptions m -> List (Module Ann) -> m Unit
|
||||
buildModules options coreFnModules =
|
||||
void $ foldM go { directives: options.directives, implementations: Map.empty, moduleIndex: 0 } (sortModules coreFnModules)
|
||||
where
|
||||
|
@ -18,9 +18,9 @@ import Data.Set as Set
|
||||
import Data.Traversable (traverse)
|
||||
import Data.Tuple (Tuple(..))
|
||||
import PureScript.Backend.Optimizer.Analysis (Usage(..))
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ident, ModuleName, Qualified(..))
|
||||
import PureScript.Backend.Optimizer.Semantics (NeutralExpr(..))
|
||||
import PureScript.Backend.Optimizer.Syntax (BackendSyntax(..), Level, Pair(..))
|
||||
import PureScript.Backend.Optimizer.CoreFn (Ident, ModuleName, Qualified(..))
|
||||
|
||||
type LocalRef = Tuple (Maybe Ident) Level
|
||||
type TcoScope = List TcoScopeItem
|
||||
|
@ -20,6 +20,7 @@ import Data.Maybe (Maybe(..))
|
||||
import Data.String (Pattern(..))
|
||||
import Data.String as String
|
||||
import Data.Tuple (Tuple(..), fst)
|
||||
import PureScript.Backend.Optimizer.CoreFn (Comment(..), Ident(..), ModuleName(..), Qualified(..))
|
||||
import PureScript.Backend.Optimizer.Semantics (EvalRef(..), InlineDirective(..))
|
||||
import PureScript.Backend.Optimizer.Syntax (BackendAccessor(..))
|
||||
import PureScript.CST.Errors (ParseError(..))
|
||||
@ -27,7 +28,6 @@ import PureScript.CST.Lexer (lex)
|
||||
import PureScript.CST.Parser.Monad (Parser, PositionedError, eof, optional, runParser, take)
|
||||
import PureScript.CST.Types (IntValue(..), SourceToken, Token(..))
|
||||
import PureScript.CST.Types as CST
|
||||
import PureScript.Backend.Optimizer.CoreFn (Comment(..), Ident(..), ModuleName(..), Qualified(..))
|
||||
|
||||
type DirectiveFileResult =
|
||||
{ errors :: Array (Tuple String PositionedError)
|
||||
|
@ -22,9 +22,9 @@ import Data.String as String
|
||||
import Data.Tuple (Tuple(..), fst, snd)
|
||||
import Partial.Unsafe (unsafeCrashWith, unsafePartial)
|
||||
import PureScript.Backend.Optimizer.Analysis (class HasAnalysis, BackendAnalysis(..), Capture(..), Complexity(..), ResultTerm(..), Usage(..), analysisOf, analyze, analyzeEffectBlock, bound, bump, complex, resultOf, updated, withResult, withRewrite)
|
||||
import PureScript.Backend.Optimizer.CoreFn (ConstructorType, Ident(..), Literal(..), ModuleName, Prop(..), ProperName, Qualified(..), findProp, propKey, propValue)
|
||||
import PureScript.Backend.Optimizer.Syntax (class HasSyntax, BackendAccessor(..), BackendEffect, BackendOperator(..), BackendOperator1(..), BackendOperator2(..), BackendOperatorNum(..), BackendOperatorOrd(..), BackendSyntax(..), Level(..), Pair(..), syntaxOf)
|
||||
import PureScript.Backend.Optimizer.Utils (foldl1Array)
|
||||
import PureScript.Backend.Optimizer.CoreFn (ConstructorType, Ident(..), Literal(..), ModuleName, Prop(..), ProperName, Qualified(..), findProp, propKey, propValue)
|
||||
|
||||
type Spine a = Array a
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user