Compare commits

...

2 Commits

Author SHA1 Message Date
8a560f5417 fixed interpolation bug 2026-03-29 12:35:10 +03:00
966b6a31ae extended inference of fun return type 2026-03-29 03:57:02 +03:00
3 changed files with 58 additions and 12 deletions

View File

@ -172,6 +172,7 @@ class Compiler(
private val callReturnTypeDeclByRef: MutableMap<CallRef, TypeDecl> = mutableMapOf()
private val callableReturnTypeByScopeId: MutableMap<Int, MutableMap<Int, ObjClass>> = mutableMapOf()
private val callableReturnTypeByName: MutableMap<String, ObjClass> = mutableMapOf()
private val callableReturnTypeDeclByName: MutableMap<String, TypeDecl> = mutableMapOf()
private val lambdaReturnTypeByRef: MutableMap<ObjRef, ObjClass> = mutableMapOf()
private val lambdaCaptureEntriesByRef: MutableMap<ValueFnRef, List<net.sergeych.lyng.bytecode.LambdaCaptureEntry>> =
mutableMapOf()
@ -4313,14 +4314,17 @@ class Compiler(
is MapLiteralRef -> inferMapLiteralTypeDecl(ref)
is ConstRef -> inferTypeDeclFromConst(ref.constValue)
is CallRef -> {
inferCallReturnClass(ref)?.let { TypeDecl.Simple(it.className, false) }
?: run {
val targetName = when (val target = ref.target) {
is LocalVarRef -> target.name
is FastLocalVarRef -> target.name
is LocalSlotRef -> target.name
else -> null
}
if (targetName != null) {
callableReturnTypeDeclByName[targetName]?.let { return it }
}
inferCallReturnClass(ref)?.let { TypeDecl.Simple(it.className, false) }
?: run {
if (targetName != null && targetName.firstOrNull()?.isUpperCase() == true) {
TypeDecl.Simple(targetName, false)
} else {
@ -8157,6 +8161,10 @@ class Compiler(
inferredReturnClass
}
}
val inferredReturnDecl = returnTypeDecl ?: inferredReturnClass?.let { TypeDecl.Simple(it.className, false) }
if (declKind != SymbolKind.MEMBER && inferredReturnDecl != null) {
callableReturnTypeDeclByName[name] = inferredReturnDecl
}
val fnStatements = rawFnStatements?.let { stmt ->
if (!compileBytecode) return@let stmt
val paramKnownClasses = mutableMapOf<String, ObjClass>()

View File

@ -732,7 +732,8 @@ private class Parser(fromPos: Pos, private val interpolationEnabled: Boolean = t
}
val expanded = mutableListOf<Token>()
expanded += Token("(", tokenPos, Token.Type.LPAREN)
var emittedPieces = 0
expanded += Token("", tokenPos, Token.Type.STRING)
var emittedPieces = 1
for (chunk in chunks) {
val pieceTokens = when (chunk) {
is StringChunk.Literal -> {
@ -755,9 +756,6 @@ private class Parser(fromPos: Pos, private val interpolationEnabled: Boolean = t
expanded += pieceTokens
emittedPieces++
}
if (emittedPieces == 0) {
expanded += Token("", tokenPos, Token.Type.STRING)
}
expanded += Token(")", tokenPos, Token.Type.RPAREN)
val first = expanded.first()
@ -766,10 +764,21 @@ private class Parser(fromPos: Pos, private val interpolationEnabled: Boolean = t
}
private fun parseEmbeddedExpressionTokens(text: String, exprPos: Pos): List<Token> {
val tokens = parseLyng(Source(exprPos.source.fileName, text), interpolationEnabled)
val tokens = try {
parseLyng(Source(exprPos.source.fileName, text), interpolationEnabled)
} catch (e: ScriptError) {
throw ScriptError(remapEmbeddedPos(exprPos, e.pos), e.errorMessage, e)
}
if (tokens.isEmpty()) return emptyList()
val withoutEof = if (tokens.last().type == Token.Type.EOF) tokens.dropLast(1) else tokens
return withoutEof
return withoutEof.map { it.copy(pos = remapEmbeddedPos(exprPos, it.pos)) }
}
private fun remapEmbeddedPos(exprPos: Pos, embeddedPos: Pos): Pos {
if (embeddedPos.line < 0 || embeddedPos.column < 0) return exprPos
val line = exprPos.line + embeddedPos.line
val column = if (embeddedPos.line == 0) exprPos.column + embeddedPos.column else embeddedPos.column
return Pos(exprPos.source, line, column)
}
private fun readInterpolationExprText(start: Pos): String {

View File

@ -21,6 +21,7 @@ import com.ionspin.kotlin.bignum.decimal.BigDecimal
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class DecimalModuleTest {
@Test
@ -266,4 +267,32 @@ class DecimalModuleTest {
assert(X == 42)
""".trimIndent())
}
@Test
fun testDecimalStringInterpolation() = runTest {
eval(
$$"""
import lyng.decimal
var X = "2".d
var re = 50.d
var im = X
val s = "$re + ${im}i"
assertEquals("50 + 2i", s)
""".trimIndent())
}
@Test
fun testDecimalInterpolationSyntaxErrorKeepsOriginalSourcePosition() = runTest {
val ex = assertFailsWith<ScriptError> {
eval(
$$"""
import lyng.decimal
var re = 50.d
val s = "${re + }"
""".trimIndent()
)
}
assertEquals(2, ex.pos.line)
}
}