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
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions input/operations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package operations;

class Operations{
public static void main(String[] args) {
// Arithmetic operators
int a = 10;
int b = 3;
int sum = a + b; // Addition
int difference = a - b; // Subtraction
int product = a * b; // Multiplication
int quotient = a / b; // Division
// int remainder = a % b; // Modulus
// int inverse = -a;


// Comparison operators
boolean isEqual = (a == b); // Equal to
boolean isNotEqual = (a != b); // Not equal to
boolean isLessThan = (a < b); // Less thans
boolean isLessThanOrEqual = (a <= b); // Less than or equal to
boolean isGreaterThan = (a > b); // Greater than
boolean isGreaterThanOrEqual = (a >= b); // Greater than or equal to

// Logical operators
boolean x = true;
boolean y = false;
boolean andResult = x && y; // Logical AND
boolean orResult = x || y; // Logical OR
//boolean negation = !x;
// Assignment operators
int c = 5;
/* c += 2; // Equivalent to c = c + 2

c -= 1; // Equivalent to c = c - 1

c *= 3; // Equivalent to c = c * 3

c /= 2; // Equivalent to c = c / 2

c %= 2; // Equivalent to c = c % 2
*/
// Mixed example
int result = (a + b) * (c - 1); // Using arithmetic operators
boolean isResultValid = (result > 10) && (result < 50); // Using comparison and logical operators

}

}


3 changes: 2 additions & 1 deletion src/main/antlr4/de/students/antlr/Java.g4
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ breakStatement: 'break' SC;
continueStatement: 'continue' SC;

// Expressions
expression: literal
expression: ('!' | '-') expression // Unary operators with higher precedence
|literal
| primary
| methodCall
| thisAccess
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/de/students/Parser/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ sealed trait Expression extends ASTNode
case class VarRef(name: String) extends Expression
case class Literal(value: Any) extends Expression
case class BinaryOp(left: Expression, op: String, right: Expression) extends Expression

case class UnaryOp(op: String, expr: Expression) extends Expression
case class ThisAccess(name: String) extends Expression
case class ClassAccess(className: String, memberName: String) extends Expression
case class NewObject(className: String, arguments: List[Expression]) extends Expression
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/de/students/Parser/ASTBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ object ASTBuilder {

override def visitExpression(ctx: ExpressionContext): Expression = {
Logger.debug(s"Visiting expression: ${ctx.getText}")
// Handle unary operators (!, -)
if (ctx.getChildCount == 2 && (ctx.getChild(0).getText == "!" || ctx.getChild(0).getText == "-")) {
val op = ctx.getChild(0).getText
val expr = visitExpression(ctx.expression(0)) // Get the nested expression
return UnaryOp(op, expr)
}

if (ctx.literal() != null) {
visitLiteral(ctx.literal())
} else if (ctx.primary() != null) {
Expand Down