Skip to content

Commit 99ffa6c

Browse files
committed
wip: unified ops
1 parent 450c433 commit 99ffa6c

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

compiler/ml/unified_ops.ml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
open Btype
2+
open Types
3+
open Misc
4+
5+
(*
6+
Unified_ops is for specialization of some primitive operators.
7+
8+
For example adding two values. We have `+` for ints, `+.` for floats, and `++` for strings.
9+
That because we don't use implicit type conversion or overloading.
10+
11+
It is a fundamental property of the ReScript language, but at the same time it is far from the best DX we can think of, and it became a problem when introducing new primitives like bigint.
12+
13+
See discussion: https://github.com/rescript-lang/rescript-compiler/issues/6525
14+
15+
1. Type level translation
16+
17+
2. IR level translation
18+
*)
19+
20+
type args = (Asttypes.arg_label * Parsetree.expression) list
21+
type targs = (Asttypes.arg_label * Typedtree.expression option) list
22+
23+
type specialized_type = {
24+
int: Path.t;
25+
bool: Path.t option;
26+
float: Path.t option;
27+
bigint: Path.t option;
28+
string: Path.t option;
29+
}
30+
31+
let specialized_types = create_hashtable [||]
32+
33+
type specialized_primitive = {
34+
int: Lambda.primitive;
35+
bool: Lambda.primitive option;
36+
float: Lambda.primitive option;
37+
bigint: Lambda.primitive option;
38+
string: Lambda.primitive option;
39+
}
40+
41+
let translate_type_application (env : Env.t) (funct : Parsetree.expression)
42+
(args : args) : (targs * type_expr) option =
43+
None
44+
45+
let translate_primitive_application (env : Env.t) (prim : Primitive.description)
46+
(args : args) : Lambda.primitive option =
47+
None

runtime/Pervasives.res

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ external __LOC_OF__: 'a => (string, 'a) = "%loc_LOC"
4040
external __LINE_OF__: 'a => (int, 'a) = "%loc_LINE"
4141
external __POS_OF__: 'a => ((string, int, int, int), 'a) = "%loc_POS"
4242

43+
/* Unified operations */
44+
45+
external \"+": ('a, 'a) => 'a = "%add"
46+
external \"-": ('a, 'a) => 'a = "%sub"
47+
4348
/* Comparisons */
49+
/* Note: Later comparisons will be converted to unified operations too */
4450

4551
external \"=": ('a, 'a) => bool = "%equal"
4652
external \"<>": ('a, 'a) => bool = "%notequal"
@@ -68,8 +74,6 @@ external \"~-": int => int = "%negint"
6874
external \"~+": int => int = "%identity"
6975
external succ: int => int = "%succint"
7076
external pred: int => int = "%predint"
71-
external \"+": ('a, 'a) => 'a = "%add"
72-
external \"-": ('a, 'a) => 'a = "%sub"
7377
external \"*": (int, int) => int = "%mulint"
7478
external \"/": (int, int) => int = "%divint"
7579
external mod: (int, int) => int = "%modint"

0 commit comments

Comments
 (0)