Skip to content

Commit f1ecae9

Browse files
committed
WIP Allow for tuple with 3+ elements
1 parent 8eb8b31 commit f1ecae9

File tree

25 files changed

+236
-361
lines changed

25 files changed

+236
-361
lines changed

src/Compiler/AST/Canonical.elm

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type Expr_
112112
| Update Name Expr (Dict String (A.Located Name) FieldUpdate)
113113
| Record (Dict String (A.Located Name) Expr)
114114
| Unit
115-
| Tuple Expr Expr (Maybe Expr)
115+
| Tuple Expr Expr (List Expr)
116116
| Shader Shader.Source Shader.Types
117117

118118

@@ -153,7 +153,7 @@ type Pattern_
153153
| PRecord (List Name)
154154
| PAlias Pattern Name
155155
| PUnit
156-
| PTuple Pattern Pattern (Maybe Pattern)
156+
| PTuple Pattern Pattern (List Pattern)
157157
| PList (List Pattern)
158158
| PCons Pattern Pattern
159159
| PBool Union Bool
@@ -201,7 +201,7 @@ type Type
201201
| TType IO.Canonical Name (List Type)
202202
| TRecord (Dict String Name FieldType) (Maybe Name)
203203
| TUnit
204-
| TTuple Type Type (Maybe Type)
204+
| TTuple Type Type (List Type)
205205
| TAlias IO.Canonical Name (List ( Name, Type )) AliasType
206206

207207

@@ -396,12 +396,12 @@ typeEncoder type_ =
396396
[ ( "type", Encode.string "TUnit" )
397397
]
398398

399-
TTuple a b maybeC ->
399+
TTuple a b cs ->
400400
Encode.object
401401
[ ( "type", Encode.string "TTuple" )
402402
, ( "a", typeEncoder a )
403403
, ( "b", typeEncoder b )
404-
, ( "maybeC", E.maybe typeEncoder maybeC )
404+
, ( "cs", Encode.list typeEncoder cs )
405405
]
406406

407407
TAlias home name args tipe ->
@@ -447,7 +447,7 @@ typeDecoder =
447447
Decode.map3 TTuple
448448
(Decode.field "a" typeDecoder)
449449
(Decode.field "b" typeDecoder)
450-
(Decode.field "maybeC" (Decode.maybe typeDecoder))
450+
(Decode.field "cs" (Decode.list typeDecoder))
451451

452452
"TAlias" ->
453453
Decode.map4 TAlias
@@ -798,12 +798,12 @@ expr_Encoder expr_ =
798798
[ ( "type", Encode.string "Unit" )
799799
]
800800

801-
Tuple a b maybeC ->
801+
Tuple a b cs ->
802802
Encode.object
803803
[ ( "type", Encode.string "Tuple" )
804804
, ( "a", exprEncoder a )
805805
, ( "b", exprEncoder b )
806-
, ( "maybeC", E.maybe exprEncoder maybeC )
806+
, ( "cs", Encode.list exprEncoder cs )
807807
]
808808

809809
Shader src types ->
@@ -948,7 +948,7 @@ expr_Decoder =
948948
Decode.map3 Tuple
949949
(Decode.field "a" exprDecoder)
950950
(Decode.field "b" exprDecoder)
951-
(Decode.field "maybeC" (Decode.maybe exprDecoder))
951+
(Decode.field "cs" (Decode.list exprDecoder))
952952

953953
"Shader" ->
954954
Decode.map2 Shader
@@ -1002,12 +1002,12 @@ pattern_Encoder pattern_ =
10021002
[ ( "type", Encode.string "PUnit" )
10031003
]
10041004

1005-
PTuple pattern1 pattern2 maybePattern3 ->
1005+
PTuple pattern1 pattern2 patterns3 ->
10061006
Encode.object
10071007
[ ( "type", Encode.string "PTuple" )
10081008
, ( "pattern1", patternEncoder pattern1 )
10091009
, ( "pattern2", patternEncoder pattern2 )
1010-
, ( "pattern3", E.maybe patternEncoder maybePattern3 )
1010+
, ( "patterns3", Encode.list patternEncoder patterns3 )
10111011
]
10121012

10131013
PList patterns ->
@@ -1089,7 +1089,7 @@ pattern_Decoder =
10891089
Decode.map3 PTuple
10901090
(Decode.field "pattern1" patternDecoder)
10911091
(Decode.field "pattern2" patternDecoder)
1092-
(Decode.field "pattern3" (Decode.maybe patternDecoder))
1092+
(Decode.field "patterns3" (Decode.list patternDecoder))
10931093

10941094
"PList" ->
10951095
Decode.map PList

src/Compiler/AST/Optimized.elm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type Expr
7575
| Record (Dict String Name Expr)
7676
| TrackedRecord A.Region (Dict String (A.Located Name) Expr)
7777
| Unit
78-
| Tuple A.Region Expr Expr (Maybe Expr)
78+
| Tuple A.Region Expr Expr (List Expr)
7979
| Shader Shader.Source (EverySet String Name) (EverySet String Name)
8080

8181

@@ -701,13 +701,13 @@ exprEncoder expr =
701701
[ ( "type", Encode.string "Unit" )
702702
]
703703

704-
Tuple region a b maybeC ->
704+
Tuple region a b cs ->
705705
Encode.object
706706
[ ( "type", Encode.string "Tuple" )
707707
, ( "region", A.regionEncoder region )
708708
, ( "a", exprEncoder a )
709709
, ( "b", exprEncoder b )
710-
, ( "maybeC", E.maybe exprEncoder maybeC )
710+
, ( "cs", Encode.list exprEncoder cs )
711711
]
712712

713713
Shader src attributes uniforms ->
@@ -876,7 +876,7 @@ exprDecoder =
876876
(Decode.field "region" A.regionDecoder)
877877
(Decode.field "a" exprDecoder)
878878
(Decode.field "b" exprDecoder)
879-
(Decode.field "maybeC" (Decode.maybe exprDecoder))
879+
(Decode.field "cs" (Decode.list exprDecoder))
880880

881881
"Shader" ->
882882
Decode.map3 Shader

src/Compiler/AST/Utils/Type.elm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ dealiasHelp typeTable tipe =
6262
TUnit ->
6363
TUnit
6464

65-
TTuple a b maybeC ->
65+
TTuple a b cs ->
6666
TTuple
6767
(dealiasHelp typeTable a)
6868
(dealiasHelp typeTable b)
69-
(Maybe.map (dealiasHelp typeTable) maybeC)
69+
(List.map (dealiasHelp typeTable) cs)
7070

7171

7272
dealiasField : Dict String Name Type -> FieldType -> FieldType
@@ -99,8 +99,8 @@ deepDealias tipe =
9999
TUnit ->
100100
TUnit
101101

102-
TTuple a b c ->
103-
TTuple (deepDealias a) (deepDealias b) (Maybe.map deepDealias c)
102+
TTuple a b cs ->
103+
TTuple (deepDealias a) (deepDealias b) (List.map deepDealias cs)
104104

105105

106106
deepDealiasField : FieldType -> FieldType

src/Compiler/Canonicalize/Effects.elm

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,10 @@ checkPayload tipe =
232232
Can.TUnit ->
233233
Ok ()
234234

235-
Can.TTuple a b maybeC ->
235+
Can.TTuple a b cs ->
236236
checkPayload a
237237
|> Result.andThen (\_ -> checkPayload b)
238-
|> Result.andThen
239-
(\_ ->
240-
case maybeC of
241-
Nothing ->
242-
Ok ()
243-
244-
Just c ->
245-
checkPayload c
246-
)
238+
|> Result.andThen (\_ -> checkPayloadTupleCs cs)
247239

248240
Can.TVar name ->
249241
Err ( tipe, Error.TypeVariable name )
@@ -261,6 +253,17 @@ checkPayload tipe =
261253
fields
262254

263255

256+
checkPayloadTupleCs : List Can.Type -> Result ( Can.Type, Error.InvalidPayload ) ()
257+
checkPayloadTupleCs types =
258+
case types of
259+
[] ->
260+
Ok ()
261+
262+
tipe :: rest ->
263+
checkPayload tipe
264+
|> Result.andThen (\_ -> checkPayloadTupleCs rest)
265+
266+
264267
checkFieldPayload : Can.FieldType -> Result ( Can.Type, Error.InvalidPayload ) ()
265268
checkFieldPayload (Can.FieldType _ tipe) =
266269
checkPayload tipe

src/Compiler/Canonicalize/Expression.elm

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,15 @@ canonicalize env (A.At region expression) =
170170
R.pure Can.Tuple
171171
|> R.apply (canonicalize env a)
172172
|> R.apply (canonicalize env b)
173-
|> R.apply (canonicalizeTupleExtras region env cs)
173+
|> R.apply (canonicalizeTupleExtras env cs)
174174

175175
Src.Shader src tipe ->
176176
R.ok (Can.Shader src tipe)
177177

178178

179-
canonicalizeTupleExtras : A.Region -> Env.Env -> List Src.Expr -> EResult FreeLocals (List W.Warning) (Maybe Can.Expr)
180-
canonicalizeTupleExtras region env extras =
181-
case extras of
182-
[] ->
183-
R.ok Nothing
184-
185-
[ three ] ->
186-
R.fmap Just <| canonicalize env three
187-
188-
_ ->
189-
R.throw (Error.TupleLargerThanThree region)
179+
canonicalizeTupleExtras : Env.Env -> List Src.Expr -> EResult FreeLocals (List W.Warning) (List Can.Expr)
180+
canonicalizeTupleExtras env extras =
181+
R.traverse (canonicalize env) extras
190182

191183

192184

src/Compiler/Canonicalize/Pattern.elm

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ canonicalize env (A.At region pattern) =
8383
R.ok Can.PTuple
8484
|> R.apply (canonicalize env a)
8585
|> R.apply (canonicalize env b)
86-
|> R.apply (canonicalizeTuple region env cs)
86+
|> R.apply (canonicalizeTuple env cs)
8787

8888
Src.PCtor nameRegion name patterns ->
8989
Env.findCtor nameRegion env name
@@ -144,17 +144,9 @@ canonicalizeCtor env region name patterns ctor =
144144
R.throw (Error.PatternHasRecordCtor region name)
145145

146146

147-
canonicalizeTuple : A.Region -> Env.Env -> List Src.Pattern -> PResult DupsDict w (Maybe Can.Pattern)
148-
canonicalizeTuple tupleRegion env extras =
149-
case extras of
150-
[] ->
151-
R.ok Nothing
152-
153-
[ three ] ->
154-
R.fmap Just (canonicalize env three)
155-
156-
_ ->
157-
R.throw (Error.TupleLargerThanThree tupleRegion)
147+
canonicalizeTuple : Env.Env -> List Src.Pattern -> PResult DupsDict w (List Can.Pattern)
148+
canonicalizeTuple env =
149+
R.traverse (canonicalize env)
158150

159151

160152
canonicalizeList : Env.Env -> List Src.Pattern -> PResult DupsDict w (List Can.Pattern)

src/Compiler/Canonicalize/Type.elm

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Compiler.Canonicalize.Type exposing
44
, toAnnotation
55
)
66

7+
import Basics.Extra exposing (flip)
78
import Compiler.AST.Canonical as Can
89
import Compiler.AST.Source as Src
910
import Compiler.Canonicalize.Environment as Env
@@ -74,16 +75,8 @@ canonicalize env (A.At typeRegion tipe) =
7475
|> R.bind (\tTuple -> R.fmap tTuple (canonicalize env b))
7576
|> R.bind
7677
(\tTuple ->
77-
case cs of
78-
[] ->
79-
R.ok (tTuple Nothing)
80-
81-
[ c ] ->
82-
canonicalize env c
83-
|> R.fmap (tTuple << Just)
84-
85-
_ ->
86-
R.throw <| Error.TupleLargerThanThree typeRegion
78+
R.traverse (canonicalize env) cs
79+
|> R.fmap tTuple
8780
)
8881

8982

@@ -156,13 +149,8 @@ addFreeVars freeVars tipe =
156149
Can.TUnit ->
157150
freeVars
158151

159-
Can.TTuple a b maybeC ->
160-
case maybeC of
161-
Nothing ->
162-
addFreeVars (addFreeVars freeVars a) b
163-
164-
Just c ->
165-
addFreeVars (addFreeVars (addFreeVars freeVars a) b) c
152+
Can.TTuple a b cs ->
153+
List.foldl (flip addFreeVars) (addFreeVars (addFreeVars freeVars a) b) cs
166154

167155
Can.TAlias _ _ args _ ->
168156
List.foldl (\( _, arg ) fvs -> addFreeVars fvs arg) freeVars args

src/Compiler/Elm/Compiler/Type/Extract.elm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import Data.Map as Dict exposing (Dict)
2424
import Data.Set as EverySet exposing (EverySet)
2525
import Json.Decode as Decode
2626
import Json.Encode as Encode
27-
import Maybe.Extra as Maybe
2827
import System.TypeCheck.IO as IO
2928
import Utils.Main as Utils
3029

@@ -60,11 +59,11 @@ extract astType =
6059
Can.TUnit ->
6160
pure T.Unit
6261

63-
Can.TTuple a b maybeC ->
62+
Can.TTuple a b cs ->
6463
pure T.Tuple
6564
|> apply (extract a)
6665
|> apply (extract b)
67-
|> apply (traverse extract (Maybe.toList maybeC))
66+
|> apply (traverse extract cs)
6867

6968
Can.TAlias home name args aliasType ->
7069
addAlias (Opt.Global home name) ()

src/Compiler/Generate/JavaScript/Expression.elm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,22 +180,26 @@ generate mode parentModule expression =
180180
Mode.Prod _ ->
181181
JsExpr <| JS.ExprInt 0
182182

183-
Opt.Tuple _ a b maybeC ->
183+
Opt.Tuple _ a b cs ->
184184
JsExpr <|
185-
case maybeC of
186-
Nothing ->
185+
case cs of
186+
[] ->
187187
JS.ExprCall (JS.ExprRef (JsName.fromKernel Name.utils "Tuple2"))
188188
[ generateJsExpr mode parentModule a
189189
, generateJsExpr mode parentModule b
190190
]
191191

192-
Just c ->
192+
[ c ] ->
193193
JS.ExprCall (JS.ExprRef (JsName.fromKernel Name.utils "Tuple3"))
194194
[ generateJsExpr mode parentModule a
195195
, generateJsExpr mode parentModule b
196196
, generateJsExpr mode parentModule c
197197
]
198198

199+
_ ->
200+
JS.ExprCall (JS.ExprRef (JsName.fromKernel Name.utils "Tuple"))
201+
(List.map (generateJsExpr mode parentModule) (a :: b :: cs))
202+
199203
Opt.Shader src attributes uniforms ->
200204
let
201205
toTranlation : Name.Name -> ( JsName.Name, JS.Expr )

src/Compiler/Nitpick/Debug.elm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ hasDebug expression =
142142
Opt.Unit ->
143143
False
144144

145-
Opt.Tuple _ a b c ->
146-
hasDebug a || hasDebug b || Maybe.withDefault False (Maybe.map hasDebug c)
145+
Opt.Tuple _ a b cs ->
146+
hasDebug a || hasDebug b || List.any hasDebug cs
147147

148148
Opt.Shader _ _ _ ->
149149
False

0 commit comments

Comments
 (0)