From 677b57bbf271732a771ecc7226624d19f63a603e Mon Sep 17 00:00:00 2001 From: gwansikk Date: Sat, 30 Nov 2024 16:32:47 +0000 Subject: [PATCH 1/3] add left shift(`<<`) operator --- compiler/ml/unified_ops.ml | 12 ++++++++++++ compiler/syntax/src/res_core.ml | 2 +- compiler/syntax/src/res_parsetree_viewer.ml | 4 ++-- compiler/syntax/src/res_token.ml | 4 +++- runtime/Pervasives.res | 1 + runtime/Pervasives_mini.res | 1 + 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/compiler/ml/unified_ops.ml b/compiler/ml/unified_ops.ml index c57c14bce0..158a234846 100644 --- a/compiler/ml/unified_ops.ml +++ b/compiler/ml/unified_ops.ml @@ -148,6 +148,18 @@ let entries = string = None; }; }; + { + path = builtin "<<"; + name = "%lsl"; + form = Binary; + specialization = { + int = Plslint; + bool = None; + float = None; + bigint = Some Plslbigint; + string = None; + }; + }; { path = builtin "mod"; name = "%mod"; diff --git a/compiler/syntax/src/res_core.ml b/compiler/syntax/src/res_core.ml index 7538874010..2c43a83667 100644 --- a/compiler/syntax/src/res_core.ml +++ b/compiler/syntax/src/res_core.ml @@ -2221,7 +2221,7 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec = * * First case is unary, second is a binary operator. * See Scanner.isBinaryOp *) - | (Minus | MinusDot | LessThan | Percent) + | (Minus | MinusDot | LessThan | Percent | LessThanLessThan) when (not (Scanner.is_binary_op p.scanner.src p.start_pos.pos_cnum p.end_pos.pos_cnum)) diff --git a/compiler/syntax/src/res_parsetree_viewer.ml b/compiler/syntax/src/res_parsetree_viewer.ml index 1e751ded1e..bcabb061a0 100644 --- a/compiler/syntax/src/res_parsetree_viewer.ml +++ b/compiler/syntax/src/res_parsetree_viewer.ml @@ -301,7 +301,7 @@ let operator_precedence operator = | "||" -> 2 | "&&" -> 3 | "=" | "==" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 4 - | "+" | "+." | "-" | "-." | "^" -> 5 + | "+" | "+." | "-" | "-." | "^" | "<<" -> 5 | "*" | "*." | "/" | "/." | "%" -> 6 | "**" -> 7 | "#" | "##" | "|." | "|.u" -> 8 @@ -326,7 +326,7 @@ let is_binary_operator operator = match operator with | ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">=" | "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|." - | "|.u" | "<>" | "%" -> + | "|.u" | "<>" | "%" | "<<" -> true | _ -> false diff --git a/compiler/syntax/src/res_token.ml b/compiler/syntax/src/res_token.ml index b55d7c0ec7..fc81a5857a 100644 --- a/compiler/syntax/src/res_token.ml +++ b/compiler/syntax/src/res_token.ml @@ -52,6 +52,7 @@ type t = | ColonGreaterThan | GreaterThan | LessThan + | LessThanLessThan | LessThanSlash | Hash | HashEqual @@ -105,7 +106,7 @@ let precedence = function | Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual | BangEqualEqual | LessEqual | GreaterEqual | BarGreater -> 4 - | Plus | PlusDot | Minus | MinusDot | PlusPlus -> 5 + | Plus | PlusDot | Minus | MinusDot | PlusPlus | LessThanLessThan -> 5 | Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 6 | Exponentiation -> 7 | MinusGreater -> 8 @@ -163,6 +164,7 @@ let to_string = function | HashEqual -> "#=" | GreaterThan -> ">" | LessThan -> "<" + | LessThanLessThan -> "<<" | LessThanSlash -> " "*" | AsteriskDot -> "*." diff --git a/runtime/Pervasives.res b/runtime/Pervasives.res index a19348538d..12a36d7237 100644 --- a/runtime/Pervasives.res +++ b/runtime/Pervasives.res @@ -50,6 +50,7 @@ external \"-": ('a, 'a) => 'a = "%sub" external \"*": ('a, 'a) => 'a = "%mul" external \"/": ('a, 'a) => 'a = "%div" external \"%": ('a, 'a) => 'a = "%mod" +external \"<<": ('a, 'a) => 'a = "%lsl" external mod: ('a, 'a) => 'a = "%mod" /* Comparisons */ diff --git a/runtime/Pervasives_mini.res b/runtime/Pervasives_mini.res index 9cf3cd90e4..8668f58012 100644 --- a/runtime/Pervasives_mini.res +++ b/runtime/Pervasives_mini.res @@ -29,6 +29,7 @@ external \"-": (int, int) => int = "%subint" external \"*": (int, int) => int = "%mulint" external \"/": (int, int) => int = "%divint" external \"%": (int, int) => int = "%modint" +external \"<<": (int, int) => int = "%lslint" external mod: (int, int) => int = "%modint" /* Comparisons */ From 9688859f46a498163eccd2d2d216dd0fe58aef6c Mon Sep 17 00:00:00 2001 From: gwansikk Date: Sun, 8 Dec 2024 16:34:28 +0000 Subject: [PATCH 2/3] update operator precedence for shift operators --- compiler/syntax/src/res_parsetree_viewer.ml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/syntax/src/res_parsetree_viewer.ml b/compiler/syntax/src/res_parsetree_viewer.ml index bcabb061a0..dc437cfffb 100644 --- a/compiler/syntax/src/res_parsetree_viewer.ml +++ b/compiler/syntax/src/res_parsetree_viewer.ml @@ -301,10 +301,11 @@ let operator_precedence operator = | "||" -> 2 | "&&" -> 3 | "=" | "==" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 4 - | "+" | "+." | "-" | "-." | "^" | "<<" -> 5 - | "*" | "*." | "/" | "/." | "%" -> 6 - | "**" -> 7 - | "#" | "##" | "|." | "|.u" -> 8 + | "<<" | ">>" | ">>>" -> 5 + | "+" | "+." | "-" | "-." | "^" -> 6 + | "*" | "*." | "/" | "/." | "%" -> 7 + | "**" -> 8 + | "#" | "##" | "|." | "|.u" -> 9 | _ -> 0 let is_unary_operator operator = From f5b443addd55551bbacfc4c060782b8290c43790 Mon Sep 17 00:00:00 2001 From: gwansikk Date: Sun, 8 Dec 2024 16:34:49 +0000 Subject: [PATCH 3/3] remove LessThanLessThan from binary operator parsing --- compiler/syntax/src/res_core.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/syntax/src/res_core.ml b/compiler/syntax/src/res_core.ml index 2c43a83667..7538874010 100644 --- a/compiler/syntax/src/res_core.ml +++ b/compiler/syntax/src/res_core.ml @@ -2221,7 +2221,7 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec = * * First case is unary, second is a binary operator. * See Scanner.isBinaryOp *) - | (Minus | MinusDot | LessThan | Percent | LessThanLessThan) + | (Minus | MinusDot | LessThan | Percent) when (not (Scanner.is_binary_op p.scanner.src p.start_pos.pos_cnum p.end_pos.pos_cnum))