From ccfddcc09a1995af53c17540a5839ce425f423fd Mon Sep 17 00:00:00 2001 From: John Date: Sun, 22 Oct 2023 18:30:00 -0500 Subject: [PATCH] Conlang: Add range operators TODO: Limit range operators to at most 2 operands --- grammar.ebnf | 3 ++- libconlang/src/ast.rs | 6 ++++++ libconlang/src/parser.rs | 7 ++++++- libconlang/src/pretty_printer.rs | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/grammar.ebnf b/grammar.ebnf index 6607877..32c9c25 100644 --- a/grammar.ebnf +++ b/grammar.ebnf @@ -17,7 +17,8 @@ Primary = Item | Identifier | Literal (* expression::math *) Ignore = Assign (IgnoreOp Assign )* ; Assign = Compare (AssignOp Compare)* ; -Compare = Logic (CompareOp Logic )* ; +Compare = Range (CompareOp Range )* ; +Range = Logic (RangeOp Logic )* ; Logic = Bitwise (LogicOp Bitwise)* ; Bitwise = Shift (BitwiseOp Shift )* ; Shift = Term (ShiftOp Term )* ; diff --git a/libconlang/src/ast.rs b/libconlang/src/ast.rs index fb0e161..0b572bc 100644 --- a/libconlang/src/ast.rs +++ b/libconlang/src/ast.rs @@ -603,6 +603,12 @@ pub mod expression { /// `^^`: **Non-short-circuiting** logical XOR LogXor, + // Range operators + /// `..`: Exclusive range + RangeExc, + /// `..=`: Inclusive range + RangeInc, + // Comparison operators /// `<`: Less-than Comparison Less, diff --git a/libconlang/src/parser.rs b/libconlang/src/parser.rs index e702543..b83a5c3 100644 --- a/libconlang/src/parser.rs +++ b/libconlang/src/parser.rs @@ -388,7 +388,8 @@ impl Parser { //name operands operators ignore = assign, ignore_op; assign = compare, assign_op; - compare = logic, compare_op; + compare = range, compare_op; + range = logic, range_op; logic = bitwise, logic_op; bitwise = shift, bitwise_op; shift = term, shift_op; @@ -443,6 +444,10 @@ impl Parser { Type::BarBar => LogOr, Type::XorXor => LogXor, } + range_op: { + Type::DotDot => RangeExc, + Type::DotDotEq => RangeInc, + } compare_op: { Type::Lt => Less, Type::LtEq => LessEq, diff --git a/libconlang/src/pretty_printer.rs b/libconlang/src/pretty_printer.rs index 4740804..4e99198 100644 --- a/libconlang/src/pretty_printer.rs +++ b/libconlang/src/pretty_printer.rs @@ -98,6 +98,8 @@ impl Visitor> for Printer { Binary::LogAnd => "&&", Binary::LogOr => "||", Binary::LogXor => "^^", + Binary::RangeExc => "..", + Binary::RangeInc => "..=", Binary::Less => "<", Binary::LessEq => "<=", Binary::Equal => "==",