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 13, 2025
1 parent 0b5dc8f commit ba1f99e
Show file tree
Hide file tree
Showing 19 changed files with 271 additions and 183 deletions.
14 changes: 7 additions & 7 deletions src/Builder/Build.elm
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ crawlFile ((Env _ root projectType _ buildID _ _) as env) mvar docsNeed expected
Err err ->
IO.pure <| SBadSyntax path time source err

Ok ((Src.Module maybeActualName _ _ imports values _ _ _ _) as modul) ->
Ok ((Src.Module _ maybeActualName _ _ imports values _ _ _ _) as modul) ->
case maybeActualName of
Nothing ->
IO.pure <| SBadSyntax path time source (Syntax.ModuleNameUnspecified expectedName)
Expand Down Expand Up @@ -543,7 +543,7 @@ checkModule ((Env _ root projectType _ _ _ _) as env) foreigns resultsMVar name
RProblem <|
Error.Module name path time source <|
case Parse.fromByteString (SV.fileSyntaxVersion path) projectType source of
Ok (Src.Module _ _ _ imports _ _ _ _ _) ->
Ok (Src.Module _ _ _ _ imports _ _ _ _ _) ->
Error.BadImports (toImportErrors env results imports problems)

Err err ->
Expand All @@ -552,7 +552,7 @@ checkModule ((Env _ root projectType _ _ _ _) as env) foreigns resultsMVar name
)
)

SChanged ((Details.Local path time deps _ _ lastCompile) as local) source ((Src.Module _ _ _ imports _ _ _ _ _) as modul) docsNeed ->
SChanged ((Details.Local path time deps _ _ lastCompile) as local) source ((Src.Module _ _ _ _ imports _ _ _ _ _) as modul) docsNeed ->
Utils.readMVar resultDictDecoder resultsMVar
|> IO.bind
(\results ->
Expand Down Expand Up @@ -1298,7 +1298,7 @@ fromRepl root details source =
Err syntaxError ->
IO.pure <| Err <| Exit.ReplBadInput source <| Error.BadSyntax syntaxError

Ok ((Src.Module _ _ _ imports _ _ _ _ _) as modul) ->
Ok ((Src.Module _ _ _ _ imports _ _ _ _ _) as modul) ->
Details.loadInterfaces root details
|> IO.bind
(\dmvar ->
Expand Down Expand Up @@ -1358,7 +1358,7 @@ fromRepl root details source =


finalizeReplArtifacts : Env -> String -> Src.Module -> DepsStatus -> ResultDict -> Dict String ModuleName.Raw BResult -> IO (Result Exit.Repl ReplArtifacts)
finalizeReplArtifacts ((Env _ root projectType _ _ _ _) as env) source ((Src.Module _ _ _ imports _ _ _ _ _) as modul) depsStatus resultMVars results =
finalizeReplArtifacts ((Env _ root projectType _ _ _ _) as env) source ((Src.Module _ _ _ _ imports _ _ _ _ _) as modul) depsStatus resultMVars results =
let
pkg : Pkg.Name
pkg =
Expand Down Expand Up @@ -1635,7 +1635,7 @@ crawlRoot ((Env _ _ projectType _ buildID _ _) as env) mvar root =
|> IO.bind
(\source ->
case Parse.fromByteString (SV.fileSyntaxVersion path) projectType source of
Ok ((Src.Module _ _ _ imports values _ _ _ _) as modul) ->
Ok ((Src.Module _ _ _ _ imports values _ _ _ _) as modul) ->
let
deps : List Name.Name
deps =
Expand Down Expand Up @@ -1675,7 +1675,7 @@ checkRoot ((Env _ root _ _ _ _ _) as env) results rootStatus =
SOutsideErr err ->
IO.pure (ROutsideErr err)

SOutsideOk ((Details.Local path time deps _ _ lastCompile) as local) source ((Src.Module _ _ _ imports _ _ _ _ _) as modul) ->
SOutsideOk ((Details.Local path time deps _ _ lastCompile) as local) source ((Src.Module _ _ _ _ imports _ _ _ _ _) as modul) ->
checkDeps root results deps lastCompile
|> IO.bind
(\depsStatus ->
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/Elm/Details.elm
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ crawlFile syntaxVersion foreignDeps mvar pkg src docsStatus expectedName path =
|> IO.bind
(\bytes ->
case Parse.fromByteString syntaxVersion (Parse.Package pkg) bytes of
Ok ((Src.Module (Just (A.At _ actualName)) _ _ imports _ _ _ _ _) as modul) ->
Ok ((Src.Module _ (Just (A.At _ actualName)) _ _ imports _ _ _ _ _) as modul) ->
if expectedName == actualName then
crawlImports foreignDeps mvar pkg src imports
|> IO.fmap (\deps -> Just (SLocal docsStatus deps modul))
Expand Down
13 changes: 13 additions & 0 deletions src/Compiler/AST/Optimized.elm
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type Destructor

type Path
= Index Index.ZeroBased Path
| ArrayIndex Int Path
| Field Name Path
| Unbox Path
| Root Name
Expand Down Expand Up @@ -1045,6 +1046,13 @@ pathEncoder path =
, ( "subPath", pathEncoder subPath )
]

ArrayIndex index subPath ->
Encode.object
[ ( "type", Encode.string "ArrayIndex" )
, ( "index", Encode.int index )
, ( "subPath", pathEncoder subPath )
]

Field field subPath ->
Encode.object
[ ( "type", Encode.string "Field" )
Expand Down Expand Up @@ -1076,6 +1084,11 @@ pathDecoder =
(Decode.field "index" Index.zeroBasedDecoder)
(Decode.field "subPath" pathDecoder)

"ArrayIndex" ->
Decode.map2 ArrayIndex
(Decode.field "index" Decode.int)
(Decode.field "subPath" pathDecoder)

"Field" ->
Decode.map2 Field
(Decode.field "field" Decode.string)
Expand Down
15 changes: 10 additions & 5 deletions src/Compiler/AST/Source.elm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Compiler.AST.Utils.Shader as Shader
import Compiler.Data.Name as Name exposing (Name)
import Compiler.Json.Encode as E
import Compiler.Parse.Primitives as P
import Compiler.Parse.SyntaxVersion as SV exposing (SyntaxVersion)
import Compiler.Reporting.Annotation as A
import Json.Decode as Decode
import Json.Encode as Encode
Expand Down Expand Up @@ -133,11 +134,11 @@ type Type_


type Module
= Module (Maybe (A.Located Name)) (A.Located Exposing) Docs (List Import) (List (A.Located Value)) (List (A.Located Union)) (List (A.Located Alias)) (List (A.Located Infix)) Effects
= Module SyntaxVersion (Maybe (A.Located Name)) (A.Located Exposing) Docs (List Import) (List (A.Located Value)) (List (A.Located Union)) (List (A.Located Alias)) (List (A.Located Infix)) Effects


getName : Module -> Name
getName (Module maybeName _ _ _ _ _ _ _ _) =
getName (Module _ maybeName _ _ _ _ _ _ _ _) =
case maybeName of
Just (A.At _ name) ->
name
Expand Down Expand Up @@ -338,9 +339,10 @@ internalTypeDecoder =


moduleEncoder : Module -> Encode.Value
moduleEncoder (Module maybeName exports docs imports values unions aliases binops effects) =
moduleEncoder (Module syntaxVersion maybeName exports docs imports values unions aliases binops effects) =
Encode.object
[ ( "type", Encode.string "Module" )
, ( "syntaxVersion", SV.encoder syntaxVersion )
, ( "maybeName", E.maybe (A.locatedEncoder Encode.string) maybeName )
, ( "exports", A.locatedEncoder exposingEncoder exports )
, ( "docs", docsEncoder docs )
Expand All @@ -355,12 +357,15 @@ moduleEncoder (Module maybeName exports docs imports values unions aliases binop

moduleDecoder : Decode.Decoder Module
moduleDecoder =
Decode.map8 (\( maybeName, exports ) -> Module maybeName exports)
Decode.map8 (\( syntaxVersion, maybeName ) ( exports, docs ) -> Module syntaxVersion maybeName exports docs)
(Decode.map2 Tuple.pair
(Decode.field "syntaxVersion" SV.decoder)
(Decode.field "maybeName" (Decode.maybe (A.locatedDecoder Decode.string)))
)
(Decode.map2 Tuple.pair
(Decode.field "exports" (A.locatedDecoder exposingDecoder))
(Decode.field "docs" docsDecoder)
)
(Decode.field "docs" docsDecoder)
(Decode.field "imports" (Decode.list importDecoder))
(Decode.field "values" (Decode.list (A.locatedDecoder valueDecoder)))
(Decode.field "unions" (Decode.list (A.locatedDecoder unionDecoder)))
Expand Down
14 changes: 8 additions & 6 deletions src/Compiler/Canonicalize/Effects.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Compiler.Canonicalize.Environment as Env
import Compiler.Canonicalize.Type as Type
import Compiler.Data.Name as Name
import Compiler.Elm.ModuleName as ModuleName
import Compiler.Parse.SyntaxVersion exposing (SyntaxVersion)
import Compiler.Reporting.Annotation as A
import Compiler.Reporting.Error.Canonicalize as Error
import Compiler.Reporting.Result as R
Expand All @@ -31,12 +32,13 @@ type alias EResult i w a =


canonicalize :
Env.Env
SyntaxVersion
-> Env.Env
-> List (A.Located Src.Value)
-> Dict String Name.Name union
-> Src.Effects
-> EResult i w Can.Effects
canonicalize env values unions effects =
canonicalize syntaxVersion env values unions effects =
case effects of
Src.NoEffects ->
R.ok Can.NoEffects
Expand All @@ -45,7 +47,7 @@ canonicalize env values unions effects =
let
pairs : R.RResult i w Error.Error (List ( Name.Name, Can.Port ))
pairs =
R.traverse (canonicalizePort env) ports
R.traverse (canonicalizePort syntaxVersion env) ports
in
R.fmap (Can.Ports << Dict.fromList identity) pairs

Expand Down Expand Up @@ -100,9 +102,9 @@ canonicalize env values unions effects =
-- CANONICALIZE PORT


canonicalizePort : Env.Env -> Src.Port -> EResult i w ( Name.Name, Can.Port )
canonicalizePort env (Src.Port (A.At region portName) tipe) =
Type.toAnnotation env tipe
canonicalizePort : SyntaxVersion -> Env.Env -> Src.Port -> EResult i w ( Name.Name, Can.Port )
canonicalizePort syntaxVersion env (Src.Port (A.At region portName) tipe) =
Type.toAnnotation syntaxVersion env tipe
|> R.bind
(\(Can.Forall freeVars ctipe) ->
case List.reverse (Type.delambda (Type.deepDealias ctipe)) of
Expand Down
43 changes: 22 additions & 21 deletions src/Compiler/Canonicalize/Environment/Local.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Compiler.Canonicalize.Environment.Dups as Dups
import Compiler.Canonicalize.Type as Type
import Compiler.Data.Index as Index
import Compiler.Data.Name as Name exposing (Name)
import Compiler.Parse.SyntaxVersion exposing (SyntaxVersion)
import Compiler.Reporting.Annotation as A
import Compiler.Reporting.Error.Canonicalize as Error
import Compiler.Reporting.Result as R
Expand Down Expand Up @@ -59,7 +60,7 @@ addVars module_ env =


collectVars : Src.Module -> LResult i w (Dict String Name.Name Env.Var)
collectVars (Src.Module _ _ _ _ values _ _ _ effects) =
collectVars (Src.Module _ _ _ _ _ values _ _ _ effects) =
let
addDecl : A.Located Src.Value -> Dups.Tracker Env.Var -> Dups.Tracker Env.Var
addDecl (A.At _ (Src.Value (A.At region name) _ _ _)) =
Expand Down Expand Up @@ -102,7 +103,7 @@ toEffectDups effects =


addTypes : Src.Module -> Env.Env -> LResult i w Env.Env
addTypes (Src.Module _ _ _ _ _ unions aliases _ _) env =
addTypes (Src.Module syntaxVersion _ _ _ _ _ unions aliases _ _) env =
let
addAliasDups : A.Located Src.Alias -> Dups.Tracker () -> Dups.Tracker ()
addAliasDups (A.At _ (Src.Alias (A.At region name) _ _)) =
Expand All @@ -120,7 +121,7 @@ addTypes (Src.Module _ _ _ _ _ unions aliases _ _) env =
|> R.bind
(\_ ->
Utils.foldM (addUnion env.home) env.types unions
|> R.bind (\ts1 -> addAliases aliases <| { env | types = ts1 })
|> R.bind (\ts1 -> addAliases syntaxVersion aliases <| { env | types = ts1 })
)


Expand All @@ -142,8 +143,8 @@ addUnion home types ((A.At _ (Src.Union (A.At _ name) _ _)) as union) =
-- ADD TYPE ALIASES


addAliases : List (A.Located Src.Alias) -> Env.Env -> LResult i w Env.Env
addAliases aliases env =
addAliases : SyntaxVersion -> List (A.Located Src.Alias) -> Env.Env -> LResult i w Env.Env
addAliases syntaxVersion aliases env =
let
nodes : List ( A.Located Src.Alias, Name, List Name )
nodes =
Expand All @@ -153,17 +154,17 @@ addAliases aliases env =
sccs =
Graph.stronglyConnComp nodes
in
Utils.foldM addAlias env sccs
Utils.foldM (addAlias syntaxVersion) env sccs


addAlias : Env.Env -> Graph.SCC (A.Located Src.Alias) -> LResult i w Env.Env
addAlias ({ home, vars, types, ctors, binops, q_vars, q_types, q_ctors } as env) scc =
addAlias : SyntaxVersion -> Env.Env -> Graph.SCC (A.Located Src.Alias) -> LResult i w Env.Env
addAlias syntaxVersion ({ home, vars, types, ctors, binops, q_vars, q_types, q_ctors } as env) scc =
case scc of
Graph.AcyclicSCC ((A.At _ (Src.Alias (A.At _ name) _ tipe)) as alias) ->
checkAliasFreeVars alias
|> R.bind
(\args ->
Type.canonicalize env tipe
Type.canonicalize syntaxVersion env tipe
|> R.bind
(\ctype ->
let
Expand Down Expand Up @@ -334,11 +335,11 @@ addFreeVars (A.At region tipe) freeVars =


addCtors : Src.Module -> Env.Env -> LResult i w ( Env.Env, Unions, Aliases )
addCtors (Src.Module _ _ _ _ _ unions aliases _ _) env =
R.traverse (canonicalizeUnion env) unions
addCtors (Src.Module syntaxVersion _ _ _ _ _ unions aliases _ _) env =
R.traverse (canonicalizeUnion syntaxVersion env) unions
|> R.bind
(\unionInfo ->
R.traverse (canonicalizeAlias env) aliases
R.traverse (canonicalizeAlias syntaxVersion env) aliases
|> R.bind
(\aliasInfo ->
(Dups.detect Error.DuplicateCtor <|
Expand Down Expand Up @@ -371,14 +372,14 @@ type alias CtorDups =
-- CANONICALIZE ALIAS


canonicalizeAlias : Env.Env -> A.Located Src.Alias -> LResult i w ( ( Name.Name, Can.Alias ), CtorDups )
canonicalizeAlias ({ home } as env) (A.At _ (Src.Alias (A.At region name) args tipe)) =
canonicalizeAlias : SyntaxVersion -> Env.Env -> A.Located Src.Alias -> LResult i w ( ( Name.Name, Can.Alias ), CtorDups )
canonicalizeAlias syntaxVersion ({ home } as env) (A.At _ (Src.Alias (A.At region name) args tipe)) =
let
vars : List Name
vars =
List.map A.toValue args
in
Type.canonicalize env tipe
Type.canonicalize syntaxVersion env tipe
|> R.bind
(\ctipe ->
R.ok
Expand Down Expand Up @@ -414,9 +415,9 @@ toRecordCtor home name vars fields =
-- CANONICALIZE UNION


canonicalizeUnion : Env.Env -> A.Located Src.Union -> LResult i w ( ( Name.Name, Can.Union ), CtorDups )
canonicalizeUnion ({ home } as env) (A.At _ (Src.Union (A.At _ name) avars ctors)) =
R.indexedTraverse (canonicalizeCtor env) ctors
canonicalizeUnion : SyntaxVersion -> Env.Env -> A.Located Src.Union -> LResult i w ( ( Name.Name, Can.Union ), CtorDups )
canonicalizeUnion syntaxVersion ({ home } as env) (A.At _ (Src.Union (A.At _ name) avars ctors)) =
R.indexedTraverse (canonicalizeCtor syntaxVersion env) ctors
|> R.bind
(\cctors ->
let
Expand All @@ -436,9 +437,9 @@ canonicalizeUnion ({ home } as env) (A.At _ (Src.Union (A.At _ name) avars ctors
)


canonicalizeCtor : Env.Env -> Index.ZeroBased -> ( A.Located Name.Name, List Src.Type ) -> LResult i w (A.Located Can.Ctor)
canonicalizeCtor env index ( A.At region ctor, tipes ) =
R.traverse (Type.canonicalize env) tipes
canonicalizeCtor : SyntaxVersion -> Env.Env -> Index.ZeroBased -> ( A.Located Name.Name, List Src.Type ) -> LResult i w (A.Located Can.Ctor)
canonicalizeCtor syntaxVersion env index ( A.At region ctor, tipes ) =
R.traverse (Type.canonicalize syntaxVersion env) tipes
|> R.bind
(\ctipes ->
R.ok <|
Expand Down
Loading

0 comments on commit ba1f99e

Please sign in to comment.