Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Option/Result to builtins #1101

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions compiler/src/typed/builtin_types.re
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ and ident_char = ident_create("Char")
and ident_void = ident_create("Void")
and ident_box = ident_create("Box")
and ident_array = ident_create("Array")
and ident_fd = ident_create("FileDescriptor");
and ident_fd = ident_create("FileDescriptor")
and ident_option = ident_create("Option")
and ident_result = ident_create("Result");

let path_number = PIdent(ident_number)
and path_exception = PIdent(ident_exception)
Expand All @@ -71,7 +73,9 @@ and path_char = PIdent(ident_char)
and path_void = PIdent(ident_void)
and path_box = PIdent(ident_box)
and path_array = PIdent(ident_array)
and path_fd = PIdent(ident_fd);
and path_fd = PIdent(ident_fd)
and path_option = PIdent(ident_option)
and path_result = PIdent(ident_result);

let type_number = newgenty(TTyConstr(path_number, [], ref(TMemNil)))
and type_exception = newgenty(TTyConstr(path_exception, [], ref(TMemNil)))
Expand All @@ -92,6 +96,10 @@ and type_void = newgenty(TTyConstr(path_void, [], ref(TMemNil)))
and type_box = var => newgenty(TTyConstr(path_box, [var], ref(TMemNil)))
and type_array = var =>
newgenty(TTyConstr(path_array, [var], ref(TMemNil)))
and type_option = var =>
newgenty(TTyConstr(path_option, [var], ref(TMemNil)))
and type_result = (var1, var2) =>
newgenty(TTyConstr(path_result, [var1, var2], ref(TMemNil)))
and type_fd = newgenty(TTyConstr(path_fd, [], ref(TMemNil)))
and type_lambda = (args, res) => newgenty(TTyArrow(args, res, TComOk));

Expand Down Expand Up @@ -122,7 +130,11 @@ let cstr = (id, args) => {

let ident_false = ident_create("false")
and ident_true = ident_create("true")
and ident_void_cstr = ident_create("()");
and ident_void_cstr = ident_create("()")
and ident_some = ident_create("Some")
and ident_none = ident_create("None")
and ident_ok = ident_create("Ok")
and ident_err = ident_create("Err");

let decl_create = decl => {
builtin_decls := [decl, ...builtin_decls^];
Expand Down Expand Up @@ -156,6 +168,27 @@ and decl_array = {
type_params: [tvar],
type_arity: 1,
});
}

and decl_option = {
let tvar = newgenvar();
decl_create({
...decl_abstr(path_option),
type_kind: TDataVariant([cstr(ident_some, [tvar]), cstr(ident_none, [])]),
type_params: [tvar],
type_arity: 1,
});
}

and decl_result = {
let tvar_t = newgenvar();
let tvar_e = newgenvar();
decl_create({
...decl_abstr(path_result),
type_kind: TDataVariant([cstr(ident_ok, [tvar_t]), cstr(ident_err, [tvar_e])]),
type_params: [tvar_t, tvar_e],
type_arity: 2,
});
};

let initial_env = (add_type, empty_env) =>
Expand All @@ -177,6 +210,8 @@ let initial_env = (add_type, empty_env) =>
|> add_type(ident_char, decl_abstr(path_char))
|> add_type(ident_void, decl_void)
|> add_type(ident_array, decl_array)
|> add_type(ident_option, decl_option)
|> add_type(ident_result, decl_result)
|> add_type(ident_fd, decl_abstr(path_fd))
|> add_type(ident_bytes, decl_abstr(path_bytes));

Expand Down