Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement parser for arithmetic expressions like a++ #155

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/deno_task_shell/src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,18 @@ logical_and = { "&&" }
logical_or = { "||" }

unary_arithmetic_expr = !{
(unary_arithmetic_op | post_arithmetic_op) ~ (parentheses_expr | VARIABLE | NUMBER) |
unary_pre_arithmetic_expr | unary_post_arithmetic_expr
}

unary_pre_arithmetic_expr = !{
(post_arithmetic_op | pre_arithmetic_op) ~ (parentheses_expr | VARIABLE | NUMBER)
}

unary_post_arithmetic_expr = !{
(parentheses_expr | VARIABLE | NUMBER) ~ post_arithmetic_op
}

unary_arithmetic_op = _{
pre_arithmetic_op= _{
unary_plus | unary_minus | logical_not | bitwise_not
}

Expand Down
150 changes: 111 additions & 39 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,10 @@ pub enum ArithmeticPart {
right: Box<ArithmeticPart>,
},
#[error("Invalid unary arithmetic expression")]
UnaryArithmeticExpr {
UnaryAritheticExpr {
Armd04 marked this conversation as resolved.
Show resolved Hide resolved
operator: UnaryArithmeticOp,
operand: Box<ArithmeticPart>,
},
#[error("Invalid post arithmetic expression")]
PostArithmeticExpr {
operand: Box<ArithmeticPart>,
operator: PostArithmeticOp,
},
#[error("Invalid variable")]
Variable(String),
#[error("Invalid number")]
Expand Down Expand Up @@ -481,7 +476,9 @@ pub enum AssignmentOp {
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum UnaryArithmeticOp {
pub enum PreArithmeticOp {
Increment, // ++
Decrement, // --
Plus, // +
Minus, // -
LogicalNot, // !
Expand All @@ -490,12 +487,20 @@ pub enum UnaryArithmeticOp {

#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum PostArithmeticOp {
Increment, // ++
Decrement, // --
}

#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum UnaryArithmeticOp {
Pre(PreArithmeticOp),
Post(PostArithmeticOp),
}

#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
#[cfg_attr(
feature = "serialization",
Expand Down Expand Up @@ -1419,56 +1424,123 @@ fn parse_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {

fn parse_unary_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
let mut inner = pair.into_inner();
let first = inner.next().unwrap();
let first = inner
.next()
.ok_or_else(|| miette!("Expected unary operator"))?;

match first.as_rule() {
Rule::unary_arithmetic_op => {
let op = parse_unary_arithmetic_op(first)?;
let operand = parse_arithmetic_expr(inner.next().unwrap())?;
Ok(ArithmeticPart::UnaryArithmeticExpr {
operator: op,
operand: Box::new(operand),
})
Rule::unary_pre_arithmetic_expr => unary_pre_arithmetic_expr(first),
Rule::unary_post_arithmetic_expr => unary_post_arithmetic_expr(first),
_ => Err(miette!(
"Unexpected rule in unary arithmetic expression: {:?}",
first.as_rule()
)),
}
}

fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
let mut inner = pair.into_inner();
let first = inner
.next()
.ok_or_else(|| miette!("Expected unary pre operator"))?;
let second = inner.next().ok_or_else(|| miette!("Expected operand"))?;
let operand = match second.as_rule() {
Rule::parentheses_expr => {
let inner = second.into_inner().next().unwrap();
let parts = parse_arithmetic_sequence(inner)?;
Ok(ArithmeticPart::ParenthesesExpr(Box::new(Arithmetic {
parts,
})))
}
Rule::post_arithmetic_op => {
let operand = parse_arithmetic_expr(inner.next().unwrap())?;
let op = parse_post_arithmetic_op(first)?;
Ok(ArithmeticPart::PostArithmeticExpr {
Rule::VARIABLE => Ok(ArithmeticPart::Variable(second.as_str().to_string())),
Rule::NUMBER => Ok(ArithmeticPart::Number(second.as_str().to_string())),
_ => Err(miette!(
"Unexpected rule in arithmetic expression: {:?}",
second.as_rule()
)),
}?;

match first.as_rule() {
Rule::pre_arithmetic_op => {
let op = parse_pre_arithmetic_op(first)?;
Ok(ArithmeticPart::UnaryAritheticExpr {
operator: UnaryArithmeticOp::Pre(op),
operand: Box::new(operand),
operator: op,
})
}
_ => {
let operand = parse_arithmetic_expr(first)?;
let op = parse_post_arithmetic_op(inner.next().unwrap())?;
Ok(ArithmeticPart::PostArithmeticExpr {
Rule::post_arithmetic_op => {
let op = parse_pre_arithmetic_op(first)?;
Ok(ArithmeticPart::UnaryAritheticExpr {
operator: UnaryArithmeticOp::Pre(op),
operand: Box::new(operand),
operator: op,
})
}
_ => Err(miette!(
"Unexpected rule in unary arithmetic operator: {:?}",
first.as_rule()
)),
}
}

fn parse_unary_arithmetic_op(pair: Pair<Rule>) -> Result<UnaryArithmeticOp> {
match pair.as_str() {
"+" => Ok(UnaryArithmeticOp::Plus),
"-" => Ok(UnaryArithmeticOp::Minus),
"!" => Ok(UnaryArithmeticOp::LogicalNot),
"~" => Ok(UnaryArithmeticOp::BitwiseNot),
fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
let mut inner = pair.into_inner();
let first = inner
.next()
.ok_or_else(|| miette!("Expected unary post operator"))?;
let second = inner.next().ok_or_else(|| miette!("Expected operand"))?;

let operand = match first.as_rule() {
Rule::parentheses_expr => {
let inner = first.into_inner().next().unwrap();
let parts = parse_arithmetic_sequence(inner)?;
Ok(ArithmeticPart::ParenthesesExpr(Box::new(Arithmetic {
parts,
})))
}
Rule::VARIABLE => Ok(ArithmeticPart::Variable(first.as_str().to_string())),
Rule::NUMBER => Ok(ArithmeticPart::Number(first.as_str().to_string())),
_ => Err(miette!(
"Unexpected rule in arithmetic expression: {:?}",
first.as_rule()
)),
}?;
let op = parse_post_arithmetic_op(second)?;
Ok(ArithmeticPart::UnaryAritheticExpr {
operator: UnaryArithmeticOp::Post(op),
operand: Box::new(operand),
})
}

fn parse_pre_arithmetic_op(pair: Pair<Rule>) -> Result<PreArithmeticOp> {
let first = pair
.into_inner()
.next()
.ok_or_else(|| miette!("Expected increment or decrement operator"))?;
match first.as_rule() {
Rule::increment => Ok(PreArithmeticOp::Increment),
Rule::decrement => Ok(PreArithmeticOp::Decrement),
Rule::add => Ok(PreArithmeticOp::Plus),
Rule::subtract => Ok(PreArithmeticOp::Minus),
Rule::logical_not => Ok(PreArithmeticOp::LogicalNot),
Rule::bitwise_not => Ok(PreArithmeticOp::BitwiseNot),
_ => Err(miette!(
"Invalid unary arithmetic operator: {}",
pair.as_str()
"Unexpected rule in post arithmetic operator: {:?}",
first.as_rule()
)),
}
}

fn parse_post_arithmetic_op(pair: Pair<Rule>) -> Result<PostArithmeticOp> {
match pair.as_str() {
"++" => Ok(PostArithmeticOp::Increment),
"--" => Ok(PostArithmeticOp::Decrement),
let first = pair
.into_inner()
.next()
.ok_or_else(|| miette!("Expected increment or decrement operator"))?;
match first.as_rule() {
Rule::increment => Ok(PostArithmeticOp::Increment),
Rule::decrement => Ok(PostArithmeticOp::Decrement),
_ => Err(miette!(
"Invalid post arithmetic operator: {}",
pair.as_str()
"Unexpected rule in post arithmetic operator: {:?}",
first.as_rule()
)),
}
}
Expand Down
88 changes: 30 additions & 58 deletions crates/deno_task_shell/src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,23 @@ use thiserror::Error;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use crate::parser::AssignmentOp;
use crate::parser::BinaryOp;
use crate::parser::Condition;
use crate::parser::ConditionInner;
use crate::parser::ElsePart;
use crate::parser::IoFile;
use crate::parser::RedirectOpInput;
use crate::parser::RedirectOpOutput;
use crate::parser::UnaryOp;
use crate::shell::commands::ShellCommand;
use crate::shell::commands::ShellCommandContext;
use crate::shell::types::pipe;
use crate::shell::types::ArithmeticResult;
use crate::shell::types::ArithmeticValue;
use crate::shell::types::EnvChange;
use crate::shell::types::ExecuteResult;
use crate::shell::types::FutureExecuteResult;
use crate::shell::types::ShellPipeReader;
use crate::shell::types::ShellPipeWriter;
use crate::shell::types::ShellState;

use crate::parser::Arithmetic;
use crate::parser::ArithmeticPart;
use crate::parser::BinaryArithmeticOp;
use crate::parser::Command;
use crate::parser::CommandInner;
use crate::parser::IfClause;
use crate::parser::PipeSequence;
use crate::parser::PipeSequenceOperator;
use crate::parser::Pipeline;
use crate::parser::PipelineInner;
use crate::parser::Redirect;
use crate::parser::RedirectFd;
use crate::parser::RedirectOp;
use crate::parser::Sequence;
use crate::parser::SequentialList;
use crate::parser::SimpleCommand;
use crate::parser::UnaryArithmeticOp;
use crate::parser::Word;
use crate::parser::WordPart;
use crate::shell::types::WordEvalResult;
use crate::parser::{
AssignmentOp, BinaryOp, Condition, ConditionInner, ElsePart, IoFile,
RedirectOpInput, RedirectOpOutput, UnaryArithmeticOp, UnaryOp,
};
use crate::shell::commands::{ShellCommand, ShellCommandContext};
use crate::shell::types::{
pipe, ArithmeticResult, ArithmeticValue, EnvChange, ExecuteResult,
FutureExecuteResult, ShellPipeReader, ShellPipeWriter, ShellState,
WordEvalResult,
};

use crate::parser::{
Arithmetic, ArithmeticPart, BinaryArithmeticOp, Command, CommandInner,
IfClause, PipeSequence, PipeSequenceOperator, Pipeline, PipelineInner,
Redirect, RedirectFd, RedirectOp, Sequence, SequentialList, SimpleCommand,
Word, WordPart,
};

use super::command::execute_unresolved_command_name;
use super::command::UnresolvedCommandName;
Expand Down Expand Up @@ -599,7 +575,10 @@ async fn evaluate_arithmetic_part(
}?
}
};
state.apply_env_var(name, &applied_value.to_string());
state.apply_change(&EnvChange::SetShellVar(
(&name).to_string(),
applied_value.value.to_string(),
));
Ok(
applied_value
.clone()
Expand Down Expand Up @@ -639,13 +618,9 @@ async fn evaluate_arithmetic_part(
let rhs = Box::pin(evaluate_arithmetic_part(right, state)).await?;
apply_conditional_binary_op(lhs, operator, rhs)
}
ArithmeticPart::UnaryArithmeticExpr { operator, operand } => {
ArithmeticPart::UnaryAritheticExpr { operator, operand } => {
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
apply_unary_op(*operator, val)
}
ArithmeticPart::PostArithmeticExpr { operand, .. } => {
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
Ok(val)
apply_unary_op(state, *operator, val, operand)
}
ArithmeticPart::Variable(name) => state
.get_var(name)
Expand Down Expand Up @@ -729,19 +704,15 @@ fn apply_conditional_binary_op(
}

fn apply_unary_op(
state: &mut ShellState,
op: UnaryArithmeticOp,
val: ArithmeticResult,
operand: &ArithmeticPart,
) -> Result<ArithmeticResult, Error> {
match op {
UnaryArithmeticOp::Plus => Ok(val),
UnaryArithmeticOp::Minus => val.checked_neg(),
UnaryArithmeticOp::LogicalNot => Ok(if val.is_zero() {
ArithmeticResult::new(ArithmeticValue::Integer(1))
} else {
ArithmeticResult::new(ArithmeticValue::Integer(0))
}),
UnaryArithmeticOp::BitwiseNot => val.checked_not(),
}
let result = val.unary_op(operand, op)?;
let result_clone = result.clone();
state.apply_changes(&result_clone.changes);
Ok(result)
}

async fn execute_pipe_sequence(
Expand Down Expand Up @@ -1350,6 +1321,7 @@ fn evaluate_word_parts(
if !current_text.is_empty() {
result.extend(evaluate_word_text(state, current_text, is_quoted)?);
}
result.with_changes(changes);
Ok(result)
}
.boxed_local()
Expand Down
Loading
Loading