Skip to content

Commit

Permalink
Add mapping/placeholders for all basic statements
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Apr 17, 2022
1 parent 515939d commit 5adae37
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
55 changes: 54 additions & 1 deletion src/main/java/parser/TreeMappings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */

Expand Down
37 changes: 29 additions & 8 deletions src/main/java/translator/Statements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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")
}
Expand All @@ -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(
Expand All @@ -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.",
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tree/Statement/SwitchBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public SwitchBlocks(SwitchBlock block) {
}
}

public SwitchBlocks(ArrayList<SwitchBlock> blocks) {
this.blocks = blocks;
for (var block : blocks) {
block.parent = this;
}
}

public SwitchBlocks add(SwitchBlock block) {
this.blocks.add(block);
if (block != null) {
Expand Down

0 comments on commit 5adae37

Please sign in to comment.