From 5adae37ed6c4bc0689ac58764d1c6a8832e016ad Mon Sep 17 00:00:00 2001 From: Nikolai Kudasov Date: Mon, 18 Apr 2022 02:46:48 +0300 Subject: [PATCH] Add mapping/placeholders for all basic statements --- src/main/java/parser/TreeMappings.kt | 55 ++++++++++++++++++- src/main/java/translator/Statements.kt | 37 ++++++++++--- .../java/tree/Statement/SwitchBlocks.java | 7 +++ 3 files changed, 90 insertions(+), 9 deletions(-) diff --git a/src/main/java/parser/TreeMappings.kt b/src/main/java/parser/TreeMappings.kt index b5093a15..aa8dd503 100644 --- a/src/main/java/parser/TreeMappings.kt +++ b/src/main/java/parser/TreeMappings.kt @@ -10,6 +10,7 @@ import tree.Declaration.* import tree.Expression.Expression import tree.Expression.Primary.Literal import tree.Expression.SimpleReference +import tree.Expression.SwitchExpression import tree.Statement.* import tree.Type.* import kotlin.reflect.typeOf @@ -103,11 +104,63 @@ fun LocalTypeDeclarationContext.toDeclaration() : Declaration = fun StatementContext.toStatement() : Statement = when (this) { - is StatementWhileContext -> While(null /* FIXME */, parExpression().expression().toExpression(), statement().toStatement()) + is StatementWhileContext -> While(null /* FIXME */, + parExpression().expression().toExpression(), + statement().toStatement()) + is StatementIfContext -> IfThenElse(null, + parExpression().expression().toExpression(), + statement(0).toStatement(), + statement(1)?.toStatement()) + is StatementExpressionContext -> StatementExpression(null, + expression().toExpression()) + is StatementAssertContext -> Assert(null, + this.expression(0).toExpression(), + this.expression(1)?.toExpression()) + is StatementBreakContext -> Break(null, identifier()?.toToken()) + is StatementContinueContext -> Continue(null, identifier()?.toToken()) + is StatementForContext -> StatementExpression(null, SimpleReference(CompoundName("for_loop_placeholder"))) /* FIXME */ + is StatementBlockLabelContext -> StatementExpression(null, SimpleReference(CompoundName("block_label_placeholder"))) /* FIXME */ + is StatementDoContext -> Do(null, statement().toStatement(), parExpression().expression().toExpression()) + is StatementReturnContext -> Return(null, expression()?.toExpression()) + is StatementIdentifierLabelContext -> StatementExpression(null, SimpleReference(CompoundName("identifier_label_placeholder"))) /* FIXME */ + is StatementSemiContext -> StatementExpression(null, SimpleReference(CompoundName("semi_noop_placeholder"))) /* FIXME */ + is StatementSwitchContext -> StatementExpression(null, SimpleReference(CompoundName("switch_statement_placeholder"))) /* FIXME */ + is StatementSwitchExpressionContext -> StatementExpression(null, switchExpression().toExpression()) + /* Switch(null, + this.parExpression().expression().toExpression(), + SwitchBlocks(ArrayList(switchBlockStatementGroup().map { it.toSwitchBlock() })), + 0 /* FIXME: switchLabels */ + ) */ + is StatementTryResourceSpecificationContext -> StatementExpression(null, SimpleReference(CompoundName("try_resource_placeholder"))) /* FIXME */ + is StatementTryBlockContext -> StatementExpression(null, SimpleReference(CompoundName("try_block_placeholder"))) /* FIXME */ else -> StatementExpression(null, SimpleReference(CompoundName("statement_placeholder"))) /* FIXME */ // else -> throw Exception("Unsupported statement") /* FIXME */ } +fun SwitchExpressionContext.toExpression() : Expression = + SimpleReference(CompoundName("switch_expression_placeholder")) /* FIXME */ + /* FIXME: + SwitchExpression( + parExpression().expression().toExpression(), + ??? + ) */ + +fun SwitchBlockStatementGroupContext.toSwitchBlock() : SwitchBlock = + SwitchBlock( + ArrayList(this.switchLabel().map { it.toSwitchLabel() }), + null + ) + +fun SwitchLabelContext.toSwitchLabel() : SwitchLabel = + SwitchLabel( + constantExpression?.toExpression() + ?: enumConstantName?.toCompoundName()?.toExpression() + ?: null + ) + +fun org.antlr.v4.runtime.Token.toCompoundName() : CompoundName = CompoundName(text) +fun CompoundName.toExpression() : Expression = SimpleReference(this) + fun ExpressionContext.toExpression() : Expression = SimpleReference(CompoundName("statement_placeholder")) /* FIXME */ diff --git a/src/main/java/translator/Statements.kt b/src/main/java/translator/Statements.kt index aab086c6..7fed1714 100644 --- a/src/main/java/translator/Statements.kt +++ b/src/main/java/translator/Statements.kt @@ -5,11 +5,7 @@ import eotree.EOCopy import eotree.EODot import eotree.EOExpr import eotree.eoDot -import tree.Statement.IfThenElse -import tree.Statement.Return -import tree.Statement.Statement -import tree.Statement.StatementExpression -import tree.Statement.While +import tree.Statement.* import util.ParseExprTasks // fun mapBlockStatement(stmt: BlockStatement): EOExpr = @@ -31,6 +27,8 @@ fun mapStatement(parseExprTasks: ParseExprTasks, statement: Statement): EOExpr = is Return -> mapReturnStatement(parseExprTasks, statement) is IfThenElse -> mapIfThenElseStatement(parseExprTasks, statement) is While -> mapWhileStatement(parseExprTasks, statement) + is Do -> mapDoStatement(parseExprTasks, statement) + // is Switch -> mapSwitchStatement(parseExprTasks, statement) else -> throw IllegalArgumentException("Statement of type ${statement.javaClass.simpleName} is not supported") } @@ -40,18 +38,32 @@ fun mapStatementExpression(parseExprTasks: ParseExprTasks, stmtExpr: StatementEx } fun mapReturnStatement(parseExprTasks: ParseExprTasks, rn: Return): EOExpr { - return parseExprTasks.addTask(rn.expression).eoDot() + if (rn.expression != null) { + return parseExprTasks.addTask(rn.expression).eoDot() + } else { + return "return_placeholder".eoDot() + } } fun mapIfThenElseStatement(parseExprTasks: ParseExprTasks, rn: IfThenElse): EOExpr = - EOCopy( + if (rn.elsePart == null) { + EOCopy( + EODot( + Option.fromNullable(mapExpression(parseExprTasks, rn.condition)), + "if" + ), + mapStatement(parseExprTasks, rn.thenPart) + ) + } else { + EOCopy( EODot( Option.fromNullable(mapExpression(parseExprTasks, rn.condition)), "if" ), mapStatement(parseExprTasks, rn.thenPart), mapStatement(parseExprTasks, rn.elsePart) - ) + ) + } fun mapWhileStatement(parseExprTasks: ParseExprTasks, rn: While): EOExpr = EOCopy( @@ -62,6 +74,15 @@ fun mapWhileStatement(parseExprTasks: ParseExprTasks, rn: While): EOExpr = mapStatement(parseExprTasks, rn.statement) ) +fun mapDoStatement(parseExprTasks: ParseExprTasks, rn: Do): EOExpr = + EOCopy( + EODot( + Option.fromNullable(mapExpression(parseExprTasks, rn.condition)), + "while" + ), + mapStatement(parseExprTasks, rn.statement) + ) + // fun mapIfThenElse(statement: IfThenElse): EOExpr = // EOCopy( // "if.", diff --git a/src/main/java/tree/Statement/SwitchBlocks.java b/src/main/java/tree/Statement/SwitchBlocks.java index 26c094de..3482667d 100644 --- a/src/main/java/tree/Statement/SwitchBlocks.java +++ b/src/main/java/tree/Statement/SwitchBlocks.java @@ -16,6 +16,13 @@ public SwitchBlocks(SwitchBlock block) { } } + public SwitchBlocks(ArrayList blocks) { + this.blocks = blocks; + for (var block : blocks) { + block.parent = this; + } + } + public SwitchBlocks add(SwitchBlock block) { this.blocks.add(block); if (block != null) {