Compare commits

...

41 Commits

Author SHA1 Message Date
c12804a806 user exceptions to kotlin fixes 2026-01-08 09:21:25 +01:00
1d089db9ff User exception classes, unified exception class architecture 2026-01-07 19:05:07 +01:00
2ce6d8e482 tools for error reporting in kotlin 2026-01-07 18:12:14 +01:00
eda34c1b3d operators overriding missing files 2026-01-07 09:44:13 +01:00
2c0a6c7b34 operators overriding 2026-01-07 09:33:10 +01:00
fe5dded7af plugin with type declarations, collection types and much better type tracking for autocomplete 2026-01-07 02:55:47 +01:00
aba0048a83 plugin no wworks with .lyng.d files. hurray 2026-01-06 19:32:40 +01:00
fdc044d1e0 Added support for extern declarations and enhanced .lyng.d merging
- Implemented `extern` support for functions, classes, objects, enums, and properties in the `MiniAST`.
- Updated `MiniAST` to include `isExtern` field for applicable nodes.
- Enabled merging of `.lyng.d` declaration files into main `.lyng` scripts.
- Adjusted tests to validate `extern` behavior and documentation handling.
- Minor fixes to parser logic for improved robustness.
2026-01-06 17:05:03 +01:00
44675b976d script to piblish all (for Linux) 2026-01-06 13:26:58 +01:00
3941ddee40 + Ma is now Delegate
+ way to add pure Lyng parents when declaring kotlin bindings
2026-01-06 13:11:07 +01:00
3ef68d8bb4 fixed autocompletion for class constructor parameters 2026-01-06 12:08:23 +01:00
72bb6ae67b formatter fix on properties 2026-01-06 12:00:04 +01:00
555c9b94de null assignment operator added 2026-01-06 11:39:23 +01:00
8cd980514b code cleanup performed (trivial cases) 2026-01-06 10:38:31 +01:00
fa4dc45f15 readme: github specific stuff/2 2026-01-06 03:47:38 +01:00
ae68ecd066 readme: github specific stuff 2026-01-06 03:44:48 +01:00
ddfdc18ba3 more readme 2026-01-06 03:40:22 +01:00
1afc6d41bc more readme 2026-01-06 03:35:03 +01:00
83ab8cdc01 updated readme for hub 2026-01-06 03:11:19 +01:00
26b8370b01 added label syntax support and highlighting 2026-01-06 02:37:16 +01:00
660a80a26b lyngio: added processes and docs for it, JVM and linux 2026-01-06 02:35:24 +01:00
d91acd593a added return statement support 2026-01-06 02:18:31 +01:00
5fc0969491 added object expression support to the site, tnbundle, etc 2026-01-06 01:51:16 +01:00
eec732d11a implemented object expressions (anonymous classes) 2026-01-06 01:25:01 +01:00
8611543623 fixed delegation sample 2026-01-06 01:00:20 +01:00
73854f21f3 reformatted root.lyng for better readability/2 2026-01-05 22:30:51 +01:00
f66e61c185 reformatted root.lyng for better readability 2026-01-05 22:27:02 +01:00
41a3617850 improved lazy delegate; added with(newThis) {} to stdlib 2026-01-05 22:25:46 +01:00
f792c73b8f 1.1.1-SNPASHOT some serious bugs in initilazation fixed. They were revealed by delegation real world usage 2026-01-05 21:13:42 +01:00
20f777f9f6 version on site 2026-01-05 19:09:46 +01:00
5f819dc87a Added delegation support: Delegate, lazy, "by" keyword for valr/var/fun and object {} singletons 2026-01-05 19:05:16 +01:00
8e766490d9 AccessException -> IllegalAccessException in docs 2026-01-05 11:20:38 +01:00
514ad96148 AccessException -< IllegalAccessException; added TODO() and NotImplementedException 2026-01-05 11:14:05 +01:00
e0a59c8db6 cleaning up OOTest 2026-01-05 10:35:38 +01:00
75e2b63923 fixed fences in the docs 2026-01-04 23:06:36 +01:00
5f1e6564d5 site: fixed scrolling overly high contents table in docs viewer 2026-01-04 23:01:11 +01:00
f5a3fbe9a3 docs refined 2026-01-04 22:50:49 +01:00
11eadc1d9f abstract classes, interfaces, MI auto implementation and fine-grained visibility 2026-01-04 22:30:24 +01:00
96e1ffc7d5 fixed oop.md spelling 2026-01-04 20:18:49 +01:00
aad7c9619b fixed bug with reformatting code sequences in markdown + lyng 2026-01-04 13:37:23 +01:00
9e138367ef plugin fixes: incorrect reformatting on }, incorrect parsing of class minidocs 2026-01-04 11:58:55 +01:00
143 changed files with 8283 additions and 1809 deletions

6
.gitignore vendored
View File

@ -20,3 +20,9 @@ xcuserdata
.output*.txt
debug.log
/build.log
/test.md
/build_output.txt
/build_output_full.txt
/check_output.txt
/compile_jvm_output.txt
/compile_metadata_output.txt

View File

@ -2,6 +2,26 @@
### Unreleased
- Language: Added `return` statement
- `return [expression]` exits the innermost enclosing callable (function or lambda).
- Supports non-local returns using `@label` syntax (e.g., `return@outer 42`).
- Named functions automatically provide their name as a label for non-local returns.
- Labeled lambdas: lambdas can be explicitly labeled using `@label { ... }`.
- Restriction: `return` is forbidden in shorthand function definitions (e.g., `fun f(x) = return x` is a syntax error).
- Control Flow: `return` and `break` are now protected from being caught by user-defined `try-catch` blocks in Lyng.
- Documentation: New `docs/return_statement.md` and updated `tutorial.md`.
- Language: stdlib improvements
- Added `with(self, block)` function to `root.lyng` which executes a block with `this` set to the provided object.
- Language: Abstract Classes and Interfaces
- Support for `abstract` modifier on classes, methods, and variables.
- Introduced `interface` as a synonym for `abstract class`, supporting full state (constructors, fields, `init` blocks) and implementation by parts via MI.
- New `closed` modifier (antonym to `open`) to prevent overriding class members.
- Refined `override` logic: mandatory keyword when re-declaring members that exist in the ancestor chain (MRO).
- MI Satisfaction: Abstract requirements are automatically satisfied by matching concrete members found later in the C3 MRO chain without requiring explicit proxy methods.
- Integration: Updated highlighters (lynglib, lyngweb, IDEA plugin), IDEA completion, and Grazie grammar checking.
- Documentation: Updated `docs/OOP.md` with sections on "Abstract Classes and Members", "Interfaces", and "Overriding and Virtual Dispatch".
- Language: Class properties with accessors
- Support for `val` (read-only) and `var` (read-write) properties in classes.
- Syntax: `val name [ : Type ] get() { body }` or `var name [ : Type ] get() { body } set(value) { body }`.
@ -100,6 +120,7 @@ All notable changes to this project will be documented in this 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).
- Fix: Formatting Markdown files no longer deletes content in `.lyng` code fences and works correctly with injected files (resolves clobbering, `StringIndexOutOfBoundsException`, and `nonempty text is not covered by block` errors).
- CLI: Preserved legacy script invocation fast-paths:
- `lyng script.lyng [args...]` executes the script directly.

View File

@ -1,6 +1,14 @@
# Lyng: modern scripting for kotlin multiplatform
# Lyng: ideal scripting for kotlin multiplatform
__Please visit the project homepage: [https://lynglang.com](https://lynglang.com) and a [telegram channel](https://t.me/lynglang).__
__Main development site:__ [https://gitea.sergeych.net/SergeychWorks/lyng](https://gitea.sergeych.net/SergeychWorks/lyng)
__github mirror__: [https://github.com/sergeych/lyng](https://github.com/sergeych/lyng)
We keep github as a mirror and backup for the project, while the main development site is hosted on gitea.sergeych.net. We use gitea for issues and pull requests, and as a main point of trust, as github access now is a thing that can momentarily be revoked for no apparent reason.
We encourage using the github if the main site is not accessible from your country and vice versa. We recommend to `publishToMavenLocal` and not depend on politics.
Please visit the project homepage: [https://lynglang.com](https://lynglang.com) and a [telegram channel](https://t.me/lynglang) for updates.
- simple, compact, intuitive and elegant modern code:
@ -21,22 +29,9 @@ fun swapEnds(first, args..., last, f) {
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
- 100% secure: no access to any API you didn't explicitly provide
- 100% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]
```
val deferred = launch {
delay(1.5) // coroutine is delayed for 1.5s, thread is not blocked!
"done"
}
// ...
// suspend current coroutine, no thread is blocked again,
// and wait for deferred to return something:
assertEquals("donw", deferred.await())
```
and it is multithreaded on platforms supporting it (automatically, no code changes required, just
`launch` more coroutines and they will be executed concurrently if possible). See [parallelism]
- functional style and OOP together, multiple inheritance, implementing interfaces for existing classes, writing extensions.
- 100% coroutines! Every function/script is a coroutine, it does not block the thread, no async/await/suspend keyword garbage, see [parallelism]. it is multithreaded on platforms supporting it (automatically, no code changes required, just `launch` more coroutines and they will be executed concurrently if possible). See [parallelism]
- functional style and OOP together: multiple inheritance (so you got it all - mixins, interfaces, etc.), delegation, sigletons, anonymous classes,extensions.
- nice literals for maps and arrays, destructuring assignment, ranges.
- Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`.
## Resources:
@ -44,6 +39,8 @@ and it is multithreaded on platforms supporting it (automatically, no code chang
- [Language home](https://lynglang.com)
- [introduction and tutorial](docs/tutorial.md) - start here please
- [Testing and Assertions](docs/Testing.md)
- [Filesystem and Processes (lyngio)](docs/lyngio.md)
- [Return Statement](docs/return_statement.md)
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
- [Samples directory](docs/samples)
- [Formatter (core + CLI + IDE)](docs/formatter.md)
@ -79,7 +76,7 @@ Now you can import lyng and use it:
### Execute script:
```kotlin
import net.sergeyh.lyng.*
import net.sergeych.lyng.*
// we need a coroutine to start, as Lyng
// is a coroutine based language, async topdown
@ -95,9 +92,7 @@ Script is executed over some `Scope`. Create instance,
add your specific vars and functions to it, and call:
```kotlin
import com.sun.source.tree.Scope
import new.sergeych.lyng.*
import net.sergeych.lyng.*
// simple function
val scope = Script.newScope().apply {
@ -176,6 +171,7 @@ Ready features:
- [x] ranges, lists, strings, interfaces: Iterable, Iterator, Collection, Array
- [x] when(value), if-then-else
- [x] exception handling: throw, try-catch-finally, exception classes.
- [x] user-defined exception classes
- [x] multiplatform maven publication
- [x] documentation for the current state
- [x] maps, sets and sequences (flows?)
@ -191,6 +187,17 @@ Ready features:
- [x] regular exceptions + extended `when`
- [x] multiple inheritance for user classes
- [x] class properties (accessors)
- [x] `return` statement for local and non-local exit
- [x] Unified Delegation model: val, var and fun
- [x] `lazy val` using delegation
- [x] singletons `object TheOnly { ... }`
- [x] object expressions `object: List { ... }`
- [x] late-init vals in classes
- [x] properties with getters and setters
- [x] assign-if-null operator `?=`
- [x] user-defined exception classes
All of this is documented in the [language site](https://lynglang.com) and locally [docs/language.md](docs/tutorial.md). the current nightly builds published on the site and in the private maven repository.
## plan: towards v1.5 Enhancing

4
archived/README.md Normal file
View File

@ -0,0 +1,4 @@
# Obsolete files
__Do not rely on contents of the files in this directory. They are kept for historical reference only and may not be up-to-date or relevant.__

View File

@ -0,0 +1,117 @@
/*
This is a tech proposal under construction, please do not use it yet
for any purpose
*/
/*
Abstract delegate can be used to proxy read/wrtie field access
or method call. Default implementation reports error.
*/
interface Delegate {
fun getValue() = Unset
fun setValue(newValue) { throw NotImplementedException("delegate setter is not implemented") }
fun invoke(args...) { throw NotImplementedException("delegate setter is not implemented") }
}
/*
Delegate cam be used to implement a val, var or fun, so there are
access type enum to distinguish:
*/
enum DelegateAccess {
Val,
Var,
Callable
}
// Delegate can be associated by a val/var/fun in a declaraion site using `by` keyword
val proxiedVal by proxy(1)
var proxiedVar by proxy(2, 3)
fun proxiedFun by proxy()
// each proxy is a Lyng expression returning instance of the Proxy interface:
/*
Proxy interface is connecting some named property of a given kind with the `Delegate`.
It removes the burden of dealing with property name and this ref on each get/set value
or invoke allowing having one delegate per instance, execution buff.
*/
interface Proxy {
fun getDelegate(propertyName: String,access: DelegateAccess,thisRef: Obj?): Delegate
}
// val, var and fun can be delegated, local or class instance:
class TestProxy: Proxy {
override getDelegate(name,access,thisRef) {
Delegate()
}
}
val proxy = TestProxy()
class Allowed {
val v1 by proxy
var v2 by proxy
fun f1 by proxy
}
val v3 by proxy
var v4 by proxy
fun f2 by proxy
/*
It means that for example
Allowed().f1("foo")
would call a delegate.invoke("foo") on the `Delegate` instance supplied by `proxy`, etc.
*/
// The practic sample: lazy value
/*
The delegate that caches single time evaluated value
*/
class LazyDelegate(creator): Delegate {
private var currentValue=Unset
override fun getValue() {
if( currentValue == Unset )
currentValue = creator()
currentValue
}
}
/*
The proxy to assign it
*/
class LazyProxy(creator) {
fun getDelegate(name,access,thisRef) {
if( access != DelegateAccess.Val )
throw IllegalArgumentException("only lazy val are allowed")
LazyDelegate(creator)
}
}
/*
A helper function to simplify creation:
*/
fun lazy(creator) {
LazyProxy(creator)
}
// Usage sample and the test:
var callCounter = 0
assertEquals(0, clallCounter)
val lazyText by lazy { "evaluated text" }
// the lazy property is not yet evaluated:
assertEquals(0, clallCounter)
// now evaluate it by using it:
assertEquals("evaluated text", lazyText)
assertEquals(1, callCounter)
// lazy delegate should fail on vars or funs:
assertThrows { var bad by lazy { "should not happen" } }
assertThrows { fun bad by lazy { 42 } }

40
bin/deploy_all Executable file
View File

@ -0,0 +1,40 @@
#!/bin/bash
#
# 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.
#
#
set -e
echo "publishing all artifacts"
echo
./gradlew publishToMavenLocal
./gradlew publish
echo
echo "Creating plugin"
echo
./gradlew buildInstallablePlugin
echo
echo "building CLI tools"
echo
bin/local_jrelease
bin/local_release
echo
echo "Deploying site"
echo
./bin/deploy_site

View File

@ -42,6 +42,77 @@ a _constructor_ that requires two parameters for fields. So when creating it wit
Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the
example above.
## Singleton Objects
Singleton objects are declared using the `object` keyword. An `object` declaration defines both a class and a single instance of that class at the same time. This is perfect for stateless utilities, global configuration, or shared delegates.
```lyng
object Config {
val version = "1.0.0"
val debug = true
fun printInfo() {
println("App version: " + version)
}
}
// Usage:
println(Config.version)
Config.printInfo()
```
Objects can also inherit from classes or interfaces:
```lyng
object DefaultLogger : Logger("Default") {
override fun log(msg) {
println("[DEFAULT] " + msg)
}
}
```
## Object Expressions
Object expressions allow you to create an instance of an anonymous class. This is useful when you need to provide a one-off implementation of an interface or inherit from a class without declaring a new named subclass.
```lyng
val worker = object : Runnable {
override fun run() {
println("Working...")
}
}
```
Object expressions can implement multiple interfaces and inherit from one class:
```lyng
val x = object : Base(arg1), Interface1, Interface2 {
val property = 42
override fun method() = property * 2
}
```
### Scoping and `this@object`
Object expressions capture their lexical scope, meaning they can access local variables and members of the outer class. When `this` is rebound (for example, inside an `apply` block), you can use the reserved alias `this@object` to refer to the innermost anonymous object instance.
```lyng
val handler = object {
fun process() {
this@object.apply {
// here 'this' is rebound to the map/context
// but we can still access the anonymous object via this@object
println("Processing in " + this@object)
}
}
}
```
### Serialization and Identity
- **Serialization**: Anonymous objects are **not serializable**. Attempting to encode an anonymous object via `Lynon` will throw a `SerializationException`. This is because their class definition is transient and cannot be safely restored in a different session or process.
- **Type Identity**: Every object expression creates a unique anonymous class. Two identical object expressions will result in two different classes with distinct type identities.
## Properties
Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors.
@ -50,7 +121,7 @@ Properties allow you to define member accessors that look like fields but execut
Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and `get()`/`set()` blocks:
```kotlin
```lyng
class Person(private var _age: Int) {
// Read-only property
val ageCategory
@ -76,7 +147,7 @@ assertEquals("Adult", p.ageCategory)
For simple accessors and methods, you can use the `=` shorthand for a more elegant and laconic form:
```kotlin
```lyng
class Circle(val radius: Real) {
val area get() = π * radius * radius
val circumference get() = 2 * π * radius
@ -104,7 +175,7 @@ class Counter {
When you want to define a property that is computed only once (on demand) and then remembered, use the built-in `cached` function. This is more efficient than a regular property with `get()` if the computation is expensive, as it avoids re-calculating the value on every access.
```kotlin
```lyng
class DataService(val id: Int) {
// The lambda passed to cached is only executed once, the first time data() is called.
val data = cached {
@ -122,6 +193,52 @@ println(service.data()) // Returns "Record 42" immediately (no second fetch)
Note that `cached` returns a lambda, so you access the value by calling it like a method: `service.data()`. This is a powerful pattern for lazy-loading resources, caching results of database queries, or delaying expensive computations until they are truly needed.
## Delegation
Delegation allows you to hand over the logic of a property or function to another object. This is done using the `by` keyword.
### Property Delegation
Instead of providing `get()` and `set()` accessors, you can delegate them to an object that implements the `getValue` and `setValue` methods.
```lyng
class User {
var name by MyDelegate()
}
```
### Function Delegation
You can also delegate a whole function to an object. When the function is called, it will invoke the delegate's `invoke` method.
```lyng
fun remoteAction by RemoteProxy("actionName")
```
### The Unified Delegate Interface
A delegate is any object that provides the following methods (all optional depending on usage):
- `getValue(thisRef, name)`: Called when a delegated `val` or `var` is read.
- `setValue(thisRef, name, newValue)`: Called when a delegated `var` is written.
- `invoke(thisRef, name, args...)`: Called when a delegated `fun` is invoked.
- `bind(name, access, thisRef)`: Called once during initialization to configure or validate the delegate.
### Map as a Delegate
Maps can also be used as delegates. When delegated to a property, the map uses the property name as the key:
```lyng
val settings = { "theme": "dark", "fontSize": 14 }
val theme by settings
var fontSize by settings
println(theme) // "dark"
fontSize = 16 // Updates settings["fontSize"]
```
For more details and advanced patterns (like `lazy`, `observable`, and shared stateless delegates), see the [Delegation Guide](delegation.md).
## Instance initialization: init block
In addition to the primary constructor arguments, you can provide an `init` block that runs on each instance creation. This is useful for more complex initializations, side effects, or setting up fields that depend on multiple constructor parameters.
@ -179,7 +296,7 @@ Note that unlike **Kotlin**, which uses `=` for named arguments, Lyng uses `:` t
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
```lyng
class DataProcessor(data: Object) {
val result: Object
@ -200,7 +317,7 @@ Key rules for late-init `val`:
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
```lyng
class T {
val x
fun check() {
@ -237,7 +354,7 @@ Functions defined inside a class body are methods, and unless declared
Lyng supports declaring a class with multiple direct base classes. The syntax is:
```
```lyng
class Foo(val a) {
var tag = "F"
fun runA() { "ResultA:" + a }
@ -286,16 +403,16 @@ Key rules and features:
- Syntax
- `class Derived(args) : Base1(b1Args), Base2(b2Args)`
- Each direct base may receive constructor arguments specified in the header. Only direct bases receive header args; indirect bases must either be default‑constructible or receive their args through their direct child (future extensions may add more control).
- Each direct base may receive constructor arguments specified in the header. Only direct bases receive header args; indirect bases must either be default‑constructible or receive their args through their direct child.
- Resolution order (C3 MRO — active)
- Resolution order (C3 MRO)
- Member lookup is deterministic and follows C3 linearization (Python‑like), which provides a monotonic, predictable order for complex hierarchies and diamonds.
- Intuition: for `class D() : B(), C()` where `B()` and `C()` both derive from `A()`, the C3 order is `D → B → C → A`.
- The first visible match along this order wins.
- Qualified dispatch
- Inside a class body, use `this@Type.member(...)` to start lookup at the specified ancestor.
- For arbitrary receivers, use casts: `(expr as Type).member(...)` or `(expr as? Type)?.member(...)` (safe‑call `?.` is already available in Lyng).
- For arbitrary receivers, use casts: `(expr as Type).member(...)` or `(expr as? Type)?.member(...)`.
- Qualified access does not relax visibility.
- Field inheritance (`val`/`var`) and collisions
@ -312,10 +429,213 @@ Key rules and features:
- `private`: accessible only inside the declaring class body; not visible in subclasses and cannot be accessed via `this@Type` or casts.
- `protected`: accessible in the declaring class and in any of its transitive subclasses (including MI), but not from unrelated contexts; qualification/casts do not bypass it.
- Diagnostics
- When a member/field is not found, error messages include the receiver class name and the considered linearization order, with suggestions to disambiguate using `this@Type` or casts if appropriate.
- Qualifying with a non‑ancestor in `this@Type` reports a clear error mentioning the receiver lineage.
- `as`/`as?` cast errors mention the actual and target types.
## Abstract Classes and Members
An `abstract` class is a class that cannot be instantiated and is intended to be inherited by other classes. It can contain `abstract` members that have no implementation and must be implemented by concrete subclasses.
### Abstract Classes
To declare an abstract class, use the `abstract` modifier:
```lyng
abstract class Shape {
abstract fun area(): Real
}
```
Abstract classes can have constructors, fields, and concrete methods, just like regular classes.
### Abstract Members
Methods and variables (`val`/`var`) can be marked as `abstract`. Abstract members must not have a body or initializer.
```lyng
abstract class Base {
abstract fun foo(): Int
abstract var bar: String
}
```
- **Safety**: `abstract` members cannot be `private`, as they must be visible to subclasses for implementation.
- **Contract of Capability**: An `abstract val/var` represents a requirement for a capability. It can be implemented by either a **field** (storage) or a **property** (logic) in a subclass.
## Interfaces
An `interface` in Lyng is a synonym for an `abstract class`. Following the principle that Lyng's Multiple Inheritance system is powerful enough to handle stateful contracts, interfaces support everything classes do, including constructors, fields, and `init` blocks.
```lyng
interface Named(val name: String) {
fun greet() { "Hello, " + name }
}
class Person(name) : Named(name)
```
Using `interface` instead of `abstract class` is a matter of semantic intent, signaling that the class is primarily intended to be used as a contract in MI.
### Implementation by Parts
One of the most powerful benefits of Lyng's Multiple Inheritance and C3 MRO is the ability to satisfy an interface's requirements "by parts" from different parent classes. Since an `interface` can have state and requirements, a subclass can inherit these requirements and satisfy them using members inherited from other parents in the MRO chain.
Example:
```lyng
// Interface with state (id) and abstract requirements
interface Character(val id) {
var health
var mana
fun isAlive() = health > 0
fun status() = name + " (#" + id + "): " + health + " HP, " + mana + " MP"
}
// Parent class 1: provides health
class HealthPool(var health)
// Parent class 2: provides mana and name
class ManaPool(var mana) {
val name = "Hero"
}
// Composite class: implements Character by combining HealthPool and ManaPool
class Warrior(id, h, m) : HealthPool(h), ManaPool(m), Character(id)
val w = Warrior(1, 100, 50)
assertEquals("Hero (#1): 100 HP, 50 MP", w.status())
```
In this example, `Warrior` inherits from `HealthPool`, `ManaPool`, and `Character`. The abstract requirements `health` and `mana` from `Character` are automatically satisfied by the matching members inherited from `HealthPool` and `ManaPool`. The `status()` method also successfully finds the `name` field from `ManaPool`. This pattern allows for highly modular and reusable "trait-like" classes that can be combined to fulfill complex contracts without boilerplate proxy methods.
## Overriding and Virtual Dispatch
When a class defines a member that already exists in one of its parents, it is called **overriding**.
### The `override` Keyword
In Lyng, the `override` keyword is **mandatory when declaring a member** that exists in the ancestor chain (MRO).
```lyng
class Parent {
fun foo() = 1
}
class Child : Parent() {
override fun foo() = 2 // Mandatory override keyword
}
```
- **Implicit Satisfaction**: If a class inherits an abstract requirement and a matching implementation from different parents, the requirement is satisfied automatically without needing an explicit `override` proxy.
- **No Accidental Overrides**: If you define a member that happens to match a parent's member but you didn't use `override`, the compiler will throw an error. This prevents the "Fragile Base Class" problem.
- **Private Members**: Private members in parent classes are NOT part of the virtual interface and cannot be overridden. Defining a member with the same name in a subclass is allowed without `override` and is treated as a new, independent member.
### Visibility Widening
A subclass can increase the visibility of an overridden member (e.g., `protected``public`), but it is strictly forbidden from narrowing it (e.g., `public``protected`).
### The `closed` Modifier
To prevent a member from being overridden in subclasses, use the `closed` modifier (equivalent to `final` in other languages).
```lyng
class Critical {
closed fun secureStep() { ... }
}
```
Attempting to override a `closed` member results in a compile-time error.
## Operator Overloading
Lyng allows you to overload standard operators by defining specific named methods in your classes. When an operator expression is evaluated, Lyng delegates the operation to these methods if they are available.
### Binary Operators
To overload a binary operator, define the corresponding method that takes one argument:
| Operator | Method Name |
| :--- | :--- |
| `a + b` | `plus(other)` |
| `a - b` | `minus(other)` |
| `a * b` | `mul(other)` |
| `a / b` | `div(other)` |
| `a % b` | `mod(other)` |
| `a && b` | `logicalAnd(other)` |
| `a \|\| b` | `logicalOr(other)` |
| `a =~ b` | `operatorMatch(other)` |
| `a & b` | `bitAnd(other)` |
| `a \| b` | `bitOr(other)` |
| `a ^ b` | `bitXor(other)` |
| `a << b` | `shl(other)` |
| `a >> b` | `shr(other)` |
Example:
```lyng
class Vector(val x, val y) {
fun plus(other) = Vector(x + other.x, y + other.y)
override fun toString() = "Vector(${x}, ${y})"
}
val v1 = Vector(1, 2)
val v2 = Vector(3, 4)
assertEquals(Vector(4, 6), v1 + v2)
```
### Unary Operators
Unary operators are overloaded by defining methods with no arguments:
| Operator | Method Name |
| :--- | :--- |
| `-a` | `negate()` |
| `!a` | `logicalNot()` |
| `~a` | `bitNot()` |
### Assignment Operators
Assignment operators like `+=` first attempt to call a specific assignment method. If that method is not defined, they fall back to a combination of the binary operator and a regular assignment (e.g., `a = a + b`).
| Operator | Method Name | Fallback |
| :--- | :--- | :--- |
| `a += b` | `plusAssign(other)` | `a = a + b` |
| `a -= b` | `minusAssign(other)` | `a = a - b` |
| `a *= b` | `mulAssign(other)` | `a = a * b` |
| `a /= b` | `divAssign(other)` | `a = a / b` |
| `a %= b` | `modAssign(other)` | `a = a % b` |
Example of in-place mutation:
```lyng
class Counter(var value) {
fun plusAssign(n) {
value = value + n
}
}
val c = Counter(10)
c += 5
assertEquals(15, c.value)
```
### Comparison Operators
Comparison operators use `compareTo` and `equals`.
| Operator | Method Name |
| :--- | :--- |
| `a == b`, `a != b` | `equals(other)` |
| `<`, `>`, `<=`, `>=`, `<=>` | `compareTo(other)` |
- `compareTo` should return:
- `0` if `a == b`
- A negative integer if `a < b`
- A positive integer if `a > b`
- The `<=>` (shuttle) operator returns the result of `compareTo` directly.
- `equals` returns a `Bool`. If `equals` is not explicitly defined, Lyng falls back to `compareTo(other) == 0`.
> **Note**: Methods that are already defined in the base `Obj` class (like `equals`, `toString`, or `contains`) require the `override` keyword when redefined in your class or as an extension. Other operator methods (like `plus` or `negate`) do not require `override` unless they are already present in your class's hierarchy.
### Increment and Decrement
`++` and `--` operators are implemented using `plus(1)` or `minus(1)` combined with an assignment back to the variable. If the variable is a field or local variable, it will be updated with the result of the operation.
Compatibility notes:
@ -400,6 +720,23 @@ Notes and limitations (current version):
- `name` and `ordinal` are read‑only properties of an entry.
- `entries` is a read‑only list owned by the enum type.
## Exception Classes
You can define your own exception classes by inheriting from the built-in `Exception` class. User-defined exceptions are regular classes and can have their own properties and methods.
```lyng
class MyError(val code, m) : Exception(m)
try {
throw MyError(500, "Internal Server Error")
}
catch(e: MyError) {
println("Error " + e.code + ": " + e.message)
}
```
For more details on error handling, see the [Exceptions Handling Guide](exceptions_handling.md).
## fields and visibility
It is possible to add non-constructor fields:
@ -438,7 +775,7 @@ You can restrict the visibility of a `var` field's or property's setter by using
#### On Fields
```kotlin
```lyng
class SecretCounter {
var count = 0
private set // Can be read anywhere, but written only in SecretCounter
@ -448,7 +785,7 @@ class SecretCounter {
val c = SecretCounter()
println(c.count) // OK
c.count = 10 // Throws AccessException
c.count = 10 // Throws IllegalAccessException
c.increment() // OK
```
@ -456,7 +793,7 @@ c.increment() // OK
You can also apply restricted visibility to custom property setters:
```kotlin
```lyng
class Person(private var _age: Int) {
var age
get() = _age
@ -468,7 +805,7 @@ class Person(private var _age: Int) {
A `protected set` allows subclasses to modify a field that is otherwise read-only to the public:
```kotlin
```lyng
class Base {
var state = "initial"
protected set
@ -484,7 +821,7 @@ 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
d.state = "bad" // Throws IllegalAccessException: public write not allowed
```
### Key Rules and Limitations
@ -651,7 +988,7 @@ Just like methods, you can extend existing classes with properties. These can be
A read-only extension can be defined by assigning an expression:
```kotlin
```lyng
val String.isLong = length > 10
val s = "Hello, world!"
@ -662,7 +999,7 @@ assert(s.isLong)
For more complex logic, use `get()` and `set()` blocks:
```kotlin
```lyng
class Box(var value: Int)
var Box.doubledValue
@ -685,7 +1022,7 @@ Extensions in Lyng are **scope-isolated**. This means an extension is only visib
You can define different extensions with the same name in different scopes:
```kotlin
```lyng
fun scopeA() {
val Int.description = "Number: " + toString()
assertEquals("Number: 42", 42.description)
@ -822,12 +1159,12 @@ request](https://gitea.sergeych.net/SergeychWorks/lyng/issues).
- ObjClass sole parent is Obj
- ObjClass contains code for instance methods, class fields, hierarchy information.
- Class information is also scoped.
- We acoid imported classes duplication using packages and import caching, so the same imported module is the same object in all its classes.
- We avoid imported classes duplication using packages and import caching, so the same imported module is the same object in all its classes.
## Instances
Result of executing of any expression or statement in the Lyng is the object that
inherits `Obj`, but is not `Obj`. For example it could be Int, void, null, real, string, bool, etc.
inherits `Obj`, but is not `Obj`. For example, it could be Int, void, null, real, string, bool, etc.
This means whatever expression returns or the variable holds, is the first-class
object, no differenes. For example:

View File

@ -93,8 +93,6 @@ If we want to evaluate the message lazily:
In this case, formatting will only occur if the condition is not met.
### `check`
check(condition, message="check failed")
@ -107,3 +105,17 @@ With lazy message evaluation:
In this case, formatting will only occur if the condition is not met.
### TODO
It is easy to mark some code and make it throw a special exception at cone with:
TODO()
or
TODO("some message")
It raises an `NotImplementedException` with the given message. You can catch it
as any other exception when necessary.
Many IDE and editors have built-in support for marking code with TODOs.

194
docs/delegation.md Normal file
View File

@ -0,0 +1,194 @@
# Delegation in Lyng
Delegation is a powerful pattern that allows you to outsource the logic of properties (`val`, `var`) and functions (`fun`) to another object. This enables code reuse, separation of concerns, and the implementation of common patterns like lazy initialization, observable properties, and remote procedure calls (RPC) with minimal boilerplate.
## The `by` Keyword
Delegation is triggered using the `by` keyword in a declaration. The expression following `by` is evaluated once when the member is initialized, and the resulting object becomes the **delegate**.
```lyng
val x by MyDelegate()
var y by MyDelegate()
fun f by MyDelegate()
```
## The Unified Delegate Model
A delegate object can implement any of the following methods to intercept member access. All methods receive the `thisRef` (the instance containing the member) and the `name` of the member.
```lyng
interface Delegate {
// Called when a 'val' or 'var' is read
fun getValue(thisRef, name)
// Called when a 'var' is assigned
fun setValue(thisRef, name, newValue)
// Called when a 'fun' is invoked
fun invoke(thisRef, name, args...)
// Optional: Called once during initialization to "bind" the delegate
// Can be used for validation or to return a different delegate instance
fun bind(name, access, thisRef) = this
}
```
### Delegate Access Types
The `bind` method receives an `access` parameter of type `DelegateAccess`, which can be one of:
- `DelegateAccess.Val`
- `DelegateAccess.Var`
- `DelegateAccess.Callable` (for `fun`)
## Usage Cases and Examples
### 1. Lazy Initialization
The classic `lazy` pattern ensures a value is computed only when first accessed and then cached. In Lyng, `lazy` is implemented as a class that follows this pattern. While classes typically start with an uppercase letter, `lazy` is an exception to make its usage feel like a native language feature.
```lyng
class lazy(val creator) : Delegate {
private var value = Unset
override fun bind(name, access, thisRef) {
if (access != DelegateAccess.Val) throw "lazy delegate can only be used with 'val'"
this
}
override fun getValue(thisRef, name) {
if (value == Unset) {
// calculate value using thisRef as this:
value = with(thisRef) creator()
}
value
}
}
// Usage:
val expensiveData by lazy {
println("Performing expensive computation...")
42
}
println(expensiveData) // Computes and prints 42
println(expensiveData) // Returns 42 immediately
```
### 2. Observable Properties
Delegates can be used to react to property changes.
```lyng
class Observable(initialValue, val onChange) {
private var value = initialValue
fun getValue(thisRef, name) = value
fun setValue(thisRef, name, newValue) {
val oldValue = value
value = newValue
onChange(name, oldValue, newValue)
}
}
class User {
var name by Observable("Guest") { name, old, new ->
println("Property %s changed from %s to %s"(name, old, new))
}
}
val u = User()
u.name = "Alice" // Prints: Property name changed from Guest to Alice
```
### 3. Function Delegation (Proxies)
You can delegate an entire function to an object. This is particularly useful for implementing decorators or RPC clients.
```lyng
object LoggerDelegate {
fun invoke(thisRef, name, args...) {
println("Calling function: " + name + " with args: " + args)
// Logic here...
"Result of " + name
}
}
fun remoteAction by LoggerDelegate
println(remoteAction(1, 2, 3))
// Prints: Calling function: remoteAction with args: [1, 2, 3]
// Prints: Result of remoteAction
```
### 4. Stateless Delegates (Shared Singletons)
Because `getValue`, `setValue`, and `invoke` receive `thisRef`, a single object can act as a delegate for multiple properties across many instances without any per-property memory overhead.
```lyng
object Constant42 {
fun getValue(thisRef, name) = 42
}
class Foo {
val a by Constant42
val b by Constant42
}
val f = Foo()
assertEquals(42, f.a)
assertEquals(42, f.b)
```
### 5. Local Delegation
Delegation is not limited to class members; you can also use it for local variables inside functions.
```lyng
fun test() {
val x by LocalProxy(123)
println(x)
}
```
### 6. Map as a Delegate
Maps can be used as delegates for `val` and `var` properties. When a map is used as a delegate, it uses the property name as a key to read from or write to the map.
```lyng
val m = { "a": 1, "b": 2 }
val a by m
var b by m
println(a) // 1
println(b) // 2
b = 42
println(m["b"]) // 42
```
Because `Map` implements `getValue` and `setValue`, it works seamlessly with any object that needs to store its properties in a map (e.g., when implementing dynamic schemas or JSON-backed objects).
## The `bind` Hook
The `bind(name, access, thisRef)` method is called exactly once when the member is being initialized. It allows the delegate to:
1. **Validate usage**: Throw an error if the delegate is used with the wrong member type (e.g., `lazy` on a `var`).
2. **Initialize state**: Set up internal state based on the property name or the containing instance.
3. **Substitute itself**: Return a different object that will act as the actual delegate.
```lyng
class ValidatedDelegate() {
fun bind(name, access, thisRef) {
if (access == DelegateAccess.Var) {
throw "This delegate cannot be used with 'var'"
}
this
}
fun getValue(thisRef, name) = "Validated"
}
```
## Summary
Delegation in Lyng combines the elegance of Kotlin-style properties with the flexibility of dynamic function interception. By unifying `val`, `var`, and `fun` delegation into a single model, Lyng provides a consistent and powerful tool for meta-programming and code reuse.

View File

@ -286,6 +286,45 @@ val isolated = net.sergeych.lyng.Scope.new()
- When registering packages, names must be unique. Register before you compile/evaluate scripts that import them.
- To debug scope content, `scope.toString()` and `scope.trace()` can help during development.
### 12) Handling and serializing exceptions
When Lyng code throws an exception, it is caught in Kotlin as an `ExecutionError`. This error wraps the actual Lyng `Obj` that was thrown (which could be a built-in `ObjException` or a user-defined `ObjInstance`).
To simplify handling these objects from Kotlin, several extension methods are provided on the `Obj` class. These methods work uniformly regardless of whether the exception is built-in or user-defined.
#### Uniform Exception API
| Method | Description |
| :--- | :--- |
| `obj.isLyngException()` | Returns `true` if the object is an instance of `Exception`. |
| `obj.isInstanceOf("ClassName")` | Returns `true` if the object is an instance of the named Lyng class or its ancestors. |
| `obj.getLyngExceptionMessage(scope)` | Returns the exception message as a Kotlin `String`. |
| `obj.getLyngExceptionString(scope)` | Returns a formatted string including the class name, message, and primary throw site. |
| `obj.getLyngExceptionStackTrace(scope)` | Returns the stack trace as an `ObjList` of `StackTraceEntry`. |
| `obj.getLyngExceptionExtraData(scope)` | Returns the extra data associated with the exception. |
| `obj.raiseAsExecutionError(scope)` | Rethrows the object as a Kotlin `ExecutionError`. |
#### Example: Serialization and Rethrowing
You can serialize Lyng exception objects using `Lynon` to transmit them across boundaries and then rethrow them.
```kotlin
try {
scope.eval("throw MyUserException(404, \"Not Found\")")
} catch (e: ExecutionError) {
// 1. Serialize the Lyng exception object
val encoded: UByteArray = lynonEncodeAny(scope, e.errorObject)
// ... (transmit 'encoded' byte array) ...
// 2. Deserialize it back to an Obj in a different context
val decoded: Obj = lynonDecodeAny(scope, encoded)
// 3. Properly rethrow it on the Kotlin side using the uniform API
decoded.raiseAsExecutionError(scope)
}
```
---
That’s it. You now have Lyng embedded in your Kotlin app: you can expose your app’s API, evaluate user scripts, and organize your own packages to import from Lyng code.

View File

@ -128,15 +128,15 @@ Serializable class that conveys information about the exception. Important membe
| name | description |
|-------------------|--------------------------------------------------------|
| message | String message |
| stackTrace | lyng stack trace, list of `StackTraceEntry`, see below |
| printStackTrace() | format and print stack trace using println() |
| message | String message |
| stackTrace() | lyng stack trace, list of `StackTraceEntry`, see below |
| printStackTrace() | format and print stack trace using println() |
## StackTraceEntry
A simple structire that stores single entry in Lyng stack, it is created automatically on exception creation:
```kotlin
```lyng
class StackTraceEntry(
val sourceName: String,
val line: Int,
@ -150,24 +150,103 @@ class StackTraceEntry(
# Custom error classes
_this functionality is not yet released_
You can define your own exception classes by inheriting from the built-in `Exception` class. This allows you to create specific error types for your application logic and catch them specifically.
## Defining a custom exception
To define a custom exception, create a class that inherits from `Exception`:
```lyng
class MyUserException : Exception("something went wrong")
```
You can also pass the message dynamically:
```lyng
class MyUserException(m) : Exception(m)
throw MyUserException("custom error message")
```
If you don't provide a message to the `Exception` constructor, the class name will be used as the default message:
```lyng
class SimpleException : Exception
val e = SimpleException()
assertEquals("SimpleException", e.message)
```
## Throwing and catching custom exceptions
Custom exceptions are thrown using the `throw` keyword and can be caught using `catch` blocks, just like standard exceptions:
```lyng
class ValidationException(m) : Exception(m)
try {
throw ValidationException("Invalid input")
}
catch(e: ValidationException) {
println("Caught validation error: " + e.message)
}
catch(e: Exception) {
println("Caught other exception: " + e.message)
}
```
Since user exceptions are real classes, inheritance works as expected:
```lyng
class BaseError : Exception
class DerivedError : BaseError
try {
throw DerivedError()
}
catch(e: BaseError) {
// This will catch DerivedError as well
assert(e is DerivedError)
}
```
## Accessing extra data
You can add your own fields to custom exception classes to carry additional information:
```lyng
class NetworkException(m, val statusCode) : Exception(m)
try {
throw NetworkException("Not Found", 404)
}
catch(e: NetworkException) {
println("Error " + e.statusCode + ": " + e.message)
}
```
# Standard exception classes
| class | notes |
|----------------------------|-------------------------------------------------------|
| Exception | root of al throwable objects |
| Exception | root of all throwable objects |
| NullReferenceException | |
| AssertionFailedException | |
| ClassCastException | |
| IndexOutOfBoundsException | |
| IllegalArgumentException | |
| IllegalStateException | |
| NoSuchElementException | |
| IllegalAssignmentException | assigning to val, etc. |
| SymbolNotDefinedException | |
| IterationEndException | attempt to read iterator past end, `hasNext == false` |
| AccessException | attempt to access private members or like |
| UnknownException | unexpected kotlin exception caught |
| | |
| IllegalAccessException | attempt to access private members or like |
| UnknownException | unexpected internal exception caught |
| NotFoundException | |
| IllegalOperationException | |
| UnsetException | access to uninitialized late-init val |
| NotImplementedException | used by `TODO()` |
| SyntaxError | |
### Symbol resolution errors

View File

@ -8,7 +8,7 @@ This module provides a uniform, suspend-first filesystem API to Lyng scripts, ba
It exposes a Lyng class `Path` with methods for file and directory operations, including streaming readers for large files.
It is a separate library because access to teh filesystem is a security risk we compensate with a separate API that user must explicitly include to the dependency and allow. Together with `FsAceessPolicy` that is required to `createFs()` which actually adds the filesystem to the scope, the security risk is isolated.
It is a separate library because access to the filesystem is a security risk we compensate with a separate API that user must explicitly include to the dependency and allow. Together with `FsAccessPolicy` that is required to `createFs()` which actually adds the filesystem to the scope, the security risk is isolated.
Also, it helps keep Lyng core small and focused.
@ -23,7 +23,7 @@ dependencies {
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
}
```
Note on maven repository. Lyngio uses ths same maven as Lyng code (`lynglib`) so it is most likely already in your project. If ont, add it to the proper section of your `build.gradle.kts` or settings.gradle.kts:
Note on maven repository. Lyngio uses the same maven as Lyng code (`lynglib`) so it is most likely already in your project. If not, add it to the proper section of your `build.gradle.kts` or settings.gradle.kts:
```kotlin
repositories {
@ -43,9 +43,13 @@ This brings in:
The filesystem module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using the installer. You can customize access control via `FsAccessPolicy`.
Kotlin (host) bootstrap example (imports omitted for brevity):
Kotlin (host) bootstrap example:
```kotlin
import net.sergeych.lyng.Scope
import net.sergeych.lyng.io.fs.createFs
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
val scope: Scope = Scope.new()
val installed: Boolean = createFs(PermitAllAccessPolicy, scope)
// installed == true on first registration in this ImportManager, false on repeats

136
docs/lyng.io.process.md Normal file
View File

@ -0,0 +1,136 @@
### lyng.io.process — Process execution and control for Lyng scripts
This module provides a way to run external processes and shell commands from Lyng scripts. It is designed to be multiplatform and uses coroutines for non-blocking execution.
> **Note:** `lyngio` is a separate library module. It must be explicitly added as a dependency to your host application and initialized in your Lyng scopes.
---
#### Add the library to your project (Gradle)
If you use this repository as a multi-module project, add a dependency on `:lyngio`:
```kotlin
dependencies {
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
}
```
For external projects, ensure you have the appropriate Maven repository configured (see `lyng.io.fs` documentation).
---
#### Install the module into a Lyng Scope
The process module is not installed automatically. You must explicitly register it in the scope’s `ImportManager` using `createProcessModule`. You can customize access control via `ProcessAccessPolicy`.
Kotlin (host) bootstrap example:
```kotlin
import net.sergeych.lyng.Scope
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
// ... inside a suspend function or runBlocking
val scope: Scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
// In scripts (or via scope.eval), import the module:
scope.eval("import lyng.io.process")
```
---
#### Using from Lyng scripts
```lyng
import lyng.io.process
// Execute a process with arguments
val p = Process.execute("ls", ["-l", "/tmp"])
for (line in p.stdout) {
println("OUT: " + line)
}
val exitCode = p.waitFor()
println("Process exited with: " + exitCode)
// Run a shell command
val sh = Process.shell("echo 'Hello from shell' | wc -w")
for (line in sh.stdout) {
println("Word count: " + line.trim())
}
// Platform information
val details = Platform.details()
println("OS: " + details.name + " " + details.version + " (" + details.arch + ")")
if (details.kernelVersion != null) {
println("Kernel: " + details.kernelVersion)
}
if (Platform.isSupported()) {
println("Processes are supported!")
}
```
---
#### API Reference
##### `Process` (static methods)
- `execute(executable: String, args: List<String>): RunningProcess` — Start an external process.
- `shell(command: String): RunningProcess` — Run a command through the system shell (e.g., `/bin/sh` or `cmd.exe`).
##### `RunningProcess` (instance methods)
- `stdout: Flow` — Standard output stream as a Lyng Flow of lines.
- `stderr: Flow` — Standard error stream as a Lyng Flow of lines.
- `waitFor(): Int` — Wait for the process to exit and return the exit code.
- `signal(name: String)` — Send a signal to the process (e.g., `"SIGINT"`, `"SIGTERM"`, `"SIGKILL"`).
- `destroy()` — Forcefully terminate the process.
##### `Platform` (static methods)
- `details(): Map` — Get platform details. Returned map keys: `name`, `version`, `arch`, `kernelVersion`.
- `isSupported(): Bool` — True if process execution is supported on the current platform.
---
#### Security Policy
Process execution is a sensitive operation. `lyngio` uses `ProcessAccessPolicy` to control access to `execute` and `shell` operations.
- `ProcessAccessPolicy` — Interface for custom policies.
- `PermitAllProcessAccessPolicy` — Allows all operations.
- `ProcessAccessOp` (sealed) — Operations to check:
- `Execute(executable, args)`
- `Shell(command)`
Example of a restricted policy in Kotlin:
```kotlin
import net.sergeych.lyngio.fs.security.AccessDecision
import net.sergeych.lyngio.fs.security.Decision
import net.sergeych.lyngio.process.security.ProcessAccessOp
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
val restrictedPolicy = object : ProcessAccessPolicy {
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision {
return when (op) {
is ProcessAccessOp.Execute -> {
if (op.executable == "ls") AccessDecision(Decision.Allow)
else AccessDecision(Decision.Deny, "Only 'ls' is allowed")
}
is ProcessAccessOp.Shell -> AccessDecision(Decision.Deny, "Shell is forbidden")
}
}
}
createProcessModule(restrictedPolicy, scope)
```
---
#### Platform Support
- **JVM:** Full support using `ProcessBuilder`.
- **Native (Linux/macOS):** Support via POSIX.
- **Windows:** Support planned.
- **Android/JS/iOS/Wasm:** Currently not supported; `isSupported()` returns `false` and attempts to run processes will throw `UnsupportedOperationException`.

87
docs/lyngio.md Normal file
View File

@ -0,0 +1,87 @@
### lyngio — Extended I/O and System Library for Lyng
`lyngio` is a separate library that extends the Lyng core (`lynglib`) with powerful, multiplatform, and secure I/O capabilities.
#### Why a separate module?
1. **Security:** I/O and process execution are sensitive operations. By keeping them in a separate module, we ensure that the Lyng core remains 100% safe by default. You only enable what you explicitly need.
2. **Footprint:** Not every script needs filesystem or process access. Keeping these as a separate module helps minimize the dependency footprint for small embedded projects.
3. **Control:** `lyngio` provides fine-grained security policies (`FsAccessPolicy`, `ProcessAccessPolicy`) that allow you to control exactly what a script can do.
#### Included Modules
- **[lyng.io.fs](lyng.io.fs.md):** Async filesystem access. Provides the `Path` class for file/directory operations, streaming, and globbing.
- **[lyng.io.process](lyng.io.process.md):** External process execution and shell commands. Provides `Process`, `RunningProcess`, and `Platform` information.
---
#### Quick Start: Embedding lyngio
##### 1. Add Dependencies (Gradle)
```kotlin
repositories {
maven("https://gitea.sergeych.net/api/packages/SergeychWorks/maven")
}
dependencies {
// Both are required for full I/O support
implementation("net.sergeych:lynglib:0.0.1-SNAPSHOT")
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT")
}
```
##### 2. Initialize in Kotlin (JVM or Native)
To use `lyngio` modules in your scripts, you must install them into your Lyng scope and provide a security policy.
```kotlin
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.fs.createFs
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.fs.security.PermitAllAccessPolicy
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
suspend fun runMyScript() {
val scope = Script.newScope()
// Install modules with policies
createFs(PermitAllAccessPolicy, scope)
createProcessModule(PermitAllProcessAccessPolicy, scope)
// Now scripts can import them
scope.eval("""
import lyng.io.fs
import lyng.io.process
println("Working dir: " + Path(".").readUtf8())
println("OS: " + Platform.details().name)
""")
}
```
---
#### Security Tools
`lyngio` is built with a "Secure by Default" philosophy. Every I/O or process operation is checked against a policy.
- **Filesystem Security:** Implement `FsAccessPolicy` to restrict access to specific paths or operations (e.g., read-only access to a sandbox directory).
- **Process Security:** Implement `ProcessAccessPolicy` to restrict which executables can be run or to disable shell execution entirely.
For more details, see the specific module documentation:
- [Filesystem Security Details](lyng.io.fs.md#access-policy-security)
- [Process Security Details](lyng.io.process.md#security-policy)
---
#### Platform Support Overview
| Platform | lyng.io.fs | lyng.io.process |
| :--- | :---: | :---: |
| **JVM** | ✅ | ✅ |
| **Native (Linux/macOS)** | ✅ | ✅ |
| **Native (Windows)** | ✅ | 🚧 (Planned) |
| **Android** | ✅ | ❌ |
| **NodeJS** | ✅ | ❌ |
| **Browser / Wasm** | ✅ (In-memory) | ❌ |

86
docs/return_statement.md Normal file
View File

@ -0,0 +1,86 @@
# The `return` statement
The `return` statement is used to terminate the execution of the innermost enclosing callable (a function or a lambda) and optionally return a value to the caller.
## Basic Usage
By default, Lyng functions and blocks return the value of their last expression. However, `return` allows you to exit early, which is particularly useful for guard clauses.
```lyng
fun divide(a, b) {
if (b == 0) return null // Guard clause: early exit
a / b
}
```
If no expression is provided, `return` returns `void`:
```lyng
fun logIfDebug(msg) {
if (!DEBUG) return
println("[DEBUG] " + msg)
}
```
## Scoping Rules
In Lyng, `return` always exits the **innermost enclosing callable**. Callables include:
* Named functions (`fun` or `fn`)
* Anonymous functions/lambdas (`{ ... }`)
Standard control flow blocks like `if`, `while`, `do`, and `for` are **not** callables; `return` inside these blocks will return from the function or lambda that contains them.
```lyng
fun findFirstPositive(list) {
list.forEach {
if (it > 0) return it // ERROR: This returns from the lambda, not findFirstPositive!
}
null
}
```
*Note: To return from an outer scope, use [Non-local Returns](#non-local-returns).*
## Non-local Returns
Lyng supports returning from outer scopes using labels. This is a powerful feature for a closure-intensive language.
### Named Functions as Labels
Every named function automatically provides its name as a label.
```lyng
fun findFirstPositive(list) {
list.forEach {
if (it > 0) return@findFirstPositive it // Returns from findFirstPositive
}
null
}
```
### Labeled Lambdas
You can explicitly label a lambda using the `@label` syntax to return from it specifically when nested.
```lyng
val process = @outer { x ->
val result = {
if (x < 0) return@outer "negative" // Returns from the outer lambda
x * 2
}()
"Result: " + result
}
```
## Restriction on Shorthand Functions
To maintain Lyng's clean, expression-oriented style, the `return` keyword is **forbidden** in shorthand function definitions (those using `=`).
```lyng
fun square(x) = x * x // Correct
fun square(x) = return x * x // Syntax Error: 'return' not allowed here
```
## Summary
* `return [expression]` exits the innermost `fun` or `{}`.
* Use `return@label` for non-local returns.
* Named functions provide automatic labels.
* Cannot be used in `=` shorthand functions.
* Consistency: Mirrors the syntax and behavior of `break@label expression`.

View File

@ -0,0 +1,63 @@
// Sample: Operator Overloading in Lyng
class Vector(val x, val y) {
// Overload +
fun plus(other) = Vector(x + other.x, y + other.y)
// Overload -
fun minus(other) = Vector(x - other.x, y - other.y)
// Overload unary -
fun negate() = Vector(-x, -y)
// Overload ==
fun equals(other) {
if (other is Vector) x == other.x && y == other.y
else false
}
// Overload * (scalar multiplication)
fun mul(scalar) = Vector(x * scalar, y * scalar)
override fun toString() = "Vector(${x}, ${y})"
}
val v1 = Vector(10, 20)
val v2 = Vector(5, 5)
println("v1: " + v1)
println("v2: " + v2)
// Test binary +
val v3 = v1 + v2
println("v1 + v2 = " + v3)
assertEquals(Vector(15, 25), v3)
// Test unary -
val v4 = -v1
println("-v1 = " + v4)
assertEquals(Vector(-10, -20), v4)
// Test scalar multiplication
val v5 = v1 * 2
println("v1 * 2 = " + v5)
assertEquals(Vector(20, 40), v5)
// Test += (falls back to plus)
var v6 = Vector(1, 1)
v6 += Vector(2, 2)
println("v6 += (2,2) -> " + v6)
assertEquals(Vector(3, 3), v6)
// Test in-place mutation with plusAssign
class Counter(var count) {
fun plusAssign(n) {
count = count + n
}
}
val c = Counter(0)
c += 10
c += 5
println("Counter: " + c.count)
assertEquals(15, c.count)

View File

@ -72,7 +72,7 @@ Tip: If a closure unexpectedly cannot see an outer local, check whether an inter
The `cached` function (defined in `lyng.stdlib`) is a classic example of using closures to maintain state. It wraps a builder into a zero-argument function that computes once and remembers the result:
```kotlin
```lyng
fun cached(builder) {
var calculated = false
var value = null

View File

@ -8,7 +8,7 @@ __Other documents to read__ maybe after this one:
- [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.md), [Scopes and Closures](scopes_and_closures.md)
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
- [math in Lyng](math.md), [the `when` statement](when.md)
- [math in Lyng](math.md), [the `when` statement](when.md), [return statement](return_statement.md)
- [Testing and Assertions](Testing.md)
- [time](time.md) and [parallelism](parallelism.md)
- [parallelism] - multithreaded code, coroutines, etc.
@ -32,6 +32,15 @@ any block also returns it's last expression:
}
>>> 6
If you want to exit a function or lambda earlier, use the `return` statement:
fn divide(a, b) {
if( b == 0 ) return null
a / b
}
See [return statement](return_statement.md) for more details on scoping and non-local returns.
If you don't want block to return anything, use `void`:
fn voidFunction() {
@ -87,6 +96,27 @@ Lyng supports simple enums for a fixed set of named constants. Declare with `enu
For more details (usage patterns, `when` switching, serialization), see OOP notes: [Enums in detail](OOP.md#enums).
## Singleton Objects
Singleton objects are declared using the `object` keyword. They define a class and create its single instance immediately.
object Logger {
fun log(msg) { println("[LOG] " + msg) }
}
Logger.log("Hello singleton!")
## Delegation (briefly)
You can delegate properties and functions to other objects using the `by` keyword. This is perfect for patterns like `lazy` initialization.
val expensiveData by lazy {
// computed only once on demand
"computed"
}
For more details on these features, see [Delegation in Lyng](delegation.md) and [OOP notes](OOP.md).
When putting multiple statments in the same line it is convenient and recommended to use `;`:
var from; var to
@ -158,14 +188,13 @@ Note that assignment operator returns rvalue, it can't be assigned.
## Modifying arithmetics
There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`.
There is also a special null-aware assignment operator `?=`: it performs the assignment only if the lvalue is `null`.
var x = 5
assert( 25 == (x*=5) )
assert( 25 == x)
assert( 24 == (x-=1) )
assert( 12 == (x/=2) )
x
>>> 12
var x = null
x ?= 10
assertEquals(10, x)
x ?= 20
assertEquals(10, x)
Notice the parentheses here: the assignment has low priority!
@ -218,6 +247,13 @@ There is also "elvis operator", null-coalesce infix operator '?:' that returns r
null ?: "nothing"
>>> "nothing"
There is also a null-aware assignment operator `?=`, which assigns a value only if the target is `null`:
var config = null
config ?= { port: 8080 }
config ?= { port: 9000 } // no-op, config is already not null
assertEquals(8080, config.port)
## Utility functions
The following functions simplify nullable values processing and
@ -277,6 +313,16 @@ It works much like `also`, but is executed in the context of the source object:
assertEquals(p, Point(2,3))
>>> void
## with
Sets `this` to the first argument and executes the block. Returns the value returned by the block:
class Point(x,y)
val p = Point(1,2)
val sum = with(p) { x + y }
assertEquals(3, sum)
>>> void
## run
Executes a block after it returning the value passed by the block. for example, can be used with elvis operator:
@ -1498,7 +1544,7 @@ See [math functions](math.md). Other general purpose functions are:
| flow {} | create flow sequence, see [parallelism] |
| delay, launch, yield | see [parallelism] |
| cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) |
| let, also, apply, run | see above, flow controls |
| let, also, apply, run, with | see above, flow controls |
(1)
: `fn` is optional lambda returning string message to add to exception string.
@ -1525,7 +1571,7 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
[Range]: Range.md
[String]: development/String.md
[String]: ../archived/development/String.md
[string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
@ -1553,13 +1599,13 @@ It is extremely simple to use: you pass it a block (lambda) that performs the co
### Basic Example
```kotlin
```lyng
val expensive = cached {
println("Performing expensive calculation...")
2 + 2
}
println(expensive()) // Prints "Performing expensive calculation..." then "4"
println(expensive()) // Prints "Performing expensive calculation...") then "4"
println(expensive()) // Prints only "4" (result is cached)
```
@ -1573,7 +1619,7 @@ println(expensive()) // Prints only "4" (result is cached)
This is the most common use case for `cached`. It allows you to define expensive "fields" that are only computed if someone actually uses them:
```kotlin
```lyng
class User(val id: Int) {
// The details will be fetched only once, on demand
val details = cached {

View File

@ -93,6 +93,81 @@ let d = Derived()
println((d as B).foo()) // Disambiguation via cast
```
### Singleton Objects
Singleton objects are declared using the `object` keyword. They provide a convenient way to define a class and its single instance in one go.
```lyng
object Config {
val version = "1.2.3"
fun show() = println("Config version: " + version)
}
Config.show()
```
### Object Expressions
You can now create anonymous objects that inherit from classes or interfaces using the `object : Base { ... }` syntax. These expressions capture their lexical scope and support multiple inheritance.
```lyng
val worker = object : Runnable {
override fun run() = println("Working...")
}
val x = object : Base(arg1), Interface1 {
val property = 42
override fun method() = this@object.property * 2
}
```
Use `this@object` to refer to the innermost anonymous object instance when `this` is rebound.
### Unified Delegation Model
A powerful new delegation system allows `val`, `var`, and `fun` members to delegate their logic to other objects using the `by` keyword.
```lyng
// Property delegation
val lazyValue by lazy { "expensive" }
// Function delegation
fun remoteAction by myProxy
// Observable properties
var name by Observable("initial") { n, old, new ->
println("Changed!")
}
```
The system features a unified interface (`getValue`, `setValue`, `invoke`) and a `bind` hook for initialization-time validation and configuration. See the [Delegation Guide](delegation.md) for more.
### User-Defined Exception Classes
You can now create custom exception types by inheriting from the built-in `Exception` class. Custom exceptions are real classes that can have their own fields and methods, and they work seamlessly with `throw` and `try-catch` blocks.
```lyng
class ValidationException(val field, m) : Exception(m)
try {
throw ValidationException("email", "Invalid format")
}
catch(e: ValidationException) {
println("Error in " + e.field + ": " + e.message)
}
```
### Assign-if-null Operator (`?=`)
The new `?=` operator provides a concise way to assign a value only if the target is `null`. It is especially useful for setting default values or lazy initialization.
```lyng
var x = null
x ?= 42 // x is now 42
x ?= 100 // x remains 42 (not null)
// Works with properties and index access
config.port ?= 8080
settings["theme"] ?= "dark"
```
The operator returns the final value of the receiver (the original value if it was not `null`, or the new value if the assignment occurred).
## Tooling and Infrastructure
### CLI: Formatting Command

View File

@ -20,7 +20,7 @@ Files
- Constants: `true`, `false`, `null`, `this`
- Annotations: `@name` (Unicode identifiers supported)
- Labels: `name:` (Unicode identifiers supported)
- Declarations: highlights declared names in `fun|fn name`, `class|enum Name`, `val|var name`
- Declarations: highlights declared names in `fun|fn name`, `class|enum|interface Name`, `val|var name`
- Types: built-ins (`Int|Real|String|Bool|Char|Regex`) and Capitalized identifiers (heuristic)
- Operators including ranges (`..`, `..<`, `...`), null-safe (`?.`, `?[`, `?(`, `?{`, `?:`, `??`), arrows (`->`, `=>`, `::`), match operators (`=~`, `!~`), bitwise, arithmetic, etc.
- Shuttle operator `<=>`

View File

@ -2,7 +2,7 @@
"name": "lyng-textmate",
"displayName": "Lyng",
"description": "TextMate grammar for the Lyng language (for JetBrains IDEs via TextMate Bundles and VS Code).",
"version": "0.0.3",
"version": "0.1.0",
"publisher": "lyng",
"license": "Apache-2.0",
"engines": { "vscode": "^1.0.0" },

View File

@ -42,7 +42,7 @@
{ "name": "constant.numeric.decimal.lyng", "match": "(?<![A-Za-z_])(?:[0-9][0-9_]*)\\.(?:[0-9_]+)(?:[eE][+-]?[0-9_]+)?|(?<![A-Za-z_])(?:[0-9][0-9_]*)(?:[eE][+-]?[0-9_]+)?" }
]
},
"annotations": { "patterns": [ { "name": "entity.name.label.at.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*:" }, { "name": "storage.modifier.annotation.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*" } ] },
"annotations": { "patterns": [ { "name": "entity.name.label.at.lyng", "match": "@[\\p{L}_][\\p{L}\\p{N}_]*" } ] },
"mapLiterals": {
"patterns": [
{
@ -74,11 +74,11 @@
}
]
},
"labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*:" } ] },
"labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*@" } ] },
"directives": { "patterns": [ { "name": "meta.directive.lyng", "match": "^\\s*#[_A-Za-z][_A-Za-z0-9]*" } ] },
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] },
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|val|var|import|package|constructor|property|open|extern|private|protected|static|get|set)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] },
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this)\\b|π)" } ] },
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum|interface|object)(?:\\s+([\\p{L}_][\\p{L}\\p{N}_]*))?", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] },
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|interface|val|var|import|package|constructor|property|abstract|override|open|closed|extern|private|protected|static|get|set|object|init|by)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] },
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this(?:@[\\p{L}_][\\p{L}\\p{N}_]*)?)\\b|π)" } ] },
"types": { "patterns": [ { "name": "storage.type.lyng", "match": "\\b(?:Int|Real|String|Bool|Char|Regex)\\b" }, { "name": "entity.name.type.lyng", "match": "\\b[A-Z][A-Za-z0-9_]*\\b(?!\\s*\\()" } ] },
"operators": { "patterns": [ { "name": "keyword.operator.comparison.lyng", "match": "===|!==|==|!=|<=|>=|<|>" }, { "name": "keyword.operator.shuttle.lyng", "match": "<=>" }, { "name": "keyword.operator.arrow.lyng", "match": "=>|->|::" }, { "name": "keyword.operator.range.lyng", "match": "\\.\\.\\.|\\.\\.<|\\.\\." }, { "name": "keyword.operator.nullsafe.lyng", "match": "\\?\\.|\\?\\[|\\?\\(|\\?\\{|\\?:|\\?\\?" }, { "name": "keyword.operator.assignment.lyng", "match": "(?:\\+=|-=|\\*=|/=|%=|=)" }, { "name": "keyword.operator.logical.lyng", "match": "&&|\\|\\|" }, { "name": "keyword.operator.bitwise.lyng", "match": "<<|>>|&|\\||\\^|~" }, { "name": "keyword.operator.match.lyng", "match": "=~|!~" }, { "name": "keyword.operator.arithmetic.lyng", "match": "\\+\\+|--|[+\\-*/%]" }, { "name": "keyword.operator.other.lyng", "match": "[!?]" } ] },
"punctuation": { "patterns": [ { "name": "punctuation.separator.comma.lyng", "match": "," }, { "name": "punctuation.terminator.statement.lyng", "match": ";" }, { "name": "punctuation.section.block.begin.lyng", "match": "[(]{1}|[{]{1}|\\[" }, { "name": "punctuation.section.block.end.lyng", "match": "[)]{1}|[}]{1}|\\]" }, { "name": "punctuation.accessor.dot.lyng", "match": "\\." }, { "name": "punctuation.separator.colon.lyng", "match": ":" } ] }

View File

@ -45,6 +45,8 @@ dependencies {
// Tests for IntelliJ Platform fixtures rely on JUnit 3/4 API (junit.framework.TestCase)
// Add JUnit 4 which contains the JUnit 3 compatibility classes used by BasePlatformTestCase/UsefulTestCase
testImplementation("junit:junit:4.13.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.10.2")
testImplementation("org.opentest4j:opentest4j:1.3.0")
}
intellij {

View File

@ -0,0 +1,33 @@
/*
* 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
import com.intellij.openapi.fileTypes.FileTypeConsumer
import com.intellij.openapi.fileTypes.FileTypeFactory
import com.intellij.openapi.fileTypes.WildcardFileNameMatcher
/**
* Legacy way to register file type matchers, used here to robustly match *.lyng.d
* without conflicting with standard .d extensions from other plugins.
*/
@Suppress("DEPRECATION")
class LyngFileTypeFactory : FileTypeFactory() {
override fun createFileTypes(consumer: FileTypeConsumer) {
// Register the multi-dot pattern explicitly
consumer.consume(LyngFileType, WildcardFileNameMatcher("*.lyng.d"))
}
}

View File

@ -25,9 +25,6 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.Source
import net.sergeych.lyng.binding.Binder
import net.sergeych.lyng.binding.SymbolKind
@ -35,7 +32,7 @@ import net.sergeych.lyng.highlight.HighlightKind
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.highlight.LyngHighlighterColors
import net.sergeych.lyng.idea.util.IdeLenientImportProvider
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.miniast.*
/**
@ -43,7 +40,7 @@ import net.sergeych.lyng.miniast.*
* and applies semantic highlighting comparable with the web highlighter.
*/
class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, LyngExternalAnnotator.Result>() {
data class Input(val text: String, val modStamp: Long, val previousSpans: List<Span>?)
data class Input(val text: String, val modStamp: Long, val previousSpans: List<Span>?, val file: PsiFile)
data class Span(val start: Int, val end: Int, val key: com.intellij.openapi.editor.colors.TextAttributesKey)
data class Error(val start: Int, val end: Int, val message: String)
@ -55,45 +52,36 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
override fun collectInformation(file: PsiFile): Input? {
val doc: Document = file.viewProvider.document ?: return null
val cached = file.getUserData(CACHE_KEY)
// Fast fix (1): reuse cached spans only if they were computed for the same modification stamp
val prev = if (cached != null && cached.modStamp == doc.modificationStamp) cached.spans else null
return Input(doc.text, doc.modificationStamp, prev)
val combinedStamp = LyngAstManager.getCombinedStamp(file)
val prev = if (cached != null && cached.modStamp == combinedStamp) cached.spans else null
return Input(doc.text, combinedStamp, prev, file)
}
override fun doAnnotate(collectedInfo: Input?): Result? {
if (collectedInfo == null) return null
ProgressManager.checkCanceled()
val text = collectedInfo.text
// Build Mini-AST using the same mechanism as web highlighter
val sink = MiniAstBuilder()
val source = Source("<ide>", text)
try {
// Call suspend API from blocking context
val provider = IdeLenientImportProvider.create()
runBlocking { Compiler.compileWithMini(source, provider, sink) }
} catch (e: Throwable) {
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
// On script parse error: keep previous spans and report the error location
if (e is ScriptError) {
val off = try { source.offsetOf(e.pos) } catch (_: Throwable) { -1 }
val start0 = off.coerceIn(0, text.length.coerceAtLeast(0))
val (start, end) = expandErrorRange(text, start0)
// Fast fix (5): clear cached highlighting after the error start position
val trimmed = collectedInfo.previousSpans?.filter { it.end <= start } ?: emptyList()
return Result(
collectedInfo.modStamp,
trimmed,
Error(start, end, e.errorMessage)
)
}
// Other failures: keep previous spans without error
return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList(), null)
}
// Use LyngAstManager to get the (potentially merged) Mini-AST
val mini = LyngAstManager.getMiniAst(collectedInfo.file)
?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList())
ProgressManager.checkCanceled()
val mini = sink.build() ?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList())
val source = Source(collectedInfo.file.name, text)
val out = ArrayList<Span>(256)
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
var i = rangeEnd
while (i < text.length) {
val ch = text[i]
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
return ch == '(' || ch == '{'
}
return false
}
fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key)
}
@ -108,7 +96,8 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
}
// Declarations
for (d in mini.declarations) {
mini.declarations.forEach { d ->
if (d.nameStart.source != source) return@forEach
when (d) {
is MiniFunDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.FUNCTION_DECLARATION)
is MiniClassDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE)
@ -122,19 +111,22 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
}
// Imports: each segment as namespace/path
for (imp in mini.imports) {
for (seg in imp.segments) putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE)
mini.imports.forEach { imp ->
if (imp.range.start.source != source) return@forEach
imp.segments.forEach { seg -> putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE) }
}
// Parameters
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
for (p in fn.params) putName(p.nameStart, p.name, LyngHighlighterColors.PARAMETER)
mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn ->
if (fn.nameStart.source != source) return@forEach
fn.params.forEach { p -> putName(p.nameStart, p.name, LyngHighlighterColors.PARAMETER) }
}
// Type name segments (including generics base & args)
fun addTypeSegments(t: MiniTypeRef?) {
when (t) {
is MiniTypeName -> t.segments.forEach { seg ->
if (seg.range.start.source != source) return@forEach
val s = source.offsetOf(seg.range.start)
putRange(s, (s + seg.name.length).coerceAtMost(text.length), LyngHighlighterColors.TYPE)
}
@ -148,12 +140,14 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
addTypeSegments(t.returnType)
}
is MiniTypeVar -> { /* name is in range; could be highlighted as TYPE as well */
putMiniRange(t.range, LyngHighlighterColors.TYPE)
if (t.range.start.source == source)
putMiniRange(t.range, LyngHighlighterColors.TYPE)
}
null -> {}
}
}
for (d in mini.declarations) {
mini.declarations.forEach { d ->
if (d.nameStart.source != source) return@forEach
when (d) {
is MiniFunDecl -> {
addTypeSegments(d.returnType)
@ -180,7 +174,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Map declaration ranges to avoid duplicating them as usages
val declKeys = HashSet<Pair<Int, Int>>(binding.symbols.size * 2)
for (sym in binding.symbols) declKeys += (sym.declStart to sym.declEnd)
binding.symbols.forEach { sym -> declKeys += (sym.declStart to sym.declEnd) }
fun keyForKind(k: SymbolKind) = when (k) {
SymbolKind.Function -> LyngHighlighterColors.FUNCTION
@ -193,13 +187,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Track covered ranges to not override later heuristics
val covered = HashSet<Pair<Int, Int>>()
for (ref in binding.references) {
binding.references.forEach { ref ->
val key = ref.start to ref.end
if (declKeys.contains(key)) continue
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } ?: continue
val color = keyForKind(sym.kind)
putRange(ref.start, ref.end, color)
covered += key
if (!declKeys.contains(key)) {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) {
val color = keyForKind(sym.kind)
putRange(ref.start, ref.end, color)
covered += key
}
}
}
// Heuristics on top of binder: function call-sites and simple name-based roles
@ -207,44 +204,43 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
var i = rangeEnd
while (i < text.length) {
val ch = text[i]
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
return ch == '(' || ch == '{'
}
return false
}
// Build simple name -> role map for top-level vals/vars and parameters
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
for (d in mini.declarations) when (d) {
is MiniValDecl -> nameRole[d.name] = if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
is MiniFunDecl -> d.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER }
else -> {}
mini.declarations.forEach { d ->
when (d) {
is MiniValDecl -> nameRole[d.name] =
if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
is MiniFunDecl -> d.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER }
else -> {}
}
}
for (s in tokens) if (s.kind == HighlightKind.Identifier) {
val start = s.range.start
val end = s.range.endExclusive
val key = start to end
if (key in covered || key in declKeys) continue
// Call-site detection first so it wins over var/param role
if (isFollowedByParenOrBlock(end)) {
putRange(start, end, LyngHighlighterColors.FUNCTION)
covered += key
continue
}
// Simple role by known names
val ident = try { text.substring(start, end) } catch (_: Throwable) { null }
if (ident != null) {
val roleKey = nameRole[ident]
if (roleKey != null) {
putRange(start, end, roleKey)
covered += key
tokens.forEach { s ->
if (s.kind == HighlightKind.Identifier) {
val start = s.range.start
val end = s.range.endExclusive
val key = start to end
if (key !in covered && key !in declKeys) {
// Call-site detection first so it wins over var/param role
if (isFollowedByParenOrBlock(end)) {
putRange(start, end, LyngHighlighterColors.FUNCTION)
covered += key
} else {
// Simple role by known names
val ident = try {
text.substring(start, end)
} catch (_: Throwable) {
null
}
if (ident != null) {
val roleKey = nameRole[ident]
if (roleKey != null) {
putRange(start, end, roleKey)
covered += key
}
}
}
}
}
}
@ -253,16 +249,43 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
}
// Add annotation coloring using token highlighter (treat @Label as annotation)
// Add annotation/label coloring using token highlighter
run {
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
for (s in tokens) if (s.kind == HighlightKind.Label) {
val start = s.range.start
val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) {
val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null }
if (lexeme != null && lexeme.startsWith("@")) {
putRange(start, end, LyngHighlighterColors.ANNOTATION)
tokens.forEach { s ->
if (s.kind == HighlightKind.Label) {
val start = s.range.start
val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) {
val lexeme = try {
text.substring(start, end)
} catch (_: Throwable) {
null
}
if (lexeme != null) {
// Heuristic: if it starts with @ and follows a control keyword, it's likely a label
// Otherwise if it starts with @ it's an annotation.
// If it ends with @ it's a loop label.
when {
lexeme.endsWith("@") -> putRange(start, end, LyngHighlighterColors.LABEL)
lexeme.startsWith("@") -> {
// Try to see if it's an exit label
val prevNonWs = prevNonWs(text, start)
val prevWord = if (prevNonWs >= 0) {
var wEnd = prevNonWs + 1
var wStart = prevNonWs
while (wStart > 0 && text[wStart - 1].isLetter()) wStart--
text.substring(wStart, wEnd)
} else null
if (prevWord in setOf("return", "break", "continue") || isFollowedByParenOrBlock(end)) {
putRange(start, end, LyngHighlighterColors.LABEL)
} else {
putRange(start, end, LyngHighlighterColors.ANNOTATION)
}
}
}
}
}
}
}
@ -271,11 +294,13 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Map Enum constants from token highlighter to IDEA enum constant color
run {
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
for (s in tokens) if (s.kind == HighlightKind.EnumConstant) {
val start = s.range.start
val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) {
putRange(start, end, LyngHighlighterColors.ENUM_CONSTANT)
tokens.forEach { s ->
if (s.kind == HighlightKind.EnumConstant) {
val start = s.range.start
val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) {
putRange(start, end, LyngHighlighterColors.ENUM_CONSTANT)
}
}
}
}
@ -284,12 +309,14 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
val idRanges = mutableSetOf<IntRange>()
try {
val binding = Binder.bind(text, mini)
for (sym in binding.symbols) {
val s = sym.declStart; val e = sym.declEnd
binding.symbols.forEach { sym ->
val s = sym.declStart
val e = sym.declEnd
if (s in 0..e && e <= text.length && s < e) idRanges += (s until e)
}
for (ref in binding.references) {
val s = ref.start; val e = ref.end
binding.references.forEach { ref ->
val s = ref.start
val e = ref.end
if (s in 0..e && e <= text.length && s < e) idRanges += (s until e)
}
} catch (_: Throwable) {
@ -308,12 +335,13 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
override fun apply(file: PsiFile, annotationResult: Result?, holder: AnnotationHolder) {
if (annotationResult == null) return
// Skip if cache is up-to-date
val doc = file.viewProvider.document
val currentStamp = doc?.modificationStamp
val combinedStamp = LyngAstManager.getCombinedStamp(file)
val cached = file.getUserData(CACHE_KEY)
val result = if (cached != null && currentStamp != null && cached.modStamp == currentStamp) cached else annotationResult
val result = if (cached != null && cached.modStamp == combinedStamp) cached else annotationResult
file.putUserData(CACHE_KEY, result)
val doc = file.viewProvider.document
// Store spell index for spell/grammar engines to consume (suspend until ready)
val ids = result.spellIdentifiers.map { TextRange(it.first, it.last + 1) }
val coms = result.spellComments.map { TextRange(it.first, it.last + 1) }
@ -364,6 +392,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
private val CACHE_KEY: Key<Result> = Key.create("LYNG_SEMANTIC_CACHE")
}
private fun prevNonWs(text: String, idxExclusive: Int): Int {
var i = idxExclusive - 1
while (i >= 0) {
val ch = text[i]
if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r') return i
i--
}
return -1
}
/**
* Make the error highlight a bit wider than a single character so it is easier to see and click.
* Strategy:

View File

@ -30,7 +30,6 @@ import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.PsiFile
import com.intellij.util.ProcessingContext
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.highlight.LyngTokenTypes
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
@ -103,7 +102,7 @@ class LyngCompletionContributor : CompletionContributor() {
// Delegate computation to the shared engine to keep behavior in sync with tests
val engineItems = try {
runBlocking { CompletionEngineLight.completeSuspend(text, caret) }
runBlocking { CompletionEngineLight.completeSuspend(text, caret, mini, binding) }
} catch (t: Throwable) {
if (DEBUG_COMPLETION) log.warn("[LYNG_DEBUG] Engine completion failed: ${t.message}")
emptyList()
@ -144,11 +143,6 @@ class LyngCompletionContributor : CompletionContributor() {
}
}
// In global context, add params in scope first (engine does not include them)
if (memberDotPos == null && mini != null) {
offerParamsInScope(emit, mini, text, caret)
}
// Render engine items
for (ci in engineItems) {
val builder = when (ci.kind) {
@ -167,7 +161,7 @@ class LyngCompletionContributor : CompletionContributor() {
Kind.Enum -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Enum)
Kind.Value -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Field)
.withIcon(AllIcons.Nodes.Variable)
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
Kind.Field -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Field)
@ -191,33 +185,51 @@ class LyngCompletionContributor : CompletionContributor() {
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
if (!inferredClass.isNullOrBlank()) {
val ext = BuiltinDocRegistry.extensionMemberNamesFor(inferredClass)
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${'$'}{ext}")
val ext = DocLookupUtils.collectExtensionMemberNames(imported, inferredClass, mini)
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${ext}")
for (name in ext) {
if (existing.contains(name)) continue
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini)
if (resolved != null) {
when (val member = resolved.second) {
val m = resolved.second
val builder = when (m) {
is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType)
val builder = LookupElementBuilder.create(name)
val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("(${ '$' }params)", true)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
}
is MiniFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
emit(builder)
existing.add(name)
}
is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
emit(builder)
existing.add(name)
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
is MiniValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
else -> {
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("()", true)
.withInsertHandler(ParenInsertHandler)
}
is MiniInitDecl -> {}
}
emit(builder)
existing.add(name)
} else {
// Fallback: emit simple method name without detailed types
val builder = LookupElementBuilder.create(name)
@ -458,30 +470,47 @@ class LyngCompletionContributor : CompletionContributor() {
for (name in common) {
if (name in already) continue
// Try resolve across classes first to get types/params; if it fails, emit a synthetic safe suggestion.
val resolved = DocLookupUtils.findMemberAcrossClasses(imported, name)
val resolved = DocLookupUtils.findMemberAcrossClasses(imported, name, mini)
if (resolved != null) {
val member = resolved.second
when (member) {
val builder = when (member) {
is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType)
val builder = LookupElementBuilder.create(name)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("(${params})", true)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
}
is MiniFunDecl -> {
val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
emit(builder)
already.add(name)
}
is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Field)
LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
emit(builder)
already.add(name)
}
is MiniInitDecl -> {}
is MiniValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
}
else -> {
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("()", true)
.withInsertHandler(ParenInsertHandler)
}
}
emit(builder)
already.add(name)
} else {
// Synthetic fallback: method without detailed params/types to improve UX in absence of docs
val isProperty = name in setOf("size", "length")
@ -508,31 +537,48 @@ class LyngCompletionContributor : CompletionContributor() {
for (name in ext) {
if (already.contains(name)) continue
// Try to resolve full signature via registry first to get params and return type
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name, mini)
if (resolved != null) {
when (val member = resolved.second) {
val m = resolved.second
val builder = when (m) {
is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType)
val builder = LookupElementBuilder.create(name)
val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("(${params})", true)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
}
is MiniFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(m.returnType)
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("($params)", true)
.withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler)
emit(builder)
already.add(name)
continue
}
is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true)
emit(builder)
already.add(name)
continue
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
is MiniValDecl -> {
LookupElementBuilder.create(name)
.withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(m.type), true)
}
else -> {
LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method)
.withTailText("()", true)
.withInsertHandler(ParenInsertHandler)
}
is MiniInitDecl -> {}
}
emit(builder)
already.add(name)
continue
}
// Fallback: emit without detailed types if we couldn't resolve
val builder = LookupElementBuilder.create(name)
@ -545,27 +591,6 @@ class LyngCompletionContributor : CompletionContributor() {
}
}
// --- MiniAst-based inference helpers ---
private fun offerParamsInScope(emit: (com.intellij.codeInsight.lookup.LookupElement) -> Unit, mini: MiniScript, text: String, caret: Int) {
val src = mini.range.start.source
// Find function whose body contains caret or whose whole range contains caret
val fns = mini.declarations.filterIsInstance<MiniFunDecl>()
for (fn in fns) {
val start = src.offsetOf(fn.range.start)
val end = src.offsetOf(fn.range.end).coerceAtMost(text.length)
if (caret in start..end) {
for (p in fn.params) {
val builder = LookupElementBuilder.create(p.name)
.withIcon(AllIcons.Nodes.Variable)
.withTypeText(typeOf(p.type), true)
emit(builder)
}
return
}
}
}
// Lenient textual import extractor (duplicated from QuickDoc privately)
private fun extractImportsFromText(text: String): List<String> {
val result = LinkedHashSet<String>()

View File

@ -24,13 +24,9 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Pos
import net.sergeych.lyng.Source
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.util.IdeLenientImportProvider
import net.sergeych.lyng.idea.util.LyngAstManager
import net.sergeych.lyng.idea.util.TextCtx
import net.sergeych.lyng.miniast.*
@ -69,80 +65,93 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val ident = text.substring(idRange.startOffset, idRange.endOffset)
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: ident='$ident' at ${idRange.startOffset}..${idRange.endOffset} in ${file.name}")
// Build MiniAst for this file (fast and resilient). Best-effort; on failure continue with partial AST.
val sink = MiniAstBuilder()
val provider = IdeLenientImportProvider.create()
val src = Source("<ide>", text)
val mini = try {
runBlocking { Compiler.compileWithMini(src, provider, sink) }
sink.build()
} catch (t: Throwable) {
if (DEBUG_LOG) log.warn("[LYNG_DEBUG] QuickDoc: compileWithMini produced partial AST: ${t.message}")
sink.build()
} ?: MiniScript(MiniRange(Pos(src, 1, 1), Pos(src, 1, 1)))
val source = src
// 1. Get merged mini-AST from Manager (handles local + .lyng.d merged declarations)
val mini = LyngAstManager.getMiniAst(file) ?: return null
val miniSource = mini.range.start.source
val imported = DocLookupUtils.canonicalImportedModules(mini, text)
// Try resolve to: function param at position, function/class/val declaration at position
// 1) Use unified declaration detection
DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) ->
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind")
// Find the actual declaration object to render
for (d in mini.declarations) {
if (d.name == name && source.offsetOf(d.nameStart) <= offset && source.offsetOf(d.nameStart) + d.name.length > offset) {
return renderDeclDoc(d)
mini.declarations.forEach { d ->
if (d.name == name) {
val s: Int = miniSource.offsetOf(d.nameStart)
if (s <= offset && s + d.name.length > offset) {
return renderDeclDoc(d, text, mini, imported)
}
}
// Handle members if it was a member
if (d is MiniClassDecl) {
for (m in d.members) {
if (m.name == name && source.offsetOf(m.nameStart) <= offset && source.offsetOf(m.nameStart) + m.name.length > offset) {
return when (m) {
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
is MiniInitDecl -> null
d.members.forEach { m ->
if (m.name == name) {
val s: Int = miniSource.offsetOf(m.nameStart)
if (s <= offset && s + m.name.length > offset) {
return when (m) {
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
else -> null
}
}
}
}
for (cf in d.ctorFields) {
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(d.name, mv)
d.ctorFields.forEach { cf ->
if (cf.name == name) {
val s: Int = miniSource.offsetOf(cf.nameStart)
if (s <= offset && s + cf.name.length > offset) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
initRange = null,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(d.name, mv)
}
}
}
for (cf in d.classFields) {
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(d.name, mv)
d.classFields.forEach { cf ->
if (cf.name == name) {
val s: Int = miniSource.offsetOf(cf.nameStart)
if (s <= offset && s + cf.name.length > offset) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
initRange = null,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(d.name, mv)
}
}
}
}
if (d is MiniEnumDecl) {
if (d.entries.contains(name) && offset >= source.offsetOf(d.range.start) && offset <= source.offsetOf(d.range.end)) {
// For enum constant, we don't have detailed docs in MiniAst yet, but we can render a title
return "<div class='doc-title'>enum constant ${d.name}.${name}</div>"
if (d.entries.contains(name)) {
val s: Int = miniSource.offsetOf(d.range.start)
val e: Int = miniSource.offsetOf(d.range.end)
if (offset >= s && offset <= e) {
// For enum constant, we don't have detailed docs in MiniAst yet, but we can render a title
return "<div class='doc-title'>enum constant ${d.name}.${name}</div>"
}
}
}
}
// Check parameters
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
for (p in fn.params) {
if (p.name == name && source.offsetOf(p.nameStart) <= offset && source.offsetOf(p.nameStart) + p.name.length > offset) {
return renderParamDoc(fn, p)
mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn ->
fn.params.forEach { p ->
if (p.name == name) {
val s: Int = miniSource.offsetOf(p.nameStart)
if (s <= offset && s + p.name.length > offset) {
return renderParamDoc(fn, p)
}
}
}
}
@ -156,62 +165,77 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) {
// Find local declaration that matches this symbol
val ds = mini.declarations.firstOrNull { decl ->
val s = source.offsetOf(decl.nameStart)
decl.name == sym.name && s == sym.declStart
var dsFound: MiniDecl? = null
mini.declarations.forEach { decl ->
if (decl.name == sym.name) {
val sOffset: Int = miniSource.offsetOf(decl.nameStart)
if (sOffset == sym.declStart) {
dsFound = decl
}
}
}
if (ds != null) return renderDeclDoc(ds)
if (dsFound != null) return renderDeclDoc(dsFound, text, mini, imported)
// Check parameters
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) {
for (p in fn.params) {
val s = source.offsetOf(p.nameStart)
if (p.name == sym.name && s == sym.declStart) {
return renderParamDoc(fn, p)
mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn ->
fn.params.forEach { p ->
if (p.name == sym.name) {
val sOffset: Int = miniSource.offsetOf(p.nameStart)
if (sOffset == sym.declStart) {
return renderParamDoc(fn, p)
}
}
}
}
// Check class members (fields/functions)
for (cls in mini.declarations.filterIsInstance<MiniClassDecl>()) {
for (m in cls.members) {
val s = source.offsetOf(m.nameStart)
if (m.name == sym.name && s == sym.declStart) {
return when (m) {
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
is MiniInitDecl -> null
mini.declarations.filterIsInstance<MiniClassDecl>().forEach { cls ->
cls.members.forEach { m ->
if (m.name == sym.name) {
val sOffset: Int = miniSource.offsetOf(m.nameStart)
if (sOffset == sym.declStart) {
return when (m) {
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
else -> null
}
}
}
}
for (cf in cls.ctorFields) {
val s = source.offsetOf(cf.nameStart)
if (cf.name == sym.name && s == sym.declStart) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(cls.name, mv)
cls.ctorFields.forEach { cf ->
if (cf.name == sym.name) {
val sOffset: Int = miniSource.offsetOf(cf.nameStart)
if (sOffset == sym.declStart) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
initRange = null,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(cls.name, mv)
}
}
}
for (cf in cls.classFields) {
val s = source.offsetOf(cf.nameStart)
if (cf.name == sym.name && s == sym.declStart) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(cls.name, mv)
cls.classFields.forEach { cf ->
if (cf.name == sym.name) {
val sOffset: Int = miniSource.offsetOf(cf.nameStart)
if (sOffset == sym.declStart) {
// Render as a member val
val mv = MiniMemberValDecl(
range = MiniRange(cf.nameStart, cf.nameStart), // dummy
name = cf.name,
mutable = cf.mutable,
type = cf.type,
initRange = null,
doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(cls.name, mv)
}
}
}
}
@ -260,35 +284,39 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} else null
}
else -> {
val guessed = DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
if (guessed != null) guessed
else {
// handle this@Type or as Type
val i2 = TextCtx.prevNonWs(text, dotPos - 1)
if (i2 >= 0) {
val identRange = TextCtx.wordRangeAt(text, i2 + 1)
if (identRange != null) {
val id = text.substring(identRange.startOffset, identRange.endOffset)
val k = TextCtx.prevNonWs(text, identRange.startOffset - 1)
if (k >= 1 && text[k] == 's' && text[k-1] == 'a' && (k-1 == 0 || !text[k-2].isLetterOrDigit())) {
id
} else if (k >= 0 && text[k] == '@') {
val k2 = TextCtx.prevNonWs(text, k - 1)
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") id else null
DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, importedModules)
?: DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini)
?: run {
// handle this@Type or as Type
val i2 = TextCtx.prevNonWs(text, dotPos - 1)
if (i2 >= 0) {
val identRange = TextCtx.wordRangeAt(text, i2 + 1)
if (identRange != null) {
val id = text.substring(identRange.startOffset, identRange.endOffset)
val k = TextCtx.prevNonWs(text, identRange.startOffset - 1)
if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
id
} else if (k >= 0 && text[k] == '@') {
val k2 = TextCtx.prevNonWs(text, k - 1)
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") id else null
} else null
} else null
} else null
} else null
}
}
}
}
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos>0) text[dotPos-1] else ' '}' classGuess=${className} imports=${importedModules}")
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos > 0) text[dotPos - 1] else ' '}' classGuess=${className} imports=${importedModules}")
if (className != null) {
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident)?.let { (owner, member) ->
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}")
return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
}
}
log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}")
@ -299,7 +327,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
// 4) As a fallback, if the caret is on an identifier text that matches any declaration name, show that
mini.declarations.firstOrNull { it.name == ident }?.let {
log.info("[LYNG_DEBUG] QuickDoc: fallback by name '${it.name}' kind=${it::class.simpleName}")
return renderDeclDoc(it)
return renderDeclDoc(it, text, mini, imported)
}
// 4) Consult BuiltinDocRegistry for imported modules (top-level and class members)
@ -319,13 +347,13 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
if (arity != null && chosen.params.size != arity && matches.size > 1) {
return renderOverloads(ident, matches)
}
return renderDeclDoc(chosen)
return renderDeclDoc(chosen, text, mini, imported)
}
// Also allow values/consts
docs.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
docs.filterIsInstance<MiniValDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
// And classes/enums
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it) }
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
}
// Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs
if (ident == "println" || ident == "print") {
@ -339,12 +367,16 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val lhs = previousWordBefore(text, idRange.startOffset)
if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) {
val className = text.substring(lhs.startOffset, lhs.endOffset)
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident)?.let { (owner, member) ->
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Inheritance resolved $className.$ident to $owner.${member.name}")
return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
}
}
} else {
@ -355,15 +387,19 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
if (dotPos != null) {
val guessed = when {
looksLikeListLiteralBefore(text, dotPos) -> "List"
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini)
}
if (guessed != null) {
DocLookupUtils.resolveMemberWithInheritance(importedModules, guessed, ident)?.let { (owner, member) ->
DocLookupUtils.resolveMemberWithInheritance(importedModules, guessed, ident, mini)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Heuristic '$guessed.$ident' resolved via inheritance to $owner.${member.name}")
return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
}
}
} else {
@ -371,19 +407,23 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
run {
val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex")
for (c in candidates) {
DocLookupUtils.resolveMemberWithInheritance(importedModules, c, ident)?.let { (owner, member) ->
DocLookupUtils.resolveMemberWithInheritance(importedModules, c, ident, mini)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Candidate '$c.$ident' resolved via inheritance to $owner.${member.name}")
return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
}
}
}
}
// As a last resort try aggregated String members (extensions from stdlib text)
run {
val classes = DocLookupUtils.aggregateClasses(importedModules)
val classes = DocLookupUtils.aggregateClasses(importedModules, mini)
val stringCls = classes["String"]
val m = stringCls?.members?.firstOrNull { it.name == ident }
if (m != null) {
@ -396,12 +436,16 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
}
}
// Search across classes; prefer Iterable, then Iterator, then List for common ops
DocLookupUtils.findMemberAcrossClasses(importedModules, ident)?.let { (owner, member) ->
DocLookupUtils.findMemberAcrossClasses(importedModules, ident, mini)?.let { (owner, member) ->
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Cross-class '$ident' resolved to $owner.${member.name}")
return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
}
}
}
@ -449,12 +493,16 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
return contextElement ?: file.findElementAt(targetOffset)
}
private fun renderDeclDoc(d: MiniDecl): String {
private fun renderDeclDoc(d: MiniDecl, text: String, mini: MiniScript, imported: List<String>): String {
val title = when (d) {
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
is MiniClassDecl -> "class ${d.name}"
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }"
is MiniValDecl -> if (d.mutable) "var ${d.name}${typeOf(d.type)}" else "val ${d.name}${typeOf(d.type)}"
is MiniValDecl -> {
val t = d.type ?: DocLookupUtils.inferTypeRefForVal(d, text, imported, mini)
val typeStr = if (t == null) ": Object?" else typeOf(t)
if (d.mutable) "var ${d.name}${typeStr}" else "val ${d.name}${typeStr}"
}
}
// Show full detailed documentation, not just the summary
val raw = d.doc?.raw
@ -487,7 +535,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
}
private fun renderMemberValDoc(className: String, m: MiniMemberValDecl): String {
val ts = typeOf(m.type)
val ts = if (m.type == null) ": Object?" else typeOf(m.type)
val kind = if (m.mutable) "var" else "val"
val staticStr = if (m.isStatic) "static " else ""
val title = "${staticStr}${kind} $className.${m.name}${ts}"
@ -508,7 +556,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
}
is MiniFunctionType -> ": (..) -> ..${if (t.nullable) "?" else ""}"
is MiniTypeVar -> ": ${t.name}${if (t.nullable) "?" else ""}"
null -> ""
null -> ": Object?"
}
private fun signatureOf(fn: MiniFunDecl): String {
@ -609,10 +657,14 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
}
private fun previousWordBefore(text: String, offset: Int): TextRange? {
// skip spaces and dots to the left, but stop after hitting a non-identifier or dot boundary
// skip spaces and the dot to the left, but stop after hitting a non-identifier boundary
var i = (offset - 1).coerceAtLeast(0)
// first, move left past spaces
while (i > 0 && text[i].isWhitespace()) i--
// skip trailing spaces
while (i >= 0 && text[i].isWhitespace()) i--
// skip the dot if present
if (i >= 0 && text[i] == '.') i--
// skip spaces before the dot
while (i >= 0 && text[i].isWhitespace()) i--
// remember position to check for dot between words
val end = i + 1
// now find the start of the identifier

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.
@ -42,7 +42,7 @@ private class LineBlocksRootBlock(
private val file: PsiFile,
private val settings: CodeStyleSettings
) : Block {
override fun getTextRange(): TextRange = file.textRange
override fun getTextRange(): TextRange = TextRange(0, file.textLength)
override fun getSubBlocks(): List<Block> = emptyList()
@ -52,7 +52,7 @@ private class LineBlocksRootBlock(
override fun getSpacing(child1: Block?, child2: Block): Spacing? = null
override fun getChildAttributes(newChildIndex: Int): ChildAttributes = ChildAttributes(Indent.getNoneIndent(), null)
override fun isIncomplete(): Boolean = false
override fun isLeaf(): Boolean = false
override fun isLeaf(): Boolean = true
}
// Intentionally no sub-blocks/spacing: indentation is handled by PreFormatProcessor + LineIndentProvider

View File

@ -44,25 +44,67 @@ class LyngPreFormatProcessor : PreFormatProcessor {
// When both spacing and wrapping are OFF, still fix indentation for the whole file to
// guarantee visible changes on Reformat Code.
val runFullFileIndent = !settings.enableSpacing && !settings.enableWrapping
// Maintain a working range and a modification flag to avoid stale offsets after replacements
var modified = false
fun fullRange(): TextRange = TextRange(0, doc.textLength)
var workingRange: TextRange = range.intersection(fullRange()) ?: fullRange()
val startLine = if (runFullFileIndent) 0 else doc.getLineNumber(workingRange.startOffset)
val endLine = if (runFullFileIndent) (doc.lineCount - 1).coerceAtLeast(0)
else doc.getLineNumber(workingRange.endOffset.coerceAtMost(doc.textLength))
val docW = doc as? com.intellij.injected.editor.DocumentWindow
// The host range of the entire injected fragment (or the whole file if not injected).
fun currentHostRange(): TextRange = if (docW != null) {
TextRange(docW.injectedToHost(0), docW.injectedToHost(doc.textLength))
} else {
file.textRange
}
// The range in 'doc' coordinate system (local 0..len for injections, host offsets for normal files).
fun currentLocalRange(): TextRange = if (docW != null) {
TextRange(0, doc.textLength)
} else {
file.textRange
}
val clr = currentLocalRange()
val chr = currentHostRange()
// Convert the input range to the coordinate system of 'doc'
var workingRangeLocal: TextRange = if (docW != null) {
val hostIntersection = range.intersection(chr)
if (hostIntersection != null) {
try {
val start = docW.hostToInjected(hostIntersection.startOffset)
val end = docW.hostToInjected(hostIntersection.endOffset)
TextRange(start.coerceAtMost(end), end.coerceAtLeast(start))
} catch (e: Exception) {
clr
}
} else {
range.intersection(clr) ?: clr
}
} else {
range.intersection(clr) ?: clr
}
val startLine = if (runFullFileIndent) {
doc.getLineNumber(currentLocalRange().startOffset)
} else {
doc.getLineNumber(workingRangeLocal.startOffset)
}
val endLine = if (runFullFileIndent) {
if (clr.endOffset <= clr.startOffset) doc.getLineNumber(clr.startOffset)
else doc.getLineNumber(clr.endOffset)
} else {
doc.getLineNumber(workingRangeLocal.endOffset.coerceAtMost(doc.textLength))
}
fun codePart(s: String): String {
val idx = s.indexOf("//")
return if (idx >= 0) s.substring(0, idx) else s
}
// Pre-scan to compute balances up to startLine
// Pre-scan to compute balances up to startLine.
val fragmentStartLine = doc.getLineNumber(currentLocalRange().startOffset)
var blockLevel = 0
var parenBalance = 0
var bracketBalance = 0
for (ln in 0 until startLine) {
for (ln in fragmentStartLine until startLine) {
val text = doc.getText(TextRange(doc.getLineStartOffset(ln), doc.getLineEndOffset(ln)))
for (ch in codePart(text)) when (ch) {
'{' -> blockLevel++
@ -85,7 +127,6 @@ class LyngPreFormatProcessor : PreFormatProcessor {
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
}
}
@ -110,15 +151,14 @@ class LyngPreFormatProcessor : PreFormatProcessor {
useTabs = options.USE_TAB_CHARACTER,
continuationIndentSize = options.CONTINUATION_INDENT_SIZE.coerceAtLeast(options.INDENT_SIZE.coerceAtLeast(1)),
)
val full = fullRange()
val r = if (runFullFileIndent) full else workingRange.intersection(full) ?: full
val r = if (runFullFileIndent) currentLocalRange() else workingRangeLocal.intersection(currentLocalRange()) ?: currentLocalRange()
val text = doc.getText(r)
val formatted = LyngFormatter.reindent(text, cfg)
if (formatted != text) {
doc.replaceString(r.startOffset, r.endOffset, formatted)
modified = true
psiDoc.commitDocument(doc)
workingRange = fullRange()
workingRangeLocal = currentLocalRange()
}
}
@ -131,14 +171,14 @@ class LyngPreFormatProcessor : PreFormatProcessor {
applySpacing = true,
applyWrapping = false,
)
val safe = workingRange.intersection(fullRange()) ?: fullRange()
val text = doc.getText(safe)
val r = if (runFullFileIndent) currentLocalRange() else workingRangeLocal.intersection(currentLocalRange()) ?: currentLocalRange()
val text = doc.getText(r)
val formatted = LyngFormatter.format(text, cfg)
if (formatted != text) {
doc.replaceString(safe.startOffset, safe.endOffset, formatted)
doc.replaceString(r.startOffset, r.endOffset, formatted)
modified = true
psiDoc.commitDocument(doc)
workingRange = fullRange()
workingRangeLocal = currentLocalRange()
}
}
// Optionally apply wrapping (after spacing) when enabled
@ -150,17 +190,19 @@ class LyngPreFormatProcessor : PreFormatProcessor {
applySpacing = settings.enableSpacing,
applyWrapping = true,
)
val safe2 = workingRange.intersection(fullRange()) ?: fullRange()
val text2 = doc.getText(safe2)
val wrapped = LyngFormatter.format(text2, cfg)
if (wrapped != text2) {
doc.replaceString(safe2.startOffset, safe2.endOffset, wrapped)
val r = if (runFullFileIndent) currentLocalRange() else workingRangeLocal.intersection(currentLocalRange()) ?: currentLocalRange()
val text = doc.getText(r)
val wrapped = LyngFormatter.format(text, cfg)
if (wrapped != text) {
doc.replaceString(r.startOffset, r.endOffset, wrapped)
modified = true
psiDoc.commitDocument(doc)
workingRange = fullRange()
workingRangeLocal = currentLocalRange()
}
}
// Return a safe range for the formatter to continue with, preventing stale offsets
return if (modified) fullRange() else (range.intersection(fullRange()) ?: fullRange())
// Return a safe range for the formatter to continue with, preventing stale offsets.
// For injected files, ALWAYS return a range in local coordinates.
val finalRange = currentLocalRange()
return if (modified) finalRange else (range.intersection(finalRange) ?: finalRange)
}
}

View File

@ -579,7 +579,8 @@ class LyngGrazieAnnotator : ExternalAnnotator<LyngGrazieAnnotator.Input, LyngGra
val s = w.lowercase()
return s in setOf(
// common code words / language keywords to avoid noise
"val","var","fun","class","enum","type","import","package","return","if","else","when","while","for","try","catch","finally","true","false","null",
"val","var","fun","class","interface","enum","type","import","package","return","if","else","when","while","for","try","catch","finally","true","false","null",
"abstract","closed","override",
// very common English words
"the","and","or","not","with","from","into","this","that","file","found","count","name","value","object"
)

View File

@ -43,10 +43,15 @@ class LyngColorSettingsPage : ColorSettingsPage {
}
var counter = 0
counter = counter + 1
outer@ while (counter < 10) {
if (counter == 5) return@outer
counter = counter + 1
}
""".trimIndent()
override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey>? = null
override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey> = mutableMapOf(
"label" to LyngHighlighterColors.LABEL
)
override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf(
AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD),
@ -58,6 +63,7 @@ class LyngColorSettingsPage : ColorSettingsPage {
AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT),
// Semantic
AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION),
AttributesDescriptor("Label (semantic)", LyngHighlighterColors.LABEL),
AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE),
AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE),
AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION),

View File

@ -82,4 +82,9 @@ object LyngHighlighterColors {
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
"LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD
)
// Labels (label@ or @label used as exit target)
val LABEL: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
"LYNG_LABEL", DefaultLanguageHighlighterColors.LABEL
)
}

View File

@ -32,10 +32,11 @@ class LyngLexer : LexerBase() {
private var myTokenType: IElementType? = null
private val keywords = setOf(
"fun", "val", "var", "class", "type", "import", "as",
"fun", "val", "var", "class", "interface", "type", "import", "as",
"abstract", "closed", "override", "static", "extern", "open", "private", "protected",
"if", "else", "for", "while", "return", "true", "false", "null",
"when", "in", "is", "break", "continue", "try", "catch", "finally",
"get", "set"
"get", "set", "object", "enum", "init", "by", "property", "constructor"
)
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
@ -132,10 +133,24 @@ class LyngLexer : LexerBase() {
return
}
// Identifier / keyword
// Labels / Annotations: @label or label@
if (ch == '@') {
i++
while (i < endOffset && (buffer[i].isIdentifierPart())) i++
myTokenEnd = i
myTokenType = LyngTokenTypes.LABEL
return
}
if (ch.isIdentifierStart()) {
i++
while (i < endOffset && buffer[i].isIdentifierPart()) i++
if (i < endOffset && buffer[i] == '@') {
i++
myTokenEnd = i
myTokenType = LyngTokenTypes.LABEL
return
}
myTokenEnd = i
val text = buffer.subSequence(myTokenStart, myTokenEnd).toString()
myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER
@ -159,5 +174,5 @@ class LyngLexer : LexerBase() {
private fun Char.isDigit(): Boolean = this in '0'..'9'
private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter()
private fun Char.isIdentifierPart(): Boolean = this.isIdentifierStart() || this.isDigit()
private fun isPunct(c: Char): Boolean = c in setOf('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~')
private fun isPunct(c: Char): Boolean = c in setOf('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~', '@')
}

View File

@ -33,6 +33,7 @@ class LyngSyntaxHighlighter : SyntaxHighlighter {
LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT)
LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT)
LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER)
LyngTokenTypes.LABEL -> pack(LyngHighlighterColors.LABEL)
else -> emptyArray()
}

View File

@ -29,6 +29,7 @@ object LyngTokenTypes {
val NUMBER = LyngTokenType("NUMBER")
val KEYWORD = LyngTokenType("KEYWORD")
val IDENTIFIER = LyngTokenType("IDENTIFIER")
val LABEL = LyngTokenType("LABEL")
val PUNCT = LyngTokenType("PUNCT")
val BAD_CHAR = LyngTokenType("BAD_CHAR")
}

View File

@ -63,6 +63,10 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
is MiniMemberFunDecl -> "Function"
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
is MiniInitDecl -> "Initializer"
is MiniFunDecl -> "Function"
is MiniValDecl -> if (member.mutable) "Variable" else "Value"
is MiniClassDecl -> "Class"
is MiniEnumDecl -> "Enum"
}
results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
}

View File

@ -17,8 +17,10 @@
package net.sergeych.lyng.idea.util
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.util.Key
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Source
@ -32,53 +34,100 @@ object LyngAstManager {
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
fun getMiniAst(file: PsiFile): MiniScript? {
val doc = file.viewProvider.document ?: return null
val stamp = doc.modificationStamp
fun getMiniAst(file: PsiFile): MiniScript? = runReadAction {
val vFile = file.virtualFile ?: return@runReadAction null
val combinedStamp = getCombinedStamp(file)
val prevStamp = file.getUserData(STAMP_KEY)
val cached = file.getUserData(MINI_KEY)
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached
val text = doc.text
val text = file.viewProvider.contents.toString()
val sink = MiniAstBuilder()
val built = try {
val provider = IdeLenientImportProvider.create()
val src = Source(file.name, text)
runBlocking { Compiler.compileWithMini(src, provider, sink) }
sink.build()
val script = sink.build()
if (script != null && !file.name.endsWith(".lyng.d")) {
val dFiles = collectDeclarationFiles(file)
for (df in dFiles) {
val scriptD = getMiniAst(df)
if (scriptD != null) {
script.declarations.addAll(scriptD.declarations)
script.imports.addAll(scriptD.imports)
}
}
}
script
} catch (_: Throwable) {
sink.build()
}
if (built != null) {
file.putUserData(MINI_KEY, built)
file.putUserData(STAMP_KEY, stamp)
file.putUserData(STAMP_KEY, combinedStamp)
// Invalidate binding too
file.putUserData(BINDING_KEY, null)
}
return built
built
}
fun getBinding(file: PsiFile): BindingSnapshot? {
val doc = file.viewProvider.document ?: return null
val stamp = doc.modificationStamp
fun getCombinedStamp(file: PsiFile): Long = runReadAction {
var combinedStamp = file.viewProvider.modificationStamp
if (!file.name.endsWith(".lyng.d")) {
collectDeclarationFiles(file).forEach { df ->
combinedStamp += df.viewProvider.modificationStamp
}
}
combinedStamp
}
private fun collectDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
val psiManager = PsiManager.getInstance(file.project)
var current = file.virtualFile?.parent
val seen = mutableSetOf<String>()
val result = mutableListOf<PsiFile>()
while (current != null) {
for (child in current.children) {
if (child.name.endsWith(".lyng.d") && child != file.virtualFile && seen.add(child.path)) {
val psiD = psiManager.findFile(child) ?: continue
result.add(psiD)
}
}
current = current.parent
}
result
}
fun getBinding(file: PsiFile): BindingSnapshot? = runReadAction {
val vFile = file.virtualFile ?: return@runReadAction null
var combinedStamp = file.viewProvider.modificationStamp
val dFiles = if (!file.name.endsWith(".lyng.d")) collectDeclarationFiles(file) else emptyList()
for (df in dFiles) {
combinedStamp += df.viewProvider.modificationStamp
}
val prevStamp = file.getUserData(STAMP_KEY)
val cached = file.getUserData(BINDING_KEY)
if (cached != null && prevStamp != null && prevStamp == stamp) return cached
val mini = getMiniAst(file) ?: return null
val text = doc.text
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached
val mini = getMiniAst(file) ?: return@runReadAction null
val text = file.viewProvider.contents.toString()
val binding = try {
Binder.bind(text, mini)
} catch (_: Throwable) {
null
}
if (binding != null) {
file.putUserData(BINDING_KEY, binding)
// stamp is already set by getMiniAst
// stamp is already set by getMiniAst or we set it here if getMiniAst was cached
file.putUserData(STAMP_KEY, combinedStamp)
}
return binding
binding
}
}

View File

@ -19,7 +19,7 @@
<!-- Open-ended compatibility: 2024.3+ (build 243 and newer) -->
<idea-version since-build="243"/>
<id>net.sergeych.lyng.idea</id>
<name>Lyng Language Support</name>
<name>Lyng</name>
<vendor email="real.sergeych@gmail.com">Sergey Chernov</vendor>
<description>
@ -43,6 +43,7 @@
<extensions defaultExtensionNs="com.intellij">
<!-- Language and file type -->
<fileType implementationClass="net.sergeych.lyng.idea.LyngFileType" name="Lyng" extensions="lyng" fieldName="INSTANCE" language="Lyng"/>
<fileTypeFactory implementation="net.sergeych.lyng.idea.LyngFileTypeFactory"/>
<!-- Minimal parser/PSI to fully wire editor services for the language -->
<lang.parserDefinition language="Lyng" implementationClass="net.sergeych.lyng.idea.psi.LyngParserDefinition"/>

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.
@ -20,7 +20,6 @@
*/
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
@ -53,11 +52,11 @@ kotlin {
browser()
nodejs()
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs() {
browser()
nodejs()
}
// @OptIn(ExperimentalWasmDsl::class)
// wasmJs() {
// browser()
// nodejs()
// }
// Keep expect/actual warning suppressed consistently with other modules
targets.configureEach {
@ -94,13 +93,13 @@ kotlin {
implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}")
}
}
// For Wasm we use in-memory VFS for now
val wasmJsMain by getting {
dependencies {
api(libs.okio)
implementation(libs.okio.fakefilesystem)
}
}
// // For Wasm we use in-memory VFS for now
// val wasmJsMain by getting {
// dependencies {
// api(libs.okio)
// implementation(libs.okio.fakefilesystem)
// }
// }
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.lyngio.process
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = "Android",
version = android.os.Build.VERSION.RELEASE,
arch = android.os.Build.SUPPORTED_ABIS.firstOrNull() ?: "unknown",
kernelVersion = System.getProperty("os.version")
)
}
actual fun isProcessSupported(): Boolean = false
actual fun getSystemProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on Android yet")
}

View File

@ -0,0 +1,234 @@
/*
* 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.io.process
import kotlinx.coroutines.flow.Flow
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.statement
import net.sergeych.lyngio.process.*
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
/**
* Install Lyng module `lyng.io.process` into the given scope's ImportManager.
*/
fun createProcessModule(policy: ProcessAccessPolicy, scope: Scope): Boolean =
createProcessModule(policy, scope.importManager)
/** Same as [createProcessModule] but with explicit [ImportManager]. */
fun createProcessModule(policy: ProcessAccessPolicy, manager: ImportManager): Boolean {
val name = "lyng.io.process"
if (manager.packageNames.contains(name)) return false
manager.addPackage(name) { module ->
buildProcessModule(module, policy)
}
return true
}
private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAccessPolicy) {
val runner = try {
SecuredLyngProcessRunner(getSystemProcessRunner(), policy)
} catch (e: Exception) {
null
}
val runningProcessType = object : ObjClass("RunningProcess") {}
runningProcessType.apply {
addFnDoc(
name = "stdout",
doc = "Get standard output stream as a Flow of lines.",
returns = type("lyng.Flow"),
moduleName = module.packageName
) {
val self = thisAs<ObjRunningProcess>()
self.process.stdout.toLyngFlow(this)
}
addFnDoc(
name = "stderr",
doc = "Get standard error stream as a Flow of lines.",
returns = type("lyng.Flow"),
moduleName = module.packageName
) {
val self = thisAs<ObjRunningProcess>()
self.process.stderr.toLyngFlow(this)
}
addFnDoc(
name = "signal",
doc = "Send a signal to the process (e.g. 'SIGINT', 'SIGTERM', 'SIGKILL').",
params = listOf(ParamDoc("signal", type("lyng.String"))),
moduleName = module.packageName
) {
processGuard {
val sigStr = requireOnlyArg<ObjString>().value.uppercase()
val sig = try {
ProcessSignal.valueOf(sigStr)
} catch (e: Exception) {
try {
ProcessSignal.valueOf("SIG$sigStr")
} catch (e2: Exception) {
raiseIllegalArgument("Unknown signal: $sigStr")
}
}
thisAs<ObjRunningProcess>().process.sendSignal(sig)
ObjVoid
}
}
addFnDoc(
name = "waitFor",
doc = "Wait for the process to exit and return its exit code.",
returns = type("lyng.Int"),
moduleName = module.packageName
) {
processGuard {
thisAs<ObjRunningProcess>().process.waitFor().toObj()
}
}
addFnDoc(
name = "destroy",
doc = "Forcefully terminate the process.",
moduleName = module.packageName
) {
thisAs<ObjRunningProcess>().process.destroy()
ObjVoid
}
}
val processType = object : ObjClass("Process") {}
processType.apply {
addClassFnDoc(
name = "execute",
doc = "Execute a process with arguments.",
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
returns = type("RunningProcess"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val executable = requiredArg<ObjString>(0).value
val args = requiredArg<ObjList>(1).list.map { it.toString() }
val lp = runner.execute(executable, args)
ObjRunningProcess(runningProcessType, lp)
}
}
addClassFnDoc(
name = "shell",
doc = "Execute a command via system shell.",
params = listOf(ParamDoc("command", type("lyng.String"))),
returns = type("RunningProcess"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val command = requireOnlyArg<ObjString>().value
val lp = runner.shell(command)
ObjRunningProcess(runningProcessType, lp)
}
}
}
val platformType = object : ObjClass("Platform") {}
platformType.apply {
addClassFnDoc(
name = "details",
doc = "Get platform core details.",
returns = type("lyng.Map"),
moduleName = module.packageName
) {
val d = getPlatformDetails()
ObjMap(mutableMapOf(
ObjString("name") to ObjString(d.name),
ObjString("version") to ObjString(d.version),
ObjString("arch") to ObjString(d.arch),
ObjString("kernelVersion") to (d.kernelVersion?.toObj() ?: ObjNull)
))
}
addClassFnDoc(
name = "isSupported",
doc = "Check if processes are supported on this platform.",
returns = type("lyng.Bool"),
moduleName = module.packageName
) {
isProcessSupported().toObj()
}
}
module.addConstDoc(
name = "Process",
value = processType,
doc = "Process execution and control.",
type = type("Process"),
moduleName = module.packageName
)
module.addConstDoc(
name = "Platform",
value = platformType,
doc = "Platform information.",
type = type("Platform"),
moduleName = module.packageName
)
module.addConstDoc(
name = "RunningProcess",
value = runningProcessType,
doc = "Handle to a running process.",
type = type("RunningProcess"),
moduleName = module.packageName
)
}
class ObjRunningProcess(
override val objClass: ObjClass,
val process: LyngProcess
) : Obj() {
override fun toString(): String = "RunningProcess($process)"
}
private suspend inline fun Scope.processGuard(crossinline block: suspend () -> Obj): Obj {
return try {
block()
} catch (e: ProcessAccessDeniedException) {
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "process access denied"))
} catch (e: Exception) {
raiseError(ObjIllegalOperationException(this, e.message ?: "process error"))
}
}
private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
val producer = statement {
val builder = (this as? net.sergeych.lyng.ClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
?: this.thisObj as? ObjFlowBuilder
this@toLyngFlow.collect {
try {
builder?.output?.send(ObjString(it))
} catch (e: Exception) {
// Channel closed or other error, stop collecting
return@collect
}
}
ObjVoid
}
return ObjFlow(producer, flowScope)
}

View File

@ -1,3 +1,20 @@
/*
* 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.
*
*/
/*
* Filesystem module builtin docs registration, located in lyngio so core library
* does not depend on external packages. The IDEA plugin (and any other tooling)

View File

@ -0,0 +1,93 @@
/*
* 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.lyngio.process
import kotlinx.coroutines.flow.Flow
import net.sergeych.lyngio.process.security.ProcessAccessOp
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
/**
* Common signals for process control.
*/
enum class ProcessSignal {
SIGINT, SIGTERM, SIGKILL
}
/**
* Multiplatform process representation.
*/
interface LyngProcess {
/**
* Standard output stream as a flow of strings (lines).
*/
val stdout: Flow<String>
/**
* Standard error stream as a flow of strings (lines).
*/
val stderr: Flow<String>
/**
* Send a signal to the process.
* Throws exception if signals are not supported on the platform or for this process.
*/
suspend fun sendSignal(signal: ProcessSignal)
/**
* Wait for the process to exit and return the exit code.
*/
suspend fun waitFor(): Int
/**
* Forcefully terminate the process.
*/
fun destroy()
}
/**
* Interface for running processes.
*/
interface LyngProcessRunner {
/**
* Execute a process with the given executable and arguments.
*/
suspend fun execute(executable: String, args: List<String>): LyngProcess
/**
* Execute a command via the platform's default shell.
*/
suspend fun shell(command: String): LyngProcess
}
/**
* Secured implementation of [LyngProcessRunner] that checks against a [ProcessAccessPolicy].
*/
class SecuredLyngProcessRunner(
private val runner: LyngProcessRunner,
private val policy: ProcessAccessPolicy
) : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
policy.require(ProcessAccessOp.Execute(executable, args))
return runner.execute(executable, args)
}
override suspend fun shell(command: String): LyngProcess {
policy.require(ProcessAccessOp.Shell(command))
return runner.shell(command)
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.lyngio.process
/**
* Platform core details.
*/
data class PlatformDetails(
val name: String,
val version: String,
val arch: String,
val kernelVersion: String? = null
)
/**
* Get the current platform core details.
*/
expect fun getPlatformDetails(): PlatformDetails
/**
* Check whether the current platform supports processes and shell execution.
*/
expect fun isProcessSupported(): Boolean
/**
* Get the system default [LyngProcessRunner].
* Throws [UnsupportedOperationException] if processes are not supported on this platform.
*/
expect fun getSystemProcessRunner(): LyngProcessRunner

View File

@ -0,0 +1,59 @@
/*
* 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.lyngio.process.security
import net.sergeych.lyngio.fs.security.AccessContext
import net.sergeych.lyngio.fs.security.AccessDecision
import net.sergeych.lyngio.fs.security.Decision
/**
* Primitive process operations for access control decisions.
*/
sealed interface ProcessAccessOp {
data class Execute(val executable: String, val args: List<String>) : ProcessAccessOp
data class Shell(val command: String) : ProcessAccessOp
}
class ProcessAccessDeniedException(
val op: ProcessAccessOp,
val reasonDetail: String? = null,
) : IllegalStateException("Process access denied for $op" + (reasonDetail?.let { ": $it" } ?: ""))
/**
* Policy interface that decides whether a specific process operation is allowed.
*/
interface ProcessAccessPolicy {
suspend fun check(op: ProcessAccessOp, ctx: AccessContext = AccessContext()): AccessDecision
// Convenience helpers
suspend fun require(op: ProcessAccessOp, ctx: AccessContext = AccessContext()) {
val res = check(op, ctx)
if (!res.isAllowed()) throw ProcessAccessDeniedException(op, res.reason)
}
suspend fun canExecute(executable: String, args: List<String>, ctx: AccessContext = AccessContext()) =
check(ProcessAccessOp.Execute(executable, args), ctx).isAllowed()
suspend fun canShell(command: String, ctx: AccessContext = AccessContext()) =
check(ProcessAccessOp.Shell(command), ctx).isAllowed()
}
object PermitAllProcessAccessPolicy : ProcessAccessPolicy {
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision =
AccessDecision(Decision.Allow)
}

View File

@ -0,0 +1,26 @@
/*
* 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.lyngio.process
internal actual fun getNativeKernelVersion(): String? = null
internal actual fun isNativeProcessSupported(): Boolean = false
internal actual fun getNativeProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on iOS")
}

View File

@ -0,0 +1,32 @@
/*
* 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.lyngio.process
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = "JavaScript",
version = "unknown",
arch = "unknown"
)
}
actual fun isProcessSupported(): Boolean = false
actual fun getSystemProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on JS")
}

View File

@ -0,0 +1,107 @@
/*
* 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.lyngio.process
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
actual fun getPlatformDetails(): PlatformDetails {
val osName = System.getProperty("os.name")
return PlatformDetails(
name = osName,
version = System.getProperty("os.version"),
arch = System.getProperty("os.arch"),
kernelVersion = if (osName.lowercase().contains("linux")) {
System.getProperty("os.version")
} else null
)
}
actual fun isProcessSupported(): Boolean = true
actual fun getSystemProcessRunner(): LyngProcessRunner = JvmProcessRunner
object JvmProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
val process = ProcessBuilder(listOf(executable) + args)
.start()
return JvmLyngProcess(process)
}
override suspend fun shell(command: String): LyngProcess {
val os = System.getProperty("os.name").lowercase()
val shellCmd = if (os.contains("win")) {
listOf("cmd.exe", "/c", command)
} else {
listOf("sh", "-c", command)
}
val process = ProcessBuilder(shellCmd)
.start()
return JvmLyngProcess(process)
}
}
class JvmLyngProcess(private val process: Process) : LyngProcess {
override val stdout: Flow<String> = flow {
val reader = process.inputStream.bufferedReader()
while (true) {
val line = reader.readLine() ?: break
emit(line)
}
}
override val stderr: Flow<String> = flow {
val reader = process.errorStream.bufferedReader()
while (true) {
val line = reader.readLine() ?: break
emit(line)
}
}
override suspend fun sendSignal(signal: ProcessSignal) {
when (signal) {
ProcessSignal.SIGINT -> {
// SIGINT is hard on JVM without native calls or external 'kill'
val os = System.getProperty("os.name").lowercase()
if (os.contains("win")) {
throw UnsupportedOperationException("SIGINT not supported on Windows JVM")
} else {
// Try to use kill -2 <pid>
try {
val pid = process.pid()
Runtime.getRuntime().exec(arrayOf("kill", "-2", pid.toString())).waitFor()
} catch (e: Exception) {
throw UnsupportedOperationException("Failed to send SIGINT: ${e.message}")
}
}
}
ProcessSignal.SIGTERM -> process.destroy()
ProcessSignal.SIGKILL -> process.destroyForcibly()
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.IO) {
process.waitFor()
}
override fun destroy() {
process.destroy()
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.io.process
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertTrue
class LyngProcessModuleTest {
@Test
fun testLyngProcess() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.execute("echo", ["hello", "lyng"])
var output = []
for (line in p.stdout()) {
output.add(line)
}
p.waitFor()
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("hello lyng"))
}
@Test
fun testLyngShell() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.shell("echo 'shell lyng'")
var output = ""
for (line in p.stdout()) {
output = output + line
}
p.waitFor()
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("shell lyng"))
}
@Test
fun testPlatformDetails() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
Platform.details()
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("name"), "Result should contain 'name', but was: ${result.inspect(scope)}")
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.lyngio.process
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class JvmProcessTest {
@Test
fun testExecute() = runBlocking {
val runner = getSystemProcessRunner()
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
val process = secured.execute("echo", listOf("hello", "world"))
val output = process.stdout.toList()
assertEquals(listOf("hello world"), output)
assertEquals(0, process.waitFor())
}
@Test
fun testShell() = runBlocking {
val runner = getSystemProcessRunner()
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
val process = secured.shell("echo 'hello shell'")
val output = process.stdout.toList()
assertEquals(listOf("hello shell"), output)
assertEquals(0, process.waitFor())
}
@Test
fun testPlatformDetails() {
val details = getPlatformDetails()
assertTrue(details.name.isNotEmpty())
assertTrue(isProcessSupported())
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.lyngio.process
import kotlinx.cinterop.*
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal actual fun getNativeKernelVersion(): String? {
return memScoped {
val u = alloc<utsname>()
if (uname(u.ptr) == 0) {
u.release.toKString()
} else null
}
}
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner

View File

@ -0,0 +1,152 @@
/*
* 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.lyngio.process
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal class NativeLyngProcess(
private val pid: pid_t,
private val stdoutFd: Int,
private val stderrFd: Int
) : LyngProcess {
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
override val stderr: Flow<String> = createPipeFlow(stderrFd)
override suspend fun sendSignal(signal: ProcessSignal) {
val sig = when (signal) {
ProcessSignal.SIGINT -> SIGINT
ProcessSignal.SIGTERM -> SIGTERM
ProcessSignal.SIGKILL -> SIGKILL
}
if (kill(pid, sig) != 0) {
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
memScoped {
val status = alloc<IntVar>()
if (waitpid(pid, status.ptr, 0) == -1) {
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
}
val s = status.value
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
}
}
override fun destroy() {
kill(pid, SIGKILL)
}
}
@OptIn(ExperimentalForeignApi::class)
private fun createPipeFlow(fd: Int): Flow<String> = flow {
val buffer = ByteArray(4096)
val lineBuffer = StringBuilder()
try {
while (true) {
val bytesRead = buffer.usePinned { pinned ->
read(fd, pinned.addressOf(0), buffer.size.toULong())
}
if (bytesRead <= 0L) break
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
lineBuffer.append(text)
var newlineIdx = lineBuffer.indexOf('\n')
while (newlineIdx != -1) {
val line = lineBuffer.substring(0, newlineIdx)
emit(line)
lineBuffer.deleteRange(0, newlineIdx + 1)
newlineIdx = lineBuffer.indexOf('\n')
}
}
if (lineBuffer.isNotEmpty()) {
emit(lineBuffer.toString())
}
} finally {
close(fd)
}
}.flowOn(Dispatchers.Default)
@OptIn(ExperimentalForeignApi::class)
object PosixProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
memScoped {
val pipeStdout = allocArray<IntVar>(2)
val pipeStderr = allocArray<IntVar>(2)
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
if (pipe(pipeStderr) != 0) {
close(pipeStdout[0])
close(pipeStdout[1])
throw RuntimeException("Failed to create stderr pipe")
}
val pid = fork()
if (pid == -1) {
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
}
if (pid == 0) {
// Child process
dup2(pipeStdout[1], 1)
dup2(pipeStderr[1], 2)
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
argv[0] = executable.cstr.ptr
for (i in args.indices) {
argv[i + 1] = args[i].cstr.ptr
}
argv[args.size + 1] = null
execvp(executable, argv)
// If we are here, exec failed
_exit(1)
}
// Parent process
close(pipeStdout[1])
close(pipeStderr[1])
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
}
}
override suspend fun shell(command: String): LyngProcess {
return execute("/bin/sh", listOf("-c", command))
}
}

View File

@ -0,0 +1,95 @@
/*
* 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.lyngio.process
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class LinuxProcessTest {
@Test
fun testExecuteEcho() = runBlocking {
val process = PosixProcessRunner.execute("echo", listOf("hello", "native"))
val stdout = process.stdout.toList()
val exitCode = process.waitFor()
assertEquals(0, exitCode)
assertEquals(listOf("hello native"), stdout)
}
@Test
fun testShellCommand() = runBlocking {
val process = PosixProcessRunner.shell("echo 'shell native' && printf 'line2'")
val stdout = process.stdout.toList()
val exitCode = process.waitFor()
assertEquals(0, exitCode)
assertEquals(listOf("shell native", "line2"), stdout)
}
@Test
fun testStderrCapture() = runBlocking {
val process = PosixProcessRunner.shell("echo 'to stdout'; echo 'to stderr' >&2")
val stdout = process.stdout.toList()
val stderr = process.stderr.toList()
process.waitFor()
assertEquals(listOf("to stdout"), stdout)
assertEquals(listOf("to stderr"), stderr)
}
@Test
fun testPlatformDetails() {
val details = getPlatformDetails()
assertEquals("LINUX", details.name)
assertTrue(details.kernelVersion != null)
assertTrue(details.kernelVersion!!.isNotEmpty())
println("Linux Native Details: $details")
}
@Test
fun testLyngModuleNative() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.execute("echo", ["hello", "lyng", "native"])
var output = []
for (line in p.stdout()) {
output.add(line)
}
p.waitFor()
println(output)
assertEquals("hello lyng native", output.joinToString(" "))
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("hello lyng native"))
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.lyngio.process
import kotlinx.cinterop.*
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal actual fun getNativeKernelVersion(): String? {
return memScoped {
val u = alloc<utsname>()
if (uname(u.ptr) == 0) {
u.release.toKString()
} else null
}
}
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner

View File

@ -0,0 +1,152 @@
/*
* 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.lyngio.process
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal class NativeLyngProcess(
private val pid: pid_t,
private val stdoutFd: Int,
private val stderrFd: Int
) : LyngProcess {
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
override val stderr: Flow<String> = createPipeFlow(stderrFd)
override suspend fun sendSignal(signal: ProcessSignal) {
val sig = when (signal) {
ProcessSignal.SIGINT -> SIGINT
ProcessSignal.SIGTERM -> SIGTERM
ProcessSignal.SIGKILL -> SIGKILL
}
if (kill(pid, sig) != 0) {
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
memScoped {
val status = alloc<IntVar>()
if (waitpid(pid, status.ptr, 0) == -1) {
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
}
val s = status.value
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
}
}
override fun destroy() {
kill(pid, SIGKILL)
}
}
@OptIn(ExperimentalForeignApi::class)
private fun createPipeFlow(fd: Int): Flow<String> = flow {
val buffer = ByteArray(4096)
val lineBuffer = StringBuilder()
try {
while (true) {
val bytesRead = buffer.usePinned { pinned ->
read(fd, pinned.addressOf(0), buffer.size.toULong())
}
if (bytesRead <= 0L) break
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
lineBuffer.append(text)
var newlineIdx = lineBuffer.indexOf('\n')
while (newlineIdx != -1) {
val line = lineBuffer.substring(0, newlineIdx)
emit(line)
lineBuffer.deleteRange(0, newlineIdx + 1)
newlineIdx = lineBuffer.indexOf('\n')
}
}
if (lineBuffer.isNotEmpty()) {
emit(lineBuffer.toString())
}
} finally {
close(fd)
}
}.flowOn(Dispatchers.Default)
@OptIn(ExperimentalForeignApi::class)
object PosixProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
memScoped {
val pipeStdout = allocArray<IntVar>(2)
val pipeStderr = allocArray<IntVar>(2)
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
if (pipe(pipeStderr) != 0) {
close(pipeStdout[0])
close(pipeStdout[1])
throw RuntimeException("Failed to create stderr pipe")
}
val pid = fork()
if (pid == -1) {
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
}
if (pid == 0) {
// Child process
dup2(pipeStdout[1], 1)
dup2(pipeStderr[1], 2)
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
argv[0] = executable.cstr.ptr
for (i in args.indices) {
argv[i + 1] = args[i].cstr.ptr
}
argv[args.size + 1] = null
execvp(executable, argv)
// If we are here, exec failed
_exit(1)
}
// Parent process
close(pipeStdout[1])
close(pipeStderr[1])
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
}
}
override suspend fun shell(command: String): LyngProcess {
return execute("/bin/sh", listOf("-c", command))
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.lyngio.process
internal actual fun getNativeKernelVersion(): String? = null
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = WindowsProcessRunner
object WindowsProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
throw UnsupportedOperationException("Windows native process execution not implemented yet")
}
override suspend fun shell(command: String): LyngProcess {
return execute("cmd.exe", listOf("/c", command))
}
}

View File

@ -0,0 +1,40 @@
/*
* 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.lyngio.process
import kotlin.experimental.ExperimentalNativeApi
@OptIn(ExperimentalNativeApi::class)
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = Platform.osFamily.name,
version = "unknown",
arch = Platform.cpuArchitecture.name,
kernelVersion = getNativeKernelVersion()
)
}
internal expect fun getNativeKernelVersion(): String?
actual fun isProcessSupported(): Boolean = isNativeProcessSupported()
internal expect fun isNativeProcessSupported(): Boolean
actual fun getSystemProcessRunner(): LyngProcessRunner = getNativeProcessRunner()
internal expect fun getNativeProcessRunner(): LyngProcessRunner

View File

@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych"
version = "1.1.0-rc"
version = "1.1.1-SNAPSHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below

View File

@ -23,7 +23,7 @@ actual object ArgBuilderProvider {
private val tl = object : ThreadLocal<AndroidArgsBuilder>() {
override fun initialValue(): AndroidArgsBuilder = AndroidArgsBuilder()
}
actual fun acquire(): ArgsBuilder = tl.get()
actual fun acquire(): ArgsBuilder = tl.get()!!
}
private class AndroidArgsBuilder : ArgsBuilder {

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.
@ -138,7 +138,7 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
} else {
val value = if (hp < callArgs.size) callArgs[hp++]
else a.defaultValue?.execute(scope)
?: scope.raiseIllegalArgument("too few arguments for the call")
?: scope.raiseIllegalArgument("too few arguments for the call (missing ${a.name})")
assign(a, value)
}
i++

View File

@ -53,26 +53,71 @@ class ClosureScope(val callScope: Scope, val closureScope: Scope) :
super.objects[name]?.let { return it }
super.localBindings[name]?.let { return it }
// 2) Members on the captured receiver instance
(closureScope.thisObj as? net.sergeych.lyng.obj.ObjInstance)
?.instanceScope
?.objects
?.get(name)
?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
// 1a) Priority: if we are in a class context, prefer our own private members to support
// non-virtual private dispatch. This prevents a subclass from accidentally
// capturing a private member call from a base class.
// We only return non-field/non-property members (methods) here; fields must
// be resolved via instance storage in priority 2.
currentClassCtx?.let { ctx ->
ctx.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Field &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property) return rec
}
}
// 2) Members on the captured receiver instance
(closureScope.thisObj as? net.sergeych.lyng.obj.ObjInstance)?.let { inst ->
// Check direct locals in instance scope (unmangled)
inst.instanceScope.objects[name]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
}
// Check mangled names for fields along MRO
for (cls in inst.objClass.mro) {
inst.instanceScope.objects["${cls.className}::$name"]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) return rec
}
}
}
findExtension(closureScope.thisObj.objClass, name)?.let { return it }
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
// Return only non-field/non-property members (methods) from class-level records.
// Fields and properties must be resolved via instance storage (mangled) or readField.
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Field &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
!rec.isAbstract) return rec
}
}
// 3) Closure scope chain (locals/parents + members), ignore ClosureScope overrides to prevent recursion
closureScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
// 4) Caller `this` members
(callScope.thisObj as? net.sergeych.lyng.obj.ObjInstance)?.let { inst ->
// Check direct locals in instance scope (unmangled)
inst.instanceScope.objects[name]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
}
// Check mangled names for fields along MRO
for (cls in inst.objClass.mro) {
inst.instanceScope.objects["${cls.className}::$name"]?.let { rec ->
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) return rec
}
}
}
findExtension(callScope.thisObj.objClass, name)?.let { return it }
callScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
if (rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Field &&
rec.type != net.sergeych.lyng.obj.ObjRecord.Type.Property &&
!rec.isAbstract) return rec
}
}
// 5) Caller chain (locals/parents + members)

View File

@ -20,7 +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, val isExtern: Boolean = false): CodeContext() {
val pendingInitializations = mutableMapOf<String, Pos>()
}
}

File diff suppressed because it is too large Load Diff

View File

@ -34,18 +34,34 @@ class CompilerContext(val tokens: List<Token>) {
}
var currentIndex = 0
private var pendingGT = 0
fun hasNext() = currentIndex < tokens.size
fun hasNext() = currentIndex < tokens.size || pendingGT > 0
fun hasPrevious() = currentIndex > 0
fun next() =
if (currentIndex < tokens.size) tokens[currentIndex++]
fun next(): Token {
if (pendingGT > 0) {
pendingGT--
val last = tokens[currentIndex - 1]
return Token(">", last.pos.copy(column = last.pos.column + 1), Token.Type.GT)
}
return if (currentIndex < tokens.size) tokens[currentIndex++]
else Token("", tokens.last().pos, Token.Type.EOF)
}
fun previous() = if (!hasPrevious()) throw IllegalStateException("No previous token") else tokens[--currentIndex]
fun pushPendingGT() {
pendingGT++
}
fun savePos() = currentIndex
fun previous() = if (pendingGT > 0) {
pendingGT-- // This is wrong, previous should go back.
// But we don't really use previous() in generics parser after splitting.
throw IllegalStateException("previous() not supported after pushPendingGT")
} else if (!hasPrevious()) throw IllegalStateException("No previous token") else tokens[--currentIndex]
fun savePos() = (currentIndex shl 2) or (pendingGT and 3)
fun restorePos(pos: Int) {
currentIndex = pos
currentIndex = pos shr 2
pendingGT = pos and 3
}
fun ensureLabelIsValid(pos: Pos, label: String) {
@ -106,12 +122,13 @@ class CompilerContext(val tokens: List<Token>) {
errorMessage: String = "expected ${tokenType.name}",
isOptional: Boolean = false
): Boolean {
val pos = savePos()
val t = next()
return if (t.type != tokenType) {
if (!isOptional) {
throw ScriptError(t.pos, errorMessage)
} else {
previous()
restorePos(pos)
false
}
} else true
@ -122,20 +139,25 @@ class CompilerContext(val tokens: List<Token>) {
* @return true if token was found and skipped
*/
fun skipNextIf(vararg types: Token.Type): Boolean {
val pos = savePos()
val t = next()
return if (t.type in types)
true
else {
previous()
restorePos(pos)
false
}
}
@Suppress("unused")
fun skipTokens(vararg tokenTypes: Token.Type) {
while (next().type in tokenTypes) { /**/
while (hasNext()) {
val pos = savePos()
if (next().type !in tokenTypes) {
restorePos(pos)
break
}
}
previous()
}
fun nextNonWhitespace(): Token {
@ -151,10 +173,11 @@ class CompilerContext(val tokens: List<Token>) {
* @return next non-whitespace token without extracting it from tokens list
*/
fun peekNextNonWhitespace(): Token {
val saved = savePos()
while (true) {
val t = next()
if (t.type !in wstokens) {
previous()
restorePos(saved)
return t
}
}
@ -162,12 +185,13 @@ class CompilerContext(val tokens: List<Token>) {
inline fun ifNextIs(typeId: Token.Type, f: (Token) -> Unit): Boolean {
val pos = savePos()
val t = next()
return if (t.type == typeId) {
f(t)
true
} else {
previous()
restorePos(pos)
false
}
}

View File

@ -325,6 +325,7 @@ private class Parser(fromPos: Pos) {
'?' -> {
when (currentChar) {
'=' -> { pos.advance(); Token("?=", from, Token.Type.IFNULLASSIGN) }
':' -> { pos.advance(); Token("?:", from, Token.Type.ELVIS) }
'?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) }
'.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) }
@ -356,6 +357,8 @@ private class Parser(fromPos: Pos) {
when (text) {
"in" -> Token("in", from, Token.Type.IN)
"is" -> Token("is", from, Token.Type.IS)
"by" -> Token("by", from, Token.Type.BY)
"object" -> Token("object", from, Token.Type.OBJECT)
"as" -> {
// support both `as` and tight `as?` without spaces
if (currentChar == '?') { pos.advance(); Token("as?", from, Token.Type.ASNULL) }

View File

@ -0,0 +1,30 @@
/*
* 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
import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjVoid
/**
* Exception used to implement `return` statement.
* It carries the return value and an optional label for non-local returns.
*/
class ReturnException(
val result: Obj = ObjVoid,
val label: String? = null
) : RuntimeException()

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.
@ -155,7 +155,10 @@ open class Scope(
this.extensions[cls]?.get(name)?.let { return it }
}
return thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
if (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property || rec.isAbstract) null
else rec
} else null
}
}
@ -176,7 +179,11 @@ open class Scope(
s.extensions[cls]?.get(name)?.let { return it }
}
s.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
if (canAccessMember(rec.visibility, rec.declaringClass, caller)) {
if (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property || rec.isAbstract) {
// ignore fields, properties and abstracts here, they will be handled by the caller via readField
} else return rec
}
}
s = s.parent
}
@ -270,16 +277,22 @@ open class Scope(
raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name"))
fun raiseError(message: String): Nothing {
throw ExecutionError(ObjException(this, message))
val ex = ObjException(this, message)
throw ExecutionError(ex, pos, ex.message.value)
}
fun raiseError(obj: ObjException): Nothing {
throw ExecutionError(obj)
throw ExecutionError(obj, obj.scope.pos, obj.message.value)
}
fun raiseError(obj: Obj, pos: Pos, message: String): Nothing {
throw ExecutionError(obj, pos, message)
}
@Suppress("unused")
fun raiseNotFound(message: String = "not found"): Nothing {
throw ExecutionError(ObjNotFoundException(this, message))
val ex = ObjNotFoundException(this, message)
throw ExecutionError(ex, ex.scope.pos, ex.message.value)
}
inline fun <reified T : Obj> requiredArg(index: Int): T {
@ -307,11 +320,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}")
}
@ -332,7 +345,10 @@ open class Scope(
?: parent?.get(name)
// Finally, fallback to class members on thisObj
?: thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
if (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property || rec.isAbstract) null
else rec
} else null
}
)
}
@ -435,7 +451,10 @@ open class Scope(
value: Obj,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
recordType: ObjRecord.Type = ObjRecord.Type.Other
recordType: ObjRecord.Type = ObjRecord.Type.Other,
isAbstract: Boolean = false,
isClosed: Boolean = false,
isOverride: Boolean = false
): ObjRecord =
objects[name]?.let {
if( !it.isMutable )
@ -449,7 +468,7 @@ open class Scope(
callScope.localBindings[name] = it
}
it
} ?: addItem(name, true, value, visibility, writeVisibility, recordType)
} ?: addItem(name, true, value, visibility, writeVisibility, recordType, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride)
fun addItem(
name: String,
@ -458,9 +477,19 @@ open class Scope(
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
recordType: ObjRecord.Type = ObjRecord.Type.Other,
declaringClass: net.sergeych.lyng.obj.ObjClass? = currentClassCtx
declaringClass: net.sergeych.lyng.obj.ObjClass? = currentClassCtx,
isAbstract: Boolean = false,
isClosed: Boolean = false,
isOverride: Boolean = false
): ObjRecord {
val rec = ObjRecord(value, isMutable, visibility, writeVisibility, declaringClass = declaringClass, type = recordType)
val rec = ObjRecord(
value, isMutable, visibility, writeVisibility,
declaringClass = declaringClass,
type = recordType,
isAbstract = isAbstract,
isClosed = isClosed,
isOverride = isOverride
)
objects[name] = rec
// Index this binding within the current frame to help resolve locals across suspension
localBindings[name] = rec
@ -598,6 +627,51 @@ open class Scope(
return ref.evalValue(this)
}
suspend fun resolve(rec: ObjRecord, name: String): Obj {
if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: run {
if (thisObj is ObjInstance) {
val res = (thisObj as ObjInstance).resolveRecord(this, rec, name, rec.declaringClass).value
rec.value = res
return res
}
raiseError("Internal error: delegated property $name has no delegate")
}
val th = if (thisObj === ObjVoid) ObjNull else thisObj
val res = del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name)), onNotFoundResult = {
// If getValue not found, return a wrapper that calls invoke
object : Statement() {
override val pos: Pos = Pos.builtIn
override suspend fun execute(scope: Scope): Obj {
val th2 = if (scope.thisObj === ObjVoid) ObjNull else scope.thisObj
val allArgs = (listOf(th2, ObjString(name)) + scope.args.list).toTypedArray()
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
}
}
})
rec.value = res
return res
}
return rec.value
}
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: run {
if (thisObj is ObjInstance) {
(thisObj as ObjInstance).writeField(this, name, newValue)
return
}
raiseError("Internal error: delegated property $name has no delegate")
}
val th = if (thisObj === ObjVoid) ObjNull else thisObj
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
return
}
if (!rec.isMutable && rec.value !== ObjUnset) raiseIllegalAssignment("can't reassign val $name")
rec.value = newValue
}
companion object {
fun new(): Scope =

View File

@ -21,6 +21,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.yield
import net.sergeych.lyng.Script.Companion.defaultImportManager
import net.sergeych.lyng.miniast.addConstDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.addVoidFnDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lyng.obj.*
@ -204,18 +205,32 @@ class Script(
)
)
}
addFn("assertThrows") {
addFnDoc(
"assertThrows",
doc = """
Asserts that the provided code block throws an exception, with or without exception:
```lyng
assertThrows { /* ode */ }
assertThrows(IllegalArgumentException) { /* code */ }
```
If an expected exception class is provided,
it checks that the thrown exception is of that class. If no expected class is provided, any exception
will be accepted.
""".trimIndent()
) {
val code: Statement
val expectedClass: ObjClass?
when(args.size) {
when (args.size) {
1 -> {
code = requiredArg<Statement>(0)
expectedClass = null
}
2 -> {
code = requiredArg<Statement>(1)
expectedClass = requiredArg<ObjClass>(0)
}
else -> raiseIllegalArgument("Expected 1 or 2 arguments, got ${args.size}")
}
val result = try {
@ -226,12 +241,16 @@ class Script(
} catch (_: ScriptError) {
ObjNull
}
if( result == null ) raiseError(ObjAssertionFailedException(this, "Expected exception but nothing was thrown"))
if (result == null) raiseError(
ObjAssertionFailedException(
this,
"Expected exception but nothing was thrown"
)
)
expectedClass?.let {
if( result !is ObjException)
raiseError("Expected $expectedClass, got $result")
if (result.exceptionClass != expectedClass) {
raiseError("Expected $expectedClass, got ${result.exceptionClass}")
if (!result.isInstanceOf(it)) {
val actual = if (result is ObjException) result.exceptionClass else result.objClass
raiseError("Expected $it, got $actual")
}
}
result
@ -255,7 +274,7 @@ class Script(
val condition = requiredArg<ObjBool>(0)
if (!condition.value) {
var message = args.list.getOrNull(1)
if( message is Statement ) message = message.execute(this)
if (message is Statement) message = message.execute(this)
raiseIllegalArgument(message?.toString() ?: "requirement not met")
}
ObjVoid
@ -264,7 +283,7 @@ class Script(
val condition = requiredArg<ObjBool>(0)
if (!condition.value) {
var message = args.list.getOrNull(1)
if( message is Statement ) message = message.execute(this)
if (message is Statement) message = message.execute(this)
raiseIllegalState(message?.toString() ?: "check failed")
}
ObjVoid

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.
@ -19,7 +19,7 @@
package net.sergeych.lyng
import net.sergeych.lyng.obj.ObjException
import net.sergeych.lyng.obj.Obj
open class ScriptError(val pos: Pos, val errorMessage: String, cause: Throwable? = null) : Exception(
"""
@ -33,6 +33,6 @@ open class ScriptError(val pos: Pos, val errorMessage: String, cause: Throwable?
class ScriptFlowIsNoMoreCollected: Exception()
class ExecutionError(val errorObject: ObjException) : ScriptError(errorObject.scope.pos, errorObject.message.value)
class ExecutionError(val errorObject: Obj, pos: Pos, message: String) : ScriptError(pos, message)
class ImportException(pos: Pos, message: String) : ScriptError(pos, message)

View File

@ -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()

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.
@ -34,9 +34,9 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA,
SEMICOLON, COLON,
PLUS, MINUS, STAR, SLASH, PERCENT,
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN,
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN,
PLUS2, MINUS2,
IN, NOTIN, IS, NOTIS,
IN, NOTIN, IS, NOTIS, BY,
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
SHUTTLE,
AND, BITAND, OR, BITOR, BITXOR, NOT, BITNOT, DOT, ARROW, EQARROW, QUESTION, COLONCOLON,
@ -44,7 +44,7 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
SINLGE_LINE_COMMENT, MULTILINE_COMMENT,
LABEL, ATLABEL, // label@ at@label
// type-checking/casting
AS, ASNULL,
AS, ASNULL, OBJECT,
//PUBLIC, PROTECTED, INTERNAL, EXPORT, OPEN, INLINE, OVERRIDE, ABSTRACT, SEALED, EXTERNAL, VAL, VAR, CONST, TYPE, FUN, CLASS, INTERFACE, ENUM, OBJECT, TRAIT, THIS,
ELLIPSIS, DOTDOT, DOTDOTLT,
NEWLINE,

View File

@ -27,5 +27,6 @@ sealed class TypeDecl(val isNullable:Boolean = false) {
object TypeAny : TypeDecl()
object TypeNullableAny : TypeDecl(true)
class Simple(val name: String,isNullable: Boolean) : TypeDecl(isNullable)
class Simple(val name: String, isNullable: Boolean) : TypeDecl(isNullable)
class Generic(val name: String, val args: List<TypeDecl>, isNullable: Boolean) : TypeDecl(isNullable)
}

View File

@ -18,7 +18,7 @@
package net.sergeych.lyng
enum class Visibility {
Public, Private, Protected;//, Internal
Public, Protected, Private;//, Internal
val isPublic by lazy { this == Public }
@Suppress("unused")
val isProtected by lazy { this == Protected }

View File

@ -42,6 +42,31 @@ object LyngFormatter {
return false
}
private fun isAccessorRelated(code: String): Boolean {
val t = code.trim()
if (t.isEmpty()) return false
if (isPropertyAccessor(t)) return true
// If it contains 'fun' or 'fn' as a word, it's probably a function declaration, not an accessor
if (Regex("\\b(fun|fn)\\b").containsMatchIn(t)) return false
val hasDecl = startsWithWord(t, "var") || startsWithWord(t, "val") ||
startsWithWord(t, "private") || startsWithWord(t, "protected") ||
startsWithWord(t, "override") || startsWithWord(t, "public")
if (hasDecl) {
val getSetMatch = Regex("\\b(get|set)\\b").find(t)
if (getSetMatch != null) {
// Check it's not part of an assignment to the property itself (e.g. val x = get())
val equalIndex = t.indexOf('=')
if (equalIndex == -1 || equalIndex > getSetMatch.range.first) {
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
@ -82,7 +107,7 @@ object LyngFormatter {
if (isIf || isElseIf || isElse) return true
// property accessors ending with ) or =
if (isPropertyAccessor(t)) {
if (isAccessorRelated(t)) {
return if (t.contains('=')) t.endsWith('=') else t.endsWith(')')
}
return false
@ -168,7 +193,8 @@ object LyngFormatter {
}
val newBlockLevel = blockLevel
if (newBlockLevel > oldBlockLevel) {
val addedThisLine = (if (applyAwaiting) awaitingExtraIndent else 0) + (if (isAccessor) 1 else 0)
val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code))
val addedThisLine = (if (applyAwaiting) awaitingExtraIndent else 0) + (if (isAccessorRelatedLine) 1 else 0)
repeat(newBlockLevel - oldBlockLevel) {
extraIndents.add(addedThisLine)
}
@ -186,7 +212,8 @@ object LyngFormatter {
val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) {
// It's another header, increment
awaitingExtraIndent += if (isAccessor) 2 else 1
val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code))
awaitingExtraIndent += if (isAccessorRelatedLine) 2 else 1
} else {
// It's the body, reset
awaitingExtraIndent = 0
@ -195,7 +222,8 @@ object LyngFormatter {
// start awaiting if current line is a control header without '{'
val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) {
awaitingExtraIndent = if (isAccessor) 2 else 1
val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code))
awaitingExtraIndent = if (isAccessorRelatedLine) 2 else 1
}
}
@ -288,7 +316,20 @@ object LyngFormatter {
i++
}
// Normalize collected base indent: replace tabs with spaces
var baseIndent = if (onlyWs) base.toString().replace("\t", " ".repeat(config.indentSize)) else ""
var baseIndent = if (onlyWs) {
if (start == lineStart) {
// Range starts at line start, pick up this line's indentation as base
var k = start
val lineIndent = StringBuilder()
while (k < text.length && text[k] != '\n' && (text[k] == ' ' || text[k] == '\t')) {
lineIndent.append(text[k])
k++
}
lineIndent.toString().replace("\t", " ".repeat(config.indentSize))
} else {
base.toString().replace("\t", " ".repeat(config.indentSize))
}
} else ""
var parentBaseIndent: String? = baseIndent
if (baseIndent.isEmpty()) {
// Fallback: use the indent of the nearest previous non-empty line as base.
@ -464,7 +505,7 @@ private fun splitIntoParts(
private fun applyMinimalSpacingRules(code: String): String {
var s = code
// Ensure space before '(' for control-flow keywords
s = s.replace(Regex("\\b(if|for|while)\\("), "$1 (")
s = s.replace(Regex("\\b(if|for|while|return|break|continue)\\("), "$1 (")
// Space before '{' for control-flow headers only (avoid function declarations)
s = s.replace(Regex("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {")
@ -485,6 +526,8 @@ private fun applyMinimalSpacingRules(code: String): String {
s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {")
// Ensure space before '(' for catch parameter
s = s.replace(Regex("\\bcatch\\("), "catch (")
// Remove space between control keyword and label: return @label -> return@label
s = s.replace(Regex("\\b(return|break|continue)\\s+(@[\\p{L}_][\\p{L}\\p{N}_]*)"), "$1$2")
// Remove spaces just inside parentheses/brackets: "( a )" -> "(a)"
s = s.replace(Regex("\\(\\s+"), "(")
// Do not strip leading indentation before a closing bracket/paren on its own line

View File

@ -41,7 +41,8 @@ private val fallbackKeywordIds = setOf(
// boolean operators
"and", "or", "not",
// declarations & modifiers
"fun", "fn", "class", "enum", "val", "var", "import", "package",
"fun", "fn", "class", "interface", "enum", "val", "var", "import", "package",
"abstract", "closed", "override",
"private", "protected", "static", "open", "extern", "init", "get", "set", "by",
// control flow and misc
"if", "else", "when", "while", "do", "for", "try", "catch", "finally",
@ -73,7 +74,7 @@ private fun kindOf(type: Type, value: String): HighlightKind? = when (type) {
Type.COMMA, Type.SEMICOLON, Type.COLON -> HighlightKind.Punctuation
// textual control keywords
Type.IN, Type.NOTIN, Type.IS, Type.NOTIS, Type.AS, Type.ASNULL,
Type.IN, Type.NOTIN, Type.IS, Type.NOTIS, Type.AS, Type.ASNULL, Type.BY, Type.OBJECT,
Type.AND, Type.OR, Type.NOT -> HighlightKind.Keyword
// labels / annotations
@ -81,7 +82,7 @@ private fun kindOf(type: Type, value: String): HighlightKind? = when (type) {
// operators and symbolic constructs
Type.PLUS, Type.MINUS, Type.STAR, Type.SLASH, Type.PERCENT,
Type.ASSIGN, Type.PLUSASSIGN, Type.MINUSASSIGN, Type.STARASSIGN, Type.SLASHASSIGN, Type.PERCENTASSIGN,
Type.ASSIGN, Type.PLUSASSIGN, Type.MINUSASSIGN, Type.STARASSIGN, Type.SLASHASSIGN, Type.PERCENTASSIGN, Type.IFNULLASSIGN,
Type.PLUS2, Type.MINUS2,
Type.EQ, Type.NEQ, Type.LT, Type.LTE, Type.GT, Type.GTE, Type.REF_EQ, Type.REF_NEQ, Type.MATCH, Type.NOTMATCH,
Type.DOT, Type.ARROW, Type.EQARROW, Type.QUESTION, Type.COLONCOLON,

View File

@ -112,8 +112,8 @@ object BuiltinDocRegistry : BuiltinDocSource {
fun extensionMemberNamesFor(className: String): List<String> {
val src = try { rootLyng } catch (_: Throwable) { null } ?: return emptyList()
val out = LinkedHashSet<String>()
// Match lines like: fun String.trim(...) or val Int.isEven = ...
val re = Regex("^\\s*(?:fun|val|var)\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\b", RegexOption.MULTILINE)
// Match lines like: fun String.trim(...) or val Int.isEven = ... (allowing modifiers)
val re = Regex("^\\s*(?:(?:abstract|override|closed|private|protected|static|open|extern)\\s+)*(?:fun|val|var)\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\b", RegexOption.MULTILINE)
re.findAll(src).forEach { m ->
val name = m.groupValues.getOrNull(1)?.trim()
if (!name.isNullOrEmpty()) out.add(name)
@ -236,6 +236,7 @@ class ClassDocsBuilder internal constructor(private val className: String) {
name = name,
mutable = mutable,
type = type?.toMiniTypeRef(),
initRange = null,
doc = md,
nameStart = Pos.builtIn,
isStatic = isStatic,
@ -371,20 +372,20 @@ private object StdlibInlineDocIndex {
else -> {
// Non-comment, non-blank: try to match a declaration just after comments
if (buf.isNotEmpty()) {
// fun/val/var Class.name( ... )
val mExt = Regex("^(?:fun|val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
// fun/val/var Class.name( ... ) (allowing modifiers)
val mExt = Regex("^(?:(?:abstract|override|closed|private|protected|static|open|extern)\\s+)*(?:fun|val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
if (mExt != null) {
val (cls, name) = mExt.destructured
flushTo(Key.Method(cls, name))
} else {
// fun name( ... )
val mTop = Regex("^fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(").find(line)
// fun name( ... ) (allowing modifiers)
val mTop = Regex("^(?:(?:abstract|override|closed|private|protected|static|open|extern)\\s+)*fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(").find(line)
if (mTop != null) {
val (name) = mTop.destructured
flushTo(Key.TopFun(name))
} else {
// class Name
val mClass = Regex("^class\\s+([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
// class/interface Name (allowing modifiers)
val mClass = Regex("^(?:(?:abstract|private|protected|static|open|extern)\\s+)*(?:class|interface)\\s+([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
if (mClass != null) {
val (name) = mClass.destructured
flushTo(Key.Clazz(name))
@ -534,6 +535,9 @@ private fun buildStdlibDocs(): List<MiniDecl> {
)
// Concurrency helpers
mod.classDoc(name = "Deferred", doc = "Represents a value that will be available in the future.", bases = listOf(type("Obj"))) {
method(name = "await", doc = "Suspend until the value is available and return it.")
}
mod.funDoc(
name = "launch",
doc = StdlibInlineDocIndex.topFunDoc("launch") ?: "Launch an asynchronous task and return a `Deferred`.",
@ -551,8 +555,17 @@ private fun buildStdlibDocs(): List<MiniDecl> {
returns = type("lyng.Iterable")
)
// Common types
mod.classDoc(name = "Int", doc = "64-bit signed integer.", bases = listOf(type("Obj")))
mod.classDoc(name = "Real", doc = "64-bit floating point number.", bases = listOf(type("Obj")))
mod.classDoc(name = "Bool", doc = "Boolean value (true or false).", bases = listOf(type("Obj")))
mod.classDoc(name = "Char", doc = "Single character (UTF-16 code unit).", bases = listOf(type("Obj")))
mod.classDoc(name = "Buffer", doc = "Mutable byte array.", bases = listOf(type("Obj")))
mod.classDoc(name = "Regex", doc = "Regular expression.", bases = listOf(type("Obj")))
mod.classDoc(name = "Range", doc = "Arithmetic progression.", bases = listOf(type("Obj")))
// Common Iterable helpers (document top-level extension-like APIs as class members)
mod.classDoc(name = "Iterable", doc = StdlibInlineDocIndex.classDoc("Iterable") ?: "Helper operations for iterable collections.") {
mod.classDoc(name = "Iterable", doc = StdlibInlineDocIndex.classDoc("Iterable") ?: "Helper operations for iterable collections.", bases = listOf(type("Obj"))) {
fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Iterable", name) ?: fallback
method(name = "filter", doc = md("filter", "Filter elements by predicate."), params = listOf(ParamDoc("predicate")), returns = type("lyng.Iterable"))
method(name = "drop", doc = md("drop", "Skip the first N elements."), params = listOf(ParamDoc("n", type("lyng.Int"))), returns = type("lyng.Iterable"))
@ -591,7 +604,7 @@ private fun buildStdlibDocs(): List<MiniDecl> {
}
// Iterator helpers
mod.classDoc(name = "Iterator", doc = StdlibInlineDocIndex.classDoc("Iterator") ?: "Iterator protocol for sequential access.") {
mod.classDoc(name = "Iterator", doc = StdlibInlineDocIndex.classDoc("Iterator") ?: "Iterator protocol for sequential access.", bases = listOf(type("Obj"))) {
fun md(name: String, fallback: String) = StdlibInlineDocIndex.methodDoc("Iterator", name) ?: fallback
method(name = "hasNext", doc = md("hasNext", "Whether another element is available."), returns = type("lyng.Bool"))
method(name = "next", doc = md("next", "Return the next element."))
@ -600,22 +613,22 @@ private fun buildStdlibDocs(): List<MiniDecl> {
}
// Exceptions and utilities
mod.classDoc(name = "Exception", doc = StdlibInlineDocIndex.classDoc("Exception") ?: "Exception helpers.") {
mod.classDoc(name = "Exception", doc = StdlibInlineDocIndex.classDoc("Exception") ?: "Exception helpers.", bases = listOf(type("Obj"))) {
method(name = "printStackTrace", doc = StdlibInlineDocIndex.methodDoc("Exception", "printStackTrace") ?: "Print this exception and its stack trace to standard output.")
}
mod.classDoc(name = "Enum", doc = StdlibInlineDocIndex.classDoc("Enum") ?: "Base class for all enums.") {
mod.classDoc(name = "Enum", doc = StdlibInlineDocIndex.classDoc("Enum") ?: "Base class for all enums.", bases = listOf(type("Obj"))) {
method(name = "name", doc = "Returns the name of this enum constant.", returns = type("lyng.String"))
method(name = "ordinal", doc = "Returns the ordinal of this enum constant.", returns = type("lyng.Int"))
}
mod.classDoc(name = "String", doc = StdlibInlineDocIndex.classDoc("String") ?: "String helpers.") {
mod.classDoc(name = "String", doc = StdlibInlineDocIndex.classDoc("String") ?: "String helpers.", bases = listOf(type("Obj"))) {
// Only include inline-source method here; Kotlin-embedded methods are now documented via DocHelpers near definitions.
method(name = "re", doc = StdlibInlineDocIndex.methodDoc("String", "re") ?: "Compile this string into a regular expression.", returns = type("lyng.Regex"))
}
// StackTraceEntry structure
mod.classDoc(name = "StackTraceEntry", doc = StdlibInlineDocIndex.classDoc("StackTraceEntry") ?: "Represents a single stack trace element.") {
mod.classDoc(name = "StackTraceEntry", doc = StdlibInlineDocIndex.classDoc("StackTraceEntry") ?: "Represents a single stack trace element.", bases = listOf(type("Obj"))) {
// Fields are not present as declarations in root.lyng's class header docs. Keep seeded defaults.
field(name = "sourceName", doc = "Source (file) name.", type = type("lyng.String"))
field(name = "line", doc = "Line number (1-based).", type = type("lyng.Int"))

View File

@ -23,6 +23,8 @@ package net.sergeych.lyng.miniast
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.binding.BindingSnapshot
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.pacman.ImportProvider
/** Minimal completion item description (IDE-agnostic). */
@ -57,11 +59,11 @@ object CompletionEngineLight {
return completeSuspend(text, idx)
}
suspend fun completeSuspend(text: String, caret: Int): List<CompletionItem> {
suspend fun completeSuspend(text: String, caret: Int, providedMini: MiniScript? = null, binding: BindingSnapshot? = null): List<CompletionItem> {
// Ensure stdlib Obj*-defined docs (e.g., String methods) are initialized before registry lookup
StdlibDocsBootstrap.ensure()
val prefix = prefixAt(text, caret)
val mini = buildMiniAst(text)
val mini = providedMini ?: buildMiniAst(text)
val imported: List<String> = DocLookupUtils.canonicalImportedModules(mini ?: return emptyList(), text)
val cap = 200
@ -72,6 +74,10 @@ object CompletionEngineLight {
val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret)
if (memberDot != null) {
// 0) Try chained member call return type inference
DocLookupUtils.guessReturnClassFromMemberCallBeforeMini(mini, text, memberDot, imported, binding)?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini)
return out
}
DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDot, imported, mini)?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini)
return out
@ -87,7 +93,7 @@ object CompletionEngineLight {
return out
}
// 1) Receiver inference fallback
(DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDot, imported) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))?.let { cls ->
(DocLookupUtils.guessReceiverClassViaMini(mini, text, memberDot, imported, binding) ?: DocLookupUtils.guessReceiverClass(text, memberDot, imported, mini))?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini)
return out
}
@ -96,18 +102,25 @@ 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) }
offerParamsInScope(out, prefix, mini, text, caret)
val locals = DocLookupUtils.extractLocalsAt(text, caret)
for (name in locals) {
if (name.startsWith(prefix, true)) {
out.add(CompletionItem(name, Kind.Value))
}
}
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" }
val order = nonStd + std
@ -132,6 +145,74 @@ object CompletionEngineLight {
// --- Emission helpers ---
private fun offerParamsInScope(out: MutableList<CompletionItem>, prefix: String, mini: MiniScript, text: String, caret: Int) {
val src = mini.range.start.source
val already = mutableSetOf<String>()
fun add(ci: CompletionItem) {
if (ci.name.startsWith(prefix, true) && already.add(ci.name)) {
out.add(ci)
}
}
fun checkNode(node: Any) {
val range: MiniRange = when (node) {
is MiniDecl -> node.range
is MiniMemberDecl -> node.range
else -> return
}
val start = src.offsetOf(range.start)
val end = src.offsetOf(range.end).coerceAtMost(text.length)
if (caret in start..end) {
when (node) {
is MiniFunDecl -> {
for (p in node.params) {
add(CompletionItem(p.name, Kind.Value, typeText = typeOf(p.type)))
}
}
is MiniClassDecl -> {
// Propose constructor parameters (ctorFields)
for (p in node.ctorFields) {
add(CompletionItem(p.name, if (p.mutable) Kind.Value else Kind.Field, typeText = typeOf(p.type)))
}
// Propose class-level fields
for (p in node.classFields) {
add(CompletionItem(p.name, if (p.mutable) Kind.Value else Kind.Field, typeText = typeOf(p.type)))
}
// Process members (methods/fields)
for (m in node.members) {
// If the member itself contains the caret (like a method), recurse
checkNode(m)
// Also offer the member itself for the class scope
when (m) {
is MiniMemberFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
add(CompletionItem(m.name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType)))
}
is MiniMemberValDecl -> {
add(CompletionItem(m.name, if (m.mutable) Kind.Value else Kind.Field, typeText = typeOf(m.type)))
}
is MiniInitDecl -> {}
}
}
}
is MiniMemberFunDecl -> {
for (p in node.params) {
add(CompletionItem(p.name, Kind.Value, typeText = typeOf(p.type)))
}
}
else -> {}
}
}
}
for (decl in mini.declarations) {
checkNode(decl)
}
}
private fun offerDeclAdd(out: MutableList<CompletionItem>, prefix: String, d: MiniDecl) {
fun add(ci: CompletionItem) { if (ci.name.startsWith(prefix, true)) out += ci }
when (d) {
@ -192,7 +273,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 -> {}
@ -203,33 +284,38 @@ object CompletionEngineLight {
emitGroup(directMap)
emitGroup(inheritedMap)
// Supplement with stdlib extension members defined in root.lyng (e.g., fun String.re(...))
// Supplement with extension members (both stdlib and local)
run {
val already = (directMap.keys + inheritedMap.keys).toMutableSet()
val ext = BuiltinDocRegistry.extensionMemberNamesFor(className)
for (name in ext) {
val extensions = DocLookupUtils.collectExtensionMemberNames(imported, className, mini)
for (name in extensions) {
if (already.contains(name)) continue
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name, mini)
if (resolved != null) {
when (val member = resolved.second) {
val m = resolved.second
val ci = when (m) {
is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name }
val ci = CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(member.returnType))
if (ci.name.startsWith(prefix, true)) out += ci
already.add(name)
val params = m.params.joinToString(", ") { it.name }
CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType))
}
is MiniMemberValDecl -> {
val ci = CompletionItem(name, Kind.Field, typeText = typeOf(member.type))
if (ci.name.startsWith(prefix, true)) out += ci
already.add(name)
is MiniFunDecl -> {
val params = m.params.joinToString(", ") { it.name }
CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType))
}
is MiniInitDecl -> {}
is MiniMemberValDecl -> CompletionItem(name, Kind.Field, typeText = typeOf(m.type))
is MiniValDecl -> CompletionItem(name, Kind.Field, typeText = typeOf(m.type))
else -> CompletionItem(name, Kind.Method, tailText = "()", typeText = null)
}
if (ci.name.startsWith(prefix, true)) {
out += ci
already.add(name)
}
} else {
// Fallback: emit simple method name without detailed types
val ci = CompletionItem(name, Kind.Method, tailText = "()", typeText = null)
if (ci.name.startsWith(prefix, true)) out += ci
already.add(name)
if (ci.name.startsWith(prefix, true)) {
out += ci
already.add(name)
}
}
}
}

View File

@ -91,14 +91,15 @@ object DocLookupUtils {
return null
}
fun findTypeByRange(mini: MiniScript?, name: String, startOffset: Int): MiniTypeRef? {
fun findTypeByRange(mini: MiniScript?, name: String, startOffset: Int, text: String? = null, imported: List<String>? = null): MiniTypeRef? {
if (mini == null) return null
val src = mini.range.start.source
fun matches(p: net.sergeych.lyng.Pos, len: Int) = src.offsetOf(p).let { s -> startOffset >= s && startOffset < s + len }
for (d in mini.declarations) {
if (d.name == name && src.offsetOf(d.nameStart) == startOffset) {
if (d.name == name && matches(d.nameStart, d.name.length)) {
return when (d) {
is MiniValDecl -> d.type
is MiniValDecl -> d.type ?: if (text != null && imported != null) inferTypeRefForVal(d, text, imported, mini) else null
is MiniFunDecl -> d.returnType
else -> null
}
@ -106,25 +107,27 @@ object DocLookupUtils {
if (d is MiniFunDecl) {
for (p in d.params) {
if (p.name == name && src.offsetOf(p.nameStart) == startOffset) return p.type
if (p.name == name && matches(p.nameStart, p.name.length)) return p.type
}
}
if (d is MiniClassDecl) {
for (m in d.members) {
if (m.name == name && src.offsetOf(m.nameStart) == startOffset) {
if (m.name == name && matches(m.nameStart, m.name.length)) {
return when (m) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type
is MiniMemberValDecl -> m.type ?: if (text != null && imported != null) {
inferTypeRefFromInitRange(m.initRange, m.nameStart, text, imported, mini)
} else null
else -> null
}
}
}
for (cf in d.ctorFields) {
if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
if (cf.name == name && matches(cf.nameStart, cf.name.length)) return cf.type
}
for (cf in d.classFields) {
if (cf.name == name && src.offsetOf(cf.nameStart) == startOffset) return cf.type
if (cf.name == name && matches(cf.nameStart, cf.name.length)) return cf.type
}
}
}
@ -157,6 +160,21 @@ object DocLookupUtils {
return result.toList()
}
fun extractLocalsAt(text: String, offset: Int): Set<String> {
val res = mutableSetOf<String>()
// 1) find val/var declarations
val re = Regex("(?:^|[\\n;])\\s*(?:val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)")
re.findAll(text).forEach { m ->
if (m.range.first < offset) res.add(m.groupValues[1])
}
// 2) find implicit assignments
val re2 = Regex("(?:^|[\\n;])\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*=[^=]")
re2.findAll(text).forEach { m ->
if (m.range.first < offset) res.add(m.groupValues[1])
}
return res
}
fun extractImportsFromText(text: String): List<String> {
val result = LinkedHashSet<String>()
val re = Regex("^\\s*import\\s+([a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*)", RegexOption.MULTILINE)
@ -232,24 +250,65 @@ object DocLookupUtils {
for ((name, list) in buckets) {
result[name] = mergeClassDecls(name, list)
}
// Root object alias
if (result.containsKey("Obj") && !result.containsKey("Any")) {
result["Any"] = result["Obj"]!!
}
return result
}
fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
fun resolveMemberWithInheritance(importedModules: List<String>, className: String, member: String, localMini: MiniScript? = null): Pair<String, MiniNamedDecl>? {
val classes = aggregateClasses(importedModules, localMini)
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniMemberDecl>? {
val cls = classes[name] ?: return null
cls.members.firstOrNull { it.name == member }?.let { return name to it }
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniNamedDecl>? {
if (!visited.add(name)) return null
for (baseName in cls.bases) {
dfs(baseName, visited)?.let { return it }
val cls = classes[name]
if (cls != null) {
cls.members.firstOrNull { it.name == member }?.let { return name to it }
for (baseName in cls.bases) {
dfs(baseName, visited)?.let { return it }
}
}
// Check for local extensions in this class or bases
localMini?.declarations?.firstOrNull { d ->
(d is MiniFunDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name && d.name == member) ||
(d is MiniValDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name && d.name == member)
}?.let { return name to it }
return null
}
return dfs(className, mutableSetOf())
}
fun findMemberAcrossClasses(importedModules: List<String>, member: String, localMini: MiniScript? = null): Pair<String, MiniMemberDecl>? {
fun collectExtensionMemberNames(importedModules: List<String>, className: String, localMini: MiniScript? = null): Set<String> {
val classes = aggregateClasses(importedModules, localMini)
val visited = mutableSetOf<String>()
val result = mutableSetOf<String>()
fun dfs(name: String) {
if (!visited.add(name)) return
// 1) stdlib extensions from BuiltinDocRegistry
result.addAll(BuiltinDocRegistry.extensionMemberNamesFor(name))
// 2) local extensions from mini
localMini?.declarations?.forEach { d ->
if (d is MiniFunDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name) result.add(d.name)
if (d is MiniValDecl && d.receiver != null && simpleClassNameOf(d.receiver) == name) result.add(d.name)
}
// 3) bases
classes[name]?.bases?.forEach { dfs(it) }
}
dfs(className)
// Hardcoded supplements for common containers if not explicitly in bases
if (className == "List" || className == "Array") {
dfs("Collection")
dfs("Iterable")
}
dfs("Any")
dfs("Obj")
return result
}
fun findMemberAcrossClasses(importedModules: List<String>, member: String, localMini: MiniScript? = null): Pair<String, MiniNamedDecl>? {
val classes = aggregateClasses(importedModules, localMini)
// Preferred order for ambiguous common ops
val preference = listOf("Iterable", "Iterator", "List")
@ -301,6 +360,12 @@ object DocLookupUtils {
if (mini == null) return null
val i = prevNonWs(text, dotPos - 1)
if (i < 0) return null
// Handle indexing x[0]. or literal [1].
if (text[i] == ']') {
return guessReceiverClass(text, dotPos, imported, mini)
}
val wordRange = wordRangeAt(text, i + 1) ?: return null
val ident = text.substring(wordRange.first, wordRange.second)
@ -310,14 +375,14 @@ object DocLookupUtils {
if (ref != null) {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) {
val type = findTypeByRange(mini, sym.name, sym.declStart)
val type = findTypeByRange(mini, sym.name, sym.declStart, text, imported)
simpleClassNameOf(type)?.let { return it }
}
} else {
// Check if it's a declaration (e.g. static access to a class)
val sym = binding.symbols.firstOrNull { it.declStart == wordRange.first && it.name == ident }
if (sym != null) {
val type = findTypeByRange(mini, sym.name, sym.declStart)
val type = findTypeByRange(mini, sym.name, sym.declStart, text, imported)
simpleClassNameOf(type)?.let { return it }
// if it's a class/enum, return its name directly
if (sym.kind == net.sergeych.lyng.binding.SymbolKind.Class || sym.kind == net.sergeych.lyng.binding.SymbolKind.Enum) return sym.name
@ -325,13 +390,17 @@ object DocLookupUtils {
}
}
// 1) Global declarations in current file (val/var/fun/class/enum)
val d = mini.declarations.firstOrNull { it.name == ident }
// 1) Declarations in current file (val/var/fun/class/enum), prioritized by proximity
val src = mini.range.start.source
val d = mini.declarations
.filter { it.name == ident && src.offsetOf(it.nameStart) < dotPos }
.maxByOrNull { src.offsetOf(it.nameStart) }
if (d != null) {
return when (d) {
is MiniClassDecl -> d.name
is MiniEnumDecl -> d.name
is MiniValDecl -> simpleClassNameOf(d.type)
is MiniValDecl -> simpleClassNameOf(d.type ?: inferTypeRefForVal(d, text, imported, mini))
is MiniFunDecl -> simpleClassNameOf(d.returnType)
}
}
@ -343,6 +412,9 @@ object DocLookupUtils {
}
}
// 2a) Try to find plain assignment in text if not found in declarations: x = test()
inferTypeFromAssignmentInText(ident, text, imported, mini, beforeOffset = dotPos)?.let { return simpleClassNameOf(it) }
// 3) Recursive chaining: Base.ident.
val dotBefore = findDotLeft(text, wordRange.first)
if (dotBefore != null) {
@ -353,7 +425,7 @@ object DocLookupUtils {
if (resolved != null) {
val rt = when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type
is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, text, imported, mini)
else -> null
}
return simpleClassNameOf(rt)
@ -368,6 +440,43 @@ object DocLookupUtils {
return null
}
private fun inferTypeFromAssignmentInText(ident: String, text: String, imported: List<String>, mini: MiniScript?, beforeOffset: Int = Int.MAX_VALUE): MiniTypeRef? {
// Heuristic: search for "val ident =" or "ident =" in text
val re = Regex("(?:^|[\\n;])\\s*(?:val|var)?\\s*${ident}\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?\\s*(?:=|by)\\s*([^\\n;]+)")
val match = re.findAll(text)
.filter { it.range.first < beforeOffset }
.lastOrNull() ?: return null
val explicitType = match.groupValues.getOrNull(1)?.takeIf { it.isNotBlank() }
if (explicitType != null) return syntheticTypeRef(explicitType)
val expr = match.groupValues.getOrNull(2)?.let { stripComments(it) } ?: return null
return inferTypeRefFromExpression(expr, imported, mini, contextText = text, beforeOffset = beforeOffset)
}
private fun stripComments(text: String): String {
var result = ""
var i = 0
var inString = false
while (i < text.length) {
val ch = text[i]
if (ch == '"' && (i == 0 || text[i - 1] != '\\')) {
inString = !inString
}
if (!inString && ch == '/' && i + 1 < text.length) {
if (text[i + 1] == '/') break // single line comment
if (text[i + 1] == '*') {
// Skip block comment
i += 2
while (i + 1 < text.length && !(text[i] == '*' && text[i + 1] == '/')) i++
i += 2 // Skip '*/'
continue
}
}
result += ch
i++
}
return result.trim()
}
fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
if (mini == null) return null
var i = prevNonWs(text, dotPos - 1)
@ -420,7 +529,9 @@ object DocLookupUtils {
val rt = when (m) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type
is MiniInitDecl -> null
is MiniFunDecl -> m.returnType
is MiniValDecl -> m.type
else -> null
}
simpleClassNameOf(rt)
}
@ -430,21 +541,22 @@ object DocLookupUtils {
fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> {
val src = mini.range.start.source
if (cls.nameStart.source != src) return emptyMap()
val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start)
val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length)
if (start !in 0..end) return emptyMap()
val body = text.substring(start, end)
val map = LinkedHashMap<String, ScannedSig>()
// fun name(params): Type
val funRe = Regex("^\\s*fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(([^)]*)\\)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
// fun name(params): Type (allowing modifiers like abstract, override, closed)
val funRe = Regex("^\\s*(?:(?:abstract|override|closed|private|protected|static|open|extern)\\s+)*fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*\\(([^)]*)\\)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
for (m in funRe.findAll(body)) {
val name = m.groupValues.getOrNull(1) ?: continue
val params = m.groupValues.getOrNull(2)?.split(',')?.mapNotNull { it.trim().takeIf { it.isNotEmpty() } } ?: emptyList()
val type = m.groupValues.getOrNull(3)?.takeIf { it.isNotBlank() }
map[name] = ScannedSig("fun", params, type)
}
// val/var name: Type
val valRe = Regex("^\\s*(val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
// val/var name: Type (allowing modifiers)
val valRe = Regex("^\\s*(?:(?:abstract|override|closed|private|protected|static|open|extern)\\s+)*(val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\s*(?::\\s*([A-Za-z_][A-Za-z0-9_]*))?", RegexOption.MULTILINE)
for (m in valRe.findAll(body)) {
val kind = m.groupValues.getOrNull(1) ?: continue
val name = m.groupValues.getOrNull(2) ?: continue
@ -454,13 +566,25 @@ object DocLookupUtils {
return map
}
fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null): String? {
fun guessReceiverClass(text: String, dotPos: Int, imported: List<String>, mini: MiniScript? = null, beforeOffset: Int = dotPos): String? {
guessClassFromCallBefore(text, dotPos, imported, mini)?.let { return it }
var i = prevNonWs(text, dotPos - 1)
if (i >= 0) {
when (text[i]) {
'"' -> return "String"
']' -> return "List"
']' -> {
// Check if literal or indexing
val matchingOpen = findMatchingOpenBracket(text, i)
if (matchingOpen != null && matchingOpen > 0) {
val beforeOpen = prevNonWs(text, matchingOpen - 1)
if (beforeOpen >= 0 && (isIdentChar(text[beforeOpen]) || text[beforeOpen] == ')' || text[beforeOpen] == ']')) {
// Likely indexing: infer type of full expression
val exprText = text.substring(0, i + 1)
return simpleClassNameOf(inferTypeRefFromExpression(exprText, imported, mini, beforeOffset = beforeOffset))
}
}
return "List"
}
'}' -> return "Dict"
')' -> {
// Parenthesized expression: walk back to matching '(' and inspect the inner expression
@ -563,7 +687,9 @@ object DocLookupUtils {
val ret = when (member) {
is MiniMemberFunDecl -> member.returnType
is MiniMemberValDecl -> member.type
is MiniInitDecl -> null
is MiniFunDecl -> member.returnType
is MiniValDecl -> member.type
else -> null
}
return simpleClassNameOf(ret)
}
@ -626,11 +752,216 @@ object DocLookupUtils {
val ret = when (member) {
is MiniMemberFunDecl -> member.returnType
is MiniMemberValDecl -> member.type
is MiniInitDecl -> null
is MiniFunDecl -> member.returnType
is MiniValDecl -> member.type
else -> null
}
return simpleClassNameOf(ret)
}
fun inferTypeRefFromExpression(text: String, imported: List<String>, mini: MiniScript? = null, contextText: String? = null, beforeOffset: Int = Int.MAX_VALUE): MiniTypeRef? {
val trimmed = stripComments(text)
if (trimmed.isEmpty()) return null
val fullText = contextText ?: text
// 1) Literals
if (trimmed.startsWith("\"")) return syntheticTypeRef("String")
if (trimmed.startsWith("[")) return syntheticTypeRef("List")
if (trimmed.startsWith("{")) return syntheticTypeRef("Dict")
if (trimmed == "true" || trimmed == "false") return syntheticTypeRef("Boolean")
if (trimmed.all { it.isDigit() || it == '.' || it == '_' || it == 'e' || it == 'E' }) {
val hasDigits = trimmed.any { it.isDigit() }
if (hasDigits)
return if (trimmed.contains('.') || trimmed.contains('e', ignoreCase = true)) syntheticTypeRef("Real") else syntheticTypeRef("Int")
}
// 2) Function/Constructor calls or Indexing
if (trimmed.endsWith(")")) {
val openParen = findMatchingOpenParen(trimmed, trimmed.length - 1)
if (openParen != null && openParen > 0) {
var j = openParen - 1
while (j >= 0 && trimmed[j].isWhitespace()) j--
val end = j + 1
while (j >= 0 && isIdentChar(trimmed[j])) j--
val start = j + 1
if (start < end) {
val callee = trimmed.substring(start, end)
// Check if it's a member call (dot before callee)
var k = start - 1
while (k >= 0 && trimmed[k].isWhitespace()) k--
if (k >= 0 && trimmed[k] == '.') {
val prevDot = k
// Recursive: try to infer type of what's before the dot
val receiverText = trimmed.substring(0, prevDot)
val receiverType = inferTypeRefFromExpression(receiverText, imported, mini, contextText = fullText, beforeOffset = beforeOffset)
val receiverClass = simpleClassNameOf(receiverType)
if (receiverClass != null) {
val resolved = resolveMemberWithInheritance(imported, receiverClass, callee, mini)
if (resolved != null) {
return when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, fullText, imported, mini)
else -> null
}
}
}
} else {
// Top-level call or constructor
val classes = aggregateClasses(imported, mini)
if (classes.containsKey(callee)) return syntheticTypeRef(callee)
for (mod in imported) {
val decls = BuiltinDocRegistry.docsForModule(mod)
val fn = decls.asSequence().filterIsInstance<MiniFunDecl>().firstOrNull { it.name == callee }
if (fn != null) return fn.returnType
}
mini?.declarations?.filterIsInstance<MiniFunDecl>()?.firstOrNull { it.name == callee }?.let { return it.returnType }
}
}
}
}
if (trimmed.endsWith("]")) {
val openBracket = findMatchingOpenBracket(trimmed, trimmed.length - 1)
if (openBracket != null && openBracket > 0) {
val receiverText = trimmed.substring(0, openBracket).trim()
if (receiverText.isNotEmpty()) {
val receiverType = inferTypeRefFromExpression(receiverText, imported, mini, contextText = fullText, beforeOffset = beforeOffset)
if (receiverType is MiniGenericType) {
val baseName = simpleClassNameOf(receiverType.base)
if (baseName == "List" && receiverType.args.isNotEmpty()) {
return receiverType.args[0]
}
if (baseName == "Map" && receiverType.args.size >= 2) {
return receiverType.args[1]
}
}
// Fallback for non-generic collections or if base name matches
val baseName = simpleClassNameOf(receiverType)
if (baseName == "List" || baseName == "Array" || baseName == "String") {
if (baseName == "String") return syntheticTypeRef("Char")
return syntheticTypeRef("Any")
}
}
}
}
// 3) Member field or simple identifier at the end
val lastWord = wordRangeAt(trimmed, trimmed.length)
if (lastWord != null && lastWord.second == trimmed.length) {
val ident = trimmed.substring(lastWord.first, lastWord.second)
var k = lastWord.first - 1
while (k >= 0 && trimmed[k].isWhitespace()) k--
if (k >= 0 && trimmed[k] == '.') {
// Member field: receiver.ident
val receiverText = trimmed.substring(0, k).trim()
val receiverType = inferTypeRefFromExpression(receiverText, imported, mini, contextText = fullText, beforeOffset = beforeOffset)
val receiverClass = simpleClassNameOf(receiverType)
if (receiverClass != null) {
val resolved = resolveMemberWithInheritance(imported, receiverClass, ident, mini)
if (resolved != null) {
return when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, fullText, imported, mini)
else -> null
}
}
}
} else {
// Simple identifier
// 1) Declarations in current file (val/var/fun/class/enum), prioritized by proximity
val src = mini?.range?.start?.source
val d = if (src != null) {
mini.declarations
.filter { it.name == ident && src.offsetOf(it.nameStart) < beforeOffset }
.maxByOrNull { src.offsetOf(it.nameStart) }
} else {
mini?.declarations?.firstOrNull { it.name == ident }
}
if (d != null) {
return when (d) {
is MiniClassDecl -> syntheticTypeRef(d.name)
is MiniEnumDecl -> syntheticTypeRef(d.name)
is MiniValDecl -> d.type ?: inferTypeRefForVal(d, fullText, imported, mini)
is MiniFunDecl -> d.returnType
}
}
// 2) Parameters in any function
for (fd in mini?.declarations?.filterIsInstance<MiniFunDecl>() ?: emptyList()) {
for (p in fd.params) {
if (p.name == ident) return p.type
}
}
// 3) Try to find plain assignment in text: ident = expr
inferTypeFromAssignmentInText(ident, fullText, imported, mini, beforeOffset = beforeOffset)?.let { return it }
// 4) Check if it's a known class (static access)
val classes = aggregateClasses(imported, mini)
if (classes.containsKey(ident)) return syntheticTypeRef(ident)
}
}
return null
}
private fun findMatchingOpenBracket(text: String, closeBracketPos: Int): Int? {
if (closeBracketPos < 0 || closeBracketPos >= text.length || text[closeBracketPos] != ']') return null
var depth = 0
var i = closeBracketPos - 1
while (i >= 0) {
when (text[i]) {
']' -> depth++
'[' -> if (depth == 0) return i else depth--
}
i--
}
return null
}
private fun findMatchingOpenParen(text: String, closeParenPos: Int): Int? {
if (closeParenPos < 0 || closeParenPos >= text.length || text[closeParenPos] != ')') return null
var depth = 0
var i = closeParenPos - 1
while (i >= 0) {
when (text[i]) {
')' -> depth++
'(' -> if (depth == 0) return i else depth--
}
i--
}
return null
}
private fun syntheticTypeRef(name: String): MiniTypeRef =
MiniTypeName(MiniRange(net.sergeych.lyng.Pos.builtIn, net.sergeych.lyng.Pos.builtIn),
listOf(MiniTypeName.Segment(name, MiniRange(net.sergeych.lyng.Pos.builtIn, net.sergeych.lyng.Pos.builtIn))), false)
fun inferTypeRefForVal(vd: MiniValDecl, text: String, imported: List<String>, mini: MiniScript?): MiniTypeRef? {
return inferTypeRefFromInitRange(vd.initRange, vd.nameStart, text, imported, mini)
}
fun inferTypeRefFromInitRange(initRange: MiniRange?, nameStart: net.sergeych.lyng.Pos, text: String, imported: List<String>, mini: MiniScript?): MiniTypeRef? {
val range = initRange ?: return null
val src = mini?.range?.start?.source ?: return null
val start = src.offsetOf(range.start)
val end = src.offsetOf(range.end)
if (start < 0 || start >= end || end > text.length) return null
var exprText = text.substring(start, end).trim()
if (exprText.startsWith("=")) {
exprText = exprText.substring(1).trim()
}
if (exprText.startsWith("by")) {
exprText = exprText.substring(2).trim()
}
val beforeOffset = src.offsetOf(nameStart)
return inferTypeRefFromExpression(exprText, imported, mini, contextText = text, beforeOffset = beforeOffset)
}
fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
null -> null
is MiniTypeName -> t.segments.lastOrNull()?.name
@ -666,13 +997,13 @@ object DocLookupUtils {
fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl {
val staticMembers = mutableListOf<MiniMemberDecl>()
// entries: List
staticMembers.add(MiniMemberValDecl(en.range, "entries", false, null, null, en.nameStart, isStatic = true))
staticMembers.add(MiniMemberValDecl(en.range, "entries", false, null, null, null, en.nameStart, isStatic = true))
// valueOf(name: String): Enum
staticMembers.add(MiniMemberFunDecl(en.range, "valueOf", listOf(MiniParam("name", null, en.nameStart)), null, null, en.nameStart, isStatic = true))
// Also add each entry as a static member (const)
for (entry in en.entries) {
staticMembers.add(MiniMemberValDecl(en.range, entry, false, MiniTypeName(en.range, listOf(MiniTypeName.Segment(en.name, en.range)), false), null, en.nameStart, isStatic = true))
staticMembers.add(MiniMemberValDecl(en.range, entry, false, MiniTypeName(en.range, listOf(MiniTypeName.Segment(en.name, en.range)), false), null, null, en.nameStart, isStatic = true))
}
return MiniClassDecl(

View File

@ -81,13 +81,16 @@ data class MiniTypeVar(
) : MiniTypeRef
// Script and declarations (lean subset; can be extended later)
sealed interface MiniDecl : MiniNode {
sealed interface MiniNamedDecl : MiniNode {
val name: String
val doc: MiniDoc?
// Start position of the declaration name identifier in source; end can be derived as start + name.length
val nameStart: Pos
val isExtern: Boolean
}
sealed interface MiniDecl : MiniNamedDecl
data class MiniScript(
override val range: MiniRange,
val declarations: MutableList<MiniDecl> = mutableListOf(),
@ -110,7 +113,8 @@ data class MiniFunDecl(
val body: MiniBlock?,
override val doc: MiniDoc?,
override val nameStart: Pos,
val receiver: MiniTypeRef? = null
val receiver: MiniTypeRef? = null,
override val isExtern: Boolean = false
) : MiniDecl
data class MiniValDecl(
@ -121,7 +125,8 @@ data class MiniValDecl(
val initRange: MiniRange?,
override val doc: MiniDoc?,
override val nameStart: Pos,
val receiver: MiniTypeRef? = null
val receiver: MiniTypeRef? = null,
override val isExtern: Boolean = false
) : MiniDecl
data class MiniClassDecl(
@ -134,7 +139,9 @@ data class MiniClassDecl(
override val doc: MiniDoc?,
override val nameStart: Pos,
// Built-in extension: list of member declarations (functions and fields)
val members: List<MiniMemberDecl> = emptyList()
val members: List<MiniMemberDecl> = emptyList(),
override val isExtern: Boolean = false,
val isObject: Boolean = false
) : MiniDecl
data class MiniEnumDecl(
@ -142,7 +149,8 @@ data class MiniEnumDecl(
override val name: String,
val entries: List<String>,
override val doc: MiniDoc?,
override val nameStart: Pos
override val nameStart: Pos,
override val isExtern: Boolean = false
) : MiniDecl
data class MiniCtorField(
@ -166,10 +174,7 @@ data class MiniIdentifier(
) : MiniNode
// --- Class member declarations (for built-in/registry docs) ---
sealed interface MiniMemberDecl : MiniNode {
val name: String
val doc: MiniDoc?
val nameStart: Pos
sealed interface MiniMemberDecl : MiniNamedDecl {
val isStatic: Boolean
}
@ -181,6 +186,8 @@ data class MiniMemberFunDecl(
override val doc: MiniDoc?,
override val nameStart: Pos,
override val isStatic: Boolean = false,
override val isExtern: Boolean = false,
val body: MiniBlock? = null
) : MiniMemberDecl
data class MiniMemberValDecl(
@ -188,9 +195,11 @@ data class MiniMemberValDecl(
override val name: String,
val mutable: Boolean,
val type: MiniTypeRef?,
val initRange: MiniRange?,
override val doc: MiniDoc?,
override val nameStart: Pos,
override val isStatic: Boolean = false,
override val isExtern: Boolean = false,
) : MiniMemberDecl
data class MiniInitDecl(
@ -200,6 +209,7 @@ data class MiniInitDecl(
override val name: String get() = "init"
override val doc: MiniDoc? get() = null
override val isStatic: Boolean get() = false
override val isExtern: Boolean get() = false
}
// Streaming sink to collect mini-AST during parsing. Implementations may assemble a tree or process events.
@ -209,6 +219,12 @@ interface MiniAstSink {
fun onDocCandidate(doc: MiniDoc) {}
fun onEnterClass(node: MiniClassDecl) {}
fun onExitClass(end: Pos) {}
fun onEnterFunction(node: MiniFunDecl?) {}
fun onExitFunction(end: Pos) {}
fun onImport(node: MiniImport) {}
fun onFunDecl(node: MiniFunDecl) {}
fun onValDecl(node: MiniValDecl) {}
@ -238,8 +254,10 @@ interface MiniTypeTrace {
class MiniAstBuilder : MiniAstSink {
private var currentScript: MiniScript? = null
private val blocks = ArrayDeque<MiniBlock>()
private val classStack = ArrayDeque<MiniClassDecl>()
private var lastDoc: MiniDoc? = null
private var scriptDepth: Int = 0
private var functionDepth: Int = 0
fun build(): MiniScript? = currentScript
@ -262,26 +280,119 @@ class MiniAstBuilder : MiniAstSink {
lastDoc = doc
}
override fun onEnterClass(node: MiniClassDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc)
classStack.addLast(attach)
lastDoc = null
}
override fun onExitClass(end: Pos) {
val finished = classStack.removeLastOrNull()
if (finished != null) {
val updated = finished.copy(range = MiniRange(finished.range.start, end))
// Always add to top-level for now to ensure visibility in light engine
currentScript?.declarations?.add(updated)
}
}
override fun onEnterFunction(node: MiniFunDecl?) {
functionDepth++
}
override fun onExitFunction(end: Pos) {
functionDepth--
}
override fun onImport(node: MiniImport) {
currentScript?.imports?.add(node)
}
override fun onFunDecl(node: MiniFunDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc)
currentScript?.declarations?.add(attach)
val currentClass = classStack.lastOrNull()
if (currentClass != null && functionDepth == 0) {
// Convert MiniFunDecl to MiniMemberFunDecl for inclusion in members
val member = MiniMemberFunDecl(
range = attach.range,
name = attach.name,
params = attach.params,
returnType = attach.returnType,
doc = attach.doc,
nameStart = attach.nameStart,
isStatic = false, // TODO: track static if needed
isExtern = attach.isExtern,
body = attach.body
)
// Need to update the class in the stack since it's immutable-ish (data class)
// Check if we already have this member (from a previous onFunDecl call for the same function)
val existing = currentClass.members.filterIsInstance<MiniMemberFunDecl>().find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val members = currentClass.members.map { if (it === existing) member else it }
classStack.removeLast()
classStack.addLast(currentClass.copy(members = members))
} else {
classStack.removeLast()
classStack.addLast(currentClass.copy(members = currentClass.members + member))
}
} else {
// Check if already in declarations to avoid duplication
val existing = currentScript?.declarations?.find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val idx = currentScript?.declarations?.indexOf(existing) ?: -1
if (idx >= 0) currentScript?.declarations?.set(idx, attach)
} else {
currentScript?.declarations?.add(attach)
}
}
lastDoc = null
}
override fun onValDecl(node: MiniValDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc)
currentScript?.declarations?.add(attach)
val currentClass = classStack.lastOrNull()
if (currentClass != null && functionDepth == 0) {
val member = MiniMemberValDecl(
range = attach.range,
name = attach.name,
mutable = attach.mutable,
type = attach.type,
initRange = attach.initRange,
doc = attach.doc,
nameStart = attach.nameStart,
isStatic = false, // TODO: track static if needed
isExtern = attach.isExtern
)
// Duplicates for vals are rare but possible if Compiler calls it twice
val existing = currentClass.members.filterIsInstance<MiniMemberValDecl>().find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val members = currentClass.members.map { if (it === existing) member else it }
classStack.removeLast()
classStack.addLast(currentClass.copy(members = members))
} else {
classStack.removeLast()
classStack.addLast(currentClass.copy(members = currentClass.members + member))
}
} else {
val existing = currentScript?.declarations?.find { it.name == attach.name && it.nameStart == attach.nameStart }
if (existing != null) {
val idx = currentScript?.declarations?.indexOf(existing) ?: -1
if (idx >= 0) currentScript?.declarations?.set(idx, attach)
} else {
currentScript?.declarations?.add(attach)
}
}
lastDoc = null
}
override fun onClassDecl(node: MiniClassDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc)
currentScript?.declarations?.add(attach)
lastDoc = null
// This is the old way, we might want to deprecate it or make it call onEnterClass
// For now, if we are NOT using enter/exit, keep behavior.
// But Compiler.kt will be updated to use enter/exit.
if (classStack.isEmpty()) {
val attach = node.copy(doc = node.doc ?: lastDoc)
currentScript?.declarations?.add(attach)
lastDoc = null
}
}
override fun onEnumDecl(node: MiniEnumDecl) {

View File

@ -4,8 +4,6 @@
*/
package net.sergeych.lyng.miniast
import net.sergeych.lyng.obj.ObjString
object StdlibDocsBootstrap {
// Simple idempotent guard; races are harmless as initializer side-effects are idempotent
private var ensured = false
@ -16,7 +14,25 @@ object StdlibDocsBootstrap {
// Touch core Obj* types whose docs are registered via addFnDoc/addConstDoc
// Accessing .type forces their static initializers to run and register docs.
@Suppress("UNUSED_VARIABLE")
val _string = ObjString.type
val _string = net.sergeych.lyng.obj.ObjString.type
@Suppress("UNUSED_VARIABLE")
val _any = net.sergeych.lyng.obj.Obj.rootObjectType
@Suppress("UNUSED_VARIABLE")
val _list = net.sergeych.lyng.obj.ObjList.type
@Suppress("UNUSED_VARIABLE")
val _map = net.sergeych.lyng.obj.ObjMap.type
@Suppress("UNUSED_VARIABLE")
val _int = net.sergeych.lyng.obj.ObjInt.type
@Suppress("UNUSED_VARIABLE")
val _real = net.sergeych.lyng.obj.ObjReal.type
@Suppress("UNUSED_VARIABLE")
val _bool = net.sergeych.lyng.obj.ObjBool.type
@Suppress("UNUSED_VARIABLE")
val _regex = net.sergeych.lyng.obj.ObjRegex.type
@Suppress("UNUSED_VARIABLE")
val _range = net.sergeych.lyng.obj.ObjRange.type
@Suppress("UNUSED_VARIABLE")
val _buffer = net.sergeych.lyng.obj.ObjBuffer.type
} catch (_: Throwable) {
// Best-effort; absence should not break consumers
} finally {

View File

@ -25,6 +25,9 @@ import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.serializer
import net.sergeych.lyng.*
import net.sergeych.lyng.miniast.ParamDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonType
@ -61,7 +64,12 @@ open class Obj {
@Suppress("SuspiciousEqualsCombination")
fun isInstanceOf(someClass: Obj) = someClass === objClass ||
objClass.allParentsSet.contains(someClass) ||
someClass == rootObjectType
someClass == rootObjectType ||
(someClass is ObjClass && objClass.allImplementingNames.contains(someClass.className))
fun isInstanceOf(className: String) =
objClass.mro.any { it.className == className } ||
objClass.allImplementingNames.contains(className)
suspend fun invokeInstanceMethod(scope: Scope, name: String, vararg args: Obj): Obj =
@ -89,18 +97,12 @@ open class Obj {
for (cls in objClass.mro) {
if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null) {
if (rec != null && !rec.isAbstract && rec.type != ObjRecord.Type.Property) {
val decl = rec.declaringClass ?: cls
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
val saved = scope.currentClassCtx
scope.currentClassCtx = decl
try {
return rec.value.invoke(scope, this, args)
} finally {
scope.currentClassCtx = saved
}
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
return rec.value.invoke(scope, this, args, decl)
}
}
@ -117,14 +119,8 @@ open class Obj {
val decl = rec.declaringClass ?: cls
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
val saved = scope.currentClassCtx
scope.currentClassCtx = decl
try {
return rec.value.invoke(scope, this, args)
} finally {
scope.currentClassCtx = saved
}
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
return rec.value.invoke(scope, this, args, decl)
}
}
}
@ -149,9 +145,24 @@ open class Obj {
// methods that to override
open suspend fun compareTo(scope: Scope, other: Obj): Int {
if( other === this) return 0
if( other === ObjNull ) return 2
scope.raiseNotImplemented()
if (other === this) return 0
if (other === ObjNull || other === ObjUnset || other === ObjVoid) return 2
return invokeInstanceMethod(scope, "compareTo", Arguments(other)) {
scope.raiseNotImplemented("compareTo for ${objClass.className}")
}.cast<ObjInt>(scope).toInt()
}
open suspend fun equals(scope: Scope, other: Obj): Boolean {
if (other === this) return true
val m = objClass.getInstanceMemberOrNull("equals") ?: scope.findExtension(objClass, "equals")
if (m != null) {
return invokeInstanceMethod(scope, "equals", Arguments(other)).toBool()
}
return try {
compareTo(scope, other) == 0
} catch (e: ExecutionError) {
false
}
}
open suspend fun contains(scope: Scope, other: Obj): Boolean {
@ -224,46 +235,66 @@ open class Obj {
* Class of the object: definition of member functions (top-level), etc.
* Note that using lazy allows to avoid endless recursion here
*/
open val objClass: ObjClass = rootObjectType
open val objClass: ObjClass get() = rootObjectType
open suspend fun plus(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "plus", Arguments(other)) {
scope.raiseNotImplemented("plus for ${objClass.className}")
}
}
open suspend fun minus(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "minus", Arguments(other)) {
scope.raiseNotImplemented("minus for ${objClass.className}")
}
}
open suspend fun negate(scope: Scope): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "negate", Arguments.EMPTY) {
scope.raiseNotImplemented("negate for ${objClass.className}")
}
}
open suspend fun mul(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "mul", Arguments(other)) {
scope.raiseNotImplemented("mul for ${objClass.className}")
}
}
open suspend fun div(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "div", Arguments(other)) {
scope.raiseNotImplemented("div for ${objClass.className}")
}
}
open suspend fun mod(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "mod", Arguments(other)) {
scope.raiseNotImplemented("mod for ${objClass.className}")
}
}
open suspend fun logicalNot(scope: Scope): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "logicalNot", Arguments.EMPTY) {
scope.raiseNotImplemented("logicalNot for ${objClass.className}")
}
}
open suspend fun logicalAnd(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "logicalAnd", Arguments(other)) {
scope.raiseNotImplemented("logicalAnd for ${objClass.className}")
}
}
open suspend fun logicalOr(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "logicalOr", Arguments(other)) {
scope.raiseNotImplemented("logicalOr for ${objClass.className}")
}
}
open suspend fun operatorMatch(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "operatorMatch", Arguments(other)) {
scope.raiseNotImplemented("operatorMatch for ${objClass.className}")
}
}
open suspend fun operatorNotMatch(scope: Scope, other: Obj): Obj {
@ -272,27 +303,39 @@ open class Obj {
// Bitwise ops default (override in numeric types that support them)
open suspend fun bitAnd(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "bitAnd", Arguments(other)) {
scope.raiseNotImplemented("bitAnd for ${objClass.className}")
}
}
open suspend fun bitOr(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "bitOr", Arguments(other)) {
scope.raiseNotImplemented("bitOr for ${objClass.className}")
}
}
open suspend fun bitXor(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "bitXor", Arguments(other)) {
scope.raiseNotImplemented("bitXor for ${objClass.className}")
}
}
open suspend fun shl(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "shl", Arguments(other)) {
scope.raiseNotImplemented("shl for ${objClass.className}")
}
}
open suspend fun shr(scope: Scope, other: Obj): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "shr", Arguments(other)) {
scope.raiseNotImplemented("shr for ${objClass.className}")
}
}
open suspend fun bitNot(scope: Scope): Obj {
scope.raiseNotImplemented()
return invokeInstanceMethod(scope, "bitNot", Arguments.EMPTY) {
scope.raiseNotImplemented("bitNot for ${objClass.className}")
}
}
open suspend fun assign(scope: Scope, other: Obj): Obj? = null
@ -304,15 +347,43 @@ open class Obj {
* if( the operation is not defined, it returns null and the compiler would try
* to generate it as 'this = this + other', reassigning its variable
*/
open suspend fun plusAssign(scope: Scope, other: Obj): Obj? = null
open suspend fun plusAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("plusAssign") ?: scope.findExtension(objClass, "plusAssign")
return if (m != null) {
invokeInstanceMethod(scope, "plusAssign", Arguments(other))
} else null
}
/**
* `-=` operations, see [plusAssign]
*/
open suspend fun minusAssign(scope: Scope, other: Obj): Obj? = null
open suspend fun mulAssign(scope: Scope, other: Obj): Obj? = null
open suspend fun divAssign(scope: Scope, other: Obj): Obj? = null
open suspend fun modAssign(scope: Scope, other: Obj): Obj? = null
open suspend fun minusAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("minusAssign") ?: scope.findExtension(objClass, "minusAssign")
return if (m != null) {
invokeInstanceMethod(scope, "minusAssign", Arguments(other))
} else null
}
open suspend fun mulAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("mulAssign") ?: scope.findExtension(objClass, "mulAssign")
return if (m != null) {
invokeInstanceMethod(scope, "mulAssign", Arguments(other))
} else null
}
open suspend fun divAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("divAssign") ?: scope.findExtension(objClass, "divAssign")
return if (m != null) {
invokeInstanceMethod(scope, "divAssign", Arguments(other))
} else null
}
open suspend fun modAssign(scope: Scope, other: Obj): Obj? {
val m = objClass.getInstanceMemberOrNull("modAssign") ?: scope.findExtension(objClass, "modAssign")
return if (m != null) {
invokeInstanceMethod(scope, "modAssign", Arguments(other))
} else null
}
open suspend fun getAndIncrement(scope: Scope): Obj {
scope.raiseNotImplemented()
@ -347,8 +418,12 @@ open class Obj {
// 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) {
if (cls.className == "Obj") break
cls.members[name]?.let { return resolveRecord(scope, it, name, it.declaringClass) }
cls.classScope?.objects?.get(name)?.let { return resolveRecord(scope, it, name, it.declaringClass) }
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null) {
if (!rec.isAbstract) {
return resolveRecord(scope, rec, name, rec.declaringClass)
}
}
}
// 2. Extensions
@ -372,7 +447,14 @@ open class Obj {
)
}
protected suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
open suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
if (obj.type == ObjRecord.Type.Delegated) {
val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
return obj.copy(
value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))),
type = ObjRecord.Type.Other
)
}
val value = obj.value
if (value is ObjProperty) {
return ObjRecord(value.callGetter(scope, this, decl), obj.isMutable)
@ -383,7 +465,7 @@ open class Obj {
val caller = scope.currentClassCtx
// Check visibility for non-property members here if they weren't checked before
if (!canAccessMember(obj.visibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't access field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
scope.raiseError(ObjIllegalAccessException(scope, "can't access field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
return obj
}
@ -393,8 +475,11 @@ open class Obj {
// 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) {
if (cls.className == "Obj") break
field = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (field != null) break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null && !rec.isAbstract) {
field = rec
break
}
}
// 2. Extensions
if (field == null) {
@ -417,8 +502,11 @@ open class Obj {
val decl = field.declaringClass
val caller = scope.currentClassCtx
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) {
scope.raiseError(ObjIllegalAccessException(scope, "can't assign field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
if (field.type == ObjRecord.Type.Delegated) {
val del = field.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
del.invokeInstanceMethod(scope, "setValue", Arguments(this, ObjString(name), newValue))
} else if (field.value is ObjProperty) {
(field.value as ObjProperty).callSetter(scope, this, newValue, decl)
} else if (field.isMutable) field.value = newValue else scope.raiseError("can't assign to read-only field: $name")
}
@ -437,13 +525,16 @@ open class Obj {
scope.raiseNotImplemented()
}
suspend fun invoke(scope: Scope, thisObj: Obj, args: Arguments): Obj =
suspend fun invoke(scope: Scope, thisObj: Obj, args: Arguments, declaringClass: ObjClass? = null): Obj =
if (PerfFlags.SCOPE_POOL)
scope.withChildFrame(args, newThisObj = thisObj) { child ->
if (declaringClass != null) child.currentClassCtx = declaringClass
callOn(child)
}
else
callOn(scope.createChildScope(scope.pos, args = args, newThisObj = thisObj))
callOn(scope.createChildScope(scope.pos, args = args, newThisObj = thisObj).also {
if (declaringClass != null) it.currentClassCtx = declaringClass
})
suspend fun invoke(scope: Scope, thisObj: Obj, vararg args: Obj): Obj =
callOn(
@ -506,20 +597,46 @@ open class Obj {
companion object {
val rootObjectType = ObjClass("Obj").apply {
addFn("toString", true) {
addFnDoc(
name = "toString",
doc = "Returns a string representation of the object.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
thisObj.toString(this, true)
}
addFn("inspect", true) {
addFnDoc(
name = "inspect",
doc = "Returns a detailed string representation for debugging.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
thisObj.inspect(this).toObj()
}
addFn("contains") {
addFnDoc(
name = "contains",
doc = "Returns true if the object contains the given element.",
params = listOf(ParamDoc("element")),
returns = type("lyng.Bool"),
moduleName = "lyng.stdlib"
) {
ObjBool(thisObj.contains(this, args.firstAndOnly()))
}
// utilities
addFn("let") {
addFnDoc(
name = "let",
doc = "Calls the specified function block with `this` value as its argument and returns its result.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
args.firstAndOnly().callOn(createChildScope(Arguments(thisObj)))
}
addFn("apply") {
addFnDoc(
name = "apply",
doc = "Calls the specified function block with `this` value as its receiver and returns `this` value.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
val body = args.firstAndOnly()
(thisObj as? ObjInstance)?.let {
body.callOn(ApplyScope(this, it.instanceScope))
@ -528,11 +645,21 @@ open class Obj {
}
thisObj
}
addFn("also") {
addFnDoc(
name = "also",
doc = "Calls the specified function block with `this` value as its argument and returns `this` value.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
args.firstAndOnly().callOn(createChildScope(Arguments(thisObj)))
thisObj
}
addFn("run") {
addFnDoc(
name = "run",
doc = "Calls the specified function block with `this` value as its receiver and returns its result.",
params = listOf(ParamDoc("block")),
moduleName = "lyng.stdlib"
) {
args.firstAndOnly().callOn(this)
}
addFn("getAt") {
@ -545,7 +672,12 @@ open class Obj {
thisObj.putAt(this, requiredArg<Obj>(0), newValue)
newValue
}
addFn("toJsonString") {
addFnDoc(
name = "toJsonString",
doc = "Encodes this object to a JSON string.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
thisObj.toJson(this).toString().toObj()
}
}

View File

@ -23,7 +23,7 @@ import net.sergeych.lynon.BitArray
class ObjBitBuffer(val bitArray: BitArray) : Obj() {
override val objClass = type
override val objClass get() = type
override suspend fun getAt(scope: Scope, index: Obj): Obj {
return bitArray[index.toLong()].toObj()

View File

@ -37,7 +37,7 @@ data class ObjBool(val value: Boolean) : Obj() {
override fun toString(): String = value.toString()
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
override suspend fun logicalNot(scope: Scope): Obj = ObjBool(!value)

View File

@ -34,7 +34,7 @@ import kotlin.math.min
open class ObjBuffer(val byteArray: UByteArray) : Obj() {
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
val hex by lazy { byteArray.encodeToHex("")}
val base64 by lazy { byteArray.toByteArray().encodeToBase64Url()}

View File

@ -23,7 +23,7 @@ import net.sergeych.lyng.miniast.type
class ObjChar(val value: Char): Obj() {
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
override suspend fun compareTo(scope: Scope, other: Obj): Int =
(other as? ObjChar)?.let { value.compareTo(it.value) } ?: -1

View File

@ -30,6 +30,7 @@ private object ClassIdGen { var c: Long = 1L; fun nextId(): Long = c++ }
val ObjClassType by lazy {
ObjClass("Class").apply {
addProperty("className", getter = { thisAs<ObjClass>().classNameObj })
addFnDoc(
name = "name",
doc = "Simple name of this class (without package).",
@ -91,6 +92,10 @@ open class ObjClass(
vararg parents: ObjClass,
) : Obj() {
var isAnonymous: Boolean = false
var isAbstract: Boolean = false
// Stable identity and simple structural version for PICs
val classId: Long = ClassIdGen.nextId()
var layoutVersion: Int = 0
@ -119,6 +124,24 @@ open class ObjClass(
/** Direct parents in declaration order (kept deterministic). */
val directParents: List<ObjClass> = parents.toList()
/**
* Names of additional interfaces this class implements, but they are not (yet) available
* as [ObjClass] instances. This is used for "implementing existing interfaces" feature.
*/
val implementingNames = mutableSetOf<String>()
/**
* Combined set of [implementingNames] from this class and all its ancestors.
*/
val allImplementingNames: Set<String> by lazy {
buildSet {
addAll(implementingNames)
for (p in allParentsSet) {
addAll(p.implementingNames)
}
}
}
/** Optional constructor argument specs for each direct parent (set by compiler for user classes). */
open val directParentArgs: MutableMap<ObjClass, List<ParsedArgument>> = mutableMapOf()
@ -178,7 +201,10 @@ open class ObjClass(
val mro: List<ObjClass> by lazy {
val base = c3Linearize(this, mutableMapOf())
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
else base + rootObjectType
else {
val root = Obj.rootObjectType
base + root
}
}
/** Parents in C3 order (no self). */
@ -189,7 +215,7 @@ open class ObjClass(
val list = mutableListOf<String>()
if (includeSelf) list += className
mroParents.forEach { list += it.className }
return list.joinToString(" ")
return list.joinToString(", ")
}
override val objClass: ObjClass by lazy { ObjClassType }
@ -204,6 +230,7 @@ open class ObjClass(
override suspend fun compareTo(scope: Scope, other: Obj): Int = if (other === this) 0 else -1
override suspend fun callOn(scope: Scope): Obj {
if (isAbstract) scope.raiseError("can't instantiate abstract class $className")
val instance = createInstance(scope)
initializeInstance(instance, scope.args, runConstructors = true)
return instance
@ -225,16 +252,16 @@ open class ObjClass(
// This mirrors Obj.autoInstanceScope behavior for ad-hoc scopes and makes fb.method() resolution robust
// 1) members-defined methods
for ((k, v) in members) {
if (v.value is Statement) {
instance.instanceScope.objects[k] = v
if (v.value is Statement || v.type == ObjRecord.Type.Delegated) {
instance.instanceScope.objects[k] = if (v.type == ObjRecord.Type.Delegated) v.copy() else v
}
}
// 2) class-scope methods registered during class-body execution
classScope?.objects?.forEach { (k, rec) ->
if (rec.value is Statement) {
if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) {
// if not already present, copy reference for dispatch
if (!instance.instanceScope.objects.containsKey(k)) {
instance.instanceScope.objects[k] = rec
instance.instanceScope.objects[k] = if (rec.type == ObjRecord.Type.Delegated) rec.copy() else rec
}
}
}
@ -347,16 +374,58 @@ open class ObjClass(
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
pos: Pos = Pos.builtIn,
declaringClass: ObjClass? = this
) {
declaringClass: ObjClass? = this,
isAbstract: Boolean = false,
isClosed: Boolean = false,
isOverride: Boolean = false,
type: ObjRecord.Type = ObjRecord.Type.Field,
): ObjRecord {
// Validation of override rules: only for non-system declarations
if (pos != Pos.builtIn) {
val existing = getInstanceMemberOrNull(name)
var actualOverride = false
if (existing != null && existing.declaringClass != this) {
// If the existing member is private in the ancestor, it's not visible for overriding.
// It should be treated as a new member in this class.
if (!existing.visibility.isPublic && !canAccessMember(existing.visibility, existing.declaringClass, this)) {
// It's effectively not there for us, so actualOverride remains false
} else {
actualOverride = true
// It's an override (implicit or explicit)
if (existing.isClosed)
throw ScriptError(pos, "can't override closed member $name from ${existing.declaringClass?.className}")
if (!isOverride)
throw ScriptError(pos, "member $name overrides parent member but 'override' keyword is missing")
if (visibility.ordinal > existing.visibility.ordinal)
throw ScriptError(pos, "can't narrow visibility of $name from ${existing.visibility} to $visibility")
}
}
if (isOverride && !actualOverride) {
throw ScriptError(pos, "member $name is marked 'override' but does not override anything")
}
}
// Allow overriding ancestors: only prevent redefinition if THIS class already defines an immutable member
val existingInSelf = members[name]
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, writeVisibility, declaringClass = declaringClass)
val rec = ObjRecord(
initialValue, isMutable, visibility, writeVisibility,
declaringClass = declaringClass,
isAbstract = isAbstract,
isClosed = isClosed,
isOverride = isOverride,
type = type
)
members[name] = rec
// Structural change: bump layout version for PIC invalidation
layoutVersion += 1
return rec
}
private fun initClassScope(): Scope {
@ -370,27 +439,37 @@ open class ObjClass(
isMutable: Boolean = false,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
pos: Pos = Pos.builtIn
) {
pos: Pos = Pos.builtIn,
type: ObjRecord.Type = ObjRecord.Type.Field
): ObjRecord {
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, writeVisibility)
val rec = classScope!!.addItem(name, isMutable, initialValue, visibility, writeVisibility, recordType = type)
// Structural change: bump layout version for PIC invalidation
layoutVersion += 1
return rec
}
fun addFn(
name: String,
isOpen: Boolean = false,
isMutable: Boolean = false,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
declaringClass: ObjClass? = this,
code: suspend Scope.() -> Obj
isAbstract: Boolean = false,
isClosed: Boolean = false,
isOverride: Boolean = false,
pos: Pos = Pos.builtIn,
code: (suspend Scope.() -> Obj)? = null
) {
val stmt = statement { code() }
createField(name, stmt, isOpen, visibility, writeVisibility, Pos.builtIn, declaringClass)
val stmt = code?.let { statement { it() } } ?: ObjNull
createField(
name, stmt, isMutable, visibility, writeVisibility, pos, declaringClass,
isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride,
type = ObjRecord.Type.Fun
)
}
fun addConst(name: String, value: Obj) = createField(name, value, isMutable = false)
@ -401,18 +480,25 @@ open class ObjClass(
setter: (suspend Scope.(Obj) -> Unit)? = null,
visibility: Visibility = Visibility.Public,
writeVisibility: Visibility? = null,
declaringClass: ObjClass? = this
declaringClass: ObjClass? = this,
isAbstract: Boolean = false,
isClosed: Boolean = false,
isOverride: Boolean = false,
pos: Pos = Pos.builtIn,
) {
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, writeVisibility, declaringClass, type = ObjRecord.Type.Property)
layoutVersion += 1
val prop = if (isAbstract) ObjNull else ObjProperty(name, g, s)
createField(
name, prop, false, visibility, writeVisibility, pos, declaringClass,
isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride,
type = ObjRecord.Type.Property
)
}
fun addClassConst(name: String, value: Obj) = createClassField(name, value)
fun addClassFn(name: String, isOpen: Boolean = false, code: suspend Scope.() -> Obj) {
createClassField(name, statement { code() }, isOpen)
createClassField(name, statement { code() }, isOpen, type = ObjRecord.Type.Fun)
}
@ -442,6 +528,38 @@ open class ObjClass(
getInstanceMemberOrNull(name)
?: throw ScriptError(atPos, "symbol doesn't exist: $name")
fun findFirstConcreteMember(name: String): ObjRecord? {
for (cls in mro) {
cls.members[name]?.let {
if (!it.isAbstract) return it
}
}
return null
}
fun checkAbstractSatisfaction(pos: Pos) {
if (isAbstract) return
val missing = mutableSetOf<String>()
for (cls in mroParents) {
for ((name, rec) in cls.members) {
if (rec.isAbstract) {
val current = findFirstConcreteMember(name)
if (current == null) {
missing.add(name)
}
}
}
}
if (missing.isNotEmpty()) {
throw ScriptError(
pos,
"class $className is not abstract and does not implement abstract members: ${missing.joinToString(", ")}"
)
}
}
/**
* Resolve member starting from a specific ancestor class [start], not from this class.
* Searches [start] first, then traverses its linearized parents.
@ -462,15 +580,21 @@ open class ObjClass(
override suspend fun readField(scope: Scope, name: String): ObjRecord {
classScope?.objects?.get(name)?.let {
if (it.visibility.isPublic) return it
if (it.visibility.isPublic) return resolveRecord(scope, it, name, this)
}
return super.readField(scope, name)
}
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
initClassScope().objects[name]?.let {
if (it.isMutable) it.value = newValue
initClassScope().objects[name]?.let { rec ->
if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
del.invokeInstanceMethod(scope, "setValue", Arguments(this, ObjString(name), newValue))
return
}
if (rec.isMutable) rec.value = newValue
else scope.raiseIllegalAssignment("can't assign $name is not mutable")
return
}
?: super.writeField(scope, name, newValue)
}
@ -479,8 +603,26 @@ open class ObjClass(
scope: Scope, name: String, args: Arguments,
onNotFoundResult: (suspend () -> Obj?)?
): Obj {
return classScope?.objects?.get(name)?.value?.invoke(scope, this, args)
?: super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
getInstanceMemberOrNull(name)?.let { rec ->
val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this
if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: scope.raiseError("Internal error: delegated member $name has no delegate")
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs), onNotFoundResult = {
// 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)
} else {
// Resolved field or property value
val resolved = readField(scope, name)
return resolved.value.invoke(scope, this, args, decl)
}
}
return super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
}
open suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =

View File

@ -25,7 +25,7 @@ import net.sergeych.lyng.miniast.type
class ObjCompletableDeferred(val completableDeferred: CompletableDeferred<Obj>): ObjDeferred(completableDeferred) {
override val objClass = type
override val objClass get() = type
companion object {
val type = object: ObjClass("CompletableDeferred", ObjDeferred.type){

View File

@ -24,7 +24,7 @@ import net.sergeych.lyng.miniast.type
open class ObjDeferred(val deferred: Deferred<Obj>): Obj() {
override val objClass = type
override val objClass get() = type
companion object {
val type = object: ObjClass("Deferred"){

View File

@ -29,7 +29,7 @@ import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
class ObjDuration(val duration: Duration) : Obj() {
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
override fun toString(): String {
return duration.toString()

View File

@ -23,7 +23,7 @@ import net.sergeych.lyng.Scope
import net.sergeych.lyng.Statement
class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
companion object {
val type = ObjClass("DelegateContext").apply {
@ -54,7 +54,7 @@ class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
*/
open class ObjDynamic(var readCallback: Statement? = null, var writeCallback: Statement? = null) : Obj() {
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
// Capture the lexical scope used to build this dynamic so callbacks can see outer locals
internal var builderScope: Scope? = null

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.
@ -41,7 +41,7 @@ open class ObjException(
val exceptionClass: ExceptionClass,
val scope: Scope,
val message: ObjString,
@Suppress("unused") val extraData: Obj = ObjNull,
val extraData: Obj = ObjNull,
val useStackTrace: ObjList? = null
) : Obj() {
constructor(name: String, scope: Scope, message: String) : this(
@ -54,6 +54,46 @@ open class ObjException(
suspend fun getStackTrace(): ObjList {
return cachedStackTrace.get {
captureStackTrace(scope)
}
}
constructor(scope: Scope, message: String) : this(Root, scope, ObjString(message))
fun raise(): Nothing {
throw ExecutionError(this, scope.pos, message.value)
}
override val objClass: ObjClass = exceptionClass
/**
* Tool to get kotlin string with error report including the Lyng stack trace.
*/
suspend fun toStringWithStackTrace(): String {
val l = getStackTrace().list
return buildString {
append(message.value)
for( t in l)
append("\n\tat ${t.toString(scope)}")
}
}
override suspend fun defaultToString(scope: Scope): ObjString {
val at = getStackTrace().list.firstOrNull()?.toString(scope)
?: ObjString("(unknown)")
return ObjString("${objClass.className}: $message at $at")
}
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
encoder.encodeAny(scope, exceptionClass.classNameObj)
encoder.encodeAny(scope, message)
encoder.encodeAny(scope, extraData)
encoder.encodeAny(scope, getStackTrace())
}
companion object {
suspend fun captureStackTrace(scope: Scope): ObjList {
val result = ObjList()
val maybeCls = scope.get("StackTraceEntry")?.value as? ObjClass
var s: Scope? = scope
@ -77,35 +117,17 @@ open class ObjException(
s = s.parent
lastPos = pos
}
result
return result
}
}
constructor(scope: Scope, message: String) : this(Root, scope, ObjString(message))
fun raise(): Nothing {
throw ExecutionError(this)
}
override val objClass: ObjClass = exceptionClass
override suspend fun defaultToString(scope: Scope): ObjString {
val at = getStackTrace().list.firstOrNull()?.toString(scope)
?: ObjString("(unknown)")
return ObjString("${objClass.className}: $message at $at")
}
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
encoder.encodeAny(scope, exceptionClass.classNameObj)
encoder.encodeAny(scope, message)
encoder.encodeAny(scope, extraData)
encoder.encodeAny(scope, getStackTrace())
}
companion object {
class ExceptionClass(val name: String, vararg parents: ObjClass) : ObjClass(name, *parents) {
init {
constructorMeta = ArgsDeclaration(
listOf(ArgsDeclaration.Item("message", defaultValue = statement { ObjString(name) })),
Token.Type.RPAREN
)
}
override suspend fun callOn(scope: Scope): Obj {
val message = scope.args.getOrNull(0)?.toString(scope) ?: ObjString(name)
return ObjException(this, scope, message)
@ -137,22 +159,74 @@ open class ObjException(
}
val Root = ExceptionClass("Exception").apply {
instanceInitializers.add(statement {
if (thisObj is ObjInstance) {
val msg = get("message")?.value ?: ObjString("Exception")
(thisObj as ObjInstance).instanceScope.addItem("Exception::message", false, msg)
val stack = captureStackTrace(this)
(thisObj as ObjInstance).instanceScope.addItem("Exception::stackTrace", false, stack)
}
ObjVoid
})
instanceConstructor = statement { ObjVoid }
addConstDoc(
name = "message",
value = statement {
(thisObj as ObjException).message.toObj()
when (val t = thisObj) {
is ObjException -> t.message
is ObjInstance -> t.instanceScope.get("Exception::message")?.value ?: ObjNull
else -> ObjNull
}
},
doc = "Human‑readable error message.",
type = type("lyng.String"),
moduleName = "lyng.stdlib"
)
addConstDoc(
name = "extraData",
value = statement {
when (val t = thisObj) {
is ObjException -> t.extraData
else -> ObjNull
}
},
doc = "Extra data associated with the exception.",
type = type("lyng.Any", nullable = true),
moduleName = "lyng.stdlib"
)
addFnDoc(
name = "stackTrace",
doc = "Stack trace captured at throw site as a list of `StackTraceEntry`.",
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.StackTraceEntry"))),
moduleName = "lyng.stdlib"
) {
(thisObj as ObjException).getStackTrace()
when (val t = thisObj) {
is ObjException -> t.getStackTrace()
is ObjInstance -> t.instanceScope.get("Exception::stackTrace")?.value as? ObjList ?: ObjList()
else -> ObjList()
}
}
addFnDoc(
name = "toString",
doc = "Human‑readable string representation of the error.",
returns = type("lyng.String"),
moduleName = "lyng.stdlib"
) {
val msg = when (val t = thisObj) {
is ObjException -> t.message.value
is ObjInstance -> (t.instanceScope.get("Exception::message")?.value as? ObjString)?.value
?: t.objClass.className
else -> t.objClass.className
}
val stack = when (val t = thisObj) {
is ObjException -> t.getStackTrace()
is ObjInstance -> t.instanceScope.get("Exception::stackTrace")?.value as? ObjList ?: ObjList()
else -> ObjList()
}
val at = stack.list.firstOrNull()?.toString(this) ?: ObjString("(unknown)")
ObjString("${thisObj.objClass.className}: $msg at $at")
}
}
@ -191,11 +265,12 @@ open class ObjException(
"IllegalAssignmentException",
"SymbolNotDefinedException",
"IterationEndException",
"AccessException",
"IllegalAccessException",
"UnknownException",
"NotFoundException",
"IllegalOperationException",
"UnsetException",
"NotImplementedException",
"SyntaxError"
)) {
scope.addConst(name, getOrCreateExceptionClass(name))
@ -236,8 +311,8 @@ class ObjSymbolNotDefinedException(scope: Scope, message: String = "symbol is no
class ObjIterationFinishedException(scope: Scope) :
ObjException("IterationEndException", scope, "iteration finished")
class ObjAccessException(scope: Scope, message: String = "access not allowed error") :
ObjException("AccessException", scope, message)
class ObjIllegalAccessException(scope: Scope, message: String = "access not allowed error") :
ObjException("IllegalAccessException", scope, message)
class ObjUnknownException(scope: Scope, message: String = "access not allowed error") :
ObjException("UnknownException", scope, message)
@ -250,3 +325,46 @@ class ObjNotFoundException(scope: Scope, message: String = "not found") :
class ObjUnsetException(scope: Scope, message: String = "property is unset (not initialized)") :
ObjException("UnsetException", scope, message)
class ObjNotImplementedException(scope: Scope, message: String = "not implemented") :
ObjException("NotImplementedException", scope, message)
/**
* Check if the object is an instance of Lyng Exception class.
*/
fun Obj.isLyngException(): Boolean = isInstanceOf("Exception")
/**
* Get the exception message.
*/
suspend fun Obj.getLyngExceptionMessage(scope: Scope): String =
invokeInstanceMethod(scope, "message").toString(scope).value
/**
* Get the exception stack trace.
*/
suspend fun Obj.getLyngExceptionStackTrace(scope: Scope): ObjList =
invokeInstanceMethod(scope, "stackTrace").cast(scope)
/**
* Get the exception extra data.
*/
suspend fun Obj.getLyngExceptionExtraData(scope: Scope): Obj =
invokeInstanceMethod(scope, "extraData")
/**
* Get the exception as a formatted string with the primary throw site.
*/
suspend fun Obj.getLyngExceptionString(scope: Scope): String =
invokeInstanceMethod(scope, "toString").toString(scope).value
/**
* Rethrow this object as a Kotlin [ExecutionError] if it's an exception.
*/
suspend fun Obj.raiseAsExecutionError(scope: Scope?=null): Nothing {
if (this is ObjException) raise()
val sc = scope ?: Script.newScope()
val msg = getLyngExceptionMessage(sc)
val pos = (this as? ObjInstance)?.instanceScope?.pos ?: Pos.builtIn
throw ExecutionError(this, pos, msg)
}

View File

@ -35,7 +35,7 @@ import kotlin.coroutines.cancellation.CancellationException
class ObjFlowBuilder(val output: SendChannel<Obj>) : Obj() {
override val objClass = type
override val objClass get() = type
companion object {
@OptIn(DelicateCoroutinesApi::class)
@ -91,7 +91,7 @@ private fun createLyngFlowInput(scope: Scope, producer: Statement): ReceiveChann
class ObjFlow(val producer: Statement, val scope: Scope) : Obj() {
override val objClass = type
override val objClass get() = type
companion object {
val type = object : ObjClass("Flow", ObjIterable) {
@ -119,7 +119,7 @@ class ObjFlow(val producer: Statement, val scope: Scope) : Obj() {
class ObjFlowIterator(val producer: Statement) : Obj() {
override val objClass: ObjClass = type
override val objClass: ObjClass get() = type
private var channel: ReceiveChannel<Obj>? = null

View File

@ -31,7 +31,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
internal lateinit var instanceScope: Scope
override suspend fun readField(scope: Scope, name: String): ObjRecord {
// Direct (unmangled) lookup first
// 1. Direct (unmangled) lookup first
instanceScope[name]?.let { rec ->
val decl = rec.declaringClass
// Allow unconditional access when accessing through `this` of the same instance
@ -40,33 +40,26 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't access field $name (declared in ${decl?.className ?: "?"})"
)
)
}
if (rec.type == ObjRecord.Type.Property) {
val prop = rec.value as ObjProperty
return rec.copy(value = prop.callGetter(scope, this, decl))
}
return rec
return resolveRecord(scope, rec, name, decl)
}
// Try MI-mangled lookup along linearization (C3 MRO): ClassName::name
val cls = objClass
// self first, then parents
fun findMangled(): ObjRecord? {
// self
// 2. MI-mangled instance scope lookup
val cls = objClass
fun findMangledInRead(): ObjRecord? {
instanceScope.objects["${cls.className}::$name"]?.let { return it }
// ancestors in deterministic C3 order
for (p in cls.mroParents) {
instanceScope.objects["${p.className}::$name"]?.let { return it }
}
return null
}
findMangled()?.let { rec ->
// derive declaring class by mangled prefix: try self then parents
findMangledInRead()?.let { rec ->
val declaring = when {
instanceScope.objects.containsKey("${cls.className}::$name") -> cls
else -> cls.mroParents.firstOrNull { instanceScope.objects.containsKey("${it.className}::$name") }
@ -75,22 +68,37 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, declaring, caller))
scope.raiseError(
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't access field $name (declared in ${declaring?.className ?: "?"})"
)
)
}
if (rec.type == ObjRecord.Type.Property) {
val prop = rec.value as ObjProperty
return rec.copy(value = prop.callGetter(scope, this, declaring))
}
return rec
return resolveRecord(scope, rec, name, declaring)
}
// Fall back to methods/properties on class
// 3. Fall back to super (handles class members and extensions)
return super.readField(scope, name)
}
override suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
if (obj.type == ObjRecord.Type.Delegated) {
val storageName = "${decl?.className}::$name"
var del = instanceScope[storageName]?.delegate
if (del == null) {
for (c in objClass.mro) {
del = instanceScope["${c.className}::$name"]?.delegate
if (del != null) break
}
}
del = del ?: obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
val res = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
obj.value = res
return obj
}
return super.resolveRecord(scope, obj, name, decl)
}
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
// Direct (unmangled) first
instanceScope[name]?.let { f ->
@ -98,7 +106,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
).raise()
@ -108,6 +116,19 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
prop.callSetter(scope, this, newValue, decl)
return
}
if (f.type == ObjRecord.Type.Delegated) {
val storageName = "${decl?.className}::$name"
var del = instanceScope[storageName]?.delegate
if (del == null) {
for (c in objClass.mro) {
del = instanceScope["${c.className}::$name"]?.delegate
if (del != null) break
}
}
del = del ?: f.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
del.invokeInstanceMethod(scope, "setValue", Arguments(this, ObjString(name), newValue))
return
}
if (!f.isMutable && f.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (f.value.assign(scope, newValue) == null)
f.value = newValue
@ -132,7 +153,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(rec.effectiveWriteVisibility, declaring, caller))
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't assign to field $name (declared in ${declaring?.className ?: "?"})"
).raise()
@ -142,6 +163,19 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
prop.callSetter(scope, this, newValue, declaring)
return
}
if (rec.type == ObjRecord.Type.Delegated) {
val storageName = "${declaring?.className}::$name"
var del = instanceScope[storageName]?.delegate
if (del == null) {
for (c in objClass.mro) {
del = instanceScope["${c.className}::$name"]?.delegate
if (del != null) break
}
}
del = del ?: rec.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
del.invokeInstanceMethod(scope, "setValue", Arguments(this, ObjString(name), newValue))
return
}
if (!rec.isMutable && rec.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
if (rec.value.assign(scope, newValue) == null)
rec.value = newValue
@ -153,39 +187,49 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
override suspend fun invokeInstanceMethod(
scope: Scope, name: String, args: Arguments,
onNotFoundResult: (suspend () -> Obj?)?
): Obj =
instanceScope[name]?.let { rec ->
val decl = rec.declaringClass
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(
ObjAccessException(
scope,
"can't invoke method $name (declared in ${decl?.className ?: "?"})"
)
)
rec.value.invoke(
instanceScope,
this,
args
)
}
?: run {
// fallback: class-scope function (registered during class body execution)
objClass.classScope?.objects?.get(name)?.let { rec ->
val decl = rec.declaringClass
): Obj {
// 1. Walk MRO to find member, handling delegation
for (cls in objClass.mro) {
if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null) {
if (rec.type == ObjRecord.Type.Delegated) {
val storageName = "${cls.className}::$name"
val del = instanceScope[storageName]?.delegate ?: rec.delegate
?: scope.raiseError("Internal error: delegated member $name has no delegate (tried $storageName)")
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs), onNotFoundResult = {
// 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
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't invoke method $name (declared in ${decl?.className ?: "?"})"
"can't invoke method $name (declared in ${decl.className})"
)
)
rec.value.invoke(instanceScope, this, args)
return rec.value.invoke(
instanceScope,
this,
args,
decl
)
} else if ((rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property) && !rec.isAbstract) {
val resolved = readField(scope, name)
return resolved.value.invoke(scope, this, args, resolved.declaringClass)
}
}
?: super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
}
// 2. Fall back to super (handles extensions and root fallback)
return super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
}
private val publicFields: Map<String, ObjRecord>
get() = instanceScope.objects.filter {
@ -219,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)
}
}
@ -250,10 +294,12 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
// }
val serializingVars: Map<String, ObjRecord> by lazy {
val metaParams = objClass.constructorMeta?.params?.map { it.name }?.toSet() ?: emptySet()
instanceScope.objects.filter {
it.value.type.serializable &&
it.value.type == ObjRecord.Type.Field &&
it.value.isMutable
it.value.isMutable &&
!metaParams.contains(it.key)
}
}
@ -306,8 +352,8 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val decl = rec.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't access field $name (declared in ${decl.className})"))
return rec
scope.raiseError(ObjIllegalAccessException(scope, "can't access field $name (declared in ${decl.className})"))
return resolveRecord(scope, rec, name, decl)
}
// Then try instance locals (unmangled) only if startClass is the dynamic class itself
if (startClass === instance.objClass) {
@ -316,12 +362,12 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't access field $name (declared in ${decl?.className ?: "?"})"
)
)
return rec
return resolveRecord(scope, rec, name, decl)
}
}
// Finally try methods/properties starting from ancestor
@ -329,19 +375,9 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val decl = r.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(r.visibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't access field $name (declared in ${decl.className})"))
return when (val value = r.value) {
is net.sergeych.lyng.Statement -> ObjRecord(
value.execute(
instance.instanceScope.createChildScope(
scope.pos,
newThisObj = instance
)
), r.isMutable
)
else -> r
}
scope.raiseError(ObjIllegalAccessException(scope, "can't access field $name (declared in ${decl.className})"))
return resolveRecord(scope, r, name, decl)
}
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
@ -351,7 +387,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val decl = f.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't assign to field $name (declared in ${decl.className})"
).raise()
@ -365,7 +401,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val decl = f.declaringClass ?: instance.objClass.findDeclaringClassOf(name)
val caller = scope.currentClassCtx
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
).raise()
@ -378,7 +414,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val decl = r.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(r.effectiveWriteVisibility, decl, caller))
ObjAccessException(scope, "can't assign to field $name (declared in ${decl.className})").raise()
ObjIllegalAccessException(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
}
@ -394,7 +430,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val decl = rec.declaringClass ?: startClass
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjAccessException(scope, "can't invoke method $name (declared in ${decl.className})"))
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke method $name (declared in ${decl.className})"))
val saved = instance.instanceScope.currentClassCtx
instance.instanceScope.currentClassCtx = decl
try {
@ -410,7 +446,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(
ObjAccessException(
ObjIllegalAccessException(
scope,
"can't invoke method $name (declared in ${decl?.className ?: "?"})"
)

View File

@ -42,9 +42,6 @@ class ObjInstanceClass(val name: String, vararg parents: ObjClass) : ObjClass(na
}
init {
addFn("toString", true) {
thisObj.toString(this, true)
}
}
}

View File

@ -20,6 +20,7 @@ package net.sergeych.lyng.obj
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonType
@ -178,7 +179,12 @@ class ObjInt(val value: Long, override val isConst: Boolean = false) : Obj(), Nu
else -> scope.raiseIllegalState("illegal type code for Int: $lynonType")
}
}.apply {
addFn("toInt") {
addFnDoc(
name = "toInt",
doc = "Returns this integer (identity operation).",
returns = net.sergeych.lyng.miniast.type("lyng.Int"),
moduleName = "lyng.stdlib"
) {
thisObj
}
}

Some files were not shown because too many files have changed in this diff Show More