Skip to content

Commit

Permalink
WIP Allow for tuple with 3+ elements
Browse files Browse the repository at this point in the history
  • Loading branch information
decioferreira committed Feb 11, 2025
1 parent 8eb8b31 commit f1ecae9
Show file tree
Hide file tree
Showing 25 changed files with 236 additions and 361 deletions.
24 changes: 12 additions & 12 deletions src/Compiler/AST/Canonical.elm
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type Expr_
| Update Name Expr (Dict String (A.Located Name) FieldUpdate)
| Record (Dict String (A.Located Name) Expr)
| Unit
| Tuple Expr Expr (Maybe Expr)
| Tuple Expr Expr (List Expr)
| Shader Shader.Source Shader.Types


Expand Down Expand Up @@ -153,7 +153,7 @@ type Pattern_
| PRecord (List Name)
| PAlias Pattern Name
| PUnit
| PTuple Pattern Pattern (Maybe Pattern)
| PTuple Pattern Pattern (List Pattern)
| PList (List Pattern)
| PCons Pattern Pattern
| PBool Union Bool
Expand Down Expand Up @@ -201,7 +201,7 @@ type Type
| TType IO.Canonical Name (List Type)
| TRecord (Dict String Name FieldType) (Maybe Name)
| TUnit
| TTuple Type Type (Maybe Type)
| TTuple Type Type (List Type)
| TAlias IO.Canonical Name (List ( Name, Type )) AliasType


Expand Down Expand Up @@ -396,12 +396,12 @@ typeEncoder type_ =
[ ( "type", Encode.string "TUnit" )
]

TTuple a b maybeC ->
TTuple a b cs ->
Encode.object
[ ( "type", Encode.string "TTuple" )
, ( "a", typeEncoder a )
, ( "b", typeEncoder b )
, ( "maybeC", E.maybe typeEncoder maybeC )
, ( "cs", Encode.list typeEncoder cs )
]

TAlias home name args tipe ->
Expand Down Expand Up @@ -447,7 +447,7 @@ typeDecoder =
Decode.map3 TTuple
(Decode.field "a" typeDecoder)
(Decode.field "b" typeDecoder)
(Decode.field "maybeC" (Decode.maybe typeDecoder))
(Decode.field "cs" (Decode.list typeDecoder))

"TAlias" ->
Decode.map4 TAlias
Expand Down Expand Up @@ -798,12 +798,12 @@ expr_Encoder expr_ =
[ ( "type", Encode.string "Unit" )
]

Tuple a b maybeC ->
Tuple a b cs ->
Encode.object
[ ( "type", Encode.string "Tuple" )
, ( "a", exprEncoder a )
, ( "b", exprEncoder b )
, ( "maybeC", E.maybe exprEncoder maybeC )
, ( "cs", Encode.list exprEncoder cs )
]

Shader src types ->
Expand Down Expand Up @@ -948,7 +948,7 @@ expr_Decoder =
Decode.map3 Tuple
(Decode.field "a" exprDecoder)
(Decode.field "b" exprDecoder)
(Decode.field "maybeC" (Decode.maybe exprDecoder))
(Decode.field "cs" (Decode.list exprDecoder))

"Shader" ->
Decode.map2 Shader
Expand Down Expand Up @@ -1002,12 +1002,12 @@ pattern_Encoder pattern_ =
[ ( "type", Encode.string "PUnit" )
]

PTuple pattern1 pattern2 maybePattern3 ->
PTuple pattern1 pattern2 patterns3 ->
Encode.object
[ ( "type", Encode.string "PTuple" )
, ( "pattern1", patternEncoder pattern1 )
, ( "pattern2", patternEncoder pattern2 )
, ( "pattern3", E.maybe patternEncoder maybePattern3 )
, ( "patterns3", Encode.list patternEncoder patterns3 )
]

PList patterns ->
Expand Down Expand Up @@ -1089,7 +1089,7 @@ pattern_Decoder =
Decode.map3 PTuple
(Decode.field "pattern1" patternDecoder)
(Decode.field "pattern2" patternDecoder)
(Decode.field "pattern3" (Decode.maybe patternDecoder))
(Decode.field "patterns3" (Decode.list patternDecoder))

"PList" ->
Decode.map PList
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/AST/Optimized.elm
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Expr
| Record (Dict String Name Expr)
| TrackedRecord A.Region (Dict String (A.Located Name) Expr)
| Unit
| Tuple A.Region Expr Expr (Maybe Expr)
| Tuple A.Region Expr Expr (List Expr)
| Shader Shader.Source (EverySet String Name) (EverySet String Name)


Expand Down Expand Up @@ -701,13 +701,13 @@ exprEncoder expr =
[ ( "type", Encode.string "Unit" )
]

Tuple region a b maybeC ->
Tuple region a b cs ->
Encode.object
[ ( "type", Encode.string "Tuple" )
, ( "region", A.regionEncoder region )
, ( "a", exprEncoder a )
, ( "b", exprEncoder b )
, ( "maybeC", E.maybe exprEncoder maybeC )
, ( "cs", Encode.list exprEncoder cs )
]

Shader src attributes uniforms ->
Expand Down Expand Up @@ -876,7 +876,7 @@ exprDecoder =
(Decode.field "region" A.regionDecoder)
(Decode.field "a" exprDecoder)
(Decode.field "b" exprDecoder)
(Decode.field "maybeC" (Decode.maybe exprDecoder))
(Decode.field "cs" (Decode.list exprDecoder))

"Shader" ->
Decode.map3 Shader
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/AST/Utils/Type.elm
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ dealiasHelp typeTable tipe =
TUnit ->
TUnit

TTuple a b maybeC ->
TTuple a b cs ->
TTuple
(dealiasHelp typeTable a)
(dealiasHelp typeTable b)
(Maybe.map (dealiasHelp typeTable) maybeC)
(List.map (dealiasHelp typeTable) cs)


dealiasField : Dict String Name Type -> FieldType -> FieldType
Expand Down Expand Up @@ -99,8 +99,8 @@ deepDealias tipe =
TUnit ->
TUnit

TTuple a b c ->
TTuple (deepDealias a) (deepDealias b) (Maybe.map deepDealias c)
TTuple a b cs ->
TTuple (deepDealias a) (deepDealias b) (List.map deepDealias cs)


deepDealiasField : FieldType -> FieldType
Expand Down
23 changes: 13 additions & 10 deletions src/Compiler/Canonicalize/Effects.elm
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,10 @@ checkPayload tipe =
Can.TUnit ->
Ok ()

Can.TTuple a b maybeC ->
Can.TTuple a b cs ->
checkPayload a
|> Result.andThen (\_ -> checkPayload b)
|> Result.andThen
(\_ ->
case maybeC of
Nothing ->
Ok ()

Just c ->
checkPayload c
)
|> Result.andThen (\_ -> checkPayloadTupleCs cs)

Can.TVar name ->
Err ( tipe, Error.TypeVariable name )
Expand All @@ -261,6 +253,17 @@ checkPayload tipe =
fields


checkPayloadTupleCs : List Can.Type -> Result ( Can.Type, Error.InvalidPayload ) ()
checkPayloadTupleCs types =
case types of
[] ->
Ok ()

tipe :: rest ->
checkPayload tipe
|> Result.andThen (\_ -> checkPayloadTupleCs rest)


checkFieldPayload : Can.FieldType -> Result ( Can.Type, Error.InvalidPayload ) ()
checkFieldPayload (Can.FieldType _ tipe) =
checkPayload tipe
Expand Down
16 changes: 4 additions & 12 deletions src/Compiler/Canonicalize/Expression.elm
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,15 @@ canonicalize env (A.At region expression) =
R.pure Can.Tuple
|> R.apply (canonicalize env a)
|> R.apply (canonicalize env b)
|> R.apply (canonicalizeTupleExtras region env cs)
|> R.apply (canonicalizeTupleExtras env cs)

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


canonicalizeTupleExtras : A.Region -> Env.Env -> List Src.Expr -> EResult FreeLocals (List W.Warning) (Maybe Can.Expr)
canonicalizeTupleExtras region env extras =
case extras of
[] ->
R.ok Nothing

[ three ] ->
R.fmap Just <| canonicalize env three

_ ->
R.throw (Error.TupleLargerThanThree region)
canonicalizeTupleExtras : Env.Env -> List Src.Expr -> EResult FreeLocals (List W.Warning) (List Can.Expr)
canonicalizeTupleExtras env extras =
R.traverse (canonicalize env) extras



Expand Down
16 changes: 4 additions & 12 deletions src/Compiler/Canonicalize/Pattern.elm
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ canonicalize env (A.At region pattern) =
R.ok Can.PTuple
|> R.apply (canonicalize env a)
|> R.apply (canonicalize env b)
|> R.apply (canonicalizeTuple region env cs)
|> R.apply (canonicalizeTuple env cs)

Src.PCtor nameRegion name patterns ->
Env.findCtor nameRegion env name
Expand Down Expand Up @@ -144,17 +144,9 @@ canonicalizeCtor env region name patterns ctor =
R.throw (Error.PatternHasRecordCtor region name)


canonicalizeTuple : A.Region -> Env.Env -> List Src.Pattern -> PResult DupsDict w (Maybe Can.Pattern)
canonicalizeTuple tupleRegion env extras =
case extras of
[] ->
R.ok Nothing

[ three ] ->
R.fmap Just (canonicalize env three)

_ ->
R.throw (Error.TupleLargerThanThree tupleRegion)
canonicalizeTuple : Env.Env -> List Src.Pattern -> PResult DupsDict w (List Can.Pattern)
canonicalizeTuple env =
R.traverse (canonicalize env)


canonicalizeList : Env.Env -> List Src.Pattern -> PResult DupsDict w (List Can.Pattern)
Expand Down
22 changes: 5 additions & 17 deletions src/Compiler/Canonicalize/Type.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Compiler.Canonicalize.Type exposing
, toAnnotation
)

import Basics.Extra exposing (flip)
import Compiler.AST.Canonical as Can
import Compiler.AST.Source as Src
import Compiler.Canonicalize.Environment as Env
Expand Down Expand Up @@ -74,16 +75,8 @@ canonicalize env (A.At typeRegion tipe) =
|> R.bind (\tTuple -> R.fmap tTuple (canonicalize env b))
|> R.bind
(\tTuple ->
case cs of
[] ->
R.ok (tTuple Nothing)

[ c ] ->
canonicalize env c
|> R.fmap (tTuple << Just)

_ ->
R.throw <| Error.TupleLargerThanThree typeRegion
R.traverse (canonicalize env) cs
|> R.fmap tTuple
)


Expand Down Expand Up @@ -156,13 +149,8 @@ addFreeVars freeVars tipe =
Can.TUnit ->
freeVars

Can.TTuple a b maybeC ->
case maybeC of
Nothing ->
addFreeVars (addFreeVars freeVars a) b

Just c ->
addFreeVars (addFreeVars (addFreeVars freeVars a) b) c
Can.TTuple a b cs ->
List.foldl (flip addFreeVars) (addFreeVars (addFreeVars freeVars a) b) cs

Can.TAlias _ _ args _ ->
List.foldl (\( _, arg ) fvs -> addFreeVars fvs arg) freeVars args
Expand Down
5 changes: 2 additions & 3 deletions src/Compiler/Elm/Compiler/Type/Extract.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import Data.Map as Dict exposing (Dict)
import Data.Set as EverySet exposing (EverySet)
import Json.Decode as Decode
import Json.Encode as Encode
import Maybe.Extra as Maybe
import System.TypeCheck.IO as IO
import Utils.Main as Utils

Expand Down Expand Up @@ -60,11 +59,11 @@ extract astType =
Can.TUnit ->
pure T.Unit

Can.TTuple a b maybeC ->
Can.TTuple a b cs ->
pure T.Tuple
|> apply (extract a)
|> apply (extract b)
|> apply (traverse extract (Maybe.toList maybeC))
|> apply (traverse extract cs)

Can.TAlias home name args aliasType ->
addAlias (Opt.Global home name) ()
Expand Down
12 changes: 8 additions & 4 deletions src/Compiler/Generate/JavaScript/Expression.elm
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,26 @@ generate mode parentModule expression =
Mode.Prod _ ->
JsExpr <| JS.ExprInt 0

Opt.Tuple _ a b maybeC ->
Opt.Tuple _ a b cs ->
JsExpr <|
case maybeC of
Nothing ->
case cs of
[] ->
JS.ExprCall (JS.ExprRef (JsName.fromKernel Name.utils "Tuple2"))
[ generateJsExpr mode parentModule a
, generateJsExpr mode parentModule b
]

Just c ->
[ c ] ->
JS.ExprCall (JS.ExprRef (JsName.fromKernel Name.utils "Tuple3"))
[ generateJsExpr mode parentModule a
, generateJsExpr mode parentModule b
, generateJsExpr mode parentModule c
]

_ ->
JS.ExprCall (JS.ExprRef (JsName.fromKernel Name.utils "Tuple"))
(List.map (generateJsExpr mode parentModule) (a :: b :: cs))

Opt.Shader src attributes uniforms ->
let
toTranlation : Name.Name -> ( JsName.Name, JS.Expr )
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Nitpick/Debug.elm
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ hasDebug expression =
Opt.Unit ->
False

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

Opt.Shader _ _ _ ->
False
Expand Down
Loading

0 comments on commit f1ecae9

Please sign in to comment.