From 451e6868829841f99075adb6aa01f76f35f68ebe Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Wed, 11 Jan 2023 20:55:16 +0200 Subject: [PATCH] queen throws statement --- .../transpiler/QueenParseTreeVisitor.java | 28 ++++++++++---- .../nodes/QueenBlockStatements.java | 2 +- .../nodes/QueenTextExpressionNode.java | 3 ++ .../nodes/QueenThrowStatementNode.java | 38 +++++++++++++++++++ 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/queenlang/transpiler/nodes/QueenThrowStatementNode.java diff --git a/src/main/java/org/queenlang/transpiler/QueenParseTreeVisitor.java b/src/main/java/org/queenlang/transpiler/QueenParseTreeVisitor.java index 17e46f7..8a019d1 100644 --- a/src/main/java/org/queenlang/transpiler/QueenParseTreeVisitor.java +++ b/src/main/java/org/queenlang/transpiler/QueenParseTreeVisitor.java @@ -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), @@ -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 diff --git a/src/main/java/org/queenlang/transpiler/nodes/QueenBlockStatements.java b/src/main/java/org/queenlang/transpiler/nodes/QueenBlockStatements.java index 6e70f07..6effb14 100644 --- a/src/main/java/org/queenlang/transpiler/nodes/QueenBlockStatements.java +++ b/src/main/java/org/queenlang/transpiler/nodes/QueenBlockStatements.java @@ -39,7 +39,7 @@ * @version $Id$ * @since 0.0.1 */ -public final class QueenBlockStatements implements QueenNode, Iterable { +public final class QueenBlockStatements implements QueenStatementNode, Iterable { private final Position position; private final List blockStatements; diff --git a/src/main/java/org/queenlang/transpiler/nodes/QueenTextExpressionNode.java b/src/main/java/org/queenlang/transpiler/nodes/QueenTextExpressionNode.java index 88399a6..519c980 100644 --- a/src/main/java/org/queenlang/transpiler/nodes/QueenTextExpressionNode.java +++ b/src/main/java/org/queenlang/transpiler/nodes/QueenTextExpressionNode.java @@ -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 { @@ -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()); } } diff --git a/src/main/java/org/queenlang/transpiler/nodes/QueenThrowStatementNode.java b/src/main/java/org/queenlang/transpiler/nodes/QueenThrowStatementNode.java new file mode 100644 index 0000000..eb49b7a --- /dev/null +++ b/src/main/java/org/queenlang/transpiler/nodes/QueenThrowStatementNode.java @@ -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 (amihaiemil@gmail.com) + * @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; + } +}