code cleanup performed (trivial cases)

This commit is contained in:
sergeych committed 2026-01-06 10:38:31 +01:00
1 parent fa4dc45f15
commit 8cd980514b
16 files changed
+86 -81

No files matched your search

@@ -175,8 +175,6 @@ class Compiler(
// A standalone newline not immediately following a comment resets doc buffer
if (!prevWasComment) clearPendingDoc() else prevWasComment = false
}
else -> {}
}
cc.next()
continue
@@ -2751,7 +2749,7 @@ class Compiler(
}
if (extTypeName != null) {
val type = context[extTypeName!!]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
context.addExtension(type, name, ObjRecord(ObjUnset, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply {
delegate = finalDelegate
@@ -2773,7 +2771,7 @@ class Compiler(
cls.createField(name, ObjUnset, false, visibility, null, start, declaringClass = cls, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride, type = ObjRecord.Type.Delegated)
cls.instanceInitializers += statement(start) { scp ->
val accessType2 = scp.resolveQualifiedIdentifier("DelegateAccess.Callable")
val initValue2 = delegateExpression!!.execute(scp)
val initValue2 = delegateExpression.execute(scp)
val finalDelegate2 = try {
initValue2.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType2, scp.thisObj))
} catch (e: Exception) {
@@ -3129,7 +3127,7 @@ class Compiler(
cc.skipWsTokens()
cc.next() // consume '='
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected getter expression")
(expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
expr
} else {
throw ScriptError(cc.current().pos, "Expected { or = after get()")
}
@@ -3150,7 +3148,7 @@ class Compiler(
cc.skipWsTokens()
cc.next() // consume '='
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected setter expression")
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
val st = expr
statement(st.pos) { scope ->
val value = scope.args.list.firstOrNull() ?: ObjNull
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
@@ -3185,7 +3183,7 @@ class Compiler(
cc.current().pos,
"Expected setter expression"
)
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
val st = expr
statement(st.pos) { scope ->
val value = scope.args.list.firstOrNull() ?: ObjNull
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
@@ -3388,7 +3386,7 @@ class Compiler(
prop
}
} else {
val isLateInitVal = !isMutable && initialExpression == null && getter == null && setter == null
val isLateInitVal = !isMutable && initialExpression == null
if (declaringClassName != null && !isStatic) {
val storageName = "$declaringClassName::$name"
// If we are in class scope now (defining instance field), defer initialization to instance time
@@ -314,11 +314,11 @@ open class Scope(
inline fun <reified T : Obj> thisAs(): T {
var s: Scope? = this
do {
val t = s!!.thisObj
while (s != null) {
val t = s.thisObj
if (t is T) return t
s = s.parent
} while (s != null)
}
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
}
@@ -642,7 +642,7 @@ open class Scope(
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
}
}
})!!
})
rec.value = res
return res
}
@@ -40,7 +40,7 @@ class Source(val fileName: String, val text: String) {
fun extractPackageName(): String {
for ((n,line) in lines.withIndex()) {
if( line.isBlank() || line.isEmpty() )
if( line.isBlank() )
continue
if( line.startsWith("package ") )
return line.substring(8).trim()
@@ -96,17 +96,15 @@ object CompletionEngineLight {
}
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
mini?.let { m ->
val decls = m.declarations
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
funs.forEach { offerDeclAdd(out, prefix, it) }
classes.forEach { offerDeclAdd(out, prefix, it) }
enums.forEach { offerDeclAdd(out, prefix, it) }
vals.forEach { offerDeclAdd(out, prefix, it) }
}
val decls = mini.declarations
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
funs.forEach { offerDeclAdd(out, prefix, it) }
classes.forEach { offerDeclAdd(out, prefix, it) }
enums.forEach { offerDeclAdd(out, prefix, it) }
vals.forEach { offerDeclAdd(out, prefix, it) }
// Imported and builtin
val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
@@ -192,7 +190,7 @@ object CompletionEngineLight {
val chosen = variants.asSequence()
.filterIsInstance<MiniMemberValDecl>()
.firstOrNull { it.type != null } ?: rep
val ci = CompletionItem(name, Kind.Field, typeText = typeOf((chosen as MiniMemberValDecl).type))
val ci = CompletionItem(name, Kind.Field, typeText = typeOf(chosen.type))
if (ci.name.startsWith(prefix, true)) out += ci
}
is MiniInitDecl -> {}
@@ -184,11 +184,8 @@ open class ObjClass(
val base = c3Linearize(this, mutableMapOf())
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
else {
// During very early bootstrap rootObjectType might not be initialized yet.
// We use a safe check here.
@Suppress("UNNECESSARY_SAFE_CALL")
val root = net.sergeych.lyng.obj.Obj.rootObjectType
if (root != null) base + root else base
val root = Obj.rootObjectType
base + root
}
}
@@ -597,7 +594,7 @@ open class ObjClass(
// Fallback: property delegation
val propVal = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
propVal.invoke(scope, this, args, decl)
})!!
})
}
if (rec.type == ObjRecord.Type.Fun) {
return rec.value.invoke(scope, this, args, decl)
@@ -202,7 +202,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
// Fallback: property delegation
val propVal = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
propVal.invoke(scope, this, args, rec.declaringClass ?: cls)
})!!
})
}
if (rec.type == ObjRecord.Type.Fun && !rec.isAbstract) {
val decl = rec.declaringClass ?: cls
@@ -211,7 +211,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
scope.raiseError(
ObjIllegalAccessException(
scope,
"can't invoke method $name (declared in ${decl.className ?: "?"})"
"can't invoke method $name (declared in ${decl.className})"
)
)
return rec.value.invoke(
@@ -263,7 +263,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
val params = meta.params.map { readField(scope, it.name).value }
encoder.encodeAnyList(scope, params)
val vars = serializingVars.values.map { it.value }
if (vars.isNotEmpty<Obj>()) {
if (vars.isNotEmpty()) {
encoder.encodeAnyList(scope, vars)
}
}
@@ -115,9 +115,9 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
}
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
if (isIntRange) {
val s = (start as ObjInt).value
val e = (end as ObjInt).value
if (start is ObjInt && end is ObjInt) {
val s = start.value
val e = end.value
if (isEndInclusive) {
for (i in s..e) {
if (!callback(ObjInt.of(i))) break
@@ -127,9 +127,9 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
if (!callback(ObjInt.of(i))) break
}
}
} else if (isCharRange) {
val s = (start as ObjChar).value
val e = (end as ObjChar).value
} else if (start is ObjChar && end is ObjChar) {
val s = start.value
val e = end.value
if (isEndInclusive) {
for (c in s..e) {
if (!callback(ObjChar(c))) break
@@ -29,14 +29,19 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
override val objClass: ObjClass = type
fun Scope.init() {
if (self.start == null || self.end == null)
raiseError("next is only available for finite ranges")
isCharRange = self.isCharRange
lastIndex = if (self.isIntRange || self.isCharRange) {
if (self.isEndInclusive)
self.end.toInt() - self.start.toInt() + 1
val s = self.start
val e = self.end
if (s is ObjInt && e is ObjInt) {
lastIndex = if (self.isEndInclusive)
(e.value - s.value + 1).toInt()
else
self.end.toInt() - self.start.toInt()
(e.value - s.value).toInt()
} else if (s is ObjChar && e is ObjChar) {
isCharRange = true
lastIndex = if (self.isEndInclusive)
(e.value.code - s.value.code + 1)
else
(e.value.code - s.value.code)
} else {
raiseError("not implemented iterator for range of $this")
}
@@ -46,10 +51,13 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
fun next(scope: Scope): Obj =
if (nextIndex < lastIndex) {
val x = if (self.isEndInclusive)
self.start!!.toLong() + nextIndex++
val start = self.start
val x = if (start is ObjInt)
start.value + nextIndex++
else if (start is ObjChar)
start.value.code.toLong() + nextIndex++
else
self.start!!.toLong() + nextIndex++
scope.raiseError("iterator error: unsupported range start")
if( isCharRange ) ObjChar(x.toInt().toChar()) else ObjInt(x)
}
else {
@@ -469,9 +469,11 @@ class FieldRef(
// Adaptive PIC (2→4) for reads/writes
private var rAccesses: Int = 0; private var rMisses: Int = 0; private var rPromotedTo4: Boolean = false
private var wAccesses: Int = 0; private var wMisses: Int = 0; private var wPromotedTo4: Boolean = false
@Suppress("NOTHING_TO_INLINE")
private inline fun size4ReadsEnabled(): Boolean =
PerfFlags.FIELD_PIC_SIZE_4 ||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && rPromotedTo4)
@Suppress("NOTHING_TO_INLINE")
private inline fun size4WritesEnabled(): Boolean =
PerfFlags.FIELD_PIC_SIZE_4 ||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && wPromotedTo4)
@@ -1064,6 +1066,7 @@ class MethodCallRef(
private var mFreezeWindowsLeft: Int = 0
private var mWindowAccesses: Int = 0
private var mWindowMisses: Int = 0
@Suppress("NOTHING_TO_INLINE")
private inline fun size4MethodsEnabled(): Boolean =
PerfFlags.METHOD_PIC_SIZE_4 ||
((PerfFlags.PIC_ADAPTIVE_2_TO_4 || PerfFlags.PIC_ADAPTIVE_METHODS_ONLY) && mPromotedTo4 && mFreezeWindowsLeft == 0)
@@ -87,7 +87,7 @@ class ObjRegexMatch(val match: MatchResult) : Obj() {
// Use groupValues so that index 0 is the whole match and subsequent indices are capturing groups,
// which matches the language/tests expectation for `$~[i]`.
ObjList(
match.groupValues.map { ObjString(it) as Obj }.toMutableList()
match.groupValues.map { ObjString(it) }.toMutableList()
)
}