Skip to content

Commit

Permalink
visiting binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jan 31, 2023
1 parent 80d66e3 commit 07a7f85
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 3 deletions.
221 changes: 219 additions & 2 deletions src/main/java/org/queenlang/transpiler/QueenParseTreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ public QueenExpressionNode visitAssignmentExpression(QueenParser.AssignmentExpre
if(ctx.assignment() != null) {
return this.visitAssignment(ctx.assignment());
} else {
return new QueenTextExpressionNode(getPosition(ctx), asString(ctx));
return this.visitConditionalExpression(ctx.conditionalExpression());
// return new QueenTextExpressionNode(getPosition(ctx), asString(ctx));
}
}

Expand All @@ -639,7 +640,7 @@ public QueenExpressionNode visitPrimary(QueenParser.PrimaryContext ctx) {
);
}
//@todo #63:60min finish visitParymary.
return null;
return new QueenTextExpressionNode(getPosition(ctx), asString(ctx));
}

@Override
Expand Down Expand Up @@ -1950,6 +1951,222 @@ public QueenExpressionNode visitUnaryExpression(QueenParser.UnaryExpressionConte
}
}

@Override
public QueenExpressionNode visitMultiplicativeExpression(QueenParser.MultiplicativeExpressionContext ctx) {
if(ctx.MUL() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitMultiplicativeExpression(ctx.multiplicativeExpression()),
ctx.MUL().getText(),
this.visitUnaryExpression(ctx.unaryExpression())
);
} else if(ctx.DIV() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitMultiplicativeExpression(ctx.multiplicativeExpression()),
ctx.DIV().getText(),
this.visitUnaryExpression(ctx.unaryExpression())
);
} else if(ctx.MOD() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitMultiplicativeExpression(ctx.multiplicativeExpression()),
ctx.MOD().getText(),
this.visitUnaryExpression(ctx.unaryExpression())
);
} else {
return this.visitUnaryExpression(ctx.unaryExpression());
}
}

@Override
public QueenExpressionNode visitAdditiveExpression(QueenParser.AdditiveExpressionContext ctx) {
if(ctx.ADD() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitAdditiveExpression(ctx.additiveExpression()),
ctx.ADD().getText(),
this.visitMultiplicativeExpression(ctx.multiplicativeExpression())
);
} else if(ctx.SUB() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitAdditiveExpression(ctx.additiveExpression()),
ctx.SUB().getText(),
this.visitMultiplicativeExpression(ctx.multiplicativeExpression())
);
} else {
return this.visitMultiplicativeExpression(ctx.multiplicativeExpression());
}
}

@Override
public QueenExpressionNode visitShiftExpression(QueenParser.ShiftExpressionContext ctx) {
if(ctx.GT() != null && ctx.GT().size() == 3) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitShiftExpression(ctx.shiftExpression()),
">>>",
this.visitAdditiveExpression(ctx.additiveExpression())
);
} else if(ctx.GT() != null && ctx.GT().size() == 2) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitShiftExpression(ctx.shiftExpression()),
">>",
this.visitAdditiveExpression(ctx.additiveExpression())
);
} else if(ctx.LT() != null && ctx.LT().size() == 2) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitShiftExpression(ctx.shiftExpression()),
"<<",
this.visitAdditiveExpression(ctx.additiveExpression())
);
} else {
return this.visitAdditiveExpression(ctx.additiveExpression());
}
}

@Override
public QueenExpressionNode visitRelationalExpression(QueenParser.RelationalExpressionContext ctx) {
if(ctx.LT() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitRelationalExpression(ctx.relationalExpression()),
ctx.LT().getText(),
this.visitShiftExpression(ctx.shiftExpression())
);
} else if(ctx.GT() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitRelationalExpression(ctx.relationalExpression()),
ctx.GT().getText(),
this.visitShiftExpression(ctx.shiftExpression())
);
} else if(ctx.LE() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitRelationalExpression(ctx.relationalExpression()),
ctx.LE().getText(),
this.visitShiftExpression(ctx.shiftExpression())
);
} else if(ctx.GE() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitRelationalExpression(ctx.relationalExpression()),
ctx.GE().getText(),
this.visitShiftExpression(ctx.shiftExpression())
);
} else if(ctx.INSTANCEOF() != null) {
//@todo #63:60min Implement InstanceofExpressionNode for Queen.
return null;
} else {
return this.visitShiftExpression(ctx.shiftExpression());
}
}

@Override
public QueenExpressionNode visitEqualityExpression(QueenParser.EqualityExpressionContext ctx) {
if(ctx.EQUAL() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitEqualityExpression(ctx.equalityExpression()),
ctx.EQUAL().getText(),
this.visitRelationalExpression(ctx.relationalExpression())
);
} else if(ctx.NOTEQUAL() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitEqualityExpression(ctx.equalityExpression()),
ctx.NOTEQUAL().getText(),
this.visitRelationalExpression(ctx.relationalExpression())
);
} else {
return this.visitRelationalExpression(ctx.relationalExpression());
}
}

@Override
public QueenExpressionNode visitAndExpression(QueenParser.AndExpressionContext ctx) {
if(ctx.BITAND() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitAndExpression(ctx.andExpression()),
ctx.BITAND().getText(),
this.visitEqualityExpression(ctx.equalityExpression())
);
} else {
return this.visitEqualityExpression(ctx.equalityExpression());
}
}

@Override
public QueenExpressionNode visitExclusiveOrExpression(final QueenParser.ExclusiveOrExpressionContext ctx) {
if(ctx.CARET() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitExclusiveOrExpression(ctx.exclusiveOrExpression()),
ctx.CARET().getText(),
this.visitAndExpression(ctx.andExpression())
);
} else {
return this.visitAndExpression(ctx.andExpression());
}
}

@Override
public QueenExpressionNode visitInclusiveOrExpression(final QueenParser.InclusiveOrExpressionContext ctx) {
if(ctx.BITOR() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitInclusiveOrExpression(ctx.inclusiveOrExpression()),
ctx.BITOR().getText(),
this.visitExclusiveOrExpression(ctx.exclusiveOrExpression())
);
} else {
return this.visitExclusiveOrExpression(ctx.exclusiveOrExpression());
}
}

@Override
public QueenExpressionNode visitConditionalAndExpression(final QueenParser.ConditionalAndExpressionContext ctx) {
if(ctx.AND() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitConditionalAndExpression(ctx.conditionalAndExpression()),
ctx.AND().getText(),
this.visitInclusiveOrExpression(ctx.inclusiveOrExpression())
);
} else {
return this.visitInclusiveOrExpression(ctx.inclusiveOrExpression());
}
}

@Override
public QueenExpressionNode visitConditionalOrExpression(final QueenParser.ConditionalOrExpressionContext ctx) {
if(ctx.OR() != null) {
return new QueenBinaryExpressionNode(
getPosition(ctx),
this.visitConditionalOrExpression(ctx.conditionalOrExpression()),
ctx.OR().getText(),
this.visitConditionalAndExpression(ctx.conditionalAndExpression())
);
} else {
return this.visitConditionalAndExpression(ctx.conditionalAndExpression());
}
}

@Override
public QueenExpressionNode visitConditionalExpression(final QueenParser.ConditionalExpressionContext ctx) {
if(ctx.QUESTION() != null) {
//@todo #63:60min Implement ConditionalTernaryExpression node for Queen.
return null;
} else {
return this.visitConditionalOrExpression(ctx.conditionalOrExpression());
}
}

@Override
public QueenExpressionNode visitPreIncrementExpression(QueenParser.PreIncrementExpressionContext ctx) {
return new QueenUnaryExpressionNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Expression toJavaExpression() {
}
}
if(operator == null) {
throw new IllegalStateException("Unkown assignment operator: " + this.operator);
throw new IllegalStateException("Unknown assignment operator: " + this.operator);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2022-2023, Extremely Distributed Technologies S.R.L. Romania
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.queenlang.transpiler.nodes;

import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.Expression;

/**
* Queen Binary Expression, AST Node.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 0.0.01
*/
public final class QueenBinaryExpressionNode implements QueenExpressionNode {
private final Position position;
private final QueenExpressionNode left;
private final String operator;
private final QueenExpressionNode right;

public QueenBinaryExpressionNode(
final Position position,
final QueenExpressionNode left,
final String operator,
final QueenExpressionNode right
) {
this.position = position;
this.left = left;
this.operator = operator;
this.right = right;
}

@Override
public Expression toJavaExpression() {
BinaryExpr.Operator operator = null;
for(int i=0; i< BinaryExpr.Operator.values().length; i++) {
if(BinaryExpr.Operator.values()[i].asString().equalsIgnoreCase(this.operator)) {
operator = BinaryExpr.Operator.values()[i];
break;
}
}
if(operator == null) {
throw new IllegalStateException("Unknown operator: " + this.operator);
}
return new BinaryExpr(
this.left.toJavaExpression(),
this.right.toJavaExpression(),
operator
);
}

@Override
public Position position() {
return this.position;
}
}

1 comment on commit 07a7f85

@zoeself
Copy link
Collaborator

Choose a reason for hiding this comment

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

@amihaiemil I've opened the Issues [#71, #72] for the newly added to-dos.

The to-dos may have been added in an earlier commit, but I've found them just now.

Please sign in to comment.