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

Implementing unary operations #41

Merged
merged 4 commits into from
Feb 2, 2025

Conversation

JakubSchwenkbeck
Copy link
Collaborator

Motivation

Issue #40 highlighted the absence of support for unary operations in our compiler. Unary operations, such as ! (logical NOT) and - (negation), are fundamental to many programming constructs and must be properly handled to ensure the compiler's correctness and completeness.

This PR addresses the parser-side implementation of unary operations, ensuring they are correctly parsed and integrated into the AST with the appropriate precedence.


Changes

  1. ANTLR Grammar:

    • Added support for unary operations (! and -) with higher precedence than binary operations.
    • Updated the grammar to ensure unary operations are parsed before binary operations, adhering to standard operator precedence rules.
  2. AST:

    • Introduced a new UnaryOp case class to represent unary operations in the AST:
      case class UnaryOp(op: String, expr: Expression) extends Expression
    • This allows the AST to accurately capture unary operations like !flag or -value.
  3. ASTBuilder:

    • Extended the visitExpression method to handle unary operations during the AST construction process.
    • Ensured that unary operations are correctly parsed and transformed into UnaryOp nodes in the AST.

Testing

To validate the changes, a comprehensive test file has been added (currently commented out) that includes examples of all supported unary and binary operations. The test cases cover:

  • Logical NOT (!)
  • Negation (-)
  • Combinations of unary and binary operations (e.g., -12 + 4, !flag && isValid)

Once the implementation is finalized, the test file will be uncommented

Copy link
Owner

@Brofian Brofian left a comment

Choose a reason for hiding this comment

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

Seems good in general. But there is a problem with the added test file:

The file extension is .Java instead of the correct .java. This would hide the invalid (empty) file in the tests, as they explicitly only load files with the .java extension. Please rename the file accordingly and just comment out all the non-working Parts :)

@JakubSchwenkbeck
Copy link
Collaborator Author

Thanks for noticing :)

Copy link
Owner

@Brofian Brofian left a comment

Choose a reason for hiding this comment

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

Thanks!

@Brofian Brofian linked an issue Feb 2, 2025 that may be closed by this pull request
@JakubSchwenkbeck JakubSchwenkbeck merged commit 25fabc7 into Brofian:main Feb 2, 2025
1 check passed
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.

Unary operators
2 participants