Compare commits

..
Author SHA1 Message Date
sergeych 827df9c8cd Fix argument priority handling in ObjInstance resolution logic and add corresponding tests 2026-01-10 20:25:06 +01:00
sergeych 7ab439d949 Updated documentation to clarify property syntax, closure resolution rules, and override usage 2026-01-10 18:08:07 +01:00
sergeych 91e6ae29ce fixed mist edge cases with new language logic 2026-01-10 14:49:54 +01:00
sergeych b9831a422a 1.2.0-SNAPSHOT started bug core refactor to support all new future and more strict rules (namely, fundamental difference between properties, functions and delegates) 2026-01-10 02:57:20 +01:00
sergeych 10b7cb2db2 improved error reporting for mismatched serialized vars in ObjInstance 2026-01-08 21:10:37 +01:00
sergeych 1931384116 fixed another closure bug 2026-01-08 19:52:38 +01:00
sergeych 2acb60697d more tools to deal with Lyng exeption in Kotlin 2026-01-08 12:42:29 +01:00
sergeych c12804a806 user exceptions to kotlin fixes 2026-01-08 09:21:25 +01:00
sergeych 1d089db9ff User exception classes, unified exception class architecture 2026-01-07 19:05:07 +01:00
sergeych 2ce6d8e482 tools for error reporting in kotlin 2026-01-07 18:12:14 +01:00
sergeych eda34c1b3d operators overriding missing files 2026-01-07 09:44:13 +01:00
sergeych 2c0a6c7b34 operators overriding 2026-01-07 09:33:10 +01:00
sergeych fe5dded7af plugin with type declarations, collection types and much better type tracking for autocomplete 2026-01-07 02:55:47 +01:00
sergeych aba0048a83 plugin no wworks with .lyng.d files. hurray 2026-01-06 19:32:40 +01:00
sergeych 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
sergeych 44675b976d script to piblish all (for Linux) 2026-01-06 13:26:58 +01:00
sergeych 3941ddee40 + Ma is now Delegate
+ way to add pure Lyng parents when declaring kotlin bindings
2026-01-06 13:11:07 +01:00
sergeych 3ef68d8bb4 fixed autocompletion for class constructor parameters 2026-01-06 12:08:23 +01:00
sergeych 72bb6ae67b formatter fix on properties 2026-01-06 12:00:04 +01:00
sergeych 555c9b94de null assignment operator added 2026-01-06 11:39:23 +01:00
sergeych 8cd980514b code cleanup performed (trivial cases) 2026-01-06 10:38:31 +01:00
sergeych fa4dc45f15 readme: github specific stuff/2 2026-01-06 03:47:38 +01:00
sergeych ae68ecd066 readme: github specific stuff 2026-01-06 03:44:48 +01:00
sergeych ddfdc18ba3 more readme 2026-01-06 03:40:22 +01:00
sergeych 1afc6d41bc more readme 2026-01-06 03:35:03 +01:00
sergeych 83ab8cdc01 updated readme for hub 2026-01-06 03:11:19 +01:00
sergeych 26b8370b01 added label syntax support and highlighting 2026-01-06 02:37:16 +01:00
sergeych 660a80a26b lyngio: added processes and docs for it, JVM and linux 2026-01-06 02:35:24 +01:00
sergeych d91acd593a added return statement support 2026-01-06 02:18:31 +01:00
sergeych 5fc0969491 added object expression support to the site, tnbundle, etc 2026-01-06 01:51:16 +01:00
sergeych eec732d11a implemented object expressions (anonymous classes) 2026-01-06 01:25:01 +01:00
sergeych 8611543623 fixed delegation sample 2026-01-06 01:00:20 +01:00
sergeych 73854f21f3 reformatted root.lyng for better readability/2 2026-01-05 22:30:51 +01:00
sergeych f66e61c185 reformatted root.lyng for better readability 2026-01-05 22:27:02 +01:00
sergeych 41a3617850 improved lazy delegate; added with(newThis) {} to stdlib 2026-01-05 22:25:46 +01:00
sergeych 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
sergeych 20f777f9f6 version on site 2026-01-05 19:09:46 +01:00
141 changed files with 7720 additions and 2143 deletions

No files matched your search

+6
View File
@@ -21,3 +21,9 @@ xcuserdata
debug.log debug.log
/build.log /build.log
/test.md /test.md
/build_output.txt
/build_output_full.txt
/check_output.txt
/compile_jvm_output.txt
/compile_metadata_output.txt
test_output*.txt
+11
View File
@@ -2,6 +2,17 @@
### Unreleased ### 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 - Language: Abstract Classes and Interfaces
- Support for `abstract` modifier on classes, methods, and variables. - 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. - Introduced `interface` as a synonym for `abstract class`, supporting full state (constructors, fields, `init` blocks) and implementation by parts via MI.
+29 -22
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: - 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) - 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% 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] - 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.
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.
- Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`. - Any Unicode letters can be used as identifiers: `assert( sin(π/2) == 1 )`.
## Resources: ## Resources:
@@ -44,6 +39,8 @@ and it is multithreaded on platforms supporting it (automatically, no code chang
- [Language home](https://lynglang.com) - [Language home](https://lynglang.com)
- [introduction and tutorial](docs/tutorial.md) - start here please - [introduction and tutorial](docs/tutorial.md) - start here please
- [Testing and Assertions](docs/Testing.md) - [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) - [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
- [Samples directory](docs/samples) - [Samples directory](docs/samples)
- [Formatter (core + CLI + IDE)](docs/formatter.md) - [Formatter (core + CLI + IDE)](docs/formatter.md)
@@ -79,7 +76,7 @@ Now you can import lyng and use it:
### Execute script: ### Execute script:
```kotlin ```kotlin
import net.sergeyh.lyng.* import net.sergeych.lyng.*
// we need a coroutine to start, as Lyng // we need a coroutine to start, as Lyng
// is a coroutine based language, async topdown // 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: add your specific vars and functions to it, and call:
```kotlin ```kotlin
import net.sergeych.lyng.*
import com.sun.source.tree.Scope
import new.sergeych.lyng.*
// simple function // simple function
val scope = Script.newScope().apply { val scope = Script.newScope().apply {
@@ -176,6 +171,7 @@ Ready features:
- [x] ranges, lists, strings, interfaces: Iterable, Iterator, Collection, Array - [x] ranges, lists, strings, interfaces: Iterable, Iterator, Collection, Array
- [x] when(value), if-then-else - [x] when(value), if-then-else
- [x] exception handling: throw, try-catch-finally, exception classes. - [x] exception handling: throw, try-catch-finally, exception classes.
- [x] user-defined exception classes
- [x] multiplatform maven publication - [x] multiplatform maven publication
- [x] documentation for the current state - [x] documentation for the current state
- [x] maps, sets and sequences (flows?) - [x] maps, sets and sequences (flows?)
@@ -191,6 +187,17 @@ Ready features:
- [x] regular exceptions + extended `when` - [x] regular exceptions + extended `when`
- [x] multiple inheritance for user classes - [x] multiple inheritance for user classes
- [x] class properties (accessors) - [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 ## plan: towards v1.5 Enhancing
+4
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.__
File renamed without changes.
+117
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 } }
File renamed without changes.
File renamed without changes.
Executable
+40
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
+2 -2
View File
@@ -23,11 +23,11 @@ There are a lo of ways to construct a buffer:
assertEquals( 5, Buffer("hello").size ) assertEquals( 5, Buffer("hello").size )
// from bytes, e.g. integers in range 0..255 // from bytes, e.g. integers in range 0..255
assertEquals( 255, Buffer(1,2,3,255).last() ) assertEquals( 255, Buffer(1,2,3,255).last )
// from whatever iterable that produces bytes, e.g. // from whatever iterable that produces bytes, e.g.
// integers in 0..255 range: // integers in 0..255 range:
assertEquals( 129, Buffer([1,2,129]).last() ) assertEquals( 129, Buffer([1,2,129]).last )
// Empty buffer of fixed size: // Empty buffer of fixed size:
assertEquals(100, Buffer(100).size) assertEquals(100, Buffer(100).size)
+176 -13
View File
@@ -71,14 +71,57 @@ object DefaultLogger : Logger("Default") {
} }
``` ```
## 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
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. 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.
### Basic Syntax ### Basic Syntax
Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and `get()`/`set()` blocks: Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and a `get` (and optionally `set`) accessor. Unlike fields, properties do not have automatic storage and must compute their values or delegate to other members.
The standard syntax uses parentheses:
```lyng ```lyng
class Person(private var _age: Int) { class Person(private var _age: Int) {
// Read-only property // Read-only property
@@ -94,21 +137,17 @@ class Person(private var _age: Int) {
if (value >= 0) _age = value if (value >= 0) _age = value
} }
} }
val p = Person(15)
assertEquals("Minor", p.ageCategory)
p.age = 20
assertEquals("Adult", p.ageCategory)
``` ```
### Laconic Expression Shorthand ### Laconic Syntax (Optional Parentheses)
For simple accessors and methods, you can use the `=` shorthand for a more elegant and laconic form: For even cleaner code, you can omit the parentheses for `get` and `set`. This is especially useful for simple expression shorthand:
```lyng ```lyng
class Circle(val radius: Real) { class Circle(val radius: Real) {
val area get() = π * radius * radius // Laconic expression shorthand
val circumference get() = 2 * π * radius val area get = π * radius * radius
val circumference get = 2 * π * radius
fun diameter() = radius * 2 fun diameter() = radius * 2
} }
@@ -117,15 +156,16 @@ fun median(a, b) = (a + b) / 2
class Counter { class Counter {
private var _count = 0 private var _count = 0
var count get() = _count set(v) = _count = v var count get = _count set(v) = _count = v
} }
``` ```
### Key Rules ### Key Rules
- **`val` properties** must have a `get()` accessor and cannot have a `set()`. - **`val` properties** must have a `get` accessor (with or without parentheses) and cannot have a `set`.
- **`var` properties** must have both `get()` and `set()` accessors. - **`var` properties** must have both `get` and `set` accessors.
- **Functions and methods** can use the `=` shorthand to return the result of a single expression. - **Functions and methods** can use the `=` shorthand to return the result of a single expression.
- **`override` is mandatory**: If you are overriding a member from a base class, you MUST use the `override` keyword.
- **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field. - **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field.
- **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing. - **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing.
@@ -182,6 +222,19 @@ A delegate is any object that provides the following methods (all optional depen
- `invoke(thisRef, name, args...)`: Called when a delegated `fun` is invoked. - `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. - `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). For more details and advanced patterns (like `lazy`, `observable`, and shared stateless delegates), see the [Delegation Guide](delegation.md).
## Instance initialization: init block ## Instance initialization: init block
@@ -489,6 +542,99 @@ class Critical {
Attempting to override a `closed` member results in a compile-time error. 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: Compatibility notes:
- Existing single‑inheritance code continues to work unchanged; its resolution order reduces to the single base. - Existing single‑inheritance code continues to work unchanged; its resolution order reduces to the single base.
@@ -572,6 +718,23 @@ Notes and limitations (current version):
- `name` and `ordinal` are read‑only properties of an entry. - `name` and `ordinal` are read‑only properties of an entry.
- `entries` is a read‑only list owned by the enum type. - `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 ## fields and visibility
It is possible to add non-constructor fields: It is possible to add non-constructor fields:
-15
View File
@@ -1,15 +0,0 @@
# AI notes: avoid Kotlin/Wasm invalid IR with suspend lambdas
## Do
- Prefer explicit `object : Statement()` with `override suspend fun execute(...)` when building compiler statements.
- Keep `Statement` objects non-lambda, especially in compiler hot paths like parsing/var declarations.
- If you need conditional behavior, return early in `execute` instead of wrapping `parseExpression()` with `statement(...) { ... }`.
- When wasmJs tests hang in the browser, first check `wasmJsNodeTest` for a compile error; hangs often mean module instantiation failed.
## Don't
- Do not create suspend lambdas inside `Statement` factories (`statement { ... }`) for wasm targets.
- Do not "fix" hangs by increasing browser timeouts; it masks invalid wasm generation.
## Debugging tips
- Look for `$invokeCOROUTINE$` in wasm function names when mapping failures.
- If node test logs a wasm compile error, the browser hang is likely the same root cause.
+20 -1
View File
@@ -57,7 +57,8 @@ class lazy(val creator) : Delegate {
override fun getValue(thisRef, name) { override fun getValue(thisRef, name) {
if (value == Unset) { if (value == Unset) {
value = creator() // calculate value using thisRef as this:
value = with(thisRef) creator()
} }
value value
} }
@@ -150,6 +151,24 @@ fun test() {
} }
``` ```
### 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` Hook
The `bind(name, access, thisRef)` method is called exactly once when the member is being initialized. It allows the delegate to: The `bind(name, access, thisRef)` method is called exactly once when the member is being initialized. It allows the delegate to:
+58
View File
@@ -103,6 +103,11 @@ scope.addVoidFn("log") {
println(items.joinToString(" ") { it.toString(this).value }) println(items.joinToString(" ") { it.toString(this).value })
} }
// When adding a member function to a class, you can use isOverride = true
// myClass.addFn("toString", isOverride = true) {
// ObjString("Custom string representation")
// }
// Call them from Lyng // Call them from Lyng
scope.eval("val y = inc(41); log('Answer:', y)") scope.eval("val y = inc(41); log('Answer:', y)")
``` ```
@@ -122,6 +127,9 @@ myClass.createField("version", ObjString("1.0.0"), isMutable = false)
// Add a mutable field with an initial value // Add a mutable field with an initial value
myClass.createField("count", ObjInt(0), isMutable = true) myClass.createField("count", ObjInt(0), isMutable = true)
// If you are overriding a field from a base class, use isOverride = true
// myClass.createField("someBaseField", ObjInt(42), isOverride = true)
scope.addConst("MyClass", myClass) scope.addConst("MyClass", myClass)
``` ```
@@ -153,6 +161,16 @@ myClass.addProperty(
} }
) )
// You can also create an ObjProperty explicitly
val explicitProp = ObjProperty(
name = "hexValue",
getter = statement { ObjString(internalValue.toString(16)) }
)
myClass.addProperty("hexValue", prop = explicitProp)
// Use isOverride = true when overriding a property from a base class
// myClass.addProperty("baseProp", getter = { ... }, isOverride = true)
scope.addConst("MyClass", myClass) scope.addConst("MyClass", myClass)
``` ```
@@ -286,6 +304,46 @@ val isolated = net.sergeych.lyng.Scope.new()
- When registering packages, names must be unique. Register before you compile/evaluate scripts that import them. - 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. - 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?=null)` | Returns the exception message as a Kotlin `String`. |
| `obj.getLyngExceptionMessageWithStackTrace(scope?=null)` | Returns a detailed message with a formatted stack trace. |
| `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?=null)` | 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. 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.
+88 -7
View File
@@ -128,9 +128,11 @@ Serializable class that conveys information about the exception. Important membe
| name | description | | name | description |
|-------------------|--------------------------------------------------------| |-------------------|--------------------------------------------------------|
| message | String message | | message | String message |
| stackTrace | lyng stack trace, list of `StackTraceEntry`, see below | | stackTrace() | lyng stack trace, list of `StackTraceEntry`, see below |
| printStackTrace() | format and print stack trace using println() | | printStackTrace() | format and print stack trace using println() |
> **Note for Kotlin users**: When working with Lyng exceptions from Kotlin, you can use extension methods like `getLyngExceptionMessageWithStackTrace()`. See [Embedding Lyng](embedding.md#12-handling-and-serializing-exceptions) for the full API.
## StackTraceEntry ## StackTraceEntry
@@ -150,24 +152,103 @@ class StackTraceEntry(
# Custom error classes # 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 # Standard exception classes
| class | notes | | class | notes |
|----------------------------|-------------------------------------------------------| |----------------------------|-------------------------------------------------------|
| Exception | root of al throwable objects | | Exception | root of all throwable objects |
| NullReferenceException | | | NullReferenceException | |
| AssertionFailedException | | | AssertionFailedException | |
| ClassCastException | | | ClassCastException | |
| IndexOutOfBoundsException | | | IndexOutOfBoundsException | |
| IllegalArgumentException | | | IllegalArgumentException | |
| IllegalStateException | |
| NoSuchElementException | |
| IllegalAssignmentException | assigning to val, etc. | | IllegalAssignmentException | assigning to val, etc. |
| SymbolNotDefinedException | | | SymbolNotDefinedException | |
| IterationEndException | attempt to read iterator past end, `hasNext == false` | | IterationEndException | attempt to read iterator past end, `hasNext == false` |
| IllegalAccessException | attempt to access private members or like | | IllegalAccessException | attempt to access private members or like |
| UnknownException | unexpected kotlin exception caught | | UnknownException | unexpected internal exception caught |
| | | | NotFoundException | |
| IllegalOperationException | |
| UnsetException | access to uninitialized late-init val |
| NotImplementedException | used by `TODO()` |
| SyntaxError | |
### Symbol resolution errors ### Symbol resolution errors
+7 -3
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 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. Also, it helps keep Lyng core small and focused.
@@ -23,7 +23,7 @@ dependencies {
implementation("net.sergeych:lyngio:0.0.1-SNAPSHOT") 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 ```kotlin
repositories { 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`. 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 ```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 scope: Scope = Scope.new()
val installed: Boolean = createFs(PermitAllAccessPolicy, scope) val installed: Boolean = createFs(PermitAllAccessPolicy, scope)
// installed == true on first registration in this ImportManager, false on repeats // installed == true on first registration in this ImportManager, false on repeats
+136
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
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) | ❌ |
+5 -7
View File
@@ -226,13 +226,11 @@ Future work: introduce thread‑safe pooling (e.g., per‑thread pools or confin
Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules: Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules:
1. Closure frame locals/arguments 1. **Current frame locals and arguments**: Variables defined within the current closure execution.
2. Captured receiver instance/class members 2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment").
3. Closure ancestry locals + each frame’s `this` members (cycle‑safe) 3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`), following MRO and respecting visibility.
4. Caller `this` members 4. **Caller environment**: Falls back to the calling context (e.g., the caller's `this` or local variables).
5. Caller ancestry locals + each frame’s `this` members (cycle‑safe) 5. **Global/Module fallbacks**: Final check for module-level constants and global functions.
6. Module pseudo‑symbols (e.g., `__PACKAGE__`)
7. Direct module/global fallback (nearest `ModuleScope` and its parent/root)
Implications: Implications:
- Outer locals (e.g., `counter`) stay visible across suspension points. - Outer locals (e.g., `counter`) stay visible across suspension points.
+86
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`.
+63
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)
+6 -9
View File
@@ -8,16 +8,13 @@ Name lookup across nested scopes and closures can accidentally form recursive re
## Resolution order in ClosureScope ## Resolution order in ClosureScope
When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order: When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order:
1. Closure frame locals and arguments 1. **Current frame locals and arguments**: Variables defined within the current closure execution.
2. Captured receiver (`closureScope.thisObj`) instance/class members 2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment").
3. Closure ancestry locals + each frame’s `thisObj` members (cycle‑safe) 3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`). This includes both instance fields/methods and class-level static members, following the MRO (C3) and respecting visibility rules (private members are only visible if the closure was defined in their class).
4. Caller `this` members 4. **Caller environment**: If not found lexically, it falls back to the calling context (e.g., the DSL's `this` or the caller's local variables).
5. Caller ancestry locals + each frame’s `thisObj` members (cycle‑safe) 5. **Global/Module fallbacks**: Final check for module-level constants and global functions.
6. Module pseudo‑symbols (e.g., `__PACKAGE__`) from the nearest `ModuleScope`
7. Direct module/global fallback (nearest `ModuleScope` and its parent/root scope)
8. Final fallback: base local/parent lookup for the current frame
This preserves intuitive visibility (locals → captured receiver → closure chain → caller members → caller chain → module/root) while preventing infinite recursion between scope types. This ensures that closures primarily interact with their defining environment (lexical capture) while still being able to participate in DSL-style calling contexts.
## Use raw‑chain helpers for ancestry walks ## Use raw‑chain helpers for ancestry walks
When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`: When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`:
+36 -11
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) - [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) - [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) - [Testing and Assertions](Testing.md)
- [time](time.md) and [parallelism](parallelism.md) - [time](time.md) and [parallelism](parallelism.md)
- [parallelism] - multithreaded code, coroutines, etc. - [parallelism] - multithreaded code, coroutines, etc.
@@ -32,6 +32,15 @@ any block also returns it's last expression:
} }
>>> 6 >>> 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`: If you don't want block to return anything, use `void`:
fn voidFunction() { fn voidFunction() {
@@ -179,14 +188,13 @@ Note that assignment operator returns rvalue, it can't be assigned.
## Modifying arithmetics ## Modifying arithmetics
There is a set of assigning operations: `+=`, `-=`, `*=`, `/=` and even `%=`. 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 var x = null
assert( 25 == (x*=5) ) x ?= 10
assert( 25 == x) assertEquals(10, x)
assert( 24 == (x-=1) ) x ?= 20
assert( 12 == (x/=2) ) assertEquals(10, x)
x
>>> 12
Notice the parentheses here: the assignment has low priority! Notice the parentheses here: the assignment has low priority!
@@ -239,6 +247,13 @@ There is also "elvis operator", null-coalesce infix operator '?:' that returns r
null ?: "nothing" null ?: "nothing"
>>> "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 ## Utility functions
The following functions simplify nullable values processing and The following functions simplify nullable values processing and
@@ -298,6 +313,16 @@ It works much like `also`, but is executed in the context of the source object:
assertEquals(p, Point(2,3)) assertEquals(p, Point(2,3))
>>> void >>> 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 ## run
Executes a block after it returning the value passed by the block. for example, can be used with elvis operator: Executes a block after it returning the value passed by the block. for example, can be used with elvis operator:
@@ -1469,7 +1494,7 @@ Typical set of String functions includes:
| s1 += s2 | self-modifying concatenation | | s1 += s2 | self-modifying concatenation |
| toReal() | attempts to parse string as a Real value | | toReal() | attempts to parse string as a Real value |
| toInt() | parse string to Int value | | toInt() | parse string to Int value |
| characters() | create [List] of characters (1) | | characters | create [List] of characters (1) |
| encodeUtf8() | returns [Buffer] with characters encoded to utf8 | | encodeUtf8() | returns [Buffer] with characters encoded to utf8 |
| matches(re) | matches the regular expression (2) | | matches(re) | matches the regular expression (2) |
| | | | | |
@@ -1519,7 +1544,7 @@ See [math functions](math.md). Other general purpose functions are:
| flow {} | create flow sequence, see [parallelism] | | flow {} | create flow sequence, see [parallelism] |
| delay, launch, yield | see [parallelism] | | delay, launch, yield | see [parallelism] |
| cached(builder) | [Lazy evaluation with `cached`](#lazy-evaluation-with-cached) | | 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) (1)
: `fn` is optional lambda returning string message to add to exception string. : `fn` is optional lambda returning string message to add to exception string.
@@ -1546,7 +1571,7 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
[Range]: Range.md [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 [string formatting]: https://github.com/sergeych/mp_stools?tab=readme-ov-file#sprintf-syntax-summary
-27
View File
@@ -1,27 +0,0 @@
# Wasm generation hang in wasmJs browser tests
## Summary
The wasmJs browser test runner hung after commit 5f819dc. The root cause was invalid WebAssembly generated by the Kotlin/Wasm backend when certain compiler paths emitted suspend lambdas for `Statement` execution. The invalid module failed to instantiate in the browser, and Karma kept the browser connected but never ran tests.
## Symptoms
- `:lynglib:wasmJsBrowserTest` hangs indefinitely in ChromeHeadless.
- `:lynglib:wasmJsNodeTest` fails with a WebAssembly compile error similar to:
- `struct.set expected type (ref null XXXX), found global.get of type (ref null YYYY)`
- The failing function name in the wasm name section looks like:
- `net.sergeych.lyng.$invokeCOROUTINE$.doResume`
## Root cause
The delegation/var-declaration changes introduced compiler-generated suspend lambdas inside `Statement` construction (e.g., `statement { ... }` wrappers). Kotlin/Wasm generates extra coroutine state for those suspend lambdas, which in this case produced invalid wasm IR (mismatched GC reference types). The browser loader then waits forever because the module fails to instantiate.
## Fix
Avoid suspend-lambda `Statement` construction in compiler code paths. Replace `statement { ... }` and other anonymous suspend lambdas with explicit `object : Statement()` implementations and move logic into `override suspend fun execute(...)`. This keeps the resulting wasm IR valid while preserving behavior.
## Where it was fixed
- `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt`
- `lynglib/src/commonMain/kotlin/net/sergeych/lyng/Scope.kt`
## Verification
- `./gradlew :lynglib:wasmJsNodeTest --info`
- `./gradlew :lynglib:wasmJsBrowserTest --info`
Both tests finish quickly after the change.
+48
View File
@@ -105,6 +105,22 @@ object Config {
Config.show() 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 ### 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. A powerful new delegation system allows `val`, `var`, and `fun` members to delegate their logic to other objects using the `by` keyword.
@@ -123,6 +139,35 @@ var name by Observable("initial") { n, old, new ->
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. 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 ## Tooling and Infrastructure
### CLI: Formatting Command ### CLI: Formatting Command
@@ -138,3 +183,6 @@ lyng fmt --check MyFile.lyng # Check if file needs formatting
Experimental lightweight autocompletion is now available in the IntelliJ plugin. It features type-aware member suggestions and inheritance-aware completion. Experimental lightweight autocompletion is now available in the IntelliJ plugin. It features type-aware member suggestions and inheritance-aware completion.
You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**. You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**.
### Kotlin API: Exception Handling
The `Obj.getLyngExceptionMessageWithStackTrace()` extension method has been added to simplify retrieving detailed error information from Lyng exception objects in Kotlin. Additionally, `getLyngExceptionMessage()` and `raiseAsExecutionError()` now accept an optional `Scope`, making it easier to use them when a scope is not immediately available.
@@ -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_]+)?" } { "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": { "mapLiterals": {
"patterns": [ "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]*" } ] }, "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|interface)\\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" } } } ] }, "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)\\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" } ] }, "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)\\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*\\()" } ] }, "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": "[!?]" } ] }, "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": ":" } ] } "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": ":" } ] }
+2
View File
@@ -45,6 +45,8 @@ dependencies {
// Tests for IntelliJ Platform fixtures rely on JUnit 3/4 API (junit.framework.TestCase) // 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 // Add JUnit 4 which contains the JUnit 3 compatibility classes used by BasePlatformTestCase/UsefulTestCase
testImplementation("junit:junit:4.13.2") testImplementation("junit:junit:4.13.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.10.2")
testImplementation("org.opentest4j:opentest4j:1.3.0")
} }
intellij { intellij {
@@ -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"))
}
}
@@ -25,9 +25,6 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile 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.Source
import net.sergeych.lyng.binding.Binder import net.sergeych.lyng.binding.Binder
import net.sergeych.lyng.binding.SymbolKind 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.SimpleLyngHighlighter
import net.sergeych.lyng.highlight.offsetOf import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.highlight.LyngHighlighterColors 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.* import net.sergeych.lyng.miniast.*
/** /**
@@ -43,7 +40,7 @@ import net.sergeych.lyng.miniast.*
* and applies semantic highlighting comparable with the web highlighter. * and applies semantic highlighting comparable with the web highlighter.
*/ */
class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, LyngExternalAnnotator.Result>() { 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 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) 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? { override fun collectInformation(file: PsiFile): Input? {
val doc: Document = file.viewProvider.document ?: return null val doc: Document = file.viewProvider.document ?: return null
val cached = file.getUserData(CACHE_KEY) val cached = file.getUserData(CACHE_KEY)
// Fast fix (1): reuse cached spans only if they were computed for the same modification stamp val combinedStamp = LyngAstManager.getCombinedStamp(file)
val prev = if (cached != null && cached.modStamp == doc.modificationStamp) cached.spans else null
return Input(doc.text, doc.modificationStamp, prev) 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? { override fun doAnnotate(collectedInfo: Input?): Result? {
if (collectedInfo == null) return null if (collectedInfo == null) return null
ProgressManager.checkCanceled() ProgressManager.checkCanceled()
val text = collectedInfo.text val text = collectedInfo.text
// Build Mini-AST using the same mechanism as web highlighter
val sink = MiniAstBuilder() // Use LyngAstManager to get the (potentially merged) Mini-AST
val source = Source("<ide>", text) val mini = LyngAstManager.getMiniAst(collectedInfo.file)
try { ?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList())
// 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)
}
ProgressManager.checkCanceled() 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) 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) { 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) 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 // Declarations
for (d in mini.declarations) { mini.declarations.forEach { d ->
if (d.nameStart.source != source) return@forEach
when (d) { when (d) {
is MiniFunDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.FUNCTION_DECLARATION) is MiniFunDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.FUNCTION_DECLARATION)
is MiniClassDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE) 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 // Imports: each segment as namespace/path
for (imp in mini.imports) { mini.imports.forEach { imp ->
for (seg in imp.segments) putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE) if (imp.range.start.source != source) return@forEach
imp.segments.forEach { seg -> putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE) }
} }
// Parameters // Parameters
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) { mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn ->
for (p in fn.params) putName(p.nameStart, p.name, LyngHighlighterColors.PARAMETER) 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) // Type name segments (including generics base & args)
fun addTypeSegments(t: MiniTypeRef?) { fun addTypeSegments(t: MiniTypeRef?) {
when (t) { when (t) {
is MiniTypeName -> t.segments.forEach { seg -> is MiniTypeName -> t.segments.forEach { seg ->
if (seg.range.start.source != source) return@forEach
val s = source.offsetOf(seg.range.start) val s = source.offsetOf(seg.range.start)
putRange(s, (s + seg.name.length).coerceAtMost(text.length), LyngHighlighterColors.TYPE) putRange(s, (s + seg.name.length).coerceAtMost(text.length), LyngHighlighterColors.TYPE)
} }
@@ -148,12 +140,14 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
addTypeSegments(t.returnType) addTypeSegments(t.returnType)
} }
is MiniTypeVar -> { /* name is in range; could be highlighted as TYPE as well */ 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 -> {} null -> {}
} }
} }
for (d in mini.declarations) { mini.declarations.forEach { d ->
if (d.nameStart.source != source) return@forEach
when (d) { when (d) {
is MiniFunDecl -> { is MiniFunDecl -> {
addTypeSegments(d.returnType) addTypeSegments(d.returnType)
@@ -180,7 +174,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Map declaration ranges to avoid duplicating them as usages // Map declaration ranges to avoid duplicating them as usages
val declKeys = HashSet<Pair<Int, Int>>(binding.symbols.size * 2) 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) { fun keyForKind(k: SymbolKind) = when (k) {
SymbolKind.Function -> LyngHighlighterColors.FUNCTION SymbolKind.Function -> LyngHighlighterColors.FUNCTION
@@ -193,13 +187,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
// Track covered ranges to not override later heuristics // Track covered ranges to not override later heuristics
val covered = HashSet<Pair<Int, Int>>() val covered = HashSet<Pair<Int, Int>>()
for (ref in binding.references) { binding.references.forEach { ref ->
val key = ref.start to ref.end val key = ref.start to ref.end
if (declKeys.contains(key)) continue if (!declKeys.contains(key)) {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } ?: continue val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
val color = keyForKind(sym.kind) if (sym != null) {
putRange(ref.start, ref.end, color) val color = keyForKind(sym.kind)
covered += key putRange(ref.start, ref.end, color)
covered += key
}
}
} }
// Heuristics on top of binder: function call-sites and simple name-based roles // 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() } 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 // Build simple name -> role map for top-level vals/vars and parameters
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8) val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
for (d in mini.declarations) when (d) { mini.declarations.forEach { d ->
is MiniValDecl -> nameRole[d.name] = if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE when (d) {
is MiniFunDecl -> d.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER } is MiniValDecl -> nameRole[d.name] =
else -> {} 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) { tokens.forEach { s ->
val start = s.range.start if (s.kind == HighlightKind.Identifier) {
val end = s.range.endExclusive val start = s.range.start
val key = start to end val end = s.range.endExclusive
if (key in covered || key in declKeys) continue val key = start to end
if (key !in covered && key !in declKeys) {
// Call-site detection first so it wins over var/param role // Call-site detection first so it wins over var/param role
if (isFollowedByParenOrBlock(end)) { if (isFollowedByParenOrBlock(end)) {
putRange(start, end, LyngHighlighterColors.FUNCTION) putRange(start, end, LyngHighlighterColors.FUNCTION)
covered += key covered += key
continue } else {
} // Simple role by known names
val ident = try {
// Simple role by known names text.substring(start, end)
val ident = try { text.substring(start, end) } catch (_: Throwable) { null } } catch (_: Throwable) {
if (ident != null) { null
val roleKey = nameRole[ident] }
if (roleKey != null) { if (ident != null) {
putRange(start, end, roleKey) val roleKey = nameRole[ident]
covered += key 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 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 { run {
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() } val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
for (s in tokens) if (s.kind == HighlightKind.Label) { tokens.forEach { s ->
val start = s.range.start if (s.kind == HighlightKind.Label) {
val end = s.range.endExclusive val start = s.range.start
if (start in 0..end && end <= text.length && start < end) { val end = s.range.endExclusive
val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null } if (start in 0..end && end <= text.length && start < end) {
if (lexeme != null && lexeme.startsWith("@")) { val lexeme = try {
putRange(start, end, LyngHighlighterColors.ANNOTATION) 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 // Map Enum constants from token highlighter to IDEA enum constant color
run { run {
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() } val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
for (s in tokens) if (s.kind == HighlightKind.EnumConstant) { tokens.forEach { s ->
val start = s.range.start if (s.kind == HighlightKind.EnumConstant) {
val end = s.range.endExclusive val start = s.range.start
if (start in 0..end && end <= text.length && start < end) { val end = s.range.endExclusive
putRange(start, end, LyngHighlighterColors.ENUM_CONSTANT) 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>() val idRanges = mutableSetOf<IntRange>()
try { try {
val binding = Binder.bind(text, mini) val binding = Binder.bind(text, mini)
for (sym in binding.symbols) { binding.symbols.forEach { sym ->
val s = sym.declStart; val e = sym.declEnd val s = sym.declStart
val e = sym.declEnd
if (s in 0..e && e <= text.length && s < e) idRanges += (s until e) if (s in 0..e && e <= text.length && s < e) idRanges += (s until e)
} }
for (ref in binding.references) { binding.references.forEach { ref ->
val s = ref.start; val e = ref.end val s = ref.start
val e = ref.end
if (s in 0..e && e <= text.length && s < e) idRanges += (s until e) if (s in 0..e && e <= text.length && s < e) idRanges += (s until e)
} }
} catch (_: Throwable) { } catch (_: Throwable) {
@@ -308,12 +335,13 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
override fun apply(file: PsiFile, annotationResult: Result?, holder: AnnotationHolder) { override fun apply(file: PsiFile, annotationResult: Result?, holder: AnnotationHolder) {
if (annotationResult == null) return if (annotationResult == null) return
// Skip if cache is up-to-date // Skip if cache is up-to-date
val doc = file.viewProvider.document val combinedStamp = LyngAstManager.getCombinedStamp(file)
val currentStamp = doc?.modificationStamp
val cached = file.getUserData(CACHE_KEY) 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) file.putUserData(CACHE_KEY, result)
val doc = file.viewProvider.document
// Store spell index for spell/grammar engines to consume (suspend until ready) // Store spell index for spell/grammar engines to consume (suspend until ready)
val ids = result.spellIdentifiers.map { TextRange(it.first, it.last + 1) } val ids = result.spellIdentifiers.map { TextRange(it.first, it.last + 1) }
val coms = result.spellComments.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 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. * Make the error highlight a bit wider than a single character so it is easier to see and click.
* Strategy: * Strategy:
@@ -30,7 +30,6 @@ import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.util.ProcessingContext import com.intellij.util.ProcessingContext
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.idea.LyngLanguage import net.sergeych.lyng.idea.LyngLanguage
import net.sergeych.lyng.idea.highlight.LyngTokenTypes import net.sergeych.lyng.idea.highlight.LyngTokenTypes
import net.sergeych.lyng.idea.settings.LyngFormatterSettings 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 // Delegate computation to the shared engine to keep behavior in sync with tests
val engineItems = try { val engineItems = try {
runBlocking { CompletionEngineLight.completeSuspend(text, caret) } runBlocking { CompletionEngineLight.completeSuspend(text, caret, mini, binding) }
} catch (t: Throwable) { } catch (t: Throwable) {
if (DEBUG_COMPLETION) log.warn("[LYNG_DEBUG] Engine completion failed: ${t.message}") if (DEBUG_COMPLETION) log.warn("[LYNG_DEBUG] Engine completion failed: ${t.message}")
emptyList() 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 // Render engine items
for (ci in engineItems) { for (ci in engineItems) {
val builder = when (ci.kind) { val builder = when (ci.kind) {
@@ -167,7 +161,7 @@ class LyngCompletionContributor : CompletionContributor() {
Kind.Enum -> LookupElementBuilder.create(ci.name) Kind.Enum -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Enum) .withIcon(AllIcons.Nodes.Enum)
Kind.Value -> LookupElementBuilder.create(ci.name) 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 } .let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
Kind.Field -> LookupElementBuilder.create(ci.name) Kind.Field -> LookupElementBuilder.create(ci.name)
.withIcon(AllIcons.Nodes.Field) .withIcon(AllIcons.Nodes.Field)
@@ -191,33 +185,51 @@ class LyngCompletionContributor : CompletionContributor() {
?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini) ?: DocLookupUtils.guessReturnClassAcrossKnownCallees(text, memberDotPos, imported, mini)
?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini) ?: DocLookupUtils.guessReceiverClass(text, memberDotPos, imported, mini)
if (!inferredClass.isNullOrBlank()) { if (!inferredClass.isNullOrBlank()) {
val ext = BuiltinDocRegistry.extensionMemberNamesFor(inferredClass) val ext = DocLookupUtils.collectExtensionMemberNames(imported, inferredClass, mini)
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${'$'}{ext}") if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${ext}")
for (name in ext) { for (name in ext) {
if (existing.contains(name)) continue if (existing.contains(name)) continue
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini) val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, inferredClass, name, mini)
if (resolved != null) { if (resolved != null) {
when (val member = resolved.second) { val m = resolved.second
val builder = when (m) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name } val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType) val ret = typeOf(m.returnType)
val builder = LookupElementBuilder.create(name) LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .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) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
emit(builder)
existing.add(name)
} }
is MiniMemberValDecl -> { is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name) LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field) .withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true) .withTypeText(typeOf(m.type), true)
emit(builder) }
existing.add(name) 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 { } else {
// Fallback: emit simple method name without detailed types // Fallback: emit simple method name without detailed types
val builder = LookupElementBuilder.create(name) val builder = LookupElementBuilder.create(name)
@@ -458,30 +470,47 @@ class LyngCompletionContributor : CompletionContributor() {
for (name in common) { for (name in common) {
if (name in already) continue if (name in already) continue
// Try resolve across classes first to get types/params; if it fails, emit a synthetic safe suggestion. // 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) { if (resolved != null) {
val member = resolved.second val member = resolved.second
when (member) { val builder = when (member) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name } val params = member.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType) val ret = typeOf(member.returnType)
val builder = LookupElementBuilder.create(name) LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .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) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
emit(builder)
already.add(name)
} }
is MiniMemberValDecl -> { is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name) LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Field) .withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true) .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 { } else {
// Synthetic fallback: method without detailed params/types to improve UX in absence of docs // Synthetic fallback: method without detailed params/types to improve UX in absence of docs
val isProperty = name in setOf("size", "length") val isProperty = name in setOf("size", "length")
@@ -508,31 +537,48 @@ class LyngCompletionContributor : CompletionContributor() {
for (name in ext) { for (name in ext) {
if (already.contains(name)) continue if (already.contains(name)) continue
// Try to resolve full signature via registry first to get params and return type // 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) { if (resolved != null) {
when (val member = resolved.second) { val m = resolved.second
val builder = when (m) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name } val params = m.params.joinToString(", ") { it.name }
val ret = typeOf(member.returnType) val ret = typeOf(m.returnType)
val builder = LookupElementBuilder.create(name) LookupElementBuilder.create(name)
.withIcon(AllIcons.Nodes.Method) .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) .withTypeText(ret, true)
.withInsertHandler(ParenInsertHandler) .withInsertHandler(ParenInsertHandler)
emit(builder)
already.add(name)
continue
} }
is MiniMemberValDecl -> { is MiniMemberValDecl -> {
val builder = LookupElementBuilder.create(name) LookupElementBuilder.create(name)
.withIcon(if (member.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field) .withIcon(if (m.mutable) AllIcons.Nodes.Variable else AllIcons.Nodes.Field)
.withTypeText(typeOf(member.type), true) .withTypeText(typeOf(m.type), true)
emit(builder) }
already.add(name) is MiniValDecl -> {
continue 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 // Fallback: emit without detailed types if we couldn't resolve
val builder = LookupElementBuilder.create(name) 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) // Lenient textual import extractor (duplicated from QuickDoc privately)
private fun extractImportsFromText(text: String): List<String> { private fun extractImportsFromText(text: String): List<String> {
val result = LinkedHashSet<String>() val result = LinkedHashSet<String>()
@@ -24,13 +24,9 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile 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.highlight.offsetOf
import net.sergeych.lyng.idea.LyngLanguage 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.idea.util.TextCtx
import net.sergeych.lyng.miniast.* import net.sergeych.lyng.miniast.*
@@ -69,80 +65,93 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val ident = text.substring(idRange.startOffset, idRange.endOffset) 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}") 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. // 1. Get merged mini-AST from Manager (handles local + .lyng.d merged declarations)
val sink = MiniAstBuilder() val mini = LyngAstManager.getMiniAst(file) ?: return null
val provider = IdeLenientImportProvider.create() val miniSource = mini.range.start.source
val src = Source("<ide>", text) val imported = DocLookupUtils.canonicalImportedModules(mini, 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
// Try resolve to: function param at position, function/class/val declaration at position // Try resolve to: function param at position, function/class/val declaration at position
// 1) Use unified declaration detection // 1) Use unified declaration detection
DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) -> DocLookupUtils.findDeclarationAt(mini, offset, ident)?.let { (name, kind) ->
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind") if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: matched declaration '$name' kind=$kind")
// Find the actual declaration object to render // Find the actual declaration object to render
for (d in mini.declarations) { mini.declarations.forEach { d ->
if (d.name == name && source.offsetOf(d.nameStart) <= offset && source.offsetOf(d.nameStart) + d.name.length > offset) { if (d.name == name) {
return renderDeclDoc(d) 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 // Handle members if it was a member
if (d is MiniClassDecl) { if (d is MiniClassDecl) {
for (m in d.members) { d.members.forEach { m ->
if (m.name == name && source.offsetOf(m.nameStart) <= offset && source.offsetOf(m.nameStart) + m.name.length > offset) { if (m.name == name) {
return when (m) { val s: Int = miniSource.offsetOf(m.nameStart)
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m) if (s <= offset && s + m.name.length > offset) {
is MiniMemberValDecl -> renderMemberValDoc(d.name, m) return when (m) {
is MiniInitDecl -> null is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
else -> null
}
} }
} }
} }
for (cf in d.ctorFields) { d.ctorFields.forEach { cf ->
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) { if (cf.name == name) {
// Render as a member val val s: Int = miniSource.offsetOf(cf.nameStart)
val mv = MiniMemberValDecl( if (s <= offset && s + cf.name.length > offset) {
range = MiniRange(cf.nameStart, cf.nameStart), // dummy // Render as a member val
name = cf.name, val mv = MiniMemberValDecl(
mutable = cf.mutable, range = MiniRange(cf.nameStart, cf.nameStart), // dummy
type = cf.type, name = cf.name,
doc = null, mutable = cf.mutable,
nameStart = cf.nameStart type = cf.type,
) initRange = null,
return renderMemberValDoc(d.name, mv) doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(d.name, mv)
}
} }
} }
for (cf in d.classFields) { d.classFields.forEach { cf ->
if (cf.name == name && source.offsetOf(cf.nameStart) <= offset && source.offsetOf(cf.nameStart) + cf.name.length > offset) { if (cf.name == name) {
// Render as a member val val s: Int = miniSource.offsetOf(cf.nameStart)
val mv = MiniMemberValDecl( if (s <= offset && s + cf.name.length > offset) {
range = MiniRange(cf.nameStart, cf.nameStart), // dummy // Render as a member val
name = cf.name, val mv = MiniMemberValDecl(
mutable = cf.mutable, range = MiniRange(cf.nameStart, cf.nameStart), // dummy
type = cf.type, name = cf.name,
doc = null, mutable = cf.mutable,
nameStart = cf.nameStart type = cf.type,
) initRange = null,
return renderMemberValDoc(d.name, mv) doc = null,
nameStart = cf.nameStart
)
return renderMemberValDoc(d.name, mv)
}
} }
} }
} }
if (d is MiniEnumDecl) { if (d is MiniEnumDecl) {
if (d.entries.contains(name) && offset >= source.offsetOf(d.range.start) && offset <= source.offsetOf(d.range.end)) { if (d.entries.contains(name)) {
// For enum constant, we don't have detailed docs in MiniAst yet, but we can render a title val s: Int = miniSource.offsetOf(d.range.start)
return "<div class='doc-title'>enum constant ${d.name}.${name}</div>" 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 // Check parameters
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) { mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn ->
for (p in fn.params) { fn.params.forEach { p ->
if (p.name == name && source.offsetOf(p.nameStart) <= offset && source.offsetOf(p.nameStart) + p.name.length > offset) { if (p.name == name) {
return renderParamDoc(fn, p) 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 } val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) { if (sym != null) {
// Find local declaration that matches this symbol // Find local declaration that matches this symbol
val ds = mini.declarations.firstOrNull { decl -> var dsFound: MiniDecl? = null
val s = source.offsetOf(decl.nameStart) mini.declarations.forEach { decl ->
decl.name == sym.name && s == sym.declStart 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 // Check parameters
for (fn in mini.declarations.filterIsInstance<MiniFunDecl>()) { mini.declarations.filterIsInstance<MiniFunDecl>().forEach { fn ->
for (p in fn.params) { fn.params.forEach { p ->
val s = source.offsetOf(p.nameStart) if (p.name == sym.name) {
if (p.name == sym.name && s == sym.declStart) { val sOffset: Int = miniSource.offsetOf(p.nameStart)
return renderParamDoc(fn, p) if (sOffset == sym.declStart) {
return renderParamDoc(fn, p)
}
} }
} }
} }
// Check class members (fields/functions) // Check class members (fields/functions)
for (cls in mini.declarations.filterIsInstance<MiniClassDecl>()) { mini.declarations.filterIsInstance<MiniClassDecl>().forEach { cls ->
for (m in cls.members) { cls.members.forEach { m ->
val s = source.offsetOf(m.nameStart) if (m.name == sym.name) {
if (m.name == sym.name && s == sym.declStart) { val sOffset: Int = miniSource.offsetOf(m.nameStart)
return when (m) { if (sOffset == sym.declStart) {
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m) return when (m) {
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m) is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
is MiniInitDecl -> null is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
else -> null
}
} }
} }
} }
for (cf in cls.ctorFields) { cls.ctorFields.forEach { cf ->
val s = source.offsetOf(cf.nameStart) if (cf.name == sym.name) {
if (cf.name == sym.name && s == sym.declStart) { val sOffset: Int = miniSource.offsetOf(cf.nameStart)
// Render as a member val if (sOffset == sym.declStart) {
val mv = MiniMemberValDecl( // Render as a member val
range = MiniRange(cf.nameStart, cf.nameStart), // dummy val mv = MiniMemberValDecl(
name = cf.name, range = MiniRange(cf.nameStart, cf.nameStart), // dummy
mutable = cf.mutable, name = cf.name,
type = cf.type, mutable = cf.mutable,
doc = null, type = cf.type,
nameStart = cf.nameStart initRange = null,
) doc = null,
return renderMemberValDoc(cls.name, mv) nameStart = cf.nameStart
)
return renderMemberValDoc(cls.name, mv)
}
} }
} }
for (cf in cls.classFields) { cls.classFields.forEach { cf ->
val s = source.offsetOf(cf.nameStart) if (cf.name == sym.name) {
if (cf.name == sym.name && s == sym.declStart) { val sOffset: Int = miniSource.offsetOf(cf.nameStart)
// Render as a member val if (sOffset == sym.declStart) {
val mv = MiniMemberValDecl( // Render as a member val
range = MiniRange(cf.nameStart, cf.nameStart), // dummy val mv = MiniMemberValDecl(
name = cf.name, range = MiniRange(cf.nameStart, cf.nameStart), // dummy
mutable = cf.mutable, name = cf.name,
type = cf.type, mutable = cf.mutable,
doc = null, type = cf.type,
nameStart = cf.nameStart initRange = null,
) doc = null,
return renderMemberValDoc(cls.name, mv) nameStart = cf.nameStart
)
return renderMemberValDoc(cls.name, mv)
}
} }
} }
} }
@@ -260,35 +284,39 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} else null } else null
} }
else -> { else -> {
val guessed = DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules) DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, importedModules)
if (guessed != null) guessed ?: DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini)
else { ?: run {
// handle this@Type or as Type // handle this@Type or as Type
val i2 = TextCtx.prevNonWs(text, dotPos - 1) val i2 = TextCtx.prevNonWs(text, dotPos - 1)
if (i2 >= 0) { if (i2 >= 0) {
val identRange = TextCtx.wordRangeAt(text, i2 + 1) val identRange = TextCtx.wordRangeAt(text, i2 + 1)
if (identRange != null) { if (identRange != null) {
val id = text.substring(identRange.startOffset, identRange.endOffset) val id = text.substring(identRange.startOffset, identRange.endOffset)
val k = TextCtx.prevNonWs(text, identRange.startOffset - 1) 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())) { if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
id id
} else if (k >= 0 && text[k] == '@') { } else if (k >= 0 && text[k] == '@') {
val k2 = TextCtx.prevNonWs(text, k - 1) val k2 = TextCtx.prevNonWs(text, k - 1)
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") id else null if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") id else null
} else null
} 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) { 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}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null 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}") 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 // 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 { mini.declarations.firstOrNull { it.name == ident }?.let {
log.info("[LYNG_DEBUG] QuickDoc: fallback by name '${it.name}' kind=${it::class.simpleName}") 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) // 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) { if (arity != null && chosen.params.size != arity && matches.size > 1) {
return renderOverloads(ident, matches) return renderOverloads(ident, matches)
} }
return renderDeclDoc(chosen) return renderDeclDoc(chosen, text, mini, imported)
} }
// Also allow values/consts // 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 // And classes/enums
docs.filterIsInstance<MiniClassDecl>().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) } 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 // Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs
if (ident == "println" || ident == "print") { if (ident == "println" || ident == "print") {
@@ -339,12 +367,16 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
val lhs = previousWordBefore(text, idRange.startOffset) val lhs = previousWordBefore(text, idRange.startOffset)
if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) { if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) {
val className = text.substring(lhs.startOffset, lhs.endOffset) 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}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Inheritance resolved $className.$ident to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null 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 { } else {
@@ -355,15 +387,19 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
if (dotPos != null) { if (dotPos != null) {
val guessed = when { val guessed = when {
looksLikeListLiteralBefore(text, dotPos) -> "List" looksLikeListLiteralBefore(text, dotPos) -> "List"
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules) else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini)
} }
if (guessed != null) { 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}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Heuristic '$guessed.$ident' resolved via inheritance to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null 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 { } else {
@@ -371,19 +407,23 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
run { run {
val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex") val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex")
for (c in candidates) { 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}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Candidate '$c.$ident' resolved via inheritance to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null 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) // As a last resort try aggregated String members (extensions from stdlib text)
run { run {
val classes = DocLookupUtils.aggregateClasses(importedModules) val classes = DocLookupUtils.aggregateClasses(importedModules, mini)
val stringCls = classes["String"] val stringCls = classes["String"]
val m = stringCls?.members?.firstOrNull { it.name == ident } val m = stringCls?.members?.firstOrNull { it.name == ident }
if (m != null) { if (m != null) {
@@ -396,12 +436,16 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
} }
// Search across classes; prefer Iterable, then Iterator, then List for common ops // 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}") if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Cross-class '$ident' resolved to $owner.${member.name}")
return when (member) { return when (member) {
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member) is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
is MiniMemberValDecl -> renderMemberValDoc(owner, member) is MiniMemberValDecl -> renderMemberValDoc(owner, member)
is MiniInitDecl -> null 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) 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) { val title = when (d) {
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}" is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
is MiniClassDecl -> "class ${d.name}" is MiniClassDecl -> "class ${d.name}"
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }" 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 // Show full detailed documentation, not just the summary
val raw = d.doc?.raw val raw = d.doc?.raw
@@ -487,7 +535,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
private fun renderMemberValDoc(className: String, m: MiniMemberValDecl): String { 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 kind = if (m.mutable) "var" else "val"
val staticStr = if (m.isStatic) "static " else "" val staticStr = if (m.isStatic) "static " else ""
val title = "${staticStr}${kind} $className.${m.name}${ts}" val title = "${staticStr}${kind} $className.${m.name}${ts}"
@@ -508,7 +556,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
is MiniFunctionType -> ": (..) -> ..${if (t.nullable) "?" else ""}" is MiniFunctionType -> ": (..) -> ..${if (t.nullable) "?" else ""}"
is MiniTypeVar -> ": ${t.name}${if (t.nullable) "?" else ""}" is MiniTypeVar -> ": ${t.name}${if (t.nullable) "?" else ""}"
null -> "" null -> ": Object?"
} }
private fun signatureOf(fn: MiniFunDecl): String { private fun signatureOf(fn: MiniFunDecl): String {
@@ -609,10 +657,14 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
} }
private fun previousWordBefore(text: String, offset: Int): TextRange? { 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) var i = (offset - 1).coerceAtLeast(0)
// first, move left past spaces // skip trailing spaces
while (i > 0 && text[i].isWhitespace()) i-- 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 // remember position to check for dot between words
val end = i + 1 val end = i + 1
// now find the start of the identifier // now find the start of the identifier
@@ -43,10 +43,15 @@ class LyngColorSettingsPage : ColorSettingsPage {
} }
var counter = 0 var counter = 0
counter = counter + 1 outer@ while (counter < 10) {
if (counter == 5) return@outer
counter = counter + 1
}
""".trimIndent() """.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( override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf(
AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD), AttributesDescriptor("Keyword", LyngHighlighterColors.KEYWORD),
@@ -58,6 +63,7 @@ class LyngColorSettingsPage : ColorSettingsPage {
AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT), AttributesDescriptor("Punctuation", LyngHighlighterColors.PUNCT),
// Semantic // Semantic
AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION), AttributesDescriptor("Annotation (semantic)", LyngHighlighterColors.ANNOTATION),
AttributesDescriptor("Label (semantic)", LyngHighlighterColors.LABEL),
AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE), AttributesDescriptor("Variable (semantic)", LyngHighlighterColors.VARIABLE),
AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE), AttributesDescriptor("Value (semantic)", LyngHighlighterColors.VALUE),
AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION), AttributesDescriptor("Function (semantic)", LyngHighlighterColors.FUNCTION),
@@ -82,4 +82,9 @@ object LyngHighlighterColors {
val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey( val ENUM_CONSTANT: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
"LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD "LYNG_ENUM_CONSTANT", DefaultLanguageHighlighterColors.STATIC_FIELD
) )
// Labels (label@ or @label used as exit target)
val LABEL: TextAttributesKey = TextAttributesKey.createTextAttributesKey(
"LYNG_LABEL", DefaultLanguageHighlighterColors.LABEL
)
} }
@@ -33,10 +33,10 @@ class LyngLexer : LexerBase() {
private val keywords = setOf( private val keywords = setOf(
"fun", "val", "var", "class", "interface", "type", "import", "as", "fun", "val", "var", "class", "interface", "type", "import", "as",
"abstract", "closed", "override", "abstract", "closed", "override", "static", "extern", "open", "private", "protected",
"if", "else", "for", "while", "return", "true", "false", "null", "if", "else", "for", "while", "return", "true", "false", "null",
"when", "in", "is", "break", "continue", "try", "catch", "finally", "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) { override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
@@ -133,10 +133,24 @@ class LyngLexer : LexerBase() {
return 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()) { if (ch.isIdentifierStart()) {
i++ i++
while (i < endOffset && buffer[i].isIdentifierPart()) i++ while (i < endOffset && buffer[i].isIdentifierPart()) i++
if (i < endOffset && buffer[i] == '@') {
i++
myTokenEnd = i
myTokenType = LyngTokenTypes.LABEL
return
}
myTokenEnd = i myTokenEnd = i
val text = buffer.subSequence(myTokenStart, myTokenEnd).toString() val text = buffer.subSequence(myTokenStart, myTokenEnd).toString()
myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER myTokenType = if (text in keywords) LyngTokenTypes.KEYWORD else LyngTokenTypes.IDENTIFIER
@@ -160,5 +174,5 @@ class LyngLexer : LexerBase() {
private fun Char.isDigit(): Boolean = this in '0'..'9' private fun Char.isDigit(): Boolean = this in '0'..'9'
private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter() private fun Char.isIdentifierStart(): Boolean = this == '_' || this.isLetter()
private fun Char.isIdentifierPart(): Boolean = this.isIdentifierStart() || this.isDigit() 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('(', ')', '{', '}', '[', ']', '.', ',', ';', ':', '+', '-', '*', '/', '%', '=', '<', '>', '!', '?', '&', '|', '^', '~', '@')
} }
@@ -33,6 +33,7 @@ class LyngSyntaxHighlighter : SyntaxHighlighter {
LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT) LyngTokenTypes.BLOCK_COMMENT -> pack(LyngHighlighterColors.BLOCK_COMMENT)
LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT) LyngTokenTypes.PUNCT -> pack(LyngHighlighterColors.PUNCT)
LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER) LyngTokenTypes.IDENTIFIER -> pack(LyngHighlighterColors.IDENTIFIER)
LyngTokenTypes.LABEL -> pack(LyngHighlighterColors.LABEL)
else -> emptyArray() else -> emptyArray()
} }
@@ -29,6 +29,7 @@ object LyngTokenTypes {
val NUMBER = LyngTokenType("NUMBER") val NUMBER = LyngTokenType("NUMBER")
val KEYWORD = LyngTokenType("KEYWORD") val KEYWORD = LyngTokenType("KEYWORD")
val IDENTIFIER = LyngTokenType("IDENTIFIER") val IDENTIFIER = LyngTokenType("IDENTIFIER")
val LABEL = LyngTokenType("LABEL")
val PUNCT = LyngTokenType("PUNCT") val PUNCT = LyngTokenType("PUNCT")
val BAD_CHAR = LyngTokenType("BAD_CHAR") val BAD_CHAR = LyngTokenType("BAD_CHAR")
} }
@@ -63,6 +63,10 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
is MiniMemberFunDecl -> "Function" is MiniMemberFunDecl -> "Function"
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value" is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
is MiniInitDecl -> "Initializer" 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))) results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
} }
@@ -17,8 +17,10 @@
package net.sergeych.lyng.idea.util package net.sergeych.lyng.idea.util
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Source import net.sergeych.lyng.Source
@@ -32,53 +34,100 @@ object LyngAstManager {
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache") private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp") private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
fun getMiniAst(file: PsiFile): MiniScript? { fun getMiniAst(file: PsiFile): MiniScript? = runReadAction {
val doc = file.viewProvider.document ?: return null val vFile = file.virtualFile ?: return@runReadAction null
val stamp = doc.modificationStamp val combinedStamp = getCombinedStamp(file)
val prevStamp = file.getUserData(STAMP_KEY) val prevStamp = file.getUserData(STAMP_KEY)
val cached = file.getUserData(MINI_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 sink = MiniAstBuilder()
val built = try { val built = try {
val provider = IdeLenientImportProvider.create() val provider = IdeLenientImportProvider.create()
val src = Source(file.name, text) val src = Source(file.name, text)
runBlocking { Compiler.compileWithMini(src, provider, sink) } 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) { } catch (_: Throwable) {
sink.build() sink.build()
} }
if (built != null) { if (built != null) {
file.putUserData(MINI_KEY, built) file.putUserData(MINI_KEY, built)
file.putUserData(STAMP_KEY, stamp) file.putUserData(STAMP_KEY, combinedStamp)
// Invalidate binding too // Invalidate binding too
file.putUserData(BINDING_KEY, null) file.putUserData(BINDING_KEY, null)
} }
return built built
} }
fun getBinding(file: PsiFile): BindingSnapshot? { fun getCombinedStamp(file: PsiFile): Long = runReadAction {
val doc = file.viewProvider.document ?: return null var combinedStamp = file.viewProvider.modificationStamp
val stamp = doc.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 prevStamp = file.getUserData(STAMP_KEY)
val cached = file.getUserData(BINDING_KEY) val cached = file.getUserData(BINDING_KEY)
if (cached != null && prevStamp != null && prevStamp == stamp) return cached if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached
val mini = getMiniAst(file) ?: return null val mini = getMiniAst(file) ?: return@runReadAction null
val text = doc.text val text = file.viewProvider.contents.toString()
val binding = try { val binding = try {
Binder.bind(text, mini) Binder.bind(text, mini)
} catch (_: Throwable) { } catch (_: Throwable) {
null null
} }
if (binding != null) { if (binding != null) {
file.putUserData(BINDING_KEY, binding) 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
} }
} }
@@ -19,7 +19,7 @@
<!-- Open-ended compatibility: 2024.3+ (build 243 and newer) --> <!-- Open-ended compatibility: 2024.3+ (build 243 and newer) -->
<idea-version since-build="243"/> <idea-version since-build="243"/>
<id>net.sergeych.lyng.idea</id> <id>net.sergeych.lyng.idea</id>
<name>Lyng Language Support</name> <name>Lyng</name>
<vendor email="real.sergeych@gmail.com">Sergey Chernov</vendor> <vendor email="real.sergeych@gmail.com">Sergey Chernov</vendor>
<description> <description>
@@ -43,6 +43,7 @@
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
<!-- Language and file type --> <!-- Language and file type -->
<fileType implementationClass="net.sergeych.lyng.idea.LyngFileType" name="Lyng" extensions="lyng" fieldName="INSTANCE" language="Lyng"/> <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 --> <!-- Minimal parser/PSI to fully wire editor services for the language -->
<lang.parserDefinition language="Lyng" implementationClass="net.sergeych.lyng.idea.psi.LyngParserDefinition"/> <lang.parserDefinition language="Lyng" implementationClass="net.sergeych.lyng.idea.psi.LyngParserDefinition"/>
+13 -14
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins { plugins {
@@ -53,11 +52,11 @@ kotlin {
browser() browser()
nodejs() nodejs()
} }
@OptIn(ExperimentalWasmDsl::class) // @OptIn(ExperimentalWasmDsl::class)
wasmJs() { // wasmJs() {
browser() // browser()
nodejs() // nodejs()
} // }
// Keep expect/actual warning suppressed consistently with other modules // Keep expect/actual warning suppressed consistently with other modules
targets.configureEach { targets.configureEach {
@@ -94,13 +93,13 @@ kotlin {
implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}") implementation("com.squareup.okio:okio-nodefilesystem:${libs.versions.okioVersion.get()}")
} }
} }
// For Wasm we use in-memory VFS for now // // For Wasm we use in-memory VFS for now
val wasmJsMain by getting { // val wasmJsMain by getting {
dependencies { // dependencies {
api(libs.okio) // api(libs.okio)
implementation(libs.okio.fakefilesystem) // implementation(libs.okio.fakefilesystem)
} // }
} // }
} }
} }
@@ -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")
}
@@ -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)
}
@@ -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 * Filesystem module builtin docs registration, located in lyngio so core library
* does not depend on external packages. The IDEA plugin (and any other tooling) * does not depend on external packages. The IDEA plugin (and any other tooling)
@@ -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)
}
}
@@ -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
@@ -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)
}
@@ -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")
}
@@ -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")
}
@@ -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()
}
}
@@ -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)}")
}
}
@@ -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())
}
}
@@ -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
@@ -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))
}
}
@@ -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"))
}
}
@@ -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
@@ -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))
}
}
@@ -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))
}
}
@@ -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
+1 -1
View File
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" group = "net.sergeych"
version = "1.1.0-SNAPSHOT" version = "1.2.0-SNAPSHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below // Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
@@ -23,7 +23,7 @@ actual object ArgBuilderProvider {
private val tl = object : ThreadLocal<AndroidArgsBuilder>() { private val tl = object : ThreadLocal<AndroidArgsBuilder>() {
override fun initialValue(): AndroidArgsBuilder = AndroidArgsBuilder() override fun initialValue(): AndroidArgsBuilder = AndroidArgsBuilder()
} }
actual fun acquire(): ArgsBuilder = tl.get() actual fun acquire(): ArgsBuilder = tl.get()!!
} }
private class AndroidArgsBuilder : ArgsBuilder { private class AndroidArgsBuilder : ArgsBuilder {
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
package net.sergeych.lyng package net.sergeych.lyng
import net.sergeych.lyng.obj.Obj
import net.sergeych.lyng.obj.ObjClass
import net.sergeych.lyng.obj.ObjRecord import net.sergeych.lyng.obj.ObjRecord
/** /**
@@ -38,133 +40,34 @@ class ClosureScope(val callScope: Scope, val closureScope: Scope) :
} }
override fun get(name: String): ObjRecord? { override fun get(name: String): ObjRecord? {
// Fast-path built-ins
if (name == "this") return thisObj.asReadonly if (name == "this") return thisObj.asReadonly
// Priority: // 1. Current frame locals (parameters, local variables)
// 1) Locals and arguments declared in this lambda frame (including values defined before suspension) tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
// 2) Instance/class members of the captured receiver (`closureScope.thisObj`)
// 3) Symbols from the captured closure scope chain (locals/parents), ignoring nested ClosureScope overrides
// 4) Instance members of the caller's `this` (e.g., FlowBuilder.emit)
// 5) Symbols from the caller chain (locals/parents), ignoring nested ClosureScope overrides
// 6) Special fallback for module pseudo-symbols (e.g., __PACKAGE__)
// 1) Locals/arguments in this closure frame // 2. Lexical environment (captured locals from entire ancestry)
super.objects[name]?.let { return it } closureScope.chainLookupIgnoreClosure(name, followClosure = true, caller = currentClassCtx)?.let { return it }
super.localBindings[name]?.let { return it }
// 1a) Priority: if we are in a class context, prefer our own private members to support // 3. Lexical this members (captured receiver)
// non-virtual private dispatch. This prevents a subclass from accidentally val receiver = thisObj
// capturing a private member call from a base class. val effectiveClass = receiver as? ObjClass ?: receiver.objClass
// We only return non-field/non-property members (methods) here; fields must for (cls in effectiveClass.mro) {
// be resolved via instance storage in priority 2. val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
currentClassCtx?.let { ctx -> if (rec != null && !rec.isAbstract) {
ctx.members[name]?.let { rec -> if (canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) {
if (rec.visibility == Visibility.Private && return rec.copy(receiver = receiver)
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
} }
} }
} }
// Finally, root object fallback
findExtension(closureScope.thisObj.objClass, name)?.let { return it } Obj.rootObjectType.members[name]?.let { rec ->
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) { if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
// Return only non-field/non-property members (methods) from class-level records. return rec.copy(receiver = receiver)
// 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 // 4. Call environment (caller locals, caller this, and global fallback)
closureScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it } return callScope.get(name)
// 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)) {
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)
callScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
// 6) Module pseudo-symbols (e.g., __PACKAGE__) — walk caller ancestry and query ModuleScope directly
if (name.startsWith("__")) {
var s: Scope? = callScope
val visited = HashSet<Long>(4)
while (s != null) {
if (!visited.add(s.frameId)) break
if (s is ModuleScope) return s.get(name)
s = s.parent
}
}
// 7) Direct module/global fallback: try to locate nearest ModuleScope and check its own locals
fun lookupInModuleAncestry(from: Scope): ObjRecord? {
var s: Scope? = from
val visited = HashSet<Long>(4)
while (s != null) {
if (!visited.add(s.frameId)) break
if (s is ModuleScope) {
s.objects[name]?.let { return it }
s.localBindings[name]?.let { return it }
// check immediate parent (root scope) locals/constants for globals like `delay`
val p = s.parent
if (p != null) {
p.objects[name]?.let { return it }
p.localBindings[name]?.let { return it }
p.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
}
return null
}
s = s.parent
}
return null
}
lookupInModuleAncestry(closureScope)?.let { return it }
lookupInModuleAncestry(callScope)?.let { return it }
// 8) Global root scope constants/functions (e.g., delay, yield) via current import provider
runCatching { this.currentImportProvider.rootScope.objects[name] }.getOrNull()?.let { return it }
// Final safe fallback: base scope lookup from this frame walking raw parents
return baseGetIgnoreClosure(name)
} }
} }
@@ -20,7 +20,7 @@ package net.sergeych.lyng
sealed class CodeContext { sealed class CodeContext {
class Module(@Suppress("unused") val packageName: String?): CodeContext() class Module(@Suppress("unused") val packageName: String?): CodeContext()
class Function(val name: 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>() val pendingInitializations = mutableMapOf<String, Pos>()
} }
} }
File diff suppressed because it is too large. Load diff
@@ -34,18 +34,34 @@ class CompilerContext(val tokens: List<Token>) {
} }
var currentIndex = 0 var currentIndex = 0
private var pendingGT = 0
fun hasNext() = currentIndex < tokens.size fun hasNext() = currentIndex < tokens.size || pendingGT > 0
fun hasPrevious() = currentIndex > 0 fun hasPrevious() = currentIndex > 0
fun next() = fun next(): Token {
if (currentIndex < tokens.size) tokens[currentIndex++] 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) 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) { fun restorePos(pos: Int) {
currentIndex = pos currentIndex = pos shr 2
pendingGT = pos and 3
} }
fun ensureLabelIsValid(pos: Pos, label: String) { fun ensureLabelIsValid(pos: Pos, label: String) {
@@ -106,12 +122,13 @@ class CompilerContext(val tokens: List<Token>) {
errorMessage: String = "expected ${tokenType.name}", errorMessage: String = "expected ${tokenType.name}",
isOptional: Boolean = false isOptional: Boolean = false
): Boolean { ): Boolean {
val pos = savePos()
val t = next() val t = next()
return if (t.type != tokenType) { return if (t.type != tokenType) {
if (!isOptional) { if (!isOptional) {
throw ScriptError(t.pos, errorMessage) throw ScriptError(t.pos, errorMessage)
} else { } else {
previous() restorePos(pos)
false false
} }
} else true } else true
@@ -122,20 +139,25 @@ class CompilerContext(val tokens: List<Token>) {
* @return true if token was found and skipped * @return true if token was found and skipped
*/ */
fun skipNextIf(vararg types: Token.Type): Boolean { fun skipNextIf(vararg types: Token.Type): Boolean {
val pos = savePos()
val t = next() val t = next()
return if (t.type in types) return if (t.type in types)
true true
else { else {
previous() restorePos(pos)
false false
} }
} }
@Suppress("unused") @Suppress("unused")
fun skipTokens(vararg tokenTypes: Token.Type) { 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 { fun nextNonWhitespace(): Token {
@@ -151,10 +173,11 @@ class CompilerContext(val tokens: List<Token>) {
* @return next non-whitespace token without extracting it from tokens list * @return next non-whitespace token without extracting it from tokens list
*/ */
fun peekNextNonWhitespace(): Token { fun peekNextNonWhitespace(): Token {
val saved = savePos()
while (true) { while (true) {
val t = next() val t = next()
if (t.type !in wstokens) { if (t.type !in wstokens) {
previous() restorePos(saved)
return t return t
} }
} }
@@ -162,12 +185,13 @@ class CompilerContext(val tokens: List<Token>) {
inline fun ifNextIs(typeId: Token.Type, f: (Token) -> Unit): Boolean { inline fun ifNextIs(typeId: Token.Type, f: (Token) -> Unit): Boolean {
val pos = savePos()
val t = next() val t = next()
return if (t.type == typeId) { return if (t.type == typeId) {
f(t) f(t)
true true
} else { } else {
previous() restorePos(pos)
false false
} }
} }
@@ -325,6 +325,7 @@ private class Parser(fromPos: Pos) {
'?' -> { '?' -> {
when (currentChar) { 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.ELVIS) } '?' -> { pos.advance(); Token("??", from, Token.Type.ELVIS) }
'.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) } '.' -> { pos.advance(); Token("?.", from, Token.Type.NULL_COALESCE) }
@@ -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()
@@ -106,10 +106,20 @@ open class Scope(
* intertwined closure frames. They traverse the plain parent chain and consult only locals * intertwined closure frames. They traverse the plain parent chain and consult only locals
* and bindings of each frame. Instance/class member fallback must be decided by the caller. * and bindings of each frame. Instance/class member fallback must be decided by the caller.
*/ */
private fun tryGetLocalRecord(s: Scope, name: String, caller: net.sergeych.lyng.obj.ObjClass?): ObjRecord? { internal fun tryGetLocalRecord(s: Scope, name: String, caller: net.sergeych.lyng.obj.ObjClass?): ObjRecord? {
caller?.let { ctx ->
s.objects["${ctx.className}::$name"]?.let { rec ->
if (rec.visibility == Visibility.Private) return rec
}
}
s.objects[name]?.let { rec -> s.objects[name]?.let { rec ->
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
} }
caller?.let { ctx ->
s.localBindings["${ctx.className}::$name"]?.let { rec ->
if (rec.visibility == Visibility.Private) return rec
}
}
s.localBindings[name]?.let { rec -> s.localBindings[name]?.let { rec ->
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
} }
@@ -120,14 +130,15 @@ open class Scope(
return null return null
} }
internal fun chainLookupIgnoreClosure(name: String): ObjRecord? { internal fun chainLookupIgnoreClosure(name: String, followClosure: Boolean = false, caller: net.sergeych.lyng.obj.ObjClass? = null): ObjRecord? {
var s: Scope? = this var s: Scope? = this
// use frameId to detect unexpected structural cycles in the parent chain // use frameId to detect unexpected structural cycles in the parent chain
val visited = HashSet<Long>(4) val visited = HashSet<Long>(4)
val effectiveCaller = caller ?: currentClassCtx
while (s != null) { while (s != null) {
if (!visited.add(s.frameId)) return null if (!visited.add(s.frameId)) return null
tryGetLocalRecord(s, name, currentClassCtx)?.let { return it } tryGetLocalRecord(s, name, effectiveCaller)?.let { return it }
s = s.parent s = if (followClosure && s is ClosureScope) s.closureScope else s.parent
} }
return null return null
} }
@@ -169,7 +180,7 @@ open class Scope(
* This completely avoids invoking overridden `get` implementations, preventing * This completely avoids invoking overridden `get` implementations, preventing
* ping-pong recursion between `ClosureScope` frames. * ping-pong recursion between `ClosureScope` frames.
*/ */
internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx): ObjRecord? { internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx, followClosure: Boolean = false): ObjRecord? {
var s: Scope? = this var s: Scope? = this
val visited = HashSet<Long>(4) val visited = HashSet<Long>(4)
while (s != null) { while (s != null) {
@@ -185,7 +196,7 @@ open class Scope(
} else return rec } else return rec
} }
} }
s = s.parent s = if (followClosure && s is ClosureScope) s.closureScope else s.parent
} }
return null return null
} }
@@ -277,16 +288,22 @@ open class Scope(
raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name")) raiseError(ObjSymbolNotDefinedException(this, "symbol is not defined: $name"))
fun raiseError(message: String): Nothing { 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 { 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") @Suppress("unused")
fun raiseNotFound(message: String = "not found"): Nothing { 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 { inline fun <reified T : Obj> requiredArg(index: Int): T {
@@ -314,39 +331,53 @@ open class Scope(
inline fun <reified T : Obj> thisAs(): T { inline fun <reified T : Obj> thisAs(): T {
var s: Scope? = this var s: Scope? = this
do { while (s != null) {
val t = s!!.thisObj val t = s.thisObj
if (t is T) return t if (t is T) return t
s = s.parent s = s.parent
} while (s != null) }
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}") raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
} }
internal val objects = mutableMapOf<String, ObjRecord>() internal val objects = mutableMapOf<String, ObjRecord>()
open operator fun get(name: String): ObjRecord? = open operator fun get(name: String): ObjRecord? {
if (name == "this") thisObj.asReadonly if (name == "this") return thisObj.asReadonly
else {
// Prefer direct locals/bindings declared in this frame // 1. Prefer direct locals/bindings declared in this frame
(objects[name]?.let { rec -> tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
} val p = parent
// Then, check known local bindings in this frame (helps after suspension)
?: localBindings[name]?.let { rec -> // 2. If we share thisObj with parent, delegate to parent to maintain
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null // "locals shadow members" priority across the this-context.
} if (p != null && p.thisObj === thisObj) {
// Walk up ancestry return p.get(name)
?: parent?.get(name)
// Finally, fallback to class members on thisObj
?: thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
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
}
)
} }
// 3. Otherwise, we are the "primary" scope for this thisObj (or have no parent),
// so check members of thisObj before walking up to a different this-context.
val receiver = thisObj
val effectiveClass = receiver as? ObjClass ?: receiver.objClass
for (cls in effectiveClass.mro) {
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null && !rec.isAbstract) {
if (canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx)) {
return rec.copy(receiver = receiver)
}
}
}
// Finally, root object fallback
Obj.rootObjectType.members[name]?.let { rec ->
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) {
return rec.copy(receiver = receiver)
}
}
// 4. Finally, walk up ancestry to a scope with a different thisObj context
return p?.get(name)
}
// Slot fast-path API // Slot fast-path API
fun getSlotRecord(index: Int): ObjRecord = slots[index] fun getSlotRecord(index: Int): ObjRecord = slots[index]
fun setSlotValue(index: Int, newValue: Obj) { fun setSlotValue(index: Int, newValue: Obj) {
@@ -374,6 +405,7 @@ open class Scope(
// that could interact badly with the new parent and produce a cycle. // that could interact badly with the new parent and produce a cycle.
this.parent = null this.parent = null
this.skipScopeCreation = false this.skipScopeCreation = false
this.currentClassCtx = parent?.currentClassCtx
// fresh identity for PIC caches // fresh identity for PIC caches
this.frameId = nextFrameId() this.frameId = nextFrameId()
// clear locals and slot maps // clear locals and slot maps
@@ -382,6 +414,7 @@ open class Scope(
nameToSlot.clear() nameToSlot.clear()
localBindings.clear() localBindings.clear()
extensions.clear() extensions.clear()
this.currentClassCtx = parent?.currentClassCtx
// Now safe to validate and re-parent // Now safe to validate and re-parent
ensureNoCycle(parent) ensureNoCycle(parent)
this.parent = parent this.parent = parent
@@ -622,31 +655,34 @@ open class Scope(
} }
suspend fun resolve(rec: ObjRecord, name: String): Obj { suspend fun resolve(rec: ObjRecord, name: String): Obj {
if (rec.type == ObjRecord.Type.Delegated) { val receiver = rec.receiver ?: thisObj
val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate") return receiver.resolveRecord(this, rec, name, rec.declaringClass).value
val th = if (thisObj === ObjVoid) ObjNull else thisObj
if (del.objClass.getInstanceMemberOrNull("getValue") == null) {
return 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))
}
}
}
return del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name)))
}
return rec.value
} }
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) { suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
if (rec.type == ObjRecord.Type.Delegated) { if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate") val receiver = rec.receiver ?: thisObj
val th = if (thisObj === ObjVoid) ObjNull else thisObj val del = rec.delegate ?: run {
if (receiver is ObjInstance) {
(receiver as ObjInstance).writeField(this, name, newValue)
return
}
raiseError("Internal error: delegated property $name has no delegate")
}
val th = if (receiver === ObjVoid) ObjNull else receiver
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue)) del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
return return
} }
if (rec.value is ObjProperty) {
(rec.value as ObjProperty).callSetter(this, rec.receiver ?: thisObj, newValue, rec.declaringClass)
return
}
// If it's a member (explicitly tracked by receiver or declaringClass), use writeField.
// Important: locals have receiver == null and declaringClass == null (enforced in addItem).
if (rec.receiver != null || (rec.declaringClass != null && (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property))) {
(rec.receiver ?: thisObj).writeField(this, name, newValue)
return
}
if (!rec.isMutable && rec.value !== ObjUnset) raiseIllegalAssignment("can't reassign val $name") if (!rec.isMutable && rec.value !== ObjUnset) raiseIllegalAssignment("can't reassign val $name")
rec.value = newValue rec.value = newValue
} }
@@ -248,10 +248,9 @@ class Script(
) )
) )
expectedClass?.let { expectedClass?.let {
if (result !is ObjException) if (!result.isInstanceOf(it)) {
raiseError("Expected $expectedClass, got non-lyng exception $result") val actual = if (result is ObjException) result.exceptionClass else result.objClass
if (result.exceptionClass != expectedClass) { raiseError("Expected $it, got $actual")
raiseError("Expected $expectedClass, got ${result.exceptionClass}")
} }
} }
result result
@@ -294,10 +293,14 @@ class Script(
ObjVoid ObjVoid
} }
// Delay in milliseconds (plain numeric). For time-aware variants use lyng.time.Duration API.
addVoidFn("delay") { addVoidFn("delay") {
val ms = (this.args.firstAndOnly().toDouble()).roundToLong() val a = args.firstAndOnly()
delay(ms) when (a) {
is ObjInt -> delay(a.value)
is ObjReal -> delay((a.value * 1000).roundToLong())
is ObjDuration -> delay(a.duration)
else -> raiseIllegalArgument("Expected Int, Real or Duration, got ${a.inspect(this)}")
}
} }
addConst("Object", rootObjectType) addConst("Object", rootObjectType)
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
package net.sergeych.lyng 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( 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 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) class ImportException(pos: Pos, message: String) : ScriptError(pos, message)
@@ -40,7 +40,7 @@ class Source(val fileName: String, val text: String) {
fun extractPackageName(): String { fun extractPackageName(): String {
for ((n,line) in lines.withIndex()) { for ((n,line) in lines.withIndex()) {
if( line.isBlank() || line.isEmpty() ) if( line.isBlank() )
continue continue
if( line.startsWith("package ") ) if( line.startsWith("package ") )
return line.substring(8).trim() return line.substring(8).trim()
@@ -34,7 +34,7 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA, LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET, COMMA,
SEMICOLON, COLON, SEMICOLON, COLON,
PLUS, MINUS, STAR, SLASH, PERCENT, PLUS, MINUS, STAR, SLASH, PERCENT,
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN,
PLUS2, MINUS2, PLUS2, MINUS2,
IN, NOTIN, IS, NOTIS, BY, IN, NOTIN, IS, NOTIS, BY,
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH, EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
@@ -27,5 +27,6 @@ sealed class TypeDecl(val isNullable:Boolean = false) {
object TypeAny : TypeDecl() object TypeAny : TypeDecl()
object TypeNullableAny : TypeDecl(true) 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)
} }
@@ -42,6 +42,31 @@ object LyngFormatter {
return false 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. */ /** Returns the input with indentation recomputed from scratch, line by line. */
fun reindent(text: String, config: LyngFormatConfig = LyngFormatConfig()): String { fun reindent(text: String, config: LyngFormatConfig = LyngFormatConfig()): String {
// Normalize tabs to spaces globally before any transformation; results must contain no tabs // 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 if (isIf || isElseIf || isElse) return true
// property accessors ending with ) or = // property accessors ending with ) or =
if (isPropertyAccessor(t)) { if (isAccessorRelated(t)) {
return if (t.contains('=')) t.endsWith('=') else t.endsWith(')') return if (t.contains('=')) t.endsWith('=') else t.endsWith(')')
} }
return false return false
@@ -168,7 +193,8 @@ object LyngFormatter {
} }
val newBlockLevel = blockLevel val newBlockLevel = blockLevel
if (newBlockLevel > oldBlockLevel) { 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) { repeat(newBlockLevel - oldBlockLevel) {
extraIndents.add(addedThisLine) extraIndents.add(addedThisLine)
} }
@@ -186,7 +212,8 @@ object LyngFormatter {
val endsWithBrace = code.trimEnd().endsWith("{") val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) { if (!endsWithBrace && isControlHeaderNoBrace(code)) {
// It's another header, increment // It's another header, increment
awaitingExtraIndent += if (isAccessor) 2 else 1 val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code))
awaitingExtraIndent += if (isAccessorRelatedLine) 2 else 1
} else { } else {
// It's the body, reset // It's the body, reset
awaitingExtraIndent = 0 awaitingExtraIndent = 0
@@ -195,7 +222,8 @@ object LyngFormatter {
// start awaiting if current line is a control header without '{' // start awaiting if current line is a control header without '{'
val endsWithBrace = code.trimEnd().endsWith("{") val endsWithBrace = code.trimEnd().endsWith("{")
if (!endsWithBrace && isControlHeaderNoBrace(code)) { if (!endsWithBrace && isControlHeaderNoBrace(code)) {
awaitingExtraIndent = if (isAccessor) 2 else 1 val isAccessorRelatedLine = isAccessor || (!inBlockComment && isAccessorRelated(code))
awaitingExtraIndent = if (isAccessorRelatedLine) 2 else 1
} }
} }
@@ -477,7 +505,7 @@ private fun splitIntoParts(
private fun applyMinimalSpacingRules(code: String): String { private fun applyMinimalSpacingRules(code: String): String {
var s = code var s = code
// Ensure space before '(' for control-flow keywords // 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) // 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("\\b(if|for|while)(\\s*\\([^)]*\\))\\s*\\{"), "$1$2 {")
s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {") s = s.replace(Regex("\\belse\\s+if(\\s*\\([^)]*\\))\\s*\\{"), "else if$1 {")
@@ -498,6 +526,8 @@ private fun applyMinimalSpacingRules(code: String): String {
s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {") s = s.replace(Regex("(\\bcatch\\s*\\([^)]*\\))\\s*\\{"), "$1 {")
// Ensure space before '(' for catch parameter // Ensure space before '(' for catch parameter
s = s.replace(Regex("\\bcatch\\("), "catch (") 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)" // Remove spaces just inside parentheses/brackets: "( a )" -> "(a)"
s = s.replace(Regex("\\(\\s+"), "(") s = s.replace(Regex("\\(\\s+"), "(")
// Do not strip leading indentation before a closing bracket/paren on its own line // Do not strip leading indentation before a closing bracket/paren on its own line
@@ -82,7 +82,7 @@ private fun kindOf(type: Type, value: String): HighlightKind? = when (type) {
// operators and symbolic constructs // operators and symbolic constructs
Type.PLUS, Type.MINUS, Type.STAR, Type.SLASH, Type.PERCENT, 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.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.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, Type.DOT, Type.ARROW, Type.EQARROW, Type.QUESTION, Type.COLONCOLON,
@@ -236,6 +236,7 @@ class ClassDocsBuilder internal constructor(private val className: String) {
name = name, name = name,
mutable = mutable, mutable = mutable,
type = type?.toMiniTypeRef(), type = type?.toMiniTypeRef(),
initRange = null,
doc = md, doc = md,
nameStart = Pos.builtIn, nameStart = Pos.builtIn,
isStatic = isStatic, isStatic = isStatic,
@@ -534,6 +535,9 @@ private fun buildStdlibDocs(): List<MiniDecl> {
) )
// Concurrency helpers // 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( mod.funDoc(
name = "launch", name = "launch",
doc = StdlibInlineDocIndex.topFunDoc("launch") ?: "Launch an asynchronous task and return a `Deferred`.", 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") 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) // 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 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 = "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")) 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 // 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 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 = "hasNext", doc = md("hasNext", "Whether another element is available."), returns = type("lyng.Bool"))
method(name = "next", doc = md("next", "Return the next element.")) method(name = "next", doc = md("next", "Return the next element."))
@@ -600,22 +613,22 @@ private fun buildStdlibDocs(): List<MiniDecl> {
} }
// Exceptions and utilities // 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.") 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 = "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")) 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. // 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")) method(name = "re", doc = StdlibInlineDocIndex.methodDoc("String", "re") ?: "Compile this string into a regular expression.", returns = type("lyng.Regex"))
} }
// StackTraceEntry structure // 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. // 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 = "sourceName", doc = "Source (file) name.", type = type("lyng.String"))
field(name = "line", doc = "Line number (1-based).", type = type("lyng.Int")) field(name = "line", doc = "Line number (1-based).", type = type("lyng.Int"))
@@ -23,6 +23,8 @@ package net.sergeych.lyng.miniast
import net.sergeych.lyng.Compiler import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script import net.sergeych.lyng.Script
import net.sergeych.lyng.Source import net.sergeych.lyng.Source
import net.sergeych.lyng.binding.BindingSnapshot
import net.sergeych.lyng.highlight.offsetOf
import net.sergeych.lyng.pacman.ImportProvider import net.sergeych.lyng.pacman.ImportProvider
/** Minimal completion item description (IDE-agnostic). */ /** Minimal completion item description (IDE-agnostic). */
@@ -57,11 +59,11 @@ object CompletionEngineLight {
return completeSuspend(text, idx) 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 // Ensure stdlib Obj*-defined docs (e.g., String methods) are initialized before registry lookup
StdlibDocsBootstrap.ensure() StdlibDocsBootstrap.ensure()
val prefix = prefixAt(text, caret) 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 imported: List<String> = DocLookupUtils.canonicalImportedModules(mini ?: return emptyList(), text)
val cap = 200 val cap = 200
@@ -72,6 +74,10 @@ object CompletionEngineLight {
val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret) val memberDot = DocLookupUtils.findDotLeft(text, word?.first ?: caret)
if (memberDot != null) { if (memberDot != null) {
// 0) Try chained member call return type inference // 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 -> DocLookupUtils.guessReturnClassFromMemberCallBefore(text, memberDot, imported, mini)?.let { cls ->
offerMembersAdd(out, prefix, imported, cls, mini) offerMembersAdd(out, prefix, imported, cls, mini)
return out return out
@@ -87,7 +93,7 @@ object CompletionEngineLight {
return out return out
} }
// 1) Receiver inference fallback // 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) offerMembersAdd(out, prefix, imported, cls, mini)
return out return out
} }
@@ -96,18 +102,25 @@ object CompletionEngineLight {
} }
// Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical // Global identifiers: params > local decls > imported > stdlib; Functions > Classes > Values; alphabetical
mini?.let { m -> offerParamsInScope(out, prefix, mini, text, caret)
val decls = m.declarations
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() } val locals = DocLookupUtils.extractLocalsAt(text, caret)
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() } for (name in locals) {
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() } if (name.startsWith(prefix, true)) {
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() } out.add(CompletionItem(name, Kind.Value))
funs.forEach { offerDeclAdd(out, prefix, it) } }
classes.forEach { offerDeclAdd(out, prefix, it) }
enums.forEach { offerDeclAdd(out, prefix, it) }
vals.forEach { offerDeclAdd(out, prefix, it) }
} }
val decls = mini.declarations
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
funs.forEach { offerDeclAdd(out, prefix, it) }
classes.forEach { offerDeclAdd(out, prefix, it) }
enums.forEach { offerDeclAdd(out, prefix, it) }
vals.forEach { offerDeclAdd(out, prefix, it) }
// Imported and builtin // Imported and builtin
val (nonStd, std) = imported.partition { it != "lyng.stdlib" } val (nonStd, std) = imported.partition { it != "lyng.stdlib" }
val order = nonStd + std val order = nonStd + std
@@ -132,6 +145,74 @@ object CompletionEngineLight {
// --- Emission helpers --- // --- 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) { private fun offerDeclAdd(out: MutableList<CompletionItem>, prefix: String, d: MiniDecl) {
fun add(ci: CompletionItem) { if (ci.name.startsWith(prefix, true)) out += ci } fun add(ci: CompletionItem) { if (ci.name.startsWith(prefix, true)) out += ci }
when (d) { when (d) {
@@ -192,7 +273,7 @@ object CompletionEngineLight {
val chosen = variants.asSequence() val chosen = variants.asSequence()
.filterIsInstance<MiniMemberValDecl>() .filterIsInstance<MiniMemberValDecl>()
.firstOrNull { it.type != null } ?: rep .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 if (ci.name.startsWith(prefix, true)) out += ci
} }
is MiniInitDecl -> {} is MiniInitDecl -> {}
@@ -203,33 +284,38 @@ object CompletionEngineLight {
emitGroup(directMap) emitGroup(directMap)
emitGroup(inheritedMap) 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 { run {
val already = (directMap.keys + inheritedMap.keys).toMutableSet() val already = (directMap.keys + inheritedMap.keys).toMutableSet()
val ext = BuiltinDocRegistry.extensionMemberNamesFor(className) val extensions = DocLookupUtils.collectExtensionMemberNames(imported, className, mini)
for (name in ext) { for (name in extensions) {
if (already.contains(name)) continue if (already.contains(name)) continue
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name) val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name, mini)
if (resolved != null) { if (resolved != null) {
when (val member = resolved.second) { val m = resolved.second
val ci = when (m) {
is MiniMemberFunDecl -> { is MiniMemberFunDecl -> {
val params = member.params.joinToString(", ") { it.name } val params = m.params.joinToString(", ") { it.name }
val ci = CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(member.returnType)) CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType))
if (ci.name.startsWith(prefix, true)) out += ci
already.add(name)
} }
is MiniMemberValDecl -> { is MiniFunDecl -> {
val ci = CompletionItem(name, Kind.Field, typeText = typeOf(member.type)) val params = m.params.joinToString(", ") { it.name }
if (ci.name.startsWith(prefix, true)) out += ci CompletionItem(name, Kind.Method, tailText = "(${params})", typeText = typeOf(m.returnType))
already.add(name)
} }
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 { } else {
// Fallback: emit simple method name without detailed types
val ci = CompletionItem(name, Kind.Method, tailText = "()", typeText = null) val ci = CompletionItem(name, Kind.Method, tailText = "()", typeText = null)
if (ci.name.startsWith(prefix, true)) out += ci if (ci.name.startsWith(prefix, true)) {
already.add(name) out += ci
already.add(name)
}
} }
} }
} }
@@ -91,14 +91,15 @@ object DocLookupUtils {
return null 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 if (mini == null) return null
val src = mini.range.start.source 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) { 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) { 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 is MiniFunDecl -> d.returnType
else -> null else -> null
} }
@@ -106,25 +107,27 @@ object DocLookupUtils {
if (d is MiniFunDecl) { if (d is MiniFunDecl) {
for (p in d.params) { 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) { if (d is MiniClassDecl) {
for (m in d.members) { 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) { return when (m) {
is MiniMemberFunDecl -> m.returnType 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 else -> null
} }
} }
} }
for (cf in d.ctorFields) { 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) { 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() 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> { fun extractImportsFromText(text: String): List<String> {
val result = LinkedHashSet<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) 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) { for ((name, list) in buckets) {
result[name] = mergeClassDecls(name, list) result[name] = mergeClassDecls(name, list)
} }
// Root object alias
if (result.containsKey("Obj") && !result.containsKey("Any")) {
result["Any"] = result["Obj"]!!
}
return result 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) val classes = aggregateClasses(importedModules, localMini)
fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniMemberDecl>? { fun dfs(name: String, visited: MutableSet<String>): Pair<String, MiniNamedDecl>? {
val cls = classes[name] ?: return null
cls.members.firstOrNull { it.name == member }?.let { return name to it }
if (!visited.add(name)) return null if (!visited.add(name)) return null
for (baseName in cls.bases) { val cls = classes[name]
dfs(baseName, visited)?.let { return it } 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 null
} }
return dfs(className, mutableSetOf()) 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) val classes = aggregateClasses(importedModules, localMini)
// Preferred order for ambiguous common ops // Preferred order for ambiguous common ops
val preference = listOf("Iterable", "Iterator", "List") val preference = listOf("Iterable", "Iterator", "List")
@@ -301,6 +360,12 @@ object DocLookupUtils {
if (mini == null) return null if (mini == null) return null
val i = prevNonWs(text, dotPos - 1) val i = prevNonWs(text, dotPos - 1)
if (i < 0) return null 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 wordRange = wordRangeAt(text, i + 1) ?: return null
val ident = text.substring(wordRange.first, wordRange.second) val ident = text.substring(wordRange.first, wordRange.second)
@@ -310,14 +375,14 @@ object DocLookupUtils {
if (ref != null) { if (ref != null) {
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId } val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
if (sym != null) { 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 } simpleClassNameOf(type)?.let { return it }
} }
} else { } else {
// Check if it's a declaration (e.g. static access to a class) // 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 } val sym = binding.symbols.firstOrNull { it.declStart == wordRange.first && it.name == ident }
if (sym != null) { 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 } simpleClassNameOf(type)?.let { return it }
// if it's a class/enum, return its name directly // 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 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) // 1) Declarations in current file (val/var/fun/class/enum), prioritized by proximity
val d = mini.declarations.firstOrNull { it.name == ident } 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) { if (d != null) {
return when (d) { return when (d) {
is MiniClassDecl -> d.name is MiniClassDecl -> d.name
is MiniEnumDecl -> 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) 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. // 3) Recursive chaining: Base.ident.
val dotBefore = findDotLeft(text, wordRange.first) val dotBefore = findDotLeft(text, wordRange.first)
if (dotBefore != null) { if (dotBefore != null) {
@@ -353,7 +425,7 @@ object DocLookupUtils {
if (resolved != null) { if (resolved != null) {
val rt = when (val m = resolved.second) { val rt = when (val m = resolved.second) {
is MiniMemberFunDecl -> m.returnType is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type is MiniMemberValDecl -> m.type ?: inferTypeRefFromInitRange(m.initRange, m.nameStart, text, imported, mini)
else -> null else -> null
} }
return simpleClassNameOf(rt) return simpleClassNameOf(rt)
@@ -368,6 +440,43 @@ object DocLookupUtils {
return null 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? { fun guessReturnClassFromMemberCallBeforeMini(mini: MiniScript?, text: String, dotPos: Int, imported: List<String>, binding: BindingSnapshot? = null): String? {
if (mini == null) return null if (mini == null) return null
var i = prevNonWs(text, dotPos - 1) var i = prevNonWs(text, dotPos - 1)
@@ -420,7 +529,9 @@ object DocLookupUtils {
val rt = when (m) { val rt = when (m) {
is MiniMemberFunDecl -> m.returnType is MiniMemberFunDecl -> m.returnType
is MiniMemberValDecl -> m.type is MiniMemberValDecl -> m.type
is MiniInitDecl -> null is MiniFunDecl -> m.returnType
is MiniValDecl -> m.type
else -> null
} }
simpleClassNameOf(rt) simpleClassNameOf(rt)
} }
@@ -430,6 +541,7 @@ object DocLookupUtils {
fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> { fun scanLocalClassMembersFromText(mini: MiniScript, text: String, cls: MiniClassDecl): Map<String, ScannedSig> {
val src = mini.range.start.source val src = mini.range.start.source
if (cls.nameStart.source != src) return emptyMap()
val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start) val start = src.offsetOf(cls.bodyRange?.start ?: cls.range.start)
val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length) val end = src.offsetOf(cls.bodyRange?.end ?: cls.range.end).coerceAtMost(text.length)
if (start !in 0..end) return emptyMap() if (start !in 0..end) return emptyMap()
@@ -454,13 +566,25 @@ object DocLookupUtils {
return map 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 } guessClassFromCallBefore(text, dotPos, imported, mini)?.let { return it }
var i = prevNonWs(text, dotPos - 1) var i = prevNonWs(text, dotPos - 1)
if (i >= 0) { if (i >= 0) {
when (text[i]) { when (text[i]) {
'"' -> return "String" '"' -> 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" '}' -> return "Dict"
')' -> { ')' -> {
// Parenthesized expression: walk back to matching '(' and inspect the inner expression // Parenthesized expression: walk back to matching '(' and inspect the inner expression
@@ -563,7 +687,9 @@ object DocLookupUtils {
val ret = when (member) { val ret = when (member) {
is MiniMemberFunDecl -> member.returnType is MiniMemberFunDecl -> member.returnType
is MiniMemberValDecl -> member.type is MiniMemberValDecl -> member.type
is MiniInitDecl -> null is MiniFunDecl -> member.returnType
is MiniValDecl -> member.type
else -> null
} }
return simpleClassNameOf(ret) return simpleClassNameOf(ret)
} }
@@ -626,11 +752,216 @@ object DocLookupUtils {
val ret = when (member) { val ret = when (member) {
is MiniMemberFunDecl -> member.returnType is MiniMemberFunDecl -> member.returnType
is MiniMemberValDecl -> member.type is MiniMemberValDecl -> member.type
is MiniInitDecl -> null is MiniFunDecl -> member.returnType
is MiniValDecl -> member.type
else -> null
} }
return simpleClassNameOf(ret) 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) { fun simpleClassNameOf(t: MiniTypeRef?): String? = when (t) {
null -> null null -> null
is MiniTypeName -> t.segments.lastOrNull()?.name is MiniTypeName -> t.segments.lastOrNull()?.name
@@ -666,13 +997,13 @@ object DocLookupUtils {
fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl { fun enumToSyntheticClass(en: MiniEnumDecl): MiniClassDecl {
val staticMembers = mutableListOf<MiniMemberDecl>() val staticMembers = mutableListOf<MiniMemberDecl>()
// entries: List // 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 // valueOf(name: String): Enum
staticMembers.add(MiniMemberFunDecl(en.range, "valueOf", listOf(MiniParam("name", null, en.nameStart)), null, null, en.nameStart, isStatic = true)) 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) // Also add each entry as a static member (const)
for (entry in en.entries) { 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( return MiniClassDecl(
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -145,6 +145,23 @@ fun ObjClass.addClassFnDoc(
} }
} }
fun ObjClass.addPropertyDoc(
name: String,
doc: String,
type: TypeDoc? = null,
visibility: Visibility = Visibility.Public,
moduleName: String? = null,
getter: (suspend Scope.() -> Obj)? = null,
setter: (suspend Scope.(Obj) -> Unit)? = null
) {
addProperty(name, getter, setter, visibility)
BuiltinDocRegistry.module(moduleName ?: ownerModuleNameFromClassOrUnknown()) {
classDoc(this@addPropertyDoc.className, doc = "") {
field(name = name, doc = doc, type = type, mutable = setter != null)
}
}
}
fun ObjClass.addClassConstDoc( fun ObjClass.addClassConstDoc(
name: String, name: String,
value: Obj, value: Obj,
@@ -81,13 +81,16 @@ data class MiniTypeVar(
) : MiniTypeRef ) : MiniTypeRef
// Script and declarations (lean subset; can be extended later) // Script and declarations (lean subset; can be extended later)
sealed interface MiniDecl : MiniNode { sealed interface MiniNamedDecl : MiniNode {
val name: String val name: String
val doc: MiniDoc? val doc: MiniDoc?
// Start position of the declaration name identifier in source; end can be derived as start + name.length // Start position of the declaration name identifier in source; end can be derived as start + name.length
val nameStart: Pos val nameStart: Pos
val isExtern: Boolean
} }
sealed interface MiniDecl : MiniNamedDecl
data class MiniScript( data class MiniScript(
override val range: MiniRange, override val range: MiniRange,
val declarations: MutableList<MiniDecl> = mutableListOf(), val declarations: MutableList<MiniDecl> = mutableListOf(),
@@ -110,7 +113,8 @@ data class MiniFunDecl(
val body: MiniBlock?, val body: MiniBlock?,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
val receiver: MiniTypeRef? = null val receiver: MiniTypeRef? = null,
override val isExtern: Boolean = false
) : MiniDecl ) : MiniDecl
data class MiniValDecl( data class MiniValDecl(
@@ -121,7 +125,8 @@ data class MiniValDecl(
val initRange: MiniRange?, val initRange: MiniRange?,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
val receiver: MiniTypeRef? = null val receiver: MiniTypeRef? = null,
override val isExtern: Boolean = false
) : MiniDecl ) : MiniDecl
data class MiniClassDecl( data class MiniClassDecl(
@@ -134,7 +139,9 @@ data class MiniClassDecl(
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
// Built-in extension: list of member declarations (functions and fields) // 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 ) : MiniDecl
data class MiniEnumDecl( data class MiniEnumDecl(
@@ -142,7 +149,8 @@ data class MiniEnumDecl(
override val name: String, override val name: String,
val entries: List<String>, val entries: List<String>,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos override val nameStart: Pos,
override val isExtern: Boolean = false
) : MiniDecl ) : MiniDecl
data class MiniCtorField( data class MiniCtorField(
@@ -166,10 +174,7 @@ data class MiniIdentifier(
) : MiniNode ) : MiniNode
// --- Class member declarations (for built-in/registry docs) --- // --- Class member declarations (for built-in/registry docs) ---
sealed interface MiniMemberDecl : MiniNode { sealed interface MiniMemberDecl : MiniNamedDecl {
val name: String
val doc: MiniDoc?
val nameStart: Pos
val isStatic: Boolean val isStatic: Boolean
} }
@@ -181,6 +186,8 @@ data class MiniMemberFunDecl(
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
override val isStatic: Boolean = false, override val isStatic: Boolean = false,
override val isExtern: Boolean = false,
val body: MiniBlock? = null
) : MiniMemberDecl ) : MiniMemberDecl
data class MiniMemberValDecl( data class MiniMemberValDecl(
@@ -188,9 +195,11 @@ data class MiniMemberValDecl(
override val name: String, override val name: String,
val mutable: Boolean, val mutable: Boolean,
val type: MiniTypeRef?, val type: MiniTypeRef?,
val initRange: MiniRange?,
override val doc: MiniDoc?, override val doc: MiniDoc?,
override val nameStart: Pos, override val nameStart: Pos,
override val isStatic: Boolean = false, override val isStatic: Boolean = false,
override val isExtern: Boolean = false,
) : MiniMemberDecl ) : MiniMemberDecl
data class MiniInitDecl( data class MiniInitDecl(
@@ -200,6 +209,7 @@ data class MiniInitDecl(
override val name: String get() = "init" override val name: String get() = "init"
override val doc: MiniDoc? get() = null override val doc: MiniDoc? get() = null
override val isStatic: Boolean get() = false 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. // 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 onDocCandidate(doc: MiniDoc) {}
fun onEnterClass(node: MiniClassDecl) {}
fun onExitClass(end: Pos) {}
fun onEnterFunction(node: MiniFunDecl?) {}
fun onExitFunction(end: Pos) {}
fun onImport(node: MiniImport) {} fun onImport(node: MiniImport) {}
fun onFunDecl(node: MiniFunDecl) {} fun onFunDecl(node: MiniFunDecl) {}
fun onValDecl(node: MiniValDecl) {} fun onValDecl(node: MiniValDecl) {}
@@ -238,8 +254,10 @@ interface MiniTypeTrace {
class MiniAstBuilder : MiniAstSink { class MiniAstBuilder : MiniAstSink {
private var currentScript: MiniScript? = null private var currentScript: MiniScript? = null
private val blocks = ArrayDeque<MiniBlock>() private val blocks = ArrayDeque<MiniBlock>()
private val classStack = ArrayDeque<MiniClassDecl>()
private var lastDoc: MiniDoc? = null private var lastDoc: MiniDoc? = null
private var scriptDepth: Int = 0 private var scriptDepth: Int = 0
private var functionDepth: Int = 0
fun build(): MiniScript? = currentScript fun build(): MiniScript? = currentScript
@@ -262,26 +280,119 @@ class MiniAstBuilder : MiniAstSink {
lastDoc = doc 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) { override fun onImport(node: MiniImport) {
currentScript?.imports?.add(node) currentScript?.imports?.add(node)
} }
override fun onFunDecl(node: MiniFunDecl) { override fun onFunDecl(node: MiniFunDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc) 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 lastDoc = null
} }
override fun onValDecl(node: MiniValDecl) { override fun onValDecl(node: MiniValDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc) 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 lastDoc = null
} }
override fun onClassDecl(node: MiniClassDecl) { override fun onClassDecl(node: MiniClassDecl) {
val attach = node.copy(doc = node.doc ?: lastDoc) // This is the old way, we might want to deprecate it or make it call onEnterClass
currentScript?.declarations?.add(attach) // For now, if we are NOT using enter/exit, keep behavior.
lastDoc = null // 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) { override fun onEnumDecl(node: MiniEnumDecl) {
@@ -4,8 +4,6 @@
*/ */
package net.sergeych.lyng.miniast package net.sergeych.lyng.miniast
import net.sergeych.lyng.obj.ObjString
object StdlibDocsBootstrap { object StdlibDocsBootstrap {
// Simple idempotent guard; races are harmless as initializer side-effects are idempotent // Simple idempotent guard; races are harmless as initializer side-effects are idempotent
private var ensured = false private var ensured = false
@@ -16,7 +14,25 @@ object StdlibDocsBootstrap {
// Touch core Obj* types whose docs are registered via addFnDoc/addConstDoc // Touch core Obj* types whose docs are registered via addFnDoc/addConstDoc
// Accessing .type forces their static initializers to run and register docs. // Accessing .type forces their static initializers to run and register docs.
@Suppress("UNUSED_VARIABLE") @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) { } catch (_: Throwable) {
// Best-effort; absence should not break consumers // Best-effort; absence should not break consumers
} finally { } finally {
@@ -25,6 +25,9 @@ import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.serializer import kotlinx.serialization.serializer
import net.sergeych.lyng.* 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.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonType import net.sergeych.lynon.LynonType
@@ -61,7 +64,12 @@ open class Obj {
@Suppress("SuspiciousEqualsCombination") @Suppress("SuspiciousEqualsCombination")
fun isInstanceOf(someClass: Obj) = someClass === objClass || fun isInstanceOf(someClass: Obj) = someClass === objClass ||
objClass.allParentsSet.contains(someClass) || 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 = suspend fun invokeInstanceMethod(scope: Scope, name: String, vararg args: Obj): Obj =
@@ -85,23 +93,45 @@ open class Obj {
args: Arguments = Arguments.EMPTY, args: Arguments = Arguments.EMPTY,
onNotFoundResult: (suspend () -> Obj?)? = null onNotFoundResult: (suspend () -> Obj?)? = null
): Obj { ): Obj {
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, caller)
} else if (rec.type != ObjRecord.Type.Delegated) {
return rec.value.invoke(scope, this, args, caller)
}
}
}
}
// 1. Hierarchy members (excluding root fallback) // 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") break if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null && !rec.isAbstract && rec.type != ObjRecord.Type.Property) { if (rec != null && !rec.isAbstract) {
val decl = rec.declaringClass ?: cls val decl = rec.declaringClass ?: cls
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller)) if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})")) 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)
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, decl)
} else if (rec.type != ObjRecord.Type.Delegated) {
return rec.value.invoke(scope, this, args, decl)
}
} }
} }
// 2. Extensions in scope // 2. Extensions in scope
val extension = scope.findExtension(objClass, name) val extension = scope.findExtension(objClass, name)
if (extension != null) { if (extension != null) {
return extension.value.invoke(scope, this, args) if (extension.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (extension.value as ObjProperty).callGetter(scope, this, extension.declaringClass)
} else if (extension.type != ObjRecord.Type.Delegated) {
return extension.value.invoke(scope, this, args)
}
} }
// 3. Root object fallback // 3. Root object fallback
@@ -112,7 +142,12 @@ open class Obj {
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller)) if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})")) 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)
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, decl)
} else if (rec.type != ObjRecord.Type.Delegated) {
return rec.value.invoke(scope, this, args, decl)
}
} }
} }
} }
@@ -137,9 +172,24 @@ open class Obj {
// methods that to override // methods that to override
open suspend fun compareTo(scope: Scope, other: Obj): Int { open suspend fun compareTo(scope: Scope, other: Obj): Int {
if( other === this) return 0 if (other === this) return 0
if( other === ObjNull ) return 2 if (other === ObjNull || other === ObjUnset || other === ObjVoid) return 2
scope.raiseNotImplemented() 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 { open suspend fun contains(scope: Scope, other: Obj): Boolean {
@@ -212,46 +262,66 @@ open class Obj {
* Class of the object: definition of member functions (top-level), etc. * Class of the object: definition of member functions (top-level), etc.
* Note that using lazy allows to avoid endless recursion here * 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { open suspend fun operatorNotMatch(scope: Scope, other: Obj): Obj {
@@ -260,27 +330,39 @@ open class Obj {
// Bitwise ops default (override in numeric types that support them) // Bitwise ops default (override in numeric types that support them)
open suspend fun bitAnd(scope: Scope, other: Obj): Obj { 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 { 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 { 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 { 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 { 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 { 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 open suspend fun assign(scope: Scope, other: Obj): Obj? = null
@@ -292,15 +374,43 @@ open class Obj {
* if( the operation is not defined, it returns null and the compiler would try * if( the operation is not defined, it returns null and the compiler would try
* to generate it as 'this = this + other', reassigning its variable * 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] * `-=` operations, see [plusAssign]
*/ */
open suspend fun minusAssign(scope: Scope, other: Obj): Obj? = null open suspend fun minusAssign(scope: Scope, other: Obj): Obj? {
open suspend fun mulAssign(scope: Scope, other: Obj): Obj? = null val m = objClass.getInstanceMemberOrNull("minusAssign") ?: scope.findExtension(objClass, "minusAssign")
open suspend fun divAssign(scope: Scope, other: Obj): Obj? = null return if (m != null) {
open suspend fun modAssign(scope: Scope, other: Obj): Obj? = 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 { open suspend fun getAndIncrement(scope: Scope): Obj {
scope.raiseNotImplemented() scope.raiseNotImplemented()
@@ -332,29 +442,52 @@ open class Obj {
// suspend fun <T> sync(block: () -> T): T = monitor.withLock { block() } // suspend fun <T> sync(block: () -> T): T = monitor.withLock { block() }
open suspend fun readField(scope: Scope, name: String): ObjRecord { open suspend fun readField(scope: Scope, name: String): ObjRecord {
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
val resolved = resolveRecord(scope, rec, name, caller)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, caller))
return resolved
}
}
}
// 1. Hierarchy members (excluding root fallback) // 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") break if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null) { if (rec != null && !rec.isAbstract) {
if (!rec.isAbstract) { val decl = rec.declaringClass ?: cls
return resolveRecord(scope, rec, name, rec.declaringClass) val resolved = resolveRecord(scope, rec, name, decl)
} if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, decl))
return resolved
} }
} }
// 2. Extensions // 2. Extensions
val extension = scope.findExtension(objClass, name) val extension = scope.findExtension(objClass, name)
if (extension != null) { if (extension != null) {
return resolveRecord(scope, extension, name, extension.declaringClass) val resolved = resolveRecord(scope, extension, name, extension.declaringClass)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, extension.declaringClass))
return resolved
} }
// 3. Root fallback // 3. Root fallback
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") { if (cls.className == "Obj") {
cls.members[name]?.let { cls.members[name]?.let { rec ->
val decl = it.declaringClass ?: cls val decl = rec.declaringClass ?: cls
return resolveRecord(scope, it, name, decl) val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't access field ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
val resolved = resolveRecord(scope, rec, name, decl)
if (resolved.type == ObjRecord.Type.Fun && resolved.value is Statement)
return resolved.copy(value = resolved.value.invoke(scope, this, Arguments.EMPTY, decl))
return resolved
} }
} }
} }
@@ -364,20 +497,32 @@ open class Obj {
) )
} }
protected open 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) { if (obj.type == ObjRecord.Type.Delegated) {
val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate") val del = obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
val th = if (this === ObjVoid) ObjNull else this
val res = del.invokeInstanceMethod(scope, "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(s: Scope): Obj {
val th2 = if (s.thisObj === ObjVoid) ObjNull else s.thisObj
val allArgs = (listOf(th2, ObjString(name)) + s.args.list).toTypedArray()
return del.invokeInstanceMethod(s, "invoke", Arguments(*allArgs))
}
}
})
return obj.copy( return obj.copy(
value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))), value = res,
type = ObjRecord.Type.Other type = ObjRecord.Type.Other
) )
} }
val value = obj.value val value = obj.value
if (value is ObjProperty) { if (value is ObjProperty || obj.type == ObjRecord.Type.Property) {
return ObjRecord(value.callGetter(scope, this, decl), obj.isMutable) val prop = if (value is ObjProperty) value else (value as? Statement)?.execute(scope.createChildScope(scope.pos, newThisObj = this)) as? ObjProperty
} ?: scope.raiseError("Expected ObjProperty for property member $name, got ${value::class}")
if (value is Statement && decl != null) { val res = prop.callGetter(scope, this, decl)
return ObjRecord(value.execute(scope.createChildScope(scope.pos, newThisObj = this)), obj.isMutable) return ObjRecord(res, obj.isMutable)
} }
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
// Check visibility for non-property members here if they weren't checked before // Check visibility for non-property members here if they weren't checked before
@@ -389,13 +534,24 @@ open class Obj {
open suspend fun writeField(scope: Scope, name: String, newValue: Obj) { open suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
willMutate(scope) willMutate(scope)
var field: ObjRecord? = null var field: ObjRecord? = null
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
field = rec
}
}
}
// 1. Hierarchy members (excluding root fallback) // 1. Hierarchy members (excluding root fallback)
for (cls in objClass.mro) { if (field == null) {
if (cls.className == "Obj") break for (cls in objClass.mro) {
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) if (cls.className == "Obj") break
if (rec != null && !rec.isAbstract) { val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
field = rec if (rec != null && !rec.isAbstract) {
break field = rec
break
}
} }
} }
// 2. Extensions // 2. Extensions
@@ -429,12 +585,19 @@ open class Obj {
} }
open suspend fun getAt(scope: Scope, index: Obj): Obj { open suspend fun getAt(scope: Scope, index: Obj): Obj {
if (index is ObjString) {
return readField(scope, index.value).value
}
scope.raiseNotImplemented("indexing") scope.raiseNotImplemented("indexing")
} }
suspend fun getAt(scope: Scope, index: Int): Obj = getAt(scope, ObjInt(index.toLong())) suspend fun getAt(scope: Scope, index: Int): Obj = getAt(scope, ObjInt(index.toLong()))
open suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) { open suspend fun putAt(scope: Scope, index: Obj, newValue: Obj) {
if (index is ObjString) {
writeField(scope, index.value, newValue)
return
}
scope.raiseNotImplemented("indexing") scope.raiseNotImplemented("indexing")
} }
@@ -442,17 +605,16 @@ open class Obj {
scope.raiseNotImplemented() scope.raiseNotImplemented()
} }
suspend fun invoke(scope: Scope, thisObj: Obj, args: Arguments, declaringClass: ObjClass? = null): Obj { suspend fun invoke(scope: Scope, thisObj: Obj, args: Arguments, declaringClass: ObjClass? = null): Obj =
if (PerfFlags.SCOPE_POOL) { if (PerfFlags.SCOPE_POOL)
return scope.withChildFrame(args, newThisObj = thisObj) { child -> scope.withChildFrame(args, newThisObj = thisObj) { child ->
if (declaringClass != null) child.currentClassCtx = declaringClass if (declaringClass != null) child.currentClassCtx = declaringClass
callOn(child) callOn(child)
} }
} else
val child = scope.createChildScope(scope.pos, args = args, newThisObj = thisObj) callOn(scope.createChildScope(scope.pos, args = args, newThisObj = thisObj).also {
if (declaringClass != null) child.currentClassCtx = declaringClass if (declaringClass != null) it.currentClassCtx = declaringClass
return callOn(child) })
}
suspend fun invoke(scope: Scope, thisObj: Obj, vararg args: Obj): Obj = suspend fun invoke(scope: Scope, thisObj: Obj, vararg args: Obj): Obj =
callOn( callOn(
@@ -515,20 +677,46 @@ open class Obj {
companion object { companion object {
val rootObjectType = ObjClass("Obj").apply { 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) 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() 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())) ObjBool(thisObj.contains(this, args.firstAndOnly()))
} }
// utilities // 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))) 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() val body = args.firstAndOnly()
(thisObj as? ObjInstance)?.let { (thisObj as? ObjInstance)?.let {
body.callOn(ApplyScope(this, it.instanceScope)) body.callOn(ApplyScope(this, it.instanceScope))
@@ -537,11 +725,21 @@ open class Obj {
} }
thisObj 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))) args.firstAndOnly().callOn(createChildScope(Arguments(thisObj)))
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) args.firstAndOnly().callOn(this)
} }
addFn("getAt") { addFn("getAt") {
@@ -554,15 +752,18 @@ open class Obj {
thisObj.putAt(this, requiredArg<Obj>(0), newValue) thisObj.putAt(this, requiredArg<Obj>(0), newValue)
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() thisObj.toJson(this).toString().toObj()
} }
} }
@Suppress("NOTHING_TO_INLINE") fun from(obj: Any?): Obj {
inline fun from(obj: Any?): Obj {
@Suppress("UNCHECKED_CAST")
return when (obj) { return when (obj) {
is Obj -> obj is Obj -> obj
is Double -> ObjReal(obj) is Double -> ObjReal(obj)
@@ -571,16 +772,16 @@ open class Obj {
is Long -> ObjInt.of(obj) is Long -> ObjInt.of(obj)
is String -> ObjString(obj) is String -> ObjString(obj)
is CharSequence -> ObjString(obj.toString()) is CharSequence -> ObjString(obj.toString())
is Char -> ObjChar(obj)
is Boolean -> ObjBool(obj) is Boolean -> ObjBool(obj)
is Set<*> -> ObjSet((obj as Set<Obj>).toMutableSet()) is Set<*> -> ObjSet(obj.map { from(it) }.toMutableSet())
is List<*> -> ObjList(obj.map { from(it) }.toMutableList())
is Map<*, *> -> ObjMap(obj.entries.associate { from(it.key) to from(it.value) }.toMutableMap())
is Map.Entry<*, *> -> ObjMapEntry(from(obj.key), from(obj.value))
is Enum<*> -> ObjString(obj.name)
Unit -> ObjVoid Unit -> ObjVoid
null -> ObjNull null -> ObjNull
is Iterator<*> -> ObjKotlinIterator(obj) is Iterator<*> -> ObjKotlinIterator(obj as Iterator<Any?>)
is Map.Entry<*, *> -> {
obj as MutableMap.MutableEntry<Obj, Obj>
ObjMapEntry(obj.key, obj.value)
}
else -> throw IllegalArgumentException("cannot convert to Obj: $obj") else -> throw IllegalArgumentException("cannot convert to Obj: $obj")
} }
} }
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -17,10 +17,7 @@
package net.sergeych.lyng.obj package net.sergeych.lyng.obj
import net.sergeych.lyng.miniast.ParamDoc import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.miniast.TypeGenericDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.type
val ObjArray by lazy { val ObjArray by lazy {
@@ -52,32 +49,35 @@ val ObjArray by lazy {
ObjFalse ObjFalse
} }
addFnDoc( addPropertyDoc(
name = "last", name = "last",
doc = "The last element of this array.", doc = "The last element of this array.",
returns = type("lyng.Any"), type = type("lyng.Any"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { getter = {
thisObj.invokeInstanceMethod( this.thisObj.invokeInstanceMethod(
this, this,
"getAt", "getAt",
(thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() (this.thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj()
) )
} }
)
addFnDoc( addPropertyDoc(
name = "lastIndex", name = "lastIndex",
doc = "Index of the last element (size - 1).", doc = "Index of the last element (size - 1).",
returns = type("lyng.Int"), type = type("lyng.Int"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { (thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() } getter = { (this.thisObj.invokeInstanceMethod(this, "size").toInt() - 1).toObj() }
)
addFnDoc( addPropertyDoc(
name = "indices", name = "indices",
doc = "Range of valid indices for this array.", doc = "Range of valid indices for this array.",
returns = type("lyng.Range"), type = type("lyng.Range"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { ObjRange(0.toObj(), thisObj.invokeInstanceMethod(this, "size"), false) } getter = { ObjRange(0.toObj(), this.thisObj.invokeInstanceMethod(this, "size"), false) }
)
addFnDoc( addFnDoc(
name = "binarySearch", name = "binarySearch",
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -19,11 +19,13 @@ package net.sergeych.lyng.obj
import net.sergeych.bintools.toDump import net.sergeych.bintools.toDump
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.BitArray import net.sergeych.lynon.BitArray
class ObjBitBuffer(val bitArray: BitArray) : Obj() { class ObjBitBuffer(val bitArray: BitArray) : Obj() {
override val objClass = type override val objClass get() = type
override suspend fun getAt(scope: Scope, index: Obj): Obj { override suspend fun getAt(scope: Scope, index: Obj): Obj {
return bitArray[index.toLong()].toObj() return bitArray[index.toLong()].toObj()
@@ -43,12 +45,20 @@ class ObjBitBuffer(val bitArray: BitArray) : Obj() {
thisAs<ObjBitBuffer>().bitArray.asUByteArray().toDump() thisAs<ObjBitBuffer>().bitArray.asUByteArray().toDump()
) )
} }
addFn("size") { addPropertyDoc(
thisAs<ObjBitBuffer>().bitArray.size.toObj() name = "size",
} doc = "Size of the bit buffer in bits.",
addFn("sizeInBytes") { type = type("lyng.Int"),
ObjInt((thisAs<ObjBitBuffer>().bitArray.size + 7) shr 3) moduleName = "lyng.stdlib",
} getter = { thisAs<ObjBitBuffer>().bitArray.size.toObj() }
)
addPropertyDoc(
name = "sizeInBytes",
doc = "Size of the bit buffer in full bytes (rounded up).",
type = type("lyng.Int"),
moduleName = "lyng.stdlib",
getter = { ObjInt((thisAs<ObjBitBuffer>().bitArray.size + 7) shr 3) }
)
} }
} }
} }
@@ -37,7 +37,7 @@ data class ObjBool(val value: Boolean) : Obj() {
override fun toString(): String = value.toString() 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) override suspend fun logicalNot(scope: Scope): Obj = ObjBool(!value)
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@ import net.sergeych.bintools.decodeHex
import net.sergeych.bintools.encodeToHex import net.sergeych.bintools.encodeToHex
import net.sergeych.bintools.toDump import net.sergeych.bintools.toDump
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.statement import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.BitArray import net.sergeych.lynon.BitArray
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
@@ -34,7 +35,7 @@ import kotlin.math.min
open class ObjBuffer(val byteArray: UByteArray) : Obj() { 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 hex by lazy { byteArray.encodeToHex("")}
val base64 by lazy { byteArray.toByteArray().encodeToBase64Url()} val base64 by lazy { byteArray.toByteArray().encodeToBase64Url()}
@@ -174,20 +175,26 @@ open class ObjBuffer(val byteArray: UByteArray) : Obj() {
addClassFn("decodeHex") { addClassFn("decodeHex") {
ObjBuffer(requireOnlyArg<Obj>().toString().decodeHex().asUByteArray()) ObjBuffer(requireOnlyArg<Obj>().toString().decodeHex().asUByteArray())
} }
createField("size", addPropertyDoc(
statement { name = "size",
(thisObj as ObjBuffer).byteArray.size.toObj() doc = "Number of bytes in this buffer.",
} type = type("lyng.Int"),
moduleName = "lyng.stdlib",
getter = { (this.thisObj as ObjBuffer).byteArray.size.toObj() }
) )
createField("hex", addPropertyDoc(
statement { name = "hex",
thisAs<ObjBuffer>().hex.toObj() doc = "Hexadecimal string representation of the buffer.",
} type = type("lyng.String"),
moduleName = "lyng.stdlib",
getter = { thisAs<ObjBuffer>().hex.toObj() }
) )
createField("base64", addPropertyDoc(
statement { name = "base64",
thisAs<ObjBuffer>().base64.toObj() doc = "Base64 (URL-safe) string representation of the buffer.",
} type = type("lyng.String"),
moduleName = "lyng.stdlib",
getter = { thisAs<ObjBuffer>().base64.toObj() }
) )
addFn("decodeUtf8") { addFn("decodeUtf8") {
ObjString( ObjString(
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,12 +18,12 @@
package net.sergeych.lyng.obj package net.sergeych.lyng.obj
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addFnDoc import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type import net.sergeych.lyng.miniast.type
class ObjChar(val value: Char): Obj() { 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 = override suspend fun compareTo(scope: Scope, other: Obj): Int =
(other as? ObjChar)?.let { value.compareTo(it.value) } ?: -1 (other as? ObjChar)?.let { value.compareTo(it.value) } ?: -1
@@ -47,12 +47,13 @@ class ObjChar(val value: Char): Obj() {
companion object { companion object {
val type = ObjClass("Char").apply { val type = ObjClass("Char").apply {
addFnDoc( addPropertyDoc(
name = "code", name = "code",
doc = "Unicode code point (UTF-16 code unit) of this character.", doc = "Unicode code point (UTF-16 code unit) of this character.",
returns = type("lyng.Int"), type = type("lyng.Int"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { ObjInt(thisAs<ObjChar>().value.code.toLong()) } getter = { ObjInt((this.thisObj as ObjChar).value.code.toLong()) }
)
addFn("isDigit") { addFn("isDigit") {
thisAs<ObjChar>().value.isDigit().toObj() thisAs<ObjChar>().value.isDigit().toObj()
} }
@@ -18,10 +18,7 @@
package net.sergeych.lyng.obj package net.sergeych.lyng.obj
import net.sergeych.lyng.* import net.sergeych.lyng.*
import net.sergeych.lyng.miniast.ParamDoc import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.miniast.TypeGenericDoc
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonType import net.sergeych.lynon.LynonType
@@ -30,46 +27,56 @@ private object ClassIdGen { var c: Long = 1L; fun nextId(): Long = c++ }
val ObjClassType by lazy { val ObjClassType by lazy {
ObjClass("Class").apply { ObjClass("Class").apply {
addFnDoc( addPropertyDoc(
name = "className",
doc = "Full name of this class including package if available.",
type = type("lyng.String"),
moduleName = "lyng.stdlib",
getter = { (this.thisObj as ObjClass).classNameObj }
)
addPropertyDoc(
name = "name", name = "name",
doc = "Simple name of this class (without package).", doc = "Simple name of this class (without package).",
returns = type("lyng.String"), type = type("lyng.String"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { thisAs<ObjClass>().classNameObj } getter = { (this.thisObj as ObjClass).classNameObj }
)
addFnDoc( addPropertyDoc(
name = "fields", name = "fields",
doc = "Declared instance fields of this class and its ancestors (C3 order), without duplicates.", doc = "Declared instance fields of this class and its ancestors (C3 order), without duplicates.",
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String"))), type = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String"))),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { getter = {
val cls = thisAs<ObjClass>() val cls = this.thisObj as ObjClass
val seen = hashSetOf<String>() val seen = hashSetOf<String>()
val names = mutableListOf<Obj>() val names = mutableListOf<Obj>()
for (c in cls.mro) { for (c in cls.mro) {
for ((n, rec) in c.members) { for ((n, rec) in c.members) {
if (rec.value !is Statement && seen.add(n)) names += ObjString(n) if (rec.value !is Statement && seen.add(n)) names += ObjString(n)
}
} }
ObjList(names.toMutableList())
} }
ObjList(names.toMutableList()) )
}
addFnDoc( addPropertyDoc(
name = "methods", name = "methods",
doc = "Declared instance methods of this class and its ancestors (C3 order), without duplicates.", doc = "Declared instance methods of this class and its ancestors (C3 order), without duplicates.",
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String"))), type = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.String"))),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { getter = {
val cls = thisAs<ObjClass>() val cls = this.thisObj as ObjClass
val seen = hashSetOf<String>() val seen = hashSetOf<String>()
val names = mutableListOf<Obj>() val names = mutableListOf<Obj>()
for (c in cls.mro) { for (c in cls.mro) {
for ((n, rec) in c.members) { for ((n, rec) in c.members) {
if (rec.value is Statement && seen.add(n)) names += ObjString(n) if (rec.value is Statement && seen.add(n)) names += ObjString(n)
}
} }
ObjList(names.toMutableList())
} }
ObjList(names.toMutableList()) )
}
addFnDoc( addFnDoc(
name = "get", name = "get",
@@ -91,6 +98,8 @@ open class ObjClass(
vararg parents: ObjClass, vararg parents: ObjClass,
) : Obj() { ) : Obj() {
var isAnonymous: Boolean = false
var isAbstract: Boolean = false var isAbstract: Boolean = false
// Stable identity and simple structural version for PICs // Stable identity and simple structural version for PICs
@@ -121,6 +130,24 @@ open class ObjClass(
/** Direct parents in declaration order (kept deterministic). */ /** Direct parents in declaration order (kept deterministic). */
val directParents: List<ObjClass> = parents.toList() 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). */ /** Optional constructor argument specs for each direct parent (set by compiler for user classes). */
open val directParentArgs: MutableMap<ObjClass, List<ParsedArgument>> = mutableMapOf() open val directParentArgs: MutableMap<ObjClass, List<ParsedArgument>> = mutableMapOf()
@@ -181,11 +208,8 @@ open class ObjClass(
val base = c3Linearize(this, mutableMapOf()) val base = c3Linearize(this, mutableMapOf())
if (this.className == "Obj" || base.any { it.className == "Obj" }) base if (this.className == "Obj" || base.any { it.className == "Obj" }) base
else { else {
// During very early bootstrap rootObjectType might not be initialized yet. val root = Obj.rootObjectType
// We use a safe check here. base + root
@Suppress("UNNECESSARY_SAFE_CALL")
val root = net.sergeych.lyng.obj.Obj.rootObjectType
if (root != null) base + root else base
} }
} }
@@ -232,18 +256,27 @@ open class ObjClass(
instance.instanceScope.currentClassCtx = null instance.instanceScope.currentClassCtx = null
// Expose instance methods (and other callable members) directly in the instance scope for fast lookup // Expose instance methods (and other callable members) directly in the instance scope for fast lookup
// This mirrors Obj.autoInstanceScope behavior for ad-hoc scopes and makes fb.method() resolution robust // This mirrors Obj.autoInstanceScope behavior for ad-hoc scopes and makes fb.method() resolution robust
// 1) members-defined methods
for ((k, v) in members) { for (cls in mro) {
if (v.value is Statement || v.type == ObjRecord.Type.Delegated) { // 1) members-defined methods and fields
instance.instanceScope.objects[k] = v for ((k, v) in cls.members) {
if (!v.isAbstract && (v.value is Statement || v.type == ObjRecord.Type.Delegated || v.type == ObjRecord.Type.Field)) {
val key = if (v.visibility == Visibility.Private || v.type == ObjRecord.Type.Field || v.type == ObjRecord.Type.Delegated) "${cls.className}::$k" else k
if (!instance.instanceScope.objects.containsKey(key)) {
instance.instanceScope.objects[key] = if (v.type == ObjRecord.Type.Fun) v else v.copy()
}
}
} }
} // 2) class-scope members registered during class-body execution
// 2) class-scope methods registered during class-body execution cls.classScope?.objects?.forEach { (k, rec) ->
classScope?.objects?.forEach { (k, rec) -> // ONLY copy methods and delegated members from class scope to instance scope.
if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) { // Fields in class scope are static fields and must NOT be per-instance.
// if not already present, copy reference for dispatch if (!rec.isAbstract && (rec.value is Statement || rec.type == ObjRecord.Type.Delegated)) {
if (!instance.instanceScope.objects.containsKey(k)) { val key = if (rec.visibility == Visibility.Private || rec.type == ObjRecord.Type.Delegated) "${cls.className}::$k" else k
instance.instanceScope.objects[k] = rec // if not already present, copy reference for dispatch
if (!instance.instanceScope.objects.containsKey(key)) {
instance.instanceScope.objects[key] = if (rec.type == ObjRecord.Type.Fun) rec else rec.copy()
}
} }
} }
} }
@@ -282,7 +315,13 @@ open class ObjClass(
c.constructorMeta?.let { meta -> c.constructorMeta?.let { meta ->
val argsHere = argsForThis ?: Arguments.EMPTY val argsHere = argsForThis ?: Arguments.EMPTY
// Assign constructor params into instance scope (unmangled) // Assign constructor params into instance scope (unmangled)
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c) val savedCtx = instance.instanceScope.currentClassCtx
instance.instanceScope.currentClassCtx = c
try {
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c)
} finally {
instance.instanceScope.currentClassCtx = savedCtx
}
// Also expose them under MI-mangled storage keys `${Class}::name` so qualified views can access them // Also expose them under MI-mangled storage keys `${Class}::name` so qualified views can access them
// and so that base-class casts like `(obj as Base).field` work. // and so that base-class casts like `(obj as Base).field` work.
for (p in meta.params) { for (p in meta.params) {
@@ -311,7 +350,13 @@ open class ObjClass(
// parameters even if they were shadowed/overwritten by parent class initialization. // parameters even if they were shadowed/overwritten by parent class initialization.
c.constructorMeta?.let { meta -> c.constructorMeta?.let { meta ->
val argsHere = argsForThis ?: Arguments.EMPTY val argsHere = argsForThis ?: Arguments.EMPTY
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c) val savedCtx = instance.instanceScope.currentClassCtx
instance.instanceScope.currentClassCtx = c
try {
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c)
} finally {
instance.instanceScope.currentClassCtx = savedCtx
}
// Re-sync mangled names to point to the fresh records to keep them consistent // Re-sync mangled names to point to the fresh records to keep them consistent
for (p in meta.params) { for (p in meta.params) {
val rec = instance.instanceScope.objects[p.name] val rec = instance.instanceScope.objects[p.name]
@@ -364,7 +409,8 @@ open class ObjClass(
): ObjRecord { ): ObjRecord {
// Validation of override rules: only for non-system declarations // Validation of override rules: only for non-system declarations
if (pos != Pos.builtIn) { if (pos != Pos.builtIn) {
val existing = getInstanceMemberOrNull(name) // Only consider TRUE instance members from ancestors for overrides
val existing = getInstanceMemberOrNull(name, includeAbstract = true, includeStatic = false)
var actualOverride = false var actualOverride = false
if (existing != null && existing.declaringClass != this) { if (existing != null && existing.declaringClass != this) {
// If the existing member is private in the ancestor, it's not visible for overriding. // If the existing member is private in the ancestor, it's not visible for overriding.
@@ -467,12 +513,13 @@ open class ObjClass(
isClosed: Boolean = false, isClosed: Boolean = false,
isOverride: Boolean = false, isOverride: Boolean = false,
pos: Pos = Pos.builtIn, pos: Pos = Pos.builtIn,
prop: ObjProperty? = null
) { ) {
val g = getter?.let { statement { it() } } val g = getter?.let { statement { it() } }
val s = setter?.let { statement { it(requiredArg(0)); ObjVoid } } val s = setter?.let { statement { it(requiredArg(0)); ObjVoid } }
val prop = if (isAbstract) ObjNull else ObjProperty(name, g, s) val finalProp = prop ?: if (isAbstract) ObjNull else ObjProperty(name, g, s)
createField( createField(
name, prop, false, visibility, writeVisibility, pos, declaringClass, name, finalProp, false, visibility, writeVisibility, pos, declaringClass,
isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride,
type = ObjRecord.Type.Property type = ObjRecord.Type.Property
) )
@@ -480,21 +527,28 @@ open class ObjClass(
fun addClassConst(name: String, value: Obj) = createClassField(name, value) fun addClassConst(name: String, value: Obj) = createClassField(name, value)
fun addClassFn(name: String, isOpen: Boolean = false, code: suspend Scope.() -> Obj) { 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)
} }
/** /**
* Get instance member traversing the hierarchy if needed. Its meaning is different for different objects. * Get instance member traversing the hierarchy if needed. Its meaning is different for different objects.
*/ */
fun getInstanceMemberOrNull(name: String): ObjRecord? { fun getInstanceMemberOrNull(name: String, includeAbstract: Boolean = false, includeStatic: Boolean = true): ObjRecord? {
// Unified traversal in strict C3 order: self, then each ancestor, checking members before classScope // Unified traversal in strict C3 order: self, then each ancestor, checking members before classScope
for (cls in mro) { for (cls in mro) {
cls.members[name]?.let { return it } cls.members[name]?.let {
cls.classScope?.objects?.get(name)?.let { return it } if (includeAbstract || !it.isAbstract) return it
}
if (includeStatic) {
cls.classScope?.objects?.get(name)?.let {
if (includeAbstract || !it.isAbstract) return it
}
}
} }
// Finally, allow root object fallback (rare; mostly built-ins like toString) // Finally, allow root object fallback (rare; mostly built-ins like toString)
return rootObjectType.members[name] val rootRec = rootObjectType.members[name]
return if (rootRec != null && (includeAbstract || !rootRec.isAbstract)) rootRec else null
} }
/** Find the declaring class where a member with [name] is defined, starting from this class along MRO. */ /** Find the declaring class where a member with [name] is defined, starting from this class along MRO. */
@@ -588,11 +642,21 @@ open class ObjClass(
getInstanceMemberOrNull(name)?.let { rec -> getInstanceMemberOrNull(name)?.let { rec ->
val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this val decl = rec.declaringClass ?: findDeclaringClassOf(name) ?: this
if (rec.type == ObjRecord.Type.Delegated) { if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: scope.raiseError("Internal error: delegated function $name has no delegate") val del = rec.delegate ?: scope.raiseError("Internal error: delegated member $name has no delegate")
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray() val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs)) 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 rec.value.invoke(scope, this, args, decl)
} }
return super.invokeInstanceMethod(scope, name, args, onNotFoundResult) return super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
} }
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,5 +21,6 @@ package net.sergeych.lyng.obj
* Collection is an iterator with `size` * Collection is an iterator with `size`
*/ */
val ObjCollection = ObjClass("Collection", ObjIterable).apply { val ObjCollection = ObjClass("Collection", ObjIterable).apply {
addProperty("size", isAbstract = true)
} }
@@ -25,7 +25,7 @@ import net.sergeych.lyng.miniast.type
class ObjCompletableDeferred(val completableDeferred: CompletableDeferred<Obj>): ObjDeferred(completableDeferred) { class ObjCompletableDeferred(val completableDeferred: CompletableDeferred<Obj>): ObjDeferred(completableDeferred) {
override val objClass = type override val objClass get() = type
companion object { companion object {
val type = object: ObjClass("CompletableDeferred", ObjDeferred.type){ val type = object: ObjClass("CompletableDeferred", ObjDeferred.type){
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,11 +20,12 @@ package net.sergeych.lyng.obj
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addFnDoc import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type import net.sergeych.lyng.miniast.type
open class ObjDeferred(val deferred: Deferred<Obj>): Obj() { open class ObjDeferred(val deferred: Deferred<Obj>): Obj() {
override val objClass = type override val objClass get() = type
companion object { companion object {
val type = object: ObjClass("Deferred"){ val type = object: ObjClass("Deferred"){
@@ -38,28 +39,30 @@ open class ObjDeferred(val deferred: Deferred<Obj>): Obj() {
returns = type("lyng.Any"), returns = type("lyng.Any"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib"
) { thisAs<ObjDeferred>().deferred.await() } ) { thisAs<ObjDeferred>().deferred.await() }
addFnDoc( addPropertyDoc(
name = "isCompleted", name = "isCompleted",
doc = "Whether this deferred has completed (successfully or with an error).", doc = "Whether this deferred has completed (successfully or with an error).",
returns = type("lyng.Bool"), type = type("lyng.Bool"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { thisAs<ObjDeferred>().deferred.isCompleted.toObj() } getter = { thisAs<ObjDeferred>().deferred.isCompleted.toObj() }
addFnDoc( )
addPropertyDoc(
name = "isActive", name = "isActive",
doc = "Whether this deferred is currently active (not completed and not cancelled).", doc = "Whether this deferred is currently active (not completed and not cancelled).",
returns = type("lyng.Bool"), type = type("lyng.Bool"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { getter = {
val d = thisAs<ObjDeferred>().deferred val d = thisAs<ObjDeferred>().deferred
(d.isActive || (!d.isCompleted && !d.isCancelled)).toObj() (d.isActive || (!d.isCompleted && !d.isCancelled)).toObj()
} }
addFnDoc( )
addPropertyDoc(
name = "isCancelled", name = "isCancelled",
doc = "Whether this deferred was cancelled.", doc = "Whether this deferred was cancelled.",
returns = type("lyng.Bool"), type = type("lyng.Bool"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
) { thisAs<ObjDeferred>().deferred.isCancelled.toObj() } getter = { thisAs<ObjDeferred>().deferred.isCancelled.toObj() }
)
} }
} }
} }
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@
package net.sergeych.lyng.obj package net.sergeych.lyng.obj
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addFnDoc import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type import net.sergeych.lyng.miniast.type
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.Duration.Companion.days import kotlin.time.Duration.Companion.days
@@ -29,7 +29,7 @@ import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit import kotlin.time.DurationUnit
class ObjDuration(val duration: Duration) : Obj() { class ObjDuration(val duration: Duration) : Obj() {
override val objClass: ObjClass = type override val objClass: ObjClass get() = type
override fun toString(): String { override fun toString(): String {
return duration.toString() return duration.toString()
@@ -74,169 +74,195 @@ class ObjDuration(val duration: Duration) : Obj() {
) )
} }
}.apply { }.apply {
addFnDoc( addPropertyDoc(
name = "days", name = "days",
doc = "Return this duration as a real number of days.", doc = "Return this duration as a real number of days.",
returns = type("lyng.Real"), type = type("lyng.Real"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.DAYS).toObj() } getter = { thisAs<ObjDuration>().duration.toDouble(DurationUnit.DAYS).toObj() }
addFnDoc( )
addPropertyDoc(
name = "hours", name = "hours",
doc = "Return this duration as a real number of hours.", doc = "Return this duration as a real number of hours.",
returns = type("lyng.Real"), type = type("lyng.Real"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.HOURS).toObj() } getter = { thisAs<ObjDuration>().duration.toDouble(DurationUnit.HOURS).toObj() }
addFnDoc( )
addPropertyDoc(
name = "minutes", name = "minutes",
doc = "Return this duration as a real number of minutes.", doc = "Return this duration as a real number of minutes.",
returns = type("lyng.Real"), type = type("lyng.Real"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MINUTES).toObj() } getter = { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MINUTES).toObj() }
addFnDoc( )
addPropertyDoc(
name = "seconds", name = "seconds",
doc = "Return this duration as a real number of seconds.", doc = "Return this duration as a real number of seconds.",
returns = type("lyng.Real"), type = type("lyng.Real"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.SECONDS).toObj() } getter = { thisAs<ObjDuration>().duration.toDouble(DurationUnit.SECONDS).toObj() }
addFnDoc( )
addPropertyDoc(
name = "milliseconds", name = "milliseconds",
doc = "Return this duration as a real number of milliseconds.", doc = "Return this duration as a real number of milliseconds.",
returns = type("lyng.Real"), type = type("lyng.Real"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MILLISECONDS).toObj() } getter = { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MILLISECONDS).toObj() }
addFnDoc( )
addPropertyDoc(
name = "microseconds", name = "microseconds",
doc = "Return this duration as a real number of microseconds.", doc = "Return this duration as a real number of microseconds.",
returns = type("lyng.Real"), type = type("lyng.Real"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MICROSECONDS).toObj() } getter = { thisAs<ObjDuration>().duration.toDouble(DurationUnit.MICROSECONDS).toObj() }
)
// extensions // extensions
ObjInt.type.addFnDoc( ObjInt.type.addPropertyDoc(
name = "seconds", name = "seconds",
doc = "Construct a `Duration` equal to this integer number of seconds.", doc = "Construct a `Duration` equal to this integer number of seconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.seconds) } getter = { ObjDuration(thisAs<ObjInt>().value.seconds) }
)
ObjInt.type.addFnDoc( ObjInt.type.addPropertyDoc(
name = "second", name = "second",
doc = "Construct a `Duration` equal to this integer number of seconds.", doc = "Construct a `Duration` equal to this integer number of seconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.seconds) } getter = { ObjDuration(thisAs<ObjInt>().value.seconds) }
ObjInt.type.addFnDoc( )
ObjInt.type.addPropertyDoc(
name = "milliseconds", name = "milliseconds",
doc = "Construct a `Duration` equal to this integer number of milliseconds.", doc = "Construct a `Duration` equal to this integer number of milliseconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.milliseconds) } getter = { ObjDuration(thisAs<ObjInt>().value.milliseconds) }
)
ObjInt.type.addFnDoc( ObjInt.type.addPropertyDoc(
name = "millisecond", name = "millisecond",
doc = "Construct a `Duration` equal to this integer number of milliseconds.", doc = "Construct a `Duration` equal to this integer number of milliseconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.milliseconds) } getter = { ObjDuration(thisAs<ObjInt>().value.milliseconds) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "seconds", name = "seconds",
doc = "Construct a `Duration` equal to this real number of seconds.", doc = "Construct a `Duration` equal to this real number of seconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.seconds) } getter = { ObjDuration(thisAs<ObjReal>().value.seconds) }
)
ObjReal.type.addFnDoc( ObjReal.type.addPropertyDoc(
name = "second", name = "second",
doc = "Construct a `Duration` equal to this real number of seconds.", doc = "Construct a `Duration` equal to this real number of seconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.seconds) } getter = { ObjDuration(thisAs<ObjReal>().value.seconds) }
)
ObjReal.type.addFnDoc( ObjReal.type.addPropertyDoc(
name = "milliseconds", name = "milliseconds",
doc = "Construct a `Duration` equal to this real number of milliseconds.", doc = "Construct a `Duration` equal to this real number of milliseconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.milliseconds) } getter = { ObjDuration(thisAs<ObjReal>().value.milliseconds) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "millisecond", name = "millisecond",
doc = "Construct a `Duration` equal to this real number of milliseconds.", doc = "Construct a `Duration` equal to this real number of milliseconds.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.milliseconds) } getter = { ObjDuration(thisAs<ObjReal>().value.milliseconds) }
)
ObjInt.type.addFnDoc( ObjInt.type.addPropertyDoc(
name = "minutes", name = "minutes",
doc = "Construct a `Duration` equal to this integer number of minutes.", doc = "Construct a `Duration` equal to this integer number of minutes.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.minutes) } getter = { ObjDuration(thisAs<ObjInt>().value.minutes) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "minutes", name = "minutes",
doc = "Construct a `Duration` equal to this real number of minutes.", doc = "Construct a `Duration` equal to this real number of minutes.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.minutes) } getter = { ObjDuration(thisAs<ObjReal>().value.minutes) }
ObjInt.type.addFnDoc( )
ObjInt.type.addPropertyDoc(
name = "minute", name = "minute",
doc = "Construct a `Duration` equal to this integer number of minutes.", doc = "Construct a `Duration` equal to this integer number of minutes.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.minutes) } getter = { ObjDuration(thisAs<ObjInt>().value.minutes) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "minute", name = "minute",
doc = "Construct a `Duration` equal to this real number of minutes.", doc = "Construct a `Duration` equal to this real number of minutes.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.minutes) } getter = { ObjDuration(thisAs<ObjReal>().value.minutes) }
ObjInt.type.addFnDoc( )
ObjInt.type.addPropertyDoc(
name = "hours", name = "hours",
doc = "Construct a `Duration` equal to this integer number of hours.", doc = "Construct a `Duration` equal to this integer number of hours.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.hours) } getter = { ObjDuration(thisAs<ObjInt>().value.hours) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "hours", name = "hours",
doc = "Construct a `Duration` equal to this real number of hours.", doc = "Construct a `Duration` equal to this real number of hours.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.hours) } getter = { ObjDuration(thisAs<ObjReal>().value.hours) }
ObjInt.type.addFnDoc( )
ObjInt.type.addPropertyDoc(
name = "hour", name = "hour",
doc = "Construct a `Duration` equal to this integer number of hours.", doc = "Construct a `Duration` equal to this integer number of hours.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.hours) } getter = { ObjDuration(thisAs<ObjInt>().value.hours) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "hour", name = "hour",
doc = "Construct a `Duration` equal to this real number of hours.", doc = "Construct a `Duration` equal to this real number of hours.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.hours) } getter = { ObjDuration(thisAs<ObjReal>().value.hours) }
ObjInt.type.addFnDoc( )
ObjInt.type.addPropertyDoc(
name = "days", name = "days",
doc = "Construct a `Duration` equal to this integer number of days.", doc = "Construct a `Duration` equal to this integer number of days.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.days) } getter = { ObjDuration(thisAs<ObjInt>().value.days) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "days", name = "days",
doc = "Construct a `Duration` equal to this real number of days.", doc = "Construct a `Duration` equal to this real number of days.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.days) } getter = { ObjDuration(thisAs<ObjReal>().value.days) }
ObjInt.type.addFnDoc( )
ObjInt.type.addPropertyDoc(
name = "day", name = "day",
doc = "Construct a `Duration` equal to this integer number of days.", doc = "Construct a `Duration` equal to this integer number of days.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjInt>().value.days) } getter = { ObjDuration(thisAs<ObjInt>().value.days) }
ObjReal.type.addFnDoc( )
ObjReal.type.addPropertyDoc(
name = "day", name = "day",
doc = "Construct a `Duration` equal to this real number of days.", doc = "Construct a `Duration` equal to this real number of days.",
returns = type("lyng.Duration"), type = type("lyng.Duration"),
moduleName = "lyng.time" moduleName = "lyng.time",
) { ObjDuration(thisAs<ObjReal>().value.days) } getter = { ObjDuration(thisAs<ObjReal>().value.days) }
)
// addFn("epochSeconds") { // addFn("epochSeconds") {
@@ -23,7 +23,7 @@ import net.sergeych.lyng.Scope
import net.sergeych.lyng.Statement import net.sergeych.lyng.Statement
class ObjDynamicContext(val delegate: ObjDynamic) : Obj() { class ObjDynamicContext(val delegate: ObjDynamic) : Obj() {
override val objClass: ObjClass = type override val objClass: ObjClass get() = type
companion object { companion object {
val type = ObjClass("DelegateContext").apply { 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() { 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 // Capture the lexical scope used to build this dynamic so callbacks can see outer locals
internal var builderScope: Scope? = null internal var builderScope: Scope? = null
@@ -35,6 +35,8 @@ package net.sergeych.lyng.obj/*
import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.JsonPrimitive
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonType import net.sergeych.lynon.LynonType
@@ -76,8 +78,8 @@ class ObjEnumClass(val name: String) : ObjClass(name, EnumBase) {
val name = requireOnlyArg<ObjString>() val name = requireOnlyArg<ObjString>()
byName[name] ?: raiseSymbolNotFound("does not exists: enum ${className}.$name") byName[name] ?: raiseSymbolNotFound("does not exists: enum ${className}.$name")
} }
addFn("name") { thisAs<ObjEnumEntry>().name } addPropertyDoc("name", doc = "Entry name as string", type = type("lyng.String"), getter = { thisAs<ObjEnumEntry>().name })
addFn("ordinal") { thisAs<ObjEnumEntry>().ordinal } addPropertyDoc("ordinal", doc = "Entry ordinal position", type = type("lyng.Int"), getter = { thisAs<ObjEnumEntry>().ordinal })
} }
@@ -17,11 +17,10 @@
package net.sergeych.lyng.obj package net.sergeych.lyng.obj
import net.sergeych.bintools.encodeToHex
import net.sergeych.lyng.* import net.sergeych.lyng.*
import net.sergeych.lyng.miniast.TypeGenericDoc import net.sergeych.lyng.miniast.TypeGenericDoc
import net.sergeych.lyng.miniast.addConstDoc
import net.sergeych.lyng.miniast.addFnDoc import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
@@ -41,7 +40,7 @@ open class ObjException(
val exceptionClass: ExceptionClass, val exceptionClass: ExceptionClass,
val scope: Scope, val scope: Scope,
val message: ObjString, val message: ObjString,
@Suppress("unused") val extraData: Obj = ObjNull, val extraData: Obj = ObjNull,
val useStackTrace: ObjList? = null val useStackTrace: ObjList? = null
) : Obj() { ) : Obj() {
constructor(name: String, scope: Scope, message: String) : this( constructor(name: String, scope: Scope, message: String) : this(
@@ -54,6 +53,46 @@ open class ObjException(
suspend fun getStackTrace(): ObjList { suspend fun getStackTrace(): ObjList {
return cachedStackTrace.get { 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 result = ObjList()
val maybeCls = scope.get("StackTraceEntry")?.value as? ObjClass val maybeCls = scope.get("StackTraceEntry")?.value as? ObjClass
var s: Scope? = scope var s: Scope? = scope
@@ -77,41 +116,23 @@ open class ObjException(
s = s.parent s = s.parent
lastPos = pos 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) { 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 { override suspend fun callOn(scope: Scope): Obj {
val message = scope.args.getOrNull(0)?.toString(scope) ?: ObjString(name) val message = scope.args.getOrNull(0)?.toString(scope) ?: ObjString(name)
return ObjException(this, scope, message) return ObjException(this, scope, message)
} }
override fun toString(): String = "ExceptionClass[$name]@${hashCode().encodeToHex()}" override fun toString(): String = name
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj { override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
return try { return try {
@@ -137,22 +158,75 @@ open class ObjException(
} }
val Root = ExceptionClass("Exception").apply { val Root = ExceptionClass("Exception").apply {
addConstDoc( 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 }
addPropertyDoc(
name = "message", name = "message",
value = statement {
(thisObj as ObjException).message.toObj()
},
doc = "Human‑readable error message.", doc = "Human‑readable error message.",
type = type("lyng.String"), type = type("lyng.String"),
moduleName = "lyng.stdlib" moduleName = "lyng.stdlib",
getter = {
when (val t = this.thisObj) {
is ObjException -> t.message
is ObjInstance -> t.instanceScope.get("Exception::message")?.value ?: ObjNull
else -> ObjNull
}
}
) )
addFnDoc( addPropertyDoc(
name = "extraData",
doc = "Extra data associated with the exception.",
type = type("lyng.Any", nullable = true),
moduleName = "lyng.stdlib",
getter = {
when (val t = this.thisObj) {
is ObjException -> t.extraData
else -> ObjNull
}
}
)
addPropertyDoc(
name = "stackTrace", name = "stackTrace",
doc = "Stack trace captured at throw site as a list of `StackTraceEntry`.", doc = "Stack trace captured at throw site as a list of `StackTraceEntry`.",
returns = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.StackTraceEntry"))), type = TypeGenericDoc(type("lyng.List"), listOf(type("lyng.StackTraceEntry"))),
moduleName = "lyng.stdlib",
getter = {
when (val t = this.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" moduleName = "lyng.stdlib"
) { ) {
(thisObj as ObjException).getStackTrace() 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")
} }
} }
@@ -254,3 +328,72 @@ class ObjUnsetException(scope: Scope, message: String = "property is unset (not
class ObjNotImplementedException(scope: Scope, message: String = "not implemented") : class ObjNotImplementedException(scope: Scope, message: String = "not implemented") :
ObjException("NotImplementedException", scope, message) 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?=null): String {
require( this.isLyngException() )
val s = scope ?: Script.newScope()
return invokeInstanceMethod(s, "message").toString(s).value
}
/**
* Retrieves a detailed exception message including the stack trace for a Lyng exception.
* This function is designed to handle objects identified as Lyng exceptions.
*
* @param scope the scope to be used for fetching the exception message and stack trace.
* If null, a new scope will be created.
* @return a string combining the exception message, the location ("at"),
* and the formatted stack trace information.
* The stack trace details each frame using indentation for clarity.
* @throws IllegalArgumentException if the object is not a Lyng exception.
*/
suspend fun Obj.getLyngExceptionMessageWithStackTrace(scope: Scope?=null): String {
require( this.isLyngException() )
val s = scope ?: Script.newScope()
val msg = getLyngExceptionMessage(s)
val trace = getLyngExceptionStackTrace(s)
var at = "unknown"
val stack = if( !trace.list.isEmpty() ) {
val first = trace.list[0]
at = (first.readField(s, "at").value as ObjString).value
"\n" + trace.list.map { " at " + it.toString(s).value }.joinToString("\n")
}
else ""
return "$at: $msg$stack"
}
/**
* 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)
}
@@ -35,7 +35,7 @@ import kotlin.coroutines.cancellation.CancellationException
class ObjFlowBuilder(val output: SendChannel<Obj>) : Obj() { class ObjFlowBuilder(val output: SendChannel<Obj>) : Obj() {
override val objClass = type override val objClass get() = type
companion object { companion object {
@OptIn(DelicateCoroutinesApi::class) @OptIn(DelicateCoroutinesApi::class)
@@ -91,7 +91,7 @@ private fun createLyngFlowInput(scope: Scope, producer: Statement): ReceiveChann
class ObjFlow(val producer: Statement, val scope: Scope) : Obj() { class ObjFlow(val producer: Statement, val scope: Scope) : Obj() {
override val objClass = type override val objClass get() = type
companion object { companion object {
val type = object : ObjClass("Flow", ObjIterable) { 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() { class ObjFlowIterator(val producer: Statement) : Obj() {
override val objClass: ObjClass = type override val objClass: ObjClass get() = type
private var channel: ReceiveChannel<Obj>? = null private var channel: ReceiveChannel<Obj>? = null
@@ -21,6 +21,7 @@ import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import net.sergeych.lyng.Arguments import net.sergeych.lyng.Arguments
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.Visibility
import net.sergeych.lyng.canAccessMember import net.sergeych.lyng.canAccessMember
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
@@ -31,50 +32,43 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
internal lateinit var instanceScope: Scope internal lateinit var instanceScope: Scope
override suspend fun readField(scope: Scope, name: String): ObjRecord { override suspend fun readField(scope: Scope, name: String): ObjRecord {
// 1. Direct (unmangled) lookup first val caller = scope.currentClassCtx
instanceScope[name]?.let { rec ->
// 0. Private mangled of current class context
caller?.let { c ->
// Check for private methods/properties
c.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private) {
return resolveRecord(scope, rec, name, c)
}
}
// Check for private fields (stored in instanceScope)
val mangled = "${c.className}::$name"
instanceScope.objects[mangled]?.let { rec ->
if (rec.visibility == Visibility.Private) {
return resolveRecord(scope, rec, name, c)
}
}
}
// 1. MRO mangled storage
for (cls in objClass.mro) {
if (cls.className == "Obj") break
val mangled = "${cls.className}::$name"
instanceScope.objects[mangled]?.let { rec ->
if ((scope.thisObj === this && caller != null) || canAccessMember(rec.visibility, cls, caller)) {
return resolveRecord(scope, rec, name, cls)
}
}
}
// 2. Unmangled storage
instanceScope.objects[name]?.let { rec ->
val decl = rec.declaringClass val decl = rec.declaringClass
// Allow unconditional access when accessing through `this` of the same instance // Unmangled access is only allowed if it's public OR we are inside the same instance's method
// BUT only if we are in the class context (not extension) if ((scope.thisObj === this && caller != null) || canAccessMember(rec.visibility, decl, caller)) {
if (scope.thisObj !== this || scope.currentClassCtx == null) { return resolveRecord(scope, rec, name, decl)
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(
ObjIllegalAccessException(
scope,
"can't access field $name (declared in ${decl?.className ?: "?"})"
)
)
} }
return resolveRecord(scope, rec, name, decl)
}
// 2. MI-mangled instance scope lookup
val cls = objClass
fun findMangledInRead(): ObjRecord? {
instanceScope.objects["${cls.className}::$name"]?.let { return it }
for (p in cls.mroParents) {
instanceScope.objects["${p.className}::$name"]?.let { return it }
}
return null
}
findMangledInRead()?.let { rec ->
val declaring = when {
instanceScope.objects.containsKey("${cls.className}::$name") -> cls
else -> cls.mroParents.firstOrNull { instanceScope.objects.containsKey("${it.className}::$name") }
}
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, declaring, caller))
scope.raiseError(
ObjIllegalAccessException(
scope,
"can't access field $name (declared in ${declaring?.className ?: "?"})"
)
)
}
return resolveRecord(scope, rec, name, declaring)
} }
// 3. Fall back to super (handles class members and extensions) // 3. Fall back to super (handles class members and extensions)
@@ -82,7 +76,100 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
} }
override suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord { override suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
if (obj.type.isArgument) return super.resolveRecord(scope, obj, name, decl)
if (obj.type == ObjRecord.Type.Delegated) { if (obj.type == ObjRecord.Type.Delegated) {
val d = decl ?: obj.declaringClass
val storageName = "${d?.className}::$name"
var del = instanceScope[storageName]?.delegate ?: obj.delegate
if (del == null) {
for (c in objClass.mro) {
del = instanceScope["${c.className}::$name"]?.delegate
if (del != null) break
}
}
del = del ?: scope.raiseError("Internal error: delegated property $name has no delegate")
val res = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
obj.value = res
return obj
}
// Map member template to instance storage if applicable
var targetRec = obj
val d = decl ?: obj.declaringClass
if (d != null) {
val mangled = "${d.className}::$name"
instanceScope.objects[mangled]?.let {
targetRec = it
}
}
if (targetRec === obj) {
instanceScope.objects[name]?.let { rec ->
// Check if this record in instanceScope is the one we want.
// For members, it must match the declaring class.
// Arguments are also preferred.
if (rec.type == ObjRecord.Type.Argument || rec.declaringClass == d || rec.declaringClass == null) {
targetRec = rec
}
}
}
return super.resolveRecord(scope, targetRec, name, d)
}
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
willMutate(scope)
val caller = scope.currentClassCtx
// 0. Private mangled of current class context
caller?.let { c ->
// Check for private methods/properties
c.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private) {
updateRecord(scope, resolveRecord(scope, rec, name, c), name, newValue, c)
return
}
}
// Check for private fields (stored in instanceScope)
val mangled = "${c.className}::$name"
instanceScope.objects[mangled]?.let { rec ->
if (rec.visibility == Visibility.Private) {
updateRecord(scope, rec, name, newValue, c)
return
}
}
}
// 1. MRO mangled storage
for (cls in objClass.mro) {
if (cls.className == "Obj") break
val mangled = "${cls.className}::$name"
instanceScope.objects[mangled]?.let { rec ->
if ((scope.thisObj === this && caller != null) || canAccessMember(rec.effectiveWriteVisibility, cls, caller)) {
updateRecord(scope, rec, name, newValue, cls)
return
}
}
}
// 2. Unmangled storage
instanceScope.objects[name]?.let { rec ->
val decl = rec.declaringClass
if ((scope.thisObj === this && caller != null) || canAccessMember(rec.effectiveWriteVisibility, decl, caller)) {
updateRecord(scope, rec, name, newValue, decl)
return
}
}
super.writeField(scope, name, newValue)
}
private suspend fun updateRecord(scope: Scope, rec: ObjRecord, name: String, newValue: Obj, decl: ObjClass?) {
if (rec.type == ObjRecord.Type.Property) {
val prop = rec.value as ObjProperty
prop.callSetter(scope, this, newValue, decl)
return
}
if (rec.type == ObjRecord.Type.Delegated) {
val storageName = "${decl?.className}::$name" val storageName = "${decl?.className}::$name"
var del = instanceScope[storageName]?.delegate var del = instanceScope[storageName]?.delegate
if (del == null) { if (del == null) {
@@ -91,132 +178,82 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
if (del != null) break if (del != null) break
} }
} }
del = del ?: obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)") del = del ?: rec.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
return obj.copy( del.invokeInstanceMethod(scope, "setValue", Arguments(this, ObjString(name), newValue))
value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))),
type = ObjRecord.Type.Other
)
}
return super.resolveRecord(scope, obj, name, decl)
}
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
// Direct (unmangled) first
instanceScope[name]?.let { f ->
val decl = f.declaringClass
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(f.effectiveWriteVisibility, decl, caller))
ObjIllegalAccessException(
scope,
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
).raise()
}
if (f.type == ObjRecord.Type.Property) {
val prop = f.value as ObjProperty
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
return return
} }
// Try MI-mangled resolution along linearization (C3 MRO) if (!rec.isMutable && rec.value !== ObjUnset) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
val cls = objClass if (rec.value.assign(scope, newValue) == null)
fun findMangled(): ObjRecord? { rec.value = newValue
instanceScope.objects["${cls.className}::$name"]?.let { return it }
for (p in cls.mroParents) {
instanceScope.objects["${p.className}::$name"]?.let { return it }
}
return null
}
val rec = findMangled()
if (rec != null) {
val declaring = when {
instanceScope.objects.containsKey("${cls.className}::$name") -> cls
else -> cls.mroParents.firstOrNull { instanceScope.objects.containsKey("${it.className}::$name") }
}
if (scope.thisObj !== this || scope.currentClassCtx == null) {
val caller = scope.currentClassCtx
if (!canAccessMember(rec.effectiveWriteVisibility, declaring, caller))
ObjIllegalAccessException(
scope,
"can't assign to field $name (declared in ${declaring?.className ?: "?"})"
).raise()
}
if (rec.type == ObjRecord.Type.Property) {
val prop = rec.value as ObjProperty
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
return
}
super.writeField(scope, name, newValue)
} }
override suspend fun invokeInstanceMethod( override suspend fun invokeInstanceMethod(
scope: Scope, name: String, args: Arguments, scope: Scope, name: String, args: Arguments,
onNotFoundResult: (suspend () -> Obj?)? onNotFoundResult: (suspend () -> Obj?)?
): Obj { ): Obj {
// 0. Prefer private member of current class context
scope.currentClassCtx?.let { caller ->
val mangled = "${caller.className}::$name"
instanceScope.objects[mangled]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, caller)
} else if (rec.type == ObjRecord.Type.Fun) {
return rec.value.invoke(instanceScope, this, args, caller)
}
}
}
caller.members[name]?.let { rec ->
if (rec.visibility == Visibility.Private && !rec.isAbstract) {
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, caller)
} else if (rec.type == ObjRecord.Type.Fun) {
return rec.value.invoke(instanceScope, this, args, caller)
}
}
}
}
// 1. Walk MRO to find member, handling delegation // 1. Walk MRO to find member, handling delegation
for (cls in objClass.mro) { for (cls in objClass.mro) {
if (cls.className == "Obj") break if (cls.className == "Obj") break
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name) val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
if (rec != null) { if (rec != null && !rec.isAbstract) {
if (rec.type == ObjRecord.Type.Delegated) { if (rec.type == ObjRecord.Type.Delegated) {
val storageName = "${cls.className}::$name" val storageName = "${cls.className}::$name"
val del = instanceScope[storageName]?.delegate ?: rec.delegate val del = instanceScope[storageName]?.delegate ?: rec.delegate
?: scope.raiseError("Internal error: delegated function $name has no delegate (tried $storageName)") ?: scope.raiseError("Internal error: delegated member $name has no delegate (tried $storageName)")
// For delegated member, try 'invoke' first if it's a function-like call
val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray() val allArgs = (listOf(this, ObjString(name)) + args.list).toTypedArray()
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs)) return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs), onNotFoundResult = {
// Fallback: property delegation (getValue then call result)
val propVal = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
propVal.invoke(scope, this, args, rec.declaringClass ?: cls)
})
} }
if (rec.type != ObjRecord.Type.Property && !rec.isAbstract) { val decl = rec.declaringClass ?: cls
val decl = rec.declaringClass ?: cls val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null if (!canAccessMember(rec.visibility, decl, caller))
if (!canAccessMember(rec.visibility, decl, caller)) scope.raiseError(
scope.raiseError( ObjIllegalAccessException(
ObjIllegalAccessException( scope,
scope, "can't invoke method $name (declared in ${decl.className})"
"can't invoke method $name (declared in ${decl.className ?: "?"})"
)
) )
)
if (rec.type == ObjRecord.Type.Property) {
if (args.isEmpty()) return (rec.value as ObjProperty).callGetter(scope, this, decl)
} else if (rec.type == ObjRecord.Type.Fun) {
return rec.value.invoke( return rec.value.invoke(
instanceScope, instanceScope,
this, this,
args, args,
decl decl
) )
} else if (rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.ConstructorField || rec.type == ObjRecord.Type.Argument) {
val resolved = readField(scope, name)
return resolved.value.invoke(scope, this, args, resolved.declaringClass)
} }
} }
} }
@@ -257,7 +294,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
val params = meta.params.map { readField(scope, it.name).value } val params = meta.params.map { readField(scope, it.name).value }
encoder.encodeAnyList(scope, params) encoder.encodeAnyList(scope, params)
val vars = serializingVars.values.map { it.value } val vars = serializingVars.values.map { it.value }
if (vars.isNotEmpty<Obj>()) { if (vars.isNotEmpty()) {
encoder.encodeAnyList(scope, vars) encoder.encodeAnyList(scope, vars)
} }
} }
@@ -288,10 +325,12 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
// } // }
val serializingVars: Map<String, ObjRecord> by lazy { val serializingVars: Map<String, ObjRecord> by lazy {
val metaParams = objClass.constructorMeta?.params?.map { it.name }?.toSet() ?: emptySet()
instanceScope.objects.filter { instanceScope.objects.filter {
it.value.type.serializable && it.value.type.serializable &&
it.value.type == ObjRecord.Type.Field && it.value.type == ObjRecord.Type.Field &&
it.value.isMutable it.value.isMutable &&
!metaParams.contains(it.key)
} }
} }
@@ -300,7 +339,10 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
if (localVars.isNotEmpty()) { if (localVars.isNotEmpty()) {
val vars = decoder.decodeAnyList(scope) val vars = decoder.decodeAnyList(scope)
if (vars.size > serializingVars.size) if (vars.size > serializingVars.size)
scope.raiseIllegalArgument("serialized vars has bigger size than instance vars") scope.raiseIllegalArgument(
"serialized vars has bigger size ${vars.size} than instance vars (${serializingVars.size}): "+
vars.joinToString(",")
)
for ((i, v) in vars.withIndex()) { for ((i, v) in vars.withIndex()) {
localVars[i].value = v localVars[i].value = v
} }
@@ -318,7 +360,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
if (other.objClass != objClass) return -1 if (other.objClass != objClass) return -1
for (f in comparableVars) { for (f in comparableVars) {
val a = f.value.value val a = f.value.value
val b = other.instanceScope[f.key]!!.value val b = other.instanceScope.objects[f.key]?.value ?: scope.raiseError("Internal error: field ${f.key} not found in other instance")
val d = a.compareTo(scope, b) val d = a.compareTo(scope, b)
if (d != 0) return d if (d != 0) return d
} }
@@ -345,7 +387,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
val caller = scope.currentClassCtx val caller = scope.currentClassCtx
if (!canAccessMember(rec.visibility, decl, caller)) if (!canAccessMember(rec.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't access field $name (declared in ${decl.className})")) scope.raiseError(ObjIllegalAccessException(scope, "can't access field $name (declared in ${decl.className})"))
return resolveRecord(scope, rec, name, decl) return instance.resolveRecord(scope, rec, name, decl)
} }
// Then try instance locals (unmangled) only if startClass is the dynamic class itself // Then try instance locals (unmangled) only if startClass is the dynamic class itself
if (startClass === instance.objClass) { if (startClass === instance.objClass) {
@@ -359,7 +401,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
"can't access field $name (declared in ${decl?.className ?: "?"})" "can't access field $name (declared in ${decl?.className ?: "?"})"
) )
) )
return resolveRecord(scope, rec, name, decl) return instance.resolveRecord(scope, rec, name, decl)
} }
} }
// Finally try methods/properties starting from ancestor // Finally try methods/properties starting from ancestor
@@ -369,7 +411,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
if (!canAccessMember(r.visibility, decl, caller)) if (!canAccessMember(r.visibility, decl, caller))
scope.raiseError(ObjIllegalAccessException(scope, "can't access field $name (declared in ${decl.className})")) scope.raiseError(ObjIllegalAccessException(scope, "can't access field $name (declared in ${decl.className})"))
return resolveRecord(scope, r, name, decl) return instance.resolveRecord(scope, r, name, decl)
} }
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) { override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
@@ -42,9 +42,6 @@ class ObjInstanceClass(val name: String, vararg parents: ObjClass) : ObjClass(na
} }
init { init {
addFn("toString", true) {
thisObj.toString(this, true)
}
} }
} }
@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,6 +20,9 @@ package net.sergeych.lyng.obj
import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.JsonPrimitive
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.addFnDoc
import net.sergeych.lyng.miniast.addPropertyDoc
import net.sergeych.lyng.miniast.type
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
import net.sergeych.lynon.LynonEncoder import net.sergeych.lynon.LynonEncoder
import net.sergeych.lynon.LynonSettings import net.sergeych.lynon.LynonSettings
@@ -148,34 +151,71 @@ class ObjInstant(val instant: Instant,val truncateMode: LynonSettings.InstantTru
} }
}.apply { }.apply {
addFn("epochSeconds") { addPropertyDoc(
val instant = thisAs<ObjInstant>().instant name = "epochSeconds",
ObjReal(instant.epochSeconds + instant.nanosecondsOfSecond * 1e-9) doc = "Return the number of seconds since the Unix epoch as a real number (including fractions).",
} type = type("lyng.Real"),
addFn("isDistantFuture") { moduleName = "lyng.time",
thisAs<ObjInstant>().instant.isDistantFuture.toObj() getter = {
} val instant = thisAs<ObjInstant>().instant
addFn("isDistantPast") { ObjReal(instant.epochSeconds + instant.nanosecondsOfSecond * 1e-9)
thisAs<ObjInstant>().instant.isDistantPast.toObj() }
} )
addFn("epochWholeSeconds") { addPropertyDoc(
ObjInt(thisAs<ObjInstant>().instant.epochSeconds) name = "isDistantFuture",
} doc = "Whether this instant represents the distant future.",
addFn("nanosecondsOfSecond") { type = type("lyng.Bool"),
ObjInt(thisAs<ObjInstant>().instant.nanosecondsOfSecond.toLong()) moduleName = "lyng.time",
} getter = { thisAs<ObjInstant>().instant.isDistantFuture.toObj() }
addFn("truncateToSecond") { )
addPropertyDoc(
name = "isDistantPast",
doc = "Whether this instant represents the distant past.",
type = type("lyng.Bool"),
moduleName = "lyng.time",
getter = { thisAs<ObjInstant>().instant.isDistantPast.toObj() }
)
addPropertyDoc(
name = "epochWholeSeconds",
doc = "Return the number of full seconds since the Unix epoch.",
type = type("lyng.Int"),
moduleName = "lyng.time",
getter = { ObjInt(thisAs<ObjInstant>().instant.epochSeconds) }
)
addPropertyDoc(
name = "nanosecondsOfSecond",
doc = "The number of nanoseconds within the current second.",
type = type("lyng.Int"),
moduleName = "lyng.time",
getter = { ObjInt(thisAs<ObjInstant>().instant.nanosecondsOfSecond.toLong()) }
)
addFnDoc(
name = "truncateToSecond",
doc = "Truncate this instant to the nearest second.",
returns = type("lyng.Instant"),
moduleName = "lyng.time"
) {
val t = thisAs<ObjInstant>().instant val t = thisAs<ObjInstant>().instant
ObjInstant(Instant.fromEpochSeconds(t.epochSeconds), LynonSettings.InstantTruncateMode.Second) ObjInstant(Instant.fromEpochSeconds(t.epochSeconds), LynonSettings.InstantTruncateMode.Second)
} }
addFn("truncateToMillisecond") { addFnDoc(
name = "truncateToMillisecond",
doc = "Truncate this instant to the nearest millisecond.",
returns = type("lyng.Instant"),
moduleName = "lyng.time"
) {
val t = thisAs<ObjInstant>().instant val t = thisAs<ObjInstant>().instant
ObjInstant( ObjInstant(
Instant.fromEpochSeconds(t.epochSeconds, t.nanosecondsOfSecond / 1_000_000 * 1_000_000), Instant.fromEpochSeconds(t.epochSeconds, t.nanosecondsOfSecond / 1_000_000 * 1_000_000),
LynonSettings.InstantTruncateMode.Millisecond LynonSettings.InstantTruncateMode.Millisecond
) )
} }
addFn("truncateToMicrosecond") { addFnDoc(
name = "truncateToMicrosecond",
doc = "Truncate this instant to the nearest microsecond.",
returns = type("lyng.Instant"),
moduleName = "lyng.time"
) {
val t = thisAs<ObjInstant>().instant val t = thisAs<ObjInstant>().instant
ObjInstant( ObjInstant(
Instant.fromEpochSeconds(t.epochSeconds, t.nanosecondsOfSecond / 1_000 * 1_000), Instant.fromEpochSeconds(t.epochSeconds, t.nanosecondsOfSecond / 1_000 * 1_000),
Loaded 100 of 141 files, more files were not shown because too many files have changed in this diff. Show more