Skip to content

Commit

Permalink
feat(compiler)!: Make List a language-supplied type (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Feb 2, 2023
1 parent 2ff9d9e commit 2ee1328
Show file tree
Hide file tree
Showing 234 changed files with 1,903 additions and 2,013 deletions.
96 changes: 13 additions & 83 deletions compiler/src/parsing/parser.messages
Original file line number Diff line number Diff line change
Expand Up @@ -649,89 +649,6 @@ program: MODULE UIDENT EOL DASH WHILE

Expected a number to follow the minus sign.

program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS RBRACK LPAREN UIDENT RCARET
##
## Ends in an error in state: 961.
##
## data_constructor -> lbrack ELLIPSIS rbrack lparen option(typs) . rparen [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrack ELLIPSIS rbrack lparen option(typs)
##
## WARNING: This example involves spurious reductions.
## This implies that, although the LR(1) items shown above provide an
## accurate view of the past (what has been recognized so far), they
## may provide an INCOMPLETE view of the future (what was expected next).
## In state 89, spurious reduction of production type_id -> lseparated_nonempty_list_inner(dot,type_id_str)
## In state 79, spurious reduction of production data_typ -> type_id
## In state 106, spurious reduction of production typ -> data_typ
## In state 88, spurious reduction of production lseparated_nonempty_list_inner(comma,typ) -> typ
## In state 94, spurious reduction of production option(comma) ->
## In state 97, spurious reduction of production typs -> lseparated_nonempty_list_inner(comma,typ) option(comma)
## In state 119, spurious reduction of production option(typs) -> typs
##
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS RBRACK LPAREN WHILE
##
## Ends in an error in state: 960.
##
## data_constructor -> lbrack ELLIPSIS rbrack lparen . option(typs) rparen [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrack ELLIPSIS rbrack lparen
##
## WARNING: This example involves spurious reductions.
## This implies that, although the LR(1) items shown above provide an
## accurate view of the past (what has been recognized so far), they
## may provide an INCOMPLETE view of the future (what was expected next).
## In state 2, spurious reduction of production lparen -> LPAREN
##
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS RBRACK WHILE
##
## Ends in an error in state: 959.
##
## data_constructor -> lbrack ELLIPSIS rbrack . lparen option(typs) rparen [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrack ELLIPSIS rbrack
##
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK ELLIPSIS WHILE
##
## Ends in an error in state: 958.
##
## data_constructor -> lbrack ELLIPSIS . rbrack lparen option(typs) rparen [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrack ELLIPSIS
##
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK RBRACK WHILE
##
## Ends in an error in state: 953.
##
## data_constructors -> lbrace lseparated_nonempty_list_inner(comma,data_constructor) . option(comma) rbrace [ SEMI EOL EOF COMMA ]
## lseparated_nonempty_list_inner(comma,data_constructor) -> lseparated_nonempty_list_inner(comma,data_constructor) . comma data_constructor [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrace lseparated_nonempty_list_inner(comma,data_constructor)
##
program: MODULE UIDENT EOL ENUM UIDENT LBRACE LBRACK WHILE
##
## Ends in an error in state: 957.
##
## data_constructor -> lbrack . rbrack [ RBRACE EOL COMMA ]
## data_constructor -> lbrack . ELLIPSIS rbrack lparen option(typs) rparen [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrack
##
## WARNING: This example involves spurious reductions.
## This implies that, although the LR(1) items shown above provide an
## accurate view of the past (what has been recognized so far), they
## may provide an INCOMPLETE view of the future (what was expected next).
## In state 19, spurious reduction of production lbrack -> LBRACK
##

Expected a list enum declaration using the syntax `enum List<a> { [], [...](a, List<a>) }`.

program: MODULE UIDENT EOL ENUM UIDENT LBRACE UIDENT COMMA EOL WHILE
##
## Ends in an error in state: 956.
Expand Down Expand Up @@ -6290,6 +6207,19 @@ program: MODULE UIDENT EOL UIDENT LPAREN WHEN

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

program: MODULE UIDENT EOL ENUM UIDENT LBRACE UIDENT LPAREN RPAREN YIELD
##
## Ends in an error in state: 778.
##
## data_constructors -> lbrace lseparated_nonempty_list_inner(comma,data_constructor) . option(comma) rbrace [ SEMI RBRACE EOL EOF COMMA ]
## lseparated_nonempty_list_inner(comma,data_constructor) -> lseparated_nonempty_list_inner(comma,data_constructor) . comma data_constructor [ RBRACE EOL COMMA ]
##
## The known suffix of the stack is as follows:
## lbrace lseparated_nonempty_list_inner(comma,data_constructor)
##

Expected a comma followed by more constructors or `}` to complete the enum declaration.

program: MODULE UIDENT EOL WHILE LPAREN UNDERSCORE
##
## Ends in an error in state: 7.
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/parsing/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,6 @@ data_constructor:
| UIDENT { ConstructorDeclaration.singleton ~loc:(to_loc $loc) (mkstr $loc $1) }
| UIDENT lparen typs? rparen { ConstructorDeclaration.tuple ~loc:(to_loc $loc) (mkstr $loc $1) (Option.value ~default:[] $3) }
| UIDENT data_labels { ConstructorDeclaration.record ~loc:(to_loc $loc) (mkstr $loc $1) $2 }
/* Special support for lists */
| lbrack rbrack { ConstructorDeclaration.singleton ~loc:(to_loc $loc) (mkstr $loc "[]") }
| lbrack ELLIPSIS rbrack lparen typs? rparen { ConstructorDeclaration.tuple ~loc:(to_loc $loc) (mkstr $loc "[...]") (Option.value ~default:[] $5) }

data_constructors:
| lbrace lseparated_nonempty_list(comma, data_constructor) comma? rbrace { $2 }
Expand Down
21 changes: 20 additions & 1 deletion compiler/src/typed/builtin_types.re
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ let ident_number = ident_create("Number")
and ident_exception = ident_create("Exception")
and ident_option = ident_create("Option")
and ident_result = ident_create("Result")
and ident_list = ident_create("List")
and ident_int32 = ident_create("Int32")
and ident_int64 = ident_create("Int64")
and ident_wasmi32 = ident_create("WasmI32")
Expand Down Expand Up @@ -63,6 +64,7 @@ let path_number = PIdent(ident_number)
and path_exception = PIdent(ident_exception)
and path_option = PIdent(ident_option)
and path_result = PIdent(ident_result)
and path_list = PIdent(ident_list)
and path_int32 = PIdent(ident_int32)
and path_int64 = PIdent(ident_int64)
and path_wasmi32 = PIdent(ident_wasmi32)
Expand All @@ -88,6 +90,7 @@ and type_option = var =>
newgenty(TTyConstr(path_option, [var], ref(TMemNil)))
and type_result = (ok, err) =>
newgenty(TTyConstr(path_result, [ok, err], ref(TMemNil)))
and type_list = var => newgenty(TTyConstr(path_list, [var], ref(TMemNil)))
and type_int32 = newgenty(TTyConstr(path_int32, [], ref(TMemNil)))
and type_int64 = newgenty(TTyConstr(path_int64, [], ref(TMemNil)))
and type_rational = newgenty(TTyConstr(path_rational, [], ref(TMemNil)))
Expand Down Expand Up @@ -139,7 +142,9 @@ and ident_void_cstr = ident_create("()")
and ident_some_cstr = ident_create("Some")
and ident_none_cstr = ident_create("None")
and ident_ok_cstr = ident_create("Ok")
and ident_err_cstr = ident_create("Err");
and ident_err_cstr = ident_create("Err")
and ident_cons_cstr = ident_create("[...]")
and ident_empty_cstr = ident_create("[]");

let decl_exception = {...decl_abstr(path_exception), type_kind: TDataOpen};
let decl_bool = {
Expand Down Expand Up @@ -177,6 +182,19 @@ and decl_result = {
]),
};
}
and decl_list = {
let tvar = newgenvar();
{
...decl_abstr(path_list),
type_params: [tvar],
type_arity: 1,
type_kind:
TDataVariant([
cstr(ident_cons_cstr, [tvar, type_list(tvar)]),
cstr(ident_empty_cstr, []),
]),
};
}
and decl_box = {
let tvar = newgenvar();
{...decl_abstr(path_box), type_params: [tvar], type_arity: 1};
Expand Down Expand Up @@ -212,6 +230,7 @@ let initial_env = (add_type, add_extension, empty_env) =>
|> add_type(ident_exception, decl_exception)
|> add_type(ident_option, decl_option)
|> add_type(ident_result, decl_result)
|> add_type(ident_list, decl_list)
|> add_type(ident_int32, decl_abstr(path_int32))
|> add_type(ident_int64, decl_abstr(path_int64))
|> add_type(ident_float32, decl_abstr(path_float32))
Expand Down
14 changes: 7 additions & 7 deletions compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ arrays › array_access
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
(global $x_1117 (mut i32) (i32.const 0))
(global $x_1116 (mut i32) (i32.const 0))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand Down Expand Up @@ -44,7 +44,7 @@ arrays › array_access
(local.set $0
(block $compile_block.10 (result i32)
(block $compile_store.3
(global.set $x_1117
(global.set $x_1116
(tuple.extract 0
(tuple.make
(block $allocate_array.1 (result i32)
Expand Down Expand Up @@ -77,7 +77,7 @@ arrays › array_access
)
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(global.get $x_1117)
(global.get $x_1116)
)
)
)
Expand All @@ -90,7 +90,7 @@ arrays › array_access
(i32.const 1)
)
(local.set $2
(global.get $x_1117)
(global.get $x_1116)
)
(block $resolve_idx.8
(if
Expand Down Expand Up @@ -149,7 +149,7 @@ arrays › array_access
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -183,7 +183,7 @@ arrays › array_access
)
(i32.store offset=12
(local.get $0)
(i32.const 51)
(i32.const 53)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -249,7 +249,7 @@ arrays › array_access
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down
6 changes: 3 additions & 3 deletions compiler/test/__snapshots__/arrays.1deb7b51.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ arrays › array_access5
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -183,7 +183,7 @@ arrays › array_access5
)
(i32.store offset=12
(local.get $0)
(i32.const 51)
(i32.const 53)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -249,7 +249,7 @@ arrays › array_access5
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down
14 changes: 7 additions & 7 deletions compiler/test/__snapshots__/arrays.28fcc534.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ arrays › array_access4
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
(global $x_1117 (mut i32) (i32.const 0))
(global $x_1116 (mut i32) (i32.const 0))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand Down Expand Up @@ -44,7 +44,7 @@ arrays › array_access4
(local.set $0
(block $compile_block.10 (result i32)
(block $compile_store.3
(global.set $x_1117
(global.set $x_1116
(tuple.extract 0
(tuple.make
(block $allocate_array.1 (result i32)
Expand Down Expand Up @@ -77,7 +77,7 @@ arrays › array_access4
)
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(global.get $x_1117)
(global.get $x_1116)
)
)
)
Expand All @@ -90,7 +90,7 @@ arrays › array_access4
(i32.const -3)
)
(local.set $2
(global.get $x_1117)
(global.get $x_1116)
)
(block $resolve_idx.8
(if
Expand Down Expand Up @@ -149,7 +149,7 @@ arrays › array_access4
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -183,7 +183,7 @@ arrays › array_access4
)
(i32.store offset=12
(local.get $0)
(i32.const 51)
(i32.const 53)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -249,7 +249,7 @@ arrays › array_access4
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down
14 changes: 7 additions & 7 deletions compiler/test/__snapshots__/arrays.4c8c9f91.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ arrays › array_access2
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
(global $x_1117 (mut i32) (i32.const 0))
(global $x_1116 (mut i32) (i32.const 0))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
Expand Down Expand Up @@ -44,7 +44,7 @@ arrays › array_access2
(local.set $0
(block $compile_block.10 (result i32)
(block $compile_store.3
(global.set $x_1117
(global.set $x_1116
(tuple.extract 0
(tuple.make
(block $allocate_array.1 (result i32)
Expand Down Expand Up @@ -77,7 +77,7 @@ arrays › array_access2
)
(call $decRef_0
(global.get $GRAIN$EXPORT$decRef_0)
(global.get $x_1117)
(global.get $x_1116)
)
)
)
Expand All @@ -90,7 +90,7 @@ arrays › array_access2
(i32.const 3)
)
(local.set $2
(global.get $x_1117)
(global.get $x_1116)
)
(block $resolve_idx.8
(if
Expand Down Expand Up @@ -149,7 +149,7 @@ arrays › array_access2
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -183,7 +183,7 @@ arrays › array_access2
)
(i32.store offset=12
(local.get $0)
(i32.const 51)
(i32.const 53)
)
(i32.store offset=16
(local.get $0)
Expand Down Expand Up @@ -249,7 +249,7 @@ arrays › array_access2
)
(i32.store offset=12
(local.get $0)
(i32.const 49)
(i32.const 51)
)
(i32.store offset=16
(local.get $0)
Expand Down
Loading

0 comments on commit 2ee1328

Please sign in to comment.