Skip to content

Commit

Permalink
queen throws statement
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jan 11, 2023
1 parent 6824b77 commit 451e686
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/main/java/org/queenlang/transpiler/QueenParseTreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ public QueenStatementNode visitStatement(QueenParser.StatementContext ctx) {
return this.visitEnhancedForStatement(ctx.forStatement().enhancedForStatement());
} else if(ctx.labeledStatement() != null) {
return this.visitLabeledStatement(ctx.labeledStatement());
} else if(ctx.statementWithoutTrailingSubstatement() != null) {
return this.visitStatementWithoutTrailingSubstatement(ctx.statementWithoutTrailingSubstatement());
} else {
return new QueenTextStatementNode(
getPosition(ctx),
Expand Down Expand Up @@ -985,22 +987,32 @@ public QueenLabeledStatementNode visitLabeledStatement(QueenParser.LabeledStatem
public QueenBlockStatements visitStatementWithoutTrailingSubstatement(
QueenParser.StatementWithoutTrailingSubstatementContext ctx
) {
final QueenStatementNode statementWithoutTrailingSubstatement;
if (ctx.block() != null && ctx.block().blockStatements() != null) {
return visitBlockStatements(
ctx.block().blockStatements()
);
} else if(ctx.throwStatement() != null) {
statementWithoutTrailingSubstatement = this.visitThrowStatement(ctx.throwStatement());
} else {
//@todo #49:60min Implement the remaining types of StatementWithoutTrailingSubstatement
return new QueenBlockStatements(
//@todo #63:60min Please implement the remaining types of StatementWithoutTrailingSubstatement
statementWithoutTrailingSubstatement = new QueenTextStatementNode(
getPosition(ctx),
List.of(
new QueenTextStatementNode(
getPosition(ctx),
asString(ctx)
)
)
asString(ctx)
);
}
return new QueenBlockStatements(
getPosition(ctx),
List.of(statementWithoutTrailingSubstatement)
);
}

@Override
public QueenThrowStatementNode visitThrowStatement(QueenParser.ThrowStatementContext ctx) {
return new QueenThrowStatementNode(
getPosition(ctx),
this.visitExpression(ctx.expression())
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @version $Id$
* @since 0.0.1
*/
public final class QueenBlockStatements implements QueenNode, Iterable<QueenBlockStatementNode> {
public final class QueenBlockStatements implements QueenStatementNode, Iterable<QueenBlockStatementNode> {

private final Position position;
private final List<QueenBlockStatementNode> blockStatements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.nodeTypes.NodeWithCondition;
import com.github.javaparser.ast.stmt.ThrowStmt;

public final class QueenTextExpressionNode implements QueenExpressionNode {

Expand All @@ -23,6 +24,8 @@ public void addToJavaNode(final Node java) {
variableDeclarator.setInitializer(this.toJavaExpression());
} else if (java instanceof NodeWithCondition) {
((NodeWithCondition) java).setCondition(this.toJavaExpression());
} else if(java instanceof ThrowStmt) {
((ThrowStmt) java).setExpression(this.toJavaExpression());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.queenlang.transpiler.nodes;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ThrowStmt;

/**
* Queen Throw AST Node.
* @author Mihai Andronache ([email protected])
* @version $Id$
* @since 0.0.1
*/
public final class QueenThrowStatementNode implements QueenStatementNode {

private final Position position;

private final QueenExpressionNode expression;

public QueenThrowStatementNode(
final Position position,
final QueenExpressionNode expression
) {
this.position = position;
this.expression = expression;
}

@Override
public void addToJavaNode(final Node java) {
ThrowStmt throwStmt = new ThrowStmt();
this.expression.addToJavaNode(throwStmt);
((BlockStmt) java).addStatement(throwStmt);
}

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

2 comments on commit 451e686

@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 [#64] for the newly added to-dos.

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

@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 closed the Issues [#63] since their to-dos disappeared from the code.

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

Please sign in to comment.