Compare commits
45
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0731d63adf | ||
|
|
434d74b07f | ||
|
|
55b27f831f | ||
|
|
5586e027ea | ||
|
|
cd2b1a9cb7 | ||
|
|
3b6504d3b1 | ||
|
|
357585d3ba | ||
|
|
3a9c567c32 | ||
|
|
8300c2a3ab | ||
|
|
17b4904ae3 | ||
|
|
7e8f1406b5 | ||
|
|
3f235878c0 | ||
|
|
5f3a54d08f | ||
|
|
157b716eb7 | ||
|
|
c9f96464f3 | ||
|
|
0732202c80 | ||
|
|
1e18a162c4 | ||
|
|
0dbfb473bf | ||
|
|
0add0ab54c | ||
|
|
3acd56f55a | ||
|
|
b7838b45ec | ||
|
|
92cb088f36 | ||
|
|
3dfe98a93d | ||
|
|
f6d51e9b94 | ||
|
|
3ac7fd7ceb | ||
|
|
99f883cfc7 | ||
|
|
76a1804dc1 | ||
|
|
35cc8fa930 | ||
|
|
6f86e6ff97 | ||
|
|
fa91afa92b | ||
|
|
00503fa105 | ||
|
|
f7a4497f3f | ||
|
|
1aef64e0fa | ||
|
|
5d8fdce637 | ||
|
|
5a8881bfd5 | ||
|
|
d487886c8f | ||
|
|
180471e4cd | ||
|
|
71a37a2906 | ||
|
|
ab05f83e77 | ||
|
|
9e11519608 | ||
|
|
a2d26fc777 | ||
|
|
dd1a1544c6 | ||
|
|
fba44622e5 | ||
|
|
2737aaa14e | ||
|
|
bce88ced43 |
No files matched your search
+4
-1
@@ -16,4 +16,7 @@ xcuserdata
|
||||
/test.lyng
|
||||
/sample_texts/1.txt.gz
|
||||
/kotlin-js-store/wasm/yarn.lock
|
||||
/distributables
|
||||
/distributables
|
||||
.output*.txt
|
||||
debug.log
|
||||
/build.log
|
||||
@@ -0,0 +1,28 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Tests in 'lyng.lynglib.jvmTest'" type="GradleRunConfiguration" factoryName="Gradle">
|
||||
<ExternalSystemSettings>
|
||||
<option name="executionName" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="externalSystemIdString" value="GRADLE" />
|
||||
<option name="scriptParameters" value="" />
|
||||
<option name="taskDescriptions">
|
||||
<list />
|
||||
</option>
|
||||
<option name="taskNames">
|
||||
<list>
|
||||
<option value=":lynglib:cleanJvmTest" />
|
||||
<option value=":lynglib:jvmTest" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="vmOptions" />
|
||||
</ExternalSystemSettings>
|
||||
<ExternalSystemDebugServerProcess>false</ExternalSystemDebugServerProcess>
|
||||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
|
||||
<ExternalSystemDebugDisabled>false</ExternalSystemDebugDisabled>
|
||||
<DebugAllEnabled>false</DebugAllEnabled>
|
||||
<RunAsTest>true</RunAsTest>
|
||||
<GradleProfilingDisabled>false</GradleProfilingDisabled>
|
||||
<GradleCoverageDisabled>false</GradleCoverageDisabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -1,3 +1,20 @@
|
||||
<!--
|
||||
~ Copyright 2025 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.
|
||||
~
|
||||
-->
|
||||
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="lyng:site [jsBrowserDevelopmentRun]" type="GradleRunConfiguration" factoryName="Gradle">
|
||||
<ExternalSystemSettings>
|
||||
@@ -17,8 +34,11 @@
|
||||
</ExternalSystemSettings>
|
||||
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
|
||||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
|
||||
<ExternalSystemDebugDisabled>false</ExternalSystemDebugDisabled>
|
||||
<DebugAllEnabled>false</DebugAllEnabled>
|
||||
<RunAsTest>false</RunAsTest>
|
||||
<GradleProfilingDisabled>true</GradleProfilingDisabled>
|
||||
<GradleCoverageDisabled>true</GradleCoverageDisabled>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -2,6 +2,15 @@
|
||||
|
||||
### Unreleased
|
||||
|
||||
- Language: Class properties with accessors
|
||||
- Support for `val` (read-only) and `var` (read-write) properties in classes.
|
||||
- Syntax: `val name [ : Type ] get() { body }` or `var name [ : Type ] get() { body } set(value) { body }`.
|
||||
- Laconic Expression Shorthand: `val prop get() = expression` and `var prop get() = read set(v) = write`.
|
||||
- Properties are pure accessors and do **not** have automatic backing fields.
|
||||
- Validation: `var` properties must have both accessors; `val` must have only a getter.
|
||||
- Integration: Updated TextMate grammar and IntelliJ plugin (highlighting + keywords).
|
||||
- Documentation: New "Properties" section in `docs/OOP.md`.
|
||||
|
||||
- Docs: Scopes and Closures guidance
|
||||
- New page: `docs/scopes_and_closures.md` detailing `ClosureScope` resolution order, recursion‑safe helpers (`chainLookupIgnoreClosure`, `chainLookupWithMembers`, `baseGetIgnoreClosure`), cycle prevention, and capturing lexical environments for callbacks (`snapshotForClosure`).
|
||||
- Updated: `docs/advanced_topics.md` (link to the new page), `docs/parallelism.md` (closures in `launch`/`flow`), `docs/OOP.md` (visibility from closures with preserved `currentClassCtx`), `docs/exceptions_handling.md` (compatibility alias `SymbolNotFound`).
|
||||
|
||||
@@ -4,11 +4,15 @@ Please visit the project homepage: [https://lynglang.com](https://lynglang.com)
|
||||
|
||||
- simple, compact, intuitive and elegant modern code:
|
||||
|
||||
```
|
||||
class Point(x,y) {
|
||||
```lyng
|
||||
class Point(x, y) {
|
||||
fun dist() { sqrt(x*x + y*y) }
|
||||
}
|
||||
Point(3,4).dist() //< 5
|
||||
|
||||
// Auto-named arguments shorthand (x: is x: x):
|
||||
val x = 3
|
||||
val y = 4
|
||||
Point(x:, y:).dist() //< 5
|
||||
|
||||
fun swapEnds(first, args..., last, f) {
|
||||
f( last, ...args, first)
|
||||
@@ -39,6 +43,8 @@ and it is multithreaded on platforms supporting it (automatically, no code chang
|
||||
|
||||
- [Language home](https://lynglang.com)
|
||||
- [introduction and tutorial](docs/tutorial.md) - start here please
|
||||
- [Testing and Assertions](docs/Testing.md)
|
||||
- [Efficient Iterables in Kotlin Interop](docs/EfficientIterables.md)
|
||||
- [Samples directory](docs/samples)
|
||||
- [Formatter (core + CLI + IDE)](docs/formatter.md)
|
||||
- [Books directory](docs)
|
||||
@@ -184,6 +190,7 @@ Ready features:
|
||||
- [x] better stack reporting
|
||||
- [x] regular exceptions + extended `when`
|
||||
- [x] multiple inheritance for user classes
|
||||
- [x] class properties (accessors)
|
||||
|
||||
## plan: towards v1.5 Enhancing
|
||||
|
||||
|
||||
+12
-2
@@ -82,7 +82,7 @@ function updateIdeaPluginDownloadLink() {
|
||||
# default target settings
|
||||
case "com" in
|
||||
com)
|
||||
SSH_HOST=sergeych@lynglang.com # host to deploy to
|
||||
SSH_HOST=sergeych@d.lynglang.com # host to deploy to
|
||||
SSH_PORT=22 # ssh port on it
|
||||
ROOT=/bigstore/sergeych_pub/lyng # directory to rsync to
|
||||
;;
|
||||
@@ -98,10 +98,20 @@ esac
|
||||
|
||||
die() { echo "ERROR: $*" 1>&2 ; exit 1; }
|
||||
|
||||
function refreshTextmateZip() {
|
||||
echo "Refreshing distributables/lyng-textmate.zip from editors/..."
|
||||
mkdir -p distributables
|
||||
# We use -r for recursive and -q for quiet (optional)
|
||||
# -j can be used if we want to junk paths, but the request says "contents of editors/"
|
||||
# usually we want to preserve the structure inside editors/
|
||||
(cd editors && zip -rq ../distributables/lyng-textmate.zip .)
|
||||
}
|
||||
|
||||
# Update the IDEA plugin download link in docs (temporarily), then build, then restore the doc
|
||||
refreshTextmateZip
|
||||
updateIdeaPluginDownloadLink || echo "WARN: proceeding without updating IDEA plugin download link"
|
||||
|
||||
./gradlew site:clean site:jsBrowserDistribution
|
||||
./gradlew site:jsBrowserDistribution
|
||||
BUILD_RC=$?
|
||||
|
||||
# Always restore original doc if backup exists
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ Is a [Iterable] with known `size`, a finite [Iterable]:
|
||||
(1)
|
||||
: `comparator(a,b)` should return -1 if `a < b`, +1 if `a > b` or zero.
|
||||
|
||||
See [List], [Set] and [Iterable]
|
||||
See [List], [Set], [Iterable] and [Efficient Iterables in Kotlin Interop](EfficientIterables.md)
|
||||
|
||||
[Iterable]: Iterable.md
|
||||
[List]: List.md
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
# Efficient Iterables in Kotlin Interop
|
||||
|
||||
Lyng provides high-performance iteration mechanisms that allow Kotlin-side code to interact with Lyng iterables efficiently and vice versa.
|
||||
|
||||
## 1. Enumerating Lyng Objects from Kotlin
|
||||
|
||||
To iterate over a Lyng object (like a `List`, `Set`, or `Range`) from Kotlin code, use the virtual `enumerate` method:
|
||||
|
||||
```kotlin
|
||||
val lyngList: Obj = ...
|
||||
lyngList.enumerate(scope) { item ->
|
||||
println("Processing $item")
|
||||
true // return true to continue, false to break
|
||||
}
|
||||
```
|
||||
|
||||
### Why it's efficient:
|
||||
- **Zero allocation**: Unlike traditional iterators, it doesn't create a `LyngIterator` object or any intermediate wrappers.
|
||||
- **Direct access**: Subclasses like `ObjList` override `enumerate` to iterate directly over their internal Kotlin collections.
|
||||
- **Reduced overhead**: It avoids multiple `invokeInstanceMethod` calls for `hasNext()` and `next()` on every step, which would normally involve dynamic dispatch and scope overhead.
|
||||
|
||||
## 2. Reactive Enumeration with Flow
|
||||
|
||||
If you prefer a reactive approach or need to integrate with Kotlin Coroutines flows, use `toFlow()`:
|
||||
|
||||
```kotlin
|
||||
lyngList.toFlow(scope).collect { item ->
|
||||
// ...
|
||||
}
|
||||
```
|
||||
*Note: `toFlow()` internally uses the Lyng iterator protocol (`iterator()`, `hasNext()`, `next()`), so it's slightly less efficient than `enumerate()` for performance-critical loops, but more idiomatic for flow-based processing.*
|
||||
|
||||
## 3. Creating Efficient Iterables for Lyng in Kotlin
|
||||
|
||||
When implementing a custom object in Kotlin that should be iterable in Lyng (e.g., usable in `for (x in myObj) { ... }`), follow these steps to ensure maximum performance.
|
||||
|
||||
### A. Inherit from `Obj` and use `ObjIterable`
|
||||
Ensure your object's class has `ObjIterable` as a parent so the Lyng compiler recognizes it as an iterable.
|
||||
|
||||
```kotlin
|
||||
class MyCollection(val items: List<Obj>) : Obj() {
|
||||
override val objClass = MyCollection.type
|
||||
|
||||
companion object {
|
||||
val type = ObjClass("MyCollection", ObjIterable).apply {
|
||||
// Provide a Lyng-side iterator for compatibility with
|
||||
// manual iterator usage in Lyng scripts.
|
||||
// Using ObjKotlinObjIterator if items are already Obj instances:
|
||||
addFn("iterator") {
|
||||
ObjKotlinObjIterator(thisAs<MyCollection>().items.iterator())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### B. Override `enumerate` for Maximum Performance
|
||||
The Lyng compiler's `for` loops use the `enumerate` method. By overriding it in your Kotlin class, you provide a "fast path" for iteration.
|
||||
|
||||
```kotlin
|
||||
class MyCollection(val items: List<Obj>) : Obj() {
|
||||
// ...
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
for (item in items) {
|
||||
// If callback returns false, it means 'break' was called in Lyng
|
||||
if (!callback(item)) break
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### C. Use `ObjInt.of()` for Numeric Data
|
||||
If your iterable contains integers, always use `ObjInt.of(Long)` instead of the `ObjInt(Long)` constructor. Lyng maintains a cache for small integers (-128 to 127), which significantly reduces object allocations and GC pressure during tight loops.
|
||||
|
||||
```kotlin
|
||||
// Efficiently creating an integer object
|
||||
val obj = ObjInt.of(42L)
|
||||
|
||||
// Or using extension methods which also use the cache:
|
||||
val obj2 = 42.toObj()
|
||||
val obj3 = 42L.toObj()
|
||||
```
|
||||
|
||||
#### Note on `toObj()` extensions:
|
||||
While `<reified T> T.toObj()` is convenient, using specific extensions like `Int.toObj()` or `Long.toObj()` is slightly more efficient as they use the `ObjInt` cache.
|
||||
|
||||
## 4. Summary of Best Practices
|
||||
|
||||
- **To Consume**: Use `enumerate(scope) { item -> ... true }`.
|
||||
- **To Implement**: Override `enumerate` in your `Obj` subclass.
|
||||
- **To Register**: Use `ObjIterable` (or `ObjCollection`) as a parent class in your `ObjClass` definition.
|
||||
- **To Optimize**: Use `ObjInt.of()` (or `.toObj()`) for all integer object allocations.
|
||||
+102
-22
@@ -40,13 +40,13 @@ available, for example
|
||||
## joinToString
|
||||
|
||||
This methods convert any iterable to a string joining string representation of each element, optionally transforming it
|
||||
and joining using specified suffix.
|
||||
and joining using specified separator.
|
||||
|
||||
Iterable.joinToString(suffux=' ', transform=null)
|
||||
Iterable.joinToString(separator=' ', transformer=null)
|
||||
|
||||
- if `Iterable` `isEmpty`, the empty string `""` is returned.
|
||||
- `suffix` is inserted between items when there are more than one.
|
||||
- `transform` of specified is applied to each element, otherwise its `toString()` method is used.
|
||||
- `separator` is inserted between items when there are more than one.
|
||||
- `transformer` of specified is applied to each element, otherwise its `toString()` method is used.
|
||||
|
||||
Here is the sample:
|
||||
|
||||
@@ -55,7 +55,7 @@ Here is the sample:
|
||||
assertEquals( (1..3).joinToString { it * 10 }, "10 20 30")
|
||||
>>> void
|
||||
|
||||
## `sum` and `sumBy`
|
||||
## `sum` and `sumOf`
|
||||
|
||||
These, again, does the thing:
|
||||
|
||||
@@ -68,6 +68,80 @@ These, again, does the thing:
|
||||
|
||||
>>> void
|
||||
|
||||
## map, filter and their variations
|
||||
|
||||
Used to transform or filter the whole iterable stream:
|
||||
|
||||
val source = [1,2,3,4]
|
||||
|
||||
// map: transform every element to something else
|
||||
assertEquals(["n1", "n2", "n3", "n4"], source.map { "n"+it } )
|
||||
|
||||
// filter: keep only elements matching the predicate
|
||||
assertEquals([2, 4], source.filter { it % 2 == 0 } )
|
||||
|
||||
// count: count elements matching the predicate
|
||||
assertEquals(2, source.count { it % 2 == 0 } )
|
||||
|
||||
// mapNotNull: transform every element, skipping null results:
|
||||
assertEquals(["n1", "n2", "n4"], source.mapNotNull { if( it == 3 ) null else "n"+it } )
|
||||
|
||||
// filterNotNull: skip all null elements:
|
||||
assertEquals([1, 2, 4], [1, 2, null, 4].filterNotNull())
|
||||
|
||||
>>> void
|
||||
|
||||
You can also use flow variations that return a cold `Flow` instead of a `List`, which is useful for large or infinite sequences:
|
||||
|
||||
val source = [1, 2, 3, 4]
|
||||
|
||||
// filterFlow: returns a Flow of filtered elements
|
||||
assert( source.filterFlow { it % 2 == 0 } is Flow )
|
||||
|
||||
// filterFlowNotNull: returns a Flow of non-null elements
|
||||
assert( [1, null, 2].filterFlowNotNull() is Flow )
|
||||
|
||||
>>> void
|
||||
|
||||
## minOf and maxOf
|
||||
|
||||
Find the minimum or maximum value of a function applied to each element:
|
||||
|
||||
val source = ["abc", "de", "fghi"]
|
||||
assertEquals(2, source.minOf { it.length })
|
||||
assertEquals(4, source.maxOf { it.length })
|
||||
>>> void
|
||||
|
||||
## flatten and flatMap
|
||||
|
||||
Work with nested collections:
|
||||
|
||||
val nested = [[1, 2], [3, 4]]
|
||||
|
||||
// flatten: combine nested collections into one list
|
||||
assertEquals([1, 2, 3, 4], nested.flatten())
|
||||
|
||||
// flatMap: map each element to a collection and flatten the result
|
||||
assertEquals([1, 10, 2, 20], [1, 2].flatMap { [it, it*10] })
|
||||
|
||||
>>> void
|
||||
|
||||
## findFirst and findFirstOrNull
|
||||
|
||||
Search for the first element that satisfies the given predicate:
|
||||
|
||||
val source = [1, 2, 3, 4]
|
||||
assertEquals( 2, source.findFirst { it % 2 == 0 } )
|
||||
assertEquals( 2, source.findFirstOrNull { it % 2 == 0 } )
|
||||
|
||||
// findFirst throws if not found:
|
||||
assertThrows( NoSuchElementException ) { source.findFirst { it > 10 } }
|
||||
|
||||
// findFirstOrNull returns null if not found:
|
||||
assertEquals( null, source.findFirstOrNull { it > 10 } )
|
||||
|
||||
>>> void
|
||||
|
||||
## Instance methods:
|
||||
|
||||
| fun/method | description |
|
||||
@@ -75,52 +149,56 @@ These, again, does the thing:
|
||||
| toList() | create a list from iterable |
|
||||
| toSet() | create a set from iterable |
|
||||
| contains(i) | check that iterable contains `i` |
|
||||
| `i in iterator` | same as `contains(i)` |
|
||||
| `i in iterable` | same as `contains(i)` |
|
||||
| isEmpty() | check iterable is empty |
|
||||
| forEach(f) | call f for each element |
|
||||
| toMap() | create a map from list of key-value pairs (arrays of 2 items or like) |
|
||||
| any(p) | true if any element matches predicate `p` |
|
||||
| all(p) | true if all elements match predicate `p` |
|
||||
| map(f) | create a list of values returned by `f` called for each element of the iterable |
|
||||
| indexOf(i) | return index if the first encounter of i or a negative value if not found |
|
||||
| associateBy(kf) | create a map where keys are returned by kf that will be called for each element |
|
||||
| filter(p) | create a list of elements matching predicate `p` |
|
||||
| count(p) | count elements matching predicate `p` |
|
||||
| filterFlow(p) | create a [Flow] of elements matching predicate `p` |
|
||||
| filterNotNull() | create a list of non-null elements |
|
||||
| filterFlowNotNull() | create a [Flow] of non-null elements |
|
||||
| minOf(f) | return minimum value of `f` applied to elements |
|
||||
| maxOf(f) | return maximum value of `f` applied to elements |
|
||||
| flatten() | flatten nested collections into a single [List] |
|
||||
| flatMap(f) | map each element with `f` and flatten results into a [List] |
|
||||
| findFirst(p) | return first element matching predicate `p` or throw (1) |
|
||||
| findFirstOrNull(p) | return first element matching predicate `p` or `null` |
|
||||
| first | first element (1) |
|
||||
| last | last element (1) |
|
||||
| take(n) | return [Iterable] of up to n first elements |
|
||||
| taleLast(n) | return [Iterable] of up to n last elements |
|
||||
| takeLast(n) | return [Iterable] of up to n last elements |
|
||||
| drop(n) | return new [Iterable] without first n elements |
|
||||
| dropLast(n) | return new [Iterable] without last n elements |
|
||||
| sum() | return sum of the collection applying `+` to its elements (3) |
|
||||
| sumOf(predicate) | sum of the modified collection items (3) |
|
||||
| sumOf(f) | sum of the modified collection items (3) |
|
||||
| sorted() | return [List] with collection items sorted naturally |
|
||||
| sortedWith(comparator) | sort using a comparator that compares elements (1) |
|
||||
| sortedBy(predicate) | sort by comparing results of the predicate function |
|
||||
| joinToString(s,t) | convert iterable to string, see (2) |
|
||||
| reversed() | create a list containing items from this in reverse order |
|
||||
| shuffled() | create a listof shiffled elements |
|
||||
| shuffled() | create a list of shuffled elements |
|
||||
|
||||
(1)
|
||||
: throws `NoSuchElementException` if there is no such element
|
||||
:: throws `NoSuchElementException` if there is no such element
|
||||
|
||||
(2)
|
||||
: `joinToString(suffix=" ",transform=null)`: suffix is inserted between items if there are more than one, trasnfom is
|
||||
:: `joinToString(separator=" ", transformer=null)`: separator is inserted between items if there are more than one, transformer is
|
||||
optional function applied to each item that must return result string for an item, otherwise `item.toString()` is used.
|
||||
|
||||
(3)
|
||||
: sum of empty collection is `null`
|
||||
|
||||
fun Iterable.toList(): List
|
||||
fun Iterable.toSet(): Set
|
||||
fun Iterable.indexOf(element): Int
|
||||
fun Iterable.contains(element): Bool
|
||||
fun Iterable.isEmpty(element): Bool
|
||||
fun Iterable.forEach(block: (Any?)->Void ): Void
|
||||
fun Iterable.map(block: (Any?)->Void ): List
|
||||
fun Iterable.associateBy( keyMaker: (Any?)->Any): Map
|
||||
:: sum of empty collection is `null`
|
||||
|
||||
## Abstract methods:
|
||||
|
||||
fun iterator(): Iterator
|
||||
|
||||
Creates a list by iterating to the end. So, the Iterator should be finite to be used with it.
|
||||
For high-performance Kotlin-side interop and custom iterable implementation details, see [Efficient Iterables in Kotlin Interop](EfficientIterables.md).
|
||||
|
||||
## Included in interfaces:
|
||||
|
||||
@@ -134,6 +212,8 @@ Creates a list by iterating to the end. So, the Iterator should be finite to be
|
||||
|
||||
[List]: List.md
|
||||
|
||||
[Flow]: parallelism.md#flow
|
||||
|
||||
[Range]: Range.md
|
||||
|
||||
[Set]: Set.md
|
||||
|
||||
@@ -23,6 +23,8 @@ must throw `ObjIterationFinishedError`.
|
||||
|
||||
Iterators are returned when implementing [Iterable] interface.
|
||||
|
||||
For high-performance Kotlin-side interop and custom iterable implementation details, see [Efficient Iterables in Kotlin Interop](EfficientIterables.md).
|
||||
|
||||
## Implemented for classes:
|
||||
|
||||
- [List], [Range]
|
||||
|
||||
+30
-1
@@ -92,6 +92,34 @@ Open end ranges remove head and tail elements:
|
||||
assert( [1, 2, 3] !== [1, 2, 3])
|
||||
>>> void
|
||||
|
||||
## Destructuring
|
||||
|
||||
Lists can be used as L-values for destructuring assignments. This allows you to unpack list elements into multiple variables.
|
||||
|
||||
### Basic Destructuring
|
||||
```lyng
|
||||
val [a, b, c] = [1, 2, 3]
|
||||
```
|
||||
|
||||
### With Splats (Variadic)
|
||||
A single ellipsis `...` can be used to capture remaining elements into a list. It can be placed at the beginning, middle, or end of the pattern.
|
||||
```lyng
|
||||
val [head, rest...] = [1, 2, 3] // head=1, rest=[2, 3]
|
||||
val [first, middle..., last] = [1, 2, 3, 4, 5] // first=1, middle=[2, 3, 4], last=5
|
||||
```
|
||||
|
||||
### Nested Patterns
|
||||
Destructuring patterns can be nested to unpack multi-dimensional lists.
|
||||
```lyng
|
||||
val [a, [b, c...], d] = [1, [2, 3, 4], 5]
|
||||
```
|
||||
|
||||
### Reassignment
|
||||
Destructuring can also be used to reassign existing variables:
|
||||
```lyng
|
||||
[x, y] = [y, x] // Swap values
|
||||
```
|
||||
|
||||
## In-place sort
|
||||
|
||||
List could be sorted in place, just like [Collection] provide sorted copies, in a very like way:
|
||||
@@ -130,7 +158,8 @@ List could be sorted in place, just like [Collection] provide sorted copies, in
|
||||
| `sort()` | in-place sort, natural order | void |
|
||||
| `sortBy(predicate)` | in-place sort bu `predicate` call result (3) | void |
|
||||
| `sortWith(comparator)` | in-place sort using `comarator` function (4) | void |
|
||||
| `shiffle()` | in-place shiffle contents | |
|
||||
| `shuffle()` | in-place shuffle contents | |
|
||||
| `toString()` | string representation like `[a,b,c]` | |
|
||||
|
||||
(1)
|
||||
: optimized implementation that override `Array` one
|
||||
|
||||
+2
-2
@@ -172,7 +172,7 @@ Maps and entries can also be merged with `+` and `+=`:
|
||||
|
||||
Notes:
|
||||
- Map literals always use string keys (identifier keys are converted to strings).
|
||||
- Spreads inside map literals and `+`/`+=` merges require string keys on the right-hand side; this aligns with named-argument splats.
|
||||
- When you need computed or non-string keys, use the constructor form `Map(...)` or build entries with `=>` and then merge.
|
||||
- Spreads inside map literals and `+`/`+=` merges allow any objects as keys.
|
||||
- When you need computed or non-string keys, use the constructor form `Map(...)`, map literals with computed keys (if supported), or build entries with `=>` and then merge.
|
||||
|
||||
[Collection](Collection.md)
|
||||
+163
-6
@@ -42,6 +42,92 @@ a _constructor_ that requires two parameters for fields. So when creating it wit
|
||||
Form now on `Point` is a class, it's type is `Class`, and we can create instances with it as in the
|
||||
example above.
|
||||
|
||||
## 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.
|
||||
|
||||
### Basic Syntax
|
||||
|
||||
Properties are declared using `val` (read-only) or `var` (read-write) followed by a name and `get()`/`set()` blocks:
|
||||
|
||||
```kotlin
|
||||
class Person(private var _age: Int) {
|
||||
// Read-only property
|
||||
val ageCategory
|
||||
get() {
|
||||
if (_age < 18) "Minor" else "Adult"
|
||||
}
|
||||
|
||||
// Read-write property
|
||||
var age: Int
|
||||
get() { _age }
|
||||
set(value) {
|
||||
if (value >= 0) _age = value
|
||||
}
|
||||
}
|
||||
|
||||
val p = Person(15)
|
||||
assertEquals("Minor", p.ageCategory)
|
||||
p.age = 20
|
||||
assertEquals("Adult", p.ageCategory)
|
||||
```
|
||||
|
||||
### Laconic Expression Shorthand
|
||||
|
||||
For simple accessors and methods, you can use the `=` shorthand for a more elegant and laconic form:
|
||||
|
||||
```kotlin
|
||||
class Circle(val radius: Real) {
|
||||
val area get() = π * radius * radius
|
||||
val circumference get() = 2 * π * radius
|
||||
|
||||
fun diameter() = radius * 2
|
||||
}
|
||||
|
||||
fun median(a, b) = (a + b) / 2
|
||||
|
||||
class Counter {
|
||||
private var _count = 0
|
||||
var count get() = _count set(v) = _count = v
|
||||
}
|
||||
```
|
||||
|
||||
### Key Rules
|
||||
|
||||
- **`val` properties** must have a `get()` accessor and cannot have a `set()`.
|
||||
- **`var` properties** must have both `get()` and `set()` accessors.
|
||||
- **Functions and methods** can use the `=` shorthand to return the result of a single expression.
|
||||
- **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field.
|
||||
- **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing.
|
||||
|
||||
## Instance initialization: init block
|
||||
|
||||
In addition to the primary constructor arguments, you can provide an `init` block that runs on each instance creation. This is useful for more complex initializations, side effects, or setting up fields that depend on multiple constructor parameters.
|
||||
|
||||
class Point(val x, val y) {
|
||||
var magnitude
|
||||
|
||||
init {
|
||||
magnitude = Math.sqrt(x*x + y*y)
|
||||
}
|
||||
}
|
||||
|
||||
Key features of `init` blocks:
|
||||
- **Scope**: They have full access to `this` members and all primary constructor parameters.
|
||||
- **Order**: In a single-inheritance scenario, `init` blocks run immediately after the instance fields are prepared but before the primary constructor body logic.
|
||||
- **Multiple blocks**: You can have multiple `init` blocks; they will be executed in the order they appear in the class body.
|
||||
|
||||
### Initialization in Multiple Inheritance
|
||||
|
||||
In cases of multiple inheritance, `init` blocks are executed following the constructor chaining rule:
|
||||
1. All ancestors are initialized first, following the inheritance hierarchy (diamond-safe: each ancestor is initialized exactly once).
|
||||
2. The `init` blocks of each class are executed after its parents have been fully initialized.
|
||||
3. For a hierarchy `class D : B, C`, the initialization order is: `B`'s chain, then `C`'s chain (skipping common ancestors with `B`), and finally `D`'s own `init` blocks.
|
||||
|
||||
### Initialization during Deserialization
|
||||
|
||||
When an object is restored from a serialized form (e.g., using `Lynon`), `init` blocks are **re-executed**. This ensures that transient state or derived fields are correctly recalculated upon restoration. However, primary constructors are **not** re-called during deserialization; only the `init` blocks and field initializers are executed to restore the instance state.
|
||||
|
||||
Class point has a _method_, or a _member function_ `length()` that uses its _fields_ `x` and `y` to
|
||||
calculate the magnitude. Length is called
|
||||
|
||||
@@ -53,8 +139,20 @@ statements discussed later, there could be default values, ellipsis, etc.
|
||||
class Point(x=0,y=0)
|
||||
val p = Point()
|
||||
assert( p.x == 0 && p.y == 0 )
|
||||
|
||||
// Named arguments in constructor calls use colon syntax:
|
||||
val p2 = Point(y: 10, x: 5)
|
||||
assert( p2.x == 5 && p2.y == 10 )
|
||||
|
||||
// Auto-substitution shorthand for named arguments:
|
||||
val x = 1
|
||||
val y = 2
|
||||
val p3 = Point(x:, y:)
|
||||
assert( p3.x == 1 && p3.y == 2 )
|
||||
>>> void
|
||||
|
||||
Note that unlike **Kotlin**, which uses `=` for named arguments, Lyng uses `:` to avoid ambiguity with assignment expressions.
|
||||
|
||||
## Methods
|
||||
|
||||
Functions defined inside a class body are methods, and unless declared
|
||||
@@ -393,9 +491,11 @@ As usual, private statics are not accessible from the outside:
|
||||
|
||||
# Extending classes
|
||||
|
||||
It sometimes happen that the class is missing some particular functionality that can be _added to it_ without rewriting its inner logic and using its private state. In this case _extension methods_ could be used, for example. we want to create an extension method
|
||||
that would test if some object of unknown type contains something that can be interpreted
|
||||
as an integer. In this case we _extend_ class `Object`, as it is the parent class for any instance of any type:
|
||||
It sometimes happen that the class is missing some particular functionality that can be _added to it_ without rewriting its inner logic and using its private state. In this case _extension members_ could be used.
|
||||
|
||||
## Extension methods
|
||||
|
||||
For example, we want to create an extension method that would test if some object of unknown type contains something that can be interpreted as an integer. In this case we _extend_ class `Object`, as it is the parent class for any instance of any type:
|
||||
|
||||
fun Object.isInteger() {
|
||||
when(this) {
|
||||
@@ -420,10 +520,67 @@ as an integer. In this case we _extend_ class `Object`, as it is the parent clas
|
||||
assert( ! "5.2".isInteger() )
|
||||
>>> void
|
||||
|
||||
__Important note__ as for version 0.6.9, extensions are in __global scope__. It means, that once applied to a global type (Int in our sample), they will be available for _all_ contexts, even new created,
|
||||
as they are modifying the type, not the context.
|
||||
## Extension properties
|
||||
|
||||
Beware of it. We might need to reconsider it later.
|
||||
Just like methods, you can extend existing classes with properties. These can be defined using simple initialization (for `val` only) or with custom accessors.
|
||||
|
||||
### Simple val extension
|
||||
|
||||
A read-only extension can be defined by assigning an expression:
|
||||
|
||||
```kotlin
|
||||
val String.isLong = length > 10
|
||||
|
||||
val s = "Hello, world!"
|
||||
assert(s.isLong)
|
||||
```
|
||||
|
||||
### Properties with accessors
|
||||
|
||||
For more complex logic, use `get()` and `set()` blocks:
|
||||
|
||||
```kotlin
|
||||
class Box(var value: Int)
|
||||
|
||||
var Box.doubledValue
|
||||
get() = value * 2
|
||||
set(v) = value = v / 2
|
||||
|
||||
val b = Box(10)
|
||||
assertEquals(20, b.doubledValue)
|
||||
b.doubledValue = 30
|
||||
assertEquals(15, b.value)
|
||||
```
|
||||
|
||||
Extension members are strictly barred from accessing private members of the class they extend, maintaining encapsulation.
|
||||
|
||||
### Extension Scoping and Isolation
|
||||
|
||||
Extensions in Lyng are **scope-isolated**. This means an extension is only visible within the scope where it is defined and its child scopes. This reduces the "attack surface" and prevents extensions from polluting the global space or other modules.
|
||||
|
||||
#### Scope Isolation Example
|
||||
|
||||
You can define different extensions with the same name in different scopes:
|
||||
|
||||
```kotlin
|
||||
fun scopeA() {
|
||||
val Int.description = "Number: " + toString()
|
||||
assertEquals("Number: 42", 42.description)
|
||||
}
|
||||
|
||||
fun scopeB() {
|
||||
val Int.description = "Value: " + toString()
|
||||
assertEquals("Value: 42", 42.description)
|
||||
}
|
||||
|
||||
scopeA()
|
||||
scopeB()
|
||||
|
||||
// Outside those scopes, Int.description is not defined
|
||||
assertThrows { 42.description }
|
||||
```
|
||||
|
||||
This isolation ensures that libraries can use extensions internally without worrying about name collisions with other libraries or the user's code. When a module is imported using `use`, its top-level extensions become available in the importing scope.
|
||||
|
||||
## dynamic symbols
|
||||
|
||||
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
# Testing and Assertions
|
||||
|
||||
Lyng provides several built-in functions for testing and verifying code behavior. These are available in all scripts.
|
||||
|
||||
## Basic Assertions
|
||||
|
||||
### `assert`
|
||||
|
||||
Assert that a condition is true.
|
||||
|
||||
assert(condition, message=null)
|
||||
|
||||
- `condition`: A boolean expression.
|
||||
- `message` (optional): A string message to include in the exception if the assertion fails.
|
||||
|
||||
If the condition is false, it throws an `AssertionFailedException`.
|
||||
|
||||
```lyng
|
||||
assert(1 + 1 == 2)
|
||||
assert(true, "This should be true")
|
||||
```
|
||||
|
||||
### `assertEquals` and `assertEqual`
|
||||
|
||||
Assert that two values are equal. `assertEqual` is an alias for `assertEquals`.
|
||||
|
||||
assertEquals(expected, actual)
|
||||
assertEqual(expected, actual)
|
||||
|
||||
If `expected != actual`, it throws an `AssertionFailedException` with a message showing both values.
|
||||
|
||||
```lyng
|
||||
assertEquals(4, 2 * 2)
|
||||
assertEqual("hello", "hel" + "lo")
|
||||
```
|
||||
|
||||
### `assertNotEquals`
|
||||
|
||||
Assert that two values are not equal.
|
||||
|
||||
assertNotEquals(unexpected, actual)
|
||||
|
||||
If `unexpected == actual`, it throws an `AssertionFailedException`.
|
||||
|
||||
```lyng
|
||||
assertNotEquals(5, 2 * 2)
|
||||
```
|
||||
|
||||
## Exception Testing
|
||||
|
||||
### `assertThrows`
|
||||
|
||||
Assert that a block of code throws an exception.
|
||||
|
||||
assertThrows(code)
|
||||
assertThrows(expectedExceptionClass, code)
|
||||
|
||||
- `expectedExceptionClass` (optional): The class of the exception that is expected to be thrown.
|
||||
- `code`: A lambda block or statement to execute.
|
||||
|
||||
If the code does not throw an exception, an `AssertionFailedException` is raised.
|
||||
If an `expectedExceptionClass` is provided, the thrown exception must be of that class (or its subclass), otherwise an error is raised.
|
||||
|
||||
`assertThrows` returns the caught exception object if successful.
|
||||
|
||||
```lyng
|
||||
// Just assert that something is thrown
|
||||
assertThrows { 1 / 0 }
|
||||
|
||||
// Assert that a specific exception class is thrown
|
||||
assertThrows(NoSuchElementException) {
|
||||
[1, 2, 3].findFirst { it > 10 }
|
||||
}
|
||||
|
||||
// You can use the returned exception
|
||||
val ex = assertThrows { throw Exception("custom error") }
|
||||
assertEquals("custom error", ex.message)
|
||||
```
|
||||
|
||||
## Other Validation Functions
|
||||
|
||||
While not strictly for testing, these functions help in defensive programming:
|
||||
|
||||
### `require`
|
||||
|
||||
require(condition, message="requirement not met")
|
||||
|
||||
Throws an `IllegalArgumentException` if the condition is false. Use this for validating function arguments.
|
||||
|
||||
If we want to evaluate the message lazily:
|
||||
|
||||
require(condition) { "requirement not met: %s"(someData) }
|
||||
|
||||
In this case, formatting will only occur if the condition is not met.
|
||||
|
||||
|
||||
|
||||
### `check`
|
||||
|
||||
check(condition, message="check failed")
|
||||
|
||||
Throws an `IllegalStateException` if the condition is false. Use this for validating internal state.
|
||||
|
||||
With lazy message evaluation:
|
||||
|
||||
check(condition) { "check failed: %s"(someData) }
|
||||
|
||||
In this case, formatting will only occur if the condition is not met.
|
||||
|
||||
+37
-17
@@ -75,6 +75,13 @@ destructuring arrays when calling functions and lambdas:
|
||||
getFirstAndLast( ...(1..10) ) // see "splats" section below
|
||||
>>> [1,10]
|
||||
|
||||
Note that array destructuring can also be used in assignments:
|
||||
|
||||
val [first, middle..., last] = [1, 2, 3, 4, 5]
|
||||
[x, y] = [y, x] // Swap
|
||||
|
||||
See [tutorial] and [List] documentation for more details on destructuring assignments.
|
||||
|
||||
# Splats
|
||||
|
||||
Ellipsis allows to convert argument lists to lists. The inversa algorithm that converts [List],
|
||||
@@ -100,41 +107,53 @@ There could be any number of splats at any positions. You can splat any other [I
|
||||
|
||||
## Named arguments in calls
|
||||
|
||||
Lyng supports named arguments at call sites using colon syntax `name: value`:
|
||||
Lyng supports named arguments at call sites using colon syntax `name: value`.
|
||||
|
||||
### Shorthand for Named Arguments
|
||||
|
||||
If you want to pass a variable as a named argument and the variable has the same name as the parameter, you can omit the value and use the shorthand `name:`. This is highly readable and matches the shorthand for map literals.
|
||||
|
||||
```lyng
|
||||
fun test(a="foo", b="bar", c="bazz") { [a, b, c] }
|
||||
|
||||
assertEquals(["foo", "b", "bazz"], test(b: "b"))
|
||||
assertEquals(["a", "bar", "c"], test("a", c: "c"))
|
||||
fun test(a, b, c) { [a, b, c] }
|
||||
|
||||
val a = 1
|
||||
val b = 2
|
||||
val c = 3
|
||||
|
||||
// Explicit:
|
||||
assertEquals([1, 2, 3], test(a: a, b: b, c: c))
|
||||
|
||||
// Shorthand (preferred):
|
||||
assertEquals([1, 2, 3], test(a:, b:, c:))
|
||||
```
|
||||
|
||||
Rules:
|
||||
This shorthand is elegant, reduces boilerplate, and is consistent with Lyng's map literal syntax. It works for both function calls and class constructors.
|
||||
|
||||
Rules for named arguments:
|
||||
|
||||
- Named arguments must follow positional arguments. After the first named argument, no positional arguments may appear inside the parentheses.
|
||||
- The only exception is the syntactic trailing block after the call: `f(args) { ... }`. This block is outside the parentheses and is handled specially (see below).
|
||||
- A named argument cannot reassign a parameter already set positionally.
|
||||
- If the last parameter has already been assigned by a named argument (or named splat), a trailing block is not allowed and results in an error.
|
||||
|
||||
Why `:` and not `=` at call sites? In Lyng, `=` is an expression (assignment), so we use `:` to avoid ambiguity. Declarations continue to use `:` for types, while call sites use `as` / `as?` for type operations.
|
||||
Why `:` and not `=` at call sites? In Lyng, `=` is an expression (assignment), so we use `:` to avoid ambiguity. This is a key difference from **Kotlin**, which uses `=` for named arguments. Declarations in Lyng continue to use `:` for types, while call sites use `as` / `as?` for type operations.
|
||||
|
||||
## Named splats (map splats)
|
||||
|
||||
Splat (`...`) of a Map provides named arguments to the call. Only string keys are allowed:
|
||||
Splat (`...`) of a Map provides named arguments to the call. Only string keys are allowed. You can use the same auto-substitution shorthand inside map literals used for splats:
|
||||
|
||||
```lyng
|
||||
fun test(a="a", b="b", c="c", d="d") { [a, b, c, d] }
|
||||
val r = test("A?", ...Map("d" => "D!", "b" => "B!"))
|
||||
assertEquals(["A?","B!","c","D!"], r)
|
||||
```
|
||||
|
||||
The same with a map literal is often more concise. Define the literal, then splat the variable:
|
||||
|
||||
fun test(a="a", b="b", c="c", d="d") { [a, b, c, d] }
|
||||
val patch = { d: "D!", b: "B!" }
|
||||
|
||||
val b = "B!"
|
||||
val d = "D!"
|
||||
|
||||
// Auto-substitution in map literal:
|
||||
val patch = { d:, b: }
|
||||
|
||||
val r = test("A?", ...patch)
|
||||
assertEquals(["A?","B!","c","D!"], r)
|
||||
>>> void
|
||||
```
|
||||
|
||||
Constraints:
|
||||
|
||||
@@ -155,3 +174,4 @@ If a call is immediately followed by a block `{ ... }`, it is treated as an extr
|
||||
|
||||
|
||||
[tutorial]: tutorial.md
|
||||
[List]: List.md
|
||||
@@ -0,0 +1,25 @@
|
||||
# Migration of Instant and Clock
|
||||
|
||||
## History
|
||||
|
||||
Before kotlin 2.0, there was an excellent library, kotlinx.datetime, which was widely used everywhere, also in Lyng and its dependencies.
|
||||
|
||||
When kotlin 2.0 was released, or soon after, JetBrains made an exptic decision to remove `Instant` and `Clock` from kotlinx.datetime and replace it with _yet experimental_ analogs in `kotlin.time`.
|
||||
|
||||
The problem is, these were not quite the same (these weren't `@Serializable`!), so people didn't migrate with ease. Okay, then JetBrains decided to not only deprecate it but also make them unusable on Apple targets. It sort of split auditories of many published libraries to those who hate JetBrains and Apple and continue to use 1.9-2.0 compatible versions that no longer work with Kotlin 2.2 on Apple targets (but work pretty well with earlier Kotlin or on other platforms).
|
||||
|
||||
Later JetBrains added serializers for their new `Instant` and `Clock` types, but strangely not in the stdlib, but in newer versions of `kotlinx.serialization`. This means that plain upgrade of dependencies to 2.2 is not enough to make them work.
|
||||
|
||||
## Solution
|
||||
|
||||
We hereby publish a new version of Lyng, 1.0.8-SNAPSHOT, which uses `ktlin.time.Instant` and `kotlin.time.Clock` instead of `kotlinx.datetime.Instant` and `kotlinx.datetime.Clock; it is in other aspects compatible also with Lynon encoded binaries. Still you might need to migrate your code to use `kotlinx.datetime` types.
|
||||
|
||||
So, if you are getting errors with new version, plase do:
|
||||
|
||||
- upgrade to Kotlin 2.2
|
||||
- upgrade to Lyng 1.0.8-SNAPSHOT
|
||||
- replace in your code imports (or other uses) of`kotlinx.datetime.Clock` to `kotlin.time.Clock` and `kotlinx.datetime.Instant` to `kotlin.time.Instant`.
|
||||
|
||||
This should solve the problem and hopefully we'll see no more suh a brillant ideas from IDEA ideologspersons.
|
||||
|
||||
Sorry for inconvenicence and send a ray of hate to JetBrains ;)
|
||||
+104
-29
@@ -9,6 +9,7 @@ __Other documents to read__ maybe after this one:
|
||||
- [Advanced topics](advanced_topics.md), [declaring arguments](declaring_arguments.md), [Scopes and Closures](scopes_and_closures.md)
|
||||
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
|
||||
- [math in Lyng](math.md), [the `when` statement](when.md)
|
||||
- [Testing and Assertions](Testing.md)
|
||||
- [time](time.md) and [parallelism](parallelism.md)
|
||||
- [parallelism] - multithreaded code, coroutines, etc.
|
||||
- Some class
|
||||
@@ -106,6 +107,41 @@ Assignemnt is an expression that changes its lvalue and return assigned value:
|
||||
>>> 11
|
||||
>>> 6
|
||||
|
||||
### Destructuring assignments
|
||||
|
||||
Lyng supports destructuring assignments for lists. This allows you to unpack list elements into multiple variables at once:
|
||||
|
||||
val [a, b, c] = [1, 2, 3]
|
||||
assertEquals(1, a)
|
||||
assertEquals(2, b)
|
||||
assertEquals(3, c)
|
||||
|
||||
It also supports *splats* (ellipsis) to capture multiple elements into a list:
|
||||
|
||||
val [head, rest...] = [1, 2, 3]
|
||||
assertEquals(1, head)
|
||||
assertEquals([2, 3], rest)
|
||||
|
||||
val [first, middle..., last] = [1, 2, 3, 4, 5]
|
||||
assertEquals(1, first)
|
||||
assertEquals([2, 3, 4], middle)
|
||||
assertEquals(5, last)
|
||||
|
||||
Destructuring can be nested:
|
||||
|
||||
val [x, [y, z...]] = [1, [2, 3, 4]]
|
||||
assertEquals(1, x)
|
||||
assertEquals(2, y)
|
||||
assertEquals([3, 4], z)
|
||||
|
||||
And it can be used for reassigning existing variables, for example, to swap values:
|
||||
|
||||
var x = 5
|
||||
var y = 10
|
||||
[x, y] = [y, x]
|
||||
assertEquals(10, x)
|
||||
assertEquals(5, y)
|
||||
|
||||
As the assignment itself is an expression, you can use it in strange ways. Just remember
|
||||
to use parentheses as assignment operation insofar is left-associated and will not
|
||||
allow chained assignments (we might fix it later). Use parentheses insofar:
|
||||
@@ -311,8 +347,6 @@ Reference quality and object equality example:
|
||||
assert( null == null) // singletons
|
||||
assert( null === null)
|
||||
// but, for non-singletons:
|
||||
assert( 5 == 5)
|
||||
assert( 5 !== 5)
|
||||
assert( "foo" !== "foo" )
|
||||
>>> void
|
||||
|
||||
@@ -329,7 +363,7 @@ will be thrown:
|
||||
// WRONG! Exception will be thrown at next line:
|
||||
foo + "bar"
|
||||
|
||||
Correct pattern is:
|
||||
The correct pattern is:
|
||||
|
||||
foo = "foo"
|
||||
// now is OK:
|
||||
@@ -403,6 +437,19 @@ It is possible to define also vararg using ellipsis:
|
||||
|
||||
See the [arguments reference](declaring_arguments.md) for more details.
|
||||
|
||||
## Named arguments
|
||||
|
||||
When calling functions, you can use named arguments with the colon syntax `name: value`. This is particularly useful when you have many parameters with default values.
|
||||
|
||||
```lyng
|
||||
fun test(a="foo", b="bar", c="bazz") { [a, b, c] }
|
||||
|
||||
assertEquals(["foo", "b", "bazz"], test(b: "b"))
|
||||
assertEquals(["a", "bar", "c"], test("a", c: "c"))
|
||||
```
|
||||
|
||||
**Note for Kotlin users:** Lyng uses `:` instead of `=` for named arguments at call sites. This is because in Lyng, `=` is an expression that returns the assigned value, and using it in an argument list would create ambiguity.
|
||||
|
||||
## Closures
|
||||
|
||||
Each __block has an isolated context that can be accessed from closures__. For example:
|
||||
@@ -478,6 +525,8 @@ one could be with ellipsis that means "the rest pf arguments as List":
|
||||
|
||||
### Using lambda as the parameter
|
||||
|
||||
See also: [Testing and Assertions](Testing.md)
|
||||
|
||||
// note that fun returns its last calculated value,
|
||||
// in our case, result after in-place addition:
|
||||
fun mapValues(iterable, transform) {
|
||||
@@ -1378,31 +1427,31 @@ Part match:
|
||||
|
||||
Typical set of String functions includes:
|
||||
|
||||
| fun/prop | description / notes |
|
||||
|--------------------|------------------------------------------------------------|
|
||||
| lower() | change case to unicode upper |
|
||||
| upper() | change case to unicode lower |
|
||||
| trim() | trim space chars from both ends |
|
||||
| startsWith(prefix) | true if starts with a prefix |
|
||||
| endsWith(prefix) | true if ends with a prefix |
|
||||
| last() | get last character of a string or throw |
|
||||
| take(n) | get a new string from up to n first characters |
|
||||
| takeLast(n) | get a new string from up to n last characters |
|
||||
| drop(n) | get a new string dropping n first chars, or empty string |
|
||||
| dropLast(n) | get a new string dropping n last chars, or empty string |
|
||||
| size | size in characters like `length` because String is [Array] |
|
||||
| (args...) | sprintf-like formatting, see [string formatting] |
|
||||
| [index] | character at index |
|
||||
| [Range] | substring at range (2) |
|
||||
| [Regex] | find first match of regex, like [Regex.find] (2) |
|
||||
| s1 + s2 | concatenation |
|
||||
| s1 += s2 | self-modifying concatenation |
|
||||
| toReal() | attempts to parse string as a Real value |
|
||||
| toInt() | parse string to Int value |
|
||||
| characters() | create [List] of characters (1) |
|
||||
| encodeUtf8() | returns [Buffer] with characters encoded to utf8 |
|
||||
| matches(re) | matches the regular expression (2) |
|
||||
| | |
|
||||
| fun/prop | description / notes |
|
||||
|----------------------|------------------------------------------------------------|
|
||||
| lower(), lowercase() | change case to unicode upper |
|
||||
| upper(), uppercase() | change case to unicode lower |
|
||||
| trim() | trim space chars from both ends |
|
||||
| startsWith(prefix) | true if starts with a prefix |
|
||||
| endsWith(prefix) | true if ends with a prefix |
|
||||
| last() | get last character of a string or throw |
|
||||
| take(n) | get a new string from up to n first characters |
|
||||
| takeLast(n) | get a new string from up to n last characters |
|
||||
| drop(n) | get a new string dropping n first chars, or empty string |
|
||||
| dropLast(n) | get a new string dropping n last chars, or empty string |
|
||||
| size | size in characters like `length` because String is [Array] |
|
||||
| (args...) | sprintf-like formatting, see [string formatting] |
|
||||
| [index] | character at index |
|
||||
| [Range] | substring at range (2) |
|
||||
| [Regex] | find first match of regex, like [Regex.find] (2) |
|
||||
| s1 + s2 | concatenation |
|
||||
| s1 += s2 | self-modifying concatenation |
|
||||
| toReal() | attempts to parse string as a Real value |
|
||||
| toInt() | parse string to Int value |
|
||||
| characters() | create [List] of characters (1) |
|
||||
| encodeUtf8() | returns [Buffer] with characters encoded to utf8 |
|
||||
| matches(re) | matches the regular expression (2) |
|
||||
| | |
|
||||
|
||||
(1)
|
||||
: List is mutable therefore a new copy is created on each call.
|
||||
@@ -1466,6 +1515,8 @@ Lambda avoid unnecessary execution if assertion is not failed. for example:
|
||||
|
||||
[List]: List.md
|
||||
|
||||
[Testing]: Testing.md
|
||||
|
||||
[Iterable]: Iterable.md
|
||||
|
||||
[Iterator]: Iterator.md
|
||||
@@ -1557,4 +1608,28 @@ Notes:
|
||||
- `private` is visible only inside the declaring class; `protected` is visible from the declaring class and any of its transitive subclasses. Qualialsofication (`this@Type`) or casts do not bypass visibility.
|
||||
- Safe‑call `?.` works with `as?` for optional dispatch.
|
||||
|
||||
To get details on OOP in Lyng, see [OOP notes](oop.md).
|
||||
## Extension members
|
||||
|
||||
You can add new methods and properties to existing classes without modifying them.
|
||||
|
||||
### Extension functions
|
||||
|
||||
fun String.shout() = this.upper() + "!!!"
|
||||
"hello".shout()
|
||||
>>> "HELLO!!!"
|
||||
|
||||
### Extension properties
|
||||
|
||||
val Int.isEven = this % 2 == 0
|
||||
4.isEven
|
||||
>>> true
|
||||
|
||||
Example with custom accessors:
|
||||
|
||||
val String.firstChar get() = this[0]
|
||||
"abc".firstChar
|
||||
>>> 'a'
|
||||
|
||||
Extension members are **scope-isolated**: they are visible only in the scope where they are defined and its children. This prevents name collisions and improves security.
|
||||
|
||||
To get details on OOP in Lyng, see [OOP notes](OOP.md).
|
||||
@@ -76,8 +76,8 @@
|
||||
},
|
||||
"labels": { "patterns": [ { "name": "entity.name.label.lyng", "match": "[\\p{L}_][\\p{L}\\p{N}_]*:" } ] },
|
||||
"directives": { "patterns": [ { "name": "meta.directive.lyng", "match": "^\\s*#[_A-Za-z][_A-Za-z0-9]*" } ] },
|
||||
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(?:fun|fn)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(?:val|var)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "variable.other.declaration.lyng" } } } ] },
|
||||
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|val|var|import|package|constructor|property|open|extern|private|protected|static)\\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" } ] },
|
||||
"declarations": { "patterns": [ { "name": "meta.function.declaration.lyng", "match": "\\b(fun|fn)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "entity.name.function.lyng" } } }, { "name": "meta.type.declaration.lyng", "match": "\\b(?:class|enum)\\s+([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "entity.name.type.lyng" } } }, { "name": "meta.variable.declaration.lyng", "match": "\\b(val|var)\\s+(?:([\\p{L}_][\\p{L}\\p{N}_]*)\\.)?([\\p{L}_][\\p{L}\\p{N}_]*)", "captures": { "1": { "name": "keyword.declaration.lyng" }, "2": { "name": "entity.name.type.lyng" }, "3": { "name": "variable.other.declaration.lyng" } } } ] },
|
||||
"keywords": { "patterns": [ { "name": "keyword.control.lyng", "match": "\\b(?:if|else|when|while|do|for|try|catch|finally|throw|return|break|continue)\\b" }, { "name": "keyword.declaration.lyng", "match": "\\b(?:fun|fn|class|enum|val|var|import|package|constructor|property|open|extern|private|protected|static|get|set)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\bnot\\s+(?:in|is)\\b" }, { "name": "keyword.operator.word.lyng", "match": "\\b(?:and|or|not|in|is|as|as\\?)\\b" } ] },
|
||||
"constants": { "patterns": [ { "name": "constant.language.lyng", "match": "(?:\\b(?:true|false|null|this)\\b|π)" } ] },
|
||||
"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": "[!?]" } ] },
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
[versions]
|
||||
agp = "8.5.2"
|
||||
clikt = "5.0.3"
|
||||
kotlin = "2.2.21"
|
||||
kotlin = "2.3.0"
|
||||
android-minSdk = "24"
|
||||
android-compileSdk = "34"
|
||||
kotlinx-coroutines = "1.10.2"
|
||||
mp_bintools = "0.1.12"
|
||||
mp_bintools = "0.3.2"
|
||||
firebaseCrashlyticsBuildtools = "3.0.3"
|
||||
okioVersion = "3.10.2"
|
||||
compiler = "3.2.0-alpha11"
|
||||
|
||||
@@ -21,7 +21,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "net.sergeych.lyng"
|
||||
version = "0.0.3-SNAPSHOT"
|
||||
version = "0.0.4-SNAPSHOT"
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(17)
|
||||
|
||||
+5
-1
@@ -157,8 +157,12 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
||||
is MiniFunDecl -> {
|
||||
addTypeSegments(d.returnType)
|
||||
d.params.forEach { addTypeSegments(it.type) }
|
||||
addTypeSegments(d.receiver)
|
||||
}
|
||||
is MiniValDecl -> {
|
||||
addTypeSegments(d.type)
|
||||
addTypeSegments(d.receiver)
|
||||
}
|
||||
is MiniValDecl -> addTypeSegments(d.type)
|
||||
is MiniClassDecl -> {
|
||||
d.ctorFields.forEach { addTypeSegments(it.type) }
|
||||
d.classFields.forEach { addTypeSegments(it.type) }
|
||||
|
||||
+46
-3
@@ -1,3 +1,20 @@
|
||||
/*
|
||||
* Copyright 2025 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Lightweight BASIC completion for Lyng, MVP version.
|
||||
* Uses MiniAst (best-effort) + BuiltinDocRegistry to suggest symbols.
|
||||
@@ -167,7 +184,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
?: guessReturnClassAcrossKnownCallees(text, memberDotPos, imported)
|
||||
?: guessReceiverClass(text, memberDotPos, imported)
|
||||
if (!inferredClass.isNullOrBlank()) {
|
||||
val ext = BuiltinDocRegistry.extensionMethodNamesFor(inferredClass)
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(inferredClass)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Post-engine extension check for $inferredClass: ${'$'}{ext}")
|
||||
for (name in ext) {
|
||||
if (existing.contains(name)) continue
|
||||
@@ -192,6 +209,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
emit(builder)
|
||||
existing.add(name)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
} else {
|
||||
// Fallback: emit simple method name without detailed types
|
||||
@@ -329,6 +347,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
when (m) {
|
||||
is MiniMemberFunDecl -> if (!m.isStatic) continue
|
||||
is MiniMemberValDecl -> if (!m.isStatic) continue
|
||||
is MiniInitDecl -> continue
|
||||
}
|
||||
}
|
||||
val list = target.getOrPut(m.name) { mutableListOf() }
|
||||
@@ -404,6 +423,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
.withTypeText(typeOf((chosen as MiniMemberValDecl).type), true)
|
||||
emit(builder)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -454,6 +474,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
emit(builder)
|
||||
already.add(name)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
} else {
|
||||
// Synthetic fallback: method without detailed params/types to improve UX in absence of docs
|
||||
@@ -473,10 +494,10 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
}
|
||||
}
|
||||
|
||||
// Supplement with stdlib extension-like methods defined in root.lyng (e.g., fun String.trim(...))
|
||||
// Supplement with stdlib extension members defined in root.lyng (e.g., fun String.trim(...))
|
||||
run {
|
||||
val already = (directMap.keys + inheritedMap.keys).toMutableSet()
|
||||
val ext = BuiltinDocRegistry.extensionMethodNamesFor(className)
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(className)
|
||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Extensions for $className: count=${ext.size} -> ${ext}")
|
||||
for (name in ext) {
|
||||
if (already.contains(name)) continue
|
||||
@@ -504,6 +525,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
already.add(name)
|
||||
continue
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
}
|
||||
// Fallback: emit without detailed types if we couldn't resolve
|
||||
@@ -613,6 +635,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
val rt = when (m) {
|
||||
is MiniMemberFunDecl -> m.returnType
|
||||
is MiniMemberValDecl -> m.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
simpleClassNameOf(rt)
|
||||
}
|
||||
@@ -715,6 +738,24 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
if (hasDigits) {
|
||||
return if (isHex) "Int" else if (hasDot || hasExp) "Real" else "Int"
|
||||
}
|
||||
|
||||
// 3) this@Type or as Type
|
||||
val identRange = TextCtx.wordRangeAt(text, i + 1)
|
||||
if (identRange != null) {
|
||||
val ident = text.substring(identRange.startOffset, identRange.endOffset)
|
||||
// if it's "as Type", we want Type
|
||||
var k2 = TextCtx.prevNonWs(text, identRange.startOffset - 1)
|
||||
if (k2 >= 1 && text[k2] == 's' && text[k2 - 1] == 'a' && (k2 - 1 == 0 || !text[k2 - 2].isLetterOrDigit())) {
|
||||
return ident
|
||||
}
|
||||
// if it's "this@Type", we want Type
|
||||
if (k2 >= 0 && text[k2] == '@') {
|
||||
val k3 = TextCtx.prevNonWs(text, k2 - 1)
|
||||
if (k3 >= 3 && text.substring(k3 - 3, k3 + 1) == "this") {
|
||||
return ident
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -761,6 +802,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
val returnType = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(returnType)
|
||||
}
|
||||
@@ -840,6 +882,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
||||
val returnType = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(returnType)
|
||||
}
|
||||
|
||||
@@ -154,7 +154,27 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
} else null
|
||||
} else null
|
||||
}
|
||||
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
|
||||
else -> {
|
||||
val guessed = DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules)
|
||||
if (guessed != null) guessed
|
||||
else {
|
||||
// handle this@Type or as Type
|
||||
val i2 = TextCtx.prevNonWs(text, dotPos - 1)
|
||||
if (i2 >= 0) {
|
||||
val identRange = TextCtx.wordRangeAt(text, i2 + 1)
|
||||
if (identRange != null) {
|
||||
val id = text.substring(identRange.startOffset, identRange.endOffset)
|
||||
val k = TextCtx.prevNonWs(text, identRange.startOffset - 1)
|
||||
if (k >= 1 && text[k] == 's' && text[k-1] == 'a' && (k-1 == 0 || !text[k-2].isLetterOrDigit())) {
|
||||
id
|
||||
} else if (k >= 0 && text[k] == '@') {
|
||||
val k2 = TextCtx.prevNonWs(text, k - 1)
|
||||
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") id else null
|
||||
} else null
|
||||
} else null
|
||||
} else null
|
||||
}
|
||||
}
|
||||
}
|
||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos>0) text[dotPos-1] else ' '}' classGuess=${className} imports=${importedModules}")
|
||||
if (className != null) {
|
||||
@@ -163,6 +183,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}")
|
||||
@@ -224,6 +245,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -242,6 +264,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -254,6 +277,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -268,6 +292,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return when (m) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc("String", m)
|
||||
is MiniMemberValDecl -> renderMemberValDoc("String", m)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -277,6 +302,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
||||
return when (member) {
|
||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,8 @@ class LyngLexer : LexerBase() {
|
||||
private val keywords = setOf(
|
||||
"fun", "val", "var", "class", "type", "import", "as",
|
||||
"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"
|
||||
)
|
||||
|
||||
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
|
||||
|
||||
@@ -38,7 +38,7 @@ kotlin {
|
||||
publishLibraryVariants("release")
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_11)
|
||||
jvmTarget.set(JvmTarget.JVM_17)
|
||||
}
|
||||
}
|
||||
iosX64()
|
||||
@@ -71,11 +71,7 @@ kotlin {
|
||||
sourceSets {
|
||||
all {
|
||||
languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
|
||||
// languageSettings.optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
|
||||
// Correct opt-in markers for coroutines
|
||||
// languageSettings.optIn("kotlinx.coroutines.DelicateCoroutinesApi")
|
||||
// languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
|
||||
// languageSettings.optIn("kotlinx.coroutines.FlowPreview")
|
||||
languageSettings.optIn("kotlin.time.ExperimentalTime")
|
||||
}
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
@@ -115,8 +111,8 @@ android {
|
||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_11
|
||||
targetCompatibility = JavaVersion.VERSION_11
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
lint {
|
||||
// Prevent Android Lint from failing the build due to Kotlin toolchain
|
||||
|
||||
@@ -163,7 +163,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
||||
fsGuard {
|
||||
val self = this.thisObj as ObjPath
|
||||
val m = self.ensureMetadata()
|
||||
m.createdAtMillis?.let { ObjInstant(kotlinx.datetime.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||
m.createdAtMillis?.let { ObjInstant(kotlin.time.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||
}
|
||||
}
|
||||
// createdAtMillis(): Int? — milliseconds since epoch or null
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
group = "net.sergeych"
|
||||
version = "1.0.7-SNAPSHOT"
|
||||
version = "1.1.0-SNAPSHOT"
|
||||
|
||||
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
||||
|
||||
@@ -51,7 +51,7 @@ kotlin {
|
||||
publishLibraryVariants("release")
|
||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_11)
|
||||
jvmTarget.set(JvmTarget.JVM_17)
|
||||
}
|
||||
}
|
||||
iosX64()
|
||||
@@ -89,16 +89,16 @@ kotlin {
|
||||
languageSettings.optIn("kotlinx.coroutines.DelicateCoroutinesApi")
|
||||
languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
|
||||
languageSettings.optIn("kotlinx.coroutines.FlowPreview")
|
||||
languageSettings.optIn("kotlin.time.ExperimentalTime")
|
||||
}
|
||||
|
||||
val commonMain by getting {
|
||||
kotlin.srcDir("$buildDir/generated/buildConfig/commonMain/kotlin")
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
||||
//put your multiplatform dependencies here
|
||||
api(libs.kotlinx.coroutines.core)
|
||||
api(libs.mp.bintools)
|
||||
api("net.sergeych:mp_stools:1.5.2")
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
@@ -116,6 +116,18 @@ kotlin {
|
||||
}
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "net.sergeych.lynglib"
|
||||
compileSdk = libs.versions.android.compileSdk.get().toInt()
|
||||
defaultConfig {
|
||||
minSdk = libs.versions.android.minSdk.get().toInt()
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Build-time generation of stdlib text from .lyng files into a Kotlin constant ----
|
||||
// Implemented as a proper task type compatible with Gradle Configuration Cache
|
||||
|
||||
|
||||
@@ -49,12 +49,14 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
||||
arguments: Arguments = scope.args,
|
||||
defaultAccessType: AccessType = AccessType.Var,
|
||||
defaultVisibility: Visibility = Visibility.Public,
|
||||
declaringClass: net.sergeych.lyng.obj.ObjClass? = scope.currentClassCtx
|
||||
) {
|
||||
fun assign(a: Item, value: Obj) {
|
||||
scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable,
|
||||
value.byValueCopy(),
|
||||
a.visibility ?: defaultVisibility,
|
||||
recordType = ObjRecord.Type.Argument)
|
||||
recordType = ObjRecord.Type.Argument,
|
||||
declaringClass = declaringClass)
|
||||
}
|
||||
|
||||
// Prepare positional args and parameter count, handle tail-block binding
|
||||
|
||||
@@ -58,17 +58,25 @@ class ClosureScope(val callScope: Scope, val closureScope: Scope) :
|
||||
?.instanceScope
|
||||
?.objects
|
||||
?.get(name)
|
||||
?.let { return it }
|
||||
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
|
||||
?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
|
||||
}
|
||||
findExtension(closureScope.thisObj.objClass, name)?.let { return it }
|
||||
closureScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
|
||||
}
|
||||
|
||||
// 3) Closure scope chain (locals/parents + members), ignore ClosureScope overrides to prevent recursion
|
||||
closureScope.chainLookupWithMembers(name)?.let { return it }
|
||||
closureScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
|
||||
|
||||
// 4) Caller `this` members
|
||||
callScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
|
||||
findExtension(callScope.thisObj.objClass, name)?.let { return it }
|
||||
callScope.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) return rec
|
||||
}
|
||||
|
||||
// 5) Caller chain (locals/parents + members)
|
||||
callScope.chainLookupWithMembers(name)?.let { return it }
|
||||
callScope.chainLookupWithMembers(name, currentClassCtx)?.let { return it }
|
||||
|
||||
// 6) Module pseudo-symbols (e.g., __PACKAGE__) — walk caller ancestry and query ModuleScope directly
|
||||
if (name.startsWith("__")) {
|
||||
|
||||
@@ -674,12 +674,19 @@ class Compiler(
|
||||
Token.Type.RBRACKET -> return entries
|
||||
Token.Type.ELLIPSIS -> {
|
||||
parseExpressionLevel()?.let { entries += ListEntry.Spread(it) }
|
||||
?: throw ScriptError(t.pos, "spread element must have an expression")
|
||||
}
|
||||
|
||||
else -> {
|
||||
cc.previous()
|
||||
parseExpressionLevel()?.let { entries += ListEntry.Element(it) }
|
||||
?: throw ScriptError(t.pos, "invalid list literal: expecting expression")
|
||||
parseExpressionLevel()?.let { expr ->
|
||||
if (cc.current().type == Token.Type.ELLIPSIS) {
|
||||
cc.next()
|
||||
entries += ListEntry.Spread(expr)
|
||||
} else {
|
||||
entries += ListEntry.Element(expr)
|
||||
}
|
||||
} ?: throw ScriptError(t.pos, "invalid list literal: expecting expression")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1029,6 +1036,12 @@ class Compiler(
|
||||
if (t2.type == Token.Type.COLON) {
|
||||
// name: expr
|
||||
val name = t1.value
|
||||
// Check for shorthand: name: (comma or rparen)
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.COMMA || next.type == Token.Type.RPAREN) {
|
||||
val localVar = LocalVarRef(name, t1.pos)
|
||||
return ParsedArgument(statement(t1.pos) { localVar.get(it).value }, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
val rhs = parseExpression() ?: t2.raiseSyntax("expected expression after named argument '${name}:'")
|
||||
return ParsedArgument(rhs, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
@@ -1044,7 +1057,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
Token.Type.ELLIPSIS -> {
|
||||
parseStatement()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
parseExpression()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
?: throw ScriptError(t.pos, "Expecting arguments list")
|
||||
}
|
||||
|
||||
@@ -1095,6 +1108,12 @@ class Compiler(
|
||||
val t2 = cc.next()
|
||||
if (t2.type == Token.Type.COLON) {
|
||||
val name = t1.value
|
||||
// Check for shorthand: name: (comma or rparen)
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.COMMA || next.type == Token.Type.RPAREN) {
|
||||
val localVar = LocalVarRef(name, t1.pos)
|
||||
return ParsedArgument(statement(t1.pos) { localVar.get(it).value }, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
val rhs = parseExpression() ?: t2.raiseSyntax("expected expression after named argument '${name}:'")
|
||||
return ParsedArgument(rhs, t1.pos, isSplat = false, name = name)
|
||||
}
|
||||
@@ -1110,7 +1129,7 @@ class Compiler(
|
||||
}
|
||||
|
||||
Token.Type.ELLIPSIS -> {
|
||||
parseStatement()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
parseExpression()?.let { args += ParsedArgument(it, t.pos, isSplat = true) }
|
||||
?: throw ScriptError(t.pos, "Expecting arguments list")
|
||||
}
|
||||
|
||||
@@ -1216,12 +1235,12 @@ class Compiler(
|
||||
return when (t.type) {
|
||||
Token.Type.INT, Token.Type.HEX -> {
|
||||
val n = t.value.replace("_", "").toLong(if (t.type == Token.Type.HEX) 16 else 10)
|
||||
if (isPlus) ObjInt(n) else ObjInt(-n)
|
||||
if (isPlus) ObjInt.of(n) else ObjInt.of(-n)
|
||||
}
|
||||
|
||||
Token.Type.REAL -> {
|
||||
val d = t.value.toDouble()
|
||||
if (isPlus) ObjReal(d) else ObjReal(-d)
|
||||
if (isPlus) ObjReal.of(d) else ObjReal.of(-d)
|
||||
}
|
||||
|
||||
else -> {
|
||||
@@ -1353,6 +1372,23 @@ class Compiler(
|
||||
parseClassDeclaration()
|
||||
}
|
||||
|
||||
"init" -> {
|
||||
if (codeContexts.lastOrNull() is CodeContext.ClassBody) {
|
||||
val block = parseBlock()
|
||||
lastParsedBlockRange?.let { range ->
|
||||
miniSink?.onInitDecl(MiniInitDecl(MiniRange(id.pos, range.end), id.pos))
|
||||
}
|
||||
val initStmt = statement(id.pos) { scp ->
|
||||
block.execute(scp)
|
||||
ObjVoid
|
||||
}
|
||||
statement {
|
||||
currentClassCtx?.instanceInitializers?.add(initStmt)
|
||||
ObjVoid
|
||||
}
|
||||
} else null
|
||||
}
|
||||
|
||||
"enum" -> {
|
||||
pendingDeclStart = id.pos
|
||||
pendingDeclDoc = consumePendingDoc()
|
||||
@@ -1754,7 +1790,7 @@ class Compiler(
|
||||
val constructorArgsDeclaration =
|
||||
if (cc.skipTokenOfType(Token.Type.LPAREN, isOptional = true))
|
||||
parseArgsDeclaration(isClassDeclaration = true)
|
||||
else null
|
||||
else ArgsDeclaration(emptyList(), Token.Type.RPAREN)
|
||||
|
||||
if (constructorArgsDeclaration != null && constructorArgsDeclaration.endTokenType != Token.Type.RPAREN)
|
||||
throw ScriptError(
|
||||
@@ -1791,7 +1827,9 @@ class Compiler(
|
||||
if (next.type == Token.Type.LBRACE) {
|
||||
// parse body
|
||||
val bodyStart = next.pos
|
||||
val st = parseScript()
|
||||
val st = withLocalNames(constructorArgsDeclaration?.params?.map { it.name }?.toSet() ?: emptySet()) {
|
||||
parseScript()
|
||||
}
|
||||
val rbTok = cc.next()
|
||||
if (rbTok.type != Token.Type.RBRACE) throw ScriptError(rbTok.pos, "unbalanced braces in class body")
|
||||
classBodyRange = MiniRange(bodyStart, rbTok.pos)
|
||||
@@ -1918,7 +1956,7 @@ class Compiler(
|
||||
while (cc.hasPrevious() && cnt < maxDepth) {
|
||||
val t = cc.previous()
|
||||
cnt++
|
||||
if (t.type == Token.Type.LABEL) {
|
||||
if (t.type == Token.Type.LABEL || t.type == Token.Type.ATLABEL) {
|
||||
found = t.value
|
||||
break
|
||||
}
|
||||
@@ -1949,7 +1987,8 @@ class Compiler(
|
||||
val namesForLoop = (currentLocalNames?.toSet() ?: emptySet()) + tVar.value
|
||||
val (canBreak, body, elseStatement) = withLocalNames(namesForLoop) {
|
||||
val loopParsed = cc.parseLoop {
|
||||
parseStatement() ?: throw ScriptError(start, "Bad for statement: expected loop body")
|
||||
if (cc.current().type == Token.Type.LBRACE) parseBlock()
|
||||
else parseStatement() ?: throw ScriptError(start, "Bad for statement: expected loop body")
|
||||
}
|
||||
// possible else clause
|
||||
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||
@@ -2001,7 +2040,7 @@ class Compiler(
|
||||
var breakCaught = false
|
||||
|
||||
if (size > 0) {
|
||||
var current = runCatching { sourceObj.getAt(forContext, ObjInt(0)) }
|
||||
var current = runCatching { sourceObj.getAt(forContext, ObjInt.of(0)) }
|
||||
.getOrElse {
|
||||
throw ScriptError(
|
||||
tOp.pos,
|
||||
@@ -2012,23 +2051,21 @@ class Compiler(
|
||||
var index = 0
|
||||
while (true) {
|
||||
loopSO.value = current
|
||||
if (canBreak) {
|
||||
try {
|
||||
result = body.execute(forContext)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
breakCaught = true
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
} else result = body.execute(forContext)
|
||||
try {
|
||||
result = body.execute(forContext)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
breakCaught = true
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
if (++index >= size) break
|
||||
current = sourceObj.getAt(forContext, ObjInt(index.toLong()))
|
||||
current = sourceObj.getAt(forContext, ObjInt.of(index.toLong()))
|
||||
}
|
||||
}
|
||||
if (!breakCaught && elseStatement != null) {
|
||||
@@ -2048,11 +2085,9 @@ class Compiler(
|
||||
body: Statement, elseStatement: Statement?, label: String?, catchBreak: Boolean
|
||||
): Obj {
|
||||
var result: Obj = ObjVoid
|
||||
val iVar = ObjInt(0)
|
||||
loopVar.value = iVar
|
||||
if (catchBreak) {
|
||||
for (i in start..<end) {
|
||||
iVar.value = i//.toLong()
|
||||
loopVar.value = ObjInt.of(i)
|
||||
try {
|
||||
result = body.execute(forScope)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
@@ -2065,7 +2100,7 @@ class Compiler(
|
||||
}
|
||||
} else {
|
||||
for (i in start..<end) {
|
||||
iVar.value = i
|
||||
loopVar.value = ObjInt.of(i)
|
||||
result = body.execute(forScope)
|
||||
}
|
||||
}
|
||||
@@ -2077,57 +2112,50 @@ class Compiler(
|
||||
body: Statement, elseStatement: Statement?, label: String?,
|
||||
catchBreak: Boolean
|
||||
): Obj {
|
||||
val iterObj = sourceObj.invokeInstanceMethod(forScope, "iterator")
|
||||
var result: Obj = ObjVoid
|
||||
var completedNaturally = false
|
||||
try {
|
||||
while (iterObj.invokeInstanceMethod(forScope, "hasNext").toBool()) {
|
||||
if (catchBreak)
|
||||
try {
|
||||
loopVar.value = iterObj.invokeInstanceMethod(forScope, "next")
|
||||
result = body.execute(forScope)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) continue
|
||||
// premature finish, will trigger cancel in finally
|
||||
return lbe.result
|
||||
}
|
||||
throw lbe
|
||||
}
|
||||
else {
|
||||
loopVar.value = iterObj.invokeInstanceMethod(forScope, "next")
|
||||
var breakCaught = false
|
||||
sourceObj.enumerate(forScope) { item ->
|
||||
loopVar.value = item
|
||||
if (catchBreak) {
|
||||
try {
|
||||
result = body.execute(forScope)
|
||||
true
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) true
|
||||
else {
|
||||
result = lbe.result
|
||||
breakCaught = true
|
||||
false
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
}
|
||||
completedNaturally = true
|
||||
return elseStatement?.execute(forScope) ?: result
|
||||
} finally {
|
||||
if (!completedNaturally) {
|
||||
// Best-effort cancellation on premature termination
|
||||
runCatching {
|
||||
iterObj.invokeInstanceMethod(forScope, "cancelIteration") { ObjVoid }
|
||||
}
|
||||
} else {
|
||||
result = body.execute(forScope)
|
||||
true
|
||||
}
|
||||
}
|
||||
return if (!breakCaught && elseStatement != null) {
|
||||
elseStatement.execute(forScope)
|
||||
} else result
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
private suspend fun parseDoWhileStatement(): Statement {
|
||||
val label = getLabel()?.also { cc.labels += it }
|
||||
val (breakFound, body) = cc.parseLoop {
|
||||
parseStatement() ?: throw ScriptError(cc.currentPos(), "Bad while statement: expected statement")
|
||||
val (canBreak, body) = cc.parseLoop {
|
||||
parseStatement() ?: throw ScriptError(cc.currentPos(), "Bad do-while statement: expected body statement")
|
||||
}
|
||||
label?.also { cc.labels -= it }
|
||||
|
||||
cc.skipTokens(Token.Type.NEWLINE)
|
||||
cc.skipWsTokens()
|
||||
val tWhile = cc.next()
|
||||
if (tWhile.type != Token.Type.ID || tWhile.value != "while")
|
||||
throw ScriptError(tWhile.pos, "Expected 'while' after do body")
|
||||
|
||||
val t = cc.next()
|
||||
if (t.type != Token.Type.ID && t.value != "while")
|
||||
cc.skipTokenOfType(Token.Type.LPAREN, "expected '(' here")
|
||||
|
||||
val conditionStart = ensureLparen()
|
||||
val condition =
|
||||
parseExpression() ?: throw ScriptError(conditionStart, "Bad while statement: expected expression")
|
||||
ensureLparen()
|
||||
val condition = parseExpression() ?: throw ScriptError(cc.currentPos(), "Expected condition after 'while'")
|
||||
ensureRparen()
|
||||
|
||||
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||
@@ -2138,13 +2166,11 @@ class Compiler(
|
||||
null
|
||||
}
|
||||
|
||||
|
||||
return statement(body.pos) {
|
||||
var wasBroken = false
|
||||
var result: Obj = ObjVoid
|
||||
lateinit var doScope: Scope
|
||||
while (true) {
|
||||
doScope = it.createChildScope().apply { skipScopeCreation = true }
|
||||
val doScope = it.createChildScope().apply { skipScopeCreation = true }
|
||||
try {
|
||||
result = body.execute(doScope)
|
||||
} catch (e: LoopBreakContinueException) {
|
||||
@@ -2156,11 +2182,12 @@ class Compiler(
|
||||
}
|
||||
// for continue: just fall through to condition check below
|
||||
} else {
|
||||
// Not our label, let outer loops handle it
|
||||
throw e
|
||||
}
|
||||
}
|
||||
if (!condition.execute(doScope).toBool()) break
|
||||
if (!condition.execute(doScope).toBool()) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!wasBroken) elseStatement?.let { s -> result = s.execute(it) }
|
||||
result
|
||||
@@ -2174,7 +2201,10 @@ class Compiler(
|
||||
parseExpression() ?: throw ScriptError(start, "Bad while statement: expected expression")
|
||||
ensureRparen()
|
||||
|
||||
val body = parseStatement() ?: throw ScriptError(start, "Bad while statement: expected statement")
|
||||
val (canBreak, body) = cc.parseLoop {
|
||||
if (cc.current().type == Token.Type.LBRACE) parseBlock()
|
||||
else parseStatement() ?: throw ScriptError(start, "Bad while statement: expected statement")
|
||||
}
|
||||
label?.also { cc.labels -= it }
|
||||
|
||||
cc.skipTokenOfType(Token.Type.NEWLINE, isOptional = true)
|
||||
@@ -2188,21 +2218,23 @@ class Compiler(
|
||||
var result: Obj = ObjVoid
|
||||
var wasBroken = false
|
||||
while (condition.execute(it).toBool()) {
|
||||
try {
|
||||
// we don't need to create new context here: if body is a block,
|
||||
// parse block will do it, otherwise single statement doesn't need it:
|
||||
result = body.execute(it)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
wasBroken = true
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
val loopScope = it.createChildScope()
|
||||
if (canBreak) {
|
||||
try {
|
||||
result = body.execute(loopScope)
|
||||
} catch (lbe: LoopBreakContinueException) {
|
||||
if (lbe.label == label || lbe.label == null) {
|
||||
if (lbe.doContinue) continue
|
||||
else {
|
||||
result = lbe.result
|
||||
wasBroken = true
|
||||
break
|
||||
}
|
||||
} else
|
||||
throw lbe
|
||||
}
|
||||
} else
|
||||
result = body.execute(loopScope)
|
||||
}
|
||||
if (!wasBroken) elseStatement?.let { s -> result = s.execute(it) }
|
||||
result
|
||||
@@ -2227,7 +2259,12 @@ class Compiler(
|
||||
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.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
|
||||
parseStatement()
|
||||
@@ -2329,6 +2366,7 @@ class Compiler(
|
||||
throw ScriptError(t.pos, "Expected identifier after 'fun'")
|
||||
else t.value
|
||||
var nameStartPos: Pos = t.pos
|
||||
var receiverMini: MiniTypeRef? = null
|
||||
|
||||
val annotation = lastAnnotation
|
||||
val parentContext = codeContexts.last()
|
||||
@@ -2337,6 +2375,12 @@ class Compiler(
|
||||
// Is extension?
|
||||
if (t.type == Token.Type.DOT) {
|
||||
extTypeName = name
|
||||
val receiverEnd = Pos(start.source, start.line, start.column + name.length)
|
||||
receiverMini = MiniTypeName(
|
||||
range = MiniRange(start, receiverEnd),
|
||||
segments = listOf(MiniTypeName.Segment(name, MiniRange(start, receiverEnd))),
|
||||
nullable = false
|
||||
)
|
||||
t = cc.next()
|
||||
if (t.type != Token.Type.ID)
|
||||
throw ScriptError(t.pos, "illegal extension format: expected function name")
|
||||
@@ -2380,7 +2424,8 @@ class Compiler(
|
||||
returnType = returnTypeMini,
|
||||
body = null,
|
||||
doc = declDocLocal,
|
||||
nameStart = nameStartPos
|
||||
nameStart = nameStartPos,
|
||||
receiver = receiverMini
|
||||
)
|
||||
miniSink?.onFunDecl(node)
|
||||
pendingDeclDoc = null
|
||||
@@ -2396,7 +2441,20 @@ class Compiler(
|
||||
val fnStatements = if (isExtern)
|
||||
statement { raiseError("extern function not provided: $name") }
|
||||
else
|
||||
withLocalNames(paramNames) { parseBlock() }
|
||||
withLocalNames(paramNames) {
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected function body expression")
|
||||
// Shorthand function returns the expression value
|
||||
statement(expr.pos) { scope ->
|
||||
expr.execute(scope)
|
||||
}
|
||||
} else {
|
||||
parseBlock()
|
||||
}
|
||||
}
|
||||
// Capture and pop the local declarations count for this function
|
||||
val fnLocalDecls = localDeclCountStack.removeLastOrNull() ?: 0
|
||||
|
||||
@@ -2436,7 +2494,7 @@ class Compiler(
|
||||
// class extension method
|
||||
val type = context[typeName]?.value ?: context.raiseSymbolNotFound("class $typeName not found")
|
||||
if (type !is ObjClass) context.raiseClassCastError("$typeName is not the class instance")
|
||||
type.addFn(name, isOpen = true, visibility = visibility) {
|
||||
val stmt = statement {
|
||||
// ObjInstance has a fixed instance scope, so we need to build a closure
|
||||
(thisObj as? ObjInstance)?.let { i ->
|
||||
annotatedFnBody.execute(ClosureScope(this, i.instanceScope))
|
||||
@@ -2444,6 +2502,7 @@ class Compiler(
|
||||
// other classes can create one-time scope for this rare case:
|
||||
?: annotatedFnBody.execute(thisObj.autoInstanceScope(this))
|
||||
}
|
||||
context.addExtension(type, name, ObjRecord(stmt, isMutable = false, visibility = visibility, declaringClass = null))
|
||||
}
|
||||
// regular function/method
|
||||
?: run {
|
||||
@@ -2532,45 +2591,139 @@ class Compiler(
|
||||
@Suppress("UNUSED_PARAMETER") isOpen: Boolean = false,
|
||||
isStatic: Boolean = false
|
||||
): Statement {
|
||||
val nameToken = cc.next()
|
||||
val start = nameToken.pos
|
||||
if (nameToken.type != Token.Type.ID)
|
||||
throw ScriptError(nameToken.pos, "Expected identifier here")
|
||||
val name = nameToken.value
|
||||
val nextToken = cc.next()
|
||||
val start = nextToken.pos
|
||||
|
||||
if (nextToken.type == Token.Type.LBRACKET) {
|
||||
// Destructuring
|
||||
if (isStatic) throw ScriptError(start, "static destructuring is not supported")
|
||||
|
||||
val entries = parseArrayLiteral()
|
||||
val pattern = ListLiteralRef(entries)
|
||||
|
||||
// Register all names in the pattern
|
||||
pattern.forEachVariableWithPos { name, namePos ->
|
||||
declareLocalName(name)
|
||||
val declRange = MiniRange(namePos, namePos)
|
||||
val node = MiniValDecl(
|
||||
range = declRange,
|
||||
name = name,
|
||||
mutable = isMutable,
|
||||
type = null,
|
||||
initRange = null,
|
||||
doc = pendingDeclDoc,
|
||||
nameStart = namePos
|
||||
)
|
||||
miniSink?.onValDecl(node)
|
||||
}
|
||||
pendingDeclDoc = null
|
||||
|
||||
val eqToken = cc.next()
|
||||
if (eqToken.type != Token.Type.ASSIGN)
|
||||
throw ScriptError(eqToken.pos, "destructuring declaration must be initialized")
|
||||
|
||||
val initialExpression = parseStatement(true)
|
||||
?: throw ScriptError(eqToken.pos, "Expected initializer expression")
|
||||
|
||||
val names = mutableListOf<String>()
|
||||
pattern.forEachVariable { names.add(it) }
|
||||
|
||||
return statement(start) { context ->
|
||||
val value = initialExpression.execute(context)
|
||||
for (name in names) {
|
||||
context.addItem(name, true, ObjVoid, visibility)
|
||||
}
|
||||
pattern.setAt(start, context, value)
|
||||
if (!isMutable) {
|
||||
for (name in names) {
|
||||
val rec = context.objects[name]!!
|
||||
val immutableRec = rec.copy(isMutable = false)
|
||||
context.objects[name] = immutableRec
|
||||
context.localBindings[name] = immutableRec
|
||||
context.updateSlotFor(name, immutableRec)
|
||||
}
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
|
||||
if (nextToken.type != Token.Type.ID)
|
||||
throw ScriptError(nextToken.pos, "Expected identifier or [ here")
|
||||
var name = nextToken.value
|
||||
var extTypeName: String? = null
|
||||
var nameStartPos: Pos = nextToken.pos
|
||||
var receiverMini: MiniTypeRef? = null
|
||||
|
||||
if (cc.peekNextNonWhitespace().type == Token.Type.DOT) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume dot
|
||||
extTypeName = name
|
||||
val receiverEnd = Pos(nextToken.pos.source, nextToken.pos.line, nextToken.pos.column + name.length)
|
||||
receiverMini = MiniTypeName(
|
||||
range = MiniRange(nextToken.pos, receiverEnd),
|
||||
segments = listOf(MiniTypeName.Segment(name, MiniRange(nextToken.pos, receiverEnd))),
|
||||
nullable = false
|
||||
)
|
||||
val nameToken = cc.next()
|
||||
if (nameToken.type != Token.Type.ID)
|
||||
throw ScriptError(nameToken.pos, "Expected identifier after dot in extension declaration")
|
||||
name = nameToken.value
|
||||
nameStartPos = nameToken.pos
|
||||
}
|
||||
|
||||
// Optional explicit type annotation
|
||||
val varTypeMini: MiniTypeRef? = if (cc.current().type == Token.Type.COLON) {
|
||||
parseTypeDeclarationWithMini().second
|
||||
} else null
|
||||
|
||||
val markBeforeEq = cc.savePos()
|
||||
cc.skipWsTokens()
|
||||
val eqToken = cc.next()
|
||||
var setNull = false
|
||||
var isProperty = false
|
||||
|
||||
val declaringClassNameCaptured = (codeContexts.lastOrNull() as? CodeContext.ClassBody)?.name
|
||||
|
||||
if (declaringClassNameCaptured != null || extTypeName != null) {
|
||||
val mark = cc.savePos()
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
val next = cc.peekNextNonWhitespace()
|
||||
if (next.isId("get") || next.isId("set")) {
|
||||
isProperty = true
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
} else {
|
||||
cc.restorePos(mark)
|
||||
}
|
||||
}
|
||||
|
||||
// Register the local name at compile time so that subsequent identifiers can be emitted as fast locals
|
||||
if (!isStatic) declareLocalName(name)
|
||||
|
||||
val isDelegate = if (eqToken.isId("by")) {
|
||||
val isDelegate = if (!isProperty && eqToken.isId("by")) {
|
||||
true
|
||||
} else {
|
||||
if (eqToken.type != Token.Type.ASSIGN) {
|
||||
if (!isMutable)
|
||||
if (!isProperty && eqToken.type != Token.Type.ASSIGN) {
|
||||
if (!isMutable && (declaringClassNameCaptured == null) && (extTypeName == null))
|
||||
throw ScriptError(start, "val must be initialized")
|
||||
else {
|
||||
cc.previous()
|
||||
cc.restorePos(markBeforeEq)
|
||||
cc.skipWsTokens()
|
||||
setNull = true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
val initialExpression = if (setNull) null
|
||||
val initialExpression = if (setNull || isProperty) null
|
||||
else parseStatement(true)
|
||||
?: throw ScriptError(eqToken.pos, "Expected initializer expression")
|
||||
|
||||
// Emit MiniValDecl for this declaration (before execution wiring), attach doc if any
|
||||
run {
|
||||
val declRange = MiniRange(pendingDeclStart ?: start, cc.currentPos())
|
||||
val initR = if (setNull) null else MiniRange(eqToken.pos, cc.currentPos())
|
||||
val initR = if (setNull || isProperty) null else MiniRange(eqToken.pos, cc.currentPos())
|
||||
val node = MiniValDecl(
|
||||
range = declRange,
|
||||
name = name,
|
||||
@@ -2578,7 +2731,8 @@ class Compiler(
|
||||
type = varTypeMini,
|
||||
initRange = initR,
|
||||
doc = pendingDeclDoc,
|
||||
nameStart = nameToken.pos
|
||||
nameStart = nameStartPos,
|
||||
receiver = receiverMini
|
||||
)
|
||||
miniSink?.onValDecl(node)
|
||||
pendingDeclDoc = null
|
||||
@@ -2599,17 +2753,95 @@ class Compiler(
|
||||
return NopStatement
|
||||
}
|
||||
|
||||
// Determine declaring class (if inside class body) at compile time, capture it in the closure
|
||||
val declaringClassNameCaptured = (codeContexts.lastOrNull() as? CodeContext.ClassBody)?.name
|
||||
// Check for accessors if it is a class member
|
||||
var getter: Statement? = null
|
||||
var setter: Statement? = null
|
||||
if (declaringClassNameCaptured != null || extTypeName != null) {
|
||||
while (true) {
|
||||
val t = cc.peekNextNonWhitespace()
|
||||
if (t.isId("get")) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume 'get'
|
||||
cc.requireToken(Token.Type.LPAREN)
|
||||
cc.requireToken(Token.Type.RPAREN)
|
||||
getter = if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
cc.skipWsTokens()
|
||||
parseBlock()
|
||||
} else if (cc.peekNextNonWhitespace().type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected getter expression")
|
||||
(expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after get()")
|
||||
}
|
||||
} else if (t.isId("set")) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume 'set'
|
||||
cc.requireToken(Token.Type.LPAREN)
|
||||
val setArg = cc.requireToken(Token.Type.ID, "Expected setter argument name")
|
||||
cc.requireToken(Token.Type.RPAREN)
|
||||
setter = if (cc.peekNextNonWhitespace().type == Token.Type.LBRACE) {
|
||||
cc.skipWsTokens()
|
||||
val body = parseBlock()
|
||||
statement(body.pos) { scope ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
body.execute(scope)
|
||||
}
|
||||
} else if (cc.peekNextNonWhitespace().type == Token.Type.ASSIGN) {
|
||||
cc.skipWsTokens()
|
||||
cc.next() // consume '='
|
||||
val expr = parseExpression() ?: throw ScriptError(cc.current().pos, "Expected setter expression")
|
||||
val st = (expr as? Statement) ?: statement(expr.pos) { s -> expr.execute(s) }
|
||||
statement(st.pos) { scope ->
|
||||
val value = scope.args.list.firstOrNull() ?: ObjNull
|
||||
scope.addItem(setArg.value, true, value, recordType = ObjRecord.Type.Argument)
|
||||
st.execute(scope)
|
||||
}
|
||||
} else {
|
||||
throw ScriptError(cc.current().pos, "Expected { or = after set(...)")
|
||||
}
|
||||
} else break
|
||||
}
|
||||
if (getter != null || setter != null) {
|
||||
if (isMutable) {
|
||||
if (getter == null || setter == null) {
|
||||
throw ScriptError(start, "var property must have both get() and set()")
|
||||
}
|
||||
} else {
|
||||
if (setter != null)
|
||||
throw ScriptError(start, "val property cannot have a setter (name: $name)")
|
||||
if (getter == null)
|
||||
throw ScriptError(start, "val property with accessors must have a getter (name: $name)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statement(nameToken.pos) { context ->
|
||||
return statement(start) { context ->
|
||||
if (extTypeName != null) {
|
||||
val prop = if (getter != null || setter != null) {
|
||||
ObjProperty(name, getter, setter)
|
||||
} else {
|
||||
// Simple val extension with initializer
|
||||
val initExpr = initialExpression ?: throw ScriptError(start, "Extension val must be initialized")
|
||||
ObjProperty(name, statement(initExpr.pos) { scp -> initExpr.execute(scp) }, null)
|
||||
}
|
||||
|
||||
val type = context[extTypeName]?.value ?: context.raiseSymbolNotFound("class $extTypeName not found")
|
||||
if (type !is ObjClass) context.raiseClassCastError("$extTypeName is not the class instance")
|
||||
|
||||
context.addExtension(type, name, ObjRecord(prop, isMutable = false, visibility = visibility, declaringClass = null, type = ObjRecord.Type.Property))
|
||||
|
||||
return@statement prop
|
||||
}
|
||||
// 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
|
||||
// ClassBody qualifies for class-field storage. Otherwise, this is a plain local.
|
||||
val declaringClassName = declaringClassNameCaptured
|
||||
if (declaringClassName == null) {
|
||||
if (context.containsLocal(name))
|
||||
throw ScriptError(nameToken.pos, "Variable $name is already defined")
|
||||
throw ScriptError(start, "Variable $name is already defined")
|
||||
}
|
||||
|
||||
// Register the local name so subsequent identifiers can be emitted as fast locals
|
||||
@@ -2617,23 +2849,32 @@ class Compiler(
|
||||
|
||||
if (isDelegate) {
|
||||
TODO()
|
||||
// println("initial expr = $initialExpression")
|
||||
// val initValue =
|
||||
// (initialExpression?.execute(context.copy(Arguments(ObjString(name)))) as? Statement)
|
||||
// ?.execute(context.copy(Arguments(ObjString(name))))
|
||||
// ?: context.raiseError("delegate initialization required")
|
||||
// println("delegate init: $initValue")
|
||||
// if (!initValue.isInstanceOf(ObjArray))
|
||||
// context.raiseIllegalArgument("delegate initialized must be an array")
|
||||
// val s = initValue.getAt(context, 1)
|
||||
// val setter = if (s == ObjNull) statement { raiseNotImplemented("setter is not provided") }
|
||||
// else (s as? Statement) ?: context.raiseClassCastError("setter must be a callable")
|
||||
// ObjDelegate(
|
||||
// (initValue.getAt(context, 0) as? Statement)
|
||||
// ?: context.raiseClassCastError("getter must be a callable"), setter
|
||||
// ).also {
|
||||
// context.addItem(name, isMutable, it, visibility, recordType = ObjRecord.Type.Field)
|
||||
// }
|
||||
} else if (getter != null || setter != null) {
|
||||
val declaringClassName = declaringClassNameCaptured!!
|
||||
val storageName = "$declaringClassName::$name"
|
||||
val prop = ObjProperty(name, getter, setter)
|
||||
|
||||
// If we are in class scope now (defining instance field), defer initialization to instance time
|
||||
val isClassScope = context.thisObj is ObjClass && (context.thisObj !is ObjInstance)
|
||||
if (isClassScope) {
|
||||
val cls = context.thisObj as ObjClass
|
||||
// Register the property
|
||||
cls.instanceInitializers += statement(start) { scp ->
|
||||
scp.addItem(
|
||||
storageName,
|
||||
isMutable,
|
||||
prop,
|
||||
visibility,
|
||||
recordType = ObjRecord.Type.Property
|
||||
)
|
||||
ObjVoid
|
||||
}
|
||||
ObjVoid
|
||||
} else {
|
||||
// We are in instance scope already: perform initialization immediately
|
||||
context.addItem(storageName, isMutable, prop, visibility, recordType = ObjRecord.Type.Property)
|
||||
prop
|
||||
}
|
||||
} else {
|
||||
if (declaringClassName != null && !isStatic) {
|
||||
val storageName = "$declaringClassName::$name"
|
||||
@@ -2642,7 +2883,7 @@ class Compiler(
|
||||
if (isClassScope) {
|
||||
val cls = context.thisObj as ObjClass
|
||||
// Defer: at instance construction, evaluate initializer in instance scope and store under mangled name
|
||||
val initStmt = statement(nameToken.pos) { scp ->
|
||||
val initStmt = statement(start) { scp ->
|
||||
val initValue = initialExpression?.execute(scp)?.byValueCopy() ?: ObjNull
|
||||
// Preserve mutability of declaration: do NOT use addOrUpdateItem here, as it creates mutable records
|
||||
scp.addItem(storageName, isMutable, initValue, visibility, recordType = ObjRecord.Type.Field)
|
||||
@@ -2762,22 +3003,22 @@ class Compiler(
|
||||
|
||||
// Arithmetic for ints only (keep semantics simple at compile time)
|
||||
BinOp.PLUS -> when {
|
||||
a is ObjInt && b is ObjInt -> ObjInt(a.value + b.value)
|
||||
a is ObjInt && b is ObjInt -> ObjInt.of(a.value + b.value)
|
||||
a is ObjString && b is ObjString -> ObjString(a.value + b.value)
|
||||
else -> null
|
||||
}
|
||||
|
||||
BinOp.MINUS -> if (a is ObjInt && b is ObjInt) ObjInt(a.value - b.value) else null
|
||||
BinOp.STAR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value * b.value) else null
|
||||
BinOp.SLASH -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt(a.value / b.value) else null
|
||||
BinOp.PERCENT -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt(a.value % b.value) else null
|
||||
BinOp.MINUS -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value - b.value) else null
|
||||
BinOp.STAR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value * b.value) else null
|
||||
BinOp.SLASH -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt.of(a.value / b.value) else null
|
||||
BinOp.PERCENT -> if (a is ObjInt && b is ObjInt && b.value != 0L) ObjInt.of(a.value % b.value) else null
|
||||
|
||||
// Bitwise for ints
|
||||
BinOp.BAND -> if (a is ObjInt && b is ObjInt) ObjInt(a.value and b.value) else null
|
||||
BinOp.BXOR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value xor b.value) else null
|
||||
BinOp.BOR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value or b.value) else null
|
||||
BinOp.SHL -> if (a is ObjInt && b is ObjInt) ObjInt(a.value shl (b.value.toInt() and 63)) else null
|
||||
BinOp.SHR -> if (a is ObjInt && b is ObjInt) ObjInt(a.value shr (b.value.toInt() and 63)) else null
|
||||
BinOp.BAND -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value and b.value) else null
|
||||
BinOp.BXOR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value xor b.value) else null
|
||||
BinOp.BOR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value or b.value) else null
|
||||
BinOp.SHL -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value shl (b.value.toInt() and 63)) else null
|
||||
BinOp.SHR -> if (a is ObjInt && b is ObjInt) ObjInt.of(a.value shr (b.value.toInt() and 63)) else null
|
||||
|
||||
// Non-folded / side-effecting or type-dependent ops
|
||||
BinOp.EQARROW, BinOp.REF_EQ, BinOp.REF_NEQ, BinOp.MATCH, BinOp.NOTMATCH,
|
||||
@@ -2790,12 +3031,12 @@ class Compiler(
|
||||
return when (op) {
|
||||
UnaryOp.NOT -> if (a is ObjBool) if (!a.value) ObjTrue else ObjFalse else null
|
||||
UnaryOp.NEGATE -> when (a) {
|
||||
is ObjInt -> ObjInt(-a.value)
|
||||
is ObjReal -> ObjReal(-a.value)
|
||||
is ObjInt -> ObjInt.of(-a.value)
|
||||
is ObjReal -> ObjReal.of(-a.value)
|
||||
else -> null
|
||||
}
|
||||
|
||||
UnaryOp.BITNOT -> if (a is ObjInt) ObjInt(a.value.inv()) else null
|
||||
UnaryOp.BITNOT -> if (a is ObjInt) ObjInt.of(a.value.inv()) else null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,11 +25,12 @@ class CompilerContext(val tokens: List<Token>) {
|
||||
var loopLevel = 0
|
||||
|
||||
inline fun <T> parseLoop(f: () -> T): Pair<Boolean, T> {
|
||||
if (++loopLevel == 0) breakFound = false
|
||||
val oldBreakFound = breakFound
|
||||
breakFound = false
|
||||
val result = f()
|
||||
return Pair(breakFound, result).also {
|
||||
--loopLevel
|
||||
}
|
||||
val currentBreakFound = breakFound
|
||||
breakFound = oldBreakFound || currentBreakFound
|
||||
return Pair(currentBreakFound, result)
|
||||
}
|
||||
|
||||
var currentIndex = 0
|
||||
@@ -108,7 +109,6 @@ class CompilerContext(val tokens: List<Token>) {
|
||||
val t = next()
|
||||
return if (t.type != tokenType) {
|
||||
if (!isOptional) {
|
||||
println("unexpected: $t (needed $tokenType)")
|
||||
throw ScriptError(t.pos, errorMessage)
|
||||
} else {
|
||||
previous()
|
||||
|
||||
@@ -45,19 +45,36 @@ class ModuleScope(
|
||||
if (record.visibility.isPublic) {
|
||||
val newName = symbols?.let { ss: Map<String, String> ->
|
||||
ss[symbol]
|
||||
?.also { symbolsToImport!!.remove(it) }
|
||||
?: scope.raiseError("internal error: symbol $symbol not found though the module is cached")
|
||||
} ?: symbol
|
||||
val existing = scope.objects[newName]
|
||||
if (existing != null ) {
|
||||
if (existing.importedFrom != record.importedFrom)
|
||||
scope.raiseError("symbol ${existing.importedFrom?.packageName}.$newName already exists, redefinition on import is not allowed")
|
||||
// already imported
|
||||
?.also { symbolsToImport!!.remove(symbol) }
|
||||
?: return@let null
|
||||
} ?: if (symbols == null) symbol else null
|
||||
|
||||
if (newName != null) {
|
||||
val existing = scope.objects[newName]
|
||||
if (existing != null) {
|
||||
if (existing.importedFrom != record.importedFrom)
|
||||
scope.raiseError("symbol ${existing.importedFrom?.packageName}.$newName already exists, redefinition on import is not allowed")
|
||||
// already imported
|
||||
} else {
|
||||
// when importing records, we keep track of its package (not otherwise needed)
|
||||
if (record.importedFrom == null) record.importedFrom = this
|
||||
scope.objects[newName] = record
|
||||
}
|
||||
}
|
||||
else {
|
||||
// when importing records, we keep track of its package (not otherwise needed)
|
||||
if (record.importedFrom == null) record.importedFrom = this
|
||||
scope.objects[newName] = record
|
||||
}
|
||||
}
|
||||
for ((cls, map) in this.extensions) {
|
||||
for ((symbol, record) in map) {
|
||||
if (record.visibility.isPublic) {
|
||||
val newName = symbols?.let { ss: Map<String, String> ->
|
||||
ss[symbol]
|
||||
?.also { symbolsToImport!!.remove(symbol) }
|
||||
?: return@let null
|
||||
} ?: if (symbols == null) symbol else null
|
||||
|
||||
if (newName != null) {
|
||||
scope.addExtension(cls, newName, record)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,30 @@ open class Scope(
|
||||
*/
|
||||
internal val localBindings: MutableMap<String, ObjRecord> = mutableMapOf()
|
||||
|
||||
internal val extensions: MutableMap<ObjClass, MutableMap<String, ObjRecord>> = mutableMapOf()
|
||||
|
||||
fun addExtension(cls: ObjClass, name: String, record: ObjRecord) {
|
||||
extensions.getOrPut(cls) { mutableMapOf() }[name] = record
|
||||
}
|
||||
|
||||
internal fun findExtension(receiverClass: ObjClass, name: String): ObjRecord? {
|
||||
var s: Scope? = this
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) break
|
||||
// Proximity rule: check all extensions in the current scope before going to parent.
|
||||
// Priority within scope: more specific class in MRO wins.
|
||||
for (cls in receiverClass.mro) {
|
||||
s.extensions[cls]?.get(name)?.let { return it }
|
||||
}
|
||||
if (s is ClosureScope) {
|
||||
s.closureScope.findExtension(receiverClass, name)?.let { return it }
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** Debug helper: ensure assigning [candidateParent] does not create a structural cycle. */
|
||||
private fun ensureNoCycle(candidateParent: Scope?) {
|
||||
if (candidateParent == null) return
|
||||
@@ -82,10 +106,17 @@ open class Scope(
|
||||
* intertwined closure frames. They traverse the plain parent chain and consult only locals
|
||||
* and bindings of each frame. Instance/class member fallback must be decided by the caller.
|
||||
*/
|
||||
private fun tryGetLocalRecord(s: Scope, name: String): ObjRecord? {
|
||||
s.objects[name]?.let { return it }
|
||||
s.localBindings[name]?.let { return it }
|
||||
s.getSlotIndexOf(name)?.let { return s.getSlotRecord(it) }
|
||||
private fun tryGetLocalRecord(s: Scope, name: String, caller: net.sergeych.lyng.obj.ObjClass?): ObjRecord? {
|
||||
s.objects[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
s.localBindings[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
s.getSlotIndexOf(name)?.let { idx ->
|
||||
val rec = s.getSlotRecord(idx)
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -95,7 +126,7 @@ open class Scope(
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) return null
|
||||
tryGetLocalRecord(s, name)?.let { return it }
|
||||
tryGetLocalRecord(s, name, currentClassCtx)?.let { return it }
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
@@ -110,17 +141,22 @@ open class Scope(
|
||||
*/
|
||||
internal fun baseGetIgnoreClosure(name: String): ObjRecord? {
|
||||
// 1) locals/bindings in this frame
|
||||
tryGetLocalRecord(this, name)?.let { return it }
|
||||
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
|
||||
// 2) walk parents for plain locals/bindings only
|
||||
var s = parent
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) return null
|
||||
tryGetLocalRecord(s, name)?.let { return it }
|
||||
tryGetLocalRecord(s, name, currentClassCtx)?.let { return it }
|
||||
s = s.parent
|
||||
}
|
||||
// 3) fallback to instance/class members of this frame's thisObj
|
||||
return thisObj.objClass.getInstanceMemberOrNull(name)
|
||||
for (cls in thisObj.objClass.mro) {
|
||||
this.extensions[cls]?.get(name)?.let { return it }
|
||||
}
|
||||
return thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,13 +166,18 @@ open class Scope(
|
||||
* This completely avoids invoking overridden `get` implementations, preventing
|
||||
* ping-pong recursion between `ClosureScope` frames.
|
||||
*/
|
||||
internal fun chainLookupWithMembers(name: String): ObjRecord? {
|
||||
internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx): ObjRecord? {
|
||||
var s: Scope? = this
|
||||
val visited = HashSet<Long>(4)
|
||||
while (s != null) {
|
||||
if (!visited.add(s.frameId)) return null
|
||||
tryGetLocalRecord(s, name)?.let { return it }
|
||||
s.thisObj.objClass.getInstanceMemberOrNull(name)?.let { return it }
|
||||
tryGetLocalRecord(s, name, caller)?.let { return it }
|
||||
for (cls in s.thisObj.objClass.mro) {
|
||||
s.extensions[cls]?.get(name)?.let { return it }
|
||||
}
|
||||
s.thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, caller)) return rec
|
||||
}
|
||||
s = s.parent
|
||||
}
|
||||
return null
|
||||
@@ -152,6 +193,10 @@ open class Scope(
|
||||
// copy locals and bindings
|
||||
snap.objects.putAll(this.objects)
|
||||
snap.localBindings.putAll(this.localBindings)
|
||||
// copy extensions
|
||||
for ((cls, map) in extensions) {
|
||||
snap.extensions[cls] = map.toMutableMap()
|
||||
}
|
||||
// copy slots map preserving indices
|
||||
if (this.slotCount() > 0) {
|
||||
var i = 0
|
||||
@@ -273,13 +318,19 @@ open class Scope(
|
||||
if (name == "this") thisObj.asReadonly
|
||||
else {
|
||||
// Prefer direct locals/bindings declared in this frame
|
||||
(objects[name]
|
||||
(objects[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
// Then, check known local bindings in this frame (helps after suspension)
|
||||
?: localBindings[name]
|
||||
?: localBindings[name]?.let { rec ->
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
// Walk up ancestry
|
||||
?: parent?.get(name)
|
||||
// Finally, fallback to class members on thisObj
|
||||
?: thisObj.objClass.getInstanceMemberOrNull(name)
|
||||
?: thisObj.objClass.getInstanceMemberOrNull(name)?.let { rec ->
|
||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx)) rec else null
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -297,6 +348,10 @@ open class Scope(
|
||||
return idx
|
||||
}
|
||||
|
||||
fun updateSlotFor(name: String, record: ObjRecord) {
|
||||
nameToSlot[name]?.let { slots[it] = record }
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset this scope instance so it can be safely reused as a fresh child frame.
|
||||
* Clears locals and slots, assigns new frameId, and sets parent/args/pos/thisObj.
|
||||
@@ -313,6 +368,7 @@ open class Scope(
|
||||
slots.clear()
|
||||
nameToSlot.clear()
|
||||
localBindings.clear()
|
||||
extensions.clear()
|
||||
// Now safe to validate and re-parent
|
||||
ensureNoCycle(parent)
|
||||
this.parent = parent
|
||||
@@ -396,9 +452,10 @@ open class Scope(
|
||||
isMutable: Boolean,
|
||||
value: Obj,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
recordType: ObjRecord.Type = ObjRecord.Type.Other
|
||||
recordType: ObjRecord.Type = ObjRecord.Type.Other,
|
||||
declaringClass: net.sergeych.lyng.obj.ObjClass? = currentClassCtx
|
||||
): ObjRecord {
|
||||
val rec = ObjRecord(value, isMutable, visibility, declaringClass = currentClassCtx, type = recordType)
|
||||
val rec = ObjRecord(value, isMutable, visibility, declaringClass = declaringClass, type = recordType)
|
||||
objects[name] = rec
|
||||
// Index this binding within the current frame to help resolve locals across suspension
|
||||
localBindings[name] = rec
|
||||
@@ -415,9 +472,12 @@ open class Scope(
|
||||
callScope.allocateSlotFor(name, rec)
|
||||
}
|
||||
}
|
||||
// Map to a slot for fast local access (if not already mapped)
|
||||
if (getSlotIndexOf(name) == null) {
|
||||
// Map to a slot for fast local access (ensure consistency)
|
||||
val idx = getSlotIndexOf(name)
|
||||
if (idx == null) {
|
||||
allocateSlotFor(name, rec)
|
||||
} else {
|
||||
slots[idx] = rec
|
||||
}
|
||||
return rec
|
||||
}
|
||||
@@ -428,13 +488,13 @@ open class Scope(
|
||||
}
|
||||
|
||||
inline fun addVoidFn(vararg names: String, crossinline fn: suspend Scope.() -> Unit) {
|
||||
addFn<ObjVoid>(*names) {
|
||||
addFn(*names) {
|
||||
fn(this)
|
||||
ObjVoid
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Obj> addFn(vararg names: String, crossinline fn: suspend Scope.() -> T) {
|
||||
fun addFn(vararg names: String, fn: suspend Scope.() -> Obj) {
|
||||
val newFn = object : Statement() {
|
||||
override val pos: Pos = Pos.builtIn
|
||||
|
||||
@@ -516,6 +576,23 @@ open class Scope(
|
||||
|
||||
open fun applyClosure(closure: Scope): Scope = ClosureScope(this, closure)
|
||||
|
||||
/**
|
||||
* Resolve and evaluate a qualified identifier exactly as compiled code would.
|
||||
* For input like `A.B.C`, it builds the same ObjRef chain the compiler emits:
|
||||
* `LocalVarRef("A", Pos.builtIn)` followed by `FieldRef` for each segment, then evaluates it.
|
||||
* This mirrors `eval("A.B.C")` resolution semantics without invoking the compiler.
|
||||
*/
|
||||
suspend fun resolveQualifiedIdentifier(qualifiedName: String): Obj {
|
||||
val trimmed = qualifiedName.trim()
|
||||
if (trimmed.isEmpty()) raiseSymbolNotFound("empty identifier")
|
||||
val parts = trimmed.split('.')
|
||||
var ref: ObjRef = LocalVarRef(parts[0], Pos.builtIn)
|
||||
for (i in 1 until parts.size) {
|
||||
ref = FieldRef(ref, parts[i], false)
|
||||
}
|
||||
return ref.evalValue(this)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun new(): Scope =
|
||||
|
||||
@@ -155,71 +155,116 @@ class Script(
|
||||
sqrt(args.firstAndOnly().toDouble())
|
||||
)
|
||||
}
|
||||
addFn( "abs" ) {
|
||||
addFn("abs") {
|
||||
val x = args.firstAndOnly()
|
||||
if( x is ObjInt) ObjInt( x.value.absoluteValue ) else ObjReal( x.toDouble().absoluteValue )
|
||||
if (x is ObjInt) ObjInt(x.value.absoluteValue) else ObjReal(x.toDouble().absoluteValue)
|
||||
}
|
||||
|
||||
addVoidFn("assert") {
|
||||
val cond = requiredArg<ObjBool>(0)
|
||||
val message = if( args.size > 1 )
|
||||
val message = if (args.size > 1)
|
||||
": " + (args[1] as Statement).execute(this).toString(this).value
|
||||
else ""
|
||||
if( !cond.value == true )
|
||||
if (!cond.value == true)
|
||||
raiseError(ObjAssertionFailedException(this, "Assertion failed$message"))
|
||||
}
|
||||
|
||||
addVoidFn("assertEquals") {
|
||||
val a = requiredArg<Obj>(0)
|
||||
val b = requiredArg<Obj>(1)
|
||||
if( a.compareTo(this, b) != 0 )
|
||||
raiseError(ObjAssertionFailedException(this,"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"))
|
||||
if (a.compareTo(this, b) != 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
this,
|
||||
"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"
|
||||
)
|
||||
)
|
||||
}
|
||||
// alias used in tests
|
||||
addVoidFn("assertEqual") {
|
||||
val a = requiredArg<Obj>(0)
|
||||
val b = requiredArg<Obj>(1)
|
||||
if( a.compareTo(this, b) != 0 )
|
||||
raiseError(ObjAssertionFailedException(this,"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"))
|
||||
if (a.compareTo(this, b) != 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
this,
|
||||
"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"
|
||||
)
|
||||
)
|
||||
}
|
||||
addVoidFn("assertNotEquals") {
|
||||
val a = requiredArg<Obj>(0)
|
||||
val b = requiredArg<Obj>(1)
|
||||
if( a.compareTo(this, b) == 0 )
|
||||
raiseError(ObjAssertionFailedException(this,"Assertion failed: ${a.inspect(this)} != ${b.inspect(this)}"))
|
||||
if (a.compareTo(this, b) == 0)
|
||||
raiseError(
|
||||
ObjAssertionFailedException(
|
||||
this,
|
||||
"Assertion failed: ${a.inspect(this)} != ${b.inspect(this)}"
|
||||
)
|
||||
)
|
||||
}
|
||||
addFn("assertThrows") {
|
||||
val code = requireOnlyArg<Statement>()
|
||||
val result =try {
|
||||
val code: Statement
|
||||
val expectedClass: ObjClass?
|
||||
when(args.size) {
|
||||
1 -> {
|
||||
code = requiredArg<Statement>(0)
|
||||
expectedClass = null
|
||||
}
|
||||
2 -> {
|
||||
code = requiredArg<Statement>(1)
|
||||
expectedClass = requiredArg<ObjClass>(0)
|
||||
}
|
||||
else -> raiseIllegalArgument("Expected 1 or 2 arguments, got ${args.size}")
|
||||
}
|
||||
val result = try {
|
||||
code.execute(this)
|
||||
null
|
||||
}
|
||||
catch( e: ExecutionError ) {
|
||||
} catch (e: ExecutionError) {
|
||||
e.errorObject
|
||||
}
|
||||
catch (_: ScriptError) {
|
||||
} catch (_: ScriptError) {
|
||||
ObjNull
|
||||
}
|
||||
result ?: raiseError(ObjAssertionFailedException(this,"Expected exception but nothing was thrown"))
|
||||
if( result == null ) raiseError(ObjAssertionFailedException(this, "Expected exception but nothing was thrown"))
|
||||
expectedClass?.let {
|
||||
if( result !is ObjException)
|
||||
raiseError("Expected $expectedClass, got $result")
|
||||
if (result.exceptionClass != expectedClass) {
|
||||
raiseError("Expected $expectedClass, got ${result.exceptionClass}")
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
addFn("dynamic") {
|
||||
ObjDynamic.create(this, requireOnlyArg())
|
||||
}
|
||||
|
||||
val root = this
|
||||
val mathClass = ObjClass("Math").apply {
|
||||
addFn("sqrt") {
|
||||
ObjReal(sqrt(args.firstAndOnly().toDouble()))
|
||||
}
|
||||
}
|
||||
addItem("Math", false, ObjInstance(mathClass).apply {
|
||||
instanceScope = Scope(root, thisObj = this)
|
||||
})
|
||||
|
||||
addFn("require") {
|
||||
val condition = requiredArg<ObjBool>(0)
|
||||
if( !condition.value ) {
|
||||
val message = args.list.getOrNull(1)?.toString() ?: "requirement not met"
|
||||
raiseIllegalArgument(message)
|
||||
if (!condition.value) {
|
||||
var message = args.list.getOrNull(1)
|
||||
if( message is Statement ) message = message.execute(this)
|
||||
raiseIllegalArgument(message?.toString() ?: "requirement not met")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
addFn("check") {
|
||||
val condition = requiredArg<ObjBool>(0)
|
||||
if( !condition.value ) {
|
||||
val message = args.list.getOrNull(1)?.toString() ?: "check failed"
|
||||
raiseIllegalState(message)
|
||||
if (!condition.value) {
|
||||
var message = args.list.getOrNull(1)
|
||||
if( message is Statement ) message = message.execute(this)
|
||||
raiseIllegalState(message?.toString() ?: "check failed")
|
||||
}
|
||||
ObjVoid
|
||||
}
|
||||
@@ -340,7 +385,7 @@ class Script(
|
||||
doc = "Suspend for the given time. Accepts Duration, Int seconds, or Real seconds."
|
||||
) {
|
||||
val a = args.firstAndOnly()
|
||||
when(a) {
|
||||
when (a) {
|
||||
is ObjInt -> delay(a.value * 1000)
|
||||
is ObjReal -> delay((a.value * 1000).roundToLong())
|
||||
is ObjDuration -> delay(a.duration)
|
||||
|
||||
@@ -26,7 +26,7 @@ enum class Visibility {
|
||||
|
||||
/** MI-aware visibility check: whether [caller] can access a member declared in [decl] with [visibility]. */
|
||||
fun canAccessMember(visibility: Visibility, decl: net.sergeych.lyng.obj.ObjClass?, caller: net.sergeych.lyng.obj.ObjClass?): Boolean {
|
||||
return when (visibility) {
|
||||
val res = when (visibility) {
|
||||
Visibility.Public -> true
|
||||
Visibility.Private -> (decl != null && caller === decl)
|
||||
Visibility.Protected -> when {
|
||||
@@ -36,4 +36,5 @@ fun canAccessMember(visibility: Visibility, decl: net.sergeych.lyng.obj.ObjClass
|
||||
else -> (caller.allParentsSet.contains(decl))
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -105,20 +105,24 @@ object BuiltinDocRegistry : BuiltinDocSource {
|
||||
}
|
||||
|
||||
/**
|
||||
* List names of extension-like methods defined for [className] in the stdlib text (`root.lyng`).
|
||||
* We do a lightweight regex scan like: `fun ClassName.methodName(` and collect distinct names.
|
||||
* List names of extension members defined for [className] in the stdlib text (`root.lyng`).
|
||||
* We do a lightweight regex scan like: `fun ClassName.methodName(` or `val ClassName.propName`
|
||||
* and collect distinct names.
|
||||
*/
|
||||
fun extensionMethodNamesFor(className: String): List<String> {
|
||||
fun extensionMemberNamesFor(className: String): List<String> {
|
||||
val src = try { rootLyng } catch (_: Throwable) { null } ?: return emptyList()
|
||||
val out = LinkedHashSet<String>()
|
||||
// Match lines like: fun String.trim(...)
|
||||
val re = Regex("(?m)^\\s*fun\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\s*\\(")
|
||||
// Match lines like: fun String.trim(...) or val Int.isEven = ...
|
||||
val re = Regex("(?m)^\\s*(?:fun|val|var)\\s+${className}\\.([A-Za-z_][A-Za-z0-9_]*)\\b")
|
||||
re.findAll(src).forEach { m ->
|
||||
val name = m.groupValues.getOrNull(1)?.trim()
|
||||
if (!name.isNullOrEmpty()) out.add(name)
|
||||
}
|
||||
return out.toList()
|
||||
}
|
||||
|
||||
@Deprecated("Use extensionMemberNamesFor", ReplaceWith("extensionMemberNamesFor(className)"))
|
||||
fun extensionMethodNamesFor(className: String): List<String> = extensionMemberNamesFor(className)
|
||||
}
|
||||
|
||||
// ---------------- Builders ----------------
|
||||
@@ -367,8 +371,8 @@ private object StdlibInlineDocIndex {
|
||||
else -> {
|
||||
// Non-comment, non-blank: try to match a declaration just after comments
|
||||
if (buf.isNotEmpty()) {
|
||||
// fun Class.name( ... )
|
||||
val mExt = Regex("^fun\\s+([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z_][A-Za-z0-9_]*)\\s*\\(").find(line)
|
||||
// fun/val/var Class.name( ... )
|
||||
val mExt = Regex("^(?:fun|val|var)\\s+([A-Za-z_][A-Za-z0-9_]*)\\.([A-Za-z_][A-Za-z0-9_]*)\\b").find(line)
|
||||
if (mExt != null) {
|
||||
val (cls, name) = mExt.destructured
|
||||
flushTo(Key.Method(cls, name))
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
/*
|
||||
* Copyright 2025 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Pure-Kotlin, PSI-free completion engine used for isolated tests and non-IDE harnesses.
|
||||
* Mirrors the IntelliJ MVP logic: MiniAst + BuiltinDocRegistry + lenient imports.
|
||||
@@ -131,7 +148,7 @@ object CompletionEngineLight {
|
||||
}
|
||||
is MiniClassDecl -> add(CompletionItem(d.name, Kind.Class_))
|
||||
is MiniValDecl -> add(CompletionItem(d.name, Kind.Value, typeText = typeOf(d.type)))
|
||||
else -> add(CompletionItem(d.name, Kind.Value))
|
||||
// else -> add(CompletionItem(d.name, Kind.Value))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,6 +200,7 @@ object CompletionEngineLight {
|
||||
val ci = CompletionItem(name, Kind.Field, typeText = typeOf((chosen as MiniMemberValDecl).type))
|
||||
if (ci.name.startsWith(prefix, true)) out += ci
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,10 +208,10 @@ object CompletionEngineLight {
|
||||
emitGroup(directMap)
|
||||
emitGroup(inheritedMap)
|
||||
|
||||
// Supplement with stdlib extension-like methods defined in root.lyng (e.g., fun String.re(...))
|
||||
// Supplement with stdlib extension members defined in root.lyng (e.g., fun String.re(...))
|
||||
run {
|
||||
val already = (directMap.keys + inheritedMap.keys).toMutableSet()
|
||||
val ext = BuiltinDocRegistry.extensionMethodNamesFor(className)
|
||||
val ext = BuiltinDocRegistry.extensionMemberNamesFor(className)
|
||||
for (name in ext) {
|
||||
if (already.contains(name)) continue
|
||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, className, name)
|
||||
@@ -210,6 +228,7 @@ object CompletionEngineLight {
|
||||
if (ci.name.startsWith(prefix, true)) out += ci
|
||||
already.add(name)
|
||||
}
|
||||
is MiniInitDecl -> {}
|
||||
}
|
||||
} else {
|
||||
// Fallback: emit simple method name without detailed types
|
||||
@@ -268,6 +287,24 @@ object CompletionEngineLight {
|
||||
break
|
||||
}
|
||||
if (hasDigits) return if (hasDot || hasExp) "Real" else "Int"
|
||||
|
||||
// 3) this@Type or as Type
|
||||
val identRange = wordRangeAt(text, i + 1)
|
||||
if (identRange != null) {
|
||||
val ident = text.substring(identRange.first, identRange.second)
|
||||
// if it's "as Type", we want Type
|
||||
var k = prevNonWs(text, identRange.first - 1)
|
||||
if (k >= 1 && text[k] == 's' && text[k - 1] == 'a' && (k - 1 == 0 || !text[k - 2].isLetterOrDigit())) {
|
||||
return ident
|
||||
}
|
||||
// if it's "this@Type", we want Type
|
||||
if (k >= 0 && text[k] == '@') {
|
||||
val k2 = prevNonWs(text, k - 1)
|
||||
if (k2 >= 3 && text.substring(k2 - 3, k2 + 1) == "this") {
|
||||
return ident
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -302,6 +339,7 @@ object CompletionEngineLight {
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
@@ -362,6 +400,7 @@ object CompletionEngineLight {
|
||||
val ret = when (member) {
|
||||
is MiniMemberFunDecl -> member.returnType
|
||||
is MiniMemberValDecl -> member.type
|
||||
is MiniInitDecl -> null
|
||||
}
|
||||
return simpleClassNameOf(ret)
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ fun ObjClass.addFnDoc(
|
||||
code: suspend Scope.() -> Obj
|
||||
) {
|
||||
// Register runtime method
|
||||
addFn(name, isOpen, visibility, code)
|
||||
addFn(name, isOpen, visibility, code = code)
|
||||
// Register docs for the member under this class
|
||||
BuiltinDocRegistry.module(moduleName ?: ownerModuleNameFromClassOrUnknown()) {
|
||||
classDoc(this@addFnDoc.className, doc = "") {
|
||||
|
||||
@@ -109,7 +109,8 @@ data class MiniFunDecl(
|
||||
val returnType: MiniTypeRef?,
|
||||
val body: MiniBlock?,
|
||||
override val doc: MiniDoc?,
|
||||
override val nameStart: Pos
|
||||
override val nameStart: Pos,
|
||||
val receiver: MiniTypeRef? = null
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniValDecl(
|
||||
@@ -119,7 +120,8 @@ data class MiniValDecl(
|
||||
val type: MiniTypeRef?,
|
||||
val initRange: MiniRange?,
|
||||
override val doc: MiniDoc?,
|
||||
override val nameStart: Pos
|
||||
override val nameStart: Pos,
|
||||
val receiver: MiniTypeRef? = null
|
||||
) : MiniDecl
|
||||
|
||||
data class MiniClassDecl(
|
||||
@@ -183,6 +185,15 @@ data class MiniMemberValDecl(
|
||||
override val isStatic: Boolean = false,
|
||||
) : MiniMemberDecl
|
||||
|
||||
data class MiniInitDecl(
|
||||
override val range: MiniRange,
|
||||
override val nameStart: Pos,
|
||||
) : MiniMemberDecl {
|
||||
override val name: String get() = "init"
|
||||
override val doc: MiniDoc? get() = null
|
||||
override val isStatic: Boolean get() = false
|
||||
}
|
||||
|
||||
// Streaming sink to collect mini-AST during parsing. Implementations may assemble a tree or process events.
|
||||
interface MiniAstSink {
|
||||
fun onScriptStart(start: Pos) {}
|
||||
@@ -193,6 +204,7 @@ interface MiniAstSink {
|
||||
fun onImport(node: MiniImport) {}
|
||||
fun onFunDecl(node: MiniFunDecl) {}
|
||||
fun onValDecl(node: MiniValDecl) {}
|
||||
fun onInitDecl(node: MiniInitDecl) {}
|
||||
fun onClassDecl(node: MiniClassDecl) {}
|
||||
|
||||
fun onBlock(node: MiniBlock) {}
|
||||
|
||||
@@ -83,23 +83,52 @@ open class Obj {
|
||||
scope: Scope,
|
||||
name: String,
|
||||
args: Arguments = Arguments.EMPTY,
|
||||
onNotFoundResult: (() -> Obj?)? = null
|
||||
onNotFoundResult: (suspend () -> Obj?)? = null
|
||||
): Obj {
|
||||
val rec = objClass.getInstanceMemberOrNull(name)
|
||||
if (rec != null) {
|
||||
val decl = rec.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
// Propagate declaring class as current class context during method execution
|
||||
val saved = scope.currentClassCtx
|
||||
scope.currentClassCtx = decl
|
||||
try {
|
||||
return rec.value.invoke(scope, this, args)
|
||||
} finally {
|
||||
scope.currentClassCtx = saved
|
||||
// 1. Hierarchy members (excluding root fallback)
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
||||
if (rec != null) {
|
||||
val decl = rec.declaringClass ?: cls
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
|
||||
val saved = scope.currentClassCtx
|
||||
scope.currentClassCtx = decl
|
||||
try {
|
||||
return rec.value.invoke(scope, this, args)
|
||||
} finally {
|
||||
scope.currentClassCtx = saved
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Extensions in scope
|
||||
val extension = scope.findExtension(objClass, name)
|
||||
if (extension != null) {
|
||||
return extension.value.invoke(scope, this, args)
|
||||
}
|
||||
|
||||
// 3. Root object fallback
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") {
|
||||
cls.members[name]?.let { rec ->
|
||||
val decl = rec.declaringClass ?: cls
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't invoke ${name}: not visible (declared in ${decl.className}, caller ${caller?.className ?: "?"})"))
|
||||
val saved = scope.currentClassCtx
|
||||
scope.currentClassCtx = decl
|
||||
try {
|
||||
return rec.value.invoke(scope, this, args)
|
||||
} finally {
|
||||
scope.currentClassCtx = saved
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return onNotFoundResult?.invoke()
|
||||
?: scope.raiseError(
|
||||
"no such member: $name on ${objClass.className}. Considered order: ${objClass.renderLinearization(true)}. " +
|
||||
@@ -127,6 +156,40 @@ open class Obj {
|
||||
return invokeInstanceMethod(scope, "contains", other).toBool()
|
||||
}
|
||||
|
||||
/**
|
||||
* Call [callback] for each element of this obj considering it provides [Iterator]
|
||||
* methods `hasNext` and `next`.
|
||||
*
|
||||
* IF callback returns false, iteration is stopped.
|
||||
*/
|
||||
open suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
val iterator = invokeInstanceMethod(scope, "iterator")
|
||||
val hasNext = iterator.getInstanceMethod(scope, "hasNext")
|
||||
val next = iterator.getInstanceMethod(scope, "next")
|
||||
var closeIt = false
|
||||
try {
|
||||
while (hasNext.invoke(scope, iterator).toBool()) {
|
||||
val nextValue = next.invoke(scope, iterator)
|
||||
val shouldContinue = try {
|
||||
callback(nextValue)
|
||||
} catch (e: Exception) {
|
||||
// iteration aborted due to exception in callback
|
||||
closeIt = true
|
||||
throw e
|
||||
}
|
||||
if (!shouldContinue) {
|
||||
closeIt = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (closeIt) {
|
||||
// Best-effort cancel on premature termination
|
||||
iterator.invokeInstanceMethod(scope, "cancelIteration") { ObjVoid }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default toString implementation:
|
||||
*
|
||||
@@ -142,15 +205,19 @@ open class Obj {
|
||||
* @param calledFromLyng true if called from Lyng's `toString`. Normally this parameter should be ignored,
|
||||
* but it is used to avoid endless recursion in [Obj.toString] base implementation
|
||||
*/
|
||||
open suspend fun toString(scope: Scope,calledFromLyng: Boolean=false): ObjString {
|
||||
return if (this is ObjString) this
|
||||
else if( !calledFromLyng ) {
|
||||
invokeInstanceMethod(scope, "toString") {
|
||||
ObjString(this.toString())
|
||||
open suspend fun toString(scope: Scope, calledFromLyng: Boolean = false): ObjString {
|
||||
if (this is ObjString) return this
|
||||
return if (!calledFromLyng) {
|
||||
invokeInstanceMethod(scope, "toString", Arguments.EMPTY) {
|
||||
defaultToString(scope)
|
||||
} as ObjString
|
||||
} else { ObjString(this.toString()) }
|
||||
} else {
|
||||
defaultToString(scope)
|
||||
}
|
||||
}
|
||||
|
||||
open suspend fun defaultToString(scope: Scope): ObjString = ObjString(this.toString())
|
||||
|
||||
/**
|
||||
* Class of the object: definition of member functions (top-level), etc.
|
||||
* Note that using lazy allows to avoid endless recursion here
|
||||
@@ -228,7 +295,7 @@ open class Obj {
|
||||
|
||||
open suspend fun assign(scope: Scope, other: Obj): Obj? = null
|
||||
|
||||
open fun getValue(scope: Scope) = this
|
||||
open suspend fun getValue(scope: Scope) = this
|
||||
|
||||
/**
|
||||
* a += b
|
||||
@@ -275,34 +342,83 @@ open class Obj {
|
||||
// suspend fun <T> sync(block: () -> T): T = monitor.withLock { block() }
|
||||
|
||||
open suspend fun readField(scope: Scope, name: String): ObjRecord {
|
||||
// could be property or class field:
|
||||
val obj = objClass.getInstanceMemberOrNull(name) ?: scope.raiseError(
|
||||
// 1. Hierarchy members (excluding root fallback)
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
cls.members[name]?.let { return resolveRecord(scope, it, name, it.declaringClass) }
|
||||
cls.classScope?.objects?.get(name)?.let { return resolveRecord(scope, it, name, it.declaringClass) }
|
||||
}
|
||||
|
||||
// 2. Extensions
|
||||
val extension = scope.findExtension(objClass, name)
|
||||
if (extension != null) {
|
||||
return resolveRecord(scope, extension, name, extension.declaringClass)
|
||||
}
|
||||
|
||||
// 3. Root fallback
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") {
|
||||
cls.members[name]?.let {
|
||||
val decl = it.declaringClass ?: cls
|
||||
return resolveRecord(scope, it, name, decl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scope.raiseError(
|
||||
"no such field: $name on ${objClass.className}. Considered order: ${objClass.renderLinearization(true)}"
|
||||
)
|
||||
val decl = obj.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
}
|
||||
|
||||
protected suspend fun resolveRecord(scope: Scope, obj: ObjRecord, name: String, decl: ObjClass?): ObjRecord {
|
||||
val value = obj.value
|
||||
if (value is ObjProperty) {
|
||||
return ObjRecord(value.callGetter(scope, this, decl), obj.isMutable)
|
||||
}
|
||||
if (value is Statement && decl != null) {
|
||||
return ObjRecord(value.execute(scope.createChildScope(scope.pos, newThisObj = this)), obj.isMutable)
|
||||
}
|
||||
val caller = scope.currentClassCtx
|
||||
// Check visibility for non-property members here if they weren't checked before
|
||||
if (!canAccessMember(obj.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't access field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
return when (val value = obj.value) {
|
||||
is Statement -> {
|
||||
ObjRecord(value.execute(scope.createChildScope(scope.pos, newThisObj = this)), obj.isMutable)
|
||||
}
|
||||
// could be writable property naturally
|
||||
// null -> ObjNull.asReadonly
|
||||
else -> obj
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
open suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
|
||||
willMutate(scope)
|
||||
val field = objClass.getInstanceMemberOrNull(name) ?: scope.raiseError(
|
||||
var field: ObjRecord? = null
|
||||
// 1. Hierarchy members (excluding root fallback)
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
field = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
||||
if (field != null) break
|
||||
}
|
||||
// 2. Extensions
|
||||
if (field == null) {
|
||||
field = scope.findExtension(objClass, name)
|
||||
}
|
||||
// 3. Root fallback
|
||||
if (field == null) {
|
||||
for (cls in objClass.mro) {
|
||||
if (cls.className == "Obj") {
|
||||
field = cls.members[name]
|
||||
if (field != null) break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (field == null) scope.raiseError(
|
||||
"no such field: $name on ${objClass.className}. Considered order: ${objClass.renderLinearization(true)}"
|
||||
)
|
||||
val decl = field.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
|
||||
val decl = field.declaringClass
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(field.visibility, decl, caller))
|
||||
scope.raiseError(ObjAccessException(scope, "can't assign field ${name}: not visible (declared in ${decl?.className ?: "?"}, caller ${caller?.className ?: "?"})"))
|
||||
if (field.isMutable) field.value = newValue else scope.raiseError("can't assign to read-only field: $name")
|
||||
if (field.value is ObjProperty) {
|
||||
(field.value as ObjProperty).callSetter(scope, this, newValue, decl)
|
||||
} else if (field.isMutable) field.value = newValue else scope.raiseError("can't assign to read-only field: $name")
|
||||
}
|
||||
|
||||
open suspend fun getAt(scope: Scope, index: Obj): Obj {
|
||||
@@ -440,8 +556,8 @@ open class Obj {
|
||||
is Obj -> obj
|
||||
is Double -> ObjReal(obj)
|
||||
is Float -> ObjReal(obj.toDouble())
|
||||
is Int -> ObjInt(obj.toLong())
|
||||
is Long -> ObjInt(obj)
|
||||
is Int -> ObjInt.of(obj.toLong())
|
||||
is Long -> ObjInt.of(obj)
|
||||
is String -> ObjString(obj)
|
||||
is CharSequence -> ObjString(obj.toString())
|
||||
is Boolean -> ObjBool(obj)
|
||||
|
||||
@@ -53,6 +53,12 @@ class ObjChar(val value: Char): Obj() {
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { ObjInt(thisAs<ObjChar>().value.code.toLong()) }
|
||||
addFn("isDigit") {
|
||||
thisAs<ObjChar>().value.isDigit().toObj()
|
||||
}
|
||||
addFn("isSpace") {
|
||||
thisAs<ObjChar>().value.isWhitespace().toObj()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,7 +175,11 @@ open class ObjClass(
|
||||
}
|
||||
|
||||
/** Full C3 MRO including this class at index 0. */
|
||||
val mro: List<ObjClass> by lazy { c3Linearize(this, mutableMapOf()) }
|
||||
val mro: List<ObjClass> by lazy {
|
||||
val base = c3Linearize(this, mutableMapOf())
|
||||
if (this.className == "Obj" || base.any { it.className == "Obj" }) base
|
||||
else base + rootObjectType
|
||||
}
|
||||
|
||||
/** Parents in C3 order (no self). */
|
||||
val mroParents: List<ObjClass> by lazy { mro.drop(1) }
|
||||
@@ -200,12 +204,23 @@ open class ObjClass(
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int = if (other === this) 0 else -1
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
val instance = createInstance(scope)
|
||||
initializeInstance(instance, scope.args, runConstructors = true)
|
||||
return instance
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of this class and initialize its [ObjInstance.instanceScope] with
|
||||
* methods. Does NOT run initializers or constructors.
|
||||
*/
|
||||
internal fun createInstance(scope: Scope): ObjInstance {
|
||||
val instance = ObjInstance(this)
|
||||
// Avoid capturing a transient (pooled) call frame as the parent of the instance scope.
|
||||
// Bind instance scope to the caller's parent chain directly so name resolution (e.g., stdlib like sqrt)
|
||||
// remains stable even when call frames are pooled and reused.
|
||||
val stableParent = scope.parent
|
||||
val stableParent = classScope ?: scope.parent
|
||||
instance.instanceScope = Scope(stableParent, scope.args, scope.pos, instance)
|
||||
instance.instanceScope.currentClassCtx = null
|
||||
// Expose instance methods (and other callable members) directly in the instance scope for fast lookup
|
||||
// This mirrors Obj.autoInstanceScope behavior for ad-hoc scopes and makes fb.method() resolution robust
|
||||
// 1) members-defined methods
|
||||
@@ -223,75 +238,101 @@ open class ObjClass(
|
||||
}
|
||||
}
|
||||
}
|
||||
// Constructor chaining MVP: initialize base classes left-to-right, then this class.
|
||||
// Ensure each ancestor is initialized at most once (diamond-safe).
|
||||
val visited = hashSetOf<ObjClass>()
|
||||
return instance
|
||||
}
|
||||
|
||||
suspend fun initClass(c: ObjClass, argsForThis: Arguments?, isRoot: Boolean = false) {
|
||||
if (!visited.add(c)) return
|
||||
// For the most-derived class, bind its constructor params BEFORE parents so base arg thunks can see them
|
||||
if (isRoot) {
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
// Assign constructor params into instance scope (unmangled)
|
||||
meta.assignToContext(instance.instanceScope, argsHere)
|
||||
// Also expose them under MI-mangled storage keys `${Class}::name` so qualified views can access them
|
||||
// and so that base-class casts like `(obj as Base).field` work.
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
// Always point the mangled name to the current record to keep writes consistent
|
||||
// across re-bindings (e.g., second pass before ctor)
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Run initializers and optionally constructors for the given [instance].
|
||||
* Handles Multiple Inheritance correctly (diamond-safe).
|
||||
*/
|
||||
internal suspend fun initializeInstance(
|
||||
instance: ObjInstance,
|
||||
args: Arguments?,
|
||||
runConstructors: Boolean
|
||||
) {
|
||||
val visited = hashSetOf<ObjClass>()
|
||||
initClassInternal(instance, visited, this, args, isRoot = true, runConstructors = runConstructors)
|
||||
}
|
||||
|
||||
private suspend fun initClassInternal(
|
||||
instance: ObjInstance,
|
||||
visited: MutableSet<ObjClass>,
|
||||
c: ObjClass,
|
||||
argsForThis: Arguments?,
|
||||
@Suppress("UNUSED_PARAMETER") isRoot: Boolean = false,
|
||||
runConstructors: Boolean = true
|
||||
) {
|
||||
if (!visited.add(c)) return
|
||||
|
||||
// Bind constructor parameters (both mangled and unmangled)
|
||||
// These are needed for:
|
||||
// 1) base constructor argument evaluation (if called from a derived class)
|
||||
// 2) this class's field initializers and `init` blocks
|
||||
// 3) this class's constructor body
|
||||
// 4) `compareTo` and other structural operations
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
// Assign constructor params into instance scope (unmangled)
|
||||
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c)
|
||||
// Also expose them under MI-mangled storage keys `${Class}::name` so qualified views can access them
|
||||
// and so that base-class casts like `(obj as Base).field` work.
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
// Always point the mangled name to the current record to keep writes consistent
|
||||
// across re-bindings
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
// Initialize direct parents first, in order
|
||||
for (p in c.directParents) {
|
||||
val raw = c.directParentArgs[p]?.toArguments(instance.instanceScope, false)
|
||||
val limited = if (raw != null) {
|
||||
val need = p.constructorMeta?.params?.size ?: 0
|
||||
if (need == 0) Arguments.EMPTY else Arguments(raw.list.take(need), tailBlockMode = false)
|
||||
} else Arguments.EMPTY
|
||||
initClass(p, limited)
|
||||
}
|
||||
// Execute per-instance initializers collected from class body for this class
|
||||
if (c.instanceInitializers.isNotEmpty()) {
|
||||
val savedCtx = instance.instanceScope.currentClassCtx
|
||||
instance.instanceScope.currentClassCtx = c
|
||||
try {
|
||||
for (initStmt in c.instanceInitializers) {
|
||||
initStmt.execute(instance.instanceScope)
|
||||
}
|
||||
} finally {
|
||||
instance.instanceScope.currentClassCtx = savedCtx
|
||||
}
|
||||
}
|
||||
// Then run this class' constructor, if any
|
||||
c.instanceConstructor?.let { ctor ->
|
||||
// Bind this class's constructor parameters into the instance scope now, right before ctor
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
meta.assignToContext(instance.instanceScope, argsHere)
|
||||
// Ensure mangled aliases exist for qualified access starting from this class
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
// Overwrite to ensure alias refers to the latest ObjRecord after re-binding
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
}
|
||||
val execScope = instance.instanceScope.createChildScope(args = argsForThis ?: Arguments.EMPTY, newThisObj = instance)
|
||||
ctor.execute(execScope)
|
||||
}
|
||||
}
|
||||
|
||||
initClass(this, instance.instanceScope.args, isRoot = true)
|
||||
return instance
|
||||
// Initialize direct parents first, in order
|
||||
for (p in c.directParents) {
|
||||
val raw = c.directParentArgs[p]?.toArguments(instance.instanceScope, false)
|
||||
val limited = if (raw != null) {
|
||||
val need = p.constructorMeta?.params?.size ?: 0
|
||||
if (need == 0) Arguments.EMPTY else Arguments(raw.list.take(need), tailBlockMode = false)
|
||||
} else Arguments.EMPTY
|
||||
initClassInternal(instance, visited, p, limited, false, runConstructors)
|
||||
}
|
||||
|
||||
// Re-bind this class's parameters right before running its initializers and constructor.
|
||||
// This ensures that unmangled names in the instance scope correctly refer to THIS class's
|
||||
// parameters even if they were shadowed/overwritten by parent class initialization.
|
||||
c.constructorMeta?.let { meta ->
|
||||
val argsHere = argsForThis ?: Arguments.EMPTY
|
||||
meta.assignToContext(instance.instanceScope, argsHere, declaringClass = c)
|
||||
// Re-sync mangled names to point to the fresh records to keep them consistent
|
||||
for (p in meta.params) {
|
||||
val rec = instance.instanceScope.objects[p.name]
|
||||
if (rec != null) {
|
||||
val mangled = "${c.className}::${p.name}"
|
||||
instance.instanceScope.objects[mangled] = rec
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Execute per-instance initializers collected from class body for this class
|
||||
if (c.instanceInitializers.isNotEmpty()) {
|
||||
val savedCtx = instance.instanceScope.currentClassCtx
|
||||
instance.instanceScope.currentClassCtx = c
|
||||
try {
|
||||
for (initStmt in c.instanceInitializers) {
|
||||
initStmt.execute(instance.instanceScope)
|
||||
}
|
||||
} finally {
|
||||
instance.instanceScope.currentClassCtx = savedCtx
|
||||
}
|
||||
}
|
||||
// Then run this class' constructor, if any
|
||||
if (runConstructors) {
|
||||
c.instanceConstructor?.let { ctor ->
|
||||
val execScope =
|
||||
instance.instanceScope.createChildScope(args = argsForThis ?: Arguments.EMPTY, newThisObj = instance)
|
||||
ctor.execute(execScope)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun callWithArgs(scope: Scope, vararg plainArgs: Obj): Obj {
|
||||
@@ -304,14 +345,15 @@ open class ObjClass(
|
||||
initialValue: Obj,
|
||||
isMutable: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
pos: Pos = Pos.builtIn
|
||||
pos: Pos = Pos.builtIn,
|
||||
declaringClass: ObjClass? = this
|
||||
) {
|
||||
// Allow overriding ancestors: only prevent redefinition if THIS class already defines an immutable member
|
||||
val existingInSelf = members[name]
|
||||
if (existingInSelf != null && existingInSelf.isMutable == false)
|
||||
throw ScriptError(pos, "$name is already defined in $objClass")
|
||||
// Install/override in this class
|
||||
members[name] = ObjRecord(initialValue, isMutable, visibility, declaringClass = this)
|
||||
members[name] = ObjRecord(initialValue, isMutable, visibility, declaringClass = declaringClass)
|
||||
// Structural change: bump layout version for PIC invalidation
|
||||
layoutVersion += 1
|
||||
}
|
||||
@@ -341,10 +383,11 @@ open class ObjClass(
|
||||
name: String,
|
||||
isOpen: Boolean = false,
|
||||
visibility: Visibility = Visibility.Public,
|
||||
declaringClass: ObjClass? = this,
|
||||
code: suspend Scope.() -> Obj
|
||||
) {
|
||||
val stmt = statement { code() }
|
||||
createField(name, stmt, isOpen, visibility)
|
||||
createField(name, stmt, isOpen, visibility, Pos.builtIn, declaringClass)
|
||||
}
|
||||
|
||||
fun addConst(name: String, value: Obj) = createField(name, value, isMutable = false)
|
||||
@@ -415,7 +458,7 @@ open class ObjClass(
|
||||
|
||||
override suspend fun invokeInstanceMethod(
|
||||
scope: Scope, name: String, args: Arguments,
|
||||
onNotFoundResult: (() -> Obj?)?
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj {
|
||||
return classScope?.objects?.get(name)?.value?.invoke(scope, this, args)
|
||||
?: super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
|
||||
|
||||
@@ -81,7 +81,7 @@ open class ObjDynamic(var readCallback: Statement? = null, var writeCallback: St
|
||||
scope: Scope,
|
||||
name: String,
|
||||
args: Arguments,
|
||||
onNotFoundResult: (() -> Obj?)?
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj {
|
||||
val execBase = builderScope?.let { ClosureScope(scope, it) } ?: scope
|
||||
val over = readCallback?.execute(execBase.createChildScope(Arguments(ObjString(name))))
|
||||
|
||||
@@ -71,7 +71,7 @@ open class ObjException(
|
||||
)
|
||||
} else {
|
||||
// Fallback textual entry if StackTraceEntry class is not available in this scope
|
||||
result.list += ObjString("${'$'}{pos.source.objSourceName}:${'$'}{pos.line}:${'$'}{pos.column}: ${'$'}{pos.currentLine}")
|
||||
result.list += ObjString("${pos.source.objSourceName}:${pos.line}:${pos.column}: ${pos.currentLine}")
|
||||
}
|
||||
}
|
||||
s = s.parent
|
||||
@@ -89,7 +89,7 @@ open class ObjException(
|
||||
|
||||
override val objClass: ObjClass = exceptionClass
|
||||
|
||||
override suspend fun toString(scope: Scope, calledFromLyng: Boolean): ObjString {
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
val at = getStackTrace().list.firstOrNull()?.toString(scope)
|
||||
?: ObjString("(unknown)")
|
||||
return ObjString("${objClass.className}: $message at $at")
|
||||
|
||||
@@ -32,24 +32,25 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
|
||||
override suspend fun readField(scope: Scope, name: String): ObjRecord {
|
||||
// Direct (unmangled) lookup first
|
||||
instanceScope[name]?.let {
|
||||
val decl = it.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
// When execution passes through suspension points (e.g., withLock),
|
||||
// currentClassCtx could be lost. Fall back to the instance scope class ctx
|
||||
// to preserve correct visibility semantics for private/protected members
|
||||
// accessed from within the class methods.
|
||||
instanceScope[name]?.let { rec ->
|
||||
val decl = rec.declaringClass
|
||||
// Allow unconditional access when accessing through `this` of the same instance
|
||||
if (scope.thisObj === this) return it
|
||||
val caller0 = scope.currentClassCtx ?: instanceScope.currentClassCtx
|
||||
val caller = caller0 // do not default to objClass for outsiders
|
||||
if (!canAccessMember(it.visibility, decl, caller))
|
||||
scope.raiseError(
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't access field $name (declared in ${decl?.className ?: "?"})"
|
||||
// BUT only if we are in the class context (not extension)
|
||||
if (scope.thisObj !== this || scope.currentClassCtx == null) {
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't access field $name (declared in ${decl?.className ?: "?"})"
|
||||
)
|
||||
)
|
||||
)
|
||||
return it
|
||||
}
|
||||
if (rec.type == ObjRecord.Type.Property) {
|
||||
val prop = rec.value as ObjProperty
|
||||
return rec.copy(value = prop.callGetter(scope, this, decl))
|
||||
}
|
||||
return rec
|
||||
}
|
||||
// Try MI-mangled lookup along linearization (C3 MRO): ClassName::name
|
||||
val cls = objClass
|
||||
@@ -70,16 +71,20 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
instanceScope.objects.containsKey("${cls.className}::$name") -> cls
|
||||
else -> cls.mroParents.firstOrNull { instanceScope.objects.containsKey("${it.className}::$name") }
|
||||
}
|
||||
if (scope.thisObj === this) return rec
|
||||
val caller0 = scope.currentClassCtx ?: instanceScope.currentClassCtx
|
||||
val caller = caller0 // do not default to objClass for outsiders
|
||||
if (!canAccessMember(rec.visibility, declaring, caller))
|
||||
scope.raiseError(
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't access field $name (declared in ${declaring?.className ?: "?"})"
|
||||
if (scope.thisObj !== this || scope.currentClassCtx == null) {
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, declaring, caller))
|
||||
scope.raiseError(
|
||||
ObjAccessException(
|
||||
scope,
|
||||
"can't access field $name (declared in ${declaring?.className ?: "?"})"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
if (rec.type == ObjRecord.Type.Property) {
|
||||
val prop = rec.value as ObjProperty
|
||||
return rec.copy(value = prop.callGetter(scope, this, declaring))
|
||||
}
|
||||
return rec
|
||||
}
|
||||
// Fall back to methods/properties on class
|
||||
@@ -89,18 +94,20 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
override suspend fun writeField(scope: Scope, name: String, newValue: Obj) {
|
||||
// Direct (unmangled) first
|
||||
instanceScope[name]?.let { f ->
|
||||
val decl = f.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
if (scope.thisObj === this) {
|
||||
// direct self-assignment allowed; enforce mutability below
|
||||
} else {
|
||||
val caller0 = scope.currentClassCtx ?: instanceScope.currentClassCtx
|
||||
val caller = caller0 // do not default to objClass for outsiders
|
||||
val decl = f.declaringClass
|
||||
if (scope.thisObj !== this || scope.currentClassCtx == null) {
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(f.visibility, decl, caller))
|
||||
ObjIllegalAssignmentException(
|
||||
scope,
|
||||
"can't assign to field $name (declared in ${decl?.className ?: "?"})"
|
||||
).raise()
|
||||
}
|
||||
if (f.type == ObjRecord.Type.Property) {
|
||||
val prop = f.value as ObjProperty
|
||||
prop.callSetter(scope, this, newValue, decl)
|
||||
return
|
||||
}
|
||||
if (!f.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (f.value.assign(scope, newValue) == null)
|
||||
f.value = newValue
|
||||
@@ -122,15 +129,19 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
instanceScope.objects.containsKey("${cls.className}::$name") -> cls
|
||||
else -> cls.mroParents.firstOrNull { instanceScope.objects.containsKey("${it.className}::$name") }
|
||||
}
|
||||
if (scope.thisObj !== this) {
|
||||
val caller0 = scope.currentClassCtx ?: instanceScope.currentClassCtx
|
||||
val caller = caller0 // do not default to objClass for outsiders
|
||||
if (scope.thisObj !== this || scope.currentClassCtx == null) {
|
||||
val caller = scope.currentClassCtx
|
||||
if (!canAccessMember(rec.visibility, declaring, caller))
|
||||
ObjIllegalAssignmentException(
|
||||
scope,
|
||||
"can't assign to field $name (declared in ${declaring?.className ?: "?"})"
|
||||
).raise()
|
||||
}
|
||||
if (rec.type == ObjRecord.Type.Property) {
|
||||
val prop = rec.value as ObjProperty
|
||||
prop.callSetter(scope, this, newValue, declaring)
|
||||
return
|
||||
}
|
||||
if (!rec.isMutable) ObjIllegalAssignmentException(scope, "can't reassign val $name").raise()
|
||||
if (rec.value.assign(scope, newValue) == null)
|
||||
rec.value = newValue
|
||||
@@ -141,12 +152,11 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
|
||||
override suspend fun invokeInstanceMethod(
|
||||
scope: Scope, name: String, args: Arguments,
|
||||
onNotFoundResult: (() -> Obj?)?
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj =
|
||||
instanceScope[name]?.let { rec ->
|
||||
val decl = rec.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
val caller0 = scope.currentClassCtx ?: instanceScope.currentClassCtx
|
||||
val caller = caller0 ?: if (scope.thisObj === this) objClass else null
|
||||
val decl = rec.declaringClass
|
||||
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(
|
||||
ObjAccessException(
|
||||
@@ -154,25 +164,17 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
"can't invoke method $name (declared in ${decl?.className ?: "?"})"
|
||||
)
|
||||
)
|
||||
// execute with lexical class context propagated to declaring class
|
||||
val saved = instanceScope.currentClassCtx
|
||||
instanceScope.currentClassCtx = decl
|
||||
try {
|
||||
rec.value.invoke(
|
||||
instanceScope,
|
||||
this,
|
||||
args
|
||||
)
|
||||
} finally {
|
||||
instanceScope.currentClassCtx = saved
|
||||
}
|
||||
rec.value.invoke(
|
||||
instanceScope,
|
||||
this,
|
||||
args
|
||||
)
|
||||
}
|
||||
?: run {
|
||||
// fallback: class-scope function (registered during class body execution)
|
||||
objClass.classScope?.objects?.get(name)?.let { rec ->
|
||||
val decl = rec.declaringClass ?: objClass.findDeclaringClassOf(name)
|
||||
val caller0 = scope.currentClassCtx ?: instanceScope.currentClassCtx
|
||||
val caller = caller0 ?: if (scope.thisObj === this) objClass else null
|
||||
val decl = rec.declaringClass
|
||||
val caller = scope.currentClassCtx ?: if (scope.thisObj === this) objClass else null
|
||||
if (!canAccessMember(rec.visibility, decl, caller))
|
||||
scope.raiseError(
|
||||
ObjAccessException(
|
||||
@@ -180,13 +182,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
"can't invoke method $name (declared in ${decl?.className ?: "?"})"
|
||||
)
|
||||
)
|
||||
val saved = instanceScope.currentClassCtx
|
||||
instanceScope.currentClassCtx = decl
|
||||
try {
|
||||
rec.value.invoke(instanceScope, this, args)
|
||||
} finally {
|
||||
instanceScope.currentClassCtx = saved
|
||||
}
|
||||
rec.value.invoke(instanceScope, this, args)
|
||||
}
|
||||
}
|
||||
?: super.invokeInstanceMethod(scope, name, args, onNotFoundResult)
|
||||
@@ -197,9 +193,20 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
!it.key.contains("::") && it.value.visibility.isPublic && it.value.type.serializable
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
val fields = publicFields.map { "${it.key}=${it.value.value}" }.joinToString(",")
|
||||
return "${objClass.className}($fields)"
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
return ObjString(buildString {
|
||||
append("${objClass.className}(")
|
||||
var first = true
|
||||
for ((name, value) in publicFields) {
|
||||
if (first) first = false else append(",")
|
||||
append("$name=${value.value.toString(scope)}")
|
||||
}
|
||||
append(")")
|
||||
})
|
||||
}
|
||||
|
||||
override suspend fun inspect(scope: Scope): String {
|
||||
return toString(scope).value
|
||||
}
|
||||
|
||||
override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
|
||||
@@ -210,10 +217,8 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
|
||||
// using objlist allow for some optimizations:
|
||||
val params = meta.params.map { readField(scope, it.name).value }
|
||||
println("serializing $objClass with params: $params")
|
||||
encoder.encodeAnyList(scope, params)
|
||||
val vars = serializingVars.values.map { it.value }
|
||||
println("encoding vars: $vars")
|
||||
if (vars.isNotEmpty<Obj>()) {
|
||||
encoder.encodeAnyList(scope, vars)
|
||||
}
|
||||
@@ -235,7 +240,7 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
for (i in serializingVars) {
|
||||
// remove T:: prefix from the field name for JSON
|
||||
val parts = i.key.split("::")
|
||||
result[if( parts.size == 1 ) parts[0] else parts.last()] = i.value.value.toJson(scope)
|
||||
result[if (parts.size == 1) parts[0] else parts.last()] = i.value.value.toJson(scope)
|
||||
}
|
||||
return JsonObject(result)
|
||||
}
|
||||
@@ -248,7 +253,8 @@ class ObjInstance(override val objClass: ObjClass) : Obj() {
|
||||
instanceScope.objects.filter {
|
||||
it.value.type.serializable &&
|
||||
it.value.type == ObjRecord.Type.Field &&
|
||||
it.value.isMutable }
|
||||
it.value.isMutable
|
||||
}
|
||||
}
|
||||
|
||||
internal suspend fun deserializeStateVars(scope: Scope, decoder: LynonDecoder) {
|
||||
@@ -381,7 +387,7 @@ class ObjQualifiedView(val instance: ObjInstance, private val startClass: ObjCla
|
||||
scope: Scope,
|
||||
name: String,
|
||||
args: Arguments,
|
||||
onNotFoundResult: (() -> Obj?)?
|
||||
onNotFoundResult: (suspend () -> Obj?)?
|
||||
): Obj {
|
||||
// Qualified method dispatch must start from the specified ancestor, not from the instance scope.
|
||||
memberFromAncestor(name)?.let { rec ->
|
||||
|
||||
@@ -33,7 +33,9 @@ class ObjInstanceClass(val name: String, vararg parents: ObjClass) : ObjClass(na
|
||||
if (args.size > actualSize)
|
||||
scope.raiseIllegalArgument("constructor $name has only $actualSize but serialized version has ${args.size}")
|
||||
val newScope = scope.createChildScope(args = Arguments(args))
|
||||
return (callOn(newScope) as ObjInstance).apply {
|
||||
val instance = createInstance(newScope)
|
||||
initializeInstance(instance, newScope.args, runConstructors = false)
|
||||
return instance.apply {
|
||||
deserializeStateVars(scope, decoder)
|
||||
invokeInstanceMethod(scope, "onDeserialized") { ObjVoid }
|
||||
}
|
||||
@@ -41,7 +43,7 @@ class ObjInstanceClass(val name: String, vararg parents: ObjClass) : ObjClass(na
|
||||
|
||||
init {
|
||||
addFn("toString", true) {
|
||||
ObjString(thisObj.toString())
|
||||
thisObj.toString(this, true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,6 @@
|
||||
|
||||
package net.sergeych.lyng.obj
|
||||
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.isDistantFuture
|
||||
import kotlinx.datetime.isDistantPast
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
@@ -28,6 +24,10 @@ import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonSettings
|
||||
import net.sergeych.lynon.LynonType
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
import kotlin.time.isDistantFuture
|
||||
import kotlin.time.isDistantPast
|
||||
|
||||
class ObjInstant(val instant: Instant,val truncateMode: LynonSettings.InstantTruncateMode=LynonSettings.InstantTruncateMode.Microsecond) : Obj() {
|
||||
override val objClass: ObjClass get() = type
|
||||
|
||||
@@ -24,36 +24,32 @@ import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
import net.sergeych.lynon.LynonType
|
||||
|
||||
class ObjInt(var value: Long, override val isConst: Boolean = false) : Obj(), Numeric {
|
||||
class ObjInt(val value: Long, override val isConst: Boolean = false) : Obj(), Numeric {
|
||||
override val longValue get() = value
|
||||
override val doubleValue get() = value.toDouble()
|
||||
override val toObjInt get() = this
|
||||
override val toObjReal = ObjReal(doubleValue)
|
||||
override val toObjReal = ObjReal.of(doubleValue)
|
||||
|
||||
override fun byValueCopy(): Obj = ObjInt(value)
|
||||
override fun byValueCopy(): Obj = this
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return value.hashCode()
|
||||
}
|
||||
|
||||
override suspend fun getAndIncrement(scope: Scope): Obj {
|
||||
ensureNotConst(scope)
|
||||
return ObjInt(value).also { value++ }
|
||||
return this
|
||||
}
|
||||
|
||||
override suspend fun getAndDecrement(scope: Scope): Obj {
|
||||
ensureNotConst(scope)
|
||||
return ObjInt(value).also { value-- }
|
||||
return this
|
||||
}
|
||||
|
||||
override suspend fun incrementAndGet(scope: Scope): Obj {
|
||||
ensureNotConst(scope)
|
||||
return ObjInt(++value)
|
||||
return of(value + 1)
|
||||
}
|
||||
|
||||
override suspend fun decrementAndGet(scope: Scope): Obj {
|
||||
ensureNotConst(scope)
|
||||
return ObjInt(--value)
|
||||
return of(value - 1)
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
@@ -67,41 +63,35 @@ class ObjInt(var value: Long, override val isConst: Boolean = false) : Obj(), Nu
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt)
|
||||
ObjInt(this.value + other.value)
|
||||
of(this.value + other.value)
|
||||
else
|
||||
ObjReal(this.doubleValue + other.toDouble())
|
||||
ObjReal.of(this.doubleValue + other.toDouble())
|
||||
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt)
|
||||
ObjInt(this.value - other.value)
|
||||
of(this.value - other.value)
|
||||
else
|
||||
ObjReal(this.doubleValue - other.toDouble())
|
||||
ObjReal.of(this.doubleValue - other.toDouble())
|
||||
|
||||
override suspend fun mul(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt) {
|
||||
ObjInt(this.value * other.value)
|
||||
} else ObjReal(this.value * other.toDouble())
|
||||
of(this.value * other.value)
|
||||
} else ObjReal.of(this.value * other.toDouble())
|
||||
|
||||
override suspend fun div(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt)
|
||||
ObjInt(this.value / other.value)
|
||||
else ObjReal(this.value / other.toDouble())
|
||||
of(this.value / other.value)
|
||||
else ObjReal.of(this.value / other.toDouble())
|
||||
|
||||
override suspend fun mod(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt)
|
||||
ObjInt(this.value % other.value)
|
||||
else ObjReal(this.value.toDouble() % other.toDouble())
|
||||
of(this.value % other.value)
|
||||
else ObjReal.of(this.value.toDouble() % other.toDouble())
|
||||
|
||||
/**
|
||||
* We are by-value type ([byValueCopy] is implemented) so we can do in-place
|
||||
* assignment
|
||||
* Numbers are now immutable, so we can't do in-place assignment.
|
||||
*/
|
||||
override suspend fun assign(scope: Scope, other: Obj): Obj? {
|
||||
return if (!isConst && other is ObjInt) {
|
||||
value = other.value
|
||||
this
|
||||
} else null
|
||||
}
|
||||
override suspend fun assign(scope: Scope, other: Obj): Obj? = null
|
||||
|
||||
override suspend fun toKotlin(scope: Scope): Any {
|
||||
return value
|
||||
@@ -117,31 +107,31 @@ class ObjInt(var value: Long, override val isConst: Boolean = false) : Obj(), Nu
|
||||
}
|
||||
|
||||
override suspend fun negate(scope: Scope): Obj {
|
||||
return ObjInt(-value)
|
||||
return of(-value)
|
||||
}
|
||||
|
||||
// Bitwise operations
|
||||
override suspend fun bitAnd(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt) ObjInt(this.value and other.value)
|
||||
if (other is ObjInt) of(this.value and other.value)
|
||||
else scope.raiseIllegalArgument("bitwise and '&' requires Int, got ${other.objClass.className}")
|
||||
|
||||
override suspend fun bitOr(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt) ObjInt(this.value or other.value)
|
||||
if (other is ObjInt) of(this.value or other.value)
|
||||
else scope.raiseIllegalArgument("bitwise or '|' requires Int, got ${other.objClass.className}")
|
||||
|
||||
override suspend fun bitXor(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt) ObjInt(this.value xor other.value)
|
||||
if (other is ObjInt) of(this.value xor other.value)
|
||||
else scope.raiseIllegalArgument("bitwise xor '^' requires Int, got ${other.objClass.className}")
|
||||
|
||||
override suspend fun shl(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt) ObjInt(this.value shl (other.value.toInt() and 63))
|
||||
if (other is ObjInt) of(this.value shl (other.value.toInt() and 63))
|
||||
else scope.raiseIllegalArgument("shift left '<<' requires Int, got ${other.objClass.className}")
|
||||
|
||||
override suspend fun shr(scope: Scope, other: Obj): Obj =
|
||||
if (other is ObjInt) ObjInt(this.value shr (other.value.toInt() and 63))
|
||||
if (other is ObjInt) of(this.value shr (other.value.toInt() and 63))
|
||||
else scope.raiseIllegalArgument("shift right '>>' requires Int, got ${other.objClass.className}")
|
||||
|
||||
override suspend fun bitNot(scope: Scope): Obj = ObjInt(this.value.inv())
|
||||
override suspend fun bitNot(scope: Scope): Obj = of(this.value.inv())
|
||||
|
||||
override suspend fun lynonType(): LynonType = when (value) {
|
||||
0L -> LynonType.Int0
|
||||
@@ -168,20 +158,32 @@ class ObjInt(var value: Long, override val isConst: Boolean = false) : Obj(), Nu
|
||||
}
|
||||
|
||||
companion object {
|
||||
val Zero = ObjInt(0, true)
|
||||
val One = ObjInt(1, true)
|
||||
private val cache = Array(256) { ObjInt((it - 128).toLong(), true) }
|
||||
|
||||
fun of(value: Long): ObjInt {
|
||||
return if (value in -128L..127L) cache[(value + 128).toInt()]
|
||||
else ObjInt(value)
|
||||
}
|
||||
|
||||
val Zero = of(0)
|
||||
val One = of(1)
|
||||
val type = object : ObjClass("Int") {
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =
|
||||
when (lynonType) {
|
||||
null -> ObjInt(decoder.unpackSigned())
|
||||
null -> of(decoder.unpackSigned())
|
||||
LynonType.Int0 -> Zero
|
||||
LynonType.IntPositive -> ObjInt(decoder.unpackUnsigned().toLong())
|
||||
LynonType.IntNegative -> ObjInt(-decoder.unpackUnsigned().toLong())
|
||||
LynonType.IntSigned -> ObjInt(decoder.unpackSigned())
|
||||
LynonType.IntPositive -> of(decoder.unpackUnsigned().toLong())
|
||||
LynonType.IntNegative -> of(-decoder.unpackUnsigned().toLong())
|
||||
LynonType.IntSigned -> of(decoder.unpackSigned())
|
||||
else -> scope.raiseIllegalState("illegal type code for Int: $lynonType")
|
||||
}
|
||||
}.apply {
|
||||
addFn("toInt") {
|
||||
thisObj
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Int.toObj() = ObjInt(this.toLong())
|
||||
fun Int.toObj() = ObjInt.of(this.toLong())
|
||||
fun Long.toObj() = ObjInt.of(this)
|
||||
@@ -104,11 +104,14 @@ val ObjIterable by lazy {
|
||||
returns = type("lyng.Map"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val result = ObjMap()
|
||||
val result = mutableMapOf<Obj, Obj>()
|
||||
thisObj.toFlow(this).collect { pair ->
|
||||
result.map[pair.getAt(this, 0)] = pair.getAt(this, 1)
|
||||
when (pair) {
|
||||
is ObjMapEntry -> result[pair.key] = pair.value
|
||||
else -> result[pair.getAt(this, 0)] = pair.getAt(this, 1)
|
||||
}
|
||||
}
|
||||
result
|
||||
ObjMap(result)
|
||||
}
|
||||
|
||||
addFnDoc(
|
||||
@@ -158,6 +161,23 @@ val ObjIterable by lazy {
|
||||
ObjList(result)
|
||||
}
|
||||
|
||||
addFnDoc(
|
||||
name = "mapNotNull",
|
||||
doc = "Transform elements by applying the given lambda unless it returns null.",
|
||||
params = listOf(ParamDoc("transform")),
|
||||
returns = type("lyng.List"),
|
||||
isOpen = true,
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
val fn = requiredArg<Statement>(0)
|
||||
val result = mutableListOf<Obj>()
|
||||
thisObj.toFlow(this).collect {
|
||||
val transformed = fn.call(this, it)
|
||||
if( transformed != ObjNull) result += transformed
|
||||
}
|
||||
ObjList(result)
|
||||
}
|
||||
|
||||
addFnDoc(
|
||||
name = "take",
|
||||
doc = "Take the first N elements and return them as a list.",
|
||||
|
||||
@@ -76,36 +76,3 @@ fun Obj.toFlow(scope: Scope): Flow<Obj> = flow {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call [callback] for each element of this obj considering it provides [Iterator]
|
||||
* methods `hasNext` and `next`.
|
||||
*
|
||||
* IF callback returns false, iteration is stopped.
|
||||
*/
|
||||
suspend fun Obj.enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
val iterator = invokeInstanceMethod(scope, "iterator")
|
||||
val hasNext = iterator.getInstanceMethod(scope, "hasNext")
|
||||
val next = iterator.getInstanceMethod(scope, "next")
|
||||
var closeIt = false
|
||||
try {
|
||||
while (hasNext.invoke(scope, iterator).toBool()) {
|
||||
val nextValue = next.invoke(scope, iterator)
|
||||
val shouldContinue = try {
|
||||
callback(nextValue)
|
||||
} catch (e: Exception) {
|
||||
// iteration aborted due to exception in callback
|
||||
closeIt = true
|
||||
throw e
|
||||
}
|
||||
if (!shouldContinue) {
|
||||
closeIt = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (closeIt) {
|
||||
// Best-effort cancel on premature termination
|
||||
iterator.invokeInstanceMethod(scope, "cancelIteration") { ObjVoid }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
}
|
||||
|
||||
else ->
|
||||
scope.raiseError("'+': can't concatenate $this with $other")
|
||||
scope.raiseError("'+': can't concatenate ${this.toString(scope)} with ${other.toString(scope)}")
|
||||
}
|
||||
|
||||
|
||||
@@ -140,6 +140,12 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
return list.contains(other)
|
||||
}
|
||||
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
for (item in list) {
|
||||
if (!callback(item)) break
|
||||
}
|
||||
}
|
||||
|
||||
override val objClass: ObjClass
|
||||
get() = type
|
||||
|
||||
@@ -198,6 +204,18 @@ class ObjList(val list: MutableList<Obj> = mutableListOf()) : Obj() {
|
||||
return JsonArray(list.map { it.toJson(scope) })
|
||||
}
|
||||
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
return ObjString(buildString {
|
||||
append("[")
|
||||
var first = true
|
||||
for (v in list) {
|
||||
if (first) first = false else append(",")
|
||||
append(v.toString(scope).value)
|
||||
}
|
||||
append("]")
|
||||
})
|
||||
}
|
||||
|
||||
companion object {
|
||||
val type = object : ObjClass("List", ObjArray) {
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj {
|
||||
|
||||
@@ -52,8 +52,8 @@ class ObjMapEntry(val key: Obj, val value: Obj) : Obj() {
|
||||
else -> scope.raiseIndexOutOfBounds()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "$key=>$value"
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
return ObjString("(${key.toString(scope).value} => ${value.toString(scope).value})")
|
||||
}
|
||||
|
||||
override val objClass = type
|
||||
@@ -124,7 +124,7 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
return -1
|
||||
}
|
||||
|
||||
override suspend fun toString(scope: Scope, calledFromLyng: Boolean): ObjString {
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
val reusult = buildString {
|
||||
append("Map(")
|
||||
var first = true
|
||||
@@ -174,18 +174,21 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
val map = mutableMapOf<Obj, Obj>()
|
||||
if (list.isEmpty()) return map
|
||||
|
||||
val first = list.first()
|
||||
if (first.isInstanceOf(ObjArray)) {
|
||||
if (first.invokeInstanceMethod(scope, "size").toInt() != 2)
|
||||
scope.raiseIllegalArgument(
|
||||
"list to construct map entry should exactly be 2 element Array like [key,value], got $list"
|
||||
)
|
||||
} else scope.raiseIllegalArgument("first element of map list be a Collection of 2 elements; got $first")
|
||||
|
||||
|
||||
|
||||
list.forEach {
|
||||
map[it.getAt(scope, ObjInt.Zero)] = it.getAt(scope, ObjInt.One)
|
||||
when (it) {
|
||||
is ObjMapEntry -> map[it.key] = it.value
|
||||
else -> {
|
||||
if (it.isInstanceOf(ObjArray)) {
|
||||
if (it.invokeInstanceMethod(scope, "size").toInt() != 2)
|
||||
scope.raiseIllegalArgument(
|
||||
"Array to construct map entry should exactly be 2 elements [key,value], got $it"
|
||||
)
|
||||
map[it.getAt(scope, ObjInt.Zero)] = it.getAt(scope, ObjInt.One)
|
||||
} else {
|
||||
scope.raiseIllegalArgument("elements to construct map must be MapEntry or Array of 2 elements; got $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return map
|
||||
}
|
||||
@@ -296,13 +299,11 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
is ObjMap -> {
|
||||
// Rightmost wins: copy all entries from `other` over existing ones
|
||||
for ((k, v) in other.map) {
|
||||
val key = k as? ObjString ?: scope.raiseIllegalArgument("map merge expects string keys; got $k")
|
||||
map[key] = v
|
||||
map[k] = v
|
||||
}
|
||||
}
|
||||
is ObjMapEntry -> {
|
||||
val key = other.key as? ObjString ?: scope.raiseIllegalArgument("map merge expects string keys; got ${other.key}")
|
||||
map[key] = other.value
|
||||
map[other.key] = other.value
|
||||
}
|
||||
is ObjList -> {
|
||||
// Treat as list of map entries
|
||||
@@ -311,8 +312,7 @@ class ObjMap(val map: MutableMap<Obj, Obj> = mutableMapOf()) : Obj() {
|
||||
is ObjMapEntry -> e
|
||||
else -> scope.raiseIllegalArgument("map can only be merged with MapEntry elements; got $e")
|
||||
}
|
||||
val key = entry.key as? ObjString ?: scope.raiseIllegalArgument("map merge expects string keys; got ${entry.key}")
|
||||
map[key] = entry.value
|
||||
map[entry.key] = entry.value
|
||||
}
|
||||
}
|
||||
else -> scope.raiseIllegalArgument("map can only be merged with Map, MapEntry, or List<MapEntry>")
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2025 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.obj
|
||||
|
||||
import net.sergeych.lyng.Arguments
|
||||
import net.sergeych.lyng.ClosureScope
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.Statement
|
||||
|
||||
/**
|
||||
* Property accessor storage. Per instructions, properties do NOT have
|
||||
* automatic backing fields. They are pure accessors.
|
||||
*/
|
||||
class ObjProperty(
|
||||
val name: String,
|
||||
val getter: Statement?,
|
||||
val setter: Statement?
|
||||
) : Obj() {
|
||||
|
||||
suspend fun callGetter(scope: Scope, instance: Obj, declaringClass: ObjClass? = null): Obj {
|
||||
val g = getter ?: scope.raiseError("property $name has no getter")
|
||||
// Execute getter in a child scope of the instance with 'this' properly set
|
||||
// Use ClosureScope to match extension function behavior (access to instance scope + call scope)
|
||||
val instanceScope = (instance as? ObjInstance)?.instanceScope ?: instance.autoInstanceScope(scope)
|
||||
val execScope = ClosureScope(scope, instanceScope).createChildScope(newThisObj = instance)
|
||||
execScope.currentClassCtx = declaringClass
|
||||
return g.execute(execScope)
|
||||
}
|
||||
|
||||
suspend fun callSetter(scope: Scope, instance: Obj, value: Obj, declaringClass: ObjClass? = null) {
|
||||
val s = setter ?: scope.raiseError("property $name has no setter")
|
||||
// Execute setter in a child scope of the instance with 'this' properly set and the value as an argument
|
||||
// Use ClosureScope to match extension function behavior
|
||||
val instanceScope = (instance as? ObjInstance)?.instanceScope ?: instance.autoInstanceScope(scope)
|
||||
val execScope = ClosureScope(scope, instanceScope).createChildScope(args = Arguments(value), newThisObj = instance)
|
||||
execScope.currentClassCtx = declaringClass
|
||||
s.execute(execScope)
|
||||
}
|
||||
|
||||
override fun toString(): String = "Property($name)"
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
|
||||
|
||||
override val objClass: ObjClass = type
|
||||
|
||||
override suspend fun toString(scope: Scope,calledFromLyng: Boolean): ObjString {
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
val result = StringBuilder()
|
||||
result.append("${start?.inspect(scope) ?: '∞'} ..")
|
||||
if (!isEndInclusive) result.append('<')
|
||||
@@ -114,6 +114,36 @@ class ObjRange(val start: Obj?, val end: Obj?, val isEndInclusive: Boolean) : Ob
|
||||
start is ObjChar && end is ObjChar
|
||||
}
|
||||
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
if (isIntRange) {
|
||||
val s = (start as ObjInt).value
|
||||
val e = (end as ObjInt).value
|
||||
if (isEndInclusive) {
|
||||
for (i in s..e) {
|
||||
if (!callback(ObjInt.of(i))) break
|
||||
}
|
||||
} else {
|
||||
for (i in s..<e) {
|
||||
if (!callback(ObjInt.of(i))) break
|
||||
}
|
||||
}
|
||||
} else if (isCharRange) {
|
||||
val s = (start as ObjChar).value
|
||||
val e = (end as ObjChar).value
|
||||
if (isEndInclusive) {
|
||||
for (c in s..e) {
|
||||
if (!callback(ObjChar(c))) break
|
||||
}
|
||||
} else {
|
||||
for (c in s..<e) {
|
||||
if (!callback(ObjChar(c))) break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
super.enumerate(scope, callback)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
return (other as? ObjRange)?.let {
|
||||
if( start == other.start && end == other.end ) 0 else -1
|
||||
|
||||
@@ -32,14 +32,14 @@ import kotlin.math.floor
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
data class ObjReal(val value: Double) : Obj(), Numeric {
|
||||
override val longValue: Long by lazy { floor(value).toLong() }
|
||||
override val doubleValue: Double by lazy { value }
|
||||
override val toObjInt: ObjInt by lazy { ObjInt(longValue) }
|
||||
override val toObjReal: ObjReal by lazy { ObjReal(value) }
|
||||
override val longValue: Long get() = floor(value).toLong()
|
||||
override val doubleValue: Double get() = value
|
||||
override val toObjInt: ObjInt get() = ObjInt.of(longValue)
|
||||
override val toObjReal: ObjReal get() = this
|
||||
|
||||
override val objClass: ObjClass = type
|
||||
|
||||
override fun byValueCopy(): Obj = ObjReal(value)
|
||||
override fun byValueCopy(): Obj = this
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if (other !is Numeric) return -2
|
||||
@@ -65,19 +65,19 @@ data class ObjReal(val value: Double) : Obj(), Numeric {
|
||||
}
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj =
|
||||
ObjReal(this.value + other.toDouble())
|
||||
of(this.value + other.toDouble())
|
||||
|
||||
override suspend fun minus(scope: Scope, other: Obj): Obj =
|
||||
ObjReal(this.value - other.toDouble())
|
||||
of(this.value - other.toDouble())
|
||||
|
||||
override suspend fun mul(scope: Scope, other: Obj): Obj =
|
||||
ObjReal(this.value * other.toDouble())
|
||||
of(this.value * other.toDouble())
|
||||
|
||||
override suspend fun div(scope: Scope, other: Obj): Obj =
|
||||
ObjReal(this.value / other.toDouble())
|
||||
of(this.value / other.toDouble())
|
||||
|
||||
override suspend fun mod(scope: Scope, other: Obj): Obj =
|
||||
ObjReal(this.value % other.toDouble())
|
||||
of(this.value % other.toDouble())
|
||||
|
||||
/**
|
||||
* Returns unboxed Double value
|
||||
@@ -96,7 +96,7 @@ data class ObjReal(val value: Double) : Obj(), Numeric {
|
||||
}
|
||||
|
||||
override suspend fun negate(scope: Scope): Obj {
|
||||
return ObjReal(-value)
|
||||
return of(-value)
|
||||
}
|
||||
|
||||
override suspend fun lynonType(): LynonType = LynonType.Real
|
||||
@@ -110,9 +110,18 @@ data class ObjReal(val value: Double) : Obj(), Numeric {
|
||||
}
|
||||
|
||||
companion object {
|
||||
val Zero = ObjReal(0.0)
|
||||
val One = ObjReal(1.0)
|
||||
|
||||
fun of(value: Double): ObjReal = when (value) {
|
||||
0.0 -> Zero
|
||||
1.0 -> One
|
||||
else -> ObjReal(value)
|
||||
}
|
||||
|
||||
val type: ObjClass = object : ObjClass("Real") {
|
||||
override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =
|
||||
ObjReal(decoder.unpackDouble())
|
||||
of(decoder.unpackDouble())
|
||||
}.apply {
|
||||
// roundToInt: number rounded to the nearest integer
|
||||
addConstDoc(
|
||||
@@ -130,7 +139,7 @@ data class ObjReal(val value: Double) : Obj(), Numeric {
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjInt(thisAs<ObjReal>().value.toLong())
|
||||
ObjInt.of(thisAs<ObjReal>().value.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ data class ObjRecord(
|
||||
@Suppress("unused")
|
||||
Class,
|
||||
Enum,
|
||||
Property,
|
||||
Other;
|
||||
|
||||
val isArgument get() = this == Argument
|
||||
|
||||
@@ -27,6 +27,7 @@ import net.sergeych.lyng.*
|
||||
*/
|
||||
sealed interface ObjRef {
|
||||
suspend fun get(scope: Scope): ObjRecord
|
||||
|
||||
/**
|
||||
* Fast path for evaluating an expression to a raw Obj value without wrapping it into ObjRecord.
|
||||
* Default implementation calls [get] and returns its value. Nodes can override to avoid record traffic.
|
||||
@@ -35,6 +36,20 @@ sealed interface ObjRef {
|
||||
suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
|
||||
throw ScriptError(pos, "can't assign value")
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [block] for each variable name that this reference targets for writing.
|
||||
* Used for declaring local variables in destructuring.
|
||||
*/
|
||||
fun forEachVariable(block: (String) -> Unit) {}
|
||||
|
||||
/**
|
||||
* Calls [block] for each variable name that this reference targets for writing,
|
||||
* including its source position if available.
|
||||
*/
|
||||
fun forEachVariableWithPos(block: (String, Pos) -> Unit) {
|
||||
forEachVariable { block(it, Pos.UNKNOWN) }
|
||||
}
|
||||
}
|
||||
|
||||
/** Runtime-computed read-only reference backed by a lambda. */
|
||||
@@ -117,16 +132,16 @@ class BinaryOpRef(private val op: BinOp, private val left: ObjRef, private val r
|
||||
val av = a.value
|
||||
val bv = b.value
|
||||
val r: Obj? = when (op) {
|
||||
BinOp.PLUS -> ObjInt(av + bv)
|
||||
BinOp.MINUS -> ObjInt(av - bv)
|
||||
BinOp.STAR -> ObjInt(av * bv)
|
||||
BinOp.SLASH -> if (bv != 0L) ObjInt(av / bv) else null
|
||||
BinOp.PERCENT -> if (bv != 0L) ObjInt(av % bv) else null
|
||||
BinOp.BAND -> ObjInt(av and bv)
|
||||
BinOp.BXOR -> ObjInt(av xor bv)
|
||||
BinOp.BOR -> ObjInt(av or bv)
|
||||
BinOp.SHL -> ObjInt(av shl (bv.toInt() and 63))
|
||||
BinOp.SHR -> ObjInt(av shr (bv.toInt() and 63))
|
||||
BinOp.PLUS -> ObjInt.of(av + bv)
|
||||
BinOp.MINUS -> ObjInt.of(av - bv)
|
||||
BinOp.STAR -> ObjInt.of(av * bv)
|
||||
BinOp.SLASH -> if (bv != 0L) ObjInt.of(av / bv) else null
|
||||
BinOp.PERCENT -> if (bv != 0L) ObjInt.of(av % bv) else null
|
||||
BinOp.BAND -> ObjInt.of(av and bv)
|
||||
BinOp.BXOR -> ObjInt.of(av xor bv)
|
||||
BinOp.BOR -> ObjInt.of(av or bv)
|
||||
BinOp.SHL -> ObjInt.of(av shl (bv.toInt() and 63))
|
||||
BinOp.SHR -> ObjInt.of(av shr (bv.toInt() and 63))
|
||||
BinOp.EQ -> if (av == bv) ObjTrue else ObjFalse
|
||||
BinOp.NEQ -> if (av != bv) ObjTrue else ObjFalse
|
||||
BinOp.LT -> if (av < bv) ObjTrue else ObjFalse
|
||||
@@ -201,11 +216,11 @@ class BinaryOpRef(private val op: BinOp, private val left: ObjRef, private val r
|
||||
val ad: Double = if (a is ObjInt) a.doubleValue else (a as ObjReal).value
|
||||
val bd: Double = if (b is ObjInt) b.doubleValue else (b as ObjReal).value
|
||||
val rNum: Obj? = when (op) {
|
||||
BinOp.PLUS -> ObjReal(ad + bd)
|
||||
BinOp.MINUS -> ObjReal(ad - bd)
|
||||
BinOp.STAR -> ObjReal(ad * bd)
|
||||
BinOp.SLASH -> ObjReal(ad / bd)
|
||||
BinOp.PERCENT -> ObjReal(ad % bd)
|
||||
BinOp.PLUS -> ObjReal.of(ad + bd)
|
||||
BinOp.MINUS -> ObjReal.of(ad - bd)
|
||||
BinOp.STAR -> ObjReal.of(ad * bd)
|
||||
BinOp.SLASH -> ObjReal.of(ad / bd)
|
||||
BinOp.PERCENT -> ObjReal.of(ad % bd)
|
||||
BinOp.LT -> if (ad < bd) ObjTrue else ObjFalse
|
||||
BinOp.LTE -> if (ad <= bd) ObjTrue else ObjFalse
|
||||
BinOp.GT -> if (ad > bd) ObjTrue else ObjFalse
|
||||
@@ -360,22 +375,12 @@ class IncDecRef(
|
||||
if (!rec.isMutable) scope.raiseError("Cannot ${if (isIncrement) "increment" else "decrement"} immutable value")
|
||||
val v = rec.value
|
||||
val one = ObjInt.One
|
||||
return if (v.isConst) {
|
||||
// Mirror existing semantics in Compiler for const values
|
||||
val result = if (isIncrement) v.plus(scope, one) else v.minus(scope, one)
|
||||
// write back
|
||||
target.setAt(atPos, scope, result)
|
||||
// For post-inc: previous code returned NEW value; for pre-inc: returned ORIGINAL value
|
||||
if (isPost) result.asReadonly else v.asReadonly
|
||||
} else {
|
||||
val res = when {
|
||||
isIncrement && isPost -> v.getAndIncrement(scope)
|
||||
isIncrement && !isPost -> v.incrementAndGet(scope)
|
||||
!isIncrement && isPost -> v.getAndDecrement(scope)
|
||||
else -> v.decrementAndGet(scope)
|
||||
}
|
||||
res.asReadonly
|
||||
}
|
||||
// We now treat numbers as immutable and always perform write-back via setAt.
|
||||
// This avoids issues where literals are shared and mutated in-place.
|
||||
// For post-inc: return ORIGINAL value; for pre-inc: return NEW value.
|
||||
val result = if (isIncrement) v.plus(scope, one) else v.minus(scope, one)
|
||||
target.setAt(atPos, scope, result)
|
||||
return (if (isPost) v else result).asReadonly
|
||||
}
|
||||
}
|
||||
|
||||
@@ -620,12 +625,16 @@ class FieldRef(
|
||||
val (k, v) = receiverKeyAndVersion(base)
|
||||
val rec = tRecord
|
||||
if (rec != null && tKey == k && tVer == v && tFrameId == scope.frameId) {
|
||||
// visibility/mutability checks
|
||||
if (!rec.isMutable) scope.raiseError(ObjIllegalAssignmentException(scope, "can't reassign val $name"))
|
||||
if (!rec.visibility.isPublic)
|
||||
scope.raiseError(ObjAccessException(scope, "can't access non-public field $name"))
|
||||
if (rec.value.assign(scope, newValue) == null) rec.value = newValue
|
||||
return
|
||||
// If it is a property, we must go through writeField (slow path for now)
|
||||
// or handle it here.
|
||||
if (rec.type != ObjRecord.Type.Property) {
|
||||
// visibility/mutability checks
|
||||
if (!rec.isMutable) scope.raiseError(ObjIllegalAssignmentException(scope, "can't reassign val $name"))
|
||||
if (!rec.visibility.isPublic)
|
||||
scope.raiseError(ObjAccessException(scope, "can't access non-public field $name"))
|
||||
if (rec.value.assign(scope, newValue) == null) rec.value = newValue
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fieldPic) {
|
||||
@@ -1169,18 +1178,25 @@ class MethodCallRef(
|
||||
when (base) {
|
||||
is ObjInstance -> {
|
||||
// Prefer resolved class member to avoid per-call lookup on hit
|
||||
val member = base.objClass.getInstanceMemberOrNull(name)
|
||||
if (member != null) {
|
||||
val visibility = member.visibility
|
||||
val callable = member.value
|
||||
// BUT only if it's NOT a root object member (which can be shadowed by extensions)
|
||||
var hierarchyMember: ObjRecord? = null
|
||||
for (cls in base.objClass.mro) {
|
||||
if (cls.className == "Obj") break
|
||||
hierarchyMember = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
||||
if (hierarchyMember != null) break
|
||||
}
|
||||
|
||||
if (hierarchyMember != null) {
|
||||
val visibility = hierarchyMember.visibility
|
||||
val callable = hierarchyMember.value
|
||||
mKey1 = key; mVer1 = ver; mInvoker1 = { obj, sc, a ->
|
||||
val inst = obj as ObjInstance
|
||||
if (!visibility.isPublic)
|
||||
if (!visibility.isPublic && !canAccessMember(visibility, hierarchyMember.declaringClass ?: inst.objClass, sc.currentClassCtx))
|
||||
sc.raiseError(ObjAccessException(sc, "can't invoke non-public method $name"))
|
||||
callable.invoke(inst.instanceScope, inst, a)
|
||||
}
|
||||
} else {
|
||||
// Fallback to name-based lookup per call (uncommon)
|
||||
// Fallback to name-based lookup per call (handles extensions and root members)
|
||||
mKey1 = key; mVer1 = ver; mInvoker1 = { obj, sc, a -> obj.invokeInstanceMethod(sc, name, a) }
|
||||
}
|
||||
}
|
||||
@@ -1214,7 +1230,14 @@ class MethodCallRef(
|
||||
/**
|
||||
* Reference to a local/visible variable by name (Phase A: scope lookup).
|
||||
*/
|
||||
class LocalVarRef(private val name: String, private val atPos: Pos) : ObjRef {
|
||||
class LocalVarRef(val name: String, private val atPos: Pos) : ObjRef {
|
||||
override fun forEachVariable(block: (String) -> Unit) {
|
||||
block(name)
|
||||
}
|
||||
|
||||
override fun forEachVariableWithPos(block: (String, Pos) -> Unit) {
|
||||
block(name, atPos)
|
||||
}
|
||||
// Per-frame slot cache to avoid repeated name lookups
|
||||
private var cachedFrameId: Long = 0L
|
||||
private var cachedSlot: Int = -1
|
||||
@@ -1231,7 +1254,6 @@ class LocalVarRef(private val name: String, private val atPos: Pos) : ObjRef {
|
||||
|
||||
override suspend fun get(scope: Scope): ObjRecord {
|
||||
scope.pos = atPos
|
||||
// 1) Try fast slot/local
|
||||
if (!PerfFlags.LOCAL_SLOT_PIC) {
|
||||
scope.getSlotIndexOf(name)?.let {
|
||||
if (PerfFlags.PIC_DEBUG_COUNTERS) PerfStats.localVarPicHit++
|
||||
@@ -1266,10 +1288,15 @@ class LocalVarRef(private val name: String, private val atPos: Pos) : ObjRef {
|
||||
val hit = (cachedFrameId == scope.frameId && cachedSlot >= 0 && cachedSlot < scope.slotCount())
|
||||
val slot = if (hit) cachedSlot else resolveSlot(scope)
|
||||
if (slot >= 0) {
|
||||
if (PerfFlags.PIC_DEBUG_COUNTERS) {
|
||||
if (hit) PerfStats.localVarPicHit++ else PerfStats.localVarPicMiss++
|
||||
val rec = scope.getSlotRecord(slot)
|
||||
if (rec.declaringClass != null && !canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
|
||||
// Not visible via slot, fallback to other lookups
|
||||
} else {
|
||||
if (PerfFlags.PIC_DEBUG_COUNTERS) {
|
||||
if (hit) PerfStats.localVarPicHit++ else PerfStats.localVarPicMiss++
|
||||
}
|
||||
return rec
|
||||
}
|
||||
return scope.getSlotRecord(slot)
|
||||
}
|
||||
if (PerfFlags.PIC_DEBUG_COUNTERS) PerfStats.localVarPicMiss++
|
||||
// 2) Fallback name in scope or field on `this`
|
||||
@@ -1321,7 +1348,11 @@ class LocalVarRef(private val name: String, private val atPos: Pos) : ObjRef {
|
||||
}
|
||||
val hit = (cachedFrameId == scope.frameId && cachedSlot >= 0 && cachedSlot < scope.slotCount())
|
||||
val slot = if (hit) cachedSlot else resolveSlot(scope)
|
||||
if (slot >= 0) return scope.getSlotRecord(slot).value
|
||||
if (slot >= 0) {
|
||||
val rec = scope.getSlotRecord(slot)
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx))
|
||||
return rec.value
|
||||
}
|
||||
// Fallback name in scope or field on `this`
|
||||
scope[name]?.let { return it.value }
|
||||
run {
|
||||
@@ -1385,9 +1416,11 @@ class LocalVarRef(private val name: String, private val atPos: Pos) : ObjRef {
|
||||
val slot = if (cachedFrameId == scope.frameId && cachedSlot >= 0 && cachedSlot < scope.slotCount()) cachedSlot else resolveSlot(scope)
|
||||
if (slot >= 0) {
|
||||
val rec = scope.getSlotRecord(slot)
|
||||
if (!rec.isMutable) scope.raiseError("Cannot assign to immutable value")
|
||||
rec.value = newValue
|
||||
return
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
|
||||
if (!rec.isMutable) scope.raiseError("Cannot assign to immutable value")
|
||||
rec.value = newValue
|
||||
return
|
||||
}
|
||||
}
|
||||
scope[name]?.let { stored ->
|
||||
if (stored.isMutable) stored.value = newValue
|
||||
@@ -1429,17 +1462,25 @@ class BoundLocalVarRef(
|
||||
) : ObjRef {
|
||||
override suspend fun get(scope: Scope): ObjRecord {
|
||||
scope.pos = atPos
|
||||
return scope.getSlotRecord(slot)
|
||||
val rec = scope.getSlotRecord(slot)
|
||||
if (rec.declaringClass != null && !canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx))
|
||||
scope.raiseError(ObjAccessException(scope, "private field access"))
|
||||
return rec
|
||||
}
|
||||
|
||||
override suspend fun evalValue(scope: Scope): Obj {
|
||||
scope.pos = atPos
|
||||
return scope.getSlotRecord(slot).value
|
||||
val rec = scope.getSlotRecord(slot)
|
||||
if (rec.declaringClass != null && !canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx))
|
||||
scope.raiseError(ObjAccessException(scope, "private field access"))
|
||||
return rec.value
|
||||
}
|
||||
|
||||
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
|
||||
scope.pos = atPos
|
||||
val rec = scope.getSlotRecord(slot)
|
||||
if (rec.declaringClass != null && !canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx))
|
||||
scope.raiseError(ObjAccessException(scope, "private field access"))
|
||||
if (!rec.isMutable) scope.raiseError("Cannot assign to immutable value")
|
||||
rec.value = newValue
|
||||
}
|
||||
@@ -1450,9 +1491,12 @@ class BoundLocalVarRef(
|
||||
* It resolves the slot once per frame and never falls back to global/module lookup.
|
||||
*/
|
||||
class FastLocalVarRef(
|
||||
private val name: String,
|
||||
val name: String,
|
||||
private val atPos: Pos,
|
||||
) : ObjRef {
|
||||
override fun forEachVariable(block: (String) -> Unit) {
|
||||
block(name)
|
||||
}
|
||||
// Cache the exact scope frame that owns the slot, not just the current frame
|
||||
private var cachedOwnerScope: Scope? = null
|
||||
private var cachedOwnerFrameId: Long = 0L
|
||||
@@ -1500,10 +1544,13 @@ class FastLocalVarRef(
|
||||
val slot = if (ownerValid && cachedSlot >= 0) cachedSlot else resolveSlotInAncestry(scope)
|
||||
val actualOwner = cachedOwnerScope
|
||||
if (slot >= 0 && actualOwner != null) {
|
||||
if (PerfFlags.PIC_DEBUG_COUNTERS) {
|
||||
if (ownerValid) PerfStats.fastLocalHit++ else PerfStats.fastLocalMiss++
|
||||
val rec = actualOwner.getSlotRecord(slot)
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
|
||||
if (PerfFlags.PIC_DEBUG_COUNTERS) {
|
||||
if (ownerValid) PerfStats.fastLocalHit++ else PerfStats.fastLocalMiss++
|
||||
}
|
||||
return rec
|
||||
}
|
||||
return actualOwner.getSlotRecord(slot)
|
||||
}
|
||||
// Try per-frame local binding maps in the ancestry first (locals declared in frames)
|
||||
run {
|
||||
@@ -1540,7 +1587,11 @@ class FastLocalVarRef(
|
||||
val ownerValid = isOwnerValidFor(scope)
|
||||
val slot = if (ownerValid && cachedSlot >= 0) cachedSlot else resolveSlotInAncestry(scope)
|
||||
val actualOwner = cachedOwnerScope
|
||||
if (slot >= 0 && actualOwner != null) return actualOwner.getSlotRecord(slot).value
|
||||
if (slot >= 0 && actualOwner != null) {
|
||||
val rec = actualOwner.getSlotRecord(slot)
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx))
|
||||
return rec.value
|
||||
}
|
||||
// Try per-frame local binding maps in the ancestry first
|
||||
run {
|
||||
var s: Scope? = scope
|
||||
@@ -1577,9 +1628,11 @@ class FastLocalVarRef(
|
||||
val actualOwner = cachedOwnerScope
|
||||
if (slot >= 0 && actualOwner != null) {
|
||||
val rec = actualOwner.getSlotRecord(slot)
|
||||
if (!rec.isMutable) scope.raiseError("Cannot assign to immutable value")
|
||||
rec.value = newValue
|
||||
return
|
||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, scope.currentClassCtx)) {
|
||||
if (!rec.isMutable) scope.raiseError("Cannot assign to immutable value")
|
||||
rec.value = newValue
|
||||
return
|
||||
}
|
||||
}
|
||||
// Try per-frame local binding maps in the ancestry first
|
||||
run {
|
||||
@@ -1610,6 +1663,24 @@ class FastLocalVarRef(
|
||||
}
|
||||
|
||||
class ListLiteralRef(private val entries: List<ListEntry>) : ObjRef {
|
||||
override fun forEachVariable(block: (String) -> Unit) {
|
||||
for (e in entries) {
|
||||
when (e) {
|
||||
is ListEntry.Element -> e.ref.forEachVariable(block)
|
||||
is ListEntry.Spread -> e.ref.forEachVariable(block)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun forEachVariableWithPos(block: (String, Pos) -> Unit) {
|
||||
for (e in entries) {
|
||||
when (e) {
|
||||
is ListEntry.Element -> e.ref.forEachVariableWithPos(block)
|
||||
is ListEntry.Spread -> e.ref.forEachVariableWithPos(block)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun get(scope: Scope): ObjRecord {
|
||||
// Heuristic capacity hint: count element entries; spreads handled opportunistically
|
||||
val elemCount = entries.count { it is ListEntry.Element }
|
||||
@@ -1625,7 +1696,7 @@ class ListLiteralRef(private val entries: List<ListEntry>) : ObjRef {
|
||||
when (elements) {
|
||||
is ObjList -> {
|
||||
// Grow underlying array once when possible
|
||||
if (list is ArrayList) list.ensureCapacity(list.size + elements.list.size)
|
||||
list.ensureCapacity(list.size + elements.list.size)
|
||||
list.addAll(elements.list)
|
||||
}
|
||||
else -> scope.raiseError("Spread element must be list")
|
||||
@@ -1633,7 +1704,54 @@ class ListLiteralRef(private val entries: List<ListEntry>) : ObjRef {
|
||||
}
|
||||
}
|
||||
}
|
||||
return ObjList(list).asReadonly
|
||||
return ObjList(list).asMutable
|
||||
}
|
||||
|
||||
override suspend fun setAt(pos: Pos, scope: Scope, newValue: Obj) {
|
||||
val sourceList = (newValue as? ObjList)?.list
|
||||
?: throw ScriptError(pos, "destructuring assignment requires a list on the right side")
|
||||
|
||||
val ellipsisIdx = entries.indexOfFirst { it is ListEntry.Spread }
|
||||
if (entries.count { it is ListEntry.Spread } > 1) {
|
||||
throw ScriptError(pos, "destructuring pattern can have only one splat")
|
||||
}
|
||||
|
||||
if (ellipsisIdx < 0) {
|
||||
if (sourceList.size < entries.size)
|
||||
throw ScriptError(pos, "too few elements for destructuring")
|
||||
for (i in entries.indices) {
|
||||
val entry = entries[i]
|
||||
if (entry is ListEntry.Element) {
|
||||
entry.ref.setAt(pos, scope, sourceList[i])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val headCount = ellipsisIdx
|
||||
val tailCount = entries.size - ellipsisIdx - 1
|
||||
if (sourceList.size < headCount + tailCount)
|
||||
throw ScriptError(pos, "too few elements for destructuring")
|
||||
|
||||
// head
|
||||
for (i in 0 until headCount) {
|
||||
val entry = entries[i]
|
||||
if (entry is ListEntry.Element) {
|
||||
entry.ref.setAt(pos, scope, sourceList[i])
|
||||
}
|
||||
}
|
||||
|
||||
// tail
|
||||
for (i in 0 until tailCount) {
|
||||
val entry = entries[entries.size - 1 - i]
|
||||
if (entry is ListEntry.Element) {
|
||||
entry.ref.setAt(pos, scope, sourceList[sourceList.size - 1 - i])
|
||||
}
|
||||
}
|
||||
|
||||
// ellipsis
|
||||
val spreadEntry = entries[ellipsisIdx] as ListEntry.Spread
|
||||
val spreadList = sourceList.subList(headCount, sourceList.size - tailCount)
|
||||
spreadEntry.ref.setAt(pos, scope, ObjList(spreadList.toMutableList()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1656,10 +1774,8 @@ class MapLiteralRef(private val entries: List<MapLiteralEntry>) : ObjRef {
|
||||
is MapLiteralEntry.Spread -> {
|
||||
val m = if (PerfFlags.RVAL_FASTPATH) e.ref.evalValue(scope) else e.ref.get(scope).value
|
||||
if (m !is ObjMap) scope.raiseIllegalArgument("spread element in map literal must be a Map")
|
||||
// Enforce string keys for map literals
|
||||
for ((k, v) in m.map) {
|
||||
val sKey = k as? ObjString ?: scope.raiseIllegalArgument("spread map must have string keys; got $k")
|
||||
result.map[sKey] = v
|
||||
result.map[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1691,10 +1807,16 @@ class AssignRef(
|
||||
) : ObjRef {
|
||||
override suspend fun get(scope: Scope): ObjRecord {
|
||||
val v = if (PerfFlags.RVAL_FASTPATH) value.evalValue(scope) else value.get(scope).value
|
||||
val rec = target.get(scope)
|
||||
if (!rec.isMutable) throw ScriptError(atPos, "cannot assign to immutable variable")
|
||||
if (rec.value.assign(scope, v) == null) {
|
||||
target.setAt(atPos, scope, v)
|
||||
// For properties, we should not call get() on target because it invokes the getter.
|
||||
// Instead, we call setAt directly.
|
||||
if (target is FieldRef || target is IndexRef || target is LocalVarRef || target is FastLocalVarRef || target is BoundLocalVarRef) {
|
||||
target.setAt(atPos, scope, v)
|
||||
} else {
|
||||
val rec = target.get(scope)
|
||||
if (!rec.isMutable) throw ScriptError(atPos, "cannot assign to immutable variable")
|
||||
if (rec.value.assign(scope, v) == null) {
|
||||
target.setAt(atPos, scope, v)
|
||||
}
|
||||
}
|
||||
return v.asReadonly
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ class ObjRegexMatch(val match: MatchResult) : Obj() {
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun toString(scope: Scope,calledFromLyng: Boolean): ObjString {
|
||||
override suspend fun defaultToString(scope: Scope): ObjString {
|
||||
return ObjString("RegexMath(${objRange.toString(scope)},${objGroups.toString(scope)})")
|
||||
}
|
||||
|
||||
|
||||
@@ -122,6 +122,12 @@ class ObjRingBuffer(val capacity: Int) : Obj() {
|
||||
returns = type("lyng.Void"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisAs<ObjRingBuffer>().apply { buffer.add(requireOnlyArg<Obj>()) } }
|
||||
addFnDoc(
|
||||
name = "first",
|
||||
doc = "Return the oldest element in the buffer.",
|
||||
returns = type("lyng.Any"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { thisAs<ObjRingBuffer>().buffer.first() }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,12 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
|
||||
return set.contains(other)
|
||||
}
|
||||
|
||||
override suspend fun enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
|
||||
for (item in set) {
|
||||
if (!callback(item)) break
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun plus(scope: Scope, other: Obj): Obj {
|
||||
return ObjSet(
|
||||
if (other is ObjSet)
|
||||
|
||||
@@ -134,7 +134,7 @@ data class ObjString(val value: String) : Obj() {
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjInt(
|
||||
ObjInt.of(
|
||||
thisAs<ObjString>().value.toLongOrNull()
|
||||
?: raiseIllegalArgument("can't convert to int: $thisObj")
|
||||
)
|
||||
@@ -159,7 +159,7 @@ data class ObjString(val value: String) : Obj() {
|
||||
}
|
||||
addConstDoc(
|
||||
name = "length",
|
||||
value = statement { ObjInt(thisAs<ObjString>().value.length.toLong()) },
|
||||
value = statement { ObjInt.of(thisAs<ObjString>().value.length.toLong()) },
|
||||
doc = "Number of UTF-16 code units in this string.",
|
||||
type = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
@@ -216,6 +216,14 @@ data class ObjString(val value: String) : Obj() {
|
||||
) {
|
||||
thisAs<ObjString>().value.lowercase().let(::ObjString)
|
||||
}
|
||||
addFnDoc(
|
||||
name = "lowercase",
|
||||
doc = "Lowercase version of this string (default locale).",
|
||||
returns = type("lyng.String"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjString>().value.lowercase().let(::ObjString)
|
||||
}
|
||||
addFnDoc(
|
||||
name = "upper",
|
||||
doc = "Uppercase version of this string (default locale).",
|
||||
@@ -224,6 +232,14 @@ data class ObjString(val value: String) : Obj() {
|
||||
) {
|
||||
thisAs<ObjString>().value.uppercase().let(::ObjString)
|
||||
}
|
||||
addFnDoc(
|
||||
name = "uppercase",
|
||||
doc = "Uppercase version of this string (default locale).",
|
||||
returns = type("lyng.String"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
thisAs<ObjString>().value.uppercase().let(::ObjString)
|
||||
}
|
||||
addFnDoc(
|
||||
name = "characters",
|
||||
doc = "List of characters of this string.",
|
||||
@@ -253,14 +269,14 @@ data class ObjString(val value: String) : Obj() {
|
||||
doc = "Alias for length: the number of characters (code units) in this string.",
|
||||
returns = type("lyng.Int"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) { ObjInt(thisAs<ObjString>().value.length.toLong()) }
|
||||
) { ObjInt.of(thisAs<ObjString>().value.length.toLong()) }
|
||||
addFnDoc(
|
||||
name = "toReal",
|
||||
doc = "Parse this string as a real number (floating point).",
|
||||
returns = type("lyng.Real"),
|
||||
moduleName = "lyng.stdlib"
|
||||
) {
|
||||
ObjReal(thisAs<ObjString>().value.toDouble())
|
||||
ObjReal.of(thisAs<ObjString>().value.toDouble())
|
||||
}
|
||||
addFnDoc(
|
||||
name = "trim",
|
||||
|
||||
@@ -47,7 +47,8 @@ abstract class Statement(
|
||||
|
||||
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||
if( other == ObjNull || other == ObjVoid ) return 1
|
||||
throw UnsupportedOperationException("not comparable")
|
||||
if( other === this ) return 0
|
||||
return -1
|
||||
}
|
||||
|
||||
override suspend fun callOn(scope: Scope): Obj {
|
||||
|
||||
@@ -82,9 +82,17 @@ open class LynonDecoder(val bin: BitInput, val settings: LynonSettings = LynonSe
|
||||
if (it !is ObjClass)
|
||||
scope.raiseClassCastError("Expected obj class but got ${it::class.simpleName}")
|
||||
it
|
||||
} ?: scope.raiseSymbolNotFound("can't deserialize: not found type $className")
|
||||
} ?: run {
|
||||
// Use Scope API that mirrors compiler-emitted ObjRef chain for qualified identifiers
|
||||
val evaluated = scope.resolveQualifiedIdentifier(className.value)
|
||||
if (evaluated !is ObjClass)
|
||||
scope.raiseClassCastError("Expected obj class but got ${evaluated::class.simpleName}")
|
||||
evaluated
|
||||
}
|
||||
}
|
||||
|
||||
// helper moved to Scope as resolveQualifiedIdentifier
|
||||
|
||||
suspend fun decodeAnyList(scope: Scope, fixedSize: Int? = null): MutableList<Obj> {
|
||||
return if (bin.getBit() == 1) {
|
||||
// homogenous
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
package net.sergeych.tools
|
||||
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlin.time.Clock
|
||||
|
||||
inline fun bm(text: String="", f: ()->Unit) {
|
||||
val start = Clock.System.now()
|
||||
|
||||
@@ -24,7 +24,6 @@ import net.sergeych.lyng.ExecutionError
|
||||
import net.sergeych.lyng.ScriptError
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
class MapLiteralTest {
|
||||
@@ -119,20 +118,21 @@ class MapLiteralTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun spreadNonStringKeysIsRuntimeError() = runTest {
|
||||
assertFailsWith<ExecutionError> {
|
||||
eval("""{ ...Map(1 => "x") }""")
|
||||
}
|
||||
fun spreadNonStringKeysIsAllowed() = runTest {
|
||||
eval("""
|
||||
val m = { ...Map(1 => "x") }
|
||||
assertEquals("x", m[1])
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun mergeNonStringKeyIsRuntimeError() = runTest {
|
||||
assertFailsWith<ExecutionError> {
|
||||
eval("""
|
||||
val e = (1 => "x")
|
||||
{ "a": 1 } + e
|
||||
""")
|
||||
}
|
||||
fun mergeNonStringKeyIsAllowed() = runTest {
|
||||
eval("""
|
||||
val e = (1 => "x")
|
||||
val m = { "a": 1 } + e
|
||||
assertEquals(1, m["a"])
|
||||
assertEquals("x", m[1])
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,13 +16,19 @@
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.Script
|
||||
import net.sergeych.lyng.eval
|
||||
import net.sergeych.lyng.obj.ObjInstance
|
||||
import net.sergeych.lyng.obj.ObjList
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
|
||||
class OOTest {
|
||||
@Test
|
||||
fun testClassProps() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
import lyng.time
|
||||
|
||||
class Point(x,y) {
|
||||
@@ -35,11 +41,14 @@ class OOTest {
|
||||
assertEquals(Point(0,0), Point.origin)
|
||||
assertEquals(Point(1,2), Point.center)
|
||||
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClassMethods() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
import lyng.time
|
||||
|
||||
class Point(x,y) {
|
||||
@@ -58,12 +67,14 @@ class OOTest {
|
||||
assertEquals(null, Point.getData() )
|
||||
Point.setData("foo")
|
||||
assertEquals( "foo!", Point.getData() )
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDynamicGet() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
val accessor = dynamic {
|
||||
get { name ->
|
||||
if( name == "foo" ) "bar" else null
|
||||
@@ -74,12 +85,14 @@ class OOTest {
|
||||
assertEquals("bar", accessor.foo)
|
||||
assertEquals(null, accessor.bar)
|
||||
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelegateSet() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
var setValueForBar = null
|
||||
val accessor = dynamic {
|
||||
get { name ->
|
||||
@@ -104,12 +117,14 @@ class OOTest {
|
||||
assertThrows {
|
||||
accessor.bad = "!23"
|
||||
}
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDynamicIndexAccess() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
val store = Map()
|
||||
val accessor = dynamic {
|
||||
get { name ->
|
||||
@@ -124,23 +139,27 @@ class OOTest {
|
||||
accessor["foo"] = "bar"
|
||||
assertEquals("bar", accessor["foo"])
|
||||
assertEquals("bar", accessor.foo)
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMultilineConstructor() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
class Point(
|
||||
x,
|
||||
y
|
||||
)
|
||||
assertEquals(Point(1,2), Point(1,2) )
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDynamicClass() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
|
||||
fun getContract(contractName) {
|
||||
dynamic {
|
||||
@@ -150,14 +169,16 @@ class OOTest {
|
||||
}
|
||||
}
|
||||
getContract("foo").bar
|
||||
""")
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDynamicClassReturn2() = runTest {
|
||||
// todo: should work without extra parenthesis
|
||||
// see below
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
|
||||
fun getContract(contractName) {
|
||||
println("1")
|
||||
@@ -184,6 +205,195 @@ class OOTest {
|
||||
assertEquals(6, x(1,2,3))
|
||||
// v HERE v
|
||||
assertEquals(15, cc.foo.bar(10,2,3))
|
||||
""")
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClassInitialization() = runTest {
|
||||
eval(
|
||||
"""
|
||||
var countInstances = 0
|
||||
class Point(val x: Int, val y: Int) {
|
||||
println("Class initializer is called 1")
|
||||
var magnitude
|
||||
|
||||
/*
|
||||
init {} section optionally provide initialization code that is called on each instance creation.
|
||||
it should have the same access to this.* and constructor parameters as any other member
|
||||
function.
|
||||
*/
|
||||
init {
|
||||
countInstances++
|
||||
magnitude = Math.sqrt(x*x + y*y)
|
||||
}
|
||||
}
|
||||
|
||||
val p = Point(1, 2)
|
||||
assertEquals(1, countInstances)
|
||||
assertEquals(p, Point(1,2) )
|
||||
assertEquals(2, countInstances)
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMIInitialization() = runTest {
|
||||
eval(
|
||||
"""
|
||||
var order = []
|
||||
class A {
|
||||
init { order.add("A") }
|
||||
}
|
||||
class B : A {
|
||||
init { order.add("B") }
|
||||
}
|
||||
class C {
|
||||
init { order.add("C") }
|
||||
}
|
||||
class D : B, C {
|
||||
init { order.add("D") }
|
||||
}
|
||||
D()
|
||||
assertEquals(["A", "B", "C", "D"], order)
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMIDiamondInitialization() = runTest {
|
||||
eval(
|
||||
"""
|
||||
var order = []
|
||||
class A {
|
||||
init { order.add("A") }
|
||||
}
|
||||
class B : A {
|
||||
init { order.add("B") }
|
||||
}
|
||||
class C : A {
|
||||
init { order.add("C") }
|
||||
}
|
||||
class D : B, C {
|
||||
init { order.add("D") }
|
||||
}
|
||||
D()
|
||||
assertEquals(["A", "B", "C", "D"], order)
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitBlockInDeserialization() = runTest {
|
||||
eval(
|
||||
"""
|
||||
import lyng.serialization
|
||||
var count = 0
|
||||
class A {
|
||||
init { count++ }
|
||||
}
|
||||
val a1 = A()
|
||||
val coded = Lynon.encode(a1)
|
||||
val a2 = Lynon.decode(coded)
|
||||
assertEquals(2, count)
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDefaultCompare() = runTest {
|
||||
eval(
|
||||
"""
|
||||
class Point(val x: Int, val y: Int)
|
||||
|
||||
assertEquals(Point(1,2), Point(1,2) )
|
||||
assert( Point(1,2) != Point(2,1) )
|
||||
assert( Point(1,2) == Point(1,2) )
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConstructorCallsWithNamedParams() = runTest {
|
||||
val scope = Script.newScope()
|
||||
val list = scope.eval(
|
||||
"""
|
||||
import lyng.time
|
||||
|
||||
class BarRequest(
|
||||
id,
|
||||
vaultId, userAddress, isDepositRequest, grossWeight, fineness, notes="",
|
||||
createdAt = Instant.now().truncateToSecond(),
|
||||
updatedAt = Instant.now().truncateToSecond()
|
||||
) {
|
||||
// unrelated for comparison
|
||||
static val stateNames = [1, 2, 3]
|
||||
|
||||
val cell = cached { Cell[id] }
|
||||
}
|
||||
assertEquals( 5,5.toInt())
|
||||
val b1 = BarRequest(1, "v1", "u1", true, 1000, 999)
|
||||
val b2 = BarRequest(1, "v1", "u1", true, 1000, 999, createdAt: b1.createdAt, updatedAt: b1.updatedAt)
|
||||
assertEquals(b1, b2)
|
||||
assertEquals( 0, b1 <=> b2)
|
||||
[b1, b2]
|
||||
""".trimIndent()
|
||||
) as ObjList
|
||||
val b1 = list.list[0] as ObjInstance
|
||||
val b2 = list.list[1] as ObjInstance
|
||||
assertEquals(0, b1.compareTo(scope, b2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPropAsExtension() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval("""
|
||||
class A(x) {
|
||||
private val privateVal = 100
|
||||
val p1 get() = x + 1
|
||||
}
|
||||
assertEquals(2, A(1).p1)
|
||||
|
||||
fun A.f() = x + 5
|
||||
assertEquals(7, A(2).f())
|
||||
|
||||
// The same, we should be able to add member values to a class;
|
||||
// notice it should access to the class public instance members,
|
||||
// somewhat like it is declared in the class body
|
||||
val A.simple = x + 3
|
||||
|
||||
assertEquals(5, A(2).simple)
|
||||
|
||||
// it should also work with properties:
|
||||
val A.p10 get() = x * 10
|
||||
assertEquals(20, A(2).p10)
|
||||
""".trimIndent())
|
||||
|
||||
// important is that such extensions should not be able to access private members
|
||||
// and thus remove privateness:
|
||||
assertFails {
|
||||
scope.eval("val A.exportPrivateVal = privateVal; A(1).exportPrivateVal")
|
||||
}
|
||||
assertFails {
|
||||
scope.eval("val A.exportPrivateValProp get() = privateVal; A(1).exportPrivateValProp")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testExtensionsAreScopeIsolated() = runTest {
|
||||
val scope1 = Script.newScope()
|
||||
scope1.eval("""
|
||||
val String.totalDigits get() {
|
||||
// notice using `this`:
|
||||
this.characters.filter{ it.isDigit() }.size()
|
||||
}
|
||||
assertEquals(2, "answer is 42".totalDigits)
|
||||
""")
|
||||
val scope2 = Script.newScope()
|
||||
scope2.eval("""
|
||||
// in scope2 we didn't override `totalDigits` extension:
|
||||
assertThrows { "answer is 42".totalDigits }
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2025 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.
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import net.sergeych.lyng.PerfFlags
|
||||
import net.sergeych.lyng.eval
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ScopePoolingRegressionTest {
|
||||
|
||||
@Test
|
||||
fun testPooledScopeInstance() = runTest {
|
||||
val saved = PerfFlags.SCOPE_POOL
|
||||
PerfFlags.SCOPE_POOL = true
|
||||
try {
|
||||
val result = eval("""
|
||||
class A {
|
||||
fun test() {
|
||||
// println is a global function
|
||||
println("Calling println from A")
|
||||
"method ok"
|
||||
}
|
||||
}
|
||||
|
||||
// Use a transient scope (lambda) to create the instance
|
||||
val creator = { A() }
|
||||
val a = creator()
|
||||
|
||||
// Re-use the pool to ensure the scope used above is reset/repurposed
|
||||
val other = { 1 + 2 }
|
||||
other()
|
||||
other()
|
||||
|
||||
// Now call method on 'a'. It should still find global 'println'
|
||||
a.test()
|
||||
""".trimIndent())
|
||||
assertEquals("method ok", result.toString())
|
||||
} finally {
|
||||
PerfFlags.SCOPE_POOL = saved
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,8 +23,6 @@ import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
@@ -36,7 +34,9 @@ import net.sergeych.lyng.pacman.InlineSourcesImportProvider
|
||||
import net.sergeych.mp_tools.globalDefer
|
||||
import net.sergeych.tools.bm
|
||||
import kotlin.test.*
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.Instant
|
||||
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
@@ -2318,29 +2318,33 @@ class ScriptTest {
|
||||
@Test
|
||||
fun doWhileValuesLabelTest() = runTest {
|
||||
withTimeout(5.seconds) {
|
||||
eval(
|
||||
"""
|
||||
var count = 0
|
||||
var count2 = 0
|
||||
var count3 = 0
|
||||
val result = outer@ do {
|
||||
count2++
|
||||
count = 0
|
||||
do {
|
||||
count++
|
||||
if( count < 10 || count2 < 5 ) {
|
||||
continue
|
||||
}
|
||||
if( count % 2 == 1 )
|
||||
break@outer "found "+count + "/" + count2
|
||||
} while(count < 14)
|
||||
count3++
|
||||
} while( count2 < 100 )
|
||||
else "no"
|
||||
assertEquals("found 11/5", result)
|
||||
assertEquals( 4, count3)
|
||||
""".trimIndent()
|
||||
)
|
||||
try {
|
||||
eval(
|
||||
"""
|
||||
var count = 0
|
||||
var count2 = 0
|
||||
var count3 = 0
|
||||
val result = outer@ do {
|
||||
count2++
|
||||
count = 0
|
||||
do {
|
||||
count++
|
||||
if( count < 10 || count2 < 5 ) {
|
||||
continue
|
||||
}
|
||||
if( count % 2 == 1 )
|
||||
break@outer "found "+count + "/" + count2
|
||||
} while(count < 14)
|
||||
count3++
|
||||
} while( count2 < 100 )
|
||||
else "no"
|
||||
assertEquals("found 11/5", result)
|
||||
assertEquals( 4, count3)
|
||||
""".trimIndent()
|
||||
)
|
||||
} catch (e: ExecutionError) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3020,6 +3024,37 @@ class ScriptTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMapWithNonStringKeys() = runTest {
|
||||
eval(
|
||||
"""
|
||||
val map = Map( 1 => "one", 2 => "two" )
|
||||
assertEquals( "one", map[1] )
|
||||
assertEquals( "two", map[2] )
|
||||
assertEquals( null, map[3] )
|
||||
map[3] = "three"
|
||||
assertEquals( "three", map[3] )
|
||||
map += (4 => "four")
|
||||
assertEquals( "four", map[4] )
|
||||
|
||||
// Test toMap()
|
||||
val map2 = [1 => "a", 2 => "b"].toMap()
|
||||
assertEquals("a", map2[1])
|
||||
assertEquals("b", map2[2])
|
||||
|
||||
// Test Map constructor with mixed entries and arrays
|
||||
val map3 = Map( 1 => "a", [2, "b"] )
|
||||
assertEquals("a", map3[1])
|
||||
assertEquals("b", map3[2])
|
||||
|
||||
// Test plus
|
||||
val map4 = map3 + (3 => "c")
|
||||
assertEquals("c", map4[3])
|
||||
assertEquals("a", map4[1])
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testBuffer() = runTest {
|
||||
eval(
|
||||
@@ -3662,7 +3697,7 @@ class ScriptTest {
|
||||
}
|
||||
|
||||
|
||||
// @Test
|
||||
// @Test
|
||||
fun testMinimumOptimization() = runTest {
|
||||
for (i in 1..200) {
|
||||
bm {
|
||||
@@ -3919,13 +3954,15 @@ class ScriptTest {
|
||||
@Test
|
||||
fun testJsonTime() = runTest {
|
||||
val now = Clock.System.now()
|
||||
val x = eval("""
|
||||
val x = eval(
|
||||
"""
|
||||
import lyng.time
|
||||
Instant.now().truncateToSecond()
|
||||
""".trimIndent()).decodeSerializable<Instant>()
|
||||
""".trimIndent()
|
||||
).decodeSerializable<Instant>()
|
||||
println(x)
|
||||
assertIs<Instant>(x)
|
||||
assertTrue( (now - x).absoluteValue < 2.seconds)
|
||||
assertTrue((now - x).absoluteValue < 2.seconds)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4038,15 +4075,17 @@ class ScriptTest {
|
||||
@Serializable
|
||||
data class TestJson2(
|
||||
val value: Int,
|
||||
val inner: Map<String,Int>
|
||||
val inner: Map<String, Int>
|
||||
)
|
||||
|
||||
@Test
|
||||
fun deserializeMapWithJsonTest() = runTest {
|
||||
val x = eval("""
|
||||
val x = eval(
|
||||
"""
|
||||
import lyng.serialization
|
||||
{ value: 1, inner: { "foo": 1, "bar": 2 }}
|
||||
""".trimIndent()).decodeSerializable<TestJson2>()
|
||||
""".trimIndent()
|
||||
).decodeSerializable<TestJson2>()
|
||||
assertEquals(TestJson2(1, mapOf("foo" to 1, "bar" to 2)), x)
|
||||
}
|
||||
|
||||
@@ -4055,42 +4094,56 @@ class ScriptTest {
|
||||
val value: Int,
|
||||
val inner: JsonObject
|
||||
)
|
||||
|
||||
@Test
|
||||
fun deserializeAnyMapWithJsonTest() = runTest {
|
||||
val x = eval("""
|
||||
val x = eval(
|
||||
"""
|
||||
import lyng.serialization
|
||||
{ value: 12, inner: { "foo": 1, "bar": "two" }}
|
||||
""".trimIndent()).decodeSerializable<TestJson3>()
|
||||
assertEquals(TestJson3(12, JsonObject(mapOf("foo" to JsonPrimitive(1), "bar" to Json.encodeToJsonElement("two")))), x)
|
||||
""".trimIndent()
|
||||
).decodeSerializable<TestJson3>()
|
||||
assertEquals(
|
||||
TestJson3(
|
||||
12,
|
||||
JsonObject(mapOf("foo" to JsonPrimitive(1), "bar" to Json.encodeToJsonElement("two")))
|
||||
), x
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
enum class TestEnum {
|
||||
One, Two
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class TestJson4(val value: TestEnum)
|
||||
|
||||
@Test
|
||||
fun deserializeEnumJsonTest() = runTest {
|
||||
val x = eval("""
|
||||
val x = eval(
|
||||
"""
|
||||
import lyng.serialization
|
||||
enum TestEnum { One, Two }
|
||||
{ value: TestEnum.One }
|
||||
""".trimIndent()).decodeSerializable<TestJson4>()
|
||||
assertEquals( TestJson4(TestEnum.One), x)
|
||||
""".trimIndent()
|
||||
).decodeSerializable<TestJson4>()
|
||||
assertEquals(TestJson4(TestEnum.One), x)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStringLast() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
assertEquals('t', "assert".last())
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLogicalNot() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
val vf = false
|
||||
fun f() { false }
|
||||
assert( !false )
|
||||
@@ -4127,7 +4180,8 @@ class ScriptTest {
|
||||
|
||||
@Test
|
||||
fun testHangOnPrintlnInMethods() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
class T(someList) {
|
||||
fun f() {
|
||||
val x = [...someList]
|
||||
@@ -4135,12 +4189,14 @@ class ScriptTest {
|
||||
}
|
||||
}
|
||||
T([1,2]).f()
|
||||
""")
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHangOnNonexistingMethod() = runTest {
|
||||
eval("""
|
||||
eval(
|
||||
"""
|
||||
class T(someList) {
|
||||
fun f() {
|
||||
nonExistingMethod()
|
||||
@@ -4156,12 +4212,14 @@ class ScriptTest {
|
||||
println(t::class)
|
||||
// ok
|
||||
}
|
||||
""")
|
||||
"""
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUsingClassConstructorVars() = runTest {
|
||||
val r = eval("""
|
||||
val r = eval(
|
||||
"""
|
||||
import lyng.time
|
||||
|
||||
class Request {
|
||||
@@ -4185,7 +4243,8 @@ class ScriptTest {
|
||||
}
|
||||
|
||||
test()
|
||||
""".trimIndent()).toJson()
|
||||
""".trimIndent()
|
||||
).toJson()
|
||||
println(r)
|
||||
}
|
||||
|
||||
@@ -4193,7 +4252,8 @@ class ScriptTest {
|
||||
fun testScopeShortCircuit() = runTest() {
|
||||
val baseScope = Script.newScope()
|
||||
|
||||
baseScope.eval("""
|
||||
baseScope.eval(
|
||||
"""
|
||||
val exports = Map()
|
||||
fun Export(name,f) {
|
||||
exports[name] = f
|
||||
@@ -4204,7 +4264,8 @@ class ScriptTest {
|
||||
|
||||
val exports: MutableMap<Obj, Obj> = (baseScope.eval("exports") as ObjMap).map
|
||||
|
||||
baseScope.eval("""
|
||||
baseScope.eval(
|
||||
"""
|
||||
class A(val a) {
|
||||
fun methodA() {
|
||||
a + 1
|
||||
@@ -4221,24 +4282,254 @@ class ScriptTest {
|
||||
fun exportedFunction(x) {
|
||||
someFunction(x)
|
||||
}
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
// Calling from the script is ok:
|
||||
val instanceScope = baseScope.createChildScope()
|
||||
instanceScope.eval("""
|
||||
instanceScope.eval(
|
||||
"""
|
||||
val a1 = a0 + 1
|
||||
""".trimIndent())
|
||||
assertEquals( ObjInt(2), instanceScope.eval("""
|
||||
""".trimIndent()
|
||||
)
|
||||
assertEquals(
|
||||
ObjInt(2), instanceScope.eval(
|
||||
"""
|
||||
exportedFunction(1)
|
||||
"""))
|
||||
assertEquals( ObjInt(103), instanceScope.eval("""
|
||||
"""
|
||||
)
|
||||
)
|
||||
assertEquals(
|
||||
ObjInt(103), instanceScope.eval(
|
||||
"""
|
||||
exportedFunction(a1 + 1)
|
||||
"""))
|
||||
"""
|
||||
)
|
||||
)
|
||||
val dummyThis = Obj()
|
||||
// but we should be able to call it directly
|
||||
val otherScope = baseScope.createChildScope()
|
||||
val r = (exports["exportedFunction".toObj()] as Statement).invoke(otherScope, dummyThis,ObjInt(50))
|
||||
val r = (exports["exportedFunction".toObj()] as Statement).invoke(otherScope, dummyThis, ObjInt(50))
|
||||
println(r)
|
||||
assertEquals(51, r.toInt())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFirstInEnum() = runTest {
|
||||
eval(
|
||||
"""
|
||||
enum E {
|
||||
one, two, three
|
||||
}
|
||||
println(E.entries)
|
||||
assertEquals( E.two, E.entries.findFirst {
|
||||
println(it.name)
|
||||
it.name in ["aaa", "two"]
|
||||
} )
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAutoSplatArgs() = runTest {
|
||||
eval(
|
||||
"""
|
||||
fun tf(x, y, z) {
|
||||
"x=%s, y=%s, z=%s"(x,y,z)
|
||||
}
|
||||
assertEquals(tf(1, 2, 3), "x=1, y=2, z=3")
|
||||
val a = { x: 3, y: 4, z: 5 }
|
||||
assertEquals(tf(...a), "x=3, y=4, z=5")
|
||||
assertEquals(tf(...{ x: 3, y: 4, z: 50 }), "x=3, y=4, z=50")
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCached() = runTest {
|
||||
eval(
|
||||
"""
|
||||
var counter = 0
|
||||
val f = cached { ++counter }
|
||||
|
||||
assertEquals(1,f())
|
||||
assertEquals(1, counter)
|
||||
assertEquals(1,f())
|
||||
assertEquals(1, counter)
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCustomToStringBug() = runTest {
|
||||
eval(
|
||||
"""
|
||||
class A(x,y)
|
||||
class B(x,y) {
|
||||
fun toString() {
|
||||
"B(%d,%d)"(x,y)
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals("B(1,2)", B(1,2).toString())
|
||||
assertEquals("A(x=1,y=2)", A(1,2).toString())
|
||||
|
||||
// now tricky part: this _should_ cakk custom toString()
|
||||
assertEquals(":B(1,2)", ":" + B(1,2).toString())
|
||||
// and this must be exactly same:
|
||||
assertEquals(":B(1,2)", ":" + B(1,2))
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testDestructuringAssignment() = runTest {
|
||||
eval(
|
||||
"""
|
||||
val abc = [1, 2, 3]
|
||||
// plain:
|
||||
val [a, b, c] = abc
|
||||
assertEquals( 1, a )
|
||||
assertEquals( 2, b )
|
||||
assertEquals( 3, c )
|
||||
// with splats, receiving into list:
|
||||
val [ab..., c1] = abc
|
||||
assertEquals( [1, 2], ab )
|
||||
assertEquals( 3, c1 )
|
||||
// also in the end
|
||||
val [a1, rest...] = abc
|
||||
assertEquals( 1, a1 )
|
||||
assertEquals( [2, 3], rest )
|
||||
// and in the middle
|
||||
val [a_mid, middle..., e0, e1] = [ 1, 2, 3, 4, 5, 6]
|
||||
assertEquals( [2, 3, 4], middle )
|
||||
assertEquals( 5, e0 )
|
||||
assertEquals( 6, e1 )
|
||||
assertEquals( 1, a_mid )
|
||||
|
||||
// nested destructuring:
|
||||
val [n1, [n2, n3...], n4] = [1, [2, 3, 4], 5]
|
||||
assertEquals(1, n1)
|
||||
assertEquals(2, n2)
|
||||
assertEquals([3, 4], n3)
|
||||
assertEquals(5, n4)
|
||||
|
||||
// also it could be used to reassign vars:
|
||||
var x = 5
|
||||
var y = 10
|
||||
|
||||
[x, y] = [y, x]
|
||||
assertEquals( 10, x )
|
||||
assertEquals( 5, y )
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testProperlyReportExceptionPos() = runTest {
|
||||
var x = assertFailsWith<ExecutionError> {
|
||||
eval(
|
||||
"""
|
||||
val tmp = 11
|
||||
|
||||
if( tmp < 100 ) {
|
||||
throw "success"
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
println(x)
|
||||
assertEquals(3, x.pos.line)
|
||||
assertContains(x.message!!, "throw \"success\"")
|
||||
|
||||
// comments shoudl not change reporting:
|
||||
x = assertFailsWith<ExecutionError> {
|
||||
eval(
|
||||
"""
|
||||
val tmp = 11
|
||||
/*
|
||||
This should not change position:
|
||||
*/
|
||||
fun test() {
|
||||
throw "success"
|
||||
}
|
||||
test()
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
println(x)
|
||||
assertEquals(5, x.pos.line)
|
||||
assertContains(x.message!!, "throw \"success\"")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testClassAndFunAutoNamedArgs() = runTest {
|
||||
// Shorthand for named arguments: name: is equivalent to name: name.
|
||||
// This is consistent with map literal shorthand in Lyng.
|
||||
eval(
|
||||
"""
|
||||
fun test(a, b, c) {
|
||||
"%s-%s-%s"(a,b,c)
|
||||
}
|
||||
|
||||
val a = 1
|
||||
val b = 2
|
||||
val c = 3
|
||||
|
||||
// Basic usage:
|
||||
assertEquals( "1-2-3", test(a:, b:, c:) )
|
||||
assertEquals( "1-2-3", test(c:, b:, a:) )
|
||||
|
||||
// Class constructors also support it:
|
||||
class Point(x, y) {
|
||||
val r = "x:%s, y:%s"(x, y)
|
||||
}
|
||||
val x = 10
|
||||
val y = 20
|
||||
assertEquals( "x:10, y:20", Point(x:, y:).r )
|
||||
assertEquals( "x:10, y:20", Point(y:, x:).r )
|
||||
|
||||
// Mixed with positional arguments:
|
||||
assertEquals( "0-2-3", test(0, b:, c:) )
|
||||
|
||||
// Mixed with regular named arguments:
|
||||
assertEquals( "1-99-3", test(a:, b: 99, c:) )
|
||||
|
||||
// Integration with splats (spread arguments):
|
||||
val args = { b:, c: } // map literal shorthand
|
||||
assertEquals( "1-2-3", test(a:, ...args) )
|
||||
|
||||
// Default values:
|
||||
fun sum(a, b=10, c=100) { a + b + c }
|
||||
assertEquals( 111, sum(a:) )
|
||||
assertEquals( 103, sum(a:, b:) )
|
||||
|
||||
// Complex scenario with multiple splats and shorthands:
|
||||
val p1 = 1
|
||||
val p2 = 2
|
||||
val more = { c: 3, d: 4 }
|
||||
fun quad(a, b, c, d) { a + b + c + d }
|
||||
assertEquals( 10, quad(a: p1, b: p2, ...more) )
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFunMiniDeclaration() = runTest {
|
||||
eval("""
|
||||
class T(x) {
|
||||
fun method() = x + 1
|
||||
}
|
||||
fun median(a,b) = (a+b)/2
|
||||
|
||||
assertEquals(11, T(10).method())
|
||||
assertEquals(2, median(1,3))
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -91,4 +91,26 @@ class StdlibTest {
|
||||
assertEquals([1,2], (1..8).dropLast(6).toList() )
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFlattenAndFilter() = runTest {
|
||||
eval("""
|
||||
assertEquals([1,2,3,4,5,6], [1,3,5].map { [it, it+1] }.flatten() )
|
||||
assertEquals([1,3,5], [null,1,null, 3,5].filterNotNull().toList())
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFlatMap() = runTest {
|
||||
eval("""
|
||||
assertEquals([1,2,3,4,5,6], [1,3,5].flatMap { [it,it+1] }.toList() )
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCount() = runTest {
|
||||
eval("""
|
||||
assertEquals(5, (1..10).toList().count { it % 2 == 1 } )
|
||||
""")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.sergeych.lyng
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlin.test.Test
|
||||
|
||||
class PropsTest {
|
||||
|
||||
@Test
|
||||
fun propsProposal() = runTest {
|
||||
eval("""
|
||||
|
||||
class WithProps {
|
||||
|
||||
// readonly property without declared type:
|
||||
val readonlyProp
|
||||
get() {
|
||||
"readonly foo"
|
||||
}
|
||||
|
||||
val readonlyWithType: Int get() { 42 }
|
||||
|
||||
private var field = 0
|
||||
private var field2 = ""
|
||||
|
||||
// with type declaration
|
||||
var propName: Int
|
||||
get() {
|
||||
field * 10
|
||||
}
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
// or without
|
||||
var propNameWithoutType
|
||||
get() {
|
||||
"/"+ field2 + "/"
|
||||
}
|
||||
set(value) {
|
||||
field2 = value
|
||||
}
|
||||
}
|
||||
|
||||
val w = WithProps()
|
||||
assertEquals("readonly foo", w.readonlyProp)
|
||||
assertEquals(42, w.readonlyWithType)
|
||||
|
||||
w.propNameWithoutType = "foo"
|
||||
assertEquals("/foo/", w.propNameWithoutType)
|
||||
|
||||
w.propName = 123
|
||||
assertEquals(1230, w.propName)
|
||||
|
||||
class Shorthand {
|
||||
private var _p = 0
|
||||
var p: Int
|
||||
get() = _p * 2
|
||||
set(v) = _p = v
|
||||
}
|
||||
val s = Shorthand()
|
||||
s.p = 21
|
||||
assertEquals(42, s.p)
|
||||
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
@@ -18,15 +18,15 @@
|
||||
package net.sergeych.lyng
|
||||
|
||||
actual object PerfDefaults {
|
||||
actual val LOCAL_SLOT_PIC: Boolean = true
|
||||
actual val EMIT_FAST_LOCAL_REFS: Boolean = true
|
||||
actual val LOCAL_SLOT_PIC: Boolean = false
|
||||
actual val EMIT_FAST_LOCAL_REFS: Boolean = false
|
||||
|
||||
actual val ARG_BUILDER: Boolean = true
|
||||
actual val SKIP_ARGS_ON_NULL_RECEIVER: Boolean = true
|
||||
actual val SCOPE_POOL: Boolean = true
|
||||
actual val ARG_BUILDER: Boolean = false
|
||||
actual val SKIP_ARGS_ON_NULL_RECEIVER: Boolean = false
|
||||
actual val SCOPE_POOL: Boolean = false
|
||||
|
||||
actual val FIELD_PIC: Boolean = true
|
||||
actual val METHOD_PIC: Boolean = true
|
||||
actual val FIELD_PIC: Boolean = false
|
||||
actual val METHOD_PIC: Boolean = false
|
||||
actual val FIELD_PIC_SIZE_4: Boolean = false
|
||||
actual val METHOD_PIC_SIZE_4: Boolean = false
|
||||
actual val PIC_ADAPTIVE_2_TO_4: Boolean = false
|
||||
@@ -35,8 +35,8 @@ actual object PerfDefaults {
|
||||
|
||||
actual val PIC_DEBUG_COUNTERS: Boolean = false
|
||||
|
||||
actual val PRIMITIVE_FASTOPS: Boolean = true
|
||||
actual val RVAL_FASTPATH: Boolean = true
|
||||
actual val PRIMITIVE_FASTOPS: Boolean = false
|
||||
actual val RVAL_FASTPATH: Boolean = false
|
||||
|
||||
// Regex caching (JVM-first): enabled by default on JVM
|
||||
actual val REGEX_CACHE: Boolean = true
|
||||
|
||||
@@ -18,12 +18,12 @@
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.datetime.Clock
|
||||
import net.sergeych.lyng.Scope
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.extension
|
||||
import kotlin.test.Test
|
||||
import kotlin.time.Clock
|
||||
|
||||
suspend fun executeSampleTests(fileName: String) {
|
||||
val sample = withContext(Dispatchers.IO) {
|
||||
|
||||
+101
-20
@@ -1,8 +1,8 @@
|
||||
package lyng.stdlib
|
||||
|
||||
|
||||
/*
|
||||
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.
|
||||
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.
|
||||
*/
|
||||
fun cached(builder) {
|
||||
var calculated = false
|
||||
@@ -15,18 +15,53 @@ fun cached(builder) {
|
||||
value
|
||||
}
|
||||
}
|
||||
/* Filter elements of this iterable using the provided predicate. */
|
||||
fun Iterable.filter(predicate) {
|
||||
/* Filter elements of this iterable using the provided predicate and provide a flow
|
||||
of results. Coudl be used to map infinte flows, etc.
|
||||
*/
|
||||
fun Iterable.filterFlow(predicate): Flow {
|
||||
val list = this
|
||||
flow {
|
||||
for( item in list ) {
|
||||
if( predicate(item) ) {ln
|
||||
if( predicate(item) ) {
|
||||
emit(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Filter this iterable and return List of elements
|
||||
*/
|
||||
fun Iterable.filter(predicate) {
|
||||
val result = []
|
||||
for( item in this ) if( predicate(item) ) result.add(item)
|
||||
result
|
||||
}
|
||||
|
||||
/*
|
||||
Count all items in this iterable for which predicate return true
|
||||
*/
|
||||
fun Iterable.count(predicate): Int {
|
||||
var hits = 0
|
||||
this.forEach {
|
||||
if( predicate(it) ) hits++
|
||||
}
|
||||
hits
|
||||
}
|
||||
/*
|
||||
filter out all null elements from this collection (Iterable); flow of
|
||||
non-null elements is returned
|
||||
*/
|
||||
fun Iterable.filterFlowNotNull(): Flow {
|
||||
filterFlow { it != null }
|
||||
}
|
||||
|
||||
/* Filter non-null elements and collect them into a List
|
||||
*/
|
||||
fun Iterable.filterNotNull(): List {
|
||||
filter { it != null }
|
||||
}
|
||||
|
||||
/* Skip the first N elements of this iterable. */
|
||||
fun Iterable.drop(n) {
|
||||
var cnt = 0
|
||||
@@ -34,14 +69,38 @@ fun Iterable.drop(n) {
|
||||
}
|
||||
|
||||
/* Return the first element or throw if the iterable is empty. */
|
||||
fun Iterable.first() {
|
||||
val Iterable.first get() {
|
||||
val i = iterator()
|
||||
if( !i.hasNext() ) throw NoSuchElementException()
|
||||
i.next().also { i.cancelIteration() }
|
||||
}
|
||||
|
||||
/*
|
||||
Return the first element that matches the predicate or throws
|
||||
NuSuchElementException
|
||||
*/
|
||||
fun Iterable.findFirst(predicate) {
|
||||
for( x in this ) {
|
||||
if( predicate(x) )
|
||||
break x
|
||||
}
|
||||
else throw NoSuchElementException()
|
||||
}
|
||||
|
||||
/*
|
||||
return the first element matching the predicate or null
|
||||
*/
|
||||
fun Iterable.findFirstOrNull(predicate) {
|
||||
for( x in this ) {
|
||||
if( predicate(x) )
|
||||
break x
|
||||
}
|
||||
else null
|
||||
}
|
||||
|
||||
|
||||
/* Return the last element or throw if the iterable is empty. */
|
||||
fun Iterable.last() {
|
||||
val Iterable.last get() {
|
||||
var found = false
|
||||
var element = null
|
||||
for( i in this ) {
|
||||
@@ -58,7 +117,7 @@ fun Iterable.dropLast(n) {
|
||||
val buffer = RingBuffer(n)
|
||||
flow {
|
||||
for( item in list ) {
|
||||
if( buffer.size == n )
|
||||
if( buffer.size == n )
|
||||
emit( buffer.first() )
|
||||
buffer += item
|
||||
}
|
||||
@@ -72,13 +131,13 @@ fun Iterable.takeLast(n) {
|
||||
buffer
|
||||
}
|
||||
|
||||
/* Join elements into a string with a separator (prefix parameter) and optional transformer. */
|
||||
fun Iterable.joinToString(prefix=" ", transformer=null) {
|
||||
/* Join elements into a string with a separator (separator parameter) and optional transformer. */
|
||||
fun Iterable.joinToString(separator=" ", transformer=null) {
|
||||
var result = null
|
||||
for( part in this ) {
|
||||
val transformed = transformer?(part)?.toString() ?: part.toString()
|
||||
if( result == null ) result = transformed
|
||||
else result += prefix + transformed
|
||||
else result += separator + transformed
|
||||
}
|
||||
result ?: ""
|
||||
}
|
||||
@@ -129,7 +188,7 @@ fun Iterable.minOf( lambda ) {
|
||||
minimum
|
||||
}
|
||||
|
||||
/* Maximum value of the given function applied to elements of the collection. */
|
||||
/* Maximum value of the given function applied to elements of the collection. */
|
||||
fun Iterable.maxOf( lambda ) {
|
||||
val i = iterator()
|
||||
var maximum = lambda( i.next() )
|
||||
@@ -155,6 +214,26 @@ fun Iterable.shuffled() {
|
||||
toList().apply { shuffle() }
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a single list of all elements from all collections in the given collection.
|
||||
@return List
|
||||
*/
|
||||
fun Iterable.flatten() {
|
||||
val result = []
|
||||
forEach { i ->
|
||||
i.forEach { result.add(it) }
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a single list of all elements yielded from results of transform function being
|
||||
invoked on each element of original collection.
|
||||
*/
|
||||
fun Iterable.flatMap(transform): List {
|
||||
map(transform).flatten()
|
||||
}
|
||||
|
||||
/* Return string representation like [a,b,c]. */
|
||||
fun List.toString() {
|
||||
"[" + joinToString(",") + "]"
|
||||
@@ -172,10 +251,10 @@ fun List.sort() {
|
||||
|
||||
/* Represents a single stack trace element. */
|
||||
class StackTraceEntry(
|
||||
val sourceName: String,
|
||||
val line: Int,
|
||||
val column: Int,
|
||||
val sourceString: String
|
||||
val sourceName: String,
|
||||
val line: Int,
|
||||
val column: Int,
|
||||
val sourceString: String
|
||||
) {
|
||||
/* Formatted representation: source:line:column: text. */
|
||||
fun toString() {
|
||||
@@ -186,12 +265,14 @@ class StackTraceEntry(
|
||||
/* Print this exception and its stack trace to standard output. */
|
||||
fun Exception.printStackTrace() {
|
||||
println(this)
|
||||
var lastEntry = null
|
||||
for( entry in stackTrace() ) {
|
||||
println("\tat "+entry)
|
||||
if( lastEntry == null || lastEntry !is StackTraceEntry || lastEntry.line != entry.line )
|
||||
println("\tat "+entry.toString())
|
||||
lastEntry = entry
|
||||
}
|
||||
}
|
||||
|
||||
/* Compile this string into a regular expression. */
|
||||
fun String.re(): Regex { Regex(this) }
|
||||
val String.re get() = Regex(this)
|
||||
|
||||
|
||||
@@ -411,6 +411,7 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
|
||||
try {
|
||||
if (mini != null) {
|
||||
val binding = Binder.bind(text, mini)
|
||||
val symbolsById = binding.symbols.associateBy { it.id }
|
||||
// Map decl ranges to avoid overriding declarations
|
||||
val declKeys = HashSet<Pair<Int, Int>>()
|
||||
for (sym in binding.symbols) {
|
||||
@@ -427,7 +428,7 @@ suspend fun applyLyngHighlightToTextAst(text: String): String {
|
||||
val key = ref.start to ref.end
|
||||
if (declKeys.contains(key)) continue
|
||||
if (!overrides.containsKey(key)) {
|
||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
||||
val sym = symbolsById[ref.symbolId]
|
||||
val cls = sym?.let { classForKind(it.kind) }
|
||||
if (cls != null) overrides[key] = cls
|
||||
}
|
||||
@@ -646,14 +647,14 @@ private fun detectDeclarationAndParamOverrides(text: String): Map<Pair<Int, Int>
|
||||
i = p
|
||||
continue
|
||||
}
|
||||
// Generic function call site: ident followed by '(' (after optional spaces)
|
||||
// Generic function call site: ident followed by '(' or '=' (after optional spaces)
|
||||
readIdent(i)?.let { (name, endIdx) ->
|
||||
val startIdx = i
|
||||
// Avoid keywords; allow member calls too (a.b()) by not checking preceding char
|
||||
if (name !in kw) {
|
||||
var q = skipWs(endIdx)
|
||||
if (q < n && text[q] == '(') {
|
||||
// Mark as function identifier at call site
|
||||
if (q < n && (text[q] == '(' || text[q] == '=')) {
|
||||
// Mark as function identifier at call site or shorthand declaration
|
||||
result[startIdx to endIdx] = "hl-fn"
|
||||
i = endIdx
|
||||
return@let
|
||||
|
||||
@@ -181,7 +181,7 @@ fun DocsPage(
|
||||
scheduled = false
|
||||
try {
|
||||
val heads = toc.mapNotNull { id -> el.querySelector("#${id.id}") as? HTMLHeadingElement }
|
||||
val tops = heads.map { it.getBoundingClientRect().top + window.scrollY }
|
||||
val tops = heads.map { it.getBoundingClientRect().top }
|
||||
val offset = (updateNavbarOffsetVar() + 16).toDouble()
|
||||
val idx = activeIndexForTops(tops, offset)
|
||||
setActiveTocId(if (idx in toc.indices) toc[idx].id else null)
|
||||
|
||||
@@ -36,7 +36,7 @@ fun HomePage() {
|
||||
// Benefits pills
|
||||
listOf(
|
||||
"Clean, familiar syntax",
|
||||
"Immutable-first collections",
|
||||
"both FP and OOP",
|
||||
"Batteries-included standard library",
|
||||
"Embeddable and testable"
|
||||
).forEach { b ->
|
||||
|
||||
@@ -1024,11 +1024,15 @@ internal fun normalizePath(path: String): String {
|
||||
// If none are above the offset, returns 0. If list is empty, returns 0.
|
||||
fun activeIndexForTops(tops: List<Double>, offsetPx: Double): Int {
|
||||
if (tops.isEmpty()) return 0
|
||||
var lastAbove = 0
|
||||
for (i in tops.indices) {
|
||||
if (tops[i] - offsetPx > 0.0) return i
|
||||
if (tops[i] <= offsetPx) {
|
||||
lastAbove = i
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
// If all headings are above the offset, select the last one
|
||||
return tops.size - 1
|
||||
return lastAbove
|
||||
}
|
||||
|
||||
fun main() {
|
||||
|
||||
@@ -127,7 +127,7 @@ fun ReferencePage() {
|
||||
Ul({ classes("mt-2") }) {
|
||||
d.members.forEach { m ->
|
||||
when (m) {
|
||||
is MiniMemberFunDecl -> {
|
||||
is MiniMemberFunDecl, -> {
|
||||
val params = m.params.joinToString(", ") { p ->
|
||||
val ts = typeOf(p.type)
|
||||
if (ts.isNotBlank()) "${p.name}${ts}" else p.name
|
||||
@@ -136,6 +136,9 @@ fun ReferencePage() {
|
||||
val staticStr = if (m.isStatic) "static " else ""
|
||||
Li { Text("${staticStr}method ${d.name}.${m.name}(${params})${ret}") }
|
||||
}
|
||||
is MiniInitDecl -> {
|
||||
// we don't doc init {} blocks at all
|
||||
}
|
||||
is MiniMemberValDecl -> {
|
||||
val ts = typeOf(m.type)
|
||||
val kindM = if (m.mutable) "var" else "val"
|
||||
|
||||
@@ -122,7 +122,7 @@ internal fun scoreQueryAdvanced(termsIn: List<String>, rec: DocRecord): Int {
|
||||
if (token.startsWith(t) && t.length > best) { best = t.length; matched = t }
|
||||
}
|
||||
if (matched != null) {
|
||||
posMap.getOrPut(matched!!) { mutableListOf() }.add(pos)
|
||||
posMap.getOrPut(matched) { mutableListOf() }.add(pos)
|
||||
}
|
||||
pos++
|
||||
if (pos > 100000) break // safety for extremely large docs
|
||||
|
||||
@@ -329,7 +329,7 @@
|
||||
<!-- Top-left version ribbon -->
|
||||
<div class="corner-ribbon bg-danger text-white">
|
||||
<span style="margin-left: -5em">
|
||||
v1.0.6-SNAPSHOT
|
||||
v1.1.0-beta2
|
||||
</span>
|
||||
</div>
|
||||
<!-- Fixed top navbar for the whole site -->
|
||||
|
||||
@@ -28,20 +28,20 @@ class ScrollSpyTest {
|
||||
fun selectsFirstWhenOnlyFirstIsAboveOffset() {
|
||||
val tops = listOf(20.0, 200.0, 800.0) // px from viewport top
|
||||
val idx = activeIndexForTops(tops, offsetPx = 80.0)
|
||||
assertEquals(1, idx) // 20 <= 80, 200 > 80 -> index 1 (second heading is first below offset)
|
||||
assertEquals(0, idx) // 20 <= 80 (active), 200 > 80 (stops) -> index 0
|
||||
}
|
||||
|
||||
@Test
|
||||
fun selectsLastHeadingAboveOffset() {
|
||||
val tops = listOf(-100.0, 50.0, 70.0)
|
||||
val idx = activeIndexForTops(tops, offsetPx = 80.0)
|
||||
assertEquals(2, idx) // all three are <= 80 -> last index
|
||||
assertEquals(2, idx) // all three are <= 80 -> last index 2
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stopsBeforeFirstBelowOffset() {
|
||||
val tops = listOf(-200.0, -50.0, 30.0, 150.0, 400.0)
|
||||
val idx = activeIndexForTops(tops, offsetPx = 80.0)
|
||||
assertEquals(3, idx) // 30 <= 80 qualifies; 150 > 80 stops, so index 3rd (0-based -> 3?)
|
||||
assertEquals(2, idx) // 30 <= 80 is the last one; 150 > 80 -> index 2
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user