Skip to content

Commit 2ee1328

Browse files
authored
feat(compiler)!: Make List a language-supplied type (#1648)
1 parent 2ff9d9e commit 2ee1328

File tree

234 files changed

+1903
-2013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+1903
-2013
lines changed

compiler/src/parsing/parser.messages

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -649,89 +649,6 @@ program: MODULE UIDENT EOL DASH WHILE
649649

650650
Expected a number to follow the minus sign.
651651

652-
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS RBRACK LPAREN UIDENT RCARET
653-
##
654-
## Ends in an error in state: 961.
655-
##
656-
## data_constructor -> lbrack ELLIPSIS rbrack lparen option(typs) . rparen [ RBRACE EOL COMMA ]
657-
##
658-
## The known suffix of the stack is as follows:
659-
## lbrack ELLIPSIS rbrack lparen option(typs)
660-
##
661-
## WARNING: This example involves spurious reductions.
662-
## This implies that, although the LR(1) items shown above provide an
663-
## accurate view of the past (what has been recognized so far), they
664-
## may provide an INCOMPLETE view of the future (what was expected next).
665-
## In state 89, spurious reduction of production type_id -> lseparated_nonempty_list_inner(dot,type_id_str)
666-
## In state 79, spurious reduction of production data_typ -> type_id
667-
## In state 106, spurious reduction of production typ -> data_typ
668-
## In state 88, spurious reduction of production lseparated_nonempty_list_inner(comma,typ) -> typ
669-
## In state 94, spurious reduction of production option(comma) ->
670-
## In state 97, spurious reduction of production typs -> lseparated_nonempty_list_inner(comma,typ) option(comma)
671-
## In state 119, spurious reduction of production option(typs) -> typs
672-
##
673-
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS RBRACK LPAREN WHILE
674-
##
675-
## Ends in an error in state: 960.
676-
##
677-
## data_constructor -> lbrack ELLIPSIS rbrack lparen . option(typs) rparen [ RBRACE EOL COMMA ]
678-
##
679-
## The known suffix of the stack is as follows:
680-
## lbrack ELLIPSIS rbrack lparen
681-
##
682-
## WARNING: This example involves spurious reductions.
683-
## This implies that, although the LR(1) items shown above provide an
684-
## accurate view of the past (what has been recognized so far), they
685-
## may provide an INCOMPLETE view of the future (what was expected next).
686-
## In state 2, spurious reduction of production lparen -> LPAREN
687-
##
688-
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS RBRACK WHILE
689-
##
690-
## Ends in an error in state: 959.
691-
##
692-
## data_constructor -> lbrack ELLIPSIS rbrack . lparen option(typs) rparen [ RBRACE EOL COMMA ]
693-
##
694-
## The known suffix of the stack is as follows:
695-
## lbrack ELLIPSIS rbrack
696-
##
697-
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS WHILE
698-
##
699-
## Ends in an error in state: 958.
700-
##
701-
## data_constructor -> lbrack ELLIPSIS . rbrack lparen option(typs) rparen [ RBRACE EOL COMMA ]
702-
##
703-
## The known suffix of the stack is as follows:
704-
## lbrack ELLIPSIS
705-
##
706-
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK RBRACK WHILE
707-
##
708-
## Ends in an error in state: 953.
709-
##
710-
## data_constructors -> lbrace lseparated_nonempty_list_inner(comma,data_constructor) . option(comma) rbrace [ SEMI EOL EOF COMMA ]
711-
## lseparated_nonempty_list_inner(comma,data_constructor) -> lseparated_nonempty_list_inner(comma,data_constructor) . comma data_constructor [ RBRACE EOL COMMA ]
712-
##
713-
## The known suffix of the stack is as follows:
714-
## lbrace lseparated_nonempty_list_inner(comma,data_constructor)
715-
##
716-
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK WHILE
717-
##
718-
## Ends in an error in state: 957.
719-
##
720-
## data_constructor -> lbrack . rbrack [ RBRACE EOL COMMA ]
721-
## data_constructor -> lbrack . ELLIPSIS rbrack lparen option(typs) rparen [ RBRACE EOL COMMA ]
722-
##
723-
## The known suffix of the stack is as follows:
724-
## lbrack
725-
##
726-
## WARNING: This example involves spurious reductions.
727-
## This implies that, although the LR(1) items shown above provide an
728-
## accurate view of the past (what has been recognized so far), they
729-
## may provide an INCOMPLETE view of the future (what was expected next).
730-
## In state 19, spurious reduction of production lbrack -> LBRACK
731-
##
732-
733-
Expected a list enum declaration using the syntax `enum List<a> { [], [...](a, List<a>) }`.
734-
735652
program: MODULE UIDENT EOL ENUM UIDENT LBRACE UIDENT COMMA EOL WHILE
736653
##
737654
## Ends in an error in state: 956.
@@ -6290,6 +6207,19 @@ program: MODULE UIDENT EOL UIDENT LPAREN WHEN
62906207

62916208
Expected a comma-separated list of expressions or `)` to complete the constructor.
62926209

6210+
program: MODULE UIDENT EOL ENUM UIDENT LBRACE UIDENT LPAREN RPAREN YIELD
6211+
##
6212+
## Ends in an error in state: 778.
6213+
##
6214+
## data_constructors -> lbrace lseparated_nonempty_list_inner(comma,data_constructor) . option(comma) rbrace [ SEMI RBRACE EOL EOF COMMA ]
6215+
## lseparated_nonempty_list_inner(comma,data_constructor) -> lseparated_nonempty_list_inner(comma,data_constructor) . comma data_constructor [ RBRACE EOL COMMA ]
6216+
##
6217+
## The known suffix of the stack is as follows:
6218+
## lbrace lseparated_nonempty_list_inner(comma,data_constructor)
6219+
##
6220+
6221+
Expected a comma followed by more constructors or `}` to complete the enum declaration.
6222+
62936223
program: MODULE UIDENT EOL WHILE LPAREN UNDERSCORE
62946224
##
62956225
## Ends in an error in state: 7.

compiler/src/parsing/parser.mly

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,6 @@ data_constructor:
369369
| UIDENT { ConstructorDeclaration.singleton ~loc:(to_loc $loc) (mkstr $loc $1) }
370370
| UIDENT lparen typs? rparen { ConstructorDeclaration.tuple ~loc:(to_loc $loc) (mkstr $loc $1) (Option.value ~default:[] $3) }
371371
| UIDENT data_labels { ConstructorDeclaration.record ~loc:(to_loc $loc) (mkstr $loc $1) $2 }
372-
/* Special support for lists */
373-
| lbrack rbrack { ConstructorDeclaration.singleton ~loc:(to_loc $loc) (mkstr $loc "[]") }
374-
| lbrack ELLIPSIS rbrack lparen typs? rparen { ConstructorDeclaration.tuple ~loc:(to_loc $loc) (mkstr $loc "[...]") (Option.value ~default:[] $5) }
375372

376373
data_constructors:
377374
| lbrace lseparated_nonempty_list(comma, data_constructor) comma? rbrace { $2 }

compiler/src/typed/builtin_types.re

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let ident_number = ident_create("Number")
3636
and ident_exception = ident_create("Exception")
3737
and ident_option = ident_create("Option")
3838
and ident_result = ident_create("Result")
39+
and ident_list = ident_create("List")
3940
and ident_int32 = ident_create("Int32")
4041
and ident_int64 = ident_create("Int64")
4142
and ident_wasmi32 = ident_create("WasmI32")
@@ -63,6 +64,7 @@ let path_number = PIdent(ident_number)
6364
and path_exception = PIdent(ident_exception)
6465
and path_option = PIdent(ident_option)
6566
and path_result = PIdent(ident_result)
67+
and path_list = PIdent(ident_list)
6668
and path_int32 = PIdent(ident_int32)
6769
and path_int64 = PIdent(ident_int64)
6870
and path_wasmi32 = PIdent(ident_wasmi32)
@@ -88,6 +90,7 @@ and type_option = var =>
8890
newgenty(TTyConstr(path_option, [var], ref(TMemNil)))
8991
and type_result = (ok, err) =>
9092
newgenty(TTyConstr(path_result, [ok, err], ref(TMemNil)))
93+
and type_list = var => newgenty(TTyConstr(path_list, [var], ref(TMemNil)))
9194
and type_int32 = newgenty(TTyConstr(path_int32, [], ref(TMemNil)))
9295
and type_int64 = newgenty(TTyConstr(path_int64, [], ref(TMemNil)))
9396
and type_rational = newgenty(TTyConstr(path_rational, [], ref(TMemNil)))
@@ -139,7 +142,9 @@ and ident_void_cstr = ident_create("()")
139142
and ident_some_cstr = ident_create("Some")
140143
and ident_none_cstr = ident_create("None")
141144
and ident_ok_cstr = ident_create("Ok")
142-
and ident_err_cstr = ident_create("Err");
145+
and ident_err_cstr = ident_create("Err")
146+
and ident_cons_cstr = ident_create("[...]")
147+
and ident_empty_cstr = ident_create("[]");
143148

144149
let decl_exception = {...decl_abstr(path_exception), type_kind: TDataOpen};
145150
let decl_bool = {
@@ -177,6 +182,19 @@ and decl_result = {
177182
]),
178183
};
179184
}
185+
and decl_list = {
186+
let tvar = newgenvar();
187+
{
188+
...decl_abstr(path_list),
189+
type_params: [tvar],
190+
type_arity: 1,
191+
type_kind:
192+
TDataVariant([
193+
cstr(ident_cons_cstr, [tvar, type_list(tvar)]),
194+
cstr(ident_empty_cstr, []),
195+
]),
196+
};
197+
}
180198
and decl_box = {
181199
let tvar = newgenvar();
182200
{...decl_abstr(path_box), type_params: [tvar], type_arity: 1};
@@ -212,6 +230,7 @@ let initial_env = (add_type, add_extension, empty_env) =>
212230
|> add_type(ident_exception, decl_exception)
213231
|> add_type(ident_option, decl_option)
214232
|> add_type(ident_result, decl_result)
233+
|> add_type(ident_list, decl_list)
215234
|> add_type(ident_int32, decl_abstr(path_int32))
216235
|> add_type(ident_int64, decl_abstr(path_int64))
217236
|> add_type(ident_float32, decl_abstr(path_float32))

compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ arrays › array_access
1515
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
1616
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
1717
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
18-
(global $x_1117 (mut i32) (i32.const 0))
18+
(global $x_1116 (mut i32) (i32.const 0))
1919
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
2020
(elem $elem (global.get $relocBase_0))
2121
(export \"memory\" (memory $0))
@@ -44,7 +44,7 @@ arrays › array_access
4444
(local.set $0
4545
(block $compile_block.10 (result i32)
4646
(block $compile_store.3
47-
(global.set $x_1117
47+
(global.set $x_1116
4848
(tuple.extract 0
4949
(tuple.make
5050
(block $allocate_array.1 (result i32)
@@ -77,7 +77,7 @@ arrays › array_access
7777
)
7878
(call $decRef_0
7979
(global.get $GRAIN$EXPORT$decRef_0)
80-
(global.get $x_1117)
80+
(global.get $x_1116)
8181
)
8282
)
8383
)
@@ -90,7 +90,7 @@ arrays › array_access
9090
(i32.const 1)
9191
)
9292
(local.set $2
93-
(global.get $x_1117)
93+
(global.get $x_1116)
9494
)
9595
(block $resolve_idx.8
9696
(if
@@ -149,7 +149,7 @@ arrays › array_access
149149
)
150150
(i32.store offset=12
151151
(local.get $0)
152-
(i32.const 49)
152+
(i32.const 51)
153153
)
154154
(i32.store offset=16
155155
(local.get $0)
@@ -183,7 +183,7 @@ arrays › array_access
183183
)
184184
(i32.store offset=12
185185
(local.get $0)
186-
(i32.const 51)
186+
(i32.const 53)
187187
)
188188
(i32.store offset=16
189189
(local.get $0)
@@ -249,7 +249,7 @@ arrays › array_access
249249
)
250250
(i32.store offset=12
251251
(local.get $0)
252-
(i32.const 49)
252+
(i32.const 51)
253253
)
254254
(i32.store offset=16
255255
(local.get $0)

compiler/test/__snapshots__/arrays.1deb7b51.0.snapshot

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ arrays › array_access5
149149
)
150150
(i32.store offset=12
151151
(local.get $0)
152-
(i32.const 49)
152+
(i32.const 51)
153153
)
154154
(i32.store offset=16
155155
(local.get $0)
@@ -183,7 +183,7 @@ arrays › array_access5
183183
)
184184
(i32.store offset=12
185185
(local.get $0)
186-
(i32.const 51)
186+
(i32.const 53)
187187
)
188188
(i32.store offset=16
189189
(local.get $0)
@@ -249,7 +249,7 @@ arrays › array_access5
249249
)
250250
(i32.store offset=12
251251
(local.get $0)
252-
(i32.const 49)
252+
(i32.const 51)
253253
)
254254
(i32.store offset=16
255255
(local.get $0)

compiler/test/__snapshots__/arrays.28fcc534.0.snapshot

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ arrays › array_access4
1515
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
1616
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
1717
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
18-
(global $x_1117 (mut i32) (i32.const 0))
18+
(global $x_1116 (mut i32) (i32.const 0))
1919
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
2020
(elem $elem (global.get $relocBase_0))
2121
(export \"memory\" (memory $0))
@@ -44,7 +44,7 @@ arrays › array_access4
4444
(local.set $0
4545
(block $compile_block.10 (result i32)
4646
(block $compile_store.3
47-
(global.set $x_1117
47+
(global.set $x_1116
4848
(tuple.extract 0
4949
(tuple.make
5050
(block $allocate_array.1 (result i32)
@@ -77,7 +77,7 @@ arrays › array_access4
7777
)
7878
(call $decRef_0
7979
(global.get $GRAIN$EXPORT$decRef_0)
80-
(global.get $x_1117)
80+
(global.get $x_1116)
8181
)
8282
)
8383
)
@@ -90,7 +90,7 @@ arrays › array_access4
9090
(i32.const -3)
9191
)
9292
(local.set $2
93-
(global.get $x_1117)
93+
(global.get $x_1116)
9494
)
9595
(block $resolve_idx.8
9696
(if
@@ -149,7 +149,7 @@ arrays › array_access4
149149
)
150150
(i32.store offset=12
151151
(local.get $0)
152-
(i32.const 49)
152+
(i32.const 51)
153153
)
154154
(i32.store offset=16
155155
(local.get $0)
@@ -183,7 +183,7 @@ arrays › array_access4
183183
)
184184
(i32.store offset=12
185185
(local.get $0)
186-
(i32.const 51)
186+
(i32.const 53)
187187
)
188188
(i32.store offset=16
189189
(local.get $0)
@@ -249,7 +249,7 @@ arrays › array_access4
249249
)
250250
(i32.store offset=12
251251
(local.get $0)
252-
(i32.const 49)
252+
(i32.const 51)
253253
)
254254
(i32.store offset=16
255255
(local.get $0)

compiler/test/__snapshots__/arrays.4c8c9f91.0.snapshot

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ arrays › array_access2
1515
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
1616
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
1717
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
18-
(global $x_1117 (mut i32) (i32.const 0))
18+
(global $x_1116 (mut i32) (i32.const 0))
1919
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
2020
(elem $elem (global.get $relocBase_0))
2121
(export \"memory\" (memory $0))
@@ -44,7 +44,7 @@ arrays › array_access2
4444
(local.set $0
4545
(block $compile_block.10 (result i32)
4646
(block $compile_store.3
47-
(global.set $x_1117
47+
(global.set $x_1116
4848
(tuple.extract 0
4949
(tuple.make
5050
(block $allocate_array.1 (result i32)
@@ -77,7 +77,7 @@ arrays › array_access2
7777
)
7878
(call $decRef_0
7979
(global.get $GRAIN$EXPORT$decRef_0)
80-
(global.get $x_1117)
80+
(global.get $x_1116)
8181
)
8282
)
8383
)
@@ -90,7 +90,7 @@ arrays › array_access2
9090
(i32.const 3)
9191
)
9292
(local.set $2
93-
(global.get $x_1117)
93+
(global.get $x_1116)
9494
)
9595
(block $resolve_idx.8
9696
(if
@@ -149,7 +149,7 @@ arrays › array_access2
149149
)
150150
(i32.store offset=12
151151
(local.get $0)
152-
(i32.const 49)
152+
(i32.const 51)
153153
)
154154
(i32.store offset=16
155155
(local.get $0)
@@ -183,7 +183,7 @@ arrays › array_access2
183183
)
184184
(i32.store offset=12
185185
(local.get $0)
186-
(i32.const 51)
186+
(i32.const 53)
187187
)
188188
(i32.store offset=16
189189
(local.get $0)
@@ -249,7 +249,7 @@ arrays › array_access2
249249
)
250250
(i32.store offset=12
251251
(local.get $0)
252-
(i32.const 49)
252+
(i32.const 51)
253253
)
254254
(i32.store offset=16
255255
(local.get $0)

0 commit comments

Comments
 (0)