Stabel

Check-in [73bcd35203]
Login
Overview
Comment:Fill in missing pieces in type checker and codegen. Should be ready to implement array builtins.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: 73bcd35203e3ee3d65fd1341b230d7b373b9421044c2db0b358667bdcf9af2d2
User & Date: robin.hansen on 2021-08-23 16:56:11
Other Links: branch diff | manifest | tags
Context
2021-08-23
17:28
Added array integration test, and implemented two of the array built ins. check-in: df837b4584 user: robin.hansen tags: arrays
16:56
Fill in missing pieces in type checker and codegen. Should be ready to implement array builtins. check-in: 73bcd35203 user: robin.hansen tags: arrays
2021-08-22
14:58
Wire up the first array builtins. We still require some changes in codegen and typechecker before we... check-in: 60f3575980 user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Codegen.elm from [d82bd3d76e] to [f568b54ec2].

184
185
186
187
188
189
190
191
192
193


194




195
196
197
198
199
200
201
...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
        ( newNode, updatedContext ) =
            case node of
                AST.IntLiteral _ val ->
                    ( IntLiteral val
                    , context
                    )

                AST.ArrayLiteral _ nodes ->
                    -- TODO: Dummy code to get project to compile
                    ( IntLiteral 1


                    , context




                    )

                AST.Function _ fn _ ->
                    let
                        ( fnId, newContext ) =
                            idForFunction fn.name context
                    in
................................................................................
        nodeType =
            case node of
                AST.IntLiteral _ _ ->
                    { input = []
                    , output = [ Type.Int ]
                    }

                AST.ArrayLiteral _ _ ->
                    -- TODO: Dummy code to get project to compile
                    { input = []
                    , output = [ Type.Int ]
                    }

                AST.Function _ _ type_ ->
                    type_

                AST.FunctionRef _ fn ->
                    { input = []







|
|
|
>
>
|
>
>
>
>







 







|
<

|







184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
...
263
264
265
266
267
268
269
270

271
272
273
274
275
276
277
278
279
        ( newNode, updatedContext ) =
            case node of
                AST.IntLiteral _ val ->
                    ( IntLiteral val
                    , context
                    )

                AST.ArrayLiteral _ nodes _ ->
                    let
                        ( _, codeGenNodes, nextContext ) =
                            List.foldl
                                (astNodeToCodegenNode def)
                                ( [], [], context )
                                nodes
                    in
                    ( ArrayLiteral (List.reverse codeGenNodes)
                    , nextContext
                    )

                AST.Function _ fn _ ->
                    let
                        ( fnId, newContext ) =
                            idForFunction fn.name context
                    in
................................................................................
        nodeType =
            case node of
                AST.IntLiteral _ _ ->
                    { input = []
                    , output = [ Type.Int ]
                    }

                AST.ArrayLiteral _ _ t ->

                    { input = []
                    , output = [ t ]
                    }

                AST.Function _ _ type_ ->
                    type_

                AST.FunctionRef _ fn ->
                    { input = []

Modified src/Stabel/TypeChecker.elm from [86e12614c6] to [10caad982a].

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
...
264
265
266
267
268
269
270
271

272







273
274
275
276
277
278
279
...
311
312
313
314
315
316
317


































318
319
320
321
322
323
324
....
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
....
1384
1385
1386
1387
1388
1389
1390



























1391
1392
1393
1394
1395
1396
1397
    = LiteralInt Int
    | LiteralType Type
    | RecursiveMatch TypeMatch


type AstNode
    = IntLiteral SourceLocationRange Int
    | ArrayLiteral SourceLocationRange (List AstNode)
    | Function SourceLocationRange FunctionDefinition FunctionType
    | FunctionRef SourceLocationRange FunctionDefinition
    | Recurse SourceLocationRange
    | Cycle SourceLocationRange CycleData
    | Builtin SourceLocationRange Builtin
    | ConstructType TypeDefinition
    | SetMember TypeDefinition String Int Type
................................................................................
untypedToTypedNode : Int -> Context -> Qualifier.Node -> AstNode
untypedToTypedNode idx context untypedNode =
    case untypedNode of
        Qualifier.Integer range num ->
            IntLiteral range num

        Qualifier.ArrayLiteral range nodes ->
            ArrayLiteral range <|

                List.map (untypedToTypedNode idx context) nodes








        Qualifier.Function range function ->
            let
                ( def, _ ) =
                    typeCheckDefinition function context
            in
            Function range def <|
................................................................................

        Qualifier.SetMember typeDef memberName memberIndex memberType ->
            SetMember typeDef memberName memberIndex memberType

        Qualifier.GetMember typeDef memberName memberIndex memberType ->
            GetMember typeDef memberName memberIndex memberType




































resolveGenericsInFunctionType : Int -> Context -> FunctionType -> FunctionType
resolveGenericsInFunctionType idx context wt =
    let
        replaceGenericWithBoundValue t =
            let
                boundType =
................................................................................
                liftTupleFirstResult ( resA, b ) =
                    case resA of
                        Ok a ->
                            Ok ( a, b )

                        Err err ->
                            Err err

                unionizeTypes ts =
                    unionizeTypesHelper ts []

                unionizeTypesHelper ts acc =
                    case ts of
                        [] ->
                            case acc of
                                [] ->
                                    Type.Generic "a"

                                [ t ] ->
                                    t

                                _ ->
                                    Type.Union Nothing acc

                        t :: rest ->
                            if List.member t acc then
                                unionizeTypesHelper rest acc

                            else
                                unionizeTypesHelper rest (t :: acc)
            in
            case res of
                Ok ( inferredType, newContext ) ->
                    Ok ( newContext, [ Push <| Type.Array inferredType ] )

                Err err ->
                    Err err
................................................................................
                )


addStackEffect : Context -> Int -> List StackEffect -> Context
addStackEffect ctx idx effects =
    { ctx | stackEffects = ctx.stackEffects ++ List.map (tagGenericEffect idx) effects }





























getStructMembers : TypeDefinition -> List ( String, Type )
getStructMembers typeDef =
    case typeDef.members of
        Qualifier.StructMembers members ->
            members








|







 







|
>
|
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
....
1292
1293
1294
1295
1296
1297
1298























1299
1300
1301
1302
1303
1304
1305
....
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
    = LiteralInt Int
    | LiteralType Type
    | RecursiveMatch TypeMatch


type AstNode
    = IntLiteral SourceLocationRange Int
    | ArrayLiteral SourceLocationRange (List AstNode) Type
    | Function SourceLocationRange FunctionDefinition FunctionType
    | FunctionRef SourceLocationRange FunctionDefinition
    | Recurse SourceLocationRange
    | Cycle SourceLocationRange CycleData
    | Builtin SourceLocationRange Builtin
    | ConstructType TypeDefinition
    | SetMember TypeDefinition String Int Type
................................................................................
untypedToTypedNode : Int -> Context -> Qualifier.Node -> AstNode
untypedToTypedNode idx context untypedNode =
    case untypedNode of
        Qualifier.Integer range num ->
            IntLiteral range num

        Qualifier.ArrayLiteral range nodes ->
            let
                typedNodes =
                    List.map (untypedToTypedNode idx context) nodes

                arrayType =
                    List.filterMap arrayNodeToType typedNodes
                        |> unionizeTypes
                        |> Type.Array
            in
            ArrayLiteral range typedNodes arrayType

        Qualifier.Function range function ->
            let
                ( def, _ ) =
                    typeCheckDefinition function context
            in
            Function range def <|
................................................................................

        Qualifier.SetMember typeDef memberName memberIndex memberType ->
            SetMember typeDef memberName memberIndex memberType

        Qualifier.GetMember typeDef memberName memberIndex memberType ->
            GetMember typeDef memberName memberIndex memberType


arrayNodeToType : AstNode -> Maybe Type
arrayNodeToType n =
    case n of
        IntLiteral _ _ ->
            Just Type.Int

        ArrayLiteral _ _ t ->
            Just t

        Function _ _ t ->
            List.head t.output

        FunctionRef _ def ->
            Just <| Type.FunctionSignature def.type_

        Recurse _ ->
            Nothing

        Cycle _ data ->
            List.head data.typeSignature.output

        Builtin _ _ ->
            Nothing

        ConstructType _ ->
            Nothing

        SetMember _ _ _ _ ->
            Nothing

        GetMember _ _ _ _ ->
            Nothing


resolveGenericsInFunctionType : Int -> Context -> FunctionType -> FunctionType
resolveGenericsInFunctionType idx context wt =
    let
        replaceGenericWithBoundValue t =
            let
                boundType =
................................................................................
                liftTupleFirstResult ( resA, b ) =
                    case resA of
                        Ok a ->
                            Ok ( a, b )

                        Err err ->
                            Err err























            in
            case res of
                Ok ( inferredType, newContext ) ->
                    Ok ( newContext, [ Push <| Type.Array inferredType ] )

                Err err ->
                    Err err
................................................................................
                )


addStackEffect : Context -> Int -> List StackEffect -> Context
addStackEffect ctx idx effects =
    { ctx | stackEffects = ctx.stackEffects ++ List.map (tagGenericEffect idx) effects }


unionizeTypes : List Type -> Type
unionizeTypes ts =
    unionizeTypesHelper ts []


unionizeTypesHelper : List Type -> List Type -> Type
unionizeTypesHelper ts acc =
    case ts of
        [] ->
            case acc of
                [] ->
                    Type.Generic "a"

                [ t ] ->
                    t

                _ ->
                    Type.Union Nothing acc

        t :: rest ->
            if List.member t acc then
                unionizeTypesHelper rest acc

            else
                unionizeTypesHelper rest (t :: acc)


getStructMembers : TypeDefinition -> List ( String, Type )
getStructMembers typeDef =
    case typeDef.members of
        Qualifier.StructMembers members ->
            members