Skip to content

Commit

Permalink
feat: add pipe operator support (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
KennedyTedesco committed Jan 20, 2023
1 parent bab4e9b commit b349d8b
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/parser/internal/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::tree::expression::operator::ArrayOperationExpression;
use crate::tree::expression::operator::AssignmentOperationExpression;
use crate::tree::expression::operator::BitwiseOperationExpression;
use crate::tree::expression::operator::ComparisonOperationExpression;
use crate::tree::expression::operator::FunctionalOperationExpression;
use crate::tree::expression::operator::LogicalOperationExpression;
use crate::tree::expression::operator::RangeOperationExpression;
use crate::tree::expression::operator::StringOperationExpression;
Expand Down Expand Up @@ -123,6 +124,32 @@ pub fn infix(
})
}
}
TokenKind::Pipe => {
let left = Box::new(left);

if op.kind == TokenKind::GreaterThan {
let greater_than = op.position;
state.iterator.next();
let right = Box::new(expression::for_precedence(state, right_precedence)?);

Expression::FunctionalOperation(FunctionalOperationExpression::Pipe {
comments,
left,
pipe: position,
greater_than,
right,
})
} else {
let right = Box::new(expression::for_precedence(state, right_precedence)?);

Expression::BitwiseOperation(BitwiseOperationExpression::Or {
comments,
left,
or: position,
right,
})
}
}
_ => {
let left = Box::new(left);
let right = Box::new(expression::for_precedence(state, right_precedence)?);
Expand Down Expand Up @@ -296,12 +323,6 @@ pub fn infix(
right,
})
}
TokenKind::Pipe => Expression::BitwiseOperation(BitwiseOperationExpression::Or {
comments,
left,
or: position,
right,
}),
TokenKind::Caret => Expression::BitwiseOperation(BitwiseOperationExpression::Xor {
comments,
left,
Expand Down
6 changes: 6 additions & 0 deletions src/tree/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::tree::expression::operator::CoalesceOperationExpression;
use crate::tree::expression::operator::ComparisonOperationExpression;
use crate::tree::expression::operator::ExceptionOperationExpression;
use crate::tree::expression::operator::FunctionOperationExpression;
use crate::tree::expression::operator::FunctionalOperationExpression;
use crate::tree::expression::operator::GeneratorOperationExpression;
use crate::tree::expression::operator::LogicalOperationExpression;
use crate::tree::expression::operator::ObjectOperationExpression;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub enum Expression {
Parenthesized(ParenthesizedExpression),
ExitConstruct(ExitConstructExpression),
Literal(Literal),
FunctionalOperation(FunctionalOperationExpression),
ArithmeticOperation(ArithmeticOperationExpression),
AsyncOperation(AsyncOperationExpression),
ArrayOperation(ArrayOperationExpression),
Expand Down Expand Up @@ -314,6 +316,7 @@ impl Node for Expression {
Self::Dict(expression) => expression.initial_position(),
Self::Tuple(expression) => expression.initial_position(),
Self::MagicConstant(expression) => expression.initial_position(),
Self::FunctionalOperation(expression) => expression.initial_position(),
}
}

Expand Down Expand Up @@ -348,6 +351,7 @@ impl Node for Expression {
Self::Dict(expression) => expression.final_position(),
Self::Tuple(expression) => expression.final_position(),
Self::MagicConstant(expression) => expression.final_position(),
Self::FunctionalOperation(expression) => expression.final_position(),
}
}

Expand Down Expand Up @@ -382,6 +386,7 @@ impl Node for Expression {
Self::Dict(expression) => vec![expression],
Self::Tuple(expression) => vec![expression],
Self::MagicConstant(expression) => vec![expression],
Self::FunctionalOperation(expression) => vec![expression],
}
}

Expand Down Expand Up @@ -416,6 +421,7 @@ impl Node for Expression {
Self::Dict(expression) => expression.get_description(),
Self::Tuple(expression) => expression.get_description(),
Self::MagicConstant(expression) => expression.get_description(),
Self::FunctionalOperation(expression) => expression.get_description(),
}
}
}
44 changes: 44 additions & 0 deletions src/tree/expression/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ use crate::tree::utils::CommaSeparated;
use crate::tree::variable::Variable;
use crate::tree::Node;

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum FunctionalOperationExpression {
Pipe {
comments: CommentGroup,
left: Box<Expression>,
pipe: usize,
greater_than: usize,
right: Box<Expression>,
},
}

#[derive(Debug, PartialEq, Eq, Clone, Hash, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum ArithmeticOperationExpression {
Expand Down Expand Up @@ -630,6 +642,38 @@ impl RangeOperationExpression {
}
}

impl Node for FunctionalOperationExpression {
fn comments(&self) -> Option<&CommentGroup> {
match self {
Self::Pipe { comments, .. } => Some(comments),
}
}

fn initial_position(&self) -> usize {
match &self {
Self::Pipe { left, .. } => left.initial_position(),
}
}

fn final_position(&self) -> usize {
match &self {
Self::Pipe { right, .. } => right.final_position(),
}
}

fn children(&self) -> Vec<&dyn Node> {
match &self {
Self::Pipe { left, right, .. } => vec![left.as_ref(), right.as_ref()],
}
}

fn get_description(&self) -> String {
match &self {
Self::Pipe { .. } => "pipe functional operation expression".to_string(),
}
}
}

impl Node for ArithmeticOperationExpression {
fn comments(&self) -> Option<&CommentGroup> {
match &self {
Expand Down
9 changes: 9 additions & 0 deletions tests/samples/0109/code.ara
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function pipe(): void {
$f1 = $a1 |> $b1;

$f2 = $a2 |> $b2 |> $c2;

$f3 = $a3 |> $b3 |> $c3 |> $d3;

$f4 = f1(...) |> f2(...) |> f3(...);
}
Loading

0 comments on commit b349d8b

Please sign in to comment.