Issue 7089: ?? operator

This commit is contained in:
Anthony Bullard 2024-12-23 07:00:33 -06:00
parent 57cab7f69a
commit c70ceb4f98
No known key found for this signature in database
7 changed files with 367 additions and 16 deletions

View file

@ -3,7 +3,7 @@ use self::BinOp::*;
use std::cmp::Ordering;
use std::fmt;
const PRECEDENCES: [(BinOp, u8); 16] = [
const PRECEDENCES: [(BinOp, u8); 17] = [
(Caret, 8),
(Star, 7),
(Slash, 7),
@ -11,6 +11,7 @@ const PRECEDENCES: [(BinOp, u8); 16] = [
(Percent, 6),
(Plus, 5),
(Minus, 5),
(DoubleQuestion, 5),
(Pizza, 4),
(Equals, 3),
(NotEquals, 3),
@ -22,7 +23,7 @@ const PRECEDENCES: [(BinOp, u8); 16] = [
(Or, 0),
];
const ASSOCIATIVITIES: [(BinOp, Associativity); 16] = [
const ASSOCIATIVITIES: [(BinOp, Associativity); 17] = [
(Caret, RightAssociative),
(Star, LeftAssociative),
(Slash, LeftAssociative),
@ -30,6 +31,7 @@ const ASSOCIATIVITIES: [(BinOp, Associativity); 16] = [
(Percent, LeftAssociative),
(Plus, LeftAssociative),
(Minus, LeftAssociative),
(DoubleQuestion, LeftAssociative),
(Pizza, LeftAssociative),
(Equals, NonAssociative),
(NotEquals, NonAssociative),
@ -41,7 +43,7 @@ const ASSOCIATIVITIES: [(BinOp, Associativity); 16] = [
(Or, RightAssociative),
];
const DISPLAY_STRINGS: [(BinOp, &str); 16] = [
const DISPLAY_STRINGS: [(BinOp, &str); 17] = [
(Caret, "^"),
(Star, "*"),
(Slash, "/"),
@ -49,6 +51,7 @@ const DISPLAY_STRINGS: [(BinOp, &str); 16] = [
(Percent, "%"),
(Plus, "+"),
(Minus, "-"),
(DoubleQuestion, "??"),
(Pizza, "|>"),
(Equals, "=="),
(NotEquals, "!="),
@ -152,6 +155,7 @@ pub enum BinOp {
Slash,
DoubleSlash,
Percent,
DoubleQuestion,
Plus,
Minus,
Pizza,
@ -172,7 +176,7 @@ impl BinOp {
match self {
Caret | Star | Slash | Percent | Plus | Minus | LessThan | GreaterThan => 1,
DoubleSlash | Equals | NotEquals | LessThanOrEq | GreaterThanOrEq | And | Or
| Pizza => 2,
| Pizza | DoubleQuestion => 2,
}
}
}
@ -206,13 +210,13 @@ pub enum Associativity {
impl BinOp {
pub fn associativity(self) -> Associativity {
const ASSOCIATIVITY_TABLE: [Associativity; 16] = generate_associativity_table();
const ASSOCIATIVITY_TABLE: [Associativity; 17] = generate_associativity_table();
ASSOCIATIVITY_TABLE[self as usize]
}
fn precedence(self) -> u8 {
const PRECEDENCE_TABLE: [u8; 16] = generate_precedence_table();
const PRECEDENCE_TABLE: [u8; 17] = generate_precedence_table();
PRECEDENCE_TABLE[self as usize]
}
@ -232,14 +236,14 @@ impl Ord for BinOp {
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
const DISPLAY_TABLE: [&str; 16] = generate_display_table();
const DISPLAY_TABLE: [&str; 17] = generate_display_table();
write!(f, "{}", DISPLAY_TABLE[*self as usize])
}
}
const fn generate_precedence_table() -> [u8; 16] {
let mut table = [0u8; 16];
const fn generate_precedence_table() -> [u8; 17] {
let mut table = [0u8; 17];
let mut i = 0;
while i < PRECEDENCES.len() {
@ -250,8 +254,8 @@ const fn generate_precedence_table() -> [u8; 16] {
table
}
const fn generate_associativity_table() -> [Associativity; 16] {
let mut table = [NonAssociative; 16];
const fn generate_associativity_table() -> [Associativity; 17] {
let mut table = [NonAssociative; 17];
let mut i = 0;
while i < ASSOCIATIVITIES.len() {
@ -262,8 +266,8 @@ const fn generate_associativity_table() -> [Associativity; 16] {
table
}
const fn generate_display_table() -> [&'static str; 16] {
let mut table = [""; 16];
const fn generate_display_table() -> [&'static str; 17] {
let mut table = [""; 17];
let mut i = 0;
while i < DISPLAY_STRINGS.len() {