Skip to content

Conversation

@Armd04
Copy link

@Armd04 Armd04 commented Sep 20, 2024

No description provided.

Copy link
Contributor

@prsabahrami prsabahrami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

I also think cases such as echo $(++2) are being parsed and created a part of the AST. I think we can raise error for this while creating the AST.

}


unary_arithmetic_op = _{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change unary_arithmetic_op to pre_arithmetic_op for better clarity?

}
Rule::unary_pre_arithmetic_expr => unary_pre_arithmetic_expr(first),
Rule::unary_post_arithmetic_expr => unary_post_arithmetic_expr(first),
_ => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this result in an error given that unary_arithmetic_expr can only be one of the unary_pre_arithmetic_expr or unary_post_arithmetic_expr?


fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
let mut inner = pair.into_inner();
let first = inner.next().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to replace unwraps with proper error handling.

Comment on lines 1438 to 1444
fn parse_unary_arithmetic_op_type(pair: Pair<Rule>) -> Result<bool> {
match pair.as_rule() {
Rule::unary_arithmetic_op => Ok(true),
Rule::post_arithmetic_op => Ok(false),
_ => Err(miette!(
"Unexpected rule in unary arithmetic operator: {:?}",
pair.as_rule()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels fairly hacky to return true of false for the type. Maybe you can try replacing this with match statement in unary_pre_arithmetic_expr?

@Armd04
Copy link
Author

Armd04 commented Sep 21, 2024

I also think cases such as echo $(++2) are being parsed and created a part of the AST. I think we can raise error for this while creating the AST.

I believe in such examples, bash raises an error while compiling. I thnik it would be better to do the same here as well.

@Armd04 Armd04 marked this pull request as ready for review September 24, 2024 22:13
@Armd04 Armd04 changed the title WIP Implement parser for arithmetic expressions like a++ Implement parser for arithmetic expressions like a++ Sep 24, 2024
@Armd04 Armd04 requested a review from prsabahrami September 24, 2024 23:20
@Armd04 Armd04 requested a review from prsabahrami September 26, 2024 17:28
@prsabahrami
Copy link
Contributor

@Armd04 Can you please take care of the issues on Windows?

wolfv pushed a commit that referenced this pull request Nov 11, 2025
Merge branch 'pr-155' to add support for unary arithmetic operators
including increment (++) and decrement (--) in both prefix and postfix forms.

Resolved conflicts in:
- parser.rs: Unified UnaryArithmeticExpr structure with Pre/Post variants
- execute.rs: Updated evaluation logic for new operator structure
- types.rs: Added unary_op method for pre/post increment/decrement

The implementation now supports expressions like:
- ++a (pre-increment)
- a++ (post-increment)
- --a (pre-decrement)
- a-- (post-decrement)
wolfv pushed a commit that referenced this pull request Nov 11, 2025
Fixed the following issues raised in code review:

1. Added validation to reject increment/decrement on literal numbers
   - Now correctly errors on expressions like `echo $(++2)`
   - Operators can only be applied to variables or parenthesized expressions

2. Replaced .unwrap() calls with proper error handling
   - Fixed unwrap() in unary_pre_arithmetic_expr (line 1738)
   - Fixed unwrap() in unary_post_arithmetic_expr (line 1783)
   - Used ok_or_else() with descriptive error messages

3. Fixed missing parse_variable_expansion function
   - Restored function that was accidentally removed during merge
   - Fixes compilation errors

4. Fixed stray conflict marker in types.rs
   - Removed leftover "<<<<<<< HEAD" marker

All tests pass and manual testing confirms:
- Post-increment (a++) returns original value, updates variable
- Pre-increment (++a) returns new value, updates variable
- Invalid expressions like ++2 correctly produce errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants