Skip to content

Commit

Permalink
Skip emitting when expression in ExpressionStatement is null, fix #27
Browse files Browse the repository at this point in the history
  • Loading branch information
ChAoSUnItY committed Mar 12, 2022
1 parent 53d5890 commit a903a1f
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/main/kotlin/org/casc/lang/emitter/Emitter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ class Emitter(private val preference: AbstractPreference) {
}

private fun emitStatement(methodVisitor: MethodVisitor, statement: Statement) {
val label = Label()
if (statement.pos != null) {
val label = Label()

methodVisitor.visitLabel(label)
methodVisitor.visitLineNumber(statement.pos?.lineNumber!!, label)
methodVisitor.visitLabel(label)
methodVisitor.visitLineNumber(statement.pos?.lineNumber!!, label)
}

when (statement) {
is VariableDeclaration -> {
Expand Down Expand Up @@ -148,7 +150,7 @@ class Emitter(private val preference: AbstractPreference) {
}
}
is JForStatement -> {
if (statement.initStatement != null && statement.initStatement is ExpressionStatement && statement.initStatement.expression != null)
if (statement.initStatement != null)
emitStatement(methodVisitor, statement.initStatement)

val startLabel = Label()
Expand Down Expand Up @@ -176,17 +178,17 @@ class Emitter(private val preference: AbstractPreference) {
}
}
is ExpressionStatement -> {
val expression = statement.expression!!

emitExpression(methodVisitor, expression)

if (expression is FunctionCallExpression) {
// Check if function's return value is unused, if yes, then add pop or pop2 opcode
if (expression.type == PrimitiveType.I64 || expression.type == PrimitiveType.F64) methodVisitor.visitInsn(
Opcodes.POP2
)
else if (expression.type != PrimitiveType.Unit) methodVisitor.visitInsn(Opcodes.POP)
} else if (expression is ConstructorCallExpression) methodVisitor.visitInsn(Opcodes.POP)
statement.expression?.let {
emitExpression(methodVisitor, it)

if (it is FunctionCallExpression) {
// Check if function's return value is unused, if yes, then add pop or pop2 opcode
if (it.type == PrimitiveType.I64 || it.type == PrimitiveType.F64) methodVisitor.visitInsn(
Opcodes.POP2
)
else if (it.type != PrimitiveType.Unit) methodVisitor.visitInsn(Opcodes.POP)
} else if (it is ConstructorCallExpression) methodVisitor.visitInsn(Opcodes.POP)
}
}
is ReturnStatement -> {
emitExpression(methodVisitor, statement.expression!!)
Expand Down

0 comments on commit a903a1f

Please sign in to comment.