Skip to content
This repository was archived by the owner on Oct 5, 2024. It is now read-only.

[WIP] add record accessors support #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions src/Elm/AST/Canonical.elm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Expr
| Tuple LocatedExpr LocatedExpr
| Tuple3 LocatedExpr LocatedExpr LocatedExpr
| Record (Dict VarName (Binding LocatedExpr))
| RecordAccessor VarName


{-| Discard the [location metadata](Elm.Data.Located#Located).
Expand Down Expand Up @@ -159,6 +160,9 @@ unwrap expr =
(always (Binding.map unwrap))
bindings

RecordAccessor name ->
Unwrapped.RecordAccessor name


{-| Adds [**dummy** locations](Elm.Data.Located#dummyRegion) to the [Unwrapped.Expr](Elm.AST.Canonical.Unwrapped#Expr).
-}
Expand Down Expand Up @@ -248,3 +252,6 @@ fromUnwrapped expr =
Dict.map
(always (Binding.map fromUnwrapped))
bindings

Unwrapped.RecordAccessor name ->
RecordAccessor name
1 change: 1 addition & 0 deletions src/Elm/AST/Canonical/Unwrapped.elm
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ type Expr
| Tuple Expr Expr
| Tuple3 Expr Expr Expr
| Record (Dict VarName (Binding Expr))
| RecordAccessor VarName
7 changes: 7 additions & 0 deletions src/Elm/AST/Frontend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Expr
| Tuple LocatedExpr LocatedExpr
| Tuple3 LocatedExpr LocatedExpr LocatedExpr
| Record (List (Binding LocatedExpr))
| RecordAccessor VarName


{-| A helper for the [Transform](/packages/Janiczek/transform/latest/) library.
Expand Down Expand Up @@ -150,6 +151,9 @@ recurse f expr =
Record bindings ->
Record <| List.map (Binding.map f_) bindings

RecordAccessor _ ->
expr


{-| [Transform](/packages/Janiczek/transform/latest/Transform#transformAll)
the expression using the provided function.
Expand Down Expand Up @@ -256,3 +260,6 @@ unwrap expr =
Record bindings ->
Unwrapped.Record <|
List.map (Binding.map unwrap) bindings

RecordAccessor name ->
Unwrapped.RecordAccessor name
1 change: 1 addition & 0 deletions src/Elm/AST/Frontend/Unwrapped.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ type Expr
| Tuple Expr Expr
| Tuple3 Expr Expr Expr
| Record (List (Binding Expr))
| RecordAccessor VarName
13 changes: 13 additions & 0 deletions src/Elm/AST/Typed.elm
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Expr_
| Tuple LocatedExpr LocatedExpr
| Tuple3 LocatedExpr LocatedExpr LocatedExpr
| Record (Dict VarName (Binding LocatedExpr))
| RecordAccessor VarName


{-| A helper for the [Transform](/packages/Janiczek/transform/latest/) library.
Expand Down Expand Up @@ -165,6 +166,9 @@ recurse fn locatedExpr =
(always (Binding.map fn))
bindings
)

RecordAccessor name ->
expr
)


Expand Down Expand Up @@ -256,6 +260,9 @@ recursiveChildren fn locatedExpr =
Record bindings ->
List.concatMap (.body >> fn) (Dict.values bindings)

RecordAccessor _ ->
[]


mapExpr : (Expr_ -> Expr_) -> LocatedExpr -> LocatedExpr
mapExpr fn locatedExpr =
Expand Down Expand Up @@ -375,6 +382,9 @@ unwrap expr =
Dict.map
(always (Binding.map unwrap))
bindings

RecordAccessor name ->
Unwrapped.RecordAccessor name
, type_
)

Expand Down Expand Up @@ -463,4 +473,7 @@ dropTypes locatedExpr =
Record bindings ->
Canonical.Record <|
Dict.map (always (Binding.map dropTypes)) bindings

RecordAccessor name ->
Canonical.RecordAccessor name
)
1 change: 1 addition & 0 deletions src/Elm/AST/Typed/Unwrapped.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ type Expr_
| Tuple Expr Expr
| Tuple3 Expr Expr Expr
| Record (Dict VarName (Binding Expr))
| RecordAccessor VarName
1 change: 1 addition & 0 deletions src/Elm/Data/Type.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Type
| Tuple Type Type
| Tuple3 Type Type Type
| Record (Dict VarName Type)
| ExtensibleRecord ( VarName, Type ) (Dict VarName Type) -- An extensible record has at least a binding
| {- The actual definitions of type aliases and custom types are elsewhere
(in the Declaration module), this is just a "pointer", "var".

Expand Down
28 changes: 28 additions & 0 deletions src/Elm/Data/Type/ToString.elm
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ toString state type_ =
else
( "{ " ++ bindingsStr ++ " }", state1 )

ExtensibleRecord firstBinding otherBindings ->
let
( bindingsStr, state1 ) =
List.foldr
(\param ( acc, state2 ) ->
let
( string, state3 ) =
niceRecordBinding state2 param
in
( string :: acc, state3 )
)
( [], state )
(firstBinding :: Dict.toList otherBindings)
|> Tuple.mapFirst (String.join ", ")

( newName, newState ) =
getFreeName state1
in
( "{ " ++ newName ++ " | " ++ bindingsStr ++ " }", newState )


getName : State -> Int -> ( String, State )
getName ((State { counter, mapping }) as state) varId =
Expand All @@ -203,6 +223,11 @@ getName ((State { counter, mapping }) as state) varId =
)


getFreeName : State -> ( String, State )
getFreeName (State r) =
( niceVarName r.counter, State { r | counter = r.counter + 1 } )


{-| Function to get from a number to a nice type variable name.
It follows this sequence:

Expand Down Expand Up @@ -322,3 +347,6 @@ shouldWrapParens type_ =

Record _ ->
False

ExtensibleRecord _ _ ->
False
3 changes: 3 additions & 0 deletions src/Stage/Desugar.elm
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ desugarExpr modules thisModule located =
|> Canonical.Record
)

Frontend.RecordAccessor name ->
return <| Canonical.RecordAccessor name



-- HELPERS
Expand Down