Add fallback expression handling for bytecode if

This commit is contained in:
sergeych committed 2026-01-25 19:24:27 +03:00
1 parent 6560457e3d
commit c8a8b12dfc
6 files changed
+97 -4

No files matched your search

@@ -26,6 +26,7 @@ import net.sergeych.lyng.obj.BinaryOpRef
import net.sergeych.lyng.obj.BinOp
import net.sergeych.lyng.obj.ConstRef
import net.sergeych.lyng.obj.ObjInt
import net.sergeych.lyng.obj.ObjVoid
import net.sergeych.lyng.obj.toInt
import kotlin.test.Test
import kotlin.test.assertEquals
@@ -68,4 +69,28 @@ class BytecodeVmTest {
val result = BytecodeVm().execute(fn, Scope(), emptyList())
assertEquals(10, result.toInt())
}
@Test
fun ifWithoutElseReturnsVoid() = kotlinx.coroutines.test.runTest {
val cond = ExpressionStatement(
BinaryOpRef(
BinOp.LT,
ConstRef(ObjInt.of(2).asReadonly),
ConstRef(ObjInt.of(1).asReadonly),
),
net.sergeych.lyng.Pos.builtIn
)
val thenStmt = ExpressionStatement(
ConstRef(ObjInt.of(10).asReadonly),
net.sergeych.lyng.Pos.builtIn
)
val ifStmt = IfStatement(cond, thenStmt, null, net.sergeych.lyng.Pos.builtIn)
val fn = BytecodeCompiler().compileStatement("ifNoElse", ifStmt).also {
if (it == null) {
error("bytecode compile failed for ifNoElse")
}
}!!
val result = BytecodeVm().execute(fn, Scope(), emptyList())
assertEquals(ObjVoid, result)
}
}