Compare commits

..
Author SHA1 Message Date
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
85 changed files with 3190 additions and 426 deletions

No files matched your search

+5
View File
@@ -21,3 +21,8 @@ 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
+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.
+25 -18
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)
@@ -191,6 +188,16 @@ 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 `?=`
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.
+42
View File
@@ -71,6 +71,48 @@ 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.
-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.
+2 -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
} }
+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) | ❌ |
+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`.
+35 -10
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:
@@ -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.
+31
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,21 @@ 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.
### 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
@@ -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": ":" } ] }
@@ -94,6 +94,16 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
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)
} }
@@ -207,16 +217,6 @@ 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) { for (d in mini.declarations) when (d) {
@@ -253,7 +253,7 @@ 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) { for (s in tokens) if (s.kind == HighlightKind.Label) {
@@ -261,8 +261,29 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
val end = s.range.endExclusive val end = s.range.endExclusive
if (start in 0..end && end <= text.length && start < end) { if (start in 0..end && end <= text.length && start < end) {
val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null } val lexeme = try { text.substring(start, end) } catch (_: Throwable) { null }
if (lexeme != null && lexeme.startsWith("@")) { if (lexeme != null) {
putRange(start, end, LyngHighlighterColors.ANNOTATION) // 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)
}
}
}
} }
} }
} }
@@ -364,6 +385,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
@@ -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)
@@ -545,27 +539,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>()
@@ -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")
} }
+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.1.1-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
@@ -95,6 +95,11 @@ class Compiler(
} }
} }
private var anonCounter = 0
private fun generateAnonName(pos: Pos): String {
return "${"$"}${"Anon"}_${pos.line+1}_${pos.column}_${++anonCounter}"
}
private fun pushPendingDocToken(t: Token) { private fun pushPendingDocToken(t: Token) {
val s = stripCommentLexeme(t.value) val s = stripCommentLexeme(t.value)
if (pendingDocStart == null) pendingDocStart = t.pos if (pendingDocStart == null) pendingDocStart = t.pos
@@ -170,7 +175,6 @@ class Compiler(
// A standalone newline not immediately following a comment resets doc buffer // A standalone newline not immediately following a comment resets doc buffer
if (!prevWasComment) clearPendingDoc() else prevWasComment = false if (!prevWasComment) clearPendingDoc() else prevWasComment = false
} }
else -> {} else -> {}
} }
cc.next() cc.next()
@@ -248,7 +252,7 @@ class Compiler(
when (t.type) { when (t.type) {
Token.Type.RBRACE, Token.Type.EOF, Token.Type.SEMICOLON -> {} Token.Type.RBRACE, Token.Type.EOF, Token.Type.SEMICOLON -> {}
else -> else ->
throw ScriptError(t.pos, "unexpeced `${t.value}` here") throw ScriptError(t.pos, "unexpected `${t.value}` here")
} }
break break
} }
@@ -280,9 +284,11 @@ class Compiler(
} }
private var lastAnnotation: (suspend (Scope, ObjString, Statement) -> Statement)? = null private var lastAnnotation: (suspend (Scope, ObjString, Statement) -> Statement)? = null
private var lastLabel: String? = null
private suspend fun parseStatement(braceMeansLambda: Boolean = false): Statement? { private suspend fun parseStatement(braceMeansLambda: Boolean = false): Statement? {
lastAnnotation = null lastAnnotation = null
lastLabel = null
while (true) { while (true) {
val t = cc.next() val t = cc.next()
return when (t.type) { return when (t.type) {
@@ -300,6 +306,10 @@ class Compiler(
} }
Token.Type.ATLABEL -> { Token.Type.ATLABEL -> {
val label = t.value
if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
lastLabel = label
}
lastAnnotation = parseAnnotation(t) lastAnnotation = parseAnnotation(t)
continue continue
} }
@@ -648,6 +658,7 @@ class Compiler(
private suspend fun parseLambdaExpression(): ObjRef { private suspend fun parseLambdaExpression(): ObjRef {
// lambda args are different: // lambda args are different:
val startPos = cc.currentPos() val startPos = cc.currentPos()
val label = lastLabel
val argsDeclaration = parseArgsDeclaration() val argsDeclaration = parseArgsDeclaration()
if (argsDeclaration != null && argsDeclaration.endTokenType != Token.Type.ARROW) if (argsDeclaration != null && argsDeclaration.endTokenType != Token.Type.ARROW)
throw ScriptError( throw ScriptError(
@@ -655,7 +666,9 @@ class Compiler(
"lambda must have either valid arguments declaration with '->' or no arguments" "lambda must have either valid arguments declaration with '->' or no arguments"
) )
label?.let { cc.labels.add(it) }
val body = parseBlock(skipLeadingBrace = true) val body = parseBlock(skipLeadingBrace = true)
label?.let { cc.labels.remove(it) }
return ValueFnRef { closureScope -> return ValueFnRef { closureScope ->
statement { statement {
@@ -679,7 +692,12 @@ class Compiler(
// assign vars as declared the standard way // assign vars as declared the standard way
argsDeclaration.assignToContext(context, defaultAccessType = AccessType.Val) argsDeclaration.assignToContext(context, defaultAccessType = AccessType.Val)
} }
body.execute(context) try {
body.execute(context)
} catch (e: ReturnException) {
if (e.label == null || e.label == label) e.result
else throw e
}
}.asReadonly }.asReadonly
} }
} }
@@ -1248,6 +1266,8 @@ class Compiler(
} }
} }
Token.Type.OBJECT -> StatementRef(parseObjectDeclaration())
else -> null else -> null
} }
} }
@@ -1418,6 +1438,7 @@ class Compiler(
"while" -> parseWhileStatement() "while" -> parseWhileStatement()
"do" -> parseDoWhileStatement() "do" -> parseDoWhileStatement()
"for" -> parseForStatement() "for" -> parseForStatement()
"return" -> parseReturnStatement(id.pos)
"break" -> parseBreakStatement(id.pos) "break" -> parseBreakStatement(id.pos)
"continue" -> parseContinueStatement(id.pos) "continue" -> parseContinueStatement(id.pos)
"if" -> parseIfStatement() "if" -> parseIfStatement()
@@ -1773,6 +1794,10 @@ class Compiler(
try { try {
// body is a parsed block, it already has separate context // body is a parsed block, it already has separate context
result = body.execute(this) result = body.execute(this)
} catch (e: ReturnException) {
throw e
} catch (e: LoopBreakContinueException) {
throw e
} catch (e: Exception) { } catch (e: Exception) {
// convert to appropriate exception // convert to appropriate exception
val objException = when (e) { val objException = when (e) {
@@ -1860,8 +1885,12 @@ class Compiler(
} }
private suspend fun parseObjectDeclaration(): Statement { private suspend fun parseObjectDeclaration(): Statement {
val nameToken = cc.requireToken(Token.Type.ID) val next = cc.peekNextNonWhitespace()
val startPos = pendingDeclStart ?: nameToken.pos val nameToken = if (next.type == Token.Type.ID) cc.requireToken(Token.Type.ID) else null
val startPos = pendingDeclStart ?: nameToken?.pos ?: cc.current().pos
val className = nameToken?.value ?: generateAnonName(startPos)
val doc = pendingDeclDoc ?: consumePendingDoc() val doc = pendingDeclDoc ?: consumePendingDoc()
pendingDeclDoc = null pendingDeclDoc = null
pendingDeclStart = null pendingDeclStart = null
@@ -1887,11 +1916,11 @@ class Compiler(
// Robust body detection // Robust body detection
var classBodyRange: MiniRange? = null var classBodyRange: MiniRange? = null
val bodyInit: Statement? = run { val bodyInit: Statement? = inCodeContext(CodeContext.ClassBody(className)) {
val saved = cc.savePos() val saved = cc.savePos()
val next = cc.nextNonWhitespace() val nextBody = cc.nextNonWhitespace()
if (next.type == Token.Type.LBRACE) { if (nextBody.type == Token.Type.LBRACE) {
val bodyStart = next.pos val bodyStart = nextBody.pos
val st = withLocalNames(emptySet()) { val st = withLocalNames(emptySet()) {
parseScript() parseScript()
} }
@@ -1906,15 +1935,15 @@ class Compiler(
} }
val initScope = popInitScope() val initScope = popInitScope()
val className = nameToken.value
return statement(startPos) { context -> return statement(startPos) { context ->
val parentClasses = baseSpecs.map { baseSpec -> val parentClasses = baseSpecs.map { baseSpec ->
val rec = context[baseSpec.name] ?: throw ScriptError(nameToken.pos, "unknown base class: ${baseSpec.name}") val rec = context[baseSpec.name] ?: throw ScriptError(startPos, "unknown base class: ${baseSpec.name}")
(rec.value as? ObjClass) ?: throw ScriptError(nameToken.pos, "${baseSpec.name} is not a class") (rec.value as? ObjClass) ?: throw ScriptError(startPos, "${baseSpec.name} is not a class")
} }
val newClass = ObjInstanceClass(className, *parentClasses.toTypedArray()) val newClass = ObjInstanceClass(className, *parentClasses.toTypedArray())
newClass.isAnonymous = nameToken == null
for (i in parentClasses.indices) { for (i in parentClasses.indices) {
val argsList = baseSpecs[i].args val argsList = baseSpecs[i].args
// In object, we evaluate parent args once at creation time // In object, we evaluate parent args once at creation time
@@ -1924,12 +1953,14 @@ class Compiler(
val classScope = context.createChildScope(newThisObj = newClass) val classScope = context.createChildScope(newThisObj = newClass)
classScope.currentClassCtx = newClass classScope.currentClassCtx = newClass
newClass.classScope = classScope newClass.classScope = classScope
classScope.addConst("object", newClass)
bodyInit?.execute(classScope) bodyInit?.execute(classScope)
// Create instance (singleton) // Create instance (singleton)
val instance = newClass.callOn(context.createChildScope(Arguments.EMPTY)) val instance = newClass.callOn(context.createChildScope(Arguments.EMPTY))
context.addItem(className, false, instance) if (nameToken != null)
context.addItem(className, false, instance)
instance instance
} }
} }
@@ -1999,20 +2030,18 @@ class Compiler(
run { run {
val declRange = MiniRange(startPos, cc.currentPos()) val declRange = MiniRange(startPos, cc.currentPos())
val bases = baseSpecs.map { it.name } val bases = baseSpecs.map { it.name }
// Collect constructor fields declared as val/var in primary constructor // Collect constructor fields declared in primary constructor
val ctorFields = mutableListOf<MiniCtorField>() val ctorFields = mutableListOf<MiniCtorField>()
constructorArgsDeclaration?.let { ad -> constructorArgsDeclaration?.let { ad ->
for (p in ad.params) { for (p in ad.params) {
val at = p.accessType val at = p.accessType
if (at != null) { val mutable = at == AccessType.Var
val mutable = at == AccessType.Var ctorFields += MiniCtorField(
ctorFields += MiniCtorField( name = p.name,
name = p.name, mutable = mutable,
mutable = mutable, type = p.miniType,
type = p.miniType, nameStart = p.pos
nameStart = p.pos )
)
}
} }
} }
val node = MiniClassDecl( val node = MiniClassDecl(
@@ -2475,6 +2504,38 @@ class Compiler(
} }
} }
private suspend fun parseReturnStatement(start: Pos): Statement {
var t = cc.next()
val label = if (t.pos.line != start.line || t.type != Token.Type.ATLABEL) {
cc.previous()
null
} else {
t.value
}
// expression?
t = cc.next()
cc.previous()
val resultExpr = if (t.pos.line == start.line && (!t.isComment &&
t.type != Token.Type.SEMICOLON &&
t.type != Token.Type.NEWLINE &&
t.type != Token.Type.RBRACE &&
t.type != Token.Type.RPAREN &&
t.type != Token.Type.RBRACKET &&
t.type != Token.Type.COMMA &&
t.type != Token.Type.EOF)
) {
// we have something on this line, could be expression
parseExpression()
} else null
return statement(start) {
val returnValue = resultExpr?.execute(it) ?: ObjVoid
throw ReturnException(returnValue, label)
}
}
private fun ensureRparen(): Pos { private fun ensureRparen(): Pos {
val t = cc.next() val t = cc.next()
if (t.type != Token.Type.RPAREN) if (t.type != Token.Type.RPAREN)
@@ -2588,6 +2649,7 @@ class Compiler(
// Capture doc locally to reuse even if we need to emit later // Capture doc locally to reuse even if we need to emit later
val declDocLocal = pendingDeclDoc val declDocLocal = pendingDeclDoc
val outerLabel = lastLabel
// Emit MiniFunDecl before body parsing (body range unknown yet) // Emit MiniFunDecl before body parsing (body range unknown yet)
run { run {
@@ -2614,6 +2676,8 @@ class Compiler(
} }
return inCodeContext(CodeContext.Function(name)) { return inCodeContext(CodeContext.Function(name)) {
cc.labels.add(name)
outerLabel?.let { cc.labels.add(it) }
val paramNames: Set<String> = argsDeclaration.params.map { it.name }.toSet() val paramNames: Set<String> = argsDeclaration.params.map { it.name }.toSet()
@@ -2629,7 +2693,9 @@ class Compiler(
val next = cc.peekNextNonWhitespace() val next = cc.peekNextNonWhitespace()
if (next.type == Token.Type.ASSIGN) { if (next.type == Token.Type.ASSIGN) {
cc.nextNonWhitespace() // consume '=' cc.nextNonWhitespace() // consume '='
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected function body expression") if (cc.peekNextNonWhitespace().value == "return")
throw ScriptError(cc.currentPos(), "return is not allowed in shorthand function")
val expr = parseExpression() ?: throw ScriptError(cc.currentPos(), "Expected function body expression")
// Shorthand function returns the expression value // Shorthand function returns the expression value
statement(expr.pos) { scope -> statement(expr.pos) { scope ->
expr.execute(scope) expr.execute(scope)
@@ -2661,8 +2727,15 @@ class Compiler(
if (extTypeName != null) { if (extTypeName != null) {
context.thisObj = callerContext.thisObj context.thisObj = callerContext.thisObj
} }
fnStatements?.execute(context) ?: ObjVoid try {
fnStatements?.execute(context) ?: ObjVoid
} catch (e: ReturnException) {
if (e.label == null || e.label == name || e.label == outerLabel) e.result
else throw e
}
} }
cc.labels.remove(name)
outerLabel?.let { cc.labels.remove(it) }
// parentContext // parentContext
val fnCreateStatement = statement(start) { context -> val fnCreateStatement = statement(start) { context ->
if (isDelegated) { if (isDelegated) {
@@ -2675,7 +2748,7 @@ class Compiler(
} }
if (extTypeName != null) { if (extTypeName != null) {
val type = context[extTypeName!!]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found") val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance") if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
context.addExtension(type, name, ObjRecord(ObjUnset, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply { context.addExtension(type, name, ObjRecord(ObjUnset, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply {
delegate = finalDelegate delegate = finalDelegate
@@ -2697,7 +2770,7 @@ class Compiler(
cls.createField(name, ObjUnset, false, visibility, null, start, declaringClass = cls, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride, type = ObjRecord.Type.Delegated) cls.createField(name, ObjUnset, false, visibility, null, start, declaringClass = cls, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride, type = ObjRecord.Type.Delegated)
cls.instanceInitializers += statement(start) { scp -> cls.instanceInitializers += statement(start) { scp ->
val accessType2 = scp.resolveQualifiedIdentifier("DelegateAccess.Callable") val accessType2 = scp.resolveQualifiedIdentifier("DelegateAccess.Callable")
val initValue2 = delegateExpression!!.execute(scp) val initValue2 = delegateExpression.execute(scp)
val finalDelegate2 = try { val finalDelegate2 = try {
initValue2.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType2, scp.thisObj)) initValue2.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType2, scp.thisObj))
} catch (e: Exception) { } catch (e: Exception) {
@@ -3141,23 +3214,14 @@ class Compiler(
} }
} }
return object : Statement() { return statement(start) { context ->
override val pos: Pos = start
override suspend fun execute(context: Scope): Obj {
if (extTypeName != null) { if (extTypeName != null) {
val prop = if (getter != null || setter != null) { val prop = if (getter != null || setter != null) {
ObjProperty(name, getter, setter) ObjProperty(name, getter, setter)
} else { } else {
// Simple val extension with initializer // Simple val extension with initializer
val initExpr = initialExpression ?: throw ScriptError(start, "Extension val must be initialized") val initExpr = initialExpression ?: throw ScriptError(start, "Extension val must be initialized")
ObjProperty( ObjProperty(name, statement(initExpr.pos) { scp -> initExpr.execute(scp) }, null)
name,
object : Statement() {
override val pos: Pos = initExpr.pos
override suspend fun execute(scp: Scope): Obj = initExpr.execute(scp)
},
null
)
} }
val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found") val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
@@ -3165,7 +3229,7 @@ class Compiler(
context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, writeVisibility = setterVisibility, declaringClass = null, type = ObjRecord.Type.Property)) context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, writeVisibility = setterVisibility, declaringClass = null, type = ObjRecord.Type.Property))
return prop return@statement prop
} }
// In true class bodies (not inside a function), store fields under a class-qualified key to support MI collisions // In true class bodies (not inside a function), store fields under a class-qualified key to support MI collisions
// Do NOT infer declaring class from runtime thisObj here; only the compile-time captured // Do NOT infer declaring class from runtime thisObj here; only the compile-time captured
@@ -3198,34 +3262,27 @@ class Compiler(
isClosed = isClosed, isClosed = isClosed,
isOverride = isOverride isOverride = isOverride
) )
cls.instanceInitializers += object : Statement() { cls.instanceInitializers += statement(start) { scp ->
override val pos: Pos = start val initValue = initialExpression!!.execute(scp)
override suspend fun execute(scp: Scope): Obj { val accessTypeStr = if (isMutable) "Var" else "Val"
val initValue = initialExpression!!.execute(scp) val accessType = scp.resolveQualifiedIdentifier("DelegateAccess.$accessTypeStr")
val accessTypeStr = if (isMutable) "Var" else "Val" val finalDelegate = try {
val accessType = scp.resolveQualifiedIdentifier("DelegateAccess.$accessTypeStr") initValue.invokeInstanceMethod(scp, "bind", Arguments(ObjString(name), accessType, scp.thisObj))
val finalDelegate = try { } catch (e: Exception) {
initValue.invokeInstanceMethod( initValue
scp,
"bind",
Arguments(ObjString(name), accessType, scp.thisObj)
)
} catch (e: Exception) {
initValue
}
scp.addItem(
storageName, isMutable, ObjUnset, visibility, setterVisibility,
recordType = ObjRecord.Type.Delegated,
isAbstract = isAbstract,
isClosed = isClosed,
isOverride = isOverride
).apply {
delegate = finalDelegate
}
return ObjVoid
} }
scp.addItem(
storageName, isMutable, ObjUnset, visibility, setterVisibility,
recordType = ObjRecord.Type.Delegated,
isAbstract = isAbstract,
isClosed = isClosed,
isOverride = isOverride
).apply {
delegate = finalDelegate
}
ObjVoid
} }
return ObjVoid return@statement ObjVoid
} else { } else {
val initValue = initialExpression!!.execute(context) val initValue = initialExpression!!.execute(context)
val accessTypeStr = if (isMutable) "Var" else "Val" val accessTypeStr = if (isMutable) "Var" else "Val"
@@ -3243,7 +3300,7 @@ class Compiler(
isOverride = isOverride isOverride = isOverride
) )
rec.delegate = finalDelegate rec.delegate = finalDelegate
return finalDelegate return@statement finalDelegate
} }
} else { } else {
val initValue = initialExpression!!.execute(context) val initValue = initialExpression!!.execute(context)
@@ -3262,7 +3319,7 @@ class Compiler(
isOverride = isOverride isOverride = isOverride
) )
rec.delegate = finalDelegate rec.delegate = finalDelegate
return finalDelegate return@statement finalDelegate
} }
} else if (getter != null || setter != null) { } else if (getter != null || setter != null) {
val declaringClassName = declaringClassNameCaptured!! val declaringClassName = declaringClassNameCaptured!!
@@ -3271,7 +3328,7 @@ class Compiler(
// If we are in class scope now (defining instance field), defer initialization to instance time // If we are in class scope now (defining instance field), defer initialization to instance time
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance) val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
return if (isClassScope) { if (isClassScope) {
val cls = context.thisObj as ObjClass val cls = context.thisObj as ObjClass
// Register in class members for reflection/MRO/satisfaction checks // Register in class members for reflection/MRO/satisfaction checks
if (isProperty) { if (isProperty) {
@@ -3300,22 +3357,19 @@ class Compiler(
// Register the property/field initialization thunk // Register the property/field initialization thunk
if (!isAbstract) { if (!isAbstract) {
cls.instanceInitializers += object : Statement() { cls.instanceInitializers += statement(start) { scp ->
override val pos: Pos = start scp.addItem(
override suspend fun execute(scp: Scope): Obj { storageName,
scp.addItem( isMutable,
storageName, prop,
isMutable, visibility,
prop, setterVisibility,
visibility, recordType = ObjRecord.Type.Property,
setterVisibility, isAbstract = isAbstract,
recordType = ObjRecord.Type.Property, isClosed = isClosed,
isAbstract = isAbstract, isOverride = isOverride
isClosed = isClosed, )
isOverride = isOverride ObjVoid
)
return ObjVoid
}
} }
} }
ObjVoid ObjVoid
@@ -3331,32 +3385,30 @@ class Compiler(
prop prop
} }
} else { } else {
val isLateInitVal = !isMutable && initialExpression == null && getter == null && setter == null val isLateInitVal = !isMutable && initialExpression == null
return if (declaringClassName != null && !isStatic) { if (declaringClassName != null && !isStatic) {
val storageName = "$declaringClassName::$name" val storageName = "$declaringClassName::$name"
// If we are in class scope now (defining instance field), defer initialization to instance time // If we are in class scope now (defining instance field), defer initialization to instance time
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance) val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
if (isClassScope) { if (isClassScope) {
val cls = context.thisObj as ObjClass val cls = context.thisObj as ObjClass
// Register in class members for reflection/MRO/satisfaction checks // Register in class members for reflection/MRO/satisfaction checks
cls.createField( cls.createField(
name, name,
ObjNull, ObjNull,
isMutable = isMutable, isMutable = isMutable,
visibility = visibility, visibility = visibility,
writeVisibility = setterVisibility, writeVisibility = setterVisibility,
isAbstract = isAbstract, isAbstract = isAbstract,
isClosed = isClosed, isClosed = isClosed,
isOverride = isOverride, isOverride = isOverride,
pos = start, pos = start,
type = ObjRecord.Type.Field type = ObjRecord.Type.Field
) )
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name // Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
if (!isAbstract) { if (!isAbstract) {
val initStmt = object : Statement() { val initStmt = statement(start) { scp ->
override val pos: Pos = start
override suspend fun execute(scp: Scope): Obj {
val initValue = val initValue =
initialExpression?.execute(scp)?.byValueCopy() initialExpression?.execute(scp)?.byValueCopy()
?: if (isLateInitVal) ObjUnset else ObjNull ?: if (isLateInitVal) ObjUnset else ObjNull
@@ -3368,38 +3420,35 @@ class Compiler(
isClosed = isClosed, isClosed = isClosed,
isOverride = isOverride isOverride = isOverride
) )
return ObjVoid ObjVoid
} }
cls.instanceInitializers += initStmt
} }
cls.instanceInitializers += initStmt ObjVoid
} else {
// We are in instance scope already: perform initialization immediately
val initValue =
initialExpression?.execute(context)?.byValueCopy() ?: if (isLateInitVal) ObjUnset else ObjNull
// Preserve mutability of declaration: create record with correct mutability
context.addItem(
storageName, isMutable, initValue, visibility, setterVisibility,
recordType = ObjRecord.Type.Field,
isAbstract = isAbstract,
isClosed = isClosed,
isOverride = isOverride
)
initValue
} }
ObjVoid
} else { } else {
// We are in instance scope already: perform initialization immediately // Not in class body: regular local/var declaration
val initValue = val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
initialExpression?.execute(context)?.byValueCopy() ?: if (isLateInitVal) ObjUnset else ObjNull context.addItem(name, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
// Preserve mutability of declaration: create record with correct mutability
context.addItem(
storageName, isMutable, initValue, visibility, setterVisibility,
recordType = ObjRecord.Type.Field,
isAbstract = isAbstract,
isClosed = isClosed,
isOverride = isOverride
)
initValue initValue
} }
} else {
// Not in class body: regular local/var declaration
val initValue = initialExpression?.execute(context)?.byValueCopy() ?: ObjNull
context.addItem(name, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
initValue
}
} }
} }
} }
}
data class Operator( data class Operator(
val tokenType: Token.Type, val tokenType: Token.Type,
val priority: Int, val arity: Int = 2, val priority: Int, val arity: Int = 2,
@@ -3552,6 +3601,9 @@ class Compiler(
Operator(Token.Type.PERCENTASSIGN, lastPriority) { pos, a, b -> Operator(Token.Type.PERCENTASSIGN, lastPriority) { pos, a, b ->
AssignOpRef(BinOp.PERCENT, a, b, pos) AssignOpRef(BinOp.PERCENT, a, b, pos)
}, },
Operator(Token.Type.IFNULLASSIGN, lastPriority) { pos, a, b ->
AssignIfNullRef(a, b, pos)
},
// logical 1 // logical 1
Operator(Token.Type.OR, ++lastPriority) { _, a, b -> Operator(Token.Type.OR, ++lastPriority) { _, a, b ->
foldBinary(BinOp.OR, a, b)?.let { return@Operator ConstRef(it.asReadonly) } foldBinary(BinOp.OR, a, b)?.let { return@Operator ConstRef(it.asReadonly) }
@@ -3706,3 +3758,4 @@ class Compiler(
} }
suspend fun eval(code: String) = compile(code).execute() suspend fun eval(code: String) = compile(code).execute()
@@ -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()
@@ -314,11 +314,11 @@ 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}")
} }
@@ -623,10 +623,18 @@ 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) { if (rec.type == ObjRecord.Type.Delegated) {
val del = rec.delegate ?: raiseError("Internal error: delegated property $name has no delegate") val del = rec.delegate ?: run {
if (thisObj is ObjInstance) {
val res = (thisObj as ObjInstance).resolveRecord(this, rec, name, rec.declaringClass).value
rec.value = res
return res
}
raiseError("Internal error: delegated property $name has no delegate")
}
val th = if (thisObj === ObjVoid) ObjNull else thisObj val th = if (thisObj === ObjVoid) ObjNull else thisObj
if (del.objClass.getInstanceMemberOrNull("getValue") == null) { val res = del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name)), onNotFoundResult = {
return object : Statement() { // If getValue not found, return a wrapper that calls invoke
object : Statement() {
override val pos: Pos = Pos.builtIn override val pos: Pos = Pos.builtIn
override suspend fun execute(scope: Scope): Obj { override suspend fun execute(scope: Scope): Obj {
val th2 = if (scope.thisObj === ObjVoid) ObjNull else scope.thisObj val th2 = if (scope.thisObj === ObjVoid) ObjNull else scope.thisObj
@@ -634,15 +642,22 @@ open class Scope(
return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs)) return del.invokeInstanceMethod(scope, "invoke", Arguments(*allArgs))
} }
} }
} })
return del.invokeInstanceMethod(this, "getValue", Arguments(th, ObjString(name))) rec.value = res
return res
} }
return rec.value 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 del = rec.delegate ?: run {
if (thisObj is ObjInstance) {
(thisObj as ObjInstance).writeField(this, name, newValue)
return
}
raiseError("Internal error: delegated property $name has no delegate")
}
val th = if (thisObj === ObjVoid) ObjNull else thisObj val th = if (thisObj === ObjVoid) ObjNull else thisObj
del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue)) del.invokeInstanceMethod(this, "setValue", Arguments(th, ObjString(name), newValue))
return return
@@ -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,
@@ -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,
@@ -23,6 +23,7 @@ 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.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). */
@@ -96,18 +97,20 @@ 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 -> if (mini != null) {
val decls = m.declarations offerParamsInScope(out, prefix, mini, text, caret)
val funs = decls.filterIsInstance<MiniFunDecl>().sortedBy { it.name.lowercase() }
val classes = decls.filterIsInstance<MiniClassDecl>().sortedBy { it.name.lowercase() }
val enums = decls.filterIsInstance<MiniEnumDecl>().sortedBy { it.name.lowercase() }
val vals = decls.filterIsInstance<MiniValDecl>().sortedBy { it.name.lowercase() }
funs.forEach { offerDeclAdd(out, prefix, it) }
classes.forEach { offerDeclAdd(out, prefix, it) }
enums.forEach { offerDeclAdd(out, prefix, it) }
vals.forEach { offerDeclAdd(out, prefix, it) }
} }
val decls = mini?.declarations ?: emptyList()
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 +135,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 +263,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 -> {}
@@ -137,11 +137,20 @@ 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() scope.raiseNotImplemented()
} }
open suspend fun equals(scope: Scope, other: Obj): Boolean {
if (other === this) return true
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 {
return invokeInstanceMethod(scope, "contains", other).toBool() return invokeInstanceMethod(scope, "contains", other).toBool()
} }
@@ -364,7 +373,7 @@ 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")
return obj.copy( return obj.copy(
@@ -442,17 +451,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(
@@ -30,6 +30,7 @@ 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 {
addProperty("className", getter = { thisAs<ObjClass>().classNameObj })
addFnDoc( addFnDoc(
name = "name", name = "name",
doc = "Simple name of this class (without package).", doc = "Simple name of this class (without package).",
@@ -91,6 +92,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
@@ -181,11 +184,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
} }
} }
@@ -235,7 +235,7 @@ open class ObjClass(
// 1) members-defined methods // 1) members-defined methods
for ((k, v) in members) { for ((k, v) in members) {
if (v.value is Statement || v.type == ObjRecord.Type.Delegated) { if (v.value is Statement || v.type == ObjRecord.Type.Delegated) {
instance.instanceScope.objects[k] = v instance.instanceScope.objects[k] = if (v.type == ObjRecord.Type.Delegated) v.copy() else v
} }
} }
// 2) class-scope methods registered during class-body execution // 2) class-scope methods registered during class-body execution
@@ -243,7 +243,7 @@ open class ObjClass(
if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) { if (rec.value is Statement || rec.type == ObjRecord.Type.Delegated) {
// if not already present, copy reference for dispatch // if not already present, copy reference for dispatch
if (!instance.instanceScope.objects.containsKey(k)) { if (!instance.instanceScope.objects.containsKey(k)) {
instance.instanceScope.objects[k] = rec instance.instanceScope.objects[k] = if (rec.type == ObjRecord.Type.Delegated) rec.copy() else rec
} }
} }
} }
@@ -480,7 +480,7 @@ 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)
} }
@@ -588,11 +588,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)
} }
@@ -92,10 +92,9 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
} }
} }
del = del ?: obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)") del = del ?: obj.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate (tried $storageName)")
return obj.copy( val res = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name)))
value = del.invokeInstanceMethod(scope, "getValue", Arguments(this, ObjString(name))), obj.value = res
type = ObjRecord.Type.Other return obj
)
} }
return super.resolveRecord(scope, obj, name, decl) return super.resolveRecord(scope, obj, name, decl)
} }
@@ -197,18 +196,22 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
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)")
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, rec.declaringClass ?: cls)
})
} }
if (rec.type != ObjRecord.Type.Property && !rec.isAbstract) { if (rec.type == ObjRecord.Type.Fun && !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})"
) )
) )
return rec.value.invoke( return rec.value.invoke(
@@ -217,6 +220,9 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
args, args,
decl decl
) )
} else if ((rec.type == ObjRecord.Type.Field || rec.type == ObjRecord.Type.Property) && !rec.isAbstract) {
val resolved = readField(scope, name)
return resolved.value.invoke(scope, this, args, resolved.declaringClass)
} }
} }
} }
@@ -257,7 +263,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)
} }
} }
@@ -115,9 +115,9 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
} }
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) { override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
if (isIntRange) { if (start is ObjInt && end is ObjInt) {
val s = (start as ObjInt).value val s = start.value
val e = (end as ObjInt).value val e = end.value
if (isEndInclusive) { if (isEndInclusive) {
for (i in s..e) { for (i in s..e) {
if (!callback(ObjInt.of(i))) break if (!callback(ObjInt.of(i))) break
@@ -127,9 +127,9 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
if (!callback(ObjInt.of(i))) break if (!callback(ObjInt.of(i))) break
} }
} }
} else if (isCharRange) { } else if (start is ObjChar && end is ObjChar) {
val s = (start as ObjChar).value val s = start.value
val e = (end as ObjChar).value val e = end.value
if (isEndInclusive) { if (isEndInclusive) {
for (c in s..e) { for (c in s..e) {
if (!callback(ObjChar(c))) break if (!callback(ObjChar(c))) break
@@ -29,14 +29,19 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
override val objClass: ObjClass = type override val objClass: ObjClass = type
fun Scope.init() { fun Scope.init() {
if (self.start == null || self.end == null) val s = self.start
raiseError("next is only available for finite ranges") val e = self.end
isCharRange = self.isCharRange if (s is ObjInt && e is ObjInt) {
lastIndex = if (self.isIntRange || self.isCharRange) { lastIndex = if (self.isEndInclusive)
if (self.isEndInclusive) (e.value - s.value + 1).toInt()
self.end.toInt() - self.start.toInt() + 1
else else
self.end.toInt() - self.start.toInt() (e.value - s.value).toInt()
} else if (s is ObjChar && e is ObjChar) {
isCharRange = true
lastIndex = if (self.isEndInclusive)
(e.value.code - s.value.code + 1)
else
(e.value.code - s.value.code)
} else { } else {
raiseError("not implemented iterator for range of $this") raiseError("not implemented iterator for range of $this")
} }
@@ -46,10 +51,13 @@ class ObjRangeIterator(val self: ObjRange) : Obj() {
fun next(scope: Scope): Obj = fun next(scope: Scope): Obj =
if (nextIndex < lastIndex) { if (nextIndex < lastIndex) {
val x = if (self.isEndInclusive) val start = self.start
self.start!!.toLong() + nextIndex++ val x = if (start is ObjInt)
start.value + nextIndex++
else if (start is ObjChar)
start.value.code.toLong() + nextIndex++
else else
self.start!!.toLong() + nextIndex++ scope.raiseError("iterator error: unsupported range start")
if( isCharRange ) ObjChar(x.toInt().toChar()) else ObjInt(x) if( isCharRange ) ObjChar(x.toInt().toChar()) else ObjInt(x)
} }
else { else {
@@ -243,8 +243,8 @@ class BinaryOpRef(private val op: BinOp, private val left: ObjRef, private val r
BinOp.OR -> a.logicalOr(scope, b) BinOp.OR -> a.logicalOr(scope, b)
BinOp.AND -> a.logicalAnd(scope, b) BinOp.AND -> a.logicalAnd(scope, b)
BinOp.EQARROW -> ObjMapEntry(a, b) BinOp.EQARROW -> ObjMapEntry(a, b)
BinOp.EQ -> ObjBool(a.compareTo(scope, b) == 0) BinOp.EQ -> ObjBool(a.equals(scope, b))
BinOp.NEQ -> ObjBool(a.compareTo(scope, b) != 0) BinOp.NEQ -> ObjBool(!a.equals(scope, b))
BinOp.REF_EQ -> ObjBool(a === b) BinOp.REF_EQ -> ObjBool(a === b)
BinOp.REF_NEQ -> ObjBool(a !== b) BinOp.REF_NEQ -> ObjBool(a !== b)
BinOp.MATCH -> a.operatorMatch(scope, b) BinOp.MATCH -> a.operatorMatch(scope, b)
@@ -321,16 +321,23 @@ class CastRef(
/** Qualified `this@Type`: resolves to a view of current `this` starting dispatch from the ancestor Type. */ /** Qualified `this@Type`: resolves to a view of current `this` starting dispatch from the ancestor Type. */
class QualifiedThisRef(private val typeName: String, private val atPos: Pos) : ObjRef { class QualifiedThisRef(private val typeName: String, private val atPos: Pos) : ObjRef {
override suspend fun get(scope: Scope): ObjRecord { override suspend fun get(scope: Scope): ObjRecord {
val thisObj = scope.thisObj
val t = scope[typeName]?.value as? ObjClass val t = scope[typeName]?.value as? ObjClass
?: scope.raiseError("unknown type $typeName") ?: scope.raiseError("unknown type $typeName")
val inst = (thisObj as? ObjInstance)
?: scope.raiseClassCastError("this is not an instance") var s: Scope? = scope
if (!inst.objClass.allParentsSet.contains(t) && inst.objClass !== t) while (s != null) {
scope.raiseClassCastError( val inst = s.thisObj as? ObjInstance
"Qualifier ${'$'}{t.className} is not an ancestor of ${'$'}{inst.objClass.className} (order: ${'$'}{inst.objClass.renderLinearization(true)})" if (inst != null) {
) if (inst.objClass === t || inst.objClass.allParentsSet.contains(t)) {
return ObjQualifiedView(inst, t).asReadonly return ObjQualifiedView(inst, t).asReadonly
}
}
s = s.parent
}
scope.raiseClassCastError(
"No instance of type ${t.className} found in the scope chain"
)
} }
} }
@@ -462,9 +469,11 @@ class FieldRef(
// Adaptive PIC (2→4) for reads/writes // Adaptive PIC (2→4) for reads/writes
private var rAccesses: Int = 0; private var rMisses: Int = 0; private var rPromotedTo4: Boolean = false private var rAccesses: Int = 0; private var rMisses: Int = 0; private var rPromotedTo4: Boolean = false
private var wAccesses: Int = 0; private var wMisses: Int = 0; private var wPromotedTo4: Boolean = false private var wAccesses: Int = 0; private var wMisses: Int = 0; private var wPromotedTo4: Boolean = false
@Suppress("NOTHING_TO_INLINE")
private inline fun size4ReadsEnabled(): Boolean = private inline fun size4ReadsEnabled(): Boolean =
PerfFlags.FIELD_PIC_SIZE_4 || PerfFlags.FIELD_PIC_SIZE_4 ||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && rPromotedTo4) (PerfFlags.PIC_ADAPTIVE_2_TO_4 && rPromotedTo4)
@Suppress("NOTHING_TO_INLINE")
private inline fun size4WritesEnabled(): Boolean = private inline fun size4WritesEnabled(): Boolean =
PerfFlags.FIELD_PIC_SIZE_4 || PerfFlags.FIELD_PIC_SIZE_4 ||
(PerfFlags.PIC_ADAPTIVE_2_TO_4 && wPromotedTo4) (PerfFlags.PIC_ADAPTIVE_2_TO_4 && wPromotedTo4)
@@ -611,7 +620,7 @@ class FieldRef(
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) { override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
val fieldPic = PerfFlags.FIELD_PIC val fieldPic = PerfFlags.FIELD_PIC
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
val base = target.get(scope).value val base = target.evalValue(scope)
if (base == ObjNull && isOptional) { if (base == ObjNull && isOptional) {
// no-op on null receiver for optional chaining assignment // no-op on null receiver for optional chaining assignment
return return
@@ -714,10 +723,9 @@ class FieldRef(
override suspend fun evalValue(scope: Scope): Obj { override suspend fun evalValue(scope: Scope): Obj {
// Mirror get(), but return raw Obj to avoid transient ObjRecord on R-value paths // Mirror get(), but return raw Obj to avoid transient ObjRecord on R-value paths
val fastRval = PerfFlags.RVAL_FASTPATH
val fieldPic = PerfFlags.FIELD_PIC val fieldPic = PerfFlags.FIELD_PIC
val picCounters = PerfFlags.PIC_DEBUG_COUNTERS val picCounters = PerfFlags.PIC_DEBUG_COUNTERS
val base = if (fastRval) target.evalValue(scope) else target.get(scope).value val base = target.evalValue(scope)
if (base == ObjNull && isOptional) return ObjNull if (base == ObjNull && isOptional) return ObjNull
if (fieldPic) { if (fieldPic) {
val (key, ver) = receiverKeyAndVersion(base) val (key, ver) = receiverKeyAndVersion(base)
@@ -1058,6 +1066,7 @@ class MethodCallRef(
private var mFreezeWindowsLeft: Int = 0 private var mFreezeWindowsLeft: Int = 0
private var mWindowAccesses: Int = 0 private var mWindowAccesses: Int = 0
private var mWindowMisses: Int = 0 private var mWindowMisses: Int = 0
@Suppress("NOTHING_TO_INLINE")
private inline fun size4MethodsEnabled(): Boolean = private inline fun size4MethodsEnabled(): Boolean =
PerfFlags.METHOD_PIC_SIZE_4 || PerfFlags.METHOD_PIC_SIZE_4 ||
((PerfFlags.PIC_ADAPTIVE_2_TO_4 || PerfFlags.PIC_ADAPTIVE_METHODS_ONLY) && mPromotedTo4 && mFreezeWindowsLeft == 0) ((PerfFlags.PIC_ADAPTIVE_2_TO_4 || PerfFlags.PIC_ADAPTIVE_METHODS_ONLY) && mPromotedTo4 && mFreezeWindowsLeft == 0)
@@ -1583,7 +1592,7 @@ class FastLocalVarRef(
if (slot >= 0 && actualOwner != null) { if (slot >= 0 && actualOwner != null) {
val rec = actualOwner.getSlotRecord(slot) val rec = actualOwner.getSlotRecord(slot)
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) { if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
return actualOwner.resolve(rec, name) return scope.resolve(rec, name)
} }
} }
// Try per-frame local binding maps in the ancestry first // Try per-frame local binding maps in the ancestry first
@@ -1796,6 +1805,21 @@ class RangeRef(
} }
} }
/** Assignment if null op: target ?= value */
class AssignIfNullRef(
private val target: ObjRef,
private val value: ObjRef,
private val atPos: Pos,
) : ObjRef {
override suspend fun get(scope: Scope): ObjRecord {
val current = target.evalValue(scope)
if (current != ObjNull) return current.asReadonly
val newValue = value.evalValue(scope)
target.setAt(atPos, scope, newValue)
return newValue.asReadonly
}
}
/** Simple assignment: target = value */ /** Simple assignment: target = value */
class AssignRef( class AssignRef(
private val target: ObjRef, private val target: ObjRef,
@@ -87,7 +87,7 @@ class ObjRegexMatch(val match: MatchResult) : Obj() {
// Use groupValues so that index 0 is the whole match and subsequent indices are capturing groups, // Use groupValues so that index 0 is the whole match and subsequent indices are capturing groups,
// which matches the language/tests expectation for `$~[i]`. // which matches the language/tests expectation for `$~[i]`.
ObjList( ObjList(
match.groupValues.map { ObjString(it) as Obj }.toMutableList() match.groupValues.map { ObjString(it) }.toMutableList()
) )
} }
@@ -101,6 +101,7 @@ open class LynonEncoder(val bout: BitOutput, val settings: LynonSettings = Lynon
* Caching is used automatically. * Caching is used automatically.
*/ */
suspend fun encodeAny(scope: Scope, obj: Obj) { suspend fun encodeAny(scope: Scope, obj: Obj) {
if (obj.objClass.isAnonymous) scope.raiseError("Cannot serialize anonymous object")
encodeCached(obj) { encodeCached(obj) {
val type = putTypeRecord(scope, obj, obj.lynonType()) val type = putTypeRecord(scope, obj, obj.lynonType())
obj.serialize(scope, this, type) obj.serialize(scope, this, type)
@@ -47,7 +47,7 @@ class BindingHighlightTest {
val mini = sink.build() val mini = sink.build()
assertNotNull(mini, "Mini-AST must be built") assertNotNull(mini, "Mini-AST must be built")
val binding = Binder.bind(text, mini!!) val binding = Binder.bind(text, mini)
// Find the top-level symbol for counter and ensure it is mutable (Variable) // Find the top-level symbol for counter and ensure it is mutable (Variable)
val sym = binding.symbols.firstOrNull { it.name == "counter" } val sym = binding.symbols.firstOrNull { it.name == "counter" }
@@ -78,7 +78,7 @@ class BindingHighlightTest {
val mini = sink.build() val mini = sink.build()
assertNotNull(mini, "Mini-AST must be built") assertNotNull(mini, "Mini-AST must be built")
val binding = Binder.bind(text, mini!!) val binding = Binder.bind(text, mini)
val sym = binding.symbols.firstOrNull { it.name == "answer" } val sym = binding.symbols.firstOrNull { it.name == "answer" }
assertNotNull(sym, "Top-level val 'answer' must be registered as a symbol") assertNotNull(sym, "Top-level val 'answer' must be registered as a symbol")
@@ -114,7 +114,7 @@ class BindingHighlightTest {
val mini = sink.build() val mini = sink.build()
assertNotNull(mini, "Mini-AST must be built") assertNotNull(mini, "Mini-AST must be built")
val binding = Binder.bind(text, mini!!) val binding = Binder.bind(text, mini)
// Ensure we registered the local var/val symbol for `name` // Ensure we registered the local var/val symbol for `name`
val nameSym = binding.symbols.firstOrNull { it.name == "name" } val nameSym = binding.symbols.firstOrNull { it.name == "name" }
@@ -163,7 +163,7 @@ class BindingHighlightTest {
val mini = sink.build() val mini = sink.build()
assertNotNull(mini, "Mini-AST must be built") assertNotNull(mini, "Mini-AST must be built")
val binding = Binder.bind(text, mini!!) val binding = Binder.bind(text, mini)
val nameSym = binding.symbols.firstOrNull { it.name == "name" && (it.kind == SymbolKind.Variable || it.kind == SymbolKind.Value) } val nameSym = binding.symbols.firstOrNull { it.name == "name" && (it.kind == SymbolKind.Variable || it.kind == SymbolKind.Value) }
assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol") assertNotNull(nameSym, "Local variable 'name' should be registered as a symbol")
+4 -4
View File
@@ -36,7 +36,7 @@ class BindingTest {
Compiler.compileWithMini(src, sink) Compiler.compileWithMini(src, sink)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini, "MiniScript should be built") assertNotNull(mini, "MiniScript should be built")
return Binder.bind(src, mini!!) return Binder.bind(src, mini)
} }
@Test @Test
@@ -72,7 +72,7 @@ class BindingTest {
val xSym = snap.symbols.firstOrNull { it.name == "x" } val xSym = snap.symbols.firstOrNull { it.name == "x" }
assertNotNull(xSym) assertNotNull(xSym)
// One reference usage to top-level x // One reference usage to top-level x
val refs = snap.references.filter { it.symbolId == xSym!!.id } val refs = snap.references.filter { it.symbolId == xSym.id }
assertEquals(1, refs.size) assertEquals(1, refs.size)
} }
@@ -111,7 +111,7 @@ class BindingTest {
val fooField = snap.symbols.firstOrNull { it.name == "foo" } val fooField = snap.symbols.firstOrNull { it.name == "foo" }
assertNotNull(fooField) assertNotNull(fooField)
// Should have at least one reference (usage in bar) // Should have at least one reference (usage in bar)
val refs = snap.references.count { it.symbolId == fooField!!.id } val refs = snap.references.count { it.symbolId == fooField.id }
assertEquals(1, refs) assertEquals(1, refs)
} }
@@ -126,7 +126,7 @@ class BindingTest {
) )
val xField = snap.symbols.firstOrNull { it.name == "x" } val xField = snap.symbols.firstOrNull { it.name == "x" }
assertNotNull(xField) assertNotNull(xField)
val refs = snap.references.count { it.symbolId == xField!!.id } val refs = snap.references.count { it.symbolId == xField.id }
assertEquals(1, refs) assertEquals(1, refs)
} }
} }
@@ -0,0 +1,101 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
class IfNullAssignTest {
@Test
fun testBasicAssignment() = runTest {
eval("""
var x = null
x ?= 42
assertEquals(42, x)
x ?= 100
assertEquals(42, x)
""".trimIndent())
}
@Test
fun testPropertyAssignment() = runTest {
eval("""
class Box(var value)
val b = Box(null)
b.value ?= 10
assertEquals(10, b.value)
b.value ?= 20
assertEquals(10, b.value)
""".trimIndent())
}
@Test
fun testIndexAssignment() = runTest {
eval("""
val a = [null, 1]
a[0] ?= 10
assertEquals(10, a[0])
a[1] ?= 20
assertEquals(1, a[1])
""".trimIndent())
}
@Test
fun testOptionalChaining() = runTest {
eval("""
class Inner(var value)
class Outer(var inner)
var o = null
o?.inner?.value ?= 10 // should do nothing
assertEquals(null, o)
o = Outer(null)
o?.inner?.value ?= 10 // should do nothing because inner is null
assertEquals(null, o.inner)
o.inner = Inner(null)
o?.inner?.value ?= 42
assertEquals(42, o.inner.value)
o?.inner?.value ?= 100
assertEquals(42, o.inner.value)
""".trimIndent())
}
@Test
fun testDoubleEvaluation() = runTest {
eval("""
var count = 0
fun getIdx() {
count = count + 1
return 0
}
val a = [null]
a[getIdx()] ?= 10
// Current behavior: double evaluation happens in ObjRef for compound ops
// getIdx() is called once for checking if null, and once for setting if it was null.
assertEquals(10, a[0])
assertEquals(2, count)
a[getIdx()] ?= 20
// If it's NOT null, it only evaluates once to check the value.
assertEquals(10, a[0])
assertEquals(3, count)
""".trimIndent())
}
@Test
fun testReturnValue() = runTest {
eval("""
var x = null
val r1 = (x ?= 42)
assertEquals(42, r1)
assertEquals(42, x)
val r2 = (x ?= 100)
assertEquals(42, r2)
assertEquals(42, x)
""".trimIndent())
}
}
+12 -12
View File
@@ -44,7 +44,7 @@ class MiniAstTest {
val (_, sink) = compileWithMini(code) val (_, sink) = compileWithMini(code)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini) assertNotNull(mini)
val imps = mini!!.imports val imps = mini.imports
assertTrue(imps.isNotEmpty(), "imports should be captured") assertTrue(imps.isNotEmpty(), "imports should be captured")
val first = imps.first() val first = imps.first()
val segNames = first.segments.map { it.name } val segNames = first.segments.map { it.name }
@@ -68,12 +68,12 @@ class MiniAstTest {
val (_, sink) = compileWithMini(code) val (_, sink) = compileWithMini(code)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini) assertNotNull(mini)
val fn = mini!!.declarations.filterIsInstance<MiniFunDecl>().firstOrNull { it.name == "foo" } val fn = mini.declarations.filterIsInstance<MiniFunDecl>().firstOrNull { it.name == "foo" }
assertNotNull(fn, "function decl should be captured") assertNotNull(fn, "function decl should be captured")
// Doc // Doc
assertNotNull(fn.doc) assertNotNull(fn.doc)
assertEquals("Summary: does foo", fn.doc!!.summary) assertEquals("Summary: does foo", fn.doc.summary)
assertTrue(fn.doc!!.raw.contains("details")) assertTrue(fn.doc.raw.contains("details"))
// Params // Params
assertEquals(2, fn.params.size) assertEquals(2, fn.params.size)
val p1 = fn.params[0] val p1 = fn.params[0]
@@ -99,10 +99,10 @@ class MiniAstTest {
val (_, sink) = compileWithMini(code) val (_, sink) = compileWithMini(code)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini) assertNotNull(mini)
val vd = mini!!.declarations.filterIsInstance<net.sergeych.lyng.miniast.MiniValDecl>().firstOrNull { it.name == "x" } val vd = mini.declarations.filterIsInstance<net.sergeych.lyng.miniast.MiniValDecl>().firstOrNull { it.name == "x" }
assertNotNull(vd) assertNotNull(vd)
assertNotNull(vd.doc) assertNotNull(vd.doc)
assertEquals("docs for x", vd.doc!!.summary) assertEquals("docs for x", vd.doc.summary)
val ty = vd.type val ty = vd.type
assertNotNull(ty) assertNotNull(ty)
val gen = ty as MiniGenericType val gen = ty as MiniGenericType
@@ -126,10 +126,10 @@ class MiniAstTest {
val (_, sink) = compileWithMini(code) val (_, sink) = compileWithMini(code)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini) assertNotNull(mini)
val cd = mini!!.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" } val cd = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" }
assertNotNull(cd) assertNotNull(cd)
assertNotNull(cd.doc, "Class doc should be preserved even with members") assertNotNull(cd.doc, "Class doc should be preserved even with members")
assertTrue(cd.doc!!.raw.contains("Class C docs")) assertTrue(cd.doc.raw.contains("Class C docs"))
} }
@Test @Test
@@ -141,10 +141,10 @@ class MiniAstTest {
val (_, sink) = compileWithMini(code) val (_, sink) = compileWithMini(code)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini) assertNotNull(mini)
val cd = mini!!.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" } val cd = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == "C" }
assertNotNull(cd) assertNotNull(cd)
assertNotNull(cd.doc) assertNotNull(cd.doc)
assertTrue(cd.doc!!.raw.contains("Class C docs")) assertTrue(cd.doc.raw.contains("Class C docs"))
// Bases captured as plain names for now // Bases captured as plain names for now
assertEquals(listOf("Base1", "Base2"), cd.bases) assertEquals(listOf("Base1", "Base2"), cd.bases)
} }
@@ -162,10 +162,10 @@ class MiniAstTest {
val (_, sink) = compileWithMini(code) val (_, sink) = compileWithMini(code)
val mini = sink.build() val mini = sink.build()
assertNotNull(mini) assertNotNull(mini)
val ed = mini!!.declarations.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == "E" } val ed = mini.declarations.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == "E" }
assertNotNull(ed) assertNotNull(ed)
assertNotNull(ed.doc) assertNotNull(ed.doc)
assertTrue(ed.doc!!.raw.contains("Enum E docs")) assertTrue(ed.doc.raw.contains("Enum E docs"))
assertEquals(listOf("A", "B", "C"), ed.entries) assertEquals(listOf("A", "B", "C"), ed.entries)
assertEquals("E", ed.name) assertEquals("E", ed.name)
} }
+20
View File
@@ -711,4 +711,24 @@ class OOTest {
""".trimIndent() """.trimIndent()
) )
} }
@Test
fun testBasicObjectExpression() = runTest {
eval("""
val x = object { val y = 1 }
assertEquals(1, x.y)
class Base(v) {
val value = v
fun squares() = value * value
}
val y = object : Base(2) {
override val value = 5
}
assertEquals(25, y.squares())
""".trimIndent())
}
} }
@@ -0,0 +1,120 @@
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import net.sergeych.lynon.lynonEncodeAny
import kotlin.test.Test
import kotlin.test.assertFailsWith
class ObjectExpressionTest {
@Test
fun testBasicObjectExpression() = runTest {
eval("""
val x = object { val y = 1 }
assertEquals(1, x.y)
""".trimIndent())
}
@Test
fun testInheritanceWithArgs() = runTest {
eval("""
class Base(x) {
val value = x
val squares = x * x
}
val y = object : Base(5) {
val z = value + 1
}
assertEquals(5, y.value)
assertEquals(25, y.squares)
assertEquals(6, y.z)
""".trimIndent())
}
@Test
fun testMultipleInheritance() = runTest {
eval("""
interface A { fun a() = "A" }
interface B { fun b() = "B" }
val x = object : A, B {
fun c() = a() + b()
}
assertEquals("AB", x.c())
""".trimIndent())
}
@Test
fun testScopeCapture() = runTest {
eval("""
fun createCounter(start) {
var count = start
object {
fun next() {
val res = count
count = count + 1
res
}
}
}
val c = createCounter(10)
assertEquals(10, c.next())
assertEquals(11, c.next())
""".trimIndent())
}
@Test
fun testThisObjectAlias() = runTest {
eval("""
val x = object {
val value = 42
fun self() = this@object
fun getValue() = this@object.value
}
assertEquals(42, x.getValue())
// assert(x === x.self()) // Lyng might not have === for identity yet, checking if it compiles and runs
""".trimIndent())
}
@Test
fun testSerializationRejection() = runTest {
val scope = Script.newScope()
val obj = scope.eval("object { val x = 1 }")
assertFailsWith<Exception> {
lynonEncodeAny(scope, obj)
}
}
@Test
fun testQualifiedThis() = runTest {
eval("""
class Outer {
val value = 1
fun getObj() {
object {
fun getOuterValue() = this@Outer.value
}
}
}
val o = Outer()
val x = o.getObj()
assertEquals(1, x.getOuterValue())
""".trimIndent())
}
@Test
fun testDiagnosticName() = runTest {
// This is harder to test directly, but we can check if it has a class and if that class name looks "anonymous"
eval("""
val x = object { }
val name = x::class.className
assert(name.startsWith("${'$'}Anon_"))
""".trimIndent())
}
}
@@ -0,0 +1,128 @@
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.ScriptError
import net.sergeych.lyng.eval
import net.sergeych.lyng.obj.toInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class ReturnStatementTest {
@Test
fun testBasicReturn() = runTest {
assertEquals(10, eval("""
fun foo() {
return 10
20
}
foo()
""").toInt())
}
@Test
fun testReturnFromIf() = runTest {
assertEquals(5, eval("""
fun foo(x) {
if (x > 0) return 5
10
}
foo(1)
""").toInt())
assertEquals(10, eval("""
fun foo(x) {
if (x > 0) return 5
10
}
foo(-1)
""").toInt())
}
@Test
fun testReturnFromLambda() = runTest {
assertEquals(2, eval("""
val f = { x ->
if (x < 0) return 0
x * 2
}
f(1)
""").toInt())
assertEquals(0, eval("""
val f = { x ->
if (x < 0) return 0
x * 2
}
f(-1)
""").toInt())
}
@Test
fun testNonLocalReturn() = runTest {
assertEquals(100, eval("""
fun outer() {
[1, 2, 3].forEach {
if (it == 2) return@outer 100
}
0
}
outer()
""").toInt())
}
@Test
fun testLabeledLambdaReturn() = runTest {
assertEquals(42, eval("""
val f = @inner { x ->
if (x == 0) return@inner 42
x
}
f(0)
""").toInt())
assertEquals(5, eval("""
val f = @inner { x ->
if (x == 0) return@inner 42
x
}
f(5)
""").toInt())
}
@Test
fun testForbidEqualReturn() = runTest {
assertFailsWith<ScriptError> {
eval("fun foo(x) = return x")
}
}
@Test
fun testDeepNestedReturn() = runTest {
assertEquals(42, eval("""
fun find() {
val data = [[1, 2], [3, 42], [5, 6]]
data.forEach { row ->
row.forEach { item ->
if (item == 42) return@find item
}
}
0
}
find()
""").toInt())
}
@Test
fun testReturnFromOuterLambda() = runTest {
assertEquals("found", eval("""
val f_outer = @outer {
val f_inner = {
return@outer "found"
}
f_inner()
"not found"
}
f_outer()
""").toString())
}
}
@@ -4560,4 +4560,16 @@ class ScriptTest {
""".trimIndent()) """.trimIndent())
} }
@Test
fun testOptOnNullAssignment() = runTest {
eval("""
var x = null
assertEquals(null, x)
x ?= 1
assertEquals(1, x)
x ?= 2
assertEquals(1, x)
""".trimIndent())
}
} }
+19 -1
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.
@@ -113,4 +113,22 @@ class StdlibTest {
assertEquals(5, (1..10).toList().count { it % 2 == 1 } ) assertEquals(5, (1..10).toList().count { it % 2 == 1 } )
""") """)
} }
@Test
fun testWith() = runTest {
eval("""
class Person(val name, var age)
val p = Person("Alice", 30)
val result = with(p) {
assertEquals("Alice", name)
assertEquals(30, age)
age = 31
"done"
}
assertEquals("done", result)
assertEquals(31, p.age)
""".trimIndent())
}
} }
@@ -210,4 +210,117 @@ class DelegationTest {
assert(l is Delegate) assert(l is Delegate)
""") """)
} }
@Test
fun testRealLifeBug1() = runTest {
eval("""
class Cell {
val tags = [1,2,3]
}
class T {
val cell by lazy { Cell() }
val tags get() = cell.tags
}
assertEquals([1,2,3], T().tags)
""".trimIndent())
}
@Test
fun testInstanceIsolation() = runTest {
eval("""
class CounterDelegate() {
private var count = 0
fun getValue(thisRef, name) = ++count
}
class Foo {
val x by CounterDelegate()
}
val f1 = Foo()
val f2 = Foo()
assertEquals(1, f1.x)
assertEquals(1, f2.x)
assertEquals(2, f1.x)
assertEquals(2, f2.x)
""")
}
@Test
fun testLazyRegexBug() = runTest {
eval("""
class T {
val re by lazy { Regex(".*") }
}
val t = T()
t.re
// Second access triggered the bug before fix (value == Unset failed)
t.re
""")
}
@Test
fun testEqualityRobustness() = runTest {
eval("""
val re1 = Regex("a")
val re2 = Regex("a")
// Equality should not throw even if types don't implement compareTo
assertEquals(true, re1 == re1)
assertEquals(false, re1 == re2)
assertEquals(false, re1 == Unset)
assertEquals(false, re1 == null)
""")
}
@Test
fun testLazy2() = runTest {
eval("""
class A {
val tags = [1,2,3]
}
class B {
val tags by lazy { myA.tags }
val myA by lazy { A() }
}
assert( B().tags == [1,2,3])
""".trimIndent())
}
@Test
fun testThisInLazy() = runTest {
eval("""
class A {
val numbers = [1,2,3]
val tags by lazy { this.numbers }
}
class B {
val a by lazy { A() }
val test by lazy { a.tags + [4] }
}
assertEquals( [1,2,3], A().tags)
assertEquals( [1,2,3,4], B().test)
""")
}
@Test
fun testScopeInLazy() = runTest {
val s1 = Script.newScope()
s1.eval("""
class A {
val tags by lazy { GLOBAL_NUMBERS }
}
""".trimIndent())
// on the same scope, it will see my GLOBAL_NUMBERS:
s1.eval("""
val GLOBAL_NUMBERS = [1,2,3]
class B {
val a by lazy { A() }
val test by lazy { a.tags + [4] }
}
assertEquals( [1,2,3], A().tags)
assertEquals( [1,2,3,4], B().test)
""")
}
} }
@@ -39,7 +39,7 @@ class BlockReindentTest {
val open = BraceUtils.findMatchingOpenBrace(text, close) val open = BraceUtils.findMatchingOpenBrace(text, close)
assertNotNull(open) assertNotNull(open)
// The char at open must be '{' // The char at open must be '{'
assertEquals('{', text[open!!]) assertEquals('{', text[open])
} }
@Test @Test
@@ -55,7 +55,7 @@ class BlockReindentTest {
val range = BraceUtils.findEnclosingBlockRange(text, close, includeTrailingNewline = true) val range = BraceUtils.findEnclosingBlockRange(text, close, includeTrailingNewline = true)
assertNotNull(range) assertNotNull(range)
// The range must start at the line start of the matching '{' and end at or after the newline after '}' // The range must start at the line start of the matching '{' and end at or after the newline after '}'
val start = range!!.first val start = range.first
val end = range.last + 1 val end = range.last + 1
val startLinePrefix = text.substring(BraceUtils.lineStart(text, start), start) val startLinePrefix = text.substring(BraceUtils.lineStart(text, start), start)
// start at column 0 of the line // start at column 0 of the line
@@ -21,6 +21,15 @@ import kotlin.test.assertEquals
class LyngFormatterTest { class LyngFormatterTest {
@Test
fun labelFormatting() {
val src = "return @label; break @outer; continue @inner"
val expected = "return@label; break@outer; continue@inner"
val cfg = LyngFormatConfig(applySpacing = true)
val out = LyngFormatter.format(src, cfg)
assertEquals(expected, out)
}
@Test @Test
fun reindent_simpleFunction() { fun reindent_simpleFunction() {
val src = """ val src = """
@@ -850,4 +859,31 @@ class LyngFormatterTest {
""".trimIndent() """.trimIndent()
assertEquals(expected2, LyngFormatter.reindent(src2, LyngFormatConfig(indentSize = 4))) assertEquals(expected2, LyngFormatter.reindent(src2, LyngFormatConfig(indentSize = 4)))
} }
@Test
fun mixedPropertyAccessors() {
val src = """
class X {
var x get() {
11
}
set(value) {
}
}
""".trimIndent()
val expected = """
class X {
var x get() {
11
}
set(value) {
}
}
""".trimIndent()
val cfg = LyngFormatConfig(indentSize = 4, continuationIndentSize = 4)
val out = LyngFormatter.reindent(src, cfg)
assertEquals(expected, out)
}
} }
@@ -34,7 +34,7 @@ class CommentEolTest {
assertTrue(cmt != null, "Expected a comment span") assertTrue(cmt != null, "Expected a comment span")
// It should start at 0 and extend exactly to the end of the line (before \n) // It should start at 0 and extend exactly to the end of the line (before \n)
val eol = text.indexOf('\n') val eol = text.indexOf('\n')
assertEquals(0, cmt!!.range.start, "Comment should start at column 0") assertEquals(0, cmt.range.start, "Comment should start at column 0")
assertEquals(eol, cmt.range.endExclusive, "Comment should extend to EOL") assertEquals(eol, cmt.range.endExclusive, "Comment should extend to EOL")
// Ensure there is no other span overlapping within the same line // Ensure there is no other span overlapping within the same line
spans.filter { it !== cmt }.forEach { spans.filter { it !== cmt }.forEach {
@@ -50,7 +50,7 @@ class CommentEolTest {
assertTrue(cmt != null, "Expected a block comment span") assertTrue(cmt != null, "Expected a block comment span")
// The comment should end right after "/* block */" // The comment should end right after "/* block */"
val expectedEnd = "/* block */".length val expectedEnd = "/* block */".length
assertEquals(expectedEnd, cmt!!.range.endExclusive, "Block comment should not be extended to EOL") assertEquals(expectedEnd, cmt.range.endExclusive, "Block comment should not be extended to EOL")
} }
@Test @Test
@@ -25,6 +25,16 @@ class HighlightMappingTest {
private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> = private fun spansToLabeled(text: String, spans: List<HighlightSpan>): List<Pair<String, HighlightKind>> =
spans.map { text.substring(it.range.start, it.range.endExclusive) to it.kind } spans.map { text.substring(it.range.start, it.range.endExclusive) to it.kind }
@Test
fun returnAndExits() {
val text = "return 42; break@outer null"
val spans = SimpleLyngHighlighter().highlight(text)
val labeled = spansToLabeled(text, spans)
assertTrue(labeled.any { it.first == "return" && it.second == HighlightKind.Keyword })
assertTrue(labeled.any { it.first == "break" && it.second == HighlightKind.Keyword })
assertTrue(labeled.any { it.first == "@outer" && it.second == HighlightKind.Label })
}
@Test @Test
fun keywordsAndIdentifiers() { fun keywordsAndIdentifiers() {
val text = "a and b or not c" val text = "a and b or not c"
@@ -3,6 +3,7 @@ package net.sergeych.lyng.miniast
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertFalse import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue import kotlin.test.assertTrue
class CompletionEngineLightTest { class CompletionEngineLightTest {
@@ -89,9 +90,9 @@ class CompletionEngineLightTest {
""".trimIndent() """.trimIndent()
val items = CompletionEngineLight.completeAtMarkerSuspend(code) val items = CompletionEngineLight.completeAtMarkerSuspend(code)
val reItem = items.firstOrNull { it.name == "re" } val reItem = items.firstOrNull { it.name == "re" }
assertTrue(reItem != null, "Expected to find 're' in String members, got: ${items.map { it.name }}") assertNotNull(reItem, "Expected to find 're' in String members, got: ${items.map { it.name }}")
// Type text should contain ": Regex" // Type text should contain ": Regex"
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected type text to contain 'Regex', was: ${reItem.typeText}") assertTrue(reItem.typeText?.contains("Regex") == true, "Expected type text to contain 'Regex', was: ${reItem.typeText}")
} }
@Test @Test
@@ -106,8 +107,8 @@ class CompletionEngineLightTest {
val names = items.map { it.name } val names = items.map { it.name }
assertTrue(names.isNotEmpty(), "Expected String members for parenthesized literal, got empty list") assertTrue(names.isNotEmpty(), "Expected String members for parenthesized literal, got empty list")
val reItem = items.firstOrNull { it.name == "re" } val reItem = items.firstOrNull { it.name == "re" }
assertTrue(reItem != null, "Expected to find 're' for parenthesized String literal, got: $names") assertNotNull(reItem, "Expected to find 're' for parenthesized String literal, got: $names")
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected ': Regex' for re(), was: ${reItem.typeText}") assertTrue(reItem.typeText?.contains("Regex") == true, "Expected ': Regex' for re(), was: ${reItem.typeText}")
} }
@Test @Test
@@ -120,8 +121,8 @@ class CompletionEngineLightTest {
val names = items.map { it.name } val names = items.map { it.name }
assertTrue(names.isNotEmpty(), "Expected String members without explicit imports, got empty list") assertTrue(names.isNotEmpty(), "Expected String members without explicit imports, got empty list")
val reItem = items.firstOrNull { it.name == "re" } val reItem = items.firstOrNull { it.name == "re" }
assertTrue(reItem != null, "Expected to find 're' without explicit imports, got: $names") assertNotNull(reItem, "Expected to find 're' without explicit imports, got: $names")
assertTrue(reItem!!.typeText?.contains("Regex") == true, "Expected ': Regex' for re() without imports, was: ${reItem.typeText}") assertTrue(reItem.typeText?.contains("Regex") == true, "Expected ': Regex' for re() without imports, was: ${reItem.typeText}")
} }
@Test @Test
@@ -144,4 +145,33 @@ class CompletionEngineLightTest {
// Should contain some iterator members // Should contain some iterator members
assertTrue(ns.isNotEmpty(), "Iterator members should be suggested after lines() with shebang present") assertTrue(ns.isNotEmpty(), "Iterator members should be suggested after lines() with shebang present")
} }
@Test
fun constructorParametersInMethod() = runBlocking {
val code = """
class MyClass(myParam) {
fun myMethod() {
myp<caret>
}
}
""".trimIndent()
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
val ns = names(items)
assertTrue(ns.contains("myParam"), "Constructor parameter 'myParam' should be proposed, but got: $ns")
}
@Test
fun classFieldsInMethod() = runBlocking {
val code = """
class MyClass {
val myField = 1
fun myMethod() {
myf<caret>
}
}
""".trimIndent()
val items = CompletionEngineLight.completeAtMarkerSuspend(code)
val ns = names(items)
assertTrue(ns.contains("myField"), "Class field 'myField' should be proposed, but got: $ns")
}
} }
+38 -27
View File
@@ -1,8 +1,8 @@
package lyng.stdlib package lyng.stdlib
/* /*
Wrap a builder into a zero-argument thunk that computes once and caches the result. Wrap a builder into a zero-argument thunk that computes once and caches the result.
The first call invokes builder() and stores the value; subsequent calls return the cached value. The first call invokes builder() and stores the value; subsequent calls return the cached value.
*/ */
fun cached(builder) { fun cached(builder) {
var calculated = false var calculated = false
@@ -16,7 +16,7 @@ fun cached(builder) {
} }
} }
/* Filter elements of this iterable using the provided predicate and provide a flow /* Filter elements of this iterable using the provided predicate and provide a flow
of results. Coudl be used to map infinte flows, etc. of results. Coudl be used to map infinte flows, etc.
*/ */
fun Iterable.filterFlow(predicate): Flow { fun Iterable.filterFlow(predicate): Flow {
val list = this val list = this
@@ -30,7 +30,7 @@ fun Iterable.filterFlow(predicate): Flow {
} }
/* /*
Filter this iterable and return List of elements Filter this iterable and return List of elements
*/ */
fun Iterable.filter(predicate) { fun Iterable.filter(predicate) {
val result = [] val result = []
@@ -39,7 +39,7 @@ fun Iterable.filter(predicate) {
} }
/* /*
Count all items in this iterable for which predicate return true Count all items in this iterable for which predicate return true
*/ */
fun Iterable.count(predicate): Int { fun Iterable.count(predicate): Int {
var hits = 0 var hits = 0
@@ -49,8 +49,8 @@ fun Iterable.count(predicate): Int {
hits hits
} }
/* /*
filter out all null elements from this collection (Iterable); flow of filter out all null elements from this collection (Iterable); flow of
non-null elements is returned non-null elements is returned
*/ */
fun Iterable.filterFlowNotNull(): Flow { fun Iterable.filterFlowNotNull(): Flow {
filterFlow { it != null } filterFlow { it != null }
@@ -72,12 +72,12 @@ fun Iterable.drop(n) {
val Iterable.first get() { val Iterable.first get() {
val i = iterator() val i = iterator()
if( !i.hasNext() ) throw NoSuchElementException() if( !i.hasNext() ) throw NoSuchElementException()
i.next().also { i.cancelIteration() } i.next().also { i.cancelIteration() }
} }
/* /*
Return the first element that matches the predicate or throws Return the first element that matches the predicate or throws
NuSuchElementException NuSuchElementException
*/ */
fun Iterable.findFirst(predicate) { fun Iterable.findFirst(predicate) {
for( x in this ) { for( x in this ) {
@@ -88,7 +88,7 @@ fun Iterable.findFirst(predicate) {
} }
/* /*
return the first element matching the predicate or null return the first element matching the predicate or null
*/ */
fun Iterable.findFirstOrNull(predicate) { fun Iterable.findFirstOrNull(predicate) {
for( x in this ) { for( x in this ) {
@@ -108,7 +108,7 @@ val Iterable.last get() {
found = true found = true
} }
if( !found ) throw NoSuchElementException() if( !found ) throw NoSuchElementException()
element element
} }
/* Emit all but the last N elements of this iterable. */ /* Emit all but the last N elements of this iterable. */
@@ -215,8 +215,8 @@ fun Iterable.shuffled() {
} }
/* /*
Returns a single list of all elements from all collections in the given collection. Returns a single list of all elements from all collections in the given collection.
@return List @return List
*/ */
fun Iterable.flatten() { fun Iterable.flatten() {
val result = [] val result = []
@@ -227,8 +227,8 @@ fun Iterable.flatten() {
} }
/* /*
Returns a single list of all elements yielded from results of transform function being Returns a single list of all elements yielded from results of transform function being
invoked on each element of original collection. invoked on each element of original collection.
*/ */
fun Iterable.flatMap(transform): List { fun Iterable.flatMap(transform): List {
map(transform).flatten() map(transform).flatten()
@@ -279,8 +279,8 @@ val String.re get() = Regex(this)
fun TODO(message=null) = throw NotImplementedException(message ?: "not implemented") fun TODO(message=null) = throw NotImplementedException(message ?: "not implemented")
/* /*
Provides different access types for delegates. Provides different access types for delegates.
Used in the 'bind' hook to validate delegate usage. Used in the 'bind' hook to validate delegate usage.
*/ */
enum DelegateAccess { enum DelegateAccess {
Val, Val,
@@ -289,9 +289,9 @@ enum DelegateAccess {
} }
/* /*
Base interface for all delegates. Base interface for all delegates.
Implementing this interface is optional as Lyng uses dynamic dispatch, Implementing this interface is optional as Lyng uses dynamic dispatch,
but it is recommended for documentation and clarity. but it is recommended for documentation and clarity.
*/ */
interface Delegate { interface Delegate {
/* Called when a delegated 'val' or 'var' is read. */ /* Called when a delegated 'val' or 'var' is read. */
@@ -304,16 +304,26 @@ interface Delegate {
fun invoke(thisRef, name, args...) = TODO("delegate invoke is not implemented") fun invoke(thisRef, name, args...) = TODO("delegate invoke is not implemented")
/* /*
Called once during initialization to configure or validate the delegate. Called once during initialization to configure or validate the delegate.
Should return the delegate object to be used (usually 'this'). Should return the delegate object to be used (usually 'this').
*/ */
fun bind(name, access, thisRef) = this fun bind(name, access, thisRef) = this
} }
/* /*
Standard implementation of a lazy-initialized property delegate. Executes the block with `this` set to self and
The provided creator lambda is called once on the first access to compute the value. returns what the block returns.
Can only be used with 'val' properties. */
fun with(self, block) {
var result = Unset
self.apply { result = block() }
result
}
/*
Standard implementation of a lazy-initialized property delegate.
The provided creator lambda is called once on the first access to compute the value.
Can only be used with 'val' properties.
*/ */
class lazy(creatorParam) : Delegate { class lazy(creatorParam) : Delegate {
private val creator = creatorParam private val creator = creatorParam
@@ -325,7 +335,8 @@ class lazy(creatorParam) : Delegate {
} }
override fun getValue(thisRef, name) { override fun getValue(thisRef, name) {
if (value == Unset) value = creator() if (value == Unset)
value = with(thisRef,creator)
value value
} }
} }
@@ -530,7 +530,7 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
fun isIdentPart(ch: Char) = ch == '_' || ch == '$' || ch == '~' || ch.isLetterOrDigit() fun isIdentPart(ch: Char) = ch == '_' || ch == '$' || ch == '~' || ch.isLetterOrDigit()
// A conservative list of language keywords to avoid misclassifying as function calls // A conservative list of language keywords to avoid misclassifying as function calls
val kw = setOf( val kw = setOf(
"package", "import", "fun", "fn", "class", "interface", "enum", "val", "var", "package", "import", "fun", "fn", "class", "interface", "enum", "object", "val", "var",
"if", "else", "while", "do", "for", "when", "try", "catch", "finally", "if", "else", "while", "do", "for", "when", "try", "catch", "finally",
"throw", "return", "break", "continue", "in", "is", "as", "as?", "not", "throw", "return", "break", "continue", "in", "is", "as", "as?", "not",
"true", "false", "null", "private", "protected", "abstract", "closed", "override", "open", "extern", "static", "true", "false", "null", "private", "protected", "abstract", "closed", "override", "open", "extern", "static",
@@ -640,8 +640,12 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
i = p i = p
continue continue
} }
if (text.startsWith("class", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+5 >= n || !isIdentPart(text.getOrNull(i+5) ?: '\u0000'))) { if ((text.startsWith("class", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+5 >= n || !isIdentPart(text.getOrNull(i+5) ?: '\u0000'))) ||
var p = skipWs(i + 5) (text.startsWith("object", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+6 >= n || !isIdentPart(text.getOrNull(i+6) ?: '\u0000'))) ||
(text.startsWith("enum", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+4 >= n || !isIdentPart(text.getOrNull(i+4) ?: '\u0000'))) ||
(text.startsWith("interface", i) && (i == 0 || !isIdentPart(text[i-1])) && (i+9 >= n || !isIdentPart(text.getOrNull(i+9) ?: '\u0000')))) {
val kwLen = if (text.startsWith("class", i)) 5 else if (text.startsWith("object", i)) 6 else if (text.startsWith("enum", i)) 4 else 9
var p = skipWs(i + kwLen)
val name = readIdent(p) val name = readIdent(p)
if (name != null) result[p to name.second] = "hl-class" if (name != null) result[p to name.second] = "hl-class"
i = p i = p
+14 -2
View File
@@ -77,6 +77,14 @@ fun HomePage() {
// Create, transform, and verify — the Lyng way // Create, transform, and verify — the Lyng way
import lyng.stdlib import lyng.stdlib
fun findFirstPositive(list) {
list.forEach {
if (it > 0) return@findFirstPositive it
}
null
}
assertEquals(42, findFirstPositive([-1, 42, -5]))
val data = 1..5 // or [1,2,3,4,5] val data = 1..5 // or [1,2,3,4,5]
val evens2 = data.filter { it % 2 == 0 }.map { it * it } val evens2 = data.filter { it % 2 == 0 }.map { it * it }
assertEquals([4, 16], evens2) assertEquals([4, 16], evens2)
@@ -86,8 +94,12 @@ val base = { a: 1, b: 2 }
val patch = { b: 3, c: } val patch = { b: 3, c: }
val m = { "a": 0, ...base, ...patch, d: 4 } val m = { "a": 0, ...base, ...patch, d: 4 }
assertEquals(1, m["a"]) // base overwrites 0 assertEquals(1, m["a"]) // base overwrites 0
assertEquals(3, m["b"]) // patch overwrites base
assertEquals(4, m["d"]) // literal key // Object expressions: anonymous classes on the fly
val worker = object : Runnable {
override fun run() = println("Working...")
}
worker.run()
>>> void >>> void
""".trimIndent() """.trimIndent()
val mapHtml = "<pre><code>" + htmlEscape(code) + "</code></pre>" val mapHtml = "<pre><code>" + htmlEscape(code) + "</code></pre>"
+1 -1
View File
@@ -329,7 +329,7 @@
<!-- Top-left version ribbon --> <!-- Top-left version ribbon -->
<div class="corner-ribbon bg-danger text-white"> <div class="corner-ribbon bg-danger text-white">
<span style="margin-left: -5em"> <span style="margin-left: -5em">
v1.1.0-rc v1.1.1-SNAPSHOT
</span> </span>
</div> </div>
<!-- Fixed top navbar for the whole site --> <!-- Fixed top navbar for the whole site -->
@@ -20,6 +20,22 @@ import kotlin.test.assertFalse
import kotlin.test.assertTrue import kotlin.test.assertTrue
class LyngHighlightTest { class LyngHighlightTest {
@Test
fun highlightsReturnAndLabels() {
val md = """
```lyng
return 42
break@outer null
return@fn val
```
""".trimIndent()
val html = renderMarkdown(md)
assertTrue(html.contains("hl-kw"), "Expected keyword class for 'return': $html")
assertTrue(html.contains("hl-lbl") || html.contains("hl-ann"), "Expected label/annotation class for @outer/@fn: $html")
assertTrue(html.contains("&gt;&gt;&gt;").xor(true), "Should not contain prompt marker unless expected")
}
@Test @Test
fun highlightsLyngFencedBlock() { fun highlightsLyngFencedBlock() {
val md = """ val md = """