From 5361be583de102d6e1c55e5ca95f848726319b61 Mon Sep 17 00:00:00 2001 From: Ben Makuh Date: Thu, 12 Nov 2020 12:13:59 -0700 Subject: [PATCH 1/5] add bitwise operators to parser.dyp --- compiler/src/parsing/parser.dyp | 29 +++++++++++++++++++++++-- docs/contributor/operator_precedence.md | 8 +++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/compiler/src/parsing/parser.dyp b/compiler/src/parsing/parser.dyp index 11ac630851..b69495fa00 100644 --- a/compiler/src/parsing/parser.dyp +++ b/compiler/src/parsing/parser.dyp @@ -20,9 +20,9 @@ include Parser_header %token TYPEID %token STRING %token LBRACK RBRACK LPAREN LPARENNOSPACE RPAREN LBRACE RBRACE LCARET RCARET -%token CARET +%token CARET LCARETLCARET RCARETRCARET RCARETRCARETRCARET %token COMMA SEMI AS -%token THICKARROW ARROW PIPE +%token THICKARROW ARROW %token IS ISNT EQEQ LESSEQ GREATEREQ %token EQUAL GETS %token UNDERSCORE @@ -35,6 +35,7 @@ include Parser_header %token LET MUT REC IF WHEN ELSE MATCH WHILE %token AMPAMP PIPEPIPE NOT +%token AMP PIPE %token ENUM RECORD IMPORT EXPORT FOREIGN WASM PRIMITIVE %token EXCEPT FROM @@ -127,11 +128,17 @@ binop_expr : | binop_expr(<=p80) isnt_op eols? binop_expr(" } +rrcaret_op : + | RCARETRCARET { ">>" } +rrrcaret_op : + | RCARETRCARETRCARET { ">>>" } lesseq_op : | LESSEQ { "<=" } greatereq_op : | GREATEREQ { ">=" } +amp_op : + | AMP { "&" } ampamp_op : | AMPAMP { "&&" } +pipe_op : + | PIPE { "|" } pipepipe_op : | PIPEPIPE { "||" } pluseq_op : @@ -312,11 +331,17 @@ infix_op : | eqeq_op | plusplus_op | noteq_op + | caret_op | lcaret_op + | llcaret_op | rcaret_op + | rrcaret_op + | rrrcaret_op | lesseq_op | greatereq_op + | amp_op | ampamp_op + | pipe_op | pipepipe_op | pluseq_op | dasheq_op diff --git a/docs/contributor/operator_precedence.md b/docs/contributor/operator_precedence.md index 8484053b09..69f5e83016 100644 --- a/docs/contributor/operator_precedence.md +++ b/docs/contributor/operator_precedence.md @@ -12,12 +12,12 @@ This table shows which operators take precedence over other operators, and can b | 130 | Exponentiation | right-to-left | NYI | | 120 | Multiplication
Division
Modulus | left-to-right | `… * …`
`… / …`
`… % …` | | 110 | Addition
Subtraction
String Concatenation | left-to-right | `… + …`
`… - …`
`… ++ …` | -| 100 | Bitwise Shift Left
Bitwise Shift Right
Bitwise Arithmetic Shift Right | left-to-right | NYI | +| 100 | Bitwise Shift Left
Bitwise Shift Right
Bitwise Arithmetic Shift Right | left-to-right | `… << …`
`… >> …`
`… >>> …` | | 90 | Less Than
Less Than or Equal To
Greater Than
Greater Than or Equal To | left-to-right | `… < …`
`… <= …`
`… > …`
`… >= …` | | 80 | Structural Equality
Structural Inequality
Physical Equality
Physical Inequality | left-to-right | `… == …`
`… != …`
`… is …`
`… isnt …`(NYI) | -| 70 | Bitwise AND | left-to-right | NYI | -| 60 | Bitwise XOR | left-to-right | NYI | -| 50 | Bitwise OR | left-to-right | NYI | +| 70 | Bitwise AND | left-to-right | `… & …` | +| 60 | Bitwise XOR | left-to-right | `… ^ …` | +| 50 | Bitwise OR | left-to-right | `… | …` | | 40 | Logical AND | left-to-right | `… && …` | | 30 | Logical OR | left-to-right | `… \|\| …` | | 20 | Ternary Conditional | right-to-left | NYI | From 44b40cfab9e5a34c1273650c1488a204f564f665 Mon Sep 17 00:00:00 2001 From: Ben Makuh Date: Thu, 12 Nov 2020 21:36:45 -0700 Subject: [PATCH 2/5] add tokens to lexer --- compiler/src/parsing/lexer.mll | 6 ++++++ compiler/src/parsing/parser.dyp | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/compiler/src/parsing/lexer.mll b/compiler/src/parsing/lexer.mll index 55546f7c09..e7de888c99 100644 --- a/compiler/src/parsing/lexer.mll +++ b/compiler/src/parsing/lexer.mll @@ -191,8 +191,12 @@ rule token = parse | "}" { RBRACE } | "[" { LBRACK } | "]" { RBRACK } + | "^" { CARET } | "<" { LCARET } + | "<<" { LLCARET } | ">" { RCARET } + | ">>" { RRCARET } + | ">>>" { RRRCARET } | "^" { CARET } | "++" { PLUSPLUS } | "+" { PLUS } @@ -202,7 +206,9 @@ rule token = parse | "%" { PERCENT } | "<=" { LESSEQ } | ">=" { GREATEREQ } + | "&" { AMP } | "&&" { AMPAMP } + | "|" { PIPE } | "||" { PIPEPIPE } | "!" { NOT } | '"' { read_dquote_str (Buffer.create 16) lexbuf } diff --git a/compiler/src/parsing/parser.dyp b/compiler/src/parsing/parser.dyp index b69495fa00..9c0071a238 100644 --- a/compiler/src/parsing/parser.dyp +++ b/compiler/src/parsing/parser.dyp @@ -20,7 +20,7 @@ include Parser_header %token TYPEID %token STRING %token LBRACK RBRACK LPAREN LPARENNOSPACE RPAREN LBRACE RBRACE LCARET RCARET -%token CARET LCARETLCARET RCARETRCARET RCARETRCARETRCARET +%token CARET LLCARET RRCARET RRRCARET %token COMMA SEMI AS %token THICKARROW ARROW %token IS ISNT EQEQ LESSEQ GREATEREQ @@ -290,13 +290,13 @@ caret_op : lcaret_op : | LCARET { "<" } llcaret_op : - | LCARETLCARET { "<<" } + | LLCARET { "<<" } rcaret_op : | RCARET { ">" } rrcaret_op : - | RCARETRCARET { ">>" } + | RRCARET { ">>" } rrrcaret_op : - | RCARETRCARETRCARET { ">>>" } + | RRRCARET { ">>>" } lesseq_op : | LESSEQ { "<=" } greatereq_op : From 5ee3e3cef9ba7f280cb988e40729aba987844d0f Mon Sep 17 00:00:00 2001 From: Ben Makuh Date: Thu, 12 Nov 2020 21:37:03 -0700 Subject: [PATCH 3/5] add operator sigils to pervasives --- compiler/src/parsing/lexer.mll | 3 --- compiler/src/parsing/parser.dyp | 8 ++++---- stdlib/pervasives.gr | 12 ++++++------ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/compiler/src/parsing/lexer.mll b/compiler/src/parsing/lexer.mll index e7de888c99..2d701c9ae5 100644 --- a/compiler/src/parsing/lexer.mll +++ b/compiler/src/parsing/lexer.mll @@ -193,10 +193,7 @@ rule token = parse | "]" { RBRACK } | "^" { CARET } | "<" { LCARET } - | "<<" { LLCARET } | ">" { RCARET } - | ">>" { RRCARET } - | ">>>" { RRRCARET } | "^" { CARET } | "++" { PLUSPLUS } | "+" { PLUS } diff --git a/compiler/src/parsing/parser.dyp b/compiler/src/parsing/parser.dyp index 9c0071a238..0012dad96a 100644 --- a/compiler/src/parsing/parser.dyp +++ b/compiler/src/parsing/parser.dyp @@ -20,7 +20,7 @@ include Parser_header %token TYPEID %token STRING %token LBRACK RBRACK LPAREN LPARENNOSPACE RPAREN LBRACE RBRACE LCARET RCARET -%token CARET LLCARET RRCARET RRRCARET +%token CARET %token COMMA SEMI AS %token THICKARROW ARROW %token IS ISNT EQEQ LESSEQ GREATEREQ @@ -290,13 +290,13 @@ caret_op : lcaret_op : | LCARET { "<" } llcaret_op : - | LLCARET { "<<" } + | LCARET LCARET { "<<" } rcaret_op : | RCARET { ">" } rrcaret_op : - | RRCARET { ">>" } + | RCARET RCARET { ">>" } rrrcaret_op : - | RRRCARET { ">>>" } + | RCARET RCARET RCARET { ">>>" } lesseq_op : | LESSEQ { "<=" } greatereq_op : diff --git a/stdlib/pervasives.gr b/stdlib/pervasives.gr index c265cfb0d9..baeac8dd6d 100644 --- a/stdlib/pervasives.gr +++ b/stdlib/pervasives.gr @@ -18,13 +18,13 @@ import foreign wasm numberGreaterEqual : (Number, Number) -> Bool as (>=) from ' // Number bit/logical operations import foreign wasm numberLnot : Number -> Number as lnot from 'stdlib-external/runtime' -import foreign wasm numberLand : (Number, Number) -> Number as land from 'stdlib-external/runtime' -import foreign wasm numberLor : (Number, Number) -> Number as lor from 'stdlib-external/runtime' -import foreign wasm numberLxor : (Number, Number) -> Number as lxor from 'stdlib-external/runtime' +import foreign wasm numberLand : (Number, Number) -> Number as (&) from 'stdlib-external/runtime' +import foreign wasm numberLor : (Number, Number) -> Number as (|) from 'stdlib-external/runtime' +import foreign wasm numberLxor : (Number, Number) -> Number as (^) from 'stdlib-external/runtime' -import foreign wasm numberLsl : (Number, Number) -> Number as lsl from 'stdlib-external/runtime' -import foreign wasm numberLsr : (Number, Number) -> Number as lsr from 'stdlib-external/runtime' -import foreign wasm numberAsr : (Number, Number) -> Number as asr from 'stdlib-external/runtime' +import foreign wasm numberLsl : (Number, Number) -> Number as (<<) from 'stdlib-external/runtime' +import foreign wasm numberLsr : (Number, Number) -> Number as (>>>) from 'stdlib-external/runtime' +import foreign wasm numberAsr : (Number, Number) -> Number as (>>) from 'stdlib-external/runtime' // Number coercions & conversions From 9cafe11cf368aee335dafccc3a1e0791cab7ea9c Mon Sep 17 00:00:00 2001 From: Ben Makuh Date: Tue, 17 Nov 2020 10:10:56 -0700 Subject: [PATCH 4/5] add tests for bitwise operators --- compiler/test/test_end_to_end.re | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/compiler/test/test_end_to_end.re b/compiler/test/test_end_to_end.re index 51d1a3b537..b92c44c140 100644 --- a/compiler/test/test_end_to_end.re +++ b/compiler/test/test_end_to_end.re @@ -190,6 +190,24 @@ let basic_functionality_tests = [ t("or2", "true || false", "true"), t("or3", "false || true", "true"), t("or4", "false || false", "false"), + t("land1", "1 & 1", "1"), + t("land2", "1 & 0", "0"), + t("land3", "0 & 1", "0"), + t("land4", "0 & 0", "0"), + t("lor1", "1 | 1", "1"), + t("lor2", "1 | 0", "1"), + t("lor3", "0 | 1", "1"), + t("lor4", "0 | 0", "0"), + t("lxor1", "1 ^ 1", "0"), + t("lxor2", "1 ^ 0", "1"), + t("lxor3", "0 ^ 1", "1"), + t("lxor4", "0 ^ 0", "0"), + t("lsl1", "7 << 1", "14"), + t("lsl2", "0 << 1", "0"), + t("lsr1", "7 >>> 1", "3"), + t("lsr2", "0 >>> 1", "0"), + t("asr1", "179 >> 1", "89"), + t("asr2", "0 >> 1", "0"), t("comp1", "if (2 < 3) {true} else {false}", "true"), te("comp1e", "if (2 < 3) {true} else {3}", "type"), t("comp2", "if (2 <= 3) {true} else {false}", "true"), From 25d7fc667260b27dde82f75356aacbea36862e93 Mon Sep 17 00:00:00 2001 From: Ben Makuh Date: Tue, 17 Nov 2020 10:39:39 -0700 Subject: [PATCH 5/5] fix: operator precedence levels --- compiler/src/parsing/parser.dyp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/src/parsing/parser.dyp b/compiler/src/parsing/parser.dyp index 0012dad96a..91d66cfbc3 100644 --- a/compiler/src/parsing/parser.dyp +++ b/compiler/src/parsing/parser.dyp @@ -128,17 +128,17 @@ binop_expr : | binop_expr(<=p80) isnt_op eols? binop_expr(