Compare commits

..
4 Commits
30 changed files with 808 additions and 137 deletions

No files matched your search

+18 -1
View File
@@ -11,7 +11,22 @@
- Integration: Updated TextMate grammar and IntelliJ plugin (highlighting + keywords).
- Documentation: New "Properties" section in `docs/OOP.md`.
- Docs: Scopes and Closures guidance
- Language: Restricted Setter Visibility
- Support for `private set` and `protected set` modifiers on `var` fields and properties.
- Allows members to be publicly readable but only writable from within the declaring class or its subclasses.
- Enforcement at runtime: throws `AccessException` on unauthorized writes.
- Supported only for declarations in class bodies (fields and properties).
- Documentation: New "Restricted Setter Visibility" section in `docs/OOP.md`.
- Language: Late-initialized `val` fields in classes
- Support for declaring `val` without an immediate initializer in class bodies.
- Compulsory initialization: every late-init `val` must be assigned at least once within the class body or an `init` block.
- Write-once enforcement: assigning to a `val` is allowed only if its current value is `Unset`.
- Access protection: reading a late-init `val` before it is assigned returns the `Unset` singleton; using `Unset` for most operations throws an `UnsetException`.
- Extension properties do not support late-init.
- Documentation: New "Late-initialized `val` fields" and "The `Unset` singleton" sections in `docs/OOP.md`.
- Docs: OOP improvements
- New page: `docs/scopes_and_closures.md` detailing `ClosureScope` resolution order, recursion‑safe helpers (`chainLookupIgnoreClosure`, `chainLookupWithMembers`, `baseGetIgnoreClosure`), cycle prevention, and capturing lexical environments for callbacks (`snapshotForClosure`).
- Updated: `docs/advanced_topics.md` (link to the new page), `docs/parallelism.md` (closures in `launch`/`flow`), `docs/OOP.md` (visibility from closures with preserved `currentClassCtx`), `docs/exceptions_handling.md` (compatibility alias `SymbolNotFound`).
- Tutorial: added quick link to Scopes and Closures.
@@ -83,6 +98,8 @@ All notable changes to this project will be documented in this file.
- Mutually exclusive: `--check` and `--in-place` together now produce an error and exit with code 1.
- Multi-file stdout prints headers `--- <path> ---` per file.
- `lyng --help` shows `fmt`; `lyng fmt --help` displays dedicated help.
- Fix: Property accessors (`get`, `set`, `private set`, `protected set`) are now correctly indented relative to the property declaration.
- Fix: Indentation now correctly carries over into blocks that start on extra‑indented lines (e.g., nested `if` statements or property accessor bodies).
- CLI: Preserved legacy script invocation fast-paths:
- `lyng script.lyng [args...]` executes the script directly.
+101
View File
@@ -175,6 +175,44 @@ statements discussed later, there could be default values, ellipsis, etc.
Note that unlike **Kotlin**, which uses `=` for named arguments, Lyng uses `:` to avoid ambiguity with assignment expressions.
### Late-initialized `val` fields
You can declare a `val` field without an immediate initializer if you provide an assignment for it within an `init` block or the class body. This is useful when the initial value depends on logic that cannot be expressed in a single expression.
```kotlin
class DataProcessor(data: Object) {
val result: Object
init {
// Complex initialization logic
result = transform(data)
}
}
```
Key rules for late-init `val`:
- **Compile-time Check**: The compiler ensures that every `val` declared without an initializer in a class body has at least one assignment within that class body (including `init` blocks). Failing to do so results in a syntax error.
- **Write-Once**: A `val` can only be assigned once. Even if it was declared without an initializer, once it is assigned a value (e.g., in `init`), any subsequent assignment will throw an `IllegalAssignmentException`.
- **Access before Initialization**: If you attempt to read a late-init `val` before it has been assigned (for example, by calling a method in `init` that reads the field before its assignment), it will hold a special `Unset` value. Using `Unset` for most operations (like arithmetic or method calls) will throw an `UnsetException`.
- **No Extensions**: Extension properties do not support late initialization as they do not have per-instance storage. Extension `val`s must always have an initializer or a `get()` accessor.
### The `Unset` singleton
The `Unset` singleton represents a field that has been declared but not yet initialized. While it can be compared and converted to a string, most other operations on it are forbidden to prevent accidental use of uninitialized data.
```kotlin
class T {
val x
fun check() {
if (x == Unset) println("Not ready")
}
init {
check() // Prints "Not ready"
x = 42
}
}
```
## Methods
Functions defined inside a class body are methods, and unless declared
@@ -394,6 +432,69 @@ Are declared with var
assert( p.isSpecial == true )
>>> void
### Restricted Setter Visibility
You can restrict the visibility of a `var` field's or property's setter by using `private set` or `protected set` modifiers. This allows the member to be publicly readable but only writable from within the class or its subclasses.
#### On Fields
```kotlin
class SecretCounter {
var count = 0
private set // Can be read anywhere, but written only in SecretCounter
fun increment() { count++ }
}
val c = SecretCounter()
println(c.count) // OK
c.count = 10 // Throws AccessException
c.increment() // OK
```
#### On Properties
You can also apply restricted visibility to custom property setters:
```kotlin
class Person(private var _age: Int) {
var age
get() = _age
private set(v) { if (v >= 0) _age = v }
}
```
#### Protected Setters and Inheritance
A `protected set` allows subclasses to modify a field that is otherwise read-only to the public:
```kotlin
class Base {
var state = "initial"
protected set
}
class Derived : Base() {
fun changeState(newVal) {
state = newVal // OK: protected access from subclass
}
}
val d = Derived()
println(d.state) // OK: "initial"
d.changeState("updated")
println(d.state) // OK: "updated"
d.state = "bad" // Throws AccessException: public write not allowed
```
### Key Rules and Limitations
- **Only for `var`**: Restricted setter visibility cannot be used with `val` declarations, as they are inherently read-only. Attempting to use it with `val` results in a syntax error.
- **Class Body Only**: These modifiers can only be used on members declared within the class body. They are not supported for primary constructor parameters.
- **`private set`**: The setter is only accessible within the same class context (specifically, when `this` is an instance of that class).
- **`protected set`**: The setter is accessible within the declaring class and all its transitive subclasses.
- **Multiple Inheritance**: In MI scenarios, visibility is checked against the class that actually declared the member. Qualified access (e.g., `this@Base.field = value`) also respects restricted setter visibility.
### Private fields
Private fields are visible only _inside the class instance_:
+110
View File
@@ -0,0 +1,110 @@
# What's New in Lyng
This document highlights the latest additions and improvements to the Lyng language and its ecosystem.
## Language Features
### Class Properties with Accessors
Classes now support properties with custom `get()` and `set()` accessors. Properties in Lyng do **not** have automatic backing fields; they are pure accessors.
```lyng
class Person(private var _age: Int) {
// Read-only property
val ageCategory get() = if (_age < 18) "Minor" else "Adult"
// Read-write property
var age: Int
get() = _age
set(v) {
if (v >= 0) _age = v
}
}
```
### Private and Protected Setters
You can now restrict the visibility of a property's or field's setter using `private set` or `protected set`. This allows members to be publicly readable but only writable from within the declaring class or its subclasses.
```lyng
class Counter {
var count = 0
private set // Field with private setter
fun increment() { count++ }
}
class AdvancedCounter : Counter {
var totalOperations = 0
protected set // Settable here and in further subclasses
}
let c = Counter()
c.increment() // OK
// c.count = 10 // Error: setter is private
```
### Late-initialized `val` Fields
`val` fields in classes can be declared without an immediate initializer, provided they are assigned exactly once. If accessed before initialization, they hold the special `Unset` singleton.
```lyng
class Service {
val logger
fun check() {
if (logger == Unset) println("Not initialized yet")
}
init {
logger = Logger("Service")
}
}
```
### Named Arguments and Named Splats
Function calls now support named arguments using the `name: value` syntax. If the variable name matches the parameter name, you can use the `name:` shorthand.
```lyng
fun greet(name, greeting = "Hello") {
println("$greeting, $name!")
}
val name = "Alice"
greet(name:) // Shorthand for greet(name: name)
greet(greeting: "Hi", name: "Bob")
let params = { name: "Charlie", greeting: "Hey")
greet(...params) // Named splat expansion
```
### Multiple Inheritance (MI)
Lyng now supports multiple inheritance using the C3 Method Resolution Order (MRO). Use `this@Type` or casts for disambiguation.
```lyng
class A { fun foo() = "A" }
class B { fun foo() = "B" }
class Derived : A, B {
fun test() {
println(foo()) // Resolves to A.foo (leftmost)
println(this@B.foo()) // Qualified dispatch to B.foo
}
}
let d = Derived()
println((d as B).foo()) // Disambiguation via cast
```
## Tooling and Infrastructure
### CLI: Formatting Command
A new `fmt` subcommand has been added to the Lyng CLI.
```bash
lyng fmt MyFile.lyng # Print formatted code to stdout
lyng fmt --in-place MyFile.lyng # Format file in-place
lyng fmt --check MyFile.lyng # Check if file needs formatting
```
### IDEA Plugin: Autocompletion
Experimental lightweight autocompletion is now available in the IntelliJ plugin. It features type-aware member suggestions and inheritance-aware completion.
You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**.
@@ -21,7 +21,6 @@
*/
package net.sergeych.lyng.idea.completion
import LyngAstManager
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.icons.AllIcons
@@ -36,6 +35,7 @@ import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
import net.sergeych.lyng.idea.util.DocsBootstrap
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.idea.util.TextCtx
import net.sergeych.lyng.miniast.*
@@ -85,7 +85,13 @@ class LyngEnterHandler : EnterHandlerDelegate {
if (code == "}" || code == "*/") {
// Adjust indent for the previous line if it's a block or comment closer
val prevStart = doc.getLineStartOffset(prevLine)
CodeStyleManager.getInstance(project).adjustLineIndent(file, prevStart)
if (file.context == null) {
try {
CodeStyleManager.getInstance(project).adjustLineIndent(file, prevStart)
} catch (e: Exception) {
log.warn("Failed to adjust line indent for previous line: ${e.message}")
}
}
// Fallback for previous line: manual application
val desiredPrev = computeDesiredIndent(project, doc, prevLine)
@@ -102,8 +108,13 @@ class LyngEnterHandler : EnterHandlerDelegate {
}
// Adjust indent for the current (new) line
val currentStart = doc.getLineStartOffsetSafe(currentLine)
val csm = CodeStyleManager.getInstance(project)
csm.adjustLineIndent(file, currentStart)
if (file.context == null) {
try {
CodeStyleManager.getInstance(project).adjustLineIndent(file, currentStart)
} catch (e: Exception) {
log.warn("Failed to adjust line indent for current line: ${e.message}")
}
}
// Fallback: if the platform didn't physically insert indentation, compute it from our formatter and apply
val lineStart = doc.getLineStartOffset(currentLine)
@@ -58,7 +58,13 @@ class LyngTypedHandler : TypedHandlerDelegate() {
}
// After block reindent, adjust line indent to what platform thinks (no-op in many cases)
val lineStart = doc.getLineStartOffset(line)
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
if (file.context == null) {
try {
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
} catch (e: Exception) {
log.warn("Failed to adjust line indent for current line: ${e.message}")
}
}
}
} else if (c == '/') {
val doc = editor.document
@@ -67,7 +73,13 @@ class LyngTypedHandler : TypedHandlerDelegate() {
PsiDocumentManager.getInstance(project).commitDocument(doc)
val line = doc.getLineNumber(offset - 1)
val lineStart = doc.getLineStartOffset(line)
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
if (file.context == null) {
try {
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
} catch (e: Exception) {
log.warn("Failed to adjust line indent for comment: ${e.message}")
}
}
// Manual application fallback
val desired = computeDesiredIndent(project, doc, line)
@@ -1,5 +1,5 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -80,7 +80,14 @@ class LyngPreFormatProcessor : PreFormatProcessor {
val lineStart = doc.getLineStartOffset(line)
// adjustLineIndent delegates to our LineIndentProvider which computes
// indentation from scratch; this is safe and idempotent
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
if (file.context == null) {
try {
CodeStyleManager.getInstance(project).adjustLineIndent(file, lineStart)
} catch (e: Exception) {
// Log as debug because this can be called many times during reformat
// and we don't want to spam warnings if it's a known platform issue with injections
}
}
// After indentation, update block/paren/bracket balances using the current line text
val lineEnd = doc.getLineEndOffset(line)
@@ -17,7 +17,6 @@
package net.sergeych.lyng.idea.navigation
import LyngAstManager
import com.intellij.lang.cacheBuilder.DefaultWordsScanner
import com.intellij.lang.cacheBuilder.WordsScanner
import com.intellij.lang.findUsages.FindUsagesProvider
@@ -26,6 +25,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.tree.TokenSet
import net.sergeych.lyng.idea.highlight.LyngLexer
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.miniast.DocLookupUtils
class LyngFindUsagesProvider : FindUsagesProvider {
@@ -17,10 +17,10 @@
package net.sergeych.lyng.idea.navigation
import LyngAstManager
import com.intellij.icons.AllIcons
import com.intellij.ide.IconProvider
import com.intellij.psi.PsiElement
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.miniast.DocLookupUtils
import javax.swing.Icon
@@ -17,13 +17,13 @@
package net.sergeych.lyng.idea.navigation
import LyngAstManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.search.GlobalSearchScope
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.idea.util.TextCtx
import net.sergeych.lyng.miniast.*
@@ -17,12 +17,12 @@
package net.sergeych.lyng.idea.navigation
import LyngAstManager
import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.*
import com.intellij.util.ProcessingContext
import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.miniast.DocLookupUtils
class LyngPsiReferenceContributor : PsiReferenceContributor() {
@@ -15,23 +15,6 @@
*
*/
a/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.idea.util
import com.intellij.openapi.util.Key
+2 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych"
version = "1.1.0-SNAPSHOT"
version = "1.1.0-rc"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
@@ -20,5 +20,7 @@ package net.sergeych.lyng
sealed class CodeContext {
class Module(@Suppress("unused") val packageName: String?): CodeContext()
class Function(val name: String): CodeContext()
class ClassBody(val name: String): CodeContext()
class ClassBody(val name: String): CodeContext() {
val pendingInitializations = mutableMapOf<String, Pos>()
}
}
@@ -138,9 +138,16 @@ class Compiler(
private var lastParsedBlockRange: MiniRange? = null
private suspend fun <T> inCodeContext(context: CodeContext, f: suspend () -> T): T {
return try {
codeContexts.add(context)
f()
codeContexts.add(context)
try {
val res = f()
if (context is CodeContext.ClassBody) {
if (context.pendingInitializations.isNotEmpty()) {
val (name, pos) = context.pendingInitializations.entries.first()
throw ScriptError(pos, "val '$name' must be initialized in the class body or init block")
}
}
return res
} finally {
codeContexts.removeLast()
}
@@ -360,7 +367,21 @@ class Compiler(
val rvalue = parseExpressionLevel(level + 1)
?: throw ScriptError(opToken.pos, "Expecting expression")
lvalue = op.generate(opToken.pos, lvalue!!, rvalue)
val res = op.generate(opToken.pos, lvalue!!, rvalue)
if (opToken.type == Token.Type.ASSIGN) {
val ctx = codeContexts.lastOrNull()
if (ctx is CodeContext.ClassBody) {
val target = lvalue
val name = when (target) {
is LocalVarRef -> target.name
is FastLocalVarRef -> target.name
is FieldRef -> if (target.target is LocalVarRef && target.target.name == "this") target.name else null
else -> null
}
if (name != null) ctx.pendingInitializations.remove(name)
}
}
lvalue = res
}
return lvalue
}
@@ -492,24 +513,32 @@ class Compiler(
// there could be terminal operators or keywords:// variable to read or like
when (t.value) {
in stopKeywords -> {
if (operand != null) throw ScriptError(t.pos, "unexpected keyword")
// Allow certain statement-like constructs to act as expressions
// when they appear in expression position (e.g., `if (...) ... else ...`).
// Other keywords should be handled by the outer statement parser.
when (t.value) {
"if" -> {
val s = parseIfStatement()
operand = StatementRef(s)
}
"when" -> {
val s = parseWhenStatement()
operand = StatementRef(s)
}
else -> {
// Do not consume the keyword as part of a term; backtrack
// and return null so outer parser handles it.
cc.previous()
return null
if (t.value == "init" && !(codeContexts.lastOrNull() is CodeContext.ClassBody && cc.peekNextNonWhitespace().type == Token.Type.LBRACE)) {
// Soft keyword: init is only a keyword in class body when followed by {
cc.previous()
operand = parseAccessor()
} else {
if (operand != null) throw ScriptError(t.pos, "unexpected keyword")
// Allow certain statement-like constructs to act as expressions
// when they appear in expression position (e.g., `if (...) ... else ...`).
// Other keywords should be handled by the outer statement parser.
when (t.value) {
"if" -> {
val s = parseIfStatement()
operand = StatementRef(s)
}
"when" -> {
val s = parseWhenStatement()
operand = StatementRef(s)
}
else -> {
// Do not consume the keyword as part of a term; backtrack
// and return null so outer parser handles it.
cc.previous()
return null
}
}
}
}
@@ -1367,7 +1396,7 @@ class Compiler(
}
"init" -> {
if (codeContexts.lastOrNull() is CodeContext.ClassBody) {
if (codeContexts.lastOrNull() is CodeContext.ClassBody && cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
val block = parseBlock()
lastParsedBlockRange?.let { range ->
miniSink?.onInitDecl(MiniInitDecl(MiniRange(id.pos, range.end), id.pos))
@@ -2696,7 +2725,7 @@ class Compiler(
cc.restorePos(markBeforeEq)
cc.skipWsTokens()
val next = cc.peekNextNonWhitespace()
if (next.isId("get") || next.isId("set")) {
if (next.isId("get") || next.isId("set") || next.isId("private") || next.isId("protected")) {
isProperty = true
cc.restorePos(markBeforeEq)
cc.skipWsTokens()
@@ -2714,7 +2743,13 @@ class Compiler(
if (!isProperty && eqToken.type != Token.Type.ASSIGN) {
if (!isMutable && (declaringClassNameCaptured == null) && (extTypeName == null))
throw ScriptError(start, "val must be initialized")
else {
else if (!isMutable && declaringClassNameCaptured != null && extTypeName == null) {
// lateinit val in class: track it
(codeContexts.lastOrNull() as? CodeContext.ClassBody)?.pendingInitializations?.put(name, start)
cc.restorePos(markBeforeEq)
cc.skipWsTokens()
setNull = true
} else {
cc.restorePos(markBeforeEq)
cc.skipWsTokens()
setNull = true
@@ -2753,8 +2788,8 @@ class Compiler(
// if (isDelegate) throw ScriptError(start, "static delegates are not yet implemented")
currentInitScope += statement {
val initValue = initialExpression?.execute(this)?.byValueCopy() ?: ObjNull
(thisObj as ObjClass).createClassField(name, initValue, isMutable, visibility, pos)
addItem(name, isMutable, initValue, visibility, ObjRecord.Type.Field)
(thisObj as ObjClass).createClassField(name, initValue, isMutable, visibility, null, pos)
addItem(name, isMutable, initValue, visibility, null, ObjRecord.Type.Field)
ObjVoid
}
return NopStatement
@@ -2763,11 +2798,11 @@ class Compiler(
// Check for accessors if it is a class member
var getter: Statement? = null
var setter: Statement? = null
var setterVisibility: Visibility? = null
if (declaringClassNameCaptured != null || extTypeName != null) {
while (true) {
val t = cc.peekNextNonWhitespace()
val t = cc.skipWsTokens()
if (t.isId("get")) {
cc.skipWsTokens()
cc.next() // consume 'get'
cc.requireToken(Token.Type.LPAREN)
cc.requireToken(Token.Type.RPAREN)
@@ -2783,7 +2818,6 @@ class Compiler(
throw ScriptError(cc.current().pos, "Expected { or = after get()")
}
} else if (t.isId("set")) {
cc.skipWsTokens()
cc.next() // consume 'set'
cc.requireToken(Token.Type.LPAREN)
val setArg = cc.requireToken(Token.Type.ID, "Expected setter argument name")
@@ -2809,6 +2843,46 @@ class Compiler(
} else {
throw ScriptError(cc.current().pos, "Expected { or = after set(...)")
}
} else if (t.isId("private") || t.isId("protected")) {
val vis = if (t.isId("private")) Visibility.Private else Visibility.Protected
val mark = cc.savePos()
cc.next() // consume modifier
if (cc.skipWsTokens().isId("set")) {
cc.next() // consume 'set'
setterVisibility = vis
if (cc.skipWsTokens().type == Token.Type.LPAREN) {
cc.next() // consume '('
val setArg = cc.requireToken(Token.Type.ID, "Expected setter argument name")
cc.requireToken(Token.Type.RPAREN)
setter = if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
cc.skipWsTokens()
val body = parseBlock()
statement(body.pos) { scope ->
val value = scope.args.list.firstOrNull() ?: ObjNull
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
body.execute(scope)
}
} else if (cc.peekNextNonWhitespace().type == Token.Type.ASSIGN) {
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) }
statement(st.pos) { scope ->
val value = scope.args.list.firstOrNull() ?: ObjNull
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
st.execute(scope)
}
} else {
throw ScriptError(cc.current().pos, "Expected { or = after set(...)")
}
}
} else {
cc.restorePos(mark)
break
}
} else break
}
if (getter != null || setter != null) {
@@ -2817,11 +2891,13 @@ class Compiler(
throw ScriptError(start, "var property must have both get() and set()")
}
} else {
if (setter != null)
throw ScriptError(start, "val property cannot have a setter (name: $name)")
if (setter != null || setterVisibility != null)
throw ScriptError(start, "val property cannot have a setter or restricted visibility set (name: $name)")
if (getter == null)
throw ScriptError(start, "val property with accessors must have a getter (name: $name)")
}
} else if (setterVisibility != null && !isMutable) {
throw ScriptError(start, "val field cannot have restricted visibility set (name: $name)")
}
}
@@ -2838,7 +2914,7 @@ class Compiler(
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(prop, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Property))
context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, writeVisibility = setterVisibility, declaringClass = null, type = ObjRecord.Type.Property))
return@statement prop
}
@@ -2872,6 +2948,7 @@ class Compiler(
isMutable,
prop,
visibility,
setterVisibility,
recordType = ObjRecord.Type.Property
)
ObjVoid
@@ -2879,38 +2956,41 @@ class Compiler(
ObjVoid
} else {
// We are in instance scope already: perform initialization immediately
context.addItem(storageName, isMutable, prop, visibility, recordType = ObjRecord.Type.Property)
context.addItem(storageName, isMutable, prop, visibility, setterVisibility, recordType = ObjRecord.Type.Property)
prop
}
} else {
if (declaringClassName != null && !isStatic) {
val storageName = "$declaringClassName::$name"
// If we are in class scope now (defining instance field), defer initialization to instance time
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
if (isClassScope) {
val cls = context.thisObj as ObjClass
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
val initStmt = statement(start) { scp ->
val initValue = initialExpression?.execute(scp)?.byValueCopy() ?: ObjNull
// Preserve mutability of declaration: do NOT use addOrUpdateItem here, as it creates mutable records
scp.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
val isLateInitVal = !isMutable && initialExpression == null && getter == null && setter == null
if (declaringClassName != null && !isStatic) {
val storageName = "$declaringClassName::$name"
// If we are in class scope now (defining instance field), defer initialization to instance time
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
if (isClassScope) {
val cls = context.thisObj as ObjClass
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
val initStmt = statement(start) { scp ->
val initValue =
initialExpression?.execute(scp)?.byValueCopy() ?: if (isLateInitVal) ObjUnset else ObjNull
// Preserve mutability of declaration: do NOT use addOrUpdateItem here, as it creates mutable records
scp.addItem(storageName, isMutable, initValue, visibility, setterVisibility, recordType = ObjRecord.Type.Field)
ObjVoid
}
cls.instanceInitializers += initStmt
ObjVoid
} else {
// We are in instance scope already: perform initialization immediately
val initValue =
initialExpression?.execute(context)?.byValueCopy() ?: if (isLateInitVal) ObjUnset else ObjNull
// Preserve mutability of declaration: create record with correct mutability
context.addItem(storageName, isMutable, initValue, visibility, setterVisibility, recordType = ObjRecord.Type.Field)
initValue
}
cls.instanceInitializers += initStmt
ObjVoid
} else {
// We are in instance scope already: perform initialization immediately
// Not in class body: regular local/var declaration
val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
// Preserve mutability of declaration: create record with correct mutability
context.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
context.addItem(name, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
initValue
}
} else {
// Not in class body: regular local/var declaration
val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
context.addItem(name, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
initValue
}
}
}
}
@@ -3213,7 +3293,10 @@ class Compiler(
* The keywords that stop processing of expression term
*/
val stopKeywords =
setOf("do", "break", "continue", "return", "if", "when", "do", "while", "for", "class")
setOf(
"break", "continue", "return", "if", "when", "do", "while", "for", "class",
"private", "protected", "val", "var", "fun", "fn", "static", "init", "enum"
)
}
}
@@ -262,6 +262,9 @@ open class Scope(
fun raiseClassCastError(msg: String): Nothing = raiseError(ObjClassCastException(this, msg))
fun raiseUnset(message: String = "property is unset (not initialized)"): Nothing =
raiseError(ObjUnsetException(this, message))
@Suppress("unused")
fun raiseSymbolNotFound(name: String): Nothing =
raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name"))
@@ -431,6 +434,7 @@ open class Scope(
name: String,
value: Obj,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
recordType: ObjRecord.Type = ObjRecord.Type.Other
): ObjRecord =
objects[name]?.let {
@@ -445,17 +449,18 @@ open class Scope(
callScope.localBindings[name] = it
}
it
} ?: addItem(name, true, value, visibility, recordType)
} ?: addItem(name, true, value, visibility, writeVisibility, recordType)
fun addItem(
name: String,
isMutable: Boolean,
value: Obj,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
recordType: ObjRecord.Type = ObjRecord.Type.Other,
declaringClass: net.sergeych.lyng.obj.ObjClass? = currentClassCtx
): ObjRecord {
val rec = ObjRecord(value, isMutable, visibility, declaringClass = declaringClass, type = recordType)
val rec = ObjRecord(value, isMutable, visibility, writeVisibility, declaringClass = declaringClass, type = recordType)
objects[name] = rec
// Index this binding within the current frame to help resolve locals across suspension
localBindings[name] = rec
@@ -60,6 +60,7 @@ class Script(
internal val rootScope: Scope = Scope(null).apply {
ObjException.addExceptionsToContext(this)
addConst("Unset", ObjUnset)
addFn("print") {
for ((i, a) in args.withIndex()) {
if (i > 0) print(' ' + a.toString(this).value)
@@ -113,6 +113,15 @@ object Binder {
symbols += fieldSym
classes.last().fields += fieldSym.id
}
// Class fields (val/var in class body, if any are reported here)
for (cf in d.classFields) {
val fs = source.offsetOf(cf.nameStart)
val fe = fs + cf.name.length
val kind = if (cf.mutable) SymbolKind.Variable else SymbolKind.Value
val fieldSym = Symbol(nextId++, cf.name, kind, fs, fe, containerId = sym.id)
symbols += fieldSym
classes.last().fields += fieldSym.id
}
}
// Second pass: functions and top-level/class vals/vars
@@ -24,6 +24,24 @@ package net.sergeych.lyng.format
*/
object LyngFormatter {
private fun startsWithWord(s: String, w: String): Boolean =
s.startsWith(w) && s.getOrNull(w.length)?.let { !it.isLetterOrDigit() && it != '_' } != false
private fun isPropertyAccessor(s: String): Boolean {
val t = s.trim()
if (t.isEmpty()) return false
if (startsWithWord(t, "get") || startsWithWord(t, "set")) return true
if (startsWithWord(t, "private")) {
val rest = t.substring("private".length).trimStart()
if (startsWithWord(rest, "set")) return true
}
if (startsWithWord(t, "protected")) {
val rest = t.substring("protected".length).trimStart()
if (startsWithWord(rest, "set")) return true
}
return false
}
/** Returns the input with indentation recomputed from scratch, line by line. */
fun reindent(text: String, config: LyngFormatConfig = LyngFormatConfig()): String {
// Normalize tabs to spaces globally before any transformation; results must contain no tabs
@@ -35,6 +53,7 @@ object LyngFormatter {
var bracketBalance = 0
var prevBracketContinuation = false
var inBlockComment = false
val extraIndents = mutableListOf<Int>()
// We don't keep per-"[" base alignment; continuation rules define alignment.
fun updateBalances(ch: Char) {
@@ -52,7 +71,7 @@ object LyngFormatter {
// Always produce spaces; tabs are not allowed in resulting code
" ".repeat(level * config.indentSize + continuation)
var awaitingSingleIndent = false
var awaitingExtraIndent = 0
fun isControlHeaderNoBrace(s: String): Boolean {
val t = s.trim()
if (t.isEmpty()) return false
@@ -60,7 +79,13 @@ object LyngFormatter {
val isIf = Regex("^if\\s*\\(.*\\)\\s*$").matches(t)
val isElseIf = Regex("^else\\s+if\\s*\\(.*\\)\\s*$").matches(t)
val isElse = t == "else"
return isIf || isElseIf || isElse
if (isIf || isElseIf || isElse) return true
// property accessors ending with ) or =
if (isPropertyAccessor(t)) {
return if (t.contains('=')) t.endsWith('=') else t.endsWith(')')
}
return false
}
for ((i, rawLine) in lines.withIndex()) {
@@ -70,7 +95,12 @@ object LyngFormatter {
val trimmedLine = rawLine.trim()
// Compute effective indent level for this line
var effectiveLevel = blockLevel
val currentExtraIndent = extraIndents.sum()
var effectiveLevel = blockLevel + currentExtraIndent
val isAccessor = !inBlockComment && isPropertyAccessor(trimmedStart)
if (isAccessor) effectiveLevel += 1
if (inBlockComment) {
if (!trimmedLine.startsWith("*/")) {
effectiveLevel += 1
@@ -83,9 +113,9 @@ object LyngFormatter {
// Single-line control header (if/else/else if) without braces: indent the next
// non-empty, non-'}', non-'else' line by one extra level
val applyAwaiting = awaitingSingleIndent && trimmedStart.isNotEmpty() &&
val applyAwaiting = awaitingExtraIndent > 0 && trimmedStart.isNotEmpty() &&
!trimmedStart.startsWith("else") && !trimmedStart.startsWith("}")
if (applyAwaiting) effectiveLevel += 1
if (applyAwaiting) effectiveLevel += awaitingExtraIndent
val firstChar = trimmedStart.firstOrNull()
// While inside parentheses, continuation applies scaled by nesting level
@@ -129,23 +159,43 @@ object LyngFormatter {
// New line (keep EOF semantics similar to input)
if (i < lines.lastIndex) sb.append('\n')
val oldBlockLevel = blockLevel
// Update balances using this line's code content
for (part in parts) {
if (part.type == PartType.Code) {
for (ch in part.text) updateBalances(ch)
}
}
val newBlockLevel = blockLevel
if (newBlockLevel > oldBlockLevel) {
val addedThisLine = (if (applyAwaiting) awaitingExtraIndent else 0) + (if (isAccessor) 1 else 0)
repeat(newBlockLevel - oldBlockLevel) {
extraIndents.add(addedThisLine)
}
} else if (newBlockLevel < oldBlockLevel) {
repeat(oldBlockLevel - newBlockLevel) {
if (extraIndents.isNotEmpty()) extraIndents.removeAt(extraIndents.size - 1)
}
}
inBlockComment = nextInBlockComment
// Update awaitingSingleIndent based on current line
// Update awaitingExtraIndent based on current line
if (applyAwaiting && trimmedStart.isNotEmpty()) {
// we have just consumed the body line
awaitingSingleIndent = false
// we have just applied it.
val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) {
// It's another header, increment
awaitingExtraIndent += if (isAccessor) 2 else 1
} else {
// It's the body, reset
awaitingExtraIndent = 0
}
} else {
// start awaiting if current line is a control header without '{'
val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) {
awaitingSingleIndent = true
awaitingExtraIndent = if (isAccessor) 2 else 1
}
}
@@ -196,9 +246,6 @@ object LyngFormatter {
return applyControlledWrapping(spacedText, config)
}
private fun startsWithWord(s: String, w: String): Boolean =
s.startsWith(w) && s.getOrNull(w.length)?.let { !it.isLetterOrDigit() && it != '_' } != false
/**
* Reindents a slice of [text] specified by [range] and returns a new string with that slice replaced.
* By default, preserves the base indent of the first line in the slice (so the block stays at
@@ -1,5 +1,5 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,10 +42,10 @@ private val fallbackKeywordIds = setOf(
"and", "or", "not",
// declarations & modifiers
"fun", "fn", "class", "enum", "val", "var", "import", "package",
"private", "protected", "static", "open", "extern",
"private", "protected", "static", "open", "extern", "init", "get", "set", "by",
// control flow and misc
"if", "else", "when", "while", "do", "for", "try", "catch", "finally",
"throw", "return", "break", "continue", "this", "null", "true", "false"
"throw", "return", "break", "continue", "this", "null", "true", "false", "unset"
)
/** Maps lexer token type (and sometimes value) to a [HighlightKind]. */
@@ -416,7 +416,7 @@ open class Obj {
val decl = field.declaringClass
val caller = scope.currentClassCtx
if (!canAccessMember(field.visibility, decl, caller))
if (!canAccessMember(field.effectiveWriteVisibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't assign field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
if (field.value is ObjProperty) {
(field.value as ObjProperty).callSetter(scope, this, newValue, decl)
@@ -665,6 +665,50 @@ object ObjNull : Obj() {
}
}
@Serializable
@SerialName("unset")
object ObjUnset : Obj() {
override suspend fun compareTo(scope: Scope, other: Obj): Int = if (other === this) 0 else -1
override fun equals(other: Any?): Boolean = other === this
override fun toString(): String = "Unset"
override suspend fun readField(scope: Scope, name: String): ObjRecord = scope.raiseUnset()
override suspend fun invokeInstanceMethod(
scope: Scope,
name: String,
args: Arguments,
onNotFoundResult: (suspend () -> Obj?)?
): Obj = scope.raiseUnset()
override suspend fun getAt(scope: Scope, index: Obj): Obj = scope.raiseUnset()
override suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) = scope.raiseUnset()
override suspend fun callOn(scope: Scope): Obj = scope.raiseUnset()
override suspend fun plus(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun minus(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun negate(scope: Scope): Obj = scope.raiseUnset()
override suspend fun mul(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun div(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun mod(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun logicalNot(scope: Scope): Obj = scope.raiseUnset()
override suspend fun logicalAnd(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun logicalOr(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun operatorMatch(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun bitAnd(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun bitOr(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun bitXor(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun shl(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun shr(scope: Scope, other: Obj): Obj = scope.raiseUnset()
override suspend fun bitNot(scope: Scope): Obj = scope.raiseUnset()
override val objClass: ObjClass by lazy {
object : ObjClass("Unset") {
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
return ObjUnset
}
}
}
}
/**
* TODO: get rid of it. Maybe we ise some Lyng inheritance instead
*/
@@ -345,6 +345,7 @@ open class ObjClass(
initialValue: Obj,
isMutable: Boolean = false,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
pos: Pos = Pos.builtIn,
declaringClass: ObjClass? = this
) {
@@ -353,7 +354,7 @@ open class ObjClass(
if (existingInSelf != null && existingInSelf.isMutable == false)
throw ScriptError(pos, "$name is already defined in $objClass")
// Install/override in this class
members[name] = ObjRecord(initialValue, isMutable, visibility, declaringClass = declaringClass)
members[name] = ObjRecord(initialValue, isMutable, visibility, writeVisibility, declaringClass = declaringClass)
// Structural change: bump layout version for PIC invalidation
layoutVersion += 1
}
@@ -368,13 +369,14 @@ open class ObjClass(
initialValue: Obj,
isMutable: Boolean = false,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
pos: Pos = Pos.builtIn
) {
initClassScope()
val existing = classScope!!.objects[name]
if (existing != null)
throw ScriptError(pos, "$name is already defined in $objClass or one of its supertypes")
classScope!!.addItem(name, isMutable, initialValue, visibility)
classScope!!.addItem(name, isMutable, initialValue, visibility, writeVisibility)
// Structural change: bump layout version for PIC invalidation
layoutVersion += 1
}
@@ -383,11 +385,12 @@ open class ObjClass(
name: String,
isOpen: Boolean = false,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
declaringClass: ObjClass? = this,
code: suspend Scope.() -> Obj
) {
val stmt = statement { code() }
createField(name, stmt, isOpen, visibility, Pos.builtIn, declaringClass)
createField(name, stmt, isOpen, visibility, writeVisibility, Pos.builtIn, declaringClass)
}
fun addConst(name: String, value: Obj) = createField(name, value, isMutable = false)
@@ -397,12 +400,13 @@ open class ObjClass(
getter: (suspend Scope.() -> Obj)? = null,
setter: (suspend Scope.(Obj) -> Unit)? = null,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
declaringClass: ObjClass? = this
) {
val g = getter?.let { statement { it() } }
val s = setter?.let { statement { it(requiredArg(0)); ObjVoid } }
val prop = ObjProperty(name, g, s)
members[name] = ObjRecord(prop, false, visibility, declaringClass, type = ObjRecord.Type.Property)
members[name] = ObjRecord(prop, false, visibility, writeVisibility, declaringClass, type = ObjRecord.Type.Property)
layoutVersion += 1
}
@@ -194,7 +194,9 @@ open class ObjException(
"AccessException",
"UnknownException",
"NotFoundException",
"IllegalOperationException"
"IllegalOperationException",
"UnsetException",
"SyntaxError"
)) {
scope.addConst(name, getOrCreateExceptionClass(name))
}
@@ -245,3 +247,6 @@ class ObjIllegalOperationException(scope: Scope, message: String = "Operation is
class ObjNotFoundException(scope: Scope, message: String = "not found") :
ObjException("NotFoundException", scope, message)
class ObjUnsetException(scope: Scope, message: String = "property is unset (not initialized)") :
ObjException("UnsetException", scope, message)
@@ -97,8 +97,8 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
val decl = f.declaringClass
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(f.visibility, decl, caller))
ObjIllegalAssignmentException(
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjAccessException(
scope,
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
).raise()
@@ -108,7 +108,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
prop.callSetter(scope, this, newValue, decl)
return
}
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (f.value.assign(scope, newValue) == null)
f.value = newValue
return
@@ -131,8 +131,8 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
}
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, declaring, caller))
ObjIllegalAssignmentException(
if (!canAccessMember(rec.effectiveWriteVisibility, declaring, caller))
ObjAccessException(
scope,
"can't assign to field $name (declared in ${declaring?.className ?: "?"})"
).raise()
@@ -142,7 +142,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
prop.callSetter(scope, this, newValue, declaring)
return
}
if (!rec.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (!rec.isMutable && rec.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (rec.value.assign(scope, newValue) == null)
rec.value = newValue
return
@@ -350,12 +350,12 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
instance.instanceScope.objects[mangled]?.let { f ->
val decl = f.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(f.visibility, decl, caller))
ObjIllegalAssignmentException(
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjAccessException(
scope,
"can't assign to field $name (declared in ${decl.className})"
).raise()
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (f.value.assign(scope, newValue) == null) f.value = newValue
return
}
@@ -364,12 +364,12 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
instance.instanceScope[name]?.let { f ->
val decl = f.declaringClass ?: instance.objClass.findDeclaringClassOf(name)
val caller = scope.currentClassCtx
if (!canAccessMember(f.visibility, decl, caller))
ObjIllegalAssignmentException(
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjAccessException(
scope,
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
).raise()
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (f.value.assign(scope, newValue) == null) f.value = newValue
return
}
@@ -377,8 +377,8 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val r = memberFromAncestor(name) ?: scope.raiseError("no such field: $name")
val decl = r.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(r.visibility, decl, caller))
ObjIllegalAssignmentException(scope, "can't assign to field $name (declared in ${decl.className})").raise()
if (!canAccessMember(r.effectiveWriteVisibility, decl, caller))
ObjAccessException(scope, "can't assign to field $name (declared in ${decl.className})").raise()
if (!r.isMutable) scope.raiseError("can't assign to read-only field: $name")
if (r.value.assign(scope, newValue) == null) r.value = newValue
}
@@ -26,12 +26,14 @@ data class ObjRecord(
var value: Obj,
val isMutable: Boolean,
val visibility: Visibility = Visibility.Public,
val writeVisibility: Visibility? = null,
/** If non-null, denotes the class that declared this member (field/method). */
val declaringClass: ObjClass? = null,
var importedFrom: Scope? = null,
val isTransient: Boolean = false,
val type: Type = Type.Other,
) {
val effectiveWriteVisibility: Visibility get() = writeVisibility ?: visibility
enum class Type(val comparable: Boolean = false,val serializable: Boolean = false) {
Field(true, true),
@Suppress("unused")
@@ -442,9 +442,9 @@ class ConstRef(private val record: ObjRecord) : ObjRef {
* Reference to an object's field with optional chaining.
*/
class FieldRef(
private val target: ObjRef,
private val name: String,
private val isOptional: Boolean,
val target: ObjRef,
val name: String,
val isOptional: Boolean,
) : ObjRef {
// 4-entry PIC for reads/writes (guarded by PerfFlags.FIELD_PIC)
// Reads
+97
View File
@@ -418,4 +418,101 @@ class OOTest {
""".trimIndent())
}
@Test
fun testLateInitValsInClasses() = runTest {
assertFails {
eval("""
class T {
val x
}
""")
}
assertFails {
eval("val String.late")
}
eval("""
// but we can "late-init" them in init block:
class OK {
val x
init {
x = "foo"
}
}
val ok = OK()
assertEquals("foo", ok.x)
// they can't be reassigned:
assertThrows(IllegalAssignmentException) {
ok.x = "bar"
}
// To test access before init, we need a trick:
class AccessBefore {
val x
fun readX() = x
init {
assertEquals(x, Unset)
// if we call readX() here, x is Unset.
// Just reading it is fine, but using it should throw:
assertThrows(UnsetException) { readX() + 1 }
x = 42
}
}
AccessBefore()
""".trimIndent())
}
@Test
fun testPrivateSet() = runTest {
eval("""
class A {
var y = 100
private set
fun setValue(newValue) { y = newValue }
}
assertEquals(100, A().y)
assertThrows(AccessException) { A().y = 200 }
val a = A()
a.setValue(200)
assertEquals(200, a.y)
class B(initial) {
var y = initial
protected set
}
class C(initial) : B(initial) {
fun setBValue(v) { y = v }
}
val c = C(10)
assertEquals(10, c.y)
assertThrows(AccessException) { c.y = 20 }
c.setBValue(30)
assertEquals(30, c.y)
class D {
private var _y = 0
var y
get() = _y
private set(v) { _y = v }
fun setY(v) { y = v }
}
val d = D()
assertEquals(0, d.y)
assertThrows(AccessException) { d.y = 10 }
d.setY(20)
assertEquals(20, d.y)
""")
}
@Test
fun testValPrivateSetError() = runTest {
assertFails {
eval("class E { val x = 1 private set }")
}
}
}
@@ -730,4 +730,124 @@ class LyngFormatterTest {
assertEquals(expected, formatted)
}
@Test
fun privateSet_indentation() {
val src = """
class A {
var x = 1
private set
}
""".trimIndent()
// What we expect: private set should be indented relative to the property
val expected = """
class A {
var x = 1
private set
}
""".trimIndent()
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
val out = LyngFormatter.reindent(src, cfg)
assertEquals(expected, out)
}
@Test
fun propertyAccessors_indentation() {
val src = """
class A {
var x
get() = 1
private set(v) {
_x = v
}
}
""".trimIndent()
val expected = """
class A {
var x
get() = 1
private set(v) {
_x = v
}
}
""".trimIndent()
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
val out = LyngFormatter.reindent(src, cfg)
assertEquals(expected, out)
}
@Test
fun nestedAwaitingIndentation() {
val src = """
if (cond)
if (other)
fun f() {
stmt
}
""".trimIndent()
val expected = """
if (cond)
if (other)
fun f() {
stmt
}
""".trimIndent()
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
val out = LyngFormatter.reindent(src, cfg)
assertEquals(expected, out)
}
@Test
fun propertyAccessor_followedByMethod() {
val src = """
var state: BarRequestState get() = getState()
set(value) = setState(value)
fun save() { cell.value = this }
""".trimIndent()
val expected = """
var state: BarRequestState get() = getState()
set(value) = setState(value)
fun save() { cell.value = this }
""".trimIndent()
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
val out = LyngFormatter.reindent(src, cfg)
assertEquals(expected, out)
}
@Test
fun propertyAccessor_dangling() {
val src1 = """
var x
get()
= 1
""".trimIndent()
val expected1 = """
var x
get()
= 1
""".trimIndent()
assertEquals(expected1, LyngFormatter.reindent(src1, LyngFormatConfig(indentSize = 4)))
val src2 = """
var x
get() =
1
""".trimIndent()
val expected2 = """
var x
get() =
1
""".trimIndent()
assertEquals(expected2, LyngFormatter.reindent(src2, LyngFormatConfig(indentSize = 4)))
}
}
@@ -28,10 +28,7 @@ import net.sergeych.lyng.binding.SymbolKind
import net.sergeych.lyng.highlight.HighlightKind
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.miniast.MiniAstBuilder
import net.sergeych.lyng.miniast.MiniClassDecl
import net.sergeych.lyng.miniast.MiniFunDecl
import net.sergeych.lyng.miniast.MiniTypeName
import net.sergeych.lyng.miniast.*
import org.w3c.dom.HTMLStyleElement
fun ensureBootstrapCodeBlocks(html: String): String {
@@ -367,6 +364,7 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
is MiniFunDecl -> putName(d.nameStart, d.name, "hl-fn")
is MiniClassDecl -> putName(d.nameStart, d.name, "hl-class")
is net.sergeych.lyng.miniast.MiniValDecl -> putName(d.nameStart, d.name, if (d.mutable) "hl-var" else "hl-val")
is MiniEnumDecl -> putName(d.nameStart, d.name, "hl-class")
}
}
// Imports: color each segment as directive/path
@@ -404,6 +402,7 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
}
is net.sergeych.lyng.miniast.MiniValDecl -> addTypeSegments(d.type)
is MiniClassDecl -> {}
is MiniEnumDecl -> {}
}
}
@@ -534,7 +533,8 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
"package", "import", "fun", "fn", "class", "enum", "val", "var",
"if", "else", "while", "do", "for", "when", "try", "catch", "finally",
"throw", "return", "break", "continue", "in", "is", "as", "as?", "not",
"true", "false", "null", "private", "protected", "open", "extern", "static"
"true", "false", "null", "private", "protected", "open", "extern", "static",
"init", "get", "set", "Unset", "by"
)
fun skipWs(idx0: Int): Int {
var idx = idx0
+13 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -127,7 +127,7 @@ fun ReferencePage() {
Ul({ classes("mt-2") }) {
d.members.forEach { m ->
when (m) {
is MiniMemberFunDecl, -> {
is MiniMemberFunDecl -> {
val params = m.params.joinToString(", ") { p ->
val ts = typeOf(p.type)
if (ts.isNotBlank()) "${p.name}${ts}" else p.name
@@ -150,6 +150,17 @@ fun ReferencePage() {
}
}
}
is MiniEnumDecl -> {
Div { Text("enum ${d.name}") }
d.doc?.summary?.let { Small({ classes("text-muted") }) { Text(it) } }
if (d.entries.isNotEmpty()) {
Ul({ classes("mt-2") }) {
d.entries.forEach { entry ->
Li { Text(entry) }
}
}
}
}
}
}
}