Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 43 additions & 29 deletions cli/gen-package/elm.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
{
"type": "application",
"source-directories": ["src", "codegen", "../../src"],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"Chadtech/elm-bool-extra": "2.4.2",
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/project-metadata-utils": "1.0.2",
"elm-community/maybe-extra": "5.2.0",
"rtfeldman/elm-hex": "1.0.0",
"stil4m/elm-syntax": "7.2.4",
"the-sett/elm-pretty-printer": "3.0.0"
"type": "application",
"source-directories": [
"src",
"codegen",
"../../src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"Chadtech/elm-bool-extra": "2.4.2",
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/project-metadata-utils": "1.0.2",
"elm-community/maybe-extra": "5.2.0",
"rtfeldman/elm-hex": "1.0.0",
"stil4m/elm-syntax": "7.2.4",
"the-sett/elm-pretty-printer": "3.0.0"
},
"indirect": {
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/basics-extra": "4.1.0",
"elm-community/list-extra": "8.3.0",
"miniBill/elm-unicode": "1.0.2",
"stil4m/structured-writer": "1.0.3"
}
},
"indirect": {
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/basics-extra": "4.1.0",
"elm-community/list-extra": "8.3.0",
"miniBill/elm-unicode": "1.0.2",
"stil4m/structured-writer": "1.0.3"
"test-dependencies": {
"direct": {
"elm-explorations/test": "2.2.0",
"miniBill/elm-diff": "1.1.0",
"wolfadex/elm-ansi": "3.0.0"
},
"indirect": {
"avh4/elm-color": "1.0.0",
"elm/bytes": "1.0.8",
"elm/random": "1.0.0",
"elm/regex": "1.0.0",
"elmcraft/core-extra": "2.2.0"
}
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
140 changes: 81 additions & 59 deletions cli/gen-package/src/Generate.elm
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
module Generate exposing (main)
module Generate exposing (main, moduleToFile)

{-| -}

import DocsFromSource
import Elm
import Elm.Annotation as Annotation
import Elm.Arg
import Elm.Op
import Elm.Docs
import Elm.Gen
import Elm.Op
import Elm.Syntax.TypeAnnotation
import Elm.Type
import Gen.Elm
import Gen.Elm.Annotation as GenType
import Gen.Elm.Arg
import Gen.Elm.Case
import Gen.List
import Gen.Tuple
import Internal.Compiler as Compiler
import Internal.Format as Format
import Internal.Write as Write
import Json.Decode as Json
import Gen.Elm.Arg


main : Program Json.Value () ()
main =
Expand Down Expand Up @@ -129,7 +130,7 @@ moduleToFile docs =
in
Elm.fileWith modName
{ docs =
"# Generated bindings for " ++ String.join "." sourceModName
"# Generated bindings for " ++ String.join "." sourceModName
, aliases =
[ ( [ "Elm", "Annotation" ], "Type" )
]
Expand Down Expand Up @@ -307,21 +308,21 @@ block2Case thisModule union =
(Elm.fn2
(Elm.Arg.var (union.name ++ "Expression"))
(Elm.Arg.varWith (union.name ++ "Tags")
(Annotation.record
(List.map
(\(tagname, subtypes) ->
( tagname
, Annotation.function
(List.map
(\_ -> Gen.Elm.annotation_.expression)
subtypes
(Annotation.record
(List.map
(\( tagname, subtypes ) ->
( tagname
, Annotation.function
(List.map
(\_ -> Gen.Elm.annotation_.expression)
subtypes
)
Gen.Elm.annotation_.expression
)
)
Gen.Elm.annotation_.expression
union.tags
)
)
union.tags
)
)
)
(\express tagRecord ->
Gen.Elm.Case.custom express
Expand All @@ -333,41 +334,40 @@ block2Case thisModule union =
)
)


toBranch : List String -> Elm.Expression -> ( String, List Elm.Type.Type ) -> Maybe Elm.Expression
toBranch thisModule tagRecord ( tagname, subtypes ) =
let
moduleName =
Elm.list (List.map Elm.string thisModule)

extractSubTypes i subs exp =
case subs of
[] ->
exp

(subtype :: remain) ->
let

subtypeName =
case typeToName subtype of
Nothing -> "arg_" ++ (String.fromInt i)

Just name -> name

newExp =
exp
|> Elm.Op.pipe
(Elm.apply Gen.Elm.Arg.values_.item
[ Gen.Elm.Arg.varWith
(Format.formatValue subtypeName)
(typeToExpression thisModule subtype)
]
)
in
extractSubTypes (i + 1) remain newExp
case subs of
[] ->
exp

subtype :: remain ->
let
subtypeName =
case typeToName subtype of
Nothing ->
"arg_" ++ String.fromInt i

Just name ->
name

newExp =
exp
|> Elm.Op.pipe
(Elm.apply Gen.Elm.Arg.values_.item
[ Gen.Elm.Arg.varWith
(Format.formatValue subtypeName)
(typeToExpression thisModule subtype)
]
)
in
extractSubTypes (i + 1) remain newExp
in
Gen.Elm.Case.call_.branch
(Gen.Elm.Arg.customType tagname (Elm.get tagname tagRecord)
|> extractSubTypes 0 subtypes
|> extractSubTypes 0 subtypes
)
basicsIdentity
|> Just
Expand All @@ -376,10 +376,10 @@ toBranch thisModule tagRecord ( tagname, subtypes ) =
basicsIdentity : Elm.Expression
basicsIdentity =
Elm.value
{ importFrom = [ "Basics" ]
, name = "identity"
, annotation = Just (Annotation.function [ Annotation.var "a" ] (Annotation.var "a"))
}
{ importFrom = [ "Basics" ]
, name = "identity"
, annotation = Just (Annotation.function [ Annotation.var "a" ] (Annotation.var "a"))
}


block2Maker : List String -> Elm.Docs.Block -> Maybe Elm.Expression
Expand Down Expand Up @@ -861,10 +861,10 @@ generateBlocks thisModule block =
]

_ ->
let
name =
Format.formatValue value.name
in
let
name =
Format.formatValue value.name
in
[ Elm.declaration name
(valueWith thisModule
value.name
Expand Down Expand Up @@ -1305,16 +1305,31 @@ typeToName elmType =
Elm.Type.Record fields maybeExtensible ->
maybeExtensible


isPrimitiveTypeName : String -> Bool
isPrimitiveTypeName name =
case name of
"List.List" -> True
"Basics.Bool" -> True
"Basics.Float" -> True
"Basics.Int" -> True
"String.String" -> True
"Char.Char" -> True
_ -> False
"List.List" ->
True

"Basics.Bool" ->
True

"Basics.Float" ->
True

"Basics.Int" ->
True

"String.String" ->
True

"Char.Char" ->
True

_ ->
False


typeToExpression : List String -> Elm.Type.Type -> Elm.Expression
typeToExpression thisModule elmType =
Expand Down Expand Up @@ -1429,6 +1444,13 @@ namedWithType thisModule name types =
"Sub"
(List.map (typeToExpression thisModule) types)

[ typeName ] ->
-- This is a type from the current module
GenType.namedWith
thisModule
typeName
(List.map (typeToExpression thisModule) types)

_ ->
let
fragsLength =
Expand Down
Loading
Loading