Conlang: Add range operators

TODO: Limit range operators to at most 2 operands
This commit is contained in:
John 2023-10-22 18:30:00 -05:00
parent b5abd2bff1
commit ccfddcc09a
4 changed files with 16 additions and 2 deletions

View File

@ -17,7 +17,8 @@ Primary = Item | Identifier | Literal
(* expression::math *) (* expression::math *)
Ignore = Assign (IgnoreOp Assign )* ; Ignore = Assign (IgnoreOp Assign )* ;
Assign = Compare (AssignOp Compare)* ; Assign = Compare (AssignOp Compare)* ;
Compare = Logic (CompareOp Logic )* ; Compare = Range (CompareOp Range )* ;
Range = Logic (RangeOp Logic )* ;
Logic = Bitwise (LogicOp Bitwise)* ; Logic = Bitwise (LogicOp Bitwise)* ;
Bitwise = Shift (BitwiseOp Shift )* ; Bitwise = Shift (BitwiseOp Shift )* ;
Shift = Term (ShiftOp Term )* ; Shift = Term (ShiftOp Term )* ;

View File

@ -603,6 +603,12 @@ pub mod expression {
/// `^^`: **Non-short-circuiting** logical XOR /// `^^`: **Non-short-circuiting** logical XOR
LogXor, LogXor,
// Range operators
/// `..`: Exclusive range
RangeExc,
/// `..=`: Inclusive range
RangeInc,
// Comparison operators // Comparison operators
/// `<`: Less-than Comparison /// `<`: Less-than Comparison
Less, Less,

View File

@ -388,7 +388,8 @@ impl Parser {
//name operands operators //name operands operators
ignore = assign, ignore_op; ignore = assign, ignore_op;
assign = compare, assign_op; assign = compare, assign_op;
compare = logic, compare_op; compare = range, compare_op;
range = logic, range_op;
logic = bitwise, logic_op; logic = bitwise, logic_op;
bitwise = shift, bitwise_op; bitwise = shift, bitwise_op;
shift = term, shift_op; shift = term, shift_op;
@ -443,6 +444,10 @@ impl Parser {
Type::BarBar => LogOr, Type::BarBar => LogOr,
Type::XorXor => LogXor, Type::XorXor => LogXor,
} }
range_op: {
Type::DotDot => RangeExc,
Type::DotDotEq => RangeInc,
}
compare_op: { compare_op: {
Type::Lt => Less, Type::Lt => Less,
Type::LtEq => LessEq, Type::LtEq => LessEq,

View File

@ -98,6 +98,8 @@ impl<W: Write> Visitor<IOResult<()>> for Printer<W> {
Binary::LogAnd => "&&", Binary::LogAnd => "&&",
Binary::LogOr => "||", Binary::LogOr => "||",
Binary::LogXor => "^^", Binary::LogXor => "^^",
Binary::RangeExc => "..",
Binary::RangeInc => "..=",
Binary::Less => "<", Binary::Less => "<",
Binary::LessEq => "<=", Binary::LessEq => "<=",
Binary::Equal => "==", Binary::Equal => "==",