From b290d72ff55bd5516b0b208fe15c9b1b31e4b686 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 11 Oct 2019 00:21:47 +0200 Subject: [PATCH 1/5] Make types in package.elm-lang.org a bit better --- library-todo.txt | 4 +- src/Elm/Compiler.elm | 161 +++++++++++++++---------------------------- 2 files changed, 57 insertions(+), 108 deletions(-) diff --git a/library-todo.txt b/library-todo.txt index 189c0f90..e9e6e8e2 100644 --- a/library-todo.txt +++ b/library-todo.txt @@ -2,4 +2,6 @@ - [ ] desugarExpr is needlessly complicated... try creating some dummy throwaway module for that one expr? Does that change behaviour ("can't find variable x")? - [ ] Emit helpers - [ ] Emit.JavaScript -- [ ] Elm.Compiler.compileToJS - do the whole pipeline with default settings. Show how it's written in doc comment +- [ ] Elm.Compiler.compileToJS - do the whole pipeline with default settings. Show how it's written in doc comment. Also show it in library README. +- [ ] maybe show the Ellie demo/playground in library README? + diff --git a/src/Elm/Compiler.elm b/src/Elm/Compiler.elm index 3504e927..06749d3f 100644 --- a/src/Elm/Compiler.elm +++ b/src/Elm/Compiler.elm @@ -9,6 +9,10 @@ module Elm.Compiler exposing {-| Functions for working with Elm source code. +> Because of package.elm-lang.org not displaying qualifiers correctly in type +> annotations, we're forced to use aliases for the various `Elm.AST.*` types. +> So when you see `FrontendLocatedExpr`, look at [`Elm.AST.Frontend.LocatedExpr`](Elm.AST.Frontend#LocatedExpr). + The compiler phases in general look like this: ![Stages of the compiler](https://github.com/elm-in-elm/compiler/raw/master/assets/stages.png) @@ -193,7 +197,7 @@ use [`Elm.AST.Frontend.unwrap`](Elm.AST.Frontend#unwrap) to get something like (String "Hello") -} -parseExpr : FileContents -> Result Error Frontend.LocatedExpr +parseExpr : FileContents -> Result Error FrontendLocatedExpr parseExpr sourceCode = parse Stage.Parse.Parser.expr sourceCode @@ -239,7 +243,7 @@ will get parsed into -} parseModule : { filePath : FilePath, sourceCode : FileContents } - -> Result Error (Module Frontend.LocatedExpr) + -> Result Error (Module FrontendLocatedExpr) parseModule { filePath, sourceCode } = -- TODO maybe we can think of a way to not force the user to give us `filePath`? parse (Stage.Parse.Parser.module_ filePath) sourceCode @@ -249,7 +253,7 @@ parseModule { filePath, sourceCode } = -} parseModules : List { filePath : FilePath, sourceCode : FileContents } - -> Result Error (Dict ModuleName (Module Frontend.LocatedExpr)) + -> Result Error (Dict ModuleName (Module FrontendLocatedExpr)) parseModules files = {- TODO same as with `parseModule` - maybe we can think of a way to not force the user to give us `filePath`? @@ -301,7 +305,7 @@ into } -} -parseDeclaration : { moduleName : ModuleName, declaration : FileContents } -> Result Error (Declaration Frontend.LocatedExpr) +parseDeclaration : { moduleName : ModuleName, declaration : FileContents } -> Result Error (Declaration FrontendLocatedExpr) parseDeclaration { moduleName, declaration } = parse Stage.Parse.Parser.declaration declaration |> Result.map (\toDeclaration -> toDeclaration moduleName) @@ -329,56 +333,33 @@ into AST like } } -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Dict ModuleName (Module Frontend.LocatedExpr) - -> Module Frontend.LocatedExpr - -> Frontend.LocatedExpr - -> Result Error Canonical.LocatedExpr - -} desugarExpr : - Dict ModuleName (Module Frontend.LocatedExpr) - -> Module Frontend.LocatedExpr - -> Frontend.LocatedExpr - -> Result Error Canonical.LocatedExpr + Dict ModuleName (Module FrontendLocatedExpr) + -> Module FrontendLocatedExpr + -> FrontendLocatedExpr + -> Result Error CanonicalLocatedExpr desugarExpr modules thisModule locatedExpr = Stage.Desugar.desugarExpr modules thisModule locatedExpr |> Result.mapError DesugarError {-| Desugar a module (one `*.elm` file). - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Dict ModuleName (Module Frontend.LocatedExpr) - -> Module Frontend.LocatedExpr - -> Result Error (Module Canonical.LocatedExpr) - -} desugarModule : - Dict ModuleName (Module Frontend.LocatedExpr) - -> Module Frontend.LocatedExpr - -> Result Error (Module Canonical.LocatedExpr) + Dict ModuleName (Module FrontendLocatedExpr) + -> Module FrontendLocatedExpr + -> Result Error (Module CanonicalLocatedExpr) desugarModule modules thisModule = Stage.Desugar.Boilerplate.desugarModule (Stage.Desugar.desugarExpr modules) thisModule |> Result.mapError DesugarError {-| Desugar multiple modules (`*.elm` files) - see [`desugarModule`](#desugarModule) for details. - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Dict ModuleName (Module Frontend.LocatedExpr) - -> Result Error (Dict ModuleName (Module Canonical.LocatedExpr)) - -} desugarModules : - Dict ModuleName (Module Frontend.LocatedExpr) - -> Result Error (Dict ModuleName (Module Canonical.LocatedExpr)) + Dict ModuleName (Module FrontendLocatedExpr) + -> Result Error (Dict ModuleName (Module CanonicalLocatedExpr)) desugarModules modules = modules |> Dict.map (always (Stage.Desugar.Boilerplate.desugarModule (Stage.Desugar.desugarExpr modules))) @@ -388,17 +369,10 @@ desugarModules modules = {-| Desugar a module (one `*.elm` file), without the intention of desugaring another one. - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Module Frontend.LocatedExpr - -> Result Error (Module Canonical.LocatedExpr) - -} desugarOnlyModule : - Module Frontend.LocatedExpr - -> Result Error (Module Canonical.LocatedExpr) + Module FrontendLocatedExpr + -> Result Error (Module CanonicalLocatedExpr) desugarOnlyModule module_ = desugarModule (Dict.singleton module_.name module_) @@ -410,48 +384,28 @@ desugarOnlyModule module_ = {-| Infer the types of a single expression. - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Canonical.LocatedExpr -> Result Error Typed.LocatedExpr - -} -inferExpr : Canonical.LocatedExpr -> Result Error Typed.LocatedExpr +inferExpr : CanonicalLocatedExpr -> Result Error TypedLocatedExpr inferExpr locatedExpr = Stage.InferTypes.inferExpr locatedExpr |> Result.mapError TypeError {-| Infer the types of expressions in a module (a single `*.elm` file). - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Module Canonical.LocatedExpr - -> Result Error (Module Typed.LocatedExpr) - -} inferModule : - Module Canonical.LocatedExpr - -> Result Error (Module Typed.LocatedExpr) + Module CanonicalLocatedExpr + -> Result Error (Module TypedLocatedExpr) inferModule thisModule = Stage.InferTypes.Boilerplate.inferModule Stage.InferTypes.inferExpr thisModule |> Result.mapError TypeError {-| Infer the types of expressions in multiple modules (`*.elm` files). - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Dict ModuleName (Module Canonical.LocatedExpr) - -> Result Error (Dict ModuleName (Module Typed.LocatedExpr)) - -} inferModules : - Dict ModuleName (Module Canonical.LocatedExpr) - -> Result Error (Dict ModuleName (Module Typed.LocatedExpr)) + Dict ModuleName (Module CanonicalLocatedExpr) + -> Result Error (Dict ModuleName (Module TypedLocatedExpr)) inferModules modules = modules |> Dict.map (always inferModule) @@ -484,7 +438,7 @@ inferModules modules = |> List.filter (\(name, _) -> Set.member name wantedOptimizations) -} -defaultOptimizations : List ( String, Typed.LocatedExpr -> Maybe Typed.LocatedExpr ) +defaultOptimizations : List ( String, TypedLocatedExpr -> Maybe TypedLocatedExpr ) defaultOptimizations = Stage.Optimize.defaultOptimizations @@ -495,7 +449,7 @@ For using your own optimizations instead of or in addition to the default ones, look at the [`optimizeExprWith`](#optimizeExprWith) function. -} -optimizeExpr : Typed.LocatedExpr -> Typed.LocatedExpr +optimizeExpr : TypedLocatedExpr -> TypedLocatedExpr optimizeExpr locatedExpr = Stage.Optimize.optimizeExpr locatedExpr @@ -503,9 +457,9 @@ optimizeExpr locatedExpr = {-| Optimize a given (typed) expression using a custom set of optimizations. -} optimizeExprWith : - List ( String, Typed.LocatedExpr -> Maybe Typed.LocatedExpr ) - -> Typed.LocatedExpr - -> Typed.LocatedExpr + List ( String, TypedLocatedExpr -> Maybe TypedLocatedExpr ) + -> TypedLocatedExpr + -> TypedLocatedExpr optimizeExprWith optimizations locatedExpr = Stage.Optimize.optimizeExprWith optimizations locatedExpr @@ -520,7 +474,7 @@ For using your own optimizations instead of or in addition to the default ones, look at the [`optimizeModuleWith`](#optimizeModuleWith) function. -} -optimizeModule : Module Typed.LocatedExpr -> Module Typed.LocatedExpr +optimizeModule : Module TypedLocatedExpr -> Module TypedLocatedExpr optimizeModule thisModule = Stage.Optimize.Boilerplate.optimizeModule optimizeExpr thisModule @@ -533,9 +487,9 @@ only the optimizations on each separate expression. -} optimizeModuleWith : - List ( String, Typed.LocatedExpr -> Maybe Typed.LocatedExpr ) - -> Module Typed.LocatedExpr - -> Module Typed.LocatedExpr + List ( String, TypedLocatedExpr -> Maybe TypedLocatedExpr ) + -> Module TypedLocatedExpr + -> Module TypedLocatedExpr optimizeModuleWith optimizations thisModule = Stage.Optimize.Boilerplate.optimizeModule (optimizeExprWith optimizations) thisModule @@ -548,8 +502,8 @@ look at the [`optimizeModulesWith`](#optimizeModulesWith) function. -} optimizeModules : - Dict ModuleName (Module Typed.LocatedExpr) - -> Dict ModuleName (Module Typed.LocatedExpr) + Dict ModuleName (Module TypedLocatedExpr) + -> Dict ModuleName (Module TypedLocatedExpr) optimizeModules modules = Dict.map (always optimizeModule) modules @@ -558,9 +512,9 @@ optimizeModules modules = optimizations. -} optimizeModulesWith : - List ( String, Typed.LocatedExpr -> Maybe Typed.LocatedExpr ) - -> Dict ModuleName (Module Typed.LocatedExpr) - -> Dict ModuleName (Module Typed.LocatedExpr) + List ( String, TypedLocatedExpr -> Maybe TypedLocatedExpr ) + -> Dict ModuleName (Module TypedLocatedExpr) + -> Dict ModuleName (Module TypedLocatedExpr) optimizeModulesWith optimizations modules = Dict.map (always (optimizeModuleWith optimizations)) modules @@ -571,11 +525,6 @@ optimizeModulesWith optimizations modules = {-| Drop types from a single expression. -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Typed.LocatedExpr -> Canonical.LocatedExpr - Example usage: Located @@ -599,36 +548,34 @@ If location info is not useful to you either, look for the `unwrap` functions in the various `Elm.AST.*` modules. -} -dropTypesExpr : Typed.LocatedExpr -> Canonical.LocatedExpr +dropTypesExpr : TypedLocatedExpr -> CanonicalLocatedExpr dropTypesExpr locatedExpr = Typed.dropTypes locatedExpr {-| Drop types from all expressions in the module. - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Module Typed.LocatedExpr - -> Module Canonical.LocatedExpr - -} -dropTypesModule : Module Typed.LocatedExpr -> Module Canonical.LocatedExpr +dropTypesModule : Module TypedLocatedExpr -> Module CanonicalLocatedExpr dropTypesModule module_ = Module.map dropTypesExpr module_ {-| Drop types from all expressions in all the modules. - -We're hitting limitations of the Elm Packages website, and the type shown isn't -very descriptive. **The real type of this function is:** - - Dict ModuleName (Module Typed.LocatedExpr) - -> Dict ModuleName (Module Canonical.LocatedExpr) - -} dropTypesModules : - Dict ModuleName (Module Typed.LocatedExpr) - -> Dict ModuleName (Module Canonical.LocatedExpr) + Dict ModuleName (Module TypedLocatedExpr) + -> Dict ModuleName (Module CanonicalLocatedExpr) dropTypesModules modules = Dict.map (always dropTypesModule) modules + + +type alias FrontendLocatedExpr = + Frontend.LocatedExpr + + +type alias CanonicalLocatedExpr = + Canonical.LocatedExpr + + +type alias TypedLocatedExpr = + Typed.LocatedExpr From 5d95bf10eb0e05cdc11b2229af08122670a9cd65 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 11 Oct 2019 00:29:13 +0200 Subject: [PATCH 2/5] Move stuff around, expose Emit and EmitJS --- cli/Main.elm | 10 ++--- elm.json | 4 +- src/Elm/Compiler.elm | 42 +++++++++---------- src/{ => Elm/Compiler}/Stage/Desugar.elm | 4 +- .../Compiler}/Stage/Desugar/Boilerplate.elm | 2 +- src/{ => Elm/Compiler}/Stage/Emit.elm | 2 +- .../Compiler}/Stage/Emit/JavaScript.elm | 4 +- src/{ => Elm/Compiler}/Stage/InferTypes.elm | 12 +++--- .../Compiler}/Stage/InferTypes/AssignIds.elm | 4 +- .../Stage/InferTypes/Boilerplate.elm | 2 +- .../Stage/InferTypes/GenerateEquations.elm | 6 +-- .../Compiler}/Stage/InferTypes/IdSource.elm | 2 +- .../Stage/InferTypes/SubstitutionMap.elm | 2 +- .../Stage/InferTypes/TypeEquation.elm | 2 +- .../Compiler}/Stage/InferTypes/Unify.elm | 6 +-- src/{ => Elm/Compiler}/Stage/Optimize.elm | 4 +- .../Compiler}/Stage/Optimize/Boilerplate.elm | 2 +- src/{ => Elm/Compiler}/Stage/Parse.elm | 4 +- src/{ => Elm/Compiler}/Stage/Parse/Parser.elm | 2 +- 19 files changed, 59 insertions(+), 57 deletions(-) rename src/{ => Elm/Compiler}/Stage/Desugar.elm (98%) rename src/{ => Elm/Compiler}/Stage/Desugar/Boilerplate.elm (98%) rename src/{ => Elm/Compiler}/Stage/Emit.elm (99%) rename src/{ => Elm/Compiler}/Stage/Emit/JavaScript.elm (98%) rename src/{ => Elm/Compiler}/Stage/InferTypes.elm (93%) rename src/{ => Elm/Compiler}/Stage/InferTypes/AssignIds.elm (98%) rename src/{ => Elm/Compiler}/Stage/InferTypes/Boilerplate.elm (97%) rename src/{ => Elm/Compiler}/Stage/InferTypes/GenerateEquations.elm (97%) rename src/{ => Elm/Compiler}/Stage/InferTypes/IdSource.elm (91%) rename src/{ => Elm/Compiler}/Stage/InferTypes/SubstitutionMap.elm (92%) rename src/{ => Elm/Compiler}/Stage/InferTypes/TypeEquation.elm (91%) rename src/{ => Elm/Compiler}/Stage/InferTypes/Unify.elm (92%) rename src/{ => Elm/Compiler}/Stage/Optimize.elm (95%) rename src/{ => Elm/Compiler}/Stage/Optimize/Boilerplate.elm (96%) rename src/{ => Elm/Compiler}/Stage/Parse.elm (87%) rename src/{ => Elm/Compiler}/Stage/Parse/Parser.elm (99%) diff --git a/cli/Main.elm b/cli/Main.elm index 48c81985..0b24a654 100644 --- a/cli/Main.elm +++ b/cli/Main.elm @@ -43,6 +43,11 @@ about returning those. import Dict exposing (Dict) import Elm.AST.Frontend as Frontend import Elm.Compiler.Error as Error exposing (Error(..), ParseError(..)) +import Elm.Compiler.Stage.Desugar as Desugar +import Elm.Compiler.Stage.Emit.JavaScript as EmitJS +import Elm.Compiler.Stage.InferTypes as InferTypes +import Elm.Compiler.Stage.Optimize as Optimize +import Elm.Compiler.Stage.Parse as Parse import Elm.Data.Declaration as Declaration import Elm.Data.FileContents exposing (FileContents) import Elm.Data.FilePath as FilePath exposing (FilePath) @@ -54,11 +59,6 @@ import Json.Decode as JD import Platform import Ports exposing (println, printlnStderr) import Set exposing (Set) -import Stage.Desugar as Desugar -import Stage.Emit.JavaScript as EmitJS -import Stage.InferTypes as InferTypes -import Stage.Optimize as Optimize -import Stage.Parse as Parse {-| We're essentially a Node.JS app (until we get self-hosting :P ). diff --git a/elm.json b/elm.json index 0758d836..131541d3 100644 --- a/elm.json +++ b/elm.json @@ -7,6 +7,8 @@ "exposed-modules": [ "Elm.Compiler", "Elm.Compiler.Error", + "Elm.Compiler.Stage.Emit", + "Elm.Compiler.Stage.Emit.JavaScript", "Elm.Data.Binding", "Elm.Data.Declaration", "Elm.Data.Exposing", @@ -48,4 +50,4 @@ "elm-community/random-extra": "3.1.0 <= v < 4.0.0", "elm-explorations/test": "1.2.2 <= v < 2.0.0" } -} \ No newline at end of file +} diff --git a/src/Elm/Compiler.elm b/src/Elm/Compiler.elm index 06749d3f..1c9f34c5 100644 --- a/src/Elm/Compiler.elm +++ b/src/Elm/Compiler.elm @@ -139,6 +139,13 @@ import Elm.Compiler.Error , ParseError(..) , ParseProblem ) +import Elm.Compiler.Stage.Desugar as Desugar +import Elm.Compiler.Stage.Desugar.Boilerplate as DesugarB +import Elm.Compiler.Stage.InferTypes as InferTypes +import Elm.Compiler.Stage.InferTypes.Boilerplate as InferTypesB +import Elm.Compiler.Stage.Optimize as Optimize +import Elm.Compiler.Stage.Optimize.Boilerplate as OptimizeB +import Elm.Compiler.Stage.Parse.Parser as Parser import Elm.Data.Declaration exposing (Declaration) import Elm.Data.FileContents exposing (FileContents) import Elm.Data.FilePath exposing (FilePath) @@ -148,13 +155,6 @@ import Elm.Data.ModuleName exposing (ModuleName) import OurExtras.Dict as Dict import Parser.Advanced as P import Result.Extra as Result -import Stage.Desugar -import Stage.Desugar.Boilerplate -import Stage.InferTypes -import Stage.InferTypes.Boilerplate -import Stage.Optimize -import Stage.Optimize.Boilerplate -import Stage.Parse.Parser @@ -199,7 +199,7 @@ use [`Elm.AST.Frontend.unwrap`](Elm.AST.Frontend#unwrap) to get something like -} parseExpr : FileContents -> Result Error FrontendLocatedExpr parseExpr sourceCode = - parse Stage.Parse.Parser.expr sourceCode + parse Parser.expr sourceCode {-| Parse a module (one `*.elm` file). Get a [`Module`](Elm.Data.Module#Module) datastructure back, holding @@ -246,7 +246,7 @@ parseModule : -> Result Error (Module FrontendLocatedExpr) parseModule { filePath, sourceCode } = -- TODO maybe we can think of a way to not force the user to give us `filePath`? - parse (Stage.Parse.Parser.module_ filePath) sourceCode + parse (Parser.module_ filePath) sourceCode {-| Parse multiple modules (`*.elm` files) - see [`parseModule`](#parseModule) for details. @@ -289,7 +289,7 @@ into -} parseImport : FileContents -> Result Error Import parseImport sourceCode = - parse Stage.Parse.Parser.import_ sourceCode + parse Parser.import_ sourceCode {-| Parse a single declaration, like @@ -307,7 +307,7 @@ into -} parseDeclaration : { moduleName : ModuleName, declaration : FileContents } -> Result Error (Declaration FrontendLocatedExpr) parseDeclaration { moduleName, declaration } = - parse Stage.Parse.Parser.declaration declaration + parse Parser.declaration declaration |> Result.map (\toDeclaration -> toDeclaration moduleName) @@ -340,7 +340,7 @@ desugarExpr : -> FrontendLocatedExpr -> Result Error CanonicalLocatedExpr desugarExpr modules thisModule locatedExpr = - Stage.Desugar.desugarExpr modules thisModule locatedExpr + Desugar.desugarExpr modules thisModule locatedExpr |> Result.mapError DesugarError @@ -351,7 +351,7 @@ desugarModule : -> Module FrontendLocatedExpr -> Result Error (Module CanonicalLocatedExpr) desugarModule modules thisModule = - Stage.Desugar.Boilerplate.desugarModule (Stage.Desugar.desugarExpr modules) thisModule + DesugarB.desugarModule (Desugar.desugarExpr modules) thisModule |> Result.mapError DesugarError @@ -362,7 +362,7 @@ desugarModules : -> Result Error (Dict ModuleName (Module CanonicalLocatedExpr)) desugarModules modules = modules - |> Dict.map (always (Stage.Desugar.Boilerplate.desugarModule (Stage.Desugar.desugarExpr modules))) + |> Dict.map (always (DesugarB.desugarModule (Desugar.desugarExpr modules))) |> Dict.combine |> Result.mapError DesugarError @@ -387,7 +387,7 @@ desugarOnlyModule module_ = -} inferExpr : CanonicalLocatedExpr -> Result Error TypedLocatedExpr inferExpr locatedExpr = - Stage.InferTypes.inferExpr locatedExpr + InferTypes.inferExpr locatedExpr |> Result.mapError TypeError @@ -397,7 +397,7 @@ inferModule : Module CanonicalLocatedExpr -> Result Error (Module TypedLocatedExpr) inferModule thisModule = - Stage.InferTypes.Boilerplate.inferModule Stage.InferTypes.inferExpr thisModule + InferTypesB.inferModule InferTypes.inferExpr thisModule |> Result.mapError TypeError @@ -440,7 +440,7 @@ inferModules modules = -} defaultOptimizations : List ( String, TypedLocatedExpr -> Maybe TypedLocatedExpr ) defaultOptimizations = - Stage.Optimize.defaultOptimizations + Optimize.defaultOptimizations {-| Optimize a given (typed) expression using the default set of optimizations. @@ -451,7 +451,7 @@ look at the [`optimizeExprWith`](#optimizeExprWith) function. -} optimizeExpr : TypedLocatedExpr -> TypedLocatedExpr optimizeExpr locatedExpr = - Stage.Optimize.optimizeExpr locatedExpr + Optimize.optimizeExpr locatedExpr {-| Optimize a given (typed) expression using a custom set of optimizations. @@ -461,7 +461,7 @@ optimizeExprWith : -> TypedLocatedExpr -> TypedLocatedExpr optimizeExprWith optimizations locatedExpr = - Stage.Optimize.optimizeExprWith optimizations locatedExpr + Optimize.optimizeExprWith optimizations locatedExpr {-| Optimize all expressions in a given module using the default set of @@ -476,7 +476,7 @@ look at the [`optimizeModuleWith`](#optimizeModuleWith) function. -} optimizeModule : Module TypedLocatedExpr -> Module TypedLocatedExpr optimizeModule thisModule = - Stage.Optimize.Boilerplate.optimizeModule optimizeExpr thisModule + OptimizeB.optimizeModule optimizeExpr thisModule {-| Optimize all expressions in a given module using a custom set of @@ -491,7 +491,7 @@ optimizeModuleWith : -> Module TypedLocatedExpr -> Module TypedLocatedExpr optimizeModuleWith optimizations thisModule = - Stage.Optimize.Boilerplate.optimizeModule (optimizeExprWith optimizations) thisModule + OptimizeB.optimizeModule (optimizeExprWith optimizations) thisModule {-| Optimize all expressions in multiple modules using the default set of diff --git a/src/Stage/Desugar.elm b/src/Elm/Compiler/Stage/Desugar.elm similarity index 98% rename from src/Stage/Desugar.elm rename to src/Elm/Compiler/Stage/Desugar.elm index 54cfb256..cc587408 100644 --- a/src/Stage/Desugar.elm +++ b/src/Elm/Compiler/Stage/Desugar.elm @@ -1,4 +1,4 @@ -module Stage.Desugar exposing (desugar, desugarExpr) +module Elm.Compiler.Stage.Desugar exposing (desugar, desugarExpr) import Basics.Extra exposing (flip) import Dict exposing (Dict) @@ -6,6 +6,7 @@ import Dict.Extra as Dict import Elm.AST.Canonical as Canonical import Elm.AST.Frontend as Frontend import Elm.Compiler.Error exposing (DesugarError(..), Error(..)) +import Elm.Compiler.Stage.Desugar.Boilerplate as Boilerplate import Elm.Data.Binding as Binding import Elm.Data.Located as Located import Elm.Data.Module as Module exposing (Module) @@ -14,7 +15,6 @@ import Elm.Data.Project exposing (Project) import Elm.Data.VarName exposing (VarName) import Maybe.Extra import Result.Extra as Result -import Stage.Desugar.Boilerplate as Boilerplate desugar : Project Frontend.ProjectFields -> Result Error (Project Canonical.ProjectFields) diff --git a/src/Stage/Desugar/Boilerplate.elm b/src/Elm/Compiler/Stage/Desugar/Boilerplate.elm similarity index 98% rename from src/Stage/Desugar/Boilerplate.elm rename to src/Elm/Compiler/Stage/Desugar/Boilerplate.elm index a0914a2d..d451f899 100644 --- a/src/Stage/Desugar/Boilerplate.elm +++ b/src/Elm/Compiler/Stage/Desugar/Boilerplate.elm @@ -1,4 +1,4 @@ -module Stage.Desugar.Boilerplate exposing +module Elm.Compiler.Stage.Desugar.Boilerplate exposing ( desugarModule , desugarProject ) diff --git a/src/Stage/Emit.elm b/src/Elm/Compiler/Stage/Emit.elm similarity index 99% rename from src/Stage/Emit.elm rename to src/Elm/Compiler/Stage/Emit.elm index 1ac805d2..bfaa1b53 100644 --- a/src/Stage/Emit.elm +++ b/src/Elm/Compiler/Stage/Emit.elm @@ -1,4 +1,4 @@ -module Stage.Emit exposing +module Elm.Compiler.Stage.Emit exposing ( projectToDeclarationList , modulesToDeclarationLists ) diff --git a/src/Stage/Emit/JavaScript.elm b/src/Elm/Compiler/Stage/Emit/JavaScript.elm similarity index 98% rename from src/Stage/Emit/JavaScript.elm rename to src/Elm/Compiler/Stage/Emit/JavaScript.elm index 8c2301fa..850d4352 100644 --- a/src/Stage/Emit/JavaScript.elm +++ b/src/Elm/Compiler/Stage/Emit/JavaScript.elm @@ -1,4 +1,4 @@ -module Stage.Emit.JavaScript exposing +module Elm.Compiler.Stage.Emit.JavaScript exposing ( emitProject , emitDeclaration, emitExpr ) @@ -20,13 +20,13 @@ testing purposes. import Dict exposing (Dict) import Elm.AST.Typed as Typed exposing (Expr_(..)) import Elm.Compiler.Error exposing (Error(..)) +import Elm.Compiler.Stage.Emit as Emit import Elm.Data.Declaration exposing (Declaration, DeclarationBody(..)) import Elm.Data.FileContents as FileContents exposing (FileContents) import Elm.Data.FilePath as FilePath exposing (FilePath) import Elm.Data.ModuleName as ModuleName exposing (ModuleName) import Elm.Data.Project exposing (Project) import Elm.Data.VarName as VarName exposing (VarName) -import Stage.Emit as Emit type alias ProjectFields = diff --git a/src/Stage/InferTypes.elm b/src/Elm/Compiler/Stage/InferTypes.elm similarity index 93% rename from src/Stage/InferTypes.elm rename to src/Elm/Compiler/Stage/InferTypes.elm index 20ee41e2..37886abd 100644 --- a/src/Stage/InferTypes.elm +++ b/src/Elm/Compiler/Stage/InferTypes.elm @@ -1,16 +1,16 @@ -module Stage.InferTypes exposing (inferExpr, inferTypes) +module Elm.Compiler.Stage.InferTypes exposing (inferExpr, inferTypes) import Elm.AST.Canonical as Canonical import Elm.AST.Typed as Typed import Elm.Compiler.Error exposing (Error(..), TypeError(..)) +import Elm.Compiler.Stage.InferTypes.AssignIds as AssignIds +import Elm.Compiler.Stage.InferTypes.Boilerplate as Boilerplate +import Elm.Compiler.Stage.InferTypes.GenerateEquations as GenerateEquations +import Elm.Compiler.Stage.InferTypes.SubstitutionMap as SubstitutionMap exposing (SubstitutionMap) +import Elm.Compiler.Stage.InferTypes.Unify as Unify import Elm.Data.Located as Located import Elm.Data.Project exposing (Project) import Elm.Data.Type exposing (Type(..)) -import Stage.InferTypes.AssignIds as AssignIds -import Stage.InferTypes.Boilerplate as Boilerplate -import Stage.InferTypes.GenerateEquations as GenerateEquations -import Stage.InferTypes.SubstitutionMap as SubstitutionMap exposing (SubstitutionMap) -import Stage.InferTypes.Unify as Unify {-| We're using Hindley-Milner algorithm (Algorithm W). It has essentially diff --git a/src/Stage/InferTypes/AssignIds.elm b/src/Elm/Compiler/Stage/InferTypes/AssignIds.elm similarity index 98% rename from src/Stage/InferTypes/AssignIds.elm rename to src/Elm/Compiler/Stage/InferTypes/AssignIds.elm index 1f1c1d9b..f0ad38a3 100644 --- a/src/Stage/InferTypes/AssignIds.elm +++ b/src/Elm/Compiler/Stage/InferTypes/AssignIds.elm @@ -1,4 +1,4 @@ -module Stage.InferTypes.AssignIds exposing (assignIds) +module Elm.Compiler.Stage.InferTypes.AssignIds exposing (assignIds) {-| Stage 1 @@ -42,9 +42,9 @@ Output: import Dict import Elm.AST.Canonical as Canonical import Elm.AST.Typed as Typed +import Elm.Compiler.Stage.InferTypes.IdSource as IdSource exposing (IdSource) import Elm.Data.Located as Located import Elm.Data.Type as Type -import Stage.InferTypes.IdSource as IdSource exposing (IdSource) assignIds : Canonical.LocatedExpr -> ( Typed.LocatedExpr, IdSource ) diff --git a/src/Stage/InferTypes/Boilerplate.elm b/src/Elm/Compiler/Stage/InferTypes/Boilerplate.elm similarity index 97% rename from src/Stage/InferTypes/Boilerplate.elm rename to src/Elm/Compiler/Stage/InferTypes/Boilerplate.elm index bacef283..74234564 100644 --- a/src/Stage/InferTypes/Boilerplate.elm +++ b/src/Elm/Compiler/Stage/InferTypes/Boilerplate.elm @@ -1,4 +1,4 @@ -module Stage.InferTypes.Boilerplate exposing +module Elm.Compiler.Stage.InferTypes.Boilerplate exposing ( inferModule , inferProject ) diff --git a/src/Stage/InferTypes/GenerateEquations.elm b/src/Elm/Compiler/Stage/InferTypes/GenerateEquations.elm similarity index 97% rename from src/Stage/InferTypes/GenerateEquations.elm rename to src/Elm/Compiler/Stage/InferTypes/GenerateEquations.elm index 6a60b470..3c0e937f 100644 --- a/src/Stage/InferTypes/GenerateEquations.elm +++ b/src/Elm/Compiler/Stage/InferTypes/GenerateEquations.elm @@ -1,4 +1,4 @@ -module Stage.InferTypes.GenerateEquations exposing (generateEquations) +module Elm.Compiler.Stage.InferTypes.GenerateEquations exposing (generateEquations) {-| Stage 2 @@ -31,11 +31,11 @@ subexpression. import Dict import Elm.AST.Typed as Typed +import Elm.Compiler.Stage.InferTypes.IdSource as IdSource exposing (IdSource) +import Elm.Compiler.Stage.InferTypes.TypeEquation exposing (TypeEquation, equals) import Elm.Data.Located as Located import Elm.Data.Type as Type import Elm.Data.VarName exposing (VarName) -import Stage.InferTypes.IdSource as IdSource exposing (IdSource) -import Stage.InferTypes.TypeEquation exposing (TypeEquation, equals) import Transform diff --git a/src/Stage/InferTypes/IdSource.elm b/src/Elm/Compiler/Stage/InferTypes/IdSource.elm similarity index 91% rename from src/Stage/InferTypes/IdSource.elm rename to src/Elm/Compiler/Stage/InferTypes/IdSource.elm index fcc4eca9..14d14e1b 100644 --- a/src/Stage/InferTypes/IdSource.elm +++ b/src/Elm/Compiler/Stage/InferTypes/IdSource.elm @@ -1,4 +1,4 @@ -module Stage.InferTypes.IdSource exposing +module Elm.Compiler.Stage.InferTypes.IdSource exposing ( IdSource , empty , increment diff --git a/src/Stage/InferTypes/SubstitutionMap.elm b/src/Elm/Compiler/Stage/InferTypes/SubstitutionMap.elm similarity index 92% rename from src/Stage/InferTypes/SubstitutionMap.elm rename to src/Elm/Compiler/Stage/InferTypes/SubstitutionMap.elm index b5e03ef5..c333442e 100644 --- a/src/Stage/InferTypes/SubstitutionMap.elm +++ b/src/Elm/Compiler/Stage/InferTypes/SubstitutionMap.elm @@ -1,4 +1,4 @@ -module Stage.InferTypes.SubstitutionMap exposing +module Elm.Compiler.Stage.InferTypes.SubstitutionMap exposing ( SubstitutionMap , empty , get diff --git a/src/Stage/InferTypes/TypeEquation.elm b/src/Elm/Compiler/Stage/InferTypes/TypeEquation.elm similarity index 91% rename from src/Stage/InferTypes/TypeEquation.elm rename to src/Elm/Compiler/Stage/InferTypes/TypeEquation.elm index c15857af..9743c9e0 100644 --- a/src/Stage/InferTypes/TypeEquation.elm +++ b/src/Elm/Compiler/Stage/InferTypes/TypeEquation.elm @@ -1,4 +1,4 @@ -module Stage.InferTypes.TypeEquation exposing +module Elm.Compiler.Stage.InferTypes.TypeEquation exposing ( TypeEquation , equals , unwrap diff --git a/src/Stage/InferTypes/Unify.elm b/src/Elm/Compiler/Stage/InferTypes/Unify.elm similarity index 92% rename from src/Stage/InferTypes/Unify.elm rename to src/Elm/Compiler/Stage/InferTypes/Unify.elm index a70aaf44..8956586e 100644 --- a/src/Stage/InferTypes/Unify.elm +++ b/src/Elm/Compiler/Stage/InferTypes/Unify.elm @@ -1,9 +1,9 @@ -module Stage.InferTypes.Unify exposing (unifyAllEquations) +module Elm.Compiler.Stage.InferTypes.Unify exposing (unifyAllEquations) import Elm.Compiler.Error exposing (TypeError(..)) +import Elm.Compiler.Stage.InferTypes.SubstitutionMap as SubstitutionMap exposing (SubstitutionMap) +import Elm.Compiler.Stage.InferTypes.TypeEquation as TypeEquation exposing (TypeEquation) import Elm.Data.Type as Type exposing (Type(..)) -import Stage.InferTypes.SubstitutionMap as SubstitutionMap exposing (SubstitutionMap) -import Stage.InferTypes.TypeEquation as TypeEquation exposing (TypeEquation) {-| TODO document diff --git a/src/Stage/Optimize.elm b/src/Elm/Compiler/Stage/Optimize.elm similarity index 95% rename from src/Stage/Optimize.elm rename to src/Elm/Compiler/Stage/Optimize.elm index 9bef00c1..52159c84 100644 --- a/src/Stage/Optimize.elm +++ b/src/Elm/Compiler/Stage/Optimize.elm @@ -1,4 +1,4 @@ -module Stage.Optimize exposing +module Elm.Compiler.Stage.Optimize exposing ( defaultOptimizations , optimize , optimizeExpr @@ -6,8 +6,8 @@ module Stage.Optimize exposing ) import Elm.AST.Typed as Typed +import Elm.Compiler.Stage.Optimize.Boilerplate as Boilerplate import Elm.Data.Project exposing (Project) -import Stage.Optimize.Boilerplate as Boilerplate optimize : Project Typed.ProjectFields -> Project Typed.ProjectFields diff --git a/src/Stage/Optimize/Boilerplate.elm b/src/Elm/Compiler/Stage/Optimize/Boilerplate.elm similarity index 96% rename from src/Stage/Optimize/Boilerplate.elm rename to src/Elm/Compiler/Stage/Optimize/Boilerplate.elm index c51c2439..c779ae81 100644 --- a/src/Stage/Optimize/Boilerplate.elm +++ b/src/Elm/Compiler/Stage/Optimize/Boilerplate.elm @@ -1,4 +1,4 @@ -module Stage.Optimize.Boilerplate exposing +module Elm.Compiler.Stage.Optimize.Boilerplate exposing ( optimizeModule , optimizeProject ) diff --git a/src/Stage/Parse.elm b/src/Elm/Compiler/Stage/Parse.elm similarity index 87% rename from src/Stage/Parse.elm rename to src/Elm/Compiler/Stage/Parse.elm index 1f50aa0e..82a09ebc 100644 --- a/src/Stage/Parse.elm +++ b/src/Elm/Compiler/Stage/Parse.elm @@ -1,13 +1,13 @@ -module Stage.Parse exposing (parse) +module Elm.Compiler.Stage.Parse exposing (parse) import Elm.AST.Frontend as Frontend import Elm.Compiler.Error exposing (Error(..), ParseError(..)) +import Elm.Compiler.Stage.Parse.Parser as Parser import Elm.Data.FileContents exposing (FileContents) import Elm.Data.FilePath exposing (FilePath) import Elm.Data.Module exposing (Module) import Elm.Data.ModuleName as ModuleName exposing (ModuleName) import Parser.Advanced as P -import Stage.Parse.Parser as Parser {-| This `parse` function is used only by cli/, not by library/. diff --git a/src/Stage/Parse/Parser.elm b/src/Elm/Compiler/Stage/Parse/Parser.elm similarity index 99% rename from src/Stage/Parse/Parser.elm rename to src/Elm/Compiler/Stage/Parse/Parser.elm index 6550965a..90c6bc83 100644 --- a/src/Stage/Parse/Parser.elm +++ b/src/Elm/Compiler/Stage/Parse/Parser.elm @@ -1,4 +1,4 @@ -module Stage.Parse.Parser exposing +module Elm.Compiler.Stage.Parse.Parser exposing ( declaration , exposingList , expr From de089b96fa94089616859a895065980bdd61e312 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 11 Oct 2019 00:32:14 +0200 Subject: [PATCH 3/5] Fix tests --- tests/EmitTest.elm | 2 +- tests/InferTypesFuzz.elm | 4 ++-- tests/InferTypesTest.elm | 4 ++-- tests/OptimizeTest.elm | 4 ++-- tests/ParserTest.elm | 12 ++++++------ 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/EmitTest.elm b/tests/EmitTest.elm index f8629bc4..30f59ec1 100644 --- a/tests/EmitTest.elm +++ b/tests/EmitTest.elm @@ -2,13 +2,13 @@ module EmitTest exposing (javascript) import Dict import Elm.AST.Typed as Typed exposing (Expr_(..), LocatedExpr) +import Elm.Compiler.Stage.Emit.JavaScript as JS import Elm.Data.Declaration exposing (Declaration, DeclarationBody(..)) import Elm.Data.Located as Located exposing (Located) import Elm.Data.ModuleName as ModuleName exposing (ModuleName) import Elm.Data.Type as Type import Elm.Data.VarName as VarName exposing (VarName) import Expect exposing (Expectation) -import Stage.Emit.JavaScript as JS import Test exposing (Test, describe, test, todo) import TestHelpers exposing diff --git a/tests/InferTypesFuzz.elm b/tests/InferTypesFuzz.elm index ebee1c57..746e53a3 100644 --- a/tests/InferTypesFuzz.elm +++ b/tests/InferTypesFuzz.elm @@ -4,6 +4,7 @@ import Elm.AST.Canonical as Canonical import Elm.AST.Canonical.Unwrapped as CanonicalU import Elm.AST.Typed as Typed import Elm.Compiler.Error exposing (TypeError(..)) +import Elm.Compiler.Stage.InferTypes as InferTypes import Elm.Data.Located as Located import Elm.Data.Type as Type exposing (Type) import Elm.Data.VarName as VarName exposing (VarName) @@ -13,7 +14,6 @@ import Random exposing (Generator) import Random.Extra as Random import Shrink exposing (Shrinker) import Shrink.Extra as Shrink -import Stage.InferTypes import Test exposing (Test, describe, fuzz, test) import TestHelpers exposing (dumpType) @@ -27,7 +27,7 @@ typeInference = \input -> input |> Canonical.fromUnwrapped - |> Stage.InferTypes.inferExpr + |> InferTypes.inferExpr |> Result.map Located.unwrap |> Result.map Tuple.second |> Expect.equal (Ok typeWanted) diff --git a/tests/InferTypesTest.elm b/tests/InferTypesTest.elm index cb8ab50a..dc668efd 100644 --- a/tests/InferTypesTest.elm +++ b/tests/InferTypesTest.elm @@ -4,6 +4,7 @@ import Elm.AST.Canonical as Canonical import Elm.AST.Canonical.Unwrapped as CanonicalU import Elm.AST.Typed as Typed import Elm.Compiler.Error as Error exposing (Error(..), TypeError(..)) +import Elm.Compiler.Stage.InferTypes as InferTypes import Elm.Data.Located as Located import Elm.Data.ModuleName as ModuleName exposing (ModuleName) import Elm.Data.Type as Type exposing (Type(..)) @@ -11,7 +12,6 @@ import Elm.Data.Type.ToString as TypeToString import Elm.Data.VarName as VarName exposing (VarName) import Expect exposing (Expectation) import Fuzz exposing (Fuzzer) -import Stage.InferTypes import Test exposing (Test, describe, fuzz, test) import TestHelpers exposing (dumpType, located) @@ -30,7 +30,7 @@ typeInference = \() -> input |> Canonical.fromUnwrapped - |> Stage.InferTypes.inferExpr + |> InferTypes.inferExpr |> Result.map Typed.getType |> Expect.equal output in diff --git a/tests/OptimizeTest.elm b/tests/OptimizeTest.elm index d2aaa0e5..413155e8 100644 --- a/tests/OptimizeTest.elm +++ b/tests/OptimizeTest.elm @@ -1,13 +1,13 @@ module OptimizeTest exposing (optimize) import Elm.AST.Typed as Typed exposing (Expr_(..)) +import Elm.Compiler.Stage.Optimize as Optimize import Elm.Data.Declaration exposing (Declaration) import Elm.Data.Located as Located exposing (Located) import Elm.Data.ModuleName as ModuleName exposing (ModuleName) import Elm.Data.Type as Type import Elm.Data.VarName as VarName exposing (VarName) import Expect exposing (Expectation) -import Stage.Optimize import Test exposing (Test, describe, test, todo) import TestHelpers exposing @@ -27,7 +27,7 @@ optimize = test description <| \() -> input - |> Stage.Optimize.optimizeExpr + |> Optimize.optimizeExpr |> Expect.equal output in describe "optimizeExpr" diff --git a/tests/ParserTest.elm b/tests/ParserTest.elm index cf2b1402..291fdddf 100644 --- a/tests/ParserTest.elm +++ b/tests/ParserTest.elm @@ -10,6 +10,7 @@ import Dict import Elm.AST.Frontend as Frontend import Elm.AST.Frontend.Unwrapped exposing (Expr(..)) import Elm.Compiler.Error exposing (ParseContext, ParseProblem) +import Elm.Compiler.Stage.Parse.Parser as Parser import Elm.Data.Exposing exposing (ExposedItem(..), Exposing(..)) import Elm.Data.Module exposing (ModuleType(..)) import Elm.Data.ModuleName as ModuleName exposing (ModuleName) @@ -17,7 +18,6 @@ import Elm.Data.VarName as VarName exposing (VarName) import Expect exposing (Expectation) import Parser.Advanced as P import Result.Extra -import Stage.Parse.Parser import Test exposing (Test, describe, test) @@ -28,7 +28,7 @@ moduleDeclaration = test description <| \() -> input - |> P.run Stage.Parse.Parser.moduleDeclaration + |> P.run Parser.moduleDeclaration |> Result.toMaybe |> Expect.equal output in @@ -99,7 +99,7 @@ exposingList = test description <| \() -> input - |> P.run Stage.Parse.Parser.exposingList + |> P.run Parser.exposingList |> Result.toMaybe |> Expect.equal output in @@ -209,7 +209,7 @@ imports = test description <| \() -> input - |> P.run Stage.Parse.Parser.imports + |> P.run Parser.imports |> Result.toMaybe |> Expect.equal output in @@ -366,7 +366,7 @@ moduleName = test description <| \() -> input - |> P.run Stage.Parse.Parser.moduleName + |> P.run Parser.moduleName |> Result.toMaybe |> Expect.equal output in @@ -426,7 +426,7 @@ expr = test description <| \() -> input - |> P.run Stage.Parse.Parser.expr + |> P.run Parser.expr |> Result.map Frontend.unwrap |> expectEqualParseResult input output in From b6de5693c5b9f390f5932bdd0c1a70bb5c6518c1 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 11 Oct 2019 01:52:33 +0200 Subject: [PATCH 4/5] Rename Emit.JS --- cli/Main.elm | 2 +- elm.json | 2 +- library-todo.txt | 6 +-- src/Elm/Compiler/Stage/Emit.elm | 38 +++++++++++++++---- .../Stage/Emit/{JavaScript.elm => JS.elm} | 14 +++++-- 5 files changed, 45 insertions(+), 17 deletions(-) rename src/Elm/Compiler/Stage/Emit/{JavaScript.elm => JS.elm} (92%) diff --git a/cli/Main.elm b/cli/Main.elm index 0b24a654..64ae1c67 100644 --- a/cli/Main.elm +++ b/cli/Main.elm @@ -44,7 +44,7 @@ import Dict exposing (Dict) import Elm.AST.Frontend as Frontend import Elm.Compiler.Error as Error exposing (Error(..), ParseError(..)) import Elm.Compiler.Stage.Desugar as Desugar -import Elm.Compiler.Stage.Emit.JavaScript as EmitJS +import Elm.Compiler.Stage.Emit.JS as EmitJS import Elm.Compiler.Stage.InferTypes as InferTypes import Elm.Compiler.Stage.Optimize as Optimize import Elm.Compiler.Stage.Parse as Parse diff --git a/elm.json b/elm.json index 131541d3..1b370bfe 100644 --- a/elm.json +++ b/elm.json @@ -8,7 +8,7 @@ "Elm.Compiler", "Elm.Compiler.Error", "Elm.Compiler.Stage.Emit", - "Elm.Compiler.Stage.Emit.JavaScript", + "Elm.Compiler.Stage.Emit.JS", "Elm.Data.Binding", "Elm.Data.Declaration", "Elm.Data.Exposing", diff --git a/library-todo.txt b/library-todo.txt index e9e6e8e2..cce76010 100644 --- a/library-todo.txt +++ b/library-todo.txt @@ -1,7 +1,7 @@ - [ ] Elm.Compiler function type annotations don't really show the transitions between AST modules. (It all looks like LocatedExpr -> LocatedExpr). Try aliasing the types just for the sake of this module. - [ ] desugarExpr is needlessly complicated... try creating some dummy throwaway module for that one expr? Does that change behaviour ("can't find variable x")? -- [ ] Emit helpers -- [ ] Emit.JavaScript -- [ ] Elm.Compiler.compileToJS - do the whole pipeline with default settings. Show how it's written in doc comment. Also show it in library README. - [ ] maybe show the Ellie demo/playground in library README? + +- [ ] Elm.Compiler.compileToJS - do the whole pipeline with default settings. Show how it's written in doc comment. Also show it in library README. + diff --git a/src/Elm/Compiler/Stage/Emit.elm b/src/Elm/Compiler/Stage/Emit.elm index bfaa1b53..cc1b935a 100644 --- a/src/Elm/Compiler/Stage/Emit.elm +++ b/src/Elm/Compiler/Stage/Emit.elm @@ -22,12 +22,12 @@ There are two emit usecases we know of: There _might_ be usecases for not eliminating dead code? We don't do that currently because we don't know of any, but if there are, please raise an issue -in the GitHub repo! - -TODO we'll probably have to detect cycles and do something like IIFE +in the [GitHub repo](https://github.com/elm-in-elm/compiler) or on [Discord](https://is.gd/elmdiscord)! -} +-- TODO we'll probably have to detect cycles and do something like IIFE + import AssocList import AssocSet import Dict exposing (Dict) @@ -46,13 +46,31 @@ import Result.Extra as Result import Set exposing (Set) -projectToDeclarationList : Project Typed.ProjectFields -> Result EmitError (List (Declaration Typed.LocatedExpr)) +{-| Find the shortest path through the declarations to `main`. + +Return the ordered list of these dependencies (`main` goes last). + +Automatically removes unused top-level declarations. + +-} +projectToDeclarationList : + Project Typed.ProjectFields + -> Result EmitError (List (Declaration Typed.LocatedExpr)) projectToDeclarationList { mainModuleName, modules } = modulesToGraph mainModuleName modules |> Result.map (findPathToMain mainModuleName) -modulesToDeclarationLists : Project Typed.ProjectFields -> Result EmitError (Dict ModuleName (List (Declaration Typed.LocatedExpr))) +{-| Find the shortest path through the declarations to all the exposed declarations. + +Return the ordered list of these dependencies (`main` goes last). + +Automatically removes unused top-level declarations. + +-} +modulesToDeclarationLists : + Project Typed.ProjectFields + -> Result EmitError (Dict ModuleName (List (Declaration Typed.LocatedExpr))) modulesToDeclarationLists ({ mainModuleName, modules } as project) = modulesToGraph mainModuleName modules |> Result.map (findPathForEachModule project) @@ -72,9 +90,13 @@ findPathToMain mainModuleName programGraph = (Set.singleton ( mainModuleName, "main" )) -{-| In this case we don't have `main`s but TODO finish writing this +{-| In this case we don't have `main`s but we'll use the exposed declarations in +each of the modules. -} -findPathForEachModule : Project Typed.ProjectFields -> Graph -> Dict ModuleName (List (Declaration Typed.LocatedExpr)) +findPathForEachModule : + Project Typed.ProjectFields + -> Graph + -> Dict ModuleName (List (Declaration Typed.LocatedExpr)) findPathForEachModule project graph = let exposedDeclarations : Set ( ModuleName, VarName ) @@ -117,7 +139,7 @@ findPathForEachModule project graph = {-| Generic function to find a good ordering of values (so that all the -dependencies are emitted before the TODO finish writing this +dependencies are emitted before the `startingDeclarations`). -} findPath : Graph -> Set ( ModuleName, VarName ) -> List (Declaration Typed.LocatedExpr) findPath graph startingDeclarations = diff --git a/src/Elm/Compiler/Stage/Emit/JavaScript.elm b/src/Elm/Compiler/Stage/Emit/JS.elm similarity index 92% rename from src/Elm/Compiler/Stage/Emit/JavaScript.elm rename to src/Elm/Compiler/Stage/Emit/JS.elm index 850d4352..8e254c76 100644 --- a/src/Elm/Compiler/Stage/Emit/JavaScript.elm +++ b/src/Elm/Compiler/Stage/Emit/JS.elm @@ -1,17 +1,19 @@ -module Elm.Compiler.Stage.Emit.JavaScript exposing +module Elm.Compiler.Stage.Emit.JS exposing ( emitProject , emitDeclaration, emitExpr ) -{-| The `emitProject` function is the main entrypoint in this module, ie. every +{-| Emit JavaScript code from the [typed AST](Elm.AST.Typed). + +The `emitProject` function is the main entrypoint in this module, ie. every `Stage.Emit.` module has to expose this function to fit -well with the APIs of the other stages. See src/cli/Main.elm and its `compile` +well with the APIs of the other stages. See `src/cli/Main.elm` and its `compile` function for example usage. @docs emitProject All the other exposed functions are (as of time of writing) exposed only for -testing purposes. +testing purposes: @docs emitDeclaration, emitExpr @@ -33,6 +35,8 @@ type alias ProjectFields = { declarationList : List (Declaration Typed.LocatedExpr) } +{-| Return a dict with a single `out.js` file with the compiled JavaScript code. +-} emitProject : Project Typed.ProjectFields -> Result Error (Dict FilePath FileContents) emitProject project = Ok project @@ -63,6 +67,7 @@ emitProject_ { declarationList } = |> Dict.singleton "out.js" +{-| -} emitExpr : Typed.LocatedExpr -> String emitExpr located = case Typed.getExpr located of @@ -136,6 +141,7 @@ emitExpr located = "[" ++ emitExpr e1 ++ "," ++ emitExpr e2 ++ "," ++ emitExpr e3 ++ "]" +{-| -} emitDeclaration : Declaration Typed.LocatedExpr -> String emitDeclaration { module_, name, body } = case body of From 2ce3fc0f587cefebe544a6572e4d71d92ff2eb99 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 11 Oct 2019 02:13:36 +0200 Subject: [PATCH 5/5] WIP --- src/Elm/Compiler.elm | 58 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/src/Elm/Compiler.elm b/src/Elm/Compiler.elm index 1c9f34c5..05c9cfd2 100644 --- a/src/Elm/Compiler.elm +++ b/src/Elm/Compiler.elm @@ -1,5 +1,6 @@ module Elm.Compiler exposing - ( parseExpr, parseModule, parseModules, parseImport, parseDeclaration + ( compileModuleToJS, compileModulesToJS + , parseExpr, parseModule, parseModules, parseImport, parseDeclaration , desugarExpr, desugarModule, desugarModules, desugarOnlyModule , inferExpr, inferModule, inferModules , defaultOptimizations @@ -13,11 +14,19 @@ module Elm.Compiler exposing > annotations, we're forced to use aliases for the various `Elm.AST.*` types. > So when you see `FrontendLocatedExpr`, look at [`Elm.AST.Frontend.LocatedExpr`](Elm.AST.Frontend#LocatedExpr). -The compiler phases in general look like this: +The compiler stages in general look like this: ![Stages of the compiler](https://github.com/elm-in-elm/compiler/raw/master/assets/stages.png) +# Shortcut through the whole pipeline + +A shortcut from the input Elm code to the output JavaScript code. Going through +all the stages with default settings. + +@docs compileModuleToJS, compileModulesToJS + + # Parsing **Useful eg. for tools like `elm-format`** that convert back to the Elm @@ -30,8 +39,8 @@ to \a -> \b -> \c -> a + b + c -That transformation is one of the things the Desugar phase does. So tools like -`elm-format` probably don't want to touch that phase, and will only want to parse! +That transformation is one of the things the Desugar stage does. So tools like +`elm-format` probably don't want to touch that stage, and will only want to parse! @docs parseExpr, parseModule, parseModules, parseImport, parseDeclaration @@ -39,7 +48,7 @@ That transformation is one of the things the Desugar phase does. So tools like # Desugaring After we parse the source code from a `String` to the AST, we desugar it - -simplify the AST type as much as possible to make later phases simpler and easier. +simplify the AST type as much as possible to make later stages simpler and easier. The best example to illustrate this (it doesn't actually happen though!) is `let` vs `where`. Imagine if Elm allowed for `where` constructs in its syntax, @@ -51,7 +60,7 @@ like Haskell does: y = x + 2 Then we'd like to convert these to `let` constructs (or the other way round) -as soon as possible, so that the other phases don't need to handle two +as soon as possible, so that the other stages don't need to handle two almost identical scenarios all over the place. Examples of real desugarings include: @@ -117,7 +126,7 @@ that they take turns on the expression. If you want to typecheck the code but then don't do anything with the types afterwards, you can drop them from the expressions you have. This is essentially -a move backwards in the compiler phases: +a move backwards in the compiler stages: ![Stages of the compiler](https://github.com/elm-in-elm/compiler/raw/master/assets/stages.png) @@ -141,6 +150,7 @@ import Elm.Compiler.Error ) import Elm.Compiler.Stage.Desugar as Desugar import Elm.Compiler.Stage.Desugar.Boilerplate as DesugarB +import Elm.Compiler.Stage.Emit.JS as EmitJS import Elm.Compiler.Stage.InferTypes as InferTypes import Elm.Compiler.Stage.InferTypes.Boilerplate as InferTypesB import Elm.Compiler.Stage.Optimize as Optimize @@ -158,6 +168,40 @@ import Result.Extra as Result +-- SHORTCUT + + +{-| TODO +-} +compileModuleToJS : { filePath : FilePath, sourceCode : FileContents } -> Result Error String +compileModuleToJS file = + let + toProject : Module Typed.LocatedExpr -> Project Typed.ProjectFields + toProject typedModule = + {} + in + file + |> parseModule + |> Result.andThen desugarModule + |> Result.andThen inferModule + |> Result.map optimizeModule + |> Result.map toProject + |> Result.andThen EmitJS.emitProject + + +{-| TODO +-} +compileModulesToJS : List { filePath : FilePath, sourceCode : FileContents } -> Result Error String +compileModulesToJS files = + files + |> parseModules + |> Result.andThen desugarModules + |> Result.andThen inferModules + |> Result.map optimizeModules + |> Result.andThen EmitJS.emitProject + + + -- PARSING