pipes
This commit is contained in:
@@ -1037,6 +1037,35 @@ pub const Parser = struct {
|
||||
var lhs = initial_lhs;
|
||||
|
||||
while (true) {
|
||||
// Pipe operator: desugar a |> f(args) → f(a, args), a |> f → f(a)
|
||||
if (self.current.tag == .pipe_arrow and Prec.pipe >= min_prec) {
|
||||
self.advance();
|
||||
// Parse the RHS as a full call expression (higher precedence)
|
||||
const rhs = try self.parseBinary(Prec.pipe + 1);
|
||||
// Desugar based on RHS shape
|
||||
if (rhs.data == .call) {
|
||||
// a |> f(args) → f(a, args...)
|
||||
var new_args = std.ArrayList(*Node).empty;
|
||||
try new_args.append(self.allocator, lhs);
|
||||
for (rhs.data.call.args) |arg| {
|
||||
try new_args.append(self.allocator, arg);
|
||||
}
|
||||
lhs = try self.createNode(lhs.span.start, .{ .call = .{
|
||||
.callee = rhs.data.call.callee,
|
||||
.args = try new_args.toOwnedSlice(self.allocator),
|
||||
} });
|
||||
} else {
|
||||
// a |> f → f(a)
|
||||
const args = try self.allocator.alloc(*Node, 1);
|
||||
args[0] = lhs;
|
||||
lhs = try self.createNode(lhs.span.start, .{ .call = .{
|
||||
.callee = rhs,
|
||||
.args = args,
|
||||
} });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const prec = self.binaryPrec();
|
||||
if (prec == 0 or prec < min_prec) break;
|
||||
|
||||
@@ -1842,15 +1871,16 @@ pub const Parser = struct {
|
||||
|
||||
const Prec = struct {
|
||||
const none: u8 = 0;
|
||||
const logical_or: u8 = 1; // or
|
||||
const logical_and: u8 = 2; // and
|
||||
const bit_or: u8 = 3; // |
|
||||
const bit_xor: u8 = 4; // ^
|
||||
const bit_and: u8 = 5; // &
|
||||
const comparison: u8 = 6; // == != < <= > >= in
|
||||
const shift: u8 = 7; // << >>
|
||||
const additive: u8 = 8; // + -
|
||||
const multiplicative: u8 = 9; // * / %
|
||||
const pipe: u8 = 1; // |>
|
||||
const logical_or: u8 = 2; // or
|
||||
const logical_and: u8 = 3; // and
|
||||
const bit_or: u8 = 4; // |
|
||||
const bit_xor: u8 = 5; // ^
|
||||
const bit_and: u8 = 6; // &
|
||||
const comparison: u8 = 7; // == != < <= > >= in
|
||||
const shift: u8 = 8; // << >>
|
||||
const additive: u8 = 9; // + -
|
||||
const multiplicative: u8 = 10; // * / %
|
||||
};
|
||||
|
||||
fn binaryPrec(self: *const Parser) u8 {
|
||||
|
||||
Reference in New Issue
Block a user