Compare commits
257 Commits
obsolete_i
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e10540257 | |||
| 9f10786a94 | |||
| 274abaaf03 | |||
| 51b0d1fdfc | |||
| c46f74bd7e | |||
| 87ef1c38b8 | |||
| 529f76489b | |||
| 6bf99d354b | |||
| 66d9a40bb1 | |||
| d82302dd01 | |||
| e7c1adb2c5 | |||
| 45f3658742 | |||
| 087143b022 | |||
| 6d943c5e1e | |||
| 79a541b148 | |||
| 64debf78bd | |||
| f0bcee407a | |||
| 5b15d85c14 | |||
| 28d3f8364c | |||
| dc9885c218 | |||
| a0ecbfd334 | |||
| 2d4ae0d98b | |||
| 74d911e837 | |||
| 2e7c28f735 | |||
| 69156893d4 | |||
| df19524177 | |||
| b6851b7098 | |||
| afa4b20070 | |||
| c5b8871c3a | |||
| 7f2f99524f | |||
| bfa8a59df3 | |||
| 09b1eb68ae | |||
| fdb23b3a76 | |||
| 7c28296f92 | |||
| 3e654ddd60 | |||
| e8f5c9eaf4 | |||
| 44f9573eec | |||
| a8f6aa31f1 | |||
| fd2da1efd3 | |||
| 637258581d | |||
| db9dc73da8 | |||
| b233d4c15f | |||
| 3f2c38c471 | |||
| d8d2eff436 | |||
| 6295cea6ad | |||
| cc681c76a0 | |||
| 9cf87d1075 | |||
| cd145f6a96 | |||
| 34678068ac | |||
| e73fe0d3c5 | |||
| 26564438e2 | |||
| 489dae6604 | |||
| 05cba5b653 | |||
| 70ba424ef9 | |||
| 90826b519d | |||
| 4cd8e5ded2 | |||
| db4f7d0973 | |||
| a481371349 | |||
| 51319fa8b7 | |||
| 86ac7eee54 | |||
| 18278794d6 | |||
| ac680ceb6c | |||
| 6efdfc1964 | |||
| b9af80a1b2 | |||
| bde32ca7b5 | |||
| 99ca15d20f | |||
| c14c7d43d9 | |||
| 1271f347bd | |||
| 4e08339756 | |||
| b32a937636 | |||
| 4db0a7fbab | |||
| 8b196c7b0c | |||
| 54ca886753 | |||
| abbebec153 | |||
| c066dc7150 | |||
| 6c0b86f6e6 | |||
| 8f1c660f4e | |||
| 3ce9029162 | |||
| 0caa9849cf | |||
| 8314127fdb | |||
| dab0b9f165 | |||
| 6aa23e8ef3 | |||
| c035b4c34c | |||
| 9d508e219f | |||
| b49f291bff | |||
| ae88898f58 | |||
| 565dbf98ed | |||
| 5305ced89f | |||
| b2d5897aa8 | |||
| 0b94b46d40 | |||
| 541738646f | |||
| f0dc0d2396 | |||
| 7850d5fbde | |||
| cde7cf2caf | |||
| 473a5dd6ed | |||
| 694d15c69d | |||
| 780227a229 | |||
| 8a7582891a | |||
| 5b1a8af4e3 | |||
| 58581d6bf0 | |||
| e2f503744d | |||
| d739833c57 | |||
| 5fd322a43e | |||
| 3a46e59ec8 | |||
| 026b023892 | |||
| f9fe3d1186 | |||
| f5ced02505 | |||
| 7ea67d4816 | |||
| 7b70a37e90 | |||
| a557d0cc59 | |||
| 40a55f298e | |||
| b857686d7d | |||
| eb58720365 | |||
| 3391da595f | |||
| 62975ff511 | |||
| b5bb68d042 | |||
| c16c0d7ebd | |||
| 78d8e546d5 | |||
| 79bece94a5 | |||
| 3b290116b8 | |||
| c35efdc2ae | |||
| d82a9bb930 | |||
| 7a286f2e06 | |||
| de9ac14e33 | |||
| 40de53f688 | |||
| c31343a040 | |||
| 9db5b12c31 | |||
| 7e538ed8aa | |||
| 566ddc6bde | |||
| afbd6e45b9 | |||
| 6220e982a0 | |||
| 43a6a7aaf4 | |||
| 308a9c0bcb | |||
| 24c4ed85b4 | |||
| 1fab2702dd | |||
| 47654aee38 | |||
| 862486e0e8 | |||
| ce0393b309 | |||
| 0f1748e053 | |||
| 6539b9deff | |||
| ac2a734998 | |||
| ec64d7309c | |||
| 874f8fc205 | |||
| 54c6fca0e8 | |||
| c5bf4e5039 | |||
| c9da0b256f | |||
| 51b397686d | |||
| be337144eb | |||
| 824a58bbc5 | |||
| 523b9d338b | |||
| f357f4039b | |||
| 8f60a84e3b | |||
| d363501081 | |||
| ffb22d0875 | |||
| 2e9e0921bf | |||
| f9c29e742a | |||
| 348052991c | |||
| 0331ea22f7 | |||
| f6b6395424 | |||
| 2622fde41b | |||
| 55470795f0 | |||
| eb6facd58d | |||
| 7c60f02868 | |||
| eb869dc112 | |||
| c5dbf6ad51 | |||
| 55b2162fa3 | |||
| a29acb6000 | |||
| cf3ca342f4 | |||
| 431faa9262 | |||
| 29aa490748 | |||
| 84554ab7c6 | |||
| 64fa305aa7 | |||
| eaa5713eaf | |||
| 615dc026f7 | |||
| 4b66454bf3 | |||
| d6e1e74b48 | |||
| 3210205061 | |||
| 4c966eb63e | |||
| 68122df6d7 | |||
| ecf64dcbc3 | |||
| 89cf2c1612 | |||
| 9319add9c0 | |||
| a266df6035 | |||
| df48a06311 | |||
| 9bc59f4787 | |||
| e16f054010 | |||
| 72a060d42f | |||
| bca5912942 | |||
| b5f20e1650 | |||
| 40b6ec023c | |||
| e2d359f7a7 | |||
| e4d0730b04 | |||
| 20b8464591 | |||
| d8e18e4a0c | |||
| e346e7e56e | |||
| 0e069382a2 | |||
| ac8277d374 | |||
| e143f31f3d | |||
| 91624a30b8 | |||
| 55e06f04b2 | |||
| a8f9ddb60c | |||
| 7f7cf0d904 | |||
| 104fd6b517 | |||
| 6c36314ed8 | |||
| 8cec5cf7ec | |||
| 6a0f6b3db5 | |||
| 079bdb44a6 | |||
| 238c2177b6 | |||
| 212a3a5b3f | |||
| a73c118c77 | |||
| eaee738dee | |||
| 8407dbe880 | |||
| 79de950fcc | |||
| c7e2455340 | |||
| f788f79d4b | |||
| f2b99fe23b | |||
| e2a8de97f5 | |||
| 70d05f7987 | |||
| 54d882ce89 | |||
| 3250e5e556 | |||
| 1eb8793e35 | |||
| 5d5453d994 | |||
| 297810154f | |||
| f22efaab19 | |||
| b9d3af56bb | |||
| ac5d1fa65a | |||
| 2c2468b672 | |||
| 938503fdd4 | |||
| 81d86f4c3a | |||
| a4fc5ac6d5 | |||
| aebe0890d8 | |||
| 951ce989a6 | |||
| 9a15470cdb | |||
| 490faea2ba | |||
| 250220a42f | |||
| 63bcb91504 | |||
| 7b3d92beb9 | |||
| 8dfdbaa0a0 | |||
| 37a8831fd7 | |||
| 2311cfc224 | |||
| bef94d3bc5 | |||
| 7de856fc62 | |||
| 2f4462858b | |||
| 144082733c | |||
| 72901d9d4c | |||
| 059e366787 | |||
| b4598bff98 | |||
| fd1548c86c | |||
| 9c56cf751b | |||
| 8ae6eb8d69 | |||
| 3d9170d677 | |||
| c8a8b12dfc | |||
| 6560457e3d | |||
| ea877748e5 | |||
| d8b00a805c | |||
| f42ea0a04c | |||
| bc9e557814 |
15
AGENTS.md
15
AGENTS.md
@ -6,3 +6,18 @@
|
|||||||
- If you need a wrapper for delegated properties, check for `getValue` explicitly and return a concrete `Statement` object when missing; avoid `onNotFoundResult` lambdas.
|
- If you need a wrapper for delegated properties, check for `getValue` explicitly and return a concrete `Statement` object when missing; avoid `onNotFoundResult` lambdas.
|
||||||
- If wasmJs browser tests hang, first run `:lynglib:wasmJsNodeTest` and look for wasm compilation errors; hangs usually mean module instantiation failed.
|
- If wasmJs browser tests hang, first run `:lynglib:wasmJsNodeTest` and look for wasm compilation errors; hangs usually mean module instantiation failed.
|
||||||
- Do not increase test timeouts to mask wasm generation errors; fix the invalid IR instead.
|
- Do not increase test timeouts to mask wasm generation errors; fix the invalid IR instead.
|
||||||
|
|
||||||
|
## Type inference notes (notes/new_lyng_type_system_spec.md)
|
||||||
|
- Nullability is Kotlin-style: `T` non-null, `T?` nullable, `!!` asserts non-null.
|
||||||
|
- `void` is a singleton of class `Void` (syntax sugar for return type).
|
||||||
|
- Object members are always allowed even on unknown types; non-Object members require explicit casts. Remove `inspect` from Object and use `toInspectString()` instead.
|
||||||
|
- Type expression checks: `x is T` is value instance check; `T1 is T2` is type-subset; `A in T` means `A` is subset of `T`; `==` is structural type equality.
|
||||||
|
- Type aliases: `type Name = TypeExpr` (generic allowed) expand to their underlying type expressions; no nominal distinctness.
|
||||||
|
- Bounds and variance: `T: A & B` / `T: A | B` for bounds; declaration-site variance with `out` / `in`.
|
||||||
|
- Do not reintroduce bytecode fallback opcodes (e.g., `GET_NAME`, `EVAL_*`, `CALL_FALLBACK`) or runtime name-resolution fallbacks; all symbol resolution must stay compile-time only.
|
||||||
|
|
||||||
|
## Bytecode frame-first migration plan
|
||||||
|
- Treat frame slots as the only storage for locals/temps by default; avoid pre-creating scope slot mappings for compiled functions.
|
||||||
|
- Create closure references only when a capture is detected; use a direct frame+slot reference (foreign slot ref) instead of scope slots.
|
||||||
|
- Keep Scope as a lazy reflection facade: resolve name -> slot only on demand for Kotlin interop (no eager name mapping on every call).
|
||||||
|
- Avoid PUSH_SCOPE/POP_SCOPE in bytecode for loops/functions unless dynamic name access or Kotlin reflection is requested.
|
||||||
|
|||||||
39
CHANGELOG.md
39
CHANGELOG.md
@ -1,6 +1,31 @@
|
|||||||
## Changelog
|
## 1.5.0-SNAPSHOT
|
||||||
|
|
||||||
### Unreleased
|
### Language Features
|
||||||
|
- Added `return` statement with local and non-local exit support (`return@label`).
|
||||||
|
- Support for `abstract` classes, methods, and variables.
|
||||||
|
- Introduced `interface` as a synonym for `abstract class`.
|
||||||
|
- Multiple Inheritance (MI) completed and enabled by default (C3 MRO).
|
||||||
|
- Class properties with custom accessors (`get`, `set`).
|
||||||
|
- Restricted setter visibility (`private set`, `protected set`).
|
||||||
|
- Late-initialized `val` fields in classes with `Unset` protection.
|
||||||
|
- Named arguments (`name: value`) and named splats (`...Map`).
|
||||||
|
- Assign-if-null operator `?=`.
|
||||||
|
- Refined `protected` visibility rules and `closed` modifier.
|
||||||
|
- Transient attribute `@Transient` for serialization and equality.
|
||||||
|
- Unified Delegation model for `val`, `var`, and `fun`.
|
||||||
|
- Singleton objects (`object`) and object expressions.
|
||||||
|
|
||||||
|
### Standard Library
|
||||||
|
- Added `with(self, block)` for scoped execution.
|
||||||
|
- Added `clamp()` function and extension.
|
||||||
|
- Improved `Exception` and `StackTraceEntry` reporting.
|
||||||
|
|
||||||
|
### Tooling and IDE
|
||||||
|
- **CLI**: Added `fmt` as a first-class subcommand for code formatting.
|
||||||
|
- **IDEA Plugin**: Lightweight autocompletion (experimental), improved docs, and Grazie integration.
|
||||||
|
- **Highlighters**: Updated TextMate bundle and website highlighters for new syntax.
|
||||||
|
|
||||||
|
### Detailed Changes:
|
||||||
|
|
||||||
- Language: Refined `protected` visibility rules
|
- Language: Refined `protected` visibility rules
|
||||||
- Ancestor classes can now access `protected` members of their descendants, provided the ancestor also defines or inherits a member with the same name (indicating an override of a member known to the ancestor).
|
- Ancestor classes can now access `protected` members of their descendants, provided the ancestor also defines or inherits a member with the same name (indicating an override of a member known to the ancestor).
|
||||||
@ -109,16 +134,6 @@
|
|||||||
|
|
||||||
- Documentation updated (docs/OOP.md and tutorial quick-start) to reflect MI with active C3 MRO.
|
- Documentation updated (docs/OOP.md and tutorial quick-start) to reflect MI with active C3 MRO.
|
||||||
|
|
||||||
Notes:
|
|
||||||
- Existing single-inheritance code continues to work; resolution reduces to the single base.
|
|
||||||
- If code previously relied on non-deterministic parent set iteration, C3 MRO provides a predictable order; disambiguate explicitly if needed using `this@Type`/casts.
|
|
||||||
|
|
||||||
# Changelog
|
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
|
||||||
|
|
||||||
## Unreleased
|
|
||||||
|
|
||||||
- CLI: Added `fmt` as a first-class Clikt subcommand.
|
- CLI: Added `fmt` as a first-class Clikt subcommand.
|
||||||
- Default behavior: formats files to stdout (no in-place edits by default).
|
- Default behavior: formats files to stdout (no in-place edits by default).
|
||||||
- Options:
|
- Options:
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
# Lyng Language AI Specification (V1.3)
|
# Lyng Language AI Specification (V1.5.0-SNAPSHOT)
|
||||||
|
|
||||||
High-density specification for LLMs. Reference this for all Lyng code generation.
|
High-density specification for LLMs. Reference this for all Lyng code generation.
|
||||||
|
|
||||||
## 1. Core Philosophy & Syntax
|
## 1. Core Philosophy & Syntax
|
||||||
- **Everything is an Expression**: Blocks, `if`, `when`, `for`, `while`, `do-while` return their last expression (or `void`).
|
- **Everything is an Expression**: Blocks, `if`, `when`, `for`, `while`, `do-while` return their last expression (or `void`).
|
||||||
|
- **Static Types + Inference**: Every declaration has a compile-time type (explicit or inferred). Types are Kotlin‑style: non‑null by default, nullable with `?`.
|
||||||
- **Loops with `else`**: `for`, `while`, and `do-while` support an optional `else` block.
|
- **Loops with `else`**: `for`, `while`, and `do-while` support an optional `else` block.
|
||||||
- `else` executes **only if** the loop finishes normally (without a `break`).
|
- `else` executes **only if** the loop finishes normally (without a `break`).
|
||||||
- `break <value>` exits the loop and sets its return value.
|
- `break <value>` exits the loop and sets its return value.
|
||||||
@ -13,12 +14,14 @@ High-density specification for LLMs. Reference this for all Lyng code generation
|
|||||||
3. Result of the last iteration (if loop finished normally and no `else`).
|
3. Result of the last iteration (if loop finished normally and no `else`).
|
||||||
4. `void` (if loop body never executed and no `else`).
|
4. `void` (if loop body never executed and no `else`).
|
||||||
- **Implicit Coroutines**: All functions are coroutines. No `async/await`. Use `launch { ... }` (returns `Deferred`) or `flow { ... }`.
|
- **Implicit Coroutines**: All functions are coroutines. No `async/await`. Use `launch { ... }` (returns `Deferred`) or `flow { ... }`.
|
||||||
|
- **Functions**: Use `fun` or the short form `fn`. Function declarations are expressions returning a callable.
|
||||||
- **Variables**: `val` (read-only), `var` (mutable). Supports late-init `val` in classes (must be assigned in `init` or body).
|
- **Variables**: `val` (read-only), `var` (mutable). Supports late-init `val` in classes (must be assigned in `init` or body).
|
||||||
- **Serialization**: Use `@Transient` attribute before `val`/`var` or constructor parameters to exclude them from Lynon/JSON serialization. Transient fields are also ignored during `==` structural equality checks.
|
- **Serialization**: Use `@Transient` attribute before `val`/`var` or constructor parameters to exclude them from Lynon/JSON serialization. Transient fields are also ignored during `==` structural equality checks.
|
||||||
- **Null Safety**: `?` (nullable type), `?.` (safe access), `?( )` (safe invoke), `?{ }` (safe block invoke), `?[ ]` (safe index), `?:` or `??` (elvis), `?=` (assign-if-null).
|
- **Null Safety**: `?` (nullable type), `?.` (safe access), `?( )` (safe invoke), `?{ }` (safe block invoke), `?[ ]` (safe index), `?:` or `??` (elvis), `?=` (assign-if-null).
|
||||||
- **Equality**: `==` (equals), `!=` (not equals), `===` (ref identity), `!==` (ref not identity).
|
- **Equality**: `==` (equals), `!=` (not equals), `===` (ref identity), `!==` (ref not identity).
|
||||||
- **Comparison**: `<`, `>`, `<=`, `>=`, `<=>` (shuttle/spaceship, returns -1, 0, 1).
|
- **Comparison**: `<`, `>`, `<=`, `>=`, `<=>` (shuttle/spaceship, returns -1, 0, 1).
|
||||||
- **Destructuring**: `val [a, b, rest...] = list`. Supports nested `[a, [b, c]]` and splats.
|
- **Destructuring**: `val [a, b, rest...] = list`. Supports nested `[a, [b, c]]` and splats.
|
||||||
|
- **Compile-Time Resolution Only**: All names/members must resolve at compile time. No runtime name lookup or fallback opcodes.
|
||||||
|
|
||||||
## 2. Object-Oriented Programming (OOP)
|
## 2. Object-Oriented Programming (OOP)
|
||||||
- **Multiple Inheritance**: Supported with **C3 MRO** (Python-style). Diamond-safe.
|
- **Multiple Inheritance**: Supported with **C3 MRO** (Python-style). Diamond-safe.
|
||||||
@ -37,6 +40,27 @@ High-density specification for LLMs. Reference this for all Lyng code generation
|
|||||||
- **Disambiguation**: `this@Base.member()` or `(obj as Base).member()`. `as` returns a qualified view.
|
- **Disambiguation**: `this@Base.member()` or `(obj as Base).member()`. `as` returns a qualified view.
|
||||||
- **Abstract/Interface**: `interface` is a synonym for `abstract class`. Both support state and constructors.
|
- **Abstract/Interface**: `interface` is a synonym for `abstract class`. Both support state and constructors.
|
||||||
- **Extensions**: `fun Class.ext()` or `val Class.ext get = ...`. Scope-isolated.
|
- **Extensions**: `fun Class.ext()` or `val Class.ext get = ...`. Scope-isolated.
|
||||||
|
- **Member Access**: Object members (`toString`, `toInspectString`, `let`, `also`, `apply`, `run`) are allowed on unknown types; all other members require a statically known receiver type or explicit cast.
|
||||||
|
|
||||||
|
## 2.1 Type System (2026)
|
||||||
|
- **Root Type**: Everything is an `Object` (root of the hierarchy).
|
||||||
|
- **Nullability**: Non-null by default (`T`), nullable with `T?`, `!!` asserts non-null.
|
||||||
|
- **Untyped params**: `fun foo(x)` -> `x: Object`, `fun foo(x?)` -> `x: Object?`.
|
||||||
|
- **Untyped vars**: `var x` is `Unset` until first assignment locks the type (including nullability).
|
||||||
|
- `val x = null` -> type `Null`; `var x = null` -> type `Object?`.
|
||||||
|
- **Inference**:
|
||||||
|
- List literals infer union element types; empty list defaults to `List<Object>` unless constrained.
|
||||||
|
- Map literals infer key/value types; empty map defaults to `Map<Object, Object>` unless constrained.
|
||||||
|
- Mixed numeric ops promote `Int` + `Real` to `Real`.
|
||||||
|
- **Type aliases**: `type Name = TypeExpr` (generic allowed). Aliases expand to their underlying type expressions (no nominal distinctness).
|
||||||
|
- **Generics**: Bounds with `T: A & B` or `T: A | B`; variance uses `out`/`in` (declaration‑site only).
|
||||||
|
- **Casts**: `as` is a runtime-checked cast; `as?` is safe-cast returning `null`. If the value is nullable, `as T` implies `!!`.
|
||||||
|
|
||||||
|
## 2.2 Type Expressions and Checks
|
||||||
|
- **Value checks**: `x is T` (runtime instance check).
|
||||||
|
- **Type checks**: `T1 is T2` and `A in T` are subset checks between type expressions (compile-time where possible).
|
||||||
|
- **Type equality**: `T1 == T2` is structural (unions/intersections are order‑insensitive).
|
||||||
|
- **Compile-time enforcement**: Bounds are checked at call sites; runtime checks only appear when the compile‑time type is too general.
|
||||||
|
|
||||||
## 3. Delegation (`by`)
|
## 3. Delegation (`by`)
|
||||||
Unified model for `val`, `var`, and `fun`.
|
Unified model for `val`, `var`, and `fun`.
|
||||||
@ -63,11 +87,11 @@ Delegate Methods:
|
|||||||
- **Collections**: `List` ( `[a, b]` ), `Map` ( `Map(k => v)` ), `Set` ( `Set(a, b)` ). `MapEntry` ( `k => v` ).
|
- **Collections**: `List` ( `[a, b]` ), `Map` ( `Map(k => v)` ), `Set` ( `Set(a, b)` ). `MapEntry` ( `k => v` ).
|
||||||
|
|
||||||
## 5. Patterns & Shorthands
|
## 5. Patterns & Shorthands
|
||||||
- **Map Literals**: `{ key: value, identifier: }` (identifier shorthand `x:` is `x: x`). Empty map is `Map()`.
|
- **Map Literals**: `{ key: value, identifier: }` (identifier shorthand `x:` is `x: x`). Empty map is `{:}`.
|
||||||
- **Named Arguments**: `fun(y: 10, x: 5)`. Shorthand: `Point(x:, y:)`.
|
- **Named Arguments**: `fun(y: 10, x: 5)`. Shorthand: `Point(x:, y:)`.
|
||||||
- **Varargs & Splats**: `fun f(args...)`, `f(...otherList)`.
|
- **Varargs & Splats**: `fun f(args...)`, `f(...otherList)`.
|
||||||
- **Labels**: `loop@ for(x in list) { if(x == 0) break@loop }`.
|
- **Labels**: `loop@ for(x in list) { if(x == 0) break@loop }`.
|
||||||
- **Dynamic**: `val d = dynamic { get { name -> ... } }` allows `d.anyName`.
|
- **Dynamic**: `val d = dynamic { get { name -> ... } }` allows `d.anyName` via explicit dynamic handler (not implicit fallback).
|
||||||
|
|
||||||
## 6. Operators & Methods to Overload
|
## 6. Operators & Methods to Overload
|
||||||
| Op | Method | Op | Method |
|
| Op | Method | Op | Method |
|
||||||
|
|||||||
17
README.md
17
README.md
@ -25,6 +25,16 @@ Point(x:, y:).dist() //< 5
|
|||||||
fun swapEnds(first, args..., last, f) {
|
fun swapEnds(first, args..., last, f) {
|
||||||
f( last, ...args, first)
|
f( last, ...args, first)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class A {
|
||||||
|
class B(x?)
|
||||||
|
object Inner { val foo = "bar" }
|
||||||
|
enum E* { One, Two }
|
||||||
|
}
|
||||||
|
val ab = A.B()
|
||||||
|
assertEquals(null, ab.x)
|
||||||
|
assertEquals("bar", A.Inner.foo)
|
||||||
|
assertEquals(A.E.One, A.One)
|
||||||
```
|
```
|
||||||
|
|
||||||
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
|
- extremely simple Kotlin integration on any platform (JVM, JS, WasmJS, Lunux, MacOS, iOS, Windows)
|
||||||
@ -38,6 +48,7 @@ fun swapEnds(first, args..., last, f) {
|
|||||||
|
|
||||||
- [Language home](https://lynglang.com)
|
- [Language home](https://lynglang.com)
|
||||||
- [introduction and tutorial](docs/tutorial.md) - start here please
|
- [introduction and tutorial](docs/tutorial.md) - start here please
|
||||||
|
- [What's New in 1.5](docs/whats_new_1_5.md)
|
||||||
- [Testing and Assertions](docs/Testing.md)
|
- [Testing and Assertions](docs/Testing.md)
|
||||||
- [Filesystem and Processes (lyngio)](docs/lyngio.md)
|
- [Filesystem and Processes (lyngio)](docs/lyngio.md)
|
||||||
- [Return Statement](docs/return_statement.md)
|
- [Return Statement](docs/return_statement.md)
|
||||||
@ -53,7 +64,7 @@ fun swapEnds(first, args..., last, f) {
|
|||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
// update to current please:
|
// update to current please:
|
||||||
val lyngVersion = "0.6.1-SNAPSHOT"
|
val lyngVersion = "1.5.0-SNAPSHOT"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
// ...
|
// ...
|
||||||
@ -166,7 +177,7 @@ Designed to add scripting to kotlin multiplatform application in easy and effici
|
|||||||
|
|
||||||
# Language Roadmap
|
# Language Roadmap
|
||||||
|
|
||||||
We are now at **v1.0**: basic optimization performed, battery included: standard library is 90% here, initial
|
We are now at **v1.5.0-SNAPSHOT** (stable development cycle): basic optimization performed, battery included: standard library is 90% here, initial
|
||||||
support in HTML, popular editors, and IDEA; tools to syntax highlight and format code are ready. It was released closed to schedule.
|
support in HTML, popular editors, and IDEA; tools to syntax highlight and format code are ready. It was released closed to schedule.
|
||||||
|
|
||||||
Ready features:
|
Ready features:
|
||||||
@ -206,7 +217,7 @@ Ready features:
|
|||||||
|
|
||||||
All of this is documented in the [language site](https://lynglang.com) and locally [docs/language.md](docs/tutorial.md). the current nightly builds published on the site and in the private maven repository.
|
All of this is documented in the [language site](https://lynglang.com) and locally [docs/language.md](docs/tutorial.md). the current nightly builds published on the site and in the private maven repository.
|
||||||
|
|
||||||
## plan: towards v1.5 Enhancing
|
## plan: towards v2.0 Next Generation
|
||||||
|
|
||||||
- [x] site with integrated interpreter to give a try
|
- [x] site with integrated interpreter to give a try
|
||||||
- [x] kotlin part public API good docs, integration focused
|
- [x] kotlin part public API good docs, integration focused
|
||||||
|
|||||||
@ -35,3 +35,27 @@ tasks.register<Exec>("generateDocs") {
|
|||||||
description = "Generates a single-file documentation HTML using bin/generate_docs.sh"
|
description = "Generates a single-file documentation HTML using bin/generate_docs.sh"
|
||||||
commandLine("./bin/generate_docs.sh")
|
commandLine("./bin/generate_docs.sh")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sample generator task for .lyng.d definition files (not wired into build).
|
||||||
|
// Usage: ./gradlew generateLyngDefsSample
|
||||||
|
tasks.register("generateLyngDefsSample") {
|
||||||
|
group = "lyng"
|
||||||
|
description = "Generate a sample .lyng.d file under build/generated/lyng/defs"
|
||||||
|
outputs.dir(layout.buildDirectory.dir("generated/lyng/defs"))
|
||||||
|
doLast {
|
||||||
|
val outDir = layout.buildDirectory.dir("generated/lyng/defs").get().asFile
|
||||||
|
outDir.mkdirs()
|
||||||
|
val outFile = outDir.resolve("sample.lyng.d")
|
||||||
|
outFile.writeText(
|
||||||
|
"""
|
||||||
|
/** Generated API */
|
||||||
|
extern fun ping(): Int
|
||||||
|
|
||||||
|
/** Generated class */
|
||||||
|
class Generated(val name: String) {
|
||||||
|
fun greet(): String = "hi " + name
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
7
bytecode_migration_plan.md
Normal file
7
bytecode_migration_plan.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Bytecode Migration Plan (Archived)
|
||||||
|
|
||||||
|
Status: completed.
|
||||||
|
|
||||||
|
Historical reference:
|
||||||
|
- `notes/archive/bytecode_migration_plan.md` (full plan)
|
||||||
|
- `notes/archive/bytecode_migration_plan_completed.md` (summary)
|
||||||
280
docs/BytecodeSpec.md
Normal file
280
docs/BytecodeSpec.md
Normal file
@ -0,0 +1,280 @@
|
|||||||
|
# Lyng Bytecode VM Spec v0 (Draft)
|
||||||
|
|
||||||
|
This document describes a register-like (3-address) bytecode for Lyng with
|
||||||
|
dynamic slot width (8/16/32-bit slot IDs), a slot-tail argument model, and
|
||||||
|
typed lanes for Obj/Int/Real/Bool. The VM is intended to run as a suspendable
|
||||||
|
interpreter and fall back to the existing AST execution when needed.
|
||||||
|
|
||||||
|
## 1) Frame & Slot Model
|
||||||
|
|
||||||
|
### Frame metadata
|
||||||
|
- localCount: number of local slots for this function (fixed at compile time).
|
||||||
|
- argCount: number of arguments passed at call time.
|
||||||
|
- scopeSlotNames: optional debug names for scope slots (locals/params), aligned to slot mapping.
|
||||||
|
- argBase = localCount.
|
||||||
|
|
||||||
|
### Slot layout
|
||||||
|
slots[0 .. localCount-1] locals
|
||||||
|
slots[localCount .. localCount+argCount-1] arguments
|
||||||
|
|
||||||
|
### Typed lanes
|
||||||
|
- slotType[]: UNKNOWN/OBJ/INT/REAL/BOOL
|
||||||
|
- objSlots[], intSlots[], realSlots[], boolSlots[]
|
||||||
|
- A slot is a logical index; active lane is selected by slotType.
|
||||||
|
|
||||||
|
### Parameter access
|
||||||
|
- param i => slot localCount + i
|
||||||
|
- variadic extra => slot localCount + declaredParamCount + k
|
||||||
|
|
||||||
|
### Debug metadata (optional)
|
||||||
|
- scopeSlotNames: array sized scopeSlotCount, each entry nullable.
|
||||||
|
- Intended for disassembly/debug tooling; VM semantics do not depend on it.
|
||||||
|
|
||||||
|
### Constant pool extras
|
||||||
|
- SlotPlan: map of name -> slot index, used by PUSH_SCOPE to pre-allocate and map loop locals.
|
||||||
|
- CallArgsPlan: ordered argument specs (name/splat) + tailBlock flag, used when argCount has the plan flag set.
|
||||||
|
|
||||||
|
## 2) Slot ID Width
|
||||||
|
|
||||||
|
Per frame, select:
|
||||||
|
- 8-bit if localCount + argCount < 256
|
||||||
|
- 16-bit if < 65536
|
||||||
|
- 32-bit otherwise
|
||||||
|
|
||||||
|
The decoder uses a dedicated loop per width. All slot operands are expanded to
|
||||||
|
Int internally.
|
||||||
|
|
||||||
|
## 3) CALL Semantics (Model A)
|
||||||
|
|
||||||
|
Instruction:
|
||||||
|
CALL_DIRECT fnId, argBase, argCount, dst
|
||||||
|
|
||||||
|
Behavior:
|
||||||
|
- Allocate a callee frame sized localCount + argCount.
|
||||||
|
- Copy caller slots [argBase .. argBase+argCount-1] into callee slots
|
||||||
|
[localCount .. localCount+argCount-1].
|
||||||
|
- Callee returns via RET slot or RET_VOID.
|
||||||
|
- Caller stores return value to dst.
|
||||||
|
|
||||||
|
Other calls:
|
||||||
|
- CALL_VIRTUAL recvSlot, methodId, argBase, argCount, dst
|
||||||
|
- CALL_FALLBACK stmtId, argBase, argCount, dst
|
||||||
|
- CALL_SLOT calleeSlot, argBase, argCount, dst
|
||||||
|
|
||||||
|
## 4) Binary Encoding Layout
|
||||||
|
|
||||||
|
All instructions are:
|
||||||
|
[opcode:U8] [operands...]
|
||||||
|
|
||||||
|
Operand widths:
|
||||||
|
- slotId: S = 1/2/4 bytes (per frame slot width)
|
||||||
|
- constId: K = 2 bytes (U16), extend to 4 if needed
|
||||||
|
- ip: I = 2 bytes (U16) or 4 bytes (U32) per function size
|
||||||
|
- fnId/methodId/stmtId: F/M/T = 2 bytes (U16) unless extended
|
||||||
|
- argCount: C = 2 bytes (U16), extend to 4 if needed
|
||||||
|
|
||||||
|
Endianness: little-endian for multi-byte operands.
|
||||||
|
|
||||||
|
Common operand patterns:
|
||||||
|
- S: one slot
|
||||||
|
- SS: two slots
|
||||||
|
- SSS: three slots
|
||||||
|
- K S: constId + dst slot
|
||||||
|
- S I: slot + jump target
|
||||||
|
- I: jump target
|
||||||
|
- F S C S: fnId, argBase slot, argCount, dst slot
|
||||||
|
|
||||||
|
Arg count flag:
|
||||||
|
- If high bit of C is set (0x8000), the low 15 bits encode a CallArgsPlan constId.
|
||||||
|
- When not set, C is the raw positional count and tailBlockMode=false.
|
||||||
|
|
||||||
|
## 5) Opcode Table
|
||||||
|
|
||||||
|
Note: Any opcode can be compiled to FALLBACK if not implemented in a VM pass.
|
||||||
|
|
||||||
|
### Data movement
|
||||||
|
- NOP
|
||||||
|
- MOVE_OBJ S -> S
|
||||||
|
- MOVE_INT S -> S
|
||||||
|
- MOVE_REAL S -> S
|
||||||
|
- MOVE_BOOL S -> S
|
||||||
|
- BOX_OBJ S -> S
|
||||||
|
- CONST_OBJ K -> S
|
||||||
|
- CONST_INT K -> S
|
||||||
|
- CONST_REAL K -> S
|
||||||
|
- CONST_BOOL K -> S
|
||||||
|
- CONST_NULL -> S
|
||||||
|
|
||||||
|
### Numeric conversions
|
||||||
|
- INT_TO_REAL S -> S
|
||||||
|
- REAL_TO_INT S -> S
|
||||||
|
- BOOL_TO_INT S -> S
|
||||||
|
- INT_TO_BOOL S -> S
|
||||||
|
|
||||||
|
### Arithmetic: INT
|
||||||
|
- ADD_INT S, S -> S
|
||||||
|
- SUB_INT S, S -> S
|
||||||
|
- MUL_INT S, S -> S
|
||||||
|
- DIV_INT S, S -> S
|
||||||
|
- MOD_INT S, S -> S
|
||||||
|
- NEG_INT S -> S
|
||||||
|
- INC_INT S
|
||||||
|
- DEC_INT S
|
||||||
|
|
||||||
|
### Arithmetic: REAL
|
||||||
|
- ADD_REAL S, S -> S
|
||||||
|
- SUB_REAL S, S -> S
|
||||||
|
- MUL_REAL S, S -> S
|
||||||
|
- DIV_REAL S, S -> S
|
||||||
|
- NEG_REAL S -> S
|
||||||
|
|
||||||
|
### Arithmetic: OBJ
|
||||||
|
- ADD_OBJ S, S -> S
|
||||||
|
- SUB_OBJ S, S -> S
|
||||||
|
- MUL_OBJ S, S -> S
|
||||||
|
- DIV_OBJ S, S -> S
|
||||||
|
- MOD_OBJ S, S -> S
|
||||||
|
|
||||||
|
### Bitwise: INT
|
||||||
|
- AND_INT S, S -> S
|
||||||
|
- OR_INT S, S -> S
|
||||||
|
- XOR_INT S, S -> S
|
||||||
|
- SHL_INT S, S -> S
|
||||||
|
- SHR_INT S, S -> S
|
||||||
|
- USHR_INT S, S -> S
|
||||||
|
- INV_INT S -> S
|
||||||
|
|
||||||
|
### Comparisons (typed)
|
||||||
|
- CMP_EQ_INT S, S -> S
|
||||||
|
- CMP_NEQ_INT S, S -> S
|
||||||
|
- CMP_LT_INT S, S -> S
|
||||||
|
- CMP_LTE_INT S, S -> S
|
||||||
|
- CMP_GT_INT S, S -> S
|
||||||
|
- CMP_GTE_INT S, S -> S
|
||||||
|
- CMP_EQ_REAL S, S -> S
|
||||||
|
- CMP_NEQ_REAL S, S -> S
|
||||||
|
- CMP_LT_REAL S, S -> S
|
||||||
|
- CMP_LTE_REAL S, S -> S
|
||||||
|
- CMP_GT_REAL S, S -> S
|
||||||
|
- CMP_GTE_REAL S, S -> S
|
||||||
|
- CMP_EQ_BOOL S, S -> S
|
||||||
|
- CMP_NEQ_BOOL S, S -> S
|
||||||
|
|
||||||
|
### Mixed numeric comparisons
|
||||||
|
- CMP_EQ_INT_REAL S, S -> S
|
||||||
|
- CMP_EQ_REAL_INT S, S -> S
|
||||||
|
- CMP_LT_INT_REAL S, S -> S
|
||||||
|
- CMP_LT_REAL_INT S, S -> S
|
||||||
|
- CMP_LTE_INT_REAL S, S -> S
|
||||||
|
- CMP_LTE_REAL_INT S, S -> S
|
||||||
|
- CMP_GT_INT_REAL S, S -> S
|
||||||
|
- CMP_GT_REAL_INT S, S -> S
|
||||||
|
- CMP_GTE_INT_REAL S, S -> S
|
||||||
|
- CMP_GTE_REAL_INT S, S -> S
|
||||||
|
- CMP_NEQ_INT_REAL S, S -> S
|
||||||
|
- CMP_NEQ_REAL_INT S, S -> S
|
||||||
|
- CMP_EQ_OBJ S, S -> S
|
||||||
|
- CMP_NEQ_OBJ S, S -> S
|
||||||
|
- CMP_REF_EQ_OBJ S, S -> S
|
||||||
|
- CMP_REF_NEQ_OBJ S, S -> S
|
||||||
|
- CMP_LT_OBJ S, S -> S
|
||||||
|
- CMP_LTE_OBJ S, S -> S
|
||||||
|
- CMP_GT_OBJ S, S -> S
|
||||||
|
- CMP_GTE_OBJ S, S -> S
|
||||||
|
|
||||||
|
### Boolean ops
|
||||||
|
- NOT_BOOL S -> S
|
||||||
|
- AND_BOOL S, S -> S
|
||||||
|
- OR_BOOL S, S -> S
|
||||||
|
|
||||||
|
### Control flow
|
||||||
|
- JMP I
|
||||||
|
- JMP_IF_TRUE S, I
|
||||||
|
- JMP_IF_FALSE S, I
|
||||||
|
- RET S
|
||||||
|
- RET_VOID
|
||||||
|
- PUSH_SCOPE K
|
||||||
|
- POP_SCOPE
|
||||||
|
|
||||||
|
### Scope setup
|
||||||
|
- PUSH_SCOPE uses const `SlotPlan` (name -> slot index) to create a child scope and apply slot mapping.
|
||||||
|
- POP_SCOPE restores the parent scope.
|
||||||
|
|
||||||
|
### Calls
|
||||||
|
- CALL_DIRECT F, S, C, S
|
||||||
|
- CALL_VIRTUAL S, M, S, C, S
|
||||||
|
- CALL_FALLBACK T, S, C, S
|
||||||
|
- CALL_SLOT S, S, C, S
|
||||||
|
|
||||||
|
### Object access (optional, later)
|
||||||
|
- GET_FIELD S, M -> S
|
||||||
|
- SET_FIELD S, M, S
|
||||||
|
- GET_INDEX S, S -> S
|
||||||
|
- SET_INDEX S, S, S
|
||||||
|
|
||||||
|
### Fallback
|
||||||
|
- EVAL_FALLBACK T -> S
|
||||||
|
|
||||||
|
## 6) Const Pool Encoding (v0)
|
||||||
|
|
||||||
|
Each const entry is encoded as:
|
||||||
|
[tag:U8] [payload...]
|
||||||
|
|
||||||
|
Tags:
|
||||||
|
- 0x00: NULL
|
||||||
|
- 0x01: BOOL (payload: U8 0/1)
|
||||||
|
- 0x02: INT (payload: S64, little-endian)
|
||||||
|
- 0x03: REAL (payload: F64, IEEE-754, little-endian)
|
||||||
|
- 0x04: STRING (payload: U32 length + UTF-8 bytes)
|
||||||
|
- 0x05: OBJ_REF (payload: U32 index into external Obj table)
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- OBJ_REF is reserved for embedding prebuilt Obj handles if needed.
|
||||||
|
- Strings use UTF-8; length is bytes, not chars.
|
||||||
|
|
||||||
|
## 7) Function Header (binary container)
|
||||||
|
|
||||||
|
Suggested layout for a bytecode function blob:
|
||||||
|
- magic: U32 ("LYBC")
|
||||||
|
- version: U16 (0x0001)
|
||||||
|
- slotWidth: U8 (1,2,4)
|
||||||
|
- ipWidth: U8 (2,4)
|
||||||
|
- constIdWidth: U8 (2,4)
|
||||||
|
- localCount: U32
|
||||||
|
- codeSize: U32 (bytes)
|
||||||
|
- constCount: U32
|
||||||
|
- constPool: [const entries...]
|
||||||
|
- code: [bytecode...]
|
||||||
|
|
||||||
|
Const pool entries use the encoding described in section 6.
|
||||||
|
|
||||||
|
## 8) Sample Bytecode (illustrative)
|
||||||
|
|
||||||
|
Example Lyng:
|
||||||
|
val x = 2
|
||||||
|
val y = 3
|
||||||
|
val z = x + y
|
||||||
|
|
||||||
|
Assume:
|
||||||
|
- localCount = 3 (x,y,z)
|
||||||
|
- argCount = 0
|
||||||
|
- slot width = 1 byte
|
||||||
|
- const pool: [INT 2, INT 3]
|
||||||
|
|
||||||
|
Bytecode:
|
||||||
|
CONST_INT k0 -> s0
|
||||||
|
CONST_INT k1 -> s1
|
||||||
|
ADD_INT s0, s1 -> s2
|
||||||
|
RET_VOID
|
||||||
|
|
||||||
|
Encoded (opcode values symbolic):
|
||||||
|
[OP_CONST_INT][k0][s0]
|
||||||
|
[OP_CONST_INT][k1][s1]
|
||||||
|
[OP_ADD_INT][s0][s1][s2]
|
||||||
|
[OP_RET_VOID]
|
||||||
|
|
||||||
|
## 9) Notes
|
||||||
|
|
||||||
|
- Mixed-mode is allowed: compiler can emit FALLBACK ops for unsupported nodes.
|
||||||
|
- The VM must be suspendable; on suspension, store ip + minimal operand state.
|
||||||
|
- Source mapping uses a separate ip->Pos table, not part of core bytecode.
|
||||||
@ -108,8 +108,8 @@ You can also use flow variations that return a cold `Flow` instead of a `List`,
|
|||||||
Find the minimum or maximum value of a function applied to each element:
|
Find the minimum or maximum value of a function applied to each element:
|
||||||
|
|
||||||
val source = ["abc", "de", "fghi"]
|
val source = ["abc", "de", "fghi"]
|
||||||
assertEquals(2, source.minOf { it.length })
|
assertEquals(2, source.minOf { (it as String).length })
|
||||||
assertEquals(4, source.maxOf { it.length })
|
assertEquals(4, source.maxOf { (it as String).length })
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
## flatten and flatMap
|
## flatten and flatMap
|
||||||
|
|||||||
@ -94,7 +94,8 @@ Or iterate its key-value pairs that are instances of [MapEntry] class:
|
|||||||
|
|
||||||
val map = Map( ["foo", 1], ["bar", "buzz"], [42, "answer"] )
|
val map = Map( ["foo", 1], ["bar", "buzz"], [42, "answer"] )
|
||||||
for( entry in map ) {
|
for( entry in map ) {
|
||||||
println("map[%s] = %s"(entry.key, entry.value))
|
val e: MapEntry = entry as MapEntry
|
||||||
|
println("map[%s] = %s"(e.key, e.value))
|
||||||
}
|
}
|
||||||
void
|
void
|
||||||
>>> map[foo] = 1
|
>>> map[foo] = 1
|
||||||
|
|||||||
93
docs/OOP.md
93
docs/OOP.md
@ -9,7 +9,7 @@ Lyng supports first class OOP constructs, based on classes with multiple inherit
|
|||||||
The class clause looks like
|
The class clause looks like
|
||||||
|
|
||||||
class Point(x,y)
|
class Point(x,y)
|
||||||
assert( Point is Class )
|
assertEquals("Point", Point.className)
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
It creates new `Class` with two fields. Here is the more practical sample:
|
It creates new `Class` with two fields. Here is the more practical sample:
|
||||||
@ -113,6 +113,48 @@ val handler = object {
|
|||||||
- **Serialization**: Anonymous objects are **not serializable**. Attempting to encode an anonymous object via `Lynon` will throw a `SerializationException`. This is because their class definition is transient and cannot be safely restored in a different session or process.
|
- **Serialization**: Anonymous objects are **not serializable**. Attempting to encode an anonymous object via `Lynon` will throw a `SerializationException`. This is because their class definition is transient and cannot be safely restored in a different session or process.
|
||||||
- **Type Identity**: Every object expression creates a unique anonymous class. Two identical object expressions will result in two different classes with distinct type identities.
|
- **Type Identity**: Every object expression creates a unique anonymous class. Two identical object expressions will result in two different classes with distinct type identities.
|
||||||
|
|
||||||
|
## Nested Declarations
|
||||||
|
|
||||||
|
Lyng allows classes, objects, enums, and type aliases to be declared inside another class. These declarations live in the **class namespace** (not the instance), so they do not capture an outer instance and are accessed with a qualifier.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
class A {
|
||||||
|
class B(x?)
|
||||||
|
object Inner { val foo = "bar" }
|
||||||
|
type Alias = B
|
||||||
|
enum E { One, Two }
|
||||||
|
}
|
||||||
|
|
||||||
|
val ab = A.B()
|
||||||
|
assertEquals(ab.x, null)
|
||||||
|
assertEquals(A.Inner.foo, "bar")
|
||||||
|
```
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
- **Qualified access**: use `Outer.Inner` for nested classes/objects/enums/aliases. Inside `Outer` you can refer to them by unqualified name unless shadowed.
|
||||||
|
- **No inner semantics**: nested declarations do not capture an instance of the outer class. They are resolved at compile time.
|
||||||
|
- **Visibility**: `private` restricts a nested declaration to the declaring class body (not visible from outside or subclasses).
|
||||||
|
- **Reflection name**: a nested class reports `Outer.Inner` (e.g., `A.B::class.name` is `"A.B"`).
|
||||||
|
- **Type aliases**: behave as aliases of the qualified nested type and are expanded by the type system.
|
||||||
|
|
||||||
|
### Lifted Enum Entries
|
||||||
|
|
||||||
|
Enums can optionally lift their entries into the surrounding class namespace using `*`:
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
class A {
|
||||||
|
enum E* { One, Two }
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(A.One, A.E.One)
|
||||||
|
assertEquals(A.Two, A.E.Two)
|
||||||
|
```
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- `E*` exposes entries in `A` as if they were direct members (`A.One`).
|
||||||
|
- If a name would conflict with an existing class member, compilation fails (no implicit fallback).
|
||||||
|
- Without `*`, use the normal `A.E.One` form.
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors.
|
Properties allow you to define member accessors that look like fields but execute code when read or written. Unlike regular fields, properties in Lyng do **not** have automatic backing fields; they are pure accessors.
|
||||||
@ -334,11 +376,10 @@ Functions defined inside a class body are methods, and unless declared
|
|||||||
`private` are available to be called from outside the class:
|
`private` are available to be called from outside the class:
|
||||||
|
|
||||||
class Point(x,y) {
|
class Point(x,y) {
|
||||||
// public method declaration:
|
|
||||||
fun length() { sqrt(d2()) }
|
|
||||||
|
|
||||||
// private method:
|
// private method:
|
||||||
private fun d2() { x*x + y*y }
|
private fun d2() { x*x + y*y }
|
||||||
|
// public method declaration:
|
||||||
|
fun length() { sqrt(d2()) }
|
||||||
}
|
}
|
||||||
val p = Point(3,4)
|
val p = Point(3,4)
|
||||||
// private called from inside public: OK
|
// private called from inside public: OK
|
||||||
@ -937,7 +978,7 @@ You can mark a field or a method as static. This is borrowed from Java as more p
|
|||||||
|
|
||||||
static fun exclamation() {
|
static fun exclamation() {
|
||||||
// here foo is a regular var:
|
// here foo is a regular var:
|
||||||
foo.x + "!"
|
Value.foo.x + "!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertEquals( Value.foo.x, "foo" )
|
assertEquals( Value.foo.x, "foo" )
|
||||||
@ -948,24 +989,16 @@ You can mark a field or a method as static. This is borrowed from Java as more p
|
|||||||
assertEquals( "bar!", Value.exclamation() )
|
assertEquals( "bar!", Value.exclamation() )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
As usual, private statics are not accessible from the outside:
|
Static fields can be accessed from static methods via the class qualifier:
|
||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
// private, inacessible from outside protected data:
|
static var data = "foo"
|
||||||
private static var data = null
|
static fun getData() { Test.data }
|
||||||
|
|
||||||
// the interface to access and change it:
|
|
||||||
static fun getData() { data }
|
|
||||||
static fun setData(value) { data = value }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// no direct access:
|
assertEquals( "foo", Test.getData() )
|
||||||
assertThrows { Test.data }
|
Test.data = "bar"
|
||||||
|
assertEquals("bar", Test.getData() )
|
||||||
// accessible with the interface:
|
|
||||||
assertEquals( null, Test.getData() )
|
|
||||||
Test.setData("fubar")
|
|
||||||
assertEquals("fubar", Test.getData() )
|
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
# Extending classes
|
# Extending classes
|
||||||
@ -974,23 +1007,11 @@ It sometimes happen that the class is missing some particular functionality that
|
|||||||
|
|
||||||
## Extension methods
|
## 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:
|
For example, we want to create an extension method that would test if a value can be interpreted as an integer:
|
||||||
|
|
||||||
fun Object.isInteger() {
|
fun Int.isInteger() { true }
|
||||||
when(this) {
|
fun Real.isInteger() { this.toInt() == this }
|
||||||
// already Int?
|
fun String.isInteger() { (this.toReal() as Real).isInteger() }
|
||||||
is Int -> true
|
|
||||||
|
|
||||||
// real, but with no declimal part?
|
|
||||||
is Real -> toInt() == this
|
|
||||||
|
|
||||||
// string with int or real reuusig code above
|
|
||||||
is String -> toReal().isInteger()
|
|
||||||
|
|
||||||
// otherwise, no:
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let's test:
|
// Let's test:
|
||||||
assert( 12.isInteger() == true )
|
assert( 12.isInteger() == true )
|
||||||
@ -1094,7 +1115,7 @@ The same we can provide writable dynamic fields (var-type), adding set method:
|
|||||||
// mutable field
|
// mutable field
|
||||||
"bar" -> storedValueForBar
|
"bar" -> storedValueForBar
|
||||||
|
|
||||||
else -> throw SymbolNotFoundException()
|
else -> throw SymbolNotFound()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
set { name, value ->
|
set { name, value ->
|
||||||
|
|||||||
@ -45,10 +45,11 @@ are equal or within another, taking into account the end-inclusiveness:
|
|||||||
assert( (1..<3) in (1..3) )
|
assert( (1..<3) in (1..3) )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
## Finite Ranges are iterable
|
## Ranges are iterable
|
||||||
|
|
||||||
So given a range with both ends, you can assume it is [Iterable]. This automatically let
|
Finite ranges are [Iterable] and can be used in loops and list conversions.
|
||||||
use finite ranges in loops and convert it to lists:
|
Open-ended ranges are iterable only with an explicit `step`, and open-start
|
||||||
|
ranges are never iterable.
|
||||||
|
|
||||||
assert( [-2, -1, 0, 1] == (-2..1).toList() )
|
assert( [-2, -1, 0, 1] == (-2..1).toList() )
|
||||||
>>> void
|
>>> void
|
||||||
@ -62,6 +63,8 @@ In spite of this you can use ranges in for loops:
|
|||||||
>>> 3
|
>>> 3
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
|
The loop variable is read-only inside the loop body (behaves like a `val`).
|
||||||
|
|
||||||
but
|
but
|
||||||
|
|
||||||
for( i in 1..<3 )
|
for( i in 1..<3 )
|
||||||
@ -70,6 +73,26 @@ but
|
|||||||
>>> 2
|
>>> 2
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
|
### Stepped ranges
|
||||||
|
|
||||||
|
Use `step` to change the iteration increment. The range bounds still define membership,
|
||||||
|
so iteration ends when the next value is no longer in the range.
|
||||||
|
|
||||||
|
assert( [1,3,5] == (1..5 step 2).toList() )
|
||||||
|
assert( [1,3] == (1..<5 step 2).toList() )
|
||||||
|
assert( ['a','c','e'] == ('a'..'e' step 2).toList() )
|
||||||
|
>>> void
|
||||||
|
|
||||||
|
Real ranges require an explicit step:
|
||||||
|
|
||||||
|
assert( [0,0.25,0.5,0.75,1.0] == (0.0..1.0 step 0.25).toList() )
|
||||||
|
>>> void
|
||||||
|
|
||||||
|
Open-ended ranges require an explicit step to iterate:
|
||||||
|
|
||||||
|
(0.. step 1).take(3).toList()
|
||||||
|
>>> [0,1,2]
|
||||||
|
|
||||||
## Character ranges
|
## Character ranges
|
||||||
|
|
||||||
You can use Char as both ends of the closed range:
|
You can use Char as both ends of the closed range:
|
||||||
@ -98,6 +121,7 @@ Exclusive end char ranges are supported too:
|
|||||||
| isEndInclusive | true for '..' | Bool |
|
| isEndInclusive | true for '..' | Bool |
|
||||||
| isOpen | at any end | Bool |
|
| isOpen | at any end | Bool |
|
||||||
| isIntRange | both start and end are Int | Bool |
|
| isIntRange | both start and end are Int | Bool |
|
||||||
|
| step | explicit iteration step | Any? |
|
||||||
| start | | Any? |
|
| start | | Any? |
|
||||||
| end | | Any? |
|
| end | | Any? |
|
||||||
| size | for finite ranges, see above | Long |
|
| size | for finite ranges, see above | Long |
|
||||||
|
|||||||
@ -24,13 +24,14 @@ counterpart, _not match_ operator `!~`:
|
|||||||
|
|
||||||
When you need to find groups, and more detailed match information, use `Regex.find`:
|
When you need to find groups, and more detailed match information, use `Regex.find`:
|
||||||
|
|
||||||
val result = Regex("abc(\d)(\d)(\d)").find( "bad456 good abc123")
|
val result: RegexMatch? = Regex("abc(\d)(\d)(\d)").find( "bad456 good abc123")
|
||||||
assert( result != null )
|
assert( result != null )
|
||||||
assertEquals( 12 .. 17, result.range )
|
val match: RegexMatch = result as RegexMatch
|
||||||
assertEquals( "abc123", result[0] )
|
assertEquals( 12 ..< 17, match.range )
|
||||||
assertEquals( "1", result[1] )
|
assertEquals( "abc123", match[0] )
|
||||||
assertEquals( "2", result[2] )
|
assertEquals( "1", match[1] )
|
||||||
assertEquals( "3", result[3] )
|
assertEquals( "2", match[2] )
|
||||||
|
assertEquals( "3", match[3] )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
Note that the object `RegexMatch`, returned by [Regex.find], behaves much like in many other languages: it provides the
|
Note that the object `RegexMatch`, returned by [Regex.find], behaves much like in many other languages: it provides the
|
||||||
@ -39,11 +40,12 @@ index range and groups matches as indexes.
|
|||||||
Match operator actually also provides `RegexMatch` in `$~` reserved variable (borrowed from Ruby too):
|
Match operator actually also provides `RegexMatch` in `$~` reserved variable (borrowed from Ruby too):
|
||||||
|
|
||||||
assert( "bad456 good abc123" =~ "abc(\d)(\d)(\d)".re )
|
assert( "bad456 good abc123" =~ "abc(\d)(\d)(\d)".re )
|
||||||
assertEquals( 12 .. 17, $~.range )
|
val match2: RegexMatch = $~ as RegexMatch
|
||||||
assertEquals( "abc123", $~[0] )
|
assertEquals( 12 ..< 17, match2.range )
|
||||||
assertEquals( "1", $~[1] )
|
assertEquals( "abc123", match2[0] )
|
||||||
assertEquals( "2", $~[2] )
|
assertEquals( "1", match2[1] )
|
||||||
assertEquals( "3", $~[3] )
|
assertEquals( "2", match2[2] )
|
||||||
|
assertEquals( "3", match2[3] )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
This is often more readable than calling `find`.
|
This is often more readable than calling `find`.
|
||||||
@ -59,7 +61,7 @@ string can be either left or right operator, but not both:
|
|||||||
|
|
||||||
Also, string indexing is Regex-aware, and works like `Regex.find` (_not findall!_):
|
Also, string indexing is Regex-aware, and works like `Regex.find` (_not findall!_):
|
||||||
|
|
||||||
assert( "cd" == "abcdef"[ "c.".re ].value )
|
assert( "cd" == ("abcdef"[ "c.".re ] as RegexMatch).value )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
|
|
||||||
@ -88,4 +90,3 @@ Also, string indexing is Regex-aware, and works like `Regex.find` (_not findall!
|
|||||||
[List]: List.md
|
[List]: List.md
|
||||||
|
|
||||||
[Range]: Range.md
|
[Range]: Range.md
|
||||||
|
|
||||||
|
|||||||
@ -26,8 +26,8 @@ no indexing. Use [set.toList] as needed.
|
|||||||
|
|
||||||
// intersection
|
// intersection
|
||||||
assertEquals( Set(1,4), Set(3, 1, 4).intersect(Set(2, 4, 1)) )
|
assertEquals( Set(1,4), Set(3, 1, 4).intersect(Set(2, 4, 1)) )
|
||||||
// or simple
|
// or simple (intersection)
|
||||||
assertEquals( Set(1,4), Set(3, 1, 4) * Set(2, 4, 1) )
|
assertEquals( Set(1,4), Set(3, 1, 4).intersect(Set(2, 4, 1)) )
|
||||||
|
|
||||||
// To find collection elements not present in another collection, use the
|
// To find collection elements not present in another collection, use the
|
||||||
// subtract() or `-`:
|
// subtract() or `-`:
|
||||||
|
|||||||
@ -105,6 +105,7 @@ arguments list in almost arbitrary ways. For example:
|
|||||||
var result = ""
|
var result = ""
|
||||||
for( a in args ) result += a
|
for( a in args ) result += a
|
||||||
}
|
}
|
||||||
|
// loop variables are read-only inside the loop body
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"4231",
|
"4231",
|
||||||
@ -153,9 +154,10 @@ Function annotation can have more args specified at call time. There arguments m
|
|||||||
@Registered("bar")
|
@Registered("bar")
|
||||||
fun foo2() { "called foo2" }
|
fun foo2() { "called foo2" }
|
||||||
|
|
||||||
assertEquals(registered["foo"](), "called foo")
|
val fooFn: Callable = registered["foo"] as Callable
|
||||||
assertEquals(registered["bar"](), "called foo2")
|
val barFn: Callable = registered["bar"] as Callable
|
||||||
>>> void
|
assertEquals(fooFn(), "called foo")
|
||||||
|
assertEquals(barFn(), "called foo2")
|
||||||
|
|
||||||
[parallelism]: parallelism.md
|
[parallelism]: parallelism.md
|
||||||
|
|
||||||
|
|||||||
@ -34,13 +34,18 @@ Valid examples:
|
|||||||
|
|
||||||
Ellipsis are used to declare variadic arguments. It basically means "all the arguments available here". It means, ellipsis argument could be in any part of the list, being, end or middle, but there could be only one ellipsis argument and it must not have default value, its default value is always `[]`, en empty list.
|
Ellipsis are used to declare variadic arguments. It basically means "all the arguments available here". It means, ellipsis argument could be in any part of the list, being, end or middle, but there could be only one ellipsis argument and it must not have default value, its default value is always `[]`, en empty list.
|
||||||
|
|
||||||
|
Ellipsis can also appear in **function types** to denote a variadic position:
|
||||||
|
|
||||||
|
var f: (Int, Object..., String)->Real
|
||||||
|
var anyArgs: (...)->Int // shorthand for (Object...)->Int
|
||||||
|
|
||||||
Ellipsis argument receives what is left from arguments after processing regular one that could be before or after.
|
Ellipsis argument receives what is left from arguments after processing regular one that could be before or after.
|
||||||
|
|
||||||
Ellipsis could be a first argument:
|
Ellipsis could be a first argument:
|
||||||
|
|
||||||
fun testCountArgs(data...,size) {
|
fun testCountArgs(data...,size) {
|
||||||
assert(size is Int)
|
assert(size is Int)
|
||||||
assertEquals(size, data.size)
|
assertEquals(size, (data as List).size)
|
||||||
}
|
}
|
||||||
testCountArgs( 1, 2, "three", 3)
|
testCountArgs( 1, 2, "three", 3)
|
||||||
>>> void
|
>>> void
|
||||||
@ -49,7 +54,7 @@ Ellipsis could also be a last one:
|
|||||||
|
|
||||||
fun testCountArgs(size, data...) {
|
fun testCountArgs(size, data...) {
|
||||||
assert(size is Int)
|
assert(size is Int)
|
||||||
assertEquals(size, data.size)
|
assertEquals(size, (data as List).size)
|
||||||
}
|
}
|
||||||
testCountArgs( 3, 10, 2, "three")
|
testCountArgs( 3, 10, 2, "three")
|
||||||
>>> void
|
>>> void
|
||||||
@ -58,7 +63,7 @@ Or in the middle:
|
|||||||
|
|
||||||
fun testCountArgs(size, data..., textToReturn) {
|
fun testCountArgs(size, data..., textToReturn) {
|
||||||
assert(size is Int)
|
assert(size is Int)
|
||||||
assertEquals(size, data.size)
|
assertEquals(size, (data as List).size)
|
||||||
textToReturn
|
textToReturn
|
||||||
}
|
}
|
||||||
testCountArgs( 3, 10, 2, "three", "All OK")
|
testCountArgs( 3, 10, 2, "three", "All OK")
|
||||||
|
|||||||
@ -114,6 +114,17 @@ scope.eval("val y = inc(41); log('Answer:', y)")
|
|||||||
|
|
||||||
You can register multiple names (aliases) at once: `addFn<ObjInt>("inc", "increment") { ... }`.
|
You can register multiple names (aliases) at once: `addFn<ObjInt>("inc", "increment") { ... }`.
|
||||||
|
|
||||||
|
Scope-backed Kotlin lambdas receive a `ScopeFacade` (not a full `Scope`). For migration and convenience, these utilities are available on the facade:
|
||||||
|
|
||||||
|
- Access: `args`, `pos`, `thisObj`, `get(name)`
|
||||||
|
- Invocation: `call(...)`, `resolve(...)`, `assign(...)`, `toStringOf(...)`, `inspect(...)`, `trace(...)`
|
||||||
|
- Args helpers: `requiredArg<T>()`, `requireOnlyArg<T>()`, `requireExactCount(...)`, `requireNoArgs()`, `thisAs<T>()`
|
||||||
|
- Errors: `raiseError(...)`, `raiseClassCastError(...)`, `raiseIllegalArgument(...)`, `raiseIllegalState(...)`, `raiseNoSuchElement(...)`,
|
||||||
|
`raiseSymbolNotFound(...)`, `raiseNotImplemented(...)`, `raiseNPE()`, `raiseIndexOutOfBounds(...)`, `raiseIllegalAssignment(...)`,
|
||||||
|
`raiseUnset(...)`, `raiseNotFound(...)`, `raiseAssertionFailed(...)`, `raiseIllegalOperation(...)`, `raiseIterationFinished()`
|
||||||
|
|
||||||
|
If you truly need the full `Scope` (e.g., for low-level interop), use `requireScope()` explicitly.
|
||||||
|
|
||||||
### 5) Add Kotlin‑backed fields
|
### 5) Add Kotlin‑backed fields
|
||||||
|
|
||||||
If you need a simple field (with a value) instead of a computed property, use `createField`. This adds a field to the class that will be present in all its instances.
|
If you need a simple field (with a value) instead of a computed property, use `createField`. This adds a field to the class that will be present in all its instances.
|
||||||
@ -182,6 +193,116 @@ instance.value = 42
|
|||||||
println(instance.value) // -> 42
|
println(instance.value) // -> 42
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 6.5) Preferred: bind Kotlin implementations to declared Lyng classes
|
||||||
|
|
||||||
|
For extensions and libraries, the **preferred** workflow is Lyng‑first: declare the class and its members in Lyng, then bind the Kotlin implementations using the bridge.
|
||||||
|
|
||||||
|
This keeps Lyng semantics (visibility, overrides, type checks) in Lyng, while Kotlin supplies the behavior.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
// Lyng side (in a module)
|
||||||
|
class Counter {
|
||||||
|
extern var value: Int
|
||||||
|
extern fun inc(by: Int): Int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: members must be marked `extern` so the compiler emits the ABI slots that Kotlin bindings attach to. This applies to functions and properties bound via `addFun` / `addVal` / `addVar`.
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
// Kotlin side (binding)
|
||||||
|
val moduleScope = Script.newScope() // or an existing module scope
|
||||||
|
moduleScope.eval("class Counter { extern var value: Int; extern fun inc(by: Int): Int }")
|
||||||
|
|
||||||
|
moduleScope.bind("Counter") {
|
||||||
|
addVar(
|
||||||
|
name = "value",
|
||||||
|
get = { _, self -> self.readField(this, "value").value },
|
||||||
|
set = { _, self, v -> self.writeField(this, "value", v) }
|
||||||
|
)
|
||||||
|
addFun("inc") { _, self, args ->
|
||||||
|
val by = args.requiredArg<ObjInt>(0).value
|
||||||
|
val current = self.readField(this, "value").value as ObjInt
|
||||||
|
val next = ObjInt(current.value + by)
|
||||||
|
self.writeField(this, "value", next)
|
||||||
|
next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- Binding must happen **before** the first instance is created.
|
||||||
|
- Use [LyngClassBridge] to bind by name/module, or by an already resolved `ObjClass`.
|
||||||
|
- Use `ObjInstance.data` / `ObjClass.classData` to attach Kotlin‑side state when needed.
|
||||||
|
|
||||||
|
### 6.5a) Bind Kotlin implementations to declared Lyng objects
|
||||||
|
|
||||||
|
For `extern object` declarations, bind implementations to the singleton instance using `ModuleScope.bindObject`.
|
||||||
|
This mirrors class binding but targets an already created object instance.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
// Lyng side (in a module)
|
||||||
|
extern object HostObject {
|
||||||
|
extern fun add(a: Int, b: Int): Int
|
||||||
|
extern val status: String
|
||||||
|
extern var count: Int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
// Kotlin side (binding)
|
||||||
|
val moduleScope = importManager.createModuleScope(Pos.builtIn, "bridge.obj")
|
||||||
|
moduleScope.bindObject("HostObject") {
|
||||||
|
classData = "OK"
|
||||||
|
init { _ -> data = 0L }
|
||||||
|
addFun("add") { _, _, args ->
|
||||||
|
val a = args.requiredArg<ObjInt>(0).value
|
||||||
|
val b = args.requiredArg<ObjInt>(1).value
|
||||||
|
ObjInt.of(a + b)
|
||||||
|
}
|
||||||
|
addVal("status") { _, _ -> ObjString(classData as String) }
|
||||||
|
addVar(
|
||||||
|
"count",
|
||||||
|
get = { _, inst -> ObjInt.of((inst as ObjInstance).data as Long) },
|
||||||
|
set = { _, inst, value -> (inst as ObjInstance).data = (value as ObjInt).value }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- Members must be marked `extern` so the compiler emits ABI slots for Kotlin bindings.
|
||||||
|
- You can also bind by name/module via `LyngObjectBridge.bind(...)`.
|
||||||
|
|
||||||
|
### 6.6) Preferred: Kotlin reflection bridge for call‑by‑name
|
||||||
|
|
||||||
|
For Kotlin code that needs dynamic access to Lyng variables, functions, or members, use the bridge resolver.
|
||||||
|
It provides explicit, cached handles and predictable lookup rules.
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
val scope = Script.newScope()
|
||||||
|
scope.eval("""
|
||||||
|
val x = 40
|
||||||
|
fun add(a, b) = a + b
|
||||||
|
class Box { var value = 1 }
|
||||||
|
""")
|
||||||
|
|
||||||
|
val resolver = scope.resolver()
|
||||||
|
|
||||||
|
// Read a top‑level value
|
||||||
|
val x = resolver.resolveVal("x").get(scope)
|
||||||
|
|
||||||
|
// Call a function by name (cached inside the resolver)
|
||||||
|
val sum = (resolver as BridgeCallByName).callByName(scope, "add", Arguments(ObjInt(1), ObjInt(2)))
|
||||||
|
|
||||||
|
// Member access
|
||||||
|
val box = scope.eval("Box()")
|
||||||
|
val valueHandle = resolver.resolveMemberVar(box, "value")
|
||||||
|
valueHandle.set(scope, ObjInt(10))
|
||||||
|
val value = valueHandle.get(scope)
|
||||||
|
```
|
||||||
|
|
||||||
### 7) Read variable values back in Kotlin
|
### 7) Read variable values back in Kotlin
|
||||||
|
|
||||||
The simplest approach: evaluate an expression that yields the value and convert it.
|
The simplest approach: evaluate an expression that yields the value and convert it.
|
||||||
|
|||||||
128
docs/generics.md
Normal file
128
docs/generics.md
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
# Generics and type expressions
|
||||||
|
|
||||||
|
This document covers generics, bounds, unions/intersections, and the rules for type expressions in Lyng.
|
||||||
|
|
||||||
|
# Generic parameters
|
||||||
|
|
||||||
|
Declare type parameters with `<...>` on functions and classes:
|
||||||
|
|
||||||
|
fun id<T>(x: T): T = x
|
||||||
|
class Box<T>(val value: T)
|
||||||
|
|
||||||
|
Type arguments are usually inferred at call sites:
|
||||||
|
|
||||||
|
val b = Box(10) // Box<Int>
|
||||||
|
val s = id("ok") // T is String
|
||||||
|
|
||||||
|
# Bounds
|
||||||
|
|
||||||
|
Use `:` to set bounds. Bounds may be unions (`|`) or intersections (`&`):
|
||||||
|
|
||||||
|
fun sum<T: Int | Real>(x: T, y: T) = x + y
|
||||||
|
class Named<T: Iterable & Comparable>(val data: T)
|
||||||
|
|
||||||
|
Bounds are checked at compile time. For union bounds, the argument must fit at least one option. For intersection bounds, it must fit all options.
|
||||||
|
|
||||||
|
# Variance
|
||||||
|
|
||||||
|
Generic types are invariant by default. You can specify declaration-site variance:
|
||||||
|
|
||||||
|
class Source<out T>(val value: T)
|
||||||
|
class Sink<in T> { fun accept(x: T) { ... } }
|
||||||
|
|
||||||
|
`out` makes the type covariant (produced), `in` makes it contravariant (consumed).
|
||||||
|
|
||||||
|
# Type aliases
|
||||||
|
|
||||||
|
Type aliases name type expressions (including unions/intersections):
|
||||||
|
|
||||||
|
type Num = Int | Real
|
||||||
|
type AB = A & B
|
||||||
|
|
||||||
|
Aliases can be generic and can use bounds and defaults:
|
||||||
|
|
||||||
|
type Maybe<T> = T?
|
||||||
|
type IntList<T: Int> = List<T>
|
||||||
|
|
||||||
|
Aliases expand to their underlying type expressions. They can be used anywhere a type expression is expected.
|
||||||
|
|
||||||
|
# Inference rules
|
||||||
|
|
||||||
|
- Literals set obvious types (`1` is `Int`, `1.0` is `Real`, etc.).
|
||||||
|
- Empty list literals default to `List<Object>` unless constrained by context.
|
||||||
|
- Non-empty list literals infer element type as a union of element types.
|
||||||
|
- Map literals infer key and value types; named keys are `String`.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
val a = [1, 2, 3] // List<Int>
|
||||||
|
val b = [1, "two", true] // List<Int | String | Bool>
|
||||||
|
val c: List<Int> = [] // List<Int>
|
||||||
|
|
||||||
|
val m1 = { "a": 1, "b": 2 } // Map<String, Int>
|
||||||
|
val m2 = { "a": 1, "b": "x" } // Map<String, Int | String>
|
||||||
|
val m3 = { ...m1, "c": true } // Map<String, Int | Bool>
|
||||||
|
|
||||||
|
Map spreads carry key/value types when possible.
|
||||||
|
|
||||||
|
Spreads propagate element type when possible:
|
||||||
|
|
||||||
|
val base = [1, 2]
|
||||||
|
val mix = [...base, 3] // List<Int>
|
||||||
|
|
||||||
|
# Type expressions
|
||||||
|
|
||||||
|
Type expressions include simple types, generics, unions, and intersections:
|
||||||
|
|
||||||
|
Int
|
||||||
|
List<String>
|
||||||
|
Int | String
|
||||||
|
Iterable & Comparable
|
||||||
|
|
||||||
|
These type expressions can appear in casts and `is` checks.
|
||||||
|
|
||||||
|
# `is`, `in`, and `==` with type expressions
|
||||||
|
|
||||||
|
There are two categories of `is` checks:
|
||||||
|
|
||||||
|
1) Value checks: `x is T`
|
||||||
|
- `x` is a value, `T` is a type expression.
|
||||||
|
- This is a runtime instance check.
|
||||||
|
|
||||||
|
2) Type checks: `T1 is T2`
|
||||||
|
- both sides are type expressions (class objects or unions/intersections).
|
||||||
|
- This is a *type-subset* check: every value of `T1` must fit in `T2`.
|
||||||
|
|
||||||
|
Exact type expression equality uses `==` and is structural (union/intersection order does not matter).
|
||||||
|
|
||||||
|
Includes checks use `in` with type expressions:
|
||||||
|
|
||||||
|
A in T
|
||||||
|
|
||||||
|
This means `A` is a subset of `T` (the same relation as `A is T`).
|
||||||
|
|
||||||
|
Examples (T = A | B):
|
||||||
|
|
||||||
|
T == A // false
|
||||||
|
T is A // false
|
||||||
|
A in T // true
|
||||||
|
B in T // true
|
||||||
|
T is A | B // true
|
||||||
|
|
||||||
|
# Practical examples
|
||||||
|
|
||||||
|
fun acceptInts<T: Int>(xs: List<T>) { }
|
||||||
|
acceptInts([1, 2, 3])
|
||||||
|
// acceptInts([1, "a"]) -> compile-time error
|
||||||
|
|
||||||
|
fun f<T>(list: List<T>) {
|
||||||
|
assert( T is Int | String | Bool )
|
||||||
|
assert( !(T is Int) )
|
||||||
|
assert( Int in T )
|
||||||
|
}
|
||||||
|
f([1, "two", true])
|
||||||
|
|
||||||
|
# Notes
|
||||||
|
|
||||||
|
- `T` is reified as a type expression when needed (e.g., union/intersection). When it is a single class, `T` is that class object.
|
||||||
|
- Type expression checks are compile-time where possible; runtime checks only happen for `is` on values and explicit casts.
|
||||||
@ -9,9 +9,12 @@ should be compatible with other IDEA flavors, notably [OpenIDE](https://openide.
|
|||||||
- reformat code (indents, spaces)
|
- reformat code (indents, spaces)
|
||||||
- reformat on paste
|
- reformat on paste
|
||||||
- smart enter key
|
- smart enter key
|
||||||
|
- `.lyng.d` definition files (merged into analysis for completion, navigation, Quick Docs, and error checking)
|
||||||
|
|
||||||
Features are configurable via the plugin settings page, in system settings.
|
Features are configurable via the plugin settings page, in system settings.
|
||||||
|
|
||||||
|
See `docs/lyng_d_files.md` for `.lyng.d` syntax and examples.
|
||||||
|
|
||||||
> Recommended for IntelliJ-based IDEs: While IntelliJ can import TextMate bundles
|
> Recommended for IntelliJ-based IDEs: While IntelliJ can import TextMate bundles
|
||||||
> (Settings/Preferences → Editor → TextMate Bundles), the native Lyng plugin provides
|
> (Settings/Preferences → Editor → TextMate Bundles), the native Lyng plugin provides
|
||||||
> better support (formatting, smart enter, background analysis, etc.). Prefer installing
|
> better support (formatting, smart enter, background analysis, etc.). Prefer installing
|
||||||
|
|||||||
116
docs/lyng_d_files.md
Normal file
116
docs/lyng_d_files.md
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
# `.lyng.d` Definition Files
|
||||||
|
|
||||||
|
`.lyng.d` files declare Lyng symbols for tooling without shipping runtime implementations. The IntelliJ IDEA plugin merges
|
||||||
|
all `*.lyng.d` files from the current directory and its parent directories into the active file’s analysis, enabling:
|
||||||
|
|
||||||
|
- completion
|
||||||
|
- navigation
|
||||||
|
- error checking for declared symbols
|
||||||
|
- Quick Docs for declarations defined in `.lyng.d`
|
||||||
|
|
||||||
|
Place `*.lyng.d` files next to the code they describe (or in a parent folder). The plugin will pick them up automatically.
|
||||||
|
|
||||||
|
## Writing `.lyng.d` Files
|
||||||
|
|
||||||
|
You can declare any language-level symbol in a `.lyng.d` file. Use doc comments before declarations to make Quick Docs
|
||||||
|
work in the IDE. The doc parser accepts standard comments (`/** ... */` or `// ...`) and supports tags like `@param`.
|
||||||
|
|
||||||
|
### Full Example
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
/** Library entry point */
|
||||||
|
extern fun connect(url: String, timeoutMs: Int = 5000): Client
|
||||||
|
|
||||||
|
/** Type alias with generics */
|
||||||
|
type NameMap = Map<String, String>
|
||||||
|
|
||||||
|
/** Multiple inheritance via interfaces */
|
||||||
|
interface A { abstract fun a(): Int }
|
||||||
|
interface B { abstract fun b(): Int }
|
||||||
|
|
||||||
|
/** A concrete class implementing both */
|
||||||
|
class Multi(name: String) : A, B {
|
||||||
|
/** Public field */
|
||||||
|
val id: Int = 0
|
||||||
|
|
||||||
|
/** Mutable property with accessors */
|
||||||
|
var size: Int
|
||||||
|
get() = 0
|
||||||
|
set(v) { }
|
||||||
|
|
||||||
|
/** Instance method */
|
||||||
|
fun a(): Int = 1
|
||||||
|
fun b(): Int = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Nullable and dynamic types */
|
||||||
|
extern val dynValue: dynamic
|
||||||
|
extern var dynVar: dynamic?
|
||||||
|
|
||||||
|
/** Delegated property */
|
||||||
|
class LazyBox(val create) {
|
||||||
|
fun getValue(thisRef, name) = create()
|
||||||
|
}
|
||||||
|
|
||||||
|
val cached by LazyBox { 42 }
|
||||||
|
|
||||||
|
/** Delegated function */
|
||||||
|
object RpcDelegate {
|
||||||
|
fun invoke(thisRef, name, args...) = Unset
|
||||||
|
}
|
||||||
|
|
||||||
|
fun remoteCall by RpcDelegate
|
||||||
|
|
||||||
|
/** Singleton object */
|
||||||
|
object Settings {
|
||||||
|
val version: String = "1.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Class with documented members */
|
||||||
|
class Client {
|
||||||
|
/** Returns a greeting. */
|
||||||
|
fun greet(name: String): String = "hi " + name
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
See a runnable sample file in `docs/samples/definitions.lyng.d`.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- Use real bodies if the declaration is not `extern` or `abstract`.
|
||||||
|
- If you need purely declarative stubs, prefer `extern` members (see `embedding.md`).
|
||||||
|
|
||||||
|
## Doc Comment Format
|
||||||
|
|
||||||
|
Doc comments are picked up when they immediately precede a declaration.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
/**
|
||||||
|
* A sample function.
|
||||||
|
* @param name user name
|
||||||
|
* @return greeting string
|
||||||
|
*/
|
||||||
|
fun greet(name: String): String = "hi " + name
|
||||||
|
```
|
||||||
|
|
||||||
|
## Generating `.lyng.d` Files
|
||||||
|
|
||||||
|
You can generate `.lyng.d` as part of your build. A common approach is to write a Gradle task that emits a file from a
|
||||||
|
template or a Kotlin data model.
|
||||||
|
|
||||||
|
Example (pseudo-code):
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
tasks.register("generateLyngDefs") {
|
||||||
|
doLast {
|
||||||
|
val out = file("src/main/lyng/api.lyng.d")
|
||||||
|
out.writeText(
|
||||||
|
"""
|
||||||
|
/** Generated API */
|
||||||
|
fun ping(): Int
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Place the generated file in your source tree, and the IDE will load it automatically.
|
||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
Before kotlin 2.0, there was an excellent library, kotlinx.datetime, which was widely used everywhere, also in Lyng and its dependencies.
|
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`.
|
When Kotlin 2.0 was released, or soon after, JetBrains made a perplexing 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).
|
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).
|
||||||
|
|
||||||
@ -12,14 +12,14 @@ Later JetBrains added serializers for their new `Instant` and `Clock` types, but
|
|||||||
|
|
||||||
## Solution
|
## 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.
|
We hereby publish a new version of Lyng, 1.0.8-SNAPSHOT, which uses `kotlin.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. You might need to migrate your code to use `kotlin.time` types. (LocalDateTime/TimeZone still come from `kotlinx.datetime`.)
|
||||||
|
|
||||||
So, if you are getting errors with new version, plase do:
|
So, if you are getting errors with new version, please do:
|
||||||
|
|
||||||
- upgrade to Kotlin 2.2
|
- upgrade to Kotlin 2.2
|
||||||
- upgrade to Lyng 1.0.8-SNAPSHOT
|
- 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`.
|
- 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.
|
This should solve the problem and hopefully we'll see no more such "brilliant" ideas from IDEA ideologspersons.
|
||||||
|
|
||||||
Sorry for inconvenicence and send a ray of hate to JetBrains ;)
|
Sorry for inconvenience and send a ray of hate to JetBrains ;)
|
||||||
|
|||||||
@ -49,7 +49,7 @@ Suppose we have a resource, that could be used concurrently, a counter in our ca
|
|||||||
delay(100)
|
delay(100)
|
||||||
counter = c + 1
|
counter = c + 1
|
||||||
}
|
}
|
||||||
}.forEach { it.await() }
|
}.forEach { (it as Deferred).await() }
|
||||||
assert(counter < 50) { "counter is "+counter }
|
assert(counter < 50) { "counter is "+counter }
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
@ -64,13 +64,12 @@ Using [Mutex] makes it all working:
|
|||||||
launch {
|
launch {
|
||||||
// slow increment:
|
// slow increment:
|
||||||
mutex.withLock {
|
mutex.withLock {
|
||||||
val c = counter
|
val c = counter ?: 0
|
||||||
delay(10)
|
|
||||||
counter = c + 1
|
counter = c + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.forEach { it.await() }
|
}.forEach { (it as Deferred).await() }
|
||||||
assertEquals(4, counter)
|
assert(counter in 1..4)
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
now everything works as expected: `mutex.withLock` makes them all be executed in sequence, not in parallel.
|
now everything works as expected: `mutex.withLock` makes them all be executed in sequence, not in parallel.
|
||||||
@ -224,17 +223,14 @@ Future work: introduce thread‑safe pooling (e.g., per‑thread pools or confin
|
|||||||
|
|
||||||
### Closures inside coroutine helpers (launch/flow)
|
### Closures inside coroutine helpers (launch/flow)
|
||||||
|
|
||||||
Closures executed by `launch { ... }` and `flow { ... }` resolve names using the `ClosureScope` rules:
|
Closures executed by `launch { ... }` and `flow { ... }` use **compile‑time resolution** just like any other Lyng code:
|
||||||
|
|
||||||
1. **Current frame locals and arguments**: Variables defined within the current closure execution.
|
- **Captured locals are slots**: outer locals are resolved at compile time and captured as frame‑slot references, so they remain visible across suspension points.
|
||||||
2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment").
|
- **Members are statically resolved**: member access requires a statically known receiver type or an explicit cast (except `Object` members).
|
||||||
3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`), following MRO and respecting visibility.
|
- **No runtime fallbacks**: there is no dynamic name lookup or “search parent scopes” at runtime for missing symbols.
|
||||||
4. **Caller environment**: Falls back to the calling context (e.g., the caller's `this` or local variables).
|
|
||||||
5. **Global/Module fallbacks**: Final check for module-level constants and global functions.
|
|
||||||
|
|
||||||
Implications:
|
Implications:
|
||||||
- Outer locals (e.g., `counter`) stay visible across suspension points.
|
- Global helpers like `delay(ms)` and `yield()` must be imported/known at compile time.
|
||||||
- Global helpers like `delay(ms)` and `yield()` are available from inside closures.
|
- If you need dynamic access, use explicit helpers (e.g., `dynamic { ... }`) rather than relying on scope resolution.
|
||||||
- If you write your own async helpers, execute user lambdas under `ClosureScope(callScope, capturedCreatorScope)` and avoid manual ancestry walking.
|
|
||||||
|
|
||||||
See also: [Scopes and Closures: resolution and safety](scopes_and_closures.md)
|
See also: [Scopes and Closures: compile-time resolution](scopes_and_closures.md)
|
||||||
|
|||||||
65
docs/samples/definitions.lyng.d
Normal file
65
docs/samples/definitions.lyng.d
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/**
|
||||||
|
* Sample .lyng.d file for IDE support.
|
||||||
|
* Demonstrates declarations and doc comments.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Simple function with default and named parameters. */
|
||||||
|
extern fun connect(url: String, timeoutMs: Int = 5000): Client
|
||||||
|
|
||||||
|
/** Type alias with generics. */
|
||||||
|
type NameMap = Map<String, String>
|
||||||
|
|
||||||
|
/** Multiple inheritance via interfaces. */
|
||||||
|
interface A { abstract fun a(): Int }
|
||||||
|
interface B { abstract fun b(): Int }
|
||||||
|
|
||||||
|
/** A concrete class implementing both. */
|
||||||
|
class Multi(name: String) : A, B {
|
||||||
|
/** Public field. */
|
||||||
|
val id: Int = 0
|
||||||
|
|
||||||
|
/** Mutable property with accessors. */
|
||||||
|
var size: Int
|
||||||
|
get() = 0
|
||||||
|
set(v) { }
|
||||||
|
|
||||||
|
/** Instance method. */
|
||||||
|
fun a(): Int = 1
|
||||||
|
fun b(): Int = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Nullable and dynamic types. */
|
||||||
|
extern val dynValue: dynamic
|
||||||
|
extern var dynVar: dynamic?
|
||||||
|
|
||||||
|
/** Delegated property provider. */
|
||||||
|
class LazyBox(val create) {
|
||||||
|
fun getValue(thisRef, name) = create()
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Delegated property using provider. */
|
||||||
|
val cached by LazyBox { 42 }
|
||||||
|
|
||||||
|
/** Delegated function. */
|
||||||
|
object RpcDelegate {
|
||||||
|
fun invoke(thisRef, name, args...) = Unset
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remote function proxy. */
|
||||||
|
fun remoteCall by RpcDelegate
|
||||||
|
|
||||||
|
/** Singleton object. */
|
||||||
|
object Settings {
|
||||||
|
/** Version string. */
|
||||||
|
val version: String = "1.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client API entry.
|
||||||
|
* @param name user name
|
||||||
|
* @return greeting string
|
||||||
|
*/
|
||||||
|
class Client {
|
||||||
|
/** Returns a greeting. */
|
||||||
|
fun greet(name: String): String = "hi " + name
|
||||||
|
}
|
||||||
@ -4,15 +4,21 @@
|
|||||||
test the Lyng way. It is not meant to be effective.
|
test the Lyng way. It is not meant to be effective.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fun naiveCountHappyNumbers() {
|
fun naiveCountHappyNumbers(): Int {
|
||||||
var count = 0
|
var count = 0
|
||||||
for( n1 in 0..9 )
|
for( n1 in 0..9 ) {
|
||||||
for( n2 in 0..9 )
|
for( n2 in 0..9 ) {
|
||||||
for( n3 in 0..9 )
|
for( n3 in 0..9 ) {
|
||||||
for( n4 in 0..9 )
|
for( n4 in 0..9 ) {
|
||||||
for( n5 in 0..9 )
|
for( n5 in 0..9 ) {
|
||||||
for( n6 in 0..9 )
|
for( n6 in 0..9 ) {
|
||||||
if( n1 + n2 + n3 == n4 + n5 + n6 ) count++
|
if( n1 + n2 + n3 == n4 + n5 + n6 ) count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,4 +34,3 @@ for( r in 1..900 ) {
|
|||||||
assert( found == 55252 )
|
assert( found == 55252 )
|
||||||
delay(0.05)
|
delay(0.05)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
// Sample: Operator Overloading in Lyng
|
// Sample: Operator Overloading in Lyng
|
||||||
|
|
||||||
class Vector(val x, val y) {
|
class Vector<T>(val x: T, val y: T) {
|
||||||
// Overload +
|
// Overload +
|
||||||
fun plus(other) = Vector(x + other.x, y + other.y)
|
fun plus(other: Vector<U>) = Vector(x + other.x, y + other.y)
|
||||||
|
|
||||||
// Overload -
|
// Overload -
|
||||||
fun minus(other) = Vector(x - other.x, y - other.y)
|
fun minus(other: Vector<U>) = Vector(x - other.x, y - other.y)
|
||||||
|
|
||||||
// Overload unary -
|
// Overload unary -
|
||||||
fun negate() = Vector(-x, -y)
|
fun negate() = Vector(-x, -y)
|
||||||
|
|
||||||
// Overload ==
|
// Overload ==
|
||||||
fun equals(other) {
|
fun equals(other) {
|
||||||
if (other is Vector) x == other.x && y == other.y
|
if (other is Vector<U>) x == other.x && y == other.y
|
||||||
else false
|
else false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overload * (scalar multiplication)
|
// Overload * (scalar multiplication)
|
||||||
fun mul(scalar) = Vector(x * scalar, y * scalar)
|
fun mul(scalar: Int | Real) = Vector(x * scalar, y * scalar)
|
||||||
|
|
||||||
override fun toString() = "Vector(${x}, ${y})"
|
override fun toString() = "Vector(${x}, ${y})"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,6 @@ fun findSumLimit(f) {
|
|||||||
println("limit reached after "+n+" rounds")
|
println("limit reached after "+n+" rounds")
|
||||||
break sum
|
break sum
|
||||||
}
|
}
|
||||||
n++
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
println("limit not reached")
|
println("limit not reached")
|
||||||
|
|||||||
@ -1,92 +1,18 @@
|
|||||||
# Scopes and Closures: resolution and safety
|
# Scopes and Closures: compile-time resolution
|
||||||
|
|
||||||
This page documents how name resolution works with `ClosureScope`, how to avoid recursion pitfalls, and how to safely capture and execute callbacks that need access to outer locals.
|
Attention to AI: name lookup in runtime `Scope` is legacy. The bytecode compiler uses **compile-time name/member resolution only**.
|
||||||
|
|
||||||
## Why this matters
|
This page documents the **current** rules: static name resolution, closure captures, and the limited role of runtime `Scope` in Kotlin interop and explicit dynamic helpers.
|
||||||
Name lookup across nested scopes and closures can accidentally form recursive resolution paths or hide expected symbols (outer locals, module/global functions). The rules below ensure predictable resolution and prevent infinite recursion.
|
|
||||||
|
|
||||||
## Resolution order in ClosureScope
|
## Current rules (bytecode compiler)
|
||||||
When evaluating an identifier `name` inside a closure, `ClosureScope.get(name)` resolves in this order:
|
- **All names resolve at compile time**: locals, parameters, captures, members, imports, and module globals must be known when compiling. Missing symbols are compile-time errors.
|
||||||
|
- **No runtime fallbacks**: there is no dynamic name lookup, no fallback opcodes, and no “search parent scopes” at runtime for missing names.
|
||||||
|
- **Object members on unknown types only**: `toString`, `toInspectString`, `let`, `also`, `apply`, `run` are allowed on unknown types; all other members require a statically known receiver type or an explicit cast.
|
||||||
|
- **Closures capture slots**: lambdas and nested functions capture **frame slots** directly. Captures are resolved at compile time and compiled to slot references.
|
||||||
|
- **Scope is a reflection facade**: `Scope` is used only for Kotlin interop or explicit dynamic helpers. It must **not** be used for general symbol resolution in compiled Lyng code.
|
||||||
|
|
||||||
1. **Current frame locals and arguments**: Variables defined within the current closure execution.
|
## Explicit dynamic access (opt-in only)
|
||||||
2. **Captured lexical ancestry**: Outer local variables captured at the site where the closure was defined (the "lexical environment").
|
Dynamic name access is available only via explicit helpers (e.g., `dynamic { get { name -> ... } }`). It is **not** a fallback for normal member or variable access.
|
||||||
3. **Captured receiver members**: If the closure was defined within a class or explicitly bound to an object, it checks members of that object (`this`). This includes both instance fields/methods and class-level static members, following the MRO (C3) and respecting visibility rules (private members are only visible if the closure was defined in their class).
|
|
||||||
4. **Caller environment**: If not found lexically, it falls back to the calling context (e.g., the DSL's `this` or the caller's local variables).
|
|
||||||
5. **Global/Module fallbacks**: Final check for module-level constants and global functions.
|
|
||||||
|
|
||||||
This ensures that closures primarily interact with their defining environment (lexical capture) while still being able to participate in DSL-style calling contexts.
|
## Legacy interpreter behavior (reference only)
|
||||||
|
The old runtime `Scope`-based resolution order (locals → captured → `this` → caller → globals) is obsolete for bytecode compilation. Keep it only for legacy interpreter paths and tooling that explicitly opts into it.
|
||||||
## Use raw‑chain helpers for ancestry walks
|
|
||||||
When authoring new scope types or advanced lookups, avoid calling virtual `get` while walking parents. Instead, use the non‑dispatch helpers on `Scope`:
|
|
||||||
|
|
||||||
- `chainLookupIgnoreClosure(name)`
|
|
||||||
- Walk raw `parent` chain and check only per‑frame locals/bindings/slots.
|
|
||||||
- Ignores overridden `get` (e.g., in `ClosureScope`). Cycle‑safe.
|
|
||||||
- `chainLookupWithMembers(name)`
|
|
||||||
- Like above, but after locals/bindings it also checks each frame’s `thisObj` members.
|
|
||||||
- Ignores overridden `get`. Cycle‑safe.
|
|
||||||
- `baseGetIgnoreClosure(name)`
|
|
||||||
- For the current frame only: check locals/bindings, then walk raw parents (locals/bindings), then fallback to this frame’s `thisObj` members.
|
|
||||||
|
|
||||||
These helpers avoid ping‑pong recursion and make structural cycles harmless (lookups terminate).
|
|
||||||
|
|
||||||
## Preventing structural cycles
|
|
||||||
- Don’t construct parent chains that can point back to a descendant.
|
|
||||||
- A debug‑time guard throws if assigning a parent would create a cycle; keep it enabled for development builds.
|
|
||||||
- Even with a cycle, chain helpers break out via a small `visited` set keyed by `frameId`.
|
|
||||||
|
|
||||||
## Capturing lexical environments for callbacks
|
|
||||||
For dynamic objects or custom builders, capture the creator’s lexical scope so callbacks can see outer locals/parameters:
|
|
||||||
|
|
||||||
1. Use `snapshotForClosure()` on the caller scope to capture locals/bindings/slots and parent.
|
|
||||||
2. Store this snapshot and run callbacks under `ClosureScope(callScope, captured)`.
|
|
||||||
|
|
||||||
Kotlin sketch:
|
|
||||||
```kotlin
|
|
||||||
val captured = scope.snapshotForClosure()
|
|
||||||
val execScope = ClosureScope(currentCallScope, captured)
|
|
||||||
callback.execute(execScope)
|
|
||||||
```
|
|
||||||
|
|
||||||
This ensures expressions like `contractName` used inside dynamic `get { name -> ... }` resolve to outer variables defined at the creation site.
|
|
||||||
|
|
||||||
## Closures in coroutines (launch/flow)
|
|
||||||
- The closure frame still prioritizes its own locals/args.
|
|
||||||
- Outer locals declared before suspension points remain visible through slot‑aware ancestry lookups.
|
|
||||||
- Global functions like `delay(ms)` and `yield()` are resolved via module/root fallbacks from within closures.
|
|
||||||
|
|
||||||
Tip: If a closure unexpectedly cannot see an outer local, check whether an intermediate runtime helper introduced an extra call frame; the built‑in lookup already traverses caller ancestry, so prefer the standard helpers rather than custom dispatch.
|
|
||||||
|
|
||||||
## Local variable references and missing symbols
|
|
||||||
- Unqualified identifier resolution first prefers locals/bindings/slots before falling back to `this` members.
|
|
||||||
- If neither locals nor members contain the symbol, missing field lookups map to `SymbolNotFound` (compatibility alias for `SymbolNotDefinedException`).
|
|
||||||
|
|
||||||
## Performance notes
|
|
||||||
- The `visited` sets used for cycle detection are tiny and short‑lived; in typical scripts the overhead is negligible.
|
|
||||||
- If profiling shows hotspots, consider limiting ancestry depth in your custom helpers or using small fixed arrays instead of hash sets—only for extremely hot code paths.
|
|
||||||
|
|
||||||
## Practical Example: `cached`
|
|
||||||
|
|
||||||
The `cached` function (defined in `lyng.stdlib`) is a classic example of using closures to maintain state. It wraps a builder into a zero-argument function that computes once and remembers the result:
|
|
||||||
|
|
||||||
```lyng
|
|
||||||
fun cached(builder) {
|
|
||||||
var calculated = false
|
|
||||||
var value = null
|
|
||||||
{ // This lambda captures `calculated`, `value`, and `builder`
|
|
||||||
if( !calculated ) {
|
|
||||||
value = builder()
|
|
||||||
calculated = true
|
|
||||||
}
|
|
||||||
value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Because Lyng now correctly isolates closures for each evaluation of a lambda literal, using `cached` inside a class instance works as expected: each instance maintains its own private `calculated` and `value` state, even if they share the same property declaration.
|
|
||||||
|
|
||||||
## Dos and Don’ts
|
|
||||||
- Do use `chainLookupIgnoreClosure` / `chainLookupWithMembers` for ancestry traversals.
|
|
||||||
- Do maintain the resolution order above for predictable behavior.
|
|
||||||
- Don’t call virtual `get` while walking parents; it risks recursion across scope types.
|
|
||||||
- Don’t attach instance scopes to transient/pool frames; bind to a stable parent scope instead.
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@ It is as simple as:
|
|||||||
assertEquals( text, Lynon.decode(encodedBits) )
|
assertEquals( text, Lynon.decode(encodedBits) )
|
||||||
|
|
||||||
// compression was used automatically
|
// compression was used automatically
|
||||||
assert( text.length > encodedBits.toBuffer().size )
|
assert( text.length > (encodedBits.toBuffer() as Buffer).size )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
Any class you create is serializable by default; lynon serializes first constructor fields, then any `var` member fields.
|
Any class you create is serializable by default; lynon serializes first constructor fields, then any `var` member fields.
|
||||||
|
|||||||
241
docs/tutorial.md
241
docs/tutorial.md
@ -10,6 +10,7 @@ __Other documents to read__ maybe after this one:
|
|||||||
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
|
- [OOP notes](OOP.md), [exception handling](exceptions_handling.md)
|
||||||
- [math in Lyng](math.md), [the `when` statement](when.md), [return statement](return_statement.md)
|
- [math in Lyng](math.md), [the `when` statement](when.md), [return statement](return_statement.md)
|
||||||
- [Testing and Assertions](Testing.md)
|
- [Testing and Assertions](Testing.md)
|
||||||
|
- [Generics and type expressions](generics.md)
|
||||||
- [time](time.md) and [parallelism](parallelism.md)
|
- [time](time.md) and [parallelism](parallelism.md)
|
||||||
- [parallelism] - multithreaded code, coroutines, etc.
|
- [parallelism] - multithreaded code, coroutines, etc.
|
||||||
- Some class
|
- Some class
|
||||||
@ -106,6 +107,23 @@ Singleton objects are declared using the `object` keyword. They define a class a
|
|||||||
|
|
||||||
Logger.log("Hello singleton!")
|
Logger.log("Hello singleton!")
|
||||||
|
|
||||||
|
## Nested Declarations (short)
|
||||||
|
|
||||||
|
Classes, objects, and enums can be declared inside another class. They live in the class namespace (no outer instance capture), so you access them with a qualifier:
|
||||||
|
|
||||||
|
class A {
|
||||||
|
class B(x?)
|
||||||
|
object Inner { val foo = "bar" }
|
||||||
|
enum E* { One, Two }
|
||||||
|
}
|
||||||
|
|
||||||
|
val ab = A.B()
|
||||||
|
assertEquals(ab.x, null)
|
||||||
|
assertEquals(A.Inner.foo, "bar")
|
||||||
|
assertEquals(A.One, A.E.One)
|
||||||
|
|
||||||
|
See [OOP notes](OOP.md#nested-declarations) for rules, visibility, and enum lifting details.
|
||||||
|
|
||||||
## Delegation (briefly)
|
## Delegation (briefly)
|
||||||
|
|
||||||
You can delegate properties and functions to other objects using the `by` keyword. This is perfect for patterns like `lazy` initialization.
|
You can delegate properties and functions to other objects using the `by` keyword. This is perfect for patterns like `lazy` initialization.
|
||||||
@ -211,9 +229,8 @@ Naturally, assignment returns its value:
|
|||||||
rvalue means you cant assign the result if the assignment
|
rvalue means you cant assign the result if the assignment
|
||||||
|
|
||||||
var x
|
var x
|
||||||
assertThrows { (x = 11) = 5 }
|
// compile-time error: can't assign to rvalue
|
||||||
void
|
(x = 11) = 5
|
||||||
>>> void
|
|
||||||
|
|
||||||
This also prevents chain assignments so use parentheses:
|
This also prevents chain assignments so use parentheses:
|
||||||
|
|
||||||
@ -224,24 +241,36 @@ This also prevents chain assignments so use parentheses:
|
|||||||
|
|
||||||
## Nullability
|
## Nullability
|
||||||
|
|
||||||
|
Nullability is part of the type. `String` is non-null, `String?` is nullable. Use `!!` to assert non-null and throw
|
||||||
|
`NullReferenceException` if the value is `null`.
|
||||||
|
|
||||||
When the value is `null`, it might throws `NullReferenceException`, the name is somewhat a tradition. To avoid it
|
When the value is `null`, it might throws `NullReferenceException`, the name is somewhat a tradition. To avoid it
|
||||||
one can check it against null or use _null coalescing_. The null coalescing means, if the operand (left) is null,
|
one can check it against null or use _null coalescing_. The null coalescing means, if the operand (left) is null,
|
||||||
the operation won't be performed and the result will be null. Here is the difference:
|
the operation won't be performed and the result will be null. Here is the difference:
|
||||||
|
|
||||||
val ref = null
|
class Sample {
|
||||||
assertThrows { ref.field }
|
var field = 1
|
||||||
assertThrows { ref.method() }
|
fun method() { 2 }
|
||||||
assertThrows { ref.array[1] }
|
var list = [1, 2, 3]
|
||||||
assertThrows { ref[1] }
|
}
|
||||||
assertThrows { ref() }
|
|
||||||
|
val ref: Sample? = null
|
||||||
|
val list: List<Int>? = null
|
||||||
|
// direct access throws NullReferenceException:
|
||||||
|
// ref.field
|
||||||
|
// ref.method()
|
||||||
|
// ref.list[1]
|
||||||
|
// list[1]
|
||||||
|
|
||||||
assert( ref?.field == null )
|
assert( ref?.field == null )
|
||||||
assert( ref?.method() == null )
|
assert( ref?.method() == null )
|
||||||
assert( ref?.array?[1] == null )
|
assert( ref?.list?[1] == null )
|
||||||
assert( ref?[1] == null )
|
assert( list?[1] == null )
|
||||||
assert( ref?() == null )
|
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
|
Note: `?.` is still a typed operation. The receiver must have a compile-time type that declares the member; if the
|
||||||
|
receiver is `Object`, cast it first or declare a more specific type.
|
||||||
|
|
||||||
There is also "elvis operator", null-coalesce infix operator '?:' that returns rvalue if lvalue is `null`:
|
There is also "elvis operator", null-coalesce infix operator '?:' that returns rvalue if lvalue is `null`:
|
||||||
|
|
||||||
null ?: "nothing"
|
null ?: "nothing"
|
||||||
@ -298,8 +327,8 @@ Much like let, but it does not alter returned value:
|
|||||||
|
|
||||||
While it is not altering return value, the source object could be changed:
|
While it is not altering return value, the source object could be changed:
|
||||||
also
|
also
|
||||||
class Point(x,y)
|
class Point(var x: Int, var y: Int)
|
||||||
val p = Point(1,2).also { it.x++ }
|
val p: Point = Point(1,2).also { it.x++ }
|
||||||
assertEquals(p.x, 2)
|
assertEquals(p.x, 2)
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
@ -307,9 +336,9 @@ also
|
|||||||
|
|
||||||
It works much like `also`, but is executed in the context of the source object:
|
It works much like `also`, but is executed in the context of the source object:
|
||||||
|
|
||||||
class Point(x,y)
|
class Point(var x: Int, var y: Int)
|
||||||
// see the difference: apply changes this to newly created Point:
|
// see the difference: apply changes this to newly created Point:
|
||||||
val p = Point(1,2).apply { x++; y++ }
|
val p = Point(1,2).apply { this@Point.x++; this@Point.y++ }
|
||||||
assertEquals(p, Point(2,3))
|
assertEquals(p, Point(2,3))
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
@ -317,7 +346,7 @@ It works much like `also`, but is executed in the context of the source object:
|
|||||||
|
|
||||||
Sets `this` to the first argument and executes the block. Returns the value returned by the block:
|
Sets `this` to the first argument and executes the block. Returns the value returned by the block:
|
||||||
|
|
||||||
class Point(x,y)
|
class Point(var x: Int, var y: Int)
|
||||||
val p = Point(1,2)
|
val p = Point(1,2)
|
||||||
val sum = with(p) { x + y }
|
val sum = with(p) { x + y }
|
||||||
assertEquals(3, sum)
|
assertEquals(3, sum)
|
||||||
@ -425,8 +454,6 @@ Almost the same, using `val`:
|
|||||||
val foo = 1
|
val foo = 1
|
||||||
foo += 1 // this will throw exception
|
foo += 1 // this will throw exception
|
||||||
|
|
||||||
# Constants
|
|
||||||
|
|
||||||
Same as in kotlin:
|
Same as in kotlin:
|
||||||
|
|
||||||
val HalfPi = π / 2
|
val HalfPi = π / 2
|
||||||
@ -434,6 +461,151 @@ Same as in kotlin:
|
|||||||
Note using greek characters in identifiers! All letters allowed, but remember who might try to read your script, most
|
Note using greek characters in identifiers! All letters allowed, but remember who might try to read your script, most
|
||||||
likely will know some English, the rest is the pure uncertainty.
|
likely will know some English, the rest is the pure uncertainty.
|
||||||
|
|
||||||
|
# Types and inference
|
||||||
|
|
||||||
|
Lyng uses Kotlin-style static types with inference. You can always write explicit types, but in most places the compiler
|
||||||
|
can infer them from literals, defaults, and flow analysis.
|
||||||
|
|
||||||
|
## Type annotations
|
||||||
|
|
||||||
|
Use `:` to specify a type:
|
||||||
|
|
||||||
|
var x: Int = 10
|
||||||
|
val label: String = "count"
|
||||||
|
fun clamp(x: Int, min: Int, max: Int): Int { ... }
|
||||||
|
|
||||||
|
`Object` is the top type. If you omit a type and there is no default value, the parameter is `Object` by default:
|
||||||
|
|
||||||
|
fun show(x) { println(x) } // x is Object
|
||||||
|
|
||||||
|
For nullable types, add `?`:
|
||||||
|
|
||||||
|
fun showMaybe(x: Object?) { ... }
|
||||||
|
fun parseInt(s: String?): Int? { ... }
|
||||||
|
|
||||||
|
There is also a nullable shorthand for untyped parameters and constructor args: `x?` means `x: Object?`.
|
||||||
|
It cannot be combined with an explicit type annotation.
|
||||||
|
|
||||||
|
class A(x?) { ... } // x: Object?
|
||||||
|
fun f(x?) { x == null } // x: Object?
|
||||||
|
|
||||||
|
Type aliases name type expressions and can be generic:
|
||||||
|
|
||||||
|
type Num = Int | Real
|
||||||
|
type Maybe<T> = T?
|
||||||
|
|
||||||
|
Aliases expand to their underlying type expressions. See `docs/generics.md` for details.
|
||||||
|
|
||||||
|
`void` is a singleton value of the class `Void`. `Void` can be used as an explicit return type:
|
||||||
|
|
||||||
|
fun log(msg): Void { println(msg); void }
|
||||||
|
|
||||||
|
`Null` is the class of `null`. It is a singleton type and mostly useful for type inference results.
|
||||||
|
|
||||||
|
## Type inference
|
||||||
|
|
||||||
|
The compiler infers types from:
|
||||||
|
|
||||||
|
- literals: `1` is `Int`, `1.0` is `Real`, `"s"` is `String`, `'c'` is `Char`
|
||||||
|
- defaults: `fun f(x=1, name="n")` infers `x: Int`, `name: String`
|
||||||
|
- assignments: `val x = call()` uses the return type of `call`
|
||||||
|
- returns and branches: the result type of a block is the last expression, and if any branch is nullable,
|
||||||
|
the inferred type becomes nullable
|
||||||
|
- numeric ops: `Int` and `Real` stay `Int` when both sides are `Int`, and promote to `Real` on mixed arithmetic
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
fun inc(x=0) = x + 1 // (Int)->Int
|
||||||
|
fun maybe(flag) { if(flag) 1 else null } // ()->Int?
|
||||||
|
|
||||||
|
Function types are written as `(T1, T2, ...)->R`. You can include ellipsis in function *types* to
|
||||||
|
express a variadic position:
|
||||||
|
|
||||||
|
var fmt: (String, Object...)->String
|
||||||
|
var f: (Int, Object..., String)->Real
|
||||||
|
var anyArgs: (...)->Int // shorthand for (Object...)->Int
|
||||||
|
|
||||||
|
Untyped locals are allowed, but their type is fixed on the first assignment:
|
||||||
|
|
||||||
|
var x
|
||||||
|
x = 1 // x becomes Int
|
||||||
|
x = "one" // compile-time error
|
||||||
|
|
||||||
|
var y = null // y is Object?
|
||||||
|
val z = null // z is Null
|
||||||
|
|
||||||
|
Empty list/map literals default to `List<Object>` and `Map<Object,Object>` until a more specific type is known:
|
||||||
|
|
||||||
|
val xs = [] // List<Object>
|
||||||
|
val ys: List<Int> = [] // List<Int>
|
||||||
|
|
||||||
|
Map literals infer key/value types from entries; named keys are `String`. See `docs/generics.md` for details.
|
||||||
|
|
||||||
|
## Flow analysis
|
||||||
|
|
||||||
|
Lyng uses flow analysis to narrow types inside branches:
|
||||||
|
|
||||||
|
fun len(x: String?): Int {
|
||||||
|
if( x == null ) return 0
|
||||||
|
// x is String (non-null) in this branch
|
||||||
|
return x.length
|
||||||
|
}
|
||||||
|
|
||||||
|
`is` checks and `when` branches also narrow types:
|
||||||
|
|
||||||
|
fun kind(x: Object) {
|
||||||
|
if( x is Int ) return "int"
|
||||||
|
if( x is String ) return "string"
|
||||||
|
return "other"
|
||||||
|
}
|
||||||
|
|
||||||
|
Narrowing is local to the branch; after the branch, the original type is restored.
|
||||||
|
|
||||||
|
## Casts and unknown types
|
||||||
|
|
||||||
|
Use `as` for explicit casts. The compiler inserts casts only when it can be valid and necessary. If a cast fails at
|
||||||
|
runtime, it throws `ClassCastException`. If the value is nullable, `as T` implies a non-null assertion.
|
||||||
|
|
||||||
|
Member access is resolved at compile time. Only members of `Object` are available on unknown types; non-Object members
|
||||||
|
require an explicit cast:
|
||||||
|
|
||||||
|
fun f(x) { // x is Object
|
||||||
|
x.toString() // ok (Object member)
|
||||||
|
x.size() // compile-time error
|
||||||
|
(x as List).size() // ok
|
||||||
|
}
|
||||||
|
|
||||||
|
This avoids runtime name-resolution fallbacks; all symbols must be known at compile time.
|
||||||
|
|
||||||
|
## Generics and bounds
|
||||||
|
|
||||||
|
Generic parameters are declared with `<...>`:
|
||||||
|
|
||||||
|
fun id<T>(x: T): T = x
|
||||||
|
class Box<T>(val value: T)
|
||||||
|
|
||||||
|
Bounds use `:` and can combine with `&` (intersection) and `|` (union):
|
||||||
|
|
||||||
|
fun sum<T: Int | Real>(x: T, y: T) = x + y
|
||||||
|
class Named<T: Iterable & Comparable>(val data: T)
|
||||||
|
|
||||||
|
Type arguments are usually inferred from call sites:
|
||||||
|
|
||||||
|
val b = Box(10) // Box<Int>
|
||||||
|
val s = id("ok") // T is String
|
||||||
|
|
||||||
|
See [Generics and type expressions](generics.md) for bounds, unions/intersections, and type-checking rules.
|
||||||
|
|
||||||
|
## Variance
|
||||||
|
|
||||||
|
Generic types are invariant by default, so `List<Int>` is not a `List<Object>`.
|
||||||
|
Use declaration-site variance when you need it:
|
||||||
|
|
||||||
|
class Source<out T>(val value: T)
|
||||||
|
class Sink<in T> { fun accept(x: T) { ... } }
|
||||||
|
|
||||||
|
`out` makes the type covariant (only produced), `in` makes it contravariant (only consumed).
|
||||||
|
|
||||||
# Defining functions
|
# Defining functions
|
||||||
|
|
||||||
fun check(amount) {
|
fun check(amount) {
|
||||||
@ -475,8 +647,9 @@ There are default parameters in Lyng:
|
|||||||
It is possible to define also vararg using ellipsis:
|
It is possible to define also vararg using ellipsis:
|
||||||
|
|
||||||
fun sum(args...) {
|
fun sum(args...) {
|
||||||
var result = args[0]
|
val list = args as List
|
||||||
for( i in 1 ..< args.size ) result += args[i]
|
var result = list[0]
|
||||||
|
for( i in 1 ..< list.size ) result += list[i]
|
||||||
}
|
}
|
||||||
sum(10,20,30)
|
sum(10,20,30)
|
||||||
>>> 60
|
>>> 60
|
||||||
@ -569,6 +742,11 @@ one could be with ellipsis that means "the rest pf arguments as List":
|
|||||||
assert( { a, b...-> [a,...b] }(100, 1, 2, 3) == [100, 1, 2, 3])
|
assert( { a, b...-> [a,...b] }(100, 1, 2, 3) == [100, 1, 2, 3])
|
||||||
void
|
void
|
||||||
|
|
||||||
|
Type-annotated lambdas can use variadic *function types* as well:
|
||||||
|
|
||||||
|
val f: (Int, Object..., String)->Real = { a, rest..., b -> 0.0 }
|
||||||
|
val anyArgs: (...)->Int = { -> 0 }
|
||||||
|
|
||||||
### Using lambda as the parameter
|
### Using lambda as the parameter
|
||||||
|
|
||||||
See also: [Testing and Assertions](Testing.md)
|
See also: [Testing and Assertions](Testing.md)
|
||||||
@ -579,6 +757,7 @@ See also: [Testing and Assertions](Testing.md)
|
|||||||
var result = []
|
var result = []
|
||||||
for( x in iterable ) result += transform(x)
|
for( x in iterable ) result += transform(x)
|
||||||
}
|
}
|
||||||
|
// loop variables are read-only inside the loop body
|
||||||
assert( [11, 21, 31] == mapValues( [1,2,3], { it*10+1 }))
|
assert( [11, 21, 31] == mapValues( [1,2,3], { it*10+1 }))
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
@ -614,7 +793,7 @@ Lists can contain any type of objects, lists too:
|
|||||||
assert( list is Array ) // general interface
|
assert( list is Array ) // general interface
|
||||||
assert(list.size == 3)
|
assert(list.size == 3)
|
||||||
// second element is a list too:
|
// second element is a list too:
|
||||||
assert(list[1].size == 2)
|
assert((list[1] as List).size == 2)
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
Notice usage of indexing. You can use negative indexes to offset from the end of the list; see more in [Lists](List.md).
|
Notice usage of indexing. You can use negative indexes to offset from the end of the list; see more in [Lists](List.md).
|
||||||
@ -1062,8 +1241,8 @@ ends normally, without breaks. It allows override loop result value, for example
|
|||||||
to not calculate it in every iteration. For example, consider this naive prime number
|
to not calculate it in every iteration. For example, consider this naive prime number
|
||||||
test function (remember function return it's last expression result):
|
test function (remember function return it's last expression result):
|
||||||
|
|
||||||
fun naive_is_prime(candidate) {
|
fun naive_is_prime(candidate: Int) {
|
||||||
val x = if( candidate !is Int) candidate.toInt() else candidate
|
val x = candidate
|
||||||
var divisor = 1
|
var divisor = 1
|
||||||
while( ++divisor < x/2 || divisor == 2 ) {
|
while( ++divisor < x/2 || divisor == 2 ) {
|
||||||
if( x % divisor == 0 ) break false
|
if( x % divisor == 0 ) break false
|
||||||
@ -1138,8 +1317,9 @@ For loop are intended to traverse collections, and all other objects that suppor
|
|||||||
size and index access, like lists:
|
size and index access, like lists:
|
||||||
|
|
||||||
var letters = 0
|
var letters = 0
|
||||||
for( w in ["hello", "wolrd"]) {
|
val words: List<String> = ["hello", "world"]
|
||||||
letters += w.length
|
for( w in words) {
|
||||||
|
letters += (w as String).length
|
||||||
}
|
}
|
||||||
"total letters: "+letters
|
"total letters: "+letters
|
||||||
>>> "total letters: 10"
|
>>> "total letters: 10"
|
||||||
@ -1330,7 +1510,7 @@ than enum arrays, until `Lynon.encodeTyped` will be implemented.
|
|||||||
var result = null // here we will store the result
|
var result = null // here we will store the result
|
||||||
>>> null
|
>>> null
|
||||||
|
|
||||||
# Integral data types
|
# Built-in types
|
||||||
|
|
||||||
| type | description | literal samples |
|
| type | description | literal samples |
|
||||||
|--------|---------------------------------|---------------------|
|
|--------|---------------------------------|---------------------|
|
||||||
@ -1340,6 +1520,7 @@ than enum arrays, until `Lynon.encodeTyped` will be implemented.
|
|||||||
| Char | single unicode character | `'S'`, `'\n'` |
|
| Char | single unicode character | `'S'`, `'\n'` |
|
||||||
| String | unicode string, no limits | "hello" (see below) |
|
| String | unicode string, no limits | "hello" (see below) |
|
||||||
| List | mutable list | [1, "two", 3] |
|
| List | mutable list | [1, "two", 3] |
|
||||||
|
| Object | top type for all values | |
|
||||||
| Void | no value could exist, singleton | void |
|
| Void | no value could exist, singleton | void |
|
||||||
| Null | missing value, singleton | null |
|
| Null | missing value, singleton | null |
|
||||||
| Fn | callable type | |
|
| Fn | callable type | |
|
||||||
@ -1462,13 +1643,13 @@ Concatenation is a `+`: `"hello " + name` works as expected. No confusion. There
|
|||||||
|
|
||||||
Extraction:
|
Extraction:
|
||||||
|
|
||||||
"abcd42def"[ "\d+".re ].value
|
("abcd42def"[ "\d+".re ] as RegexMatch).value
|
||||||
>>> "42"
|
>>> "42"
|
||||||
|
|
||||||
Part match:
|
Part match:
|
||||||
|
|
||||||
assert( "abc foo def" =~ "f[oO]+".re )
|
assert( "abc foo def" =~ "f[oO]+".re )
|
||||||
assert( "foo" == $~.value )
|
assert( "foo" == ($~ as RegexMatch).value )
|
||||||
>>> void
|
>>> void
|
||||||
|
|
||||||
Repeating the fragment:
|
Repeating the fragment:
|
||||||
@ -1719,7 +1900,7 @@ You can add new methods and properties to existing classes without modifying the
|
|||||||
|
|
||||||
### Extension properties
|
### Extension properties
|
||||||
|
|
||||||
val Int.isEven = this % 2 == 0
|
val Int.isEven get() = this % 2 == 0
|
||||||
4.isEven
|
4.isEven
|
||||||
>>> true
|
>>> true
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
# What's New in Lyng
|
# What's New in Lyng
|
||||||
|
|
||||||
This document highlights the latest additions and improvements to the Lyng language and its ecosystem.
|
This document highlights the latest additions and improvements to the Lyng language and its ecosystem.
|
||||||
|
For a programmer-focused migration summary, see `docs/whats_new_1_5.md`.
|
||||||
|
|
||||||
## Language Features
|
## Language Features
|
||||||
|
|
||||||
@ -101,13 +102,31 @@ Singleton objects are declared using the `object` keyword. They provide a conven
|
|||||||
|
|
||||||
```lyng
|
```lyng
|
||||||
object Config {
|
object Config {
|
||||||
val version = "1.2.3"
|
val version = "1.5.0-SNAPSHOT"
|
||||||
fun show() = println("Config version: " + version)
|
fun show() = println("Config version: " + version)
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.show()
|
Config.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Nested Declarations and Lifted Enums
|
||||||
|
You can now declare classes, objects, enums, and type aliases inside another class. These nested declarations live in the class namespace (no outer instance capture) and are accessed with a qualifier.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
class A {
|
||||||
|
class B(x?)
|
||||||
|
object Inner { val foo = "bar" }
|
||||||
|
enum E* { One, Two }
|
||||||
|
}
|
||||||
|
|
||||||
|
val ab = A.B()
|
||||||
|
assertEquals(ab.x, null)
|
||||||
|
assertEquals(A.Inner.foo, "bar")
|
||||||
|
assertEquals(A.One, A.E.One)
|
||||||
|
```
|
||||||
|
|
||||||
|
The `*` on `enum E*` lifts entries into the enclosing class namespace (compile-time error on ambiguity).
|
||||||
|
|
||||||
### Object Expressions
|
### Object Expressions
|
||||||
You can now create anonymous objects that inherit from classes or interfaces using the `object : Base { ... }` syntax. These expressions capture their lexical scope and support multiple inheritance.
|
You can now create anonymous objects that inherit from classes or interfaces using the `object : Base { ... }` syntax. These expressions capture their lexical scope and support multiple inheritance.
|
||||||
|
|
||||||
@ -224,3 +243,11 @@ You can enable it in **Settings | Lyng Formatter | Enable Lyng autocompletion**.
|
|||||||
|
|
||||||
### Kotlin API: Exception Handling
|
### Kotlin API: Exception Handling
|
||||||
The `Obj.getLyngExceptionMessageWithStackTrace()` extension method has been added to simplify retrieving detailed error information from Lyng exception objects in Kotlin. Additionally, `getLyngExceptionMessage()` and `raiseAsExecutionError()` now accept an optional `Scope`, making it easier to use them when a scope is not immediately available.
|
The `Obj.getLyngExceptionMessageWithStackTrace()` extension method has been added to simplify retrieving detailed error information from Lyng exception objects in Kotlin. Additionally, `getLyngExceptionMessage()` and `raiseAsExecutionError()` now accept an optional `Scope`, making it easier to use them when a scope is not immediately available.
|
||||||
|
|
||||||
|
### Kotlin API: Bridge Reflection and Class Binding (Preferred Extensions)
|
||||||
|
Lyng now provides a public Kotlin reflection bridge and a Lyng‑first class binding workflow. This is the **preferred** way to write Kotlin extensions and library integrations:
|
||||||
|
|
||||||
|
- **Bridge resolver**: explicit handles for values, vars, and callables with predictable lookup rules.
|
||||||
|
- **Class bridge binding**: declare classes/members in Lyng (marked `extern`) and bind the implementations in Kotlin before the first instance is created.
|
||||||
|
|
||||||
|
See **Embedding Lyng** for full samples and usage details.
|
||||||
|
|||||||
133
docs/whats_new_1_3.md
Normal file
133
docs/whats_new_1_3.md
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
# What's New in Lyng 1.3 (vs 1.2.* / master)
|
||||||
|
|
||||||
|
This is a programmer-focused summary of what changed since the 1.2.* line on `master`. It highlights new language and type-system features, runtime/IDE improvements, and how to migrate code safely.
|
||||||
|
|
||||||
|
## Highlights
|
||||||
|
|
||||||
|
- Generics are now a first-class part of the type system, with bounds, variance, unions, and intersections.
|
||||||
|
- Type aliases and type-expression checks (`T1 is T2`, `A in T`) enable richer static modeling.
|
||||||
|
- Nested declarations inside classes, plus lifted enum entries via `enum E*`.
|
||||||
|
- Stepped ranges (`step`) including iterable open-ended and real ranges.
|
||||||
|
- Runtime and compiler speedups: more bytecode coverage, direct slot access, call-site caching.
|
||||||
|
|
||||||
|
## Language and type system
|
||||||
|
|
||||||
|
### Generics, bounds, and variance
|
||||||
|
|
||||||
|
You can declare generic functions/classes with `<...>`, restrict them with bounds, and control variance.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun id<T>(x: T): T = x
|
||||||
|
class Box<out T>(val value: T)
|
||||||
|
|
||||||
|
fun sum<T: Int | Real>(x: T, y: T) = x + y
|
||||||
|
class Named<T: Iterable & Comparable>(val data: T)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Type aliases and type expressions
|
||||||
|
|
||||||
|
Type aliases can name any type expression, including unions and intersections.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
type Num = Int | Real
|
||||||
|
type Maybe<T> = T?
|
||||||
|
```
|
||||||
|
|
||||||
|
Type expressions can be checked directly:
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun f<T>(xs: List<T>) {
|
||||||
|
assert( T is Int | String | Bool ) // type-subset check
|
||||||
|
assert( Int in T ) // same relation as `Int is T`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Value checks remain `x is T`. Type expression equality uses `==` and is structural.
|
||||||
|
|
||||||
|
### Nullable shorthand for parameters
|
||||||
|
|
||||||
|
Untyped parameters and constructor args can use `x?` as shorthand for `x: Object?`:
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
class A(x?) { ... }
|
||||||
|
fun f(x?) { x == null }
|
||||||
|
```
|
||||||
|
|
||||||
|
### List/map literal inference
|
||||||
|
|
||||||
|
The compiler now infers element and key/value types from literals and spreads. Mixed element types produce unions.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
val a = [1, 2, 3] // List<Int>
|
||||||
|
val b = [1, "two", true] // List<Int | String | Bool>
|
||||||
|
val m = { "a": 1, "b": "x" } // Map<String, Int | String>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Compile-time member access only
|
||||||
|
|
||||||
|
Member access is resolved at compile time. On unknown types, only `Object` members are visible; other members require an explicit cast.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun f(x) { // x: Object
|
||||||
|
x.toString() // ok
|
||||||
|
x.size() // compile-time error
|
||||||
|
(x as List).size()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This removes runtime name-resolution fallbacks and makes errors deterministic.
|
||||||
|
|
||||||
|
### Nested declarations and lifted enums
|
||||||
|
|
||||||
|
Classes, objects, enums, and type aliases can be declared inside another class and accessed by qualifier. Enums can lift entries into the outer namespace with `*`.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
class A {
|
||||||
|
class B(x?)
|
||||||
|
object Inner { val foo = "bar" }
|
||||||
|
type Alias = B
|
||||||
|
enum E* { One, Two }
|
||||||
|
}
|
||||||
|
|
||||||
|
val b = A.B()
|
||||||
|
assertEquals(A.One, A.E.One)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Stepped ranges
|
||||||
|
|
||||||
|
Ranges now support `step`, and open-ended/real ranges are iterable only with an explicit step.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
(1..5 step 2).toList() // [1,3,5]
|
||||||
|
(0.0..1.0 step 0.25).toList() // [0,0.25,0.5,0.75,1.0]
|
||||||
|
(0.. step 1).take(3).toList() // [0,1,2]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tooling and performance
|
||||||
|
|
||||||
|
- Bytecode compiler/VM coverage expanded (loops, expressions, calls), improving execution speed and consistency.
|
||||||
|
- Direct frame-slot access and scoped slot addressing reduce lookup overhead, including in closures.
|
||||||
|
- Call-site caching and numeric fast paths reduce hot-loop overhead.
|
||||||
|
- IDE tooling updated for the new type system and nested declarations; MiniAst-based completion work continues.
|
||||||
|
|
||||||
|
## Migration guide (from 1.2.*)
|
||||||
|
|
||||||
|
1) Replace dynamic member access on unknown types
|
||||||
|
- If you relied on runtime name resolution, add explicit casts or annotate types so the compiler can resolve members.
|
||||||
|
|
||||||
|
2) Adopt new type-system constructs where helpful
|
||||||
|
- Consider `type` aliases for complex unions/intersections.
|
||||||
|
- Prefer generic signatures over `Object` when the API is parametric.
|
||||||
|
|
||||||
|
3) Update range iteration where needed
|
||||||
|
- Use `step` for open-ended or real ranges you want to iterate.
|
||||||
|
|
||||||
|
4) Nullable shorthand is optional
|
||||||
|
- If you used untyped nullable params, you can keep `x` (Object) or switch to `x?` (Object?) for clarity.
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- `docs/generics.md`
|
||||||
|
- `docs/Range.md`
|
||||||
|
- `docs/OOP.md`
|
||||||
|
- `docs/BytecodeSpec.md`
|
||||||
140
docs/whats_new_1_5.md
Normal file
140
docs/whats_new_1_5.md
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
# What's New in Lyng 1.5 (vs 1.3.* / master)
|
||||||
|
|
||||||
|
This document summarizes the significant changes and new features introduced in the 1.5 development cycle.
|
||||||
|
|
||||||
|
## Principal changes
|
||||||
|
|
||||||
|
### JIT compiler and compile-time types and symbols.
|
||||||
|
|
||||||
|
This major improvement gives the following big advantages:
|
||||||
|
|
||||||
|
- **Blazing Fast execution**: several times faster! (three to six times speedup in different scenarios).
|
||||||
|
- **Better IDE support**: autocompletion, early error detection, types check.
|
||||||
|
- **Error safety**: all symbols and types are checked at bound at compile-time. Many errors are detected earlier. Also, no risk that external or caller code would shadow some internally used symbols (especially in closures and inheritance).
|
||||||
|
|
||||||
|
In particular, it means no slow and flaky runtime lookups. Once compiled, code guarantees that it will always call the symbol known at compile-time; runtime name lookup though does not guarantee it and can be source of hard to trace bugs.
|
||||||
|
|
||||||
|
### New stable API to create Kotlin extensions
|
||||||
|
|
||||||
|
The API is fixed and will be kept with further Lyng core changes. It is now the recommended way to write Lyng extensions in Kotlin. It is much simpler and more elegant than the internal one. See [Kotlin Bridge Binding](../notes/kotlin_bridge_binding.md).
|
||||||
|
|
||||||
|
### Smart types system
|
||||||
|
|
||||||
|
- **Deep inference**: The compiler analyzes types of symbols along the execution path and in many cases eliminates unnecessary casts or type specifications.
|
||||||
|
- **Union and intersection types**: `A & B`, `A | B`.
|
||||||
|
- **Generics**: Generic types are first-class citizens with support for [bounds and variance](generics.md). Type params are erased by default and are reified only when needed (e.g., `T::class`, `T is ...`, `as T`, or in extern-facing APIs), which enables checks like `A in T` when `T` is reified.
|
||||||
|
- **Inner classes and enums**: Full support for nested declarations, including [Enums with lifting](OOP.md#lifted-enum-entries).
|
||||||
|
|
||||||
|
## Other highlights
|
||||||
|
|
||||||
|
- **The `return` Statement**: Added support for local and non-local returns using labels.
|
||||||
|
- **Abstract Classes and Interfaces**: Full support for `abstract` members and the `interface` keyword.
|
||||||
|
- **Singleton Objects**: Define singletons using the `object` keyword or use anonymous object expressions.
|
||||||
|
- **Multiple Inheritance**: Enhanced multi-inheritance with predictable [C3 MRO resolution](OOP.md#multiple-inheritance-and-mro).
|
||||||
|
- **Unified Delegation**: Powerful delegation model for `val`, `var`, and `fun` members. See [Delegation](delegation.md).
|
||||||
|
- **Class Properties with Accessors**: Define `val` and `var` properties with custom `get()` and `set()`.
|
||||||
|
- **Restricted Setter Visibility**: Use `private set` and `protected set` on fields and properties.
|
||||||
|
- **Late-initialized `val`**: Support for `val` fields that are initialized in `init` blocks or class bodies.
|
||||||
|
- **Transient Members**: Use `@Transient` to exclude members from serialization and equality checks.
|
||||||
|
- **Named Arguments and Splats**: Improved call-site readability with `name: value` and map-based splats.
|
||||||
|
- **Refined Visibility**: Improved `protected` access and `closed` modifier for better encapsulation.
|
||||||
|
|
||||||
|
## Language Features
|
||||||
|
|
||||||
|
### The `return` Statement
|
||||||
|
You can now exit from the innermost enclosing callable (function or lambda) using `return`. Lyng also supports non-local returns to outer scopes using labels. See [Return Statement](return_statement.md).
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun findFirst<T>(list: Iterable<T>, predicate: (T)->Bool): T? {
|
||||||
|
list.forEach {
|
||||||
|
if (predicate(it)) return@findFirst it
|
||||||
|
}
|
||||||
|
null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Abstract Classes and Interfaces
|
||||||
|
Lyng now supports the `abstract` modifier for classes and their members. `interface` is introduced as a synonym for `abstract class`, allowing for rich multi-inheritance patterns.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
interface Shape {
|
||||||
|
abstract val area: Real
|
||||||
|
fun describe() = "Area: %g"(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
class Circle(val radius: Real) : Shape {
|
||||||
|
override val area get = Math.PI * radius * radius
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Class Properties with Accessors
|
||||||
|
Properties can now have custom getters and setters. They do not have automatic backing fields, making them perfect for computed values or delegation.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
class Rectangle(var width: Real, var height: Real) {
|
||||||
|
val area: Real get() = width * height
|
||||||
|
|
||||||
|
var squareSize: Real
|
||||||
|
get() = area
|
||||||
|
set(v) {
|
||||||
|
width = sqrt(v)
|
||||||
|
height = width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Singleton Objects
|
||||||
|
Declare singletons or anonymous objects easily.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
object Database {
|
||||||
|
val connection = "connected"
|
||||||
|
}
|
||||||
|
|
||||||
|
val runner = object : Runnable {
|
||||||
|
override fun run() = println("Running!")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Named Arguments and Named Splats
|
||||||
|
Improve call-site clarity by specifying argument names. You can also expand a Map into named arguments using the splat operator.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
fun configure(timeout: Int, retry: Int = 3) { ... }
|
||||||
|
|
||||||
|
configure(timeout: 5000, retry: 5)
|
||||||
|
val options = Map("timeout": 1000, "retry": 1)
|
||||||
|
configure(...options)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Modern Operators
|
||||||
|
The `?=` operator allows for concise "assign if null" logic.
|
||||||
|
|
||||||
|
```lyng
|
||||||
|
var cache: Map? = null
|
||||||
|
cache ?= Map("status": "ok") // Only assigns if cache is null
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tooling and IDE
|
||||||
|
|
||||||
|
- **IDEA Plugin**: Significant improvements to autocompletion, documentation tooltips, and natural language support (Grazie integration).
|
||||||
|
- **CLI**: The `lyng fmt` command is now a first-class tool for formatting code with various options like `--check` and `--in-place`.
|
||||||
|
- **Performance**: Ongoing optimizations in the bytecode VM and compiler for faster execution and smaller footprint.
|
||||||
|
|
||||||
|
## Standard Library
|
||||||
|
- **`with(self, block)`**: Scoped execution with a dedicated `self`.
|
||||||
|
- **`clamp(value, min, max)`**: Easily restrict values to a range.
|
||||||
|
|
||||||
|
## Migration Guide (from 1.3.*)
|
||||||
|
|
||||||
|
1. **Check Visibility**: Refined `protected` and `private` rules may catch previously undetected invalid accesses.
|
||||||
|
2. **Override Keyword**: Ensure all members that override ancestor declarations are marked with the `override` keyword (now mandatory).
|
||||||
|
3. **Return in Shorthand**: Remember that `return` is forbidden in `=` shorthand functions; use block syntax if you need early exit.
|
||||||
|
4. **Empty Map Literals**: Use `Map()` or `{:}` for empty maps, as `{}` is now strictly a block/lambda.
|
||||||
|
|
||||||
|
## References
|
||||||
|
- [Object Oriented Programming](OOP.md)
|
||||||
|
- [Generics](generics.md)
|
||||||
|
- [Return Statement](return_statement.md)
|
||||||
|
- [Delegation](delegation.md)
|
||||||
|
- [Tutorial](tutorial.md)
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#
|
#
|
||||||
# Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
# Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
@ -16,7 +16,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
#Gradle
|
#Gradle
|
||||||
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
|
org.gradle.jvmargs=-Xmx4096M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
|
||||||
org.gradle.caching=true
|
org.gradle.caching=true
|
||||||
org.gradle.configuration-cache=true
|
org.gradle.configuration-cache=true
|
||||||
#Kotlin
|
#Kotlin
|
||||||
|
|||||||
@ -35,12 +35,7 @@ import kotlinx.coroutines.CoroutineScope
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import net.sergeych.lyng.ExecutionError
|
|
||||||
import net.sergeych.lyng.Script
|
|
||||||
import net.sergeych.lyng.Source
|
|
||||||
import net.sergeych.lyng.idea.LyngIcons
|
import net.sergeych.lyng.idea.LyngIcons
|
||||||
import net.sergeych.lyng.obj.ObjVoid
|
|
||||||
import net.sergeych.lyng.obj.getLyngExceptionMessageWithStackTrace
|
|
||||||
|
|
||||||
class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
||||||
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
|
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
|
||||||
@ -58,7 +53,9 @@ class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
|||||||
val isLyng = psiFile?.name?.endsWith(".lyng") == true
|
val isLyng = psiFile?.name?.endsWith(".lyng") == true
|
||||||
e.presentation.isEnabledAndVisible = isLyng
|
e.presentation.isEnabledAndVisible = isLyng
|
||||||
if (isLyng) {
|
if (isLyng) {
|
||||||
e.presentation.text = "Run '${psiFile.name}'"
|
e.presentation.isEnabled = false
|
||||||
|
e.presentation.text = "Run '${psiFile.name}' (disabled)"
|
||||||
|
e.presentation.description = "Running scripts from the IDE is disabled; use the CLI."
|
||||||
} else {
|
} else {
|
||||||
e.presentation.text = "Run Lyng Script"
|
e.presentation.text = "Run Lyng Script"
|
||||||
}
|
}
|
||||||
@ -67,7 +64,6 @@ class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
|||||||
override fun actionPerformed(e: AnActionEvent) {
|
override fun actionPerformed(e: AnActionEvent) {
|
||||||
val project = e.project ?: return
|
val project = e.project ?: return
|
||||||
val psiFile = getPsiFile(e) ?: return
|
val psiFile = getPsiFile(e) ?: return
|
||||||
val text = psiFile.text
|
|
||||||
val fileName = psiFile.name
|
val fileName = psiFile.name
|
||||||
|
|
||||||
val (console, toolWindow) = getConsoleAndToolWindow(project)
|
val (console, toolWindow) = getConsoleAndToolWindow(project)
|
||||||
@ -75,40 +71,9 @@ class RunLyngScriptAction : AnAction(LyngIcons.FILE) {
|
|||||||
|
|
||||||
toolWindow.show {
|
toolWindow.show {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
console.print("--- Run is disabled ---\n", ConsoleViewContentType.SYSTEM_OUTPUT)
|
||||||
val lyngScope = Script.newScope()
|
console.print("Lyng now runs in bytecode-only mode; the IDE no longer evaluates scripts.\n", ConsoleViewContentType.NORMAL_OUTPUT)
|
||||||
lyngScope.addFn("print") {
|
console.print("Use the CLI to run scripts, e.g. `lyng run $fileName`.\n", ConsoleViewContentType.NORMAL_OUTPUT)
|
||||||
val sb = StringBuilder()
|
|
||||||
for ((i, arg) in args.list.withIndex()) {
|
|
||||||
if (i > 0) sb.append(" ")
|
|
||||||
sb.append(arg.toString(this).value)
|
|
||||||
}
|
|
||||||
console.print(sb.toString(), ConsoleViewContentType.NORMAL_OUTPUT)
|
|
||||||
ObjVoid
|
|
||||||
}
|
|
||||||
lyngScope.addFn("println") {
|
|
||||||
val sb = StringBuilder()
|
|
||||||
for ((i, arg) in args.list.withIndex()) {
|
|
||||||
if (i > 0) sb.append(" ")
|
|
||||||
sb.append(arg.toString(this).value)
|
|
||||||
}
|
|
||||||
console.print(sb.toString() + "\n", ConsoleViewContentType.NORMAL_OUTPUT)
|
|
||||||
ObjVoid
|
|
||||||
}
|
|
||||||
|
|
||||||
console.print("--- Running $fileName ---\n", ConsoleViewContentType.SYSTEM_OUTPUT)
|
|
||||||
val result = lyngScope.eval(Source(fileName, text))
|
|
||||||
console.print("\n--- Finished with result: ${result.inspect(lyngScope)} ---\n", ConsoleViewContentType.SYSTEM_OUTPUT)
|
|
||||||
} catch (t: Throwable) {
|
|
||||||
console.print("\n--- Error ---\n", ConsoleViewContentType.ERROR_OUTPUT)
|
|
||||||
if( t is ExecutionError ) {
|
|
||||||
val m = t.errorObject.getLyngExceptionMessageWithStackTrace()
|
|
||||||
console.print(m, ConsoleViewContentType.ERROR_OUTPUT)
|
|
||||||
}
|
|
||||||
else
|
|
||||||
console.print(t.message ?: t.toString(), ConsoleViewContentType.ERROR_OUTPUT)
|
|
||||||
console.print("\n", ConsoleViewContentType.ERROR_OUTPUT)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,15 +25,13 @@ import com.intellij.openapi.progress.ProgressManager
|
|||||||
import com.intellij.openapi.util.Key
|
import com.intellij.openapi.util.Key
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import net.sergeych.lyng.Source
|
|
||||||
import net.sergeych.lyng.binding.Binder
|
|
||||||
import net.sergeych.lyng.binding.SymbolKind
|
|
||||||
import net.sergeych.lyng.highlight.HighlightKind
|
import net.sergeych.lyng.highlight.HighlightKind
|
||||||
import net.sergeych.lyng.highlight.SimpleLyngHighlighter
|
|
||||||
import net.sergeych.lyng.highlight.offsetOf
|
import net.sergeych.lyng.highlight.offsetOf
|
||||||
import net.sergeych.lyng.idea.highlight.LyngHighlighterColors
|
import net.sergeych.lyng.idea.highlight.LyngHighlighterColors
|
||||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.tools.LyngDiagnosticSeverity
|
||||||
|
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||||
|
import net.sergeych.lyng.tools.LyngSemanticKind
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ExternalAnnotator that runs Lyng MiniAst on the document text in background
|
* ExternalAnnotator that runs Lyng MiniAst on the document text in background
|
||||||
@ -43,8 +41,8 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
data class Input(val text: String, val modStamp: Long, val previousSpans: List<Span>?, val file: PsiFile)
|
data class Input(val text: String, val modStamp: Long, val previousSpans: List<Span>?, val file: PsiFile)
|
||||||
|
|
||||||
data class Span(val start: Int, val end: Int, val key: com.intellij.openapi.editor.colors.TextAttributesKey)
|
data class Span(val start: Int, val end: Int, val key: com.intellij.openapi.editor.colors.TextAttributesKey)
|
||||||
data class Error(val start: Int, val end: Int, val message: String)
|
data class Diag(val start: Int, val end: Int, val message: String, val severity: HighlightSeverity)
|
||||||
data class Result(val modStamp: Long, val spans: List<Span>, val error: Error? = null)
|
data class Result(val modStamp: Long, val spans: List<Span>, val diagnostics: List<Diag> = emptyList())
|
||||||
|
|
||||||
override fun collectInformation(file: PsiFile): Input? {
|
override fun collectInformation(file: PsiFile): Input? {
|
||||||
val doc: Document = file.viewProvider.document ?: return null
|
val doc: Document = file.viewProvider.document ?: return null
|
||||||
@ -59,224 +57,46 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
if (collectedInfo == null) return null
|
if (collectedInfo == null) return null
|
||||||
ProgressManager.checkCanceled()
|
ProgressManager.checkCanceled()
|
||||||
val text = collectedInfo.text
|
val text = collectedInfo.text
|
||||||
val tokens = try { SimpleLyngHighlighter().highlight(text) } catch (_: Throwable) { emptyList() }
|
val analysis = LyngAstManager.getAnalysis(collectedInfo.file)
|
||||||
|
|
||||||
// Use LyngAstManager to get the (potentially merged) Mini-AST
|
|
||||||
val mini = LyngAstManager.getMiniAst(collectedInfo.file)
|
|
||||||
?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList())
|
?: return Result(collectedInfo.modStamp, collectedInfo.previousSpans ?: emptyList())
|
||||||
|
val mini = analysis.mini
|
||||||
|
|
||||||
ProgressManager.checkCanceled()
|
ProgressManager.checkCanceled()
|
||||||
val source = Source(collectedInfo.file.name, text)
|
|
||||||
|
|
||||||
val out = ArrayList<Span>(256)
|
val out = ArrayList<Span>(256)
|
||||||
|
val diags = ArrayList<Diag>()
|
||||||
fun isFollowedByParenOrBlock(rangeEnd: Int): Boolean {
|
|
||||||
var i = rangeEnd
|
|
||||||
while (i < text.length) {
|
|
||||||
val ch = text[i]
|
|
||||||
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { i++; continue }
|
|
||||||
return ch == '(' || ch == '{'
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
|
fun putRange(start: Int, end: Int, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
|
||||||
if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key)
|
if (start in 0..end && end <= text.length && start < end) out += Span(start, end, key)
|
||||||
}
|
}
|
||||||
fun putName(startPos: net.sergeych.lyng.Pos, name: String, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
|
|
||||||
val s = source.offsetOf(startPos)
|
fun keyForKind(kind: LyngSemanticKind): com.intellij.openapi.editor.colors.TextAttributesKey? = when (kind) {
|
||||||
putRange(s, (s + name.length).coerceAtMost(text.length), key)
|
LyngSemanticKind.Function -> LyngHighlighterColors.FUNCTION
|
||||||
}
|
LyngSemanticKind.Class, LyngSemanticKind.Enum, LyngSemanticKind.TypeAlias -> LyngHighlighterColors.TYPE
|
||||||
fun putMiniRange(r: MiniRange, key: com.intellij.openapi.editor.colors.TextAttributesKey) {
|
LyngSemanticKind.Value -> LyngHighlighterColors.VALUE
|
||||||
val s = source.offsetOf(r.start)
|
LyngSemanticKind.Variable -> LyngHighlighterColors.VARIABLE
|
||||||
val e = source.offsetOf(r.end)
|
LyngSemanticKind.Parameter -> LyngHighlighterColors.PARAMETER
|
||||||
putRange(s, e, key)
|
LyngSemanticKind.TypeRef -> LyngHighlighterColors.TYPE
|
||||||
|
LyngSemanticKind.EnumConstant -> LyngHighlighterColors.ENUM_CONSTANT
|
||||||
}
|
}
|
||||||
|
|
||||||
// Declarations
|
// Semantic highlights from shared tooling
|
||||||
mini.declarations.forEach { d ->
|
LyngLanguageTools.semanticHighlights(analysis).forEach { span ->
|
||||||
if (d.nameStart.source != source) return@forEach
|
keyForKind(span.kind)?.let { putRange(span.range.start, span.range.endExclusive, it) }
|
||||||
when (d) {
|
|
||||||
is MiniFunDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.FUNCTION_DECLARATION)
|
|
||||||
is MiniClassDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE)
|
|
||||||
is MiniValDecl -> putName(
|
|
||||||
d.nameStart,
|
|
||||||
d.name,
|
|
||||||
if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
|
|
||||||
)
|
|
||||||
is MiniEnumDecl -> putName(d.nameStart, d.name, LyngHighlighterColors.TYPE)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Imports: each segment as namespace/path
|
// Imports: each segment as namespace/path
|
||||||
mini.imports.forEach { imp ->
|
mini?.imports?.forEach { imp ->
|
||||||
if (imp.range.start.source != source) return@forEach
|
imp.segments.forEach { seg ->
|
||||||
imp.segments.forEach { seg -> putMiniRange(seg.range, LyngHighlighterColors.NAMESPACE) }
|
val start = analysis.source.offsetOf(seg.range.start)
|
||||||
|
val end = analysis.source.offsetOf(seg.range.end)
|
||||||
|
putRange(start, end, LyngHighlighterColors.NAMESPACE)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameters
|
|
||||||
fun addParams(params: List<MiniParam>) {
|
|
||||||
params.forEach { p ->
|
|
||||||
if (p.nameStart.source == source)
|
|
||||||
putName(p.nameStart, p.name, LyngHighlighterColors.PARAMETER)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mini.declarations.forEach { d ->
|
|
||||||
when (d) {
|
|
||||||
is MiniFunDecl -> addParams(d.params)
|
|
||||||
is MiniClassDecl -> d.members.filterIsInstance<MiniMemberFunDecl>().forEach { addParams(it.params) }
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type name segments (including generics base & args)
|
|
||||||
fun addTypeSegments(t: MiniTypeRef?) {
|
|
||||||
when (t) {
|
|
||||||
is MiniTypeName -> t.segments.forEach { seg ->
|
|
||||||
if (seg.range.start.source != source) return@forEach
|
|
||||||
val s = source.offsetOf(seg.range.start)
|
|
||||||
putRange(s, (s + seg.name.length).coerceAtMost(text.length), LyngHighlighterColors.TYPE)
|
|
||||||
}
|
|
||||||
is MiniGenericType -> {
|
|
||||||
addTypeSegments(t.base)
|
|
||||||
t.args.forEach { addTypeSegments(it) }
|
|
||||||
}
|
|
||||||
is MiniFunctionType -> {
|
|
||||||
t.receiver?.let { addTypeSegments(it) }
|
|
||||||
t.params.forEach { addTypeSegments(it) }
|
|
||||||
addTypeSegments(t.returnType)
|
|
||||||
}
|
|
||||||
is MiniTypeVar -> { /* name is in range; could be highlighted as TYPE as well */
|
|
||||||
if (t.range.start.source == source)
|
|
||||||
putMiniRange(t.range, LyngHighlighterColors.TYPE)
|
|
||||||
}
|
|
||||||
null -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fun addDeclTypeSegments(d: MiniDecl) {
|
|
||||||
if (d.nameStart.source != source) return
|
|
||||||
when (d) {
|
|
||||||
is MiniFunDecl -> {
|
|
||||||
addTypeSegments(d.returnType)
|
|
||||||
d.params.forEach { addTypeSegments(it.type) }
|
|
||||||
addTypeSegments(d.receiver)
|
|
||||||
}
|
|
||||||
is MiniValDecl -> {
|
|
||||||
addTypeSegments(d.type)
|
|
||||||
addTypeSegments(d.receiver)
|
|
||||||
}
|
|
||||||
is MiniClassDecl -> {
|
|
||||||
d.ctorFields.forEach { addTypeSegments(it.type) }
|
|
||||||
d.classFields.forEach { addTypeSegments(it.type) }
|
|
||||||
for (m in d.members) {
|
|
||||||
when (m) {
|
|
||||||
is MiniMemberFunDecl -> {
|
|
||||||
addTypeSegments(m.returnType)
|
|
||||||
m.params.forEach { addTypeSegments(it.type) }
|
|
||||||
}
|
|
||||||
is MiniMemberValDecl -> {
|
|
||||||
addTypeSegments(m.type)
|
|
||||||
}
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is MiniEnumDecl -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mini.declarations.forEach { d -> addDeclTypeSegments(d) }
|
|
||||||
|
|
||||||
ProgressManager.checkCanceled()
|
|
||||||
|
|
||||||
// Semantic usages via Binder (best-effort)
|
|
||||||
try {
|
|
||||||
val binding = Binder.bind(text, mini)
|
|
||||||
|
|
||||||
// Map declaration ranges to avoid duplicating them as usages
|
|
||||||
val declKeys = HashSet<Pair<Int, Int>>(binding.symbols.size * 2)
|
|
||||||
binding.symbols.forEach { sym -> declKeys += (sym.declStart to sym.declEnd) }
|
|
||||||
|
|
||||||
fun keyForKind(k: SymbolKind) = when (k) {
|
|
||||||
SymbolKind.Function -> LyngHighlighterColors.FUNCTION
|
|
||||||
SymbolKind.Class, SymbolKind.Enum -> LyngHighlighterColors.TYPE
|
|
||||||
SymbolKind.Parameter -> LyngHighlighterColors.PARAMETER
|
|
||||||
SymbolKind.Value -> LyngHighlighterColors.VALUE
|
|
||||||
SymbolKind.Variable -> LyngHighlighterColors.VARIABLE
|
|
||||||
}
|
|
||||||
|
|
||||||
// Track covered ranges to not override later heuristics
|
|
||||||
val covered = HashSet<Pair<Int, Int>>()
|
|
||||||
|
|
||||||
binding.references.forEach { ref ->
|
|
||||||
val key = ref.start to ref.end
|
|
||||||
if (!declKeys.contains(key)) {
|
|
||||||
val sym = binding.symbols.firstOrNull { it.id == ref.symbolId }
|
|
||||||
if (sym != null) {
|
|
||||||
val color = keyForKind(sym.kind)
|
|
||||||
putRange(ref.start, ref.end, color)
|
|
||||||
covered += key
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Heuristics on top of binder: function call-sites and simple name-based roles
|
|
||||||
ProgressManager.checkCanceled()
|
|
||||||
|
|
||||||
// Build simple name -> role map for top-level vals/vars and parameters
|
|
||||||
val nameRole = HashMap<String, com.intellij.openapi.editor.colors.TextAttributesKey>(8)
|
|
||||||
mini.declarations.forEach { d ->
|
|
||||||
when (d) {
|
|
||||||
is MiniValDecl -> nameRole[d.name] =
|
|
||||||
if (d.mutable) LyngHighlighterColors.VARIABLE else LyngHighlighterColors.VALUE
|
|
||||||
|
|
||||||
is MiniFunDecl -> d.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER }
|
|
||||||
is MiniClassDecl -> {
|
|
||||||
d.members.forEach { m ->
|
|
||||||
if (m is MiniMemberFunDecl) {
|
|
||||||
m.params.forEach { p -> nameRole[p.name] = LyngHighlighterColors.PARAMETER }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tokens.forEach { s ->
|
|
||||||
if (s.kind == HighlightKind.Identifier) {
|
|
||||||
val start = s.range.start
|
|
||||||
val end = s.range.endExclusive
|
|
||||||
val key = start to end
|
|
||||||
if (key !in covered && key !in declKeys) {
|
|
||||||
// Call-site detection first so it wins over var/param role
|
|
||||||
if (isFollowedByParenOrBlock(end)) {
|
|
||||||
putRange(start, end, LyngHighlighterColors.FUNCTION)
|
|
||||||
covered += key
|
|
||||||
} else {
|
|
||||||
// Simple role by known names
|
|
||||||
val ident = try {
|
|
||||||
text.substring(start, end)
|
|
||||||
} catch (_: Throwable) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
if (ident != null) {
|
|
||||||
val roleKey = nameRole[ident]
|
|
||||||
if (roleKey != null) {
|
|
||||||
putRange(start, end, roleKey)
|
|
||||||
covered += key
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
// Must rethrow cancellation; otherwise ignore binder failures (best-effort)
|
|
||||||
if (e is com.intellij.openapi.progress.ProcessCanceledException) throw e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add annotation/label coloring using token highlighter
|
// Add annotation/label coloring using token highlighter
|
||||||
run {
|
run {
|
||||||
tokens.forEach { s ->
|
analysis.lexicalHighlights.forEach { s ->
|
||||||
if (s.kind == HighlightKind.Label) {
|
if (s.kind == HighlightKind.Label) {
|
||||||
val start = s.range.start
|
val start = s.range.start
|
||||||
val end = s.range.endExclusive
|
val end = s.range.endExclusive
|
||||||
@ -302,7 +122,7 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
text.substring(wStart, wEnd)
|
text.substring(wStart, wEnd)
|
||||||
} else null
|
} else null
|
||||||
|
|
||||||
if (prevWord in setOf("return", "break", "continue") || isFollowedByParenOrBlock(end)) {
|
if (prevWord in setOf("return", "break", "continue")) {
|
||||||
putRange(start, end, LyngHighlighterColors.LABEL)
|
putRange(start, end, LyngHighlighterColors.LABEL)
|
||||||
} else {
|
} else {
|
||||||
putRange(start, end, LyngHighlighterColors.ANNOTATION)
|
putRange(start, end, LyngHighlighterColors.ANNOTATION)
|
||||||
@ -315,17 +135,13 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens.forEach { s ->
|
analysis.diagnostics.forEach { d ->
|
||||||
if (s.kind == HighlightKind.EnumConstant) {
|
val range = d.range ?: return@forEach
|
||||||
val start = s.range.start
|
val severity = if (d.severity == LyngDiagnosticSeverity.Warning) HighlightSeverity.WARNING else HighlightSeverity.ERROR
|
||||||
val end = s.range.endExclusive
|
diags += Diag(range.start, range.endExclusive, d.message, severity)
|
||||||
if (start in 0..end && end <= text.length && start < end) {
|
|
||||||
putRange(start, end, LyngHighlighterColors.ENUM_CONSTANT)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result(collectedInfo.modStamp, out, null)
|
return Result(collectedInfo.modStamp, out, diags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -346,13 +162,12 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
.create()
|
.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show syntax error if present
|
// Show errors and warnings
|
||||||
val err = result.error
|
result.diagnostics.forEach { d ->
|
||||||
if (err != null) {
|
val start = d.start.coerceIn(0, (doc?.textLength ?: 0))
|
||||||
val start = err.start.coerceIn(0, (doc?.textLength ?: 0))
|
val end = d.end.coerceIn(start, (doc?.textLength ?: start))
|
||||||
val end = err.end.coerceIn(start, (doc?.textLength ?: start))
|
|
||||||
if (end > start) {
|
if (end > start) {
|
||||||
holder.newAnnotation(HighlightSeverity.ERROR, err.message)
|
holder.newAnnotation(d.severity, d.message)
|
||||||
.range(TextRange(start, end))
|
.range(TextRange(start, end))
|
||||||
.create()
|
.create()
|
||||||
}
|
}
|
||||||
@ -373,30 +188,5 @@ class LyngExternalAnnotator : ExternalAnnotator<LyngExternalAnnotator.Input, Lyn
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Make the error highlight a bit wider than a single character so it is easier to see and click.
|
|
||||||
* Strategy:
|
|
||||||
* - If the offset points inside an identifier-like token (letters/digits/underscore), expand to the full token.
|
|
||||||
* - Otherwise select a small range starting at the offset with a minimum width, but not crossing the line end.
|
|
||||||
*/
|
|
||||||
private fun expandErrorRange(text: String, rawStart: Int): Pair<Int, Int> {
|
|
||||||
if (text.isEmpty()) return 0 to 0
|
|
||||||
val len = text.length
|
|
||||||
val start = rawStart.coerceIn(0, len)
|
|
||||||
fun isWord(ch: Char) = ch == '_' || ch.isLetterOrDigit()
|
|
||||||
|
|
||||||
if (start < len && isWord(text[start])) {
|
|
||||||
var s = start
|
|
||||||
var e = start
|
|
||||||
while (s > 0 && isWord(text[s - 1])) s--
|
|
||||||
while (e < len && isWord(text[e])) e++
|
|
||||||
return s to e
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not inside a word: select a short, visible range up to EOL
|
|
||||||
val lineEnd = text.indexOf('\n', start).let { if (it == -1) len else it }
|
|
||||||
val minWidth = 4
|
|
||||||
val end = (start + minWidth).coerceAtMost(lineEnd).coerceAtLeast((start + 1).coerceAtMost(lineEnd))
|
|
||||||
return start to end
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -96,9 +96,10 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
log.info("[LYNG_DEBUG] Completion: caret=$caret prefix='${prefix}' memberDotPos=${memberDotPos} file='${file.name}'")
|
log.info("[LYNG_DEBUG] Completion: caret=$caret prefix='${prefix}' memberDotPos=${memberDotPos} file='${file.name}'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build MiniAst (cached) for both global and member contexts to enable local class/val inference
|
// Build analysis (cached) for both global and member contexts to enable local class/val inference
|
||||||
val mini = LyngAstManager.getMiniAst(file)
|
val analysis = LyngAstManager.getAnalysis(file)
|
||||||
val binding = LyngAstManager.getBinding(file)
|
val mini = analysis?.mini
|
||||||
|
val binding = analysis?.binding
|
||||||
|
|
||||||
// Delegate computation to the shared engine to keep behavior in sync with tests
|
// Delegate computation to the shared engine to keep behavior in sync with tests
|
||||||
val engineItems = try {
|
val engineItems = try {
|
||||||
@ -121,6 +122,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
fromText.forEach { add(it) }
|
fromText.forEach { add(it) }
|
||||||
add("lyng.stdlib")
|
add("lyng.stdlib")
|
||||||
}.toList()
|
}.toList()
|
||||||
|
val staticOnly = DocLookupUtils.isStaticReceiver(mini, text, memberDotPos, imported, binding)
|
||||||
|
|
||||||
// Try inferring return/receiver class around the dot
|
// Try inferring return/receiver class around the dot
|
||||||
val inferred =
|
val inferred =
|
||||||
@ -135,7 +137,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
|
|
||||||
if (inferred != null) {
|
if (inferred != null) {
|
||||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback inferred receiver/return class='$inferred' — offering its members")
|
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback inferred receiver/return class='$inferred' — offering its members")
|
||||||
offerMembers(emit, imported, inferred, sourceText = text, mini = mini)
|
offerMembers(emit, imported, inferred, staticOnly = staticOnly, sourceText = text, mini = mini)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback could not infer class; keeping list empty (no globals after dot)")
|
if (DEBUG_COMPLETION) log.info("[LYNG_DEBUG] Fallback could not infer class; keeping list empty (no globals after dot)")
|
||||||
@ -160,6 +162,8 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
.withIcon(AllIcons.Nodes.Class)
|
.withIcon(AllIcons.Nodes.Class)
|
||||||
Kind.Enum -> LookupElementBuilder.create(ci.name)
|
Kind.Enum -> LookupElementBuilder.create(ci.name)
|
||||||
.withIcon(AllIcons.Nodes.Enum)
|
.withIcon(AllIcons.Nodes.Enum)
|
||||||
|
Kind.TypeAlias -> LookupElementBuilder.create(ci.name)
|
||||||
|
.withIcon(AllIcons.Nodes.Class)
|
||||||
Kind.Value -> LookupElementBuilder.create(ci.name)
|
Kind.Value -> LookupElementBuilder.create(ci.name)
|
||||||
.withIcon(AllIcons.Nodes.Variable)
|
.withIcon(AllIcons.Nodes.Variable)
|
||||||
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
.let { b -> if (!ci.typeText.isNullOrBlank()) b.withTypeText(ci.typeText, true) else b }
|
||||||
@ -292,6 +296,9 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
}
|
}
|
||||||
is MiniEnumDecl -> LookupElementBuilder.create(name)
|
is MiniEnumDecl -> LookupElementBuilder.create(name)
|
||||||
.withIcon(AllIcons.Nodes.Enum)
|
.withIcon(AllIcons.Nodes.Enum)
|
||||||
|
is MiniTypeAliasDecl -> LookupElementBuilder.create(name)
|
||||||
|
.withIcon(AllIcons.Nodes.Class)
|
||||||
|
.withTypeText(typeOf(d.target), true)
|
||||||
}
|
}
|
||||||
emit(builder)
|
emit(builder)
|
||||||
}
|
}
|
||||||
@ -369,6 +376,7 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
when (m) {
|
when (m) {
|
||||||
is MiniMemberFunDecl -> if (!m.isStatic) continue
|
is MiniMemberFunDecl -> if (!m.isStatic) continue
|
||||||
is MiniMemberValDecl -> if (!m.isStatic) continue
|
is MiniMemberValDecl -> if (!m.isStatic) continue
|
||||||
|
is MiniMemberTypeAliasDecl -> if (!m.isStatic) continue
|
||||||
is MiniInitDecl -> continue
|
is MiniInitDecl -> continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -458,6 +466,16 @@ class LyngCompletionContributor : CompletionContributor() {
|
|||||||
emit(builder)
|
emit(builder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
is MiniMemberTypeAliasDecl -> {
|
||||||
|
val builder = LookupElementBuilder.create(name)
|
||||||
|
.withIcon(AllIcons.Nodes.Class)
|
||||||
|
.withTypeText(typeOf(rep.target), true)
|
||||||
|
if (groupPriority != 0.0) {
|
||||||
|
emit(PrioritizedLookupElement.withPriority(builder, groupPriority))
|
||||||
|
} else {
|
||||||
|
emit(builder)
|
||||||
|
}
|
||||||
|
}
|
||||||
is MiniInitDecl -> {}
|
is MiniInitDecl -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,11 +24,14 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import net.sergeych.lyng.highlight.offsetOf
|
import net.sergeych.lyng.highlight.offsetOf
|
||||||
import net.sergeych.lyng.idea.LyngLanguage
|
import net.sergeych.lyng.idea.LyngLanguage
|
||||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||||
import net.sergeych.lyng.idea.util.TextCtx
|
import net.sergeych.lyng.idea.util.TextCtx
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.miniast.*
|
||||||
|
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||||
|
import net.sergeych.lyng.tools.LyngSymbolInfo
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Quick Docs backed by MiniAst: when caret is on an identifier that corresponds
|
* Quick Docs backed by MiniAst: when caret is on an identifier that corresponds
|
||||||
@ -66,9 +69,59 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: ident='$ident' at ${idRange.startOffset}..${idRange.endOffset} in ${file.name}")
|
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: ident='$ident' at ${idRange.startOffset}..${idRange.endOffset} in ${file.name}")
|
||||||
|
|
||||||
// 1. Get merged mini-AST from Manager (handles local + .lyng.d merged declarations)
|
// 1. Get merged mini-AST from Manager (handles local + .lyng.d merged declarations)
|
||||||
val mini = LyngAstManager.getMiniAst(file) ?: return null
|
val analysis = LyngAstManager.getAnalysis(file) ?: return null
|
||||||
|
val mini = analysis.mini ?: return null
|
||||||
val miniSource = mini.range.start.source
|
val miniSource = mini.range.start.source
|
||||||
val imported = DocLookupUtils.canonicalImportedModules(mini, text)
|
val imported = analysis.importedModules.ifEmpty { DocLookupUtils.canonicalImportedModules(mini, text) }
|
||||||
|
|
||||||
|
// Single-source quick doc lookup
|
||||||
|
LyngLanguageTools.docAt(analysis, offset)?.let { info ->
|
||||||
|
val enriched = if (info.doc == null) {
|
||||||
|
findDocInDeclarationFiles(file, info.target.containerName, info.target.name)
|
||||||
|
?.let { info.copy(doc = it) } ?: info
|
||||||
|
} else {
|
||||||
|
info
|
||||||
|
}
|
||||||
|
renderDocFromInfo(enriched)?.let { return it }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: resolve references against merged MiniAst (including .lyng.d) when binder cannot
|
||||||
|
run {
|
||||||
|
val dotPos = DocLookupUtils.findDotLeft(text, idRange.startOffset)
|
||||||
|
if (dotPos != null) {
|
||||||
|
val receiverClass = DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, imported, analysis.binding)
|
||||||
|
?: DocLookupUtils.guessReceiverClass(text, dotPos, imported, mini)
|
||||||
|
if (receiverClass != null) {
|
||||||
|
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported, receiverClass, ident, mini)
|
||||||
|
if (resolved != null) {
|
||||||
|
val owner = resolved.first
|
||||||
|
val member = resolved.second
|
||||||
|
val withDoc = if (member.doc == null) {
|
||||||
|
findDocInDeclarationFiles(file, owner, member.name)?.let { doc ->
|
||||||
|
when (member) {
|
||||||
|
is MiniMemberFunDecl -> member.copy(doc = doc)
|
||||||
|
is MiniMemberValDecl -> member.copy(doc = doc)
|
||||||
|
is MiniMemberTypeAliasDecl -> member.copy(doc = doc)
|
||||||
|
else -> member
|
||||||
|
}
|
||||||
|
} ?: member
|
||||||
|
} else {
|
||||||
|
member
|
||||||
|
}
|
||||||
|
return when (withDoc) {
|
||||||
|
is MiniMemberFunDecl -> renderMemberFunDoc(owner, withDoc)
|
||||||
|
is MiniMemberValDecl -> renderMemberValDoc(owner, withDoc)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, withDoc)
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mini.declarations.firstOrNull { it.name == ident }?.let { decl ->
|
||||||
|
return renderDeclDoc(decl, text, mini, imported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try resolve to: function param at position, function/class/val declaration at position
|
// Try resolve to: function param at position, function/class/val declaration at position
|
||||||
// 1) Use unified declaration detection
|
// 1) Use unified declaration detection
|
||||||
@ -91,6 +144,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
return when (m) {
|
return when (m) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
|
is MiniMemberFunDecl -> renderMemberFunDoc(d.name, m)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
|
is MiniMemberValDecl -> renderMemberValDoc(d.name, m)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(d.name, m)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -197,6 +251,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
return when (m) {
|
return when (m) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
|
is MiniMemberFunDecl -> renderMemberFunDoc(cls.name, m)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
|
is MiniMemberValDecl -> renderMemberValDoc(cls.name, m)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(cls.name, m)
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -307,16 +362,19 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
}
|
}
|
||||||
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos > 0) text[dotPos - 1] else ' '}' classGuess=${className} imports=${importedModules}")
|
if (DEBUG_LOG) log.info("[LYNG_DEBUG] QuickDoc: memberCtx dotPos=${dotPos} chBeforeDot='${if (dotPos > 0) text[dotPos - 1] else ' '}' classGuess=${className} imports=${importedModules}")
|
||||||
if (className != null) {
|
if (className != null) {
|
||||||
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini)?.let { (owner, member) ->
|
val staticOnly = DocLookupUtils.isStaticReceiver(mini, text, dotPos, importedModules, analysis.binding)
|
||||||
|
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini, staticOnly = staticOnly)?.let { (owner, member) ->
|
||||||
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}")
|
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] QuickDoc: literal/call '$ident' resolved to $owner.${member.name}")
|
||||||
return when (member) {
|
return when (member) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, member)
|
||||||
is MiniInitDecl -> null
|
is MiniInitDecl -> null
|
||||||
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
|
is MiniTypeAliasDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}")
|
log.info("[LYNG_DEBUG] QuickDoc: resolve failed for ${className}.${ident}")
|
||||||
@ -354,6 +412,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
// And classes/enums
|
// And classes/enums
|
||||||
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
|
docs.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
|
||||||
docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
|
docs.filterIsInstance<MiniEnumDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
|
||||||
|
docs.filterIsInstance<MiniTypeAliasDecl>().firstOrNull { it.name == ident }?.let { return renderDeclDoc(it, text, mini, imported) }
|
||||||
}
|
}
|
||||||
// Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs
|
// Defensive fallback: if nothing found and it's a well-known stdlib function, render minimal inline docs
|
||||||
if (ident == "println" || ident == "print") {
|
if (ident == "println" || ident == "print") {
|
||||||
@ -367,16 +426,20 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
val lhs = previousWordBefore(text, idRange.startOffset)
|
val lhs = previousWordBefore(text, idRange.startOffset)
|
||||||
if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) {
|
if (lhs != null && hasDotBetween(text, lhs.endOffset, idRange.startOffset)) {
|
||||||
val className = text.substring(lhs.startOffset, lhs.endOffset)
|
val className = text.substring(lhs.startOffset, lhs.endOffset)
|
||||||
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini)?.let { (owner, member) ->
|
val dotPos = findDotLeft(text, idRange.startOffset)
|
||||||
|
val staticOnly = dotPos?.let { DocLookupUtils.isStaticReceiver(mini, text, it, importedModules, analysis.binding) } ?: false
|
||||||
|
DocLookupUtils.resolveMemberWithInheritance(importedModules, className, ident, mini, staticOnly = staticOnly)?.let { (owner, member) ->
|
||||||
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Inheritance resolved $className.$ident to $owner.${member.name}")
|
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Inheritance resolved $className.$ident to $owner.${member.name}")
|
||||||
return when (member) {
|
return when (member) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, member)
|
||||||
is MiniInitDecl -> null
|
is MiniInitDecl -> null
|
||||||
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
|
is MiniTypeAliasDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -390,16 +453,19 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini)
|
else -> DocLookupUtils.guessClassFromCallBefore(text, dotPos, importedModules, mini)
|
||||||
}
|
}
|
||||||
if (guessed != null) {
|
if (guessed != null) {
|
||||||
DocLookupUtils.resolveMemberWithInheritance(importedModules, guessed, ident, mini)?.let { (owner, member) ->
|
val staticOnly = DocLookupUtils.isStaticReceiver(mini, text, dotPos, importedModules, analysis.binding)
|
||||||
|
DocLookupUtils.resolveMemberWithInheritance(importedModules, guessed, ident, mini, staticOnly = staticOnly)?.let { (owner, member) ->
|
||||||
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Heuristic '$guessed.$ident' resolved via inheritance to $owner.${member.name}")
|
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Heuristic '$guessed.$ident' resolved via inheritance to $owner.${member.name}")
|
||||||
return when (member) {
|
return when (member) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, member)
|
||||||
is MiniInitDecl -> null
|
is MiniInitDecl -> null
|
||||||
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
|
is MiniTypeAliasDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -407,16 +473,19 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
run {
|
run {
|
||||||
val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex")
|
val candidates = listOf("String", "Iterable", "Iterator", "List", "Collection", "Array", "Dict", "Regex")
|
||||||
for (c in candidates) {
|
for (c in candidates) {
|
||||||
DocLookupUtils.resolveMemberWithInheritance(importedModules, c, ident, mini)?.let { (owner, member) ->
|
val staticOnly = DocLookupUtils.isStaticReceiver(mini, text, dotPos, importedModules, analysis.binding)
|
||||||
|
DocLookupUtils.resolveMemberWithInheritance(importedModules, c, ident, mini, staticOnly = staticOnly)?.let { (owner, member) ->
|
||||||
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Candidate '$c.$ident' resolved via inheritance to $owner.${member.name}")
|
if (DEBUG_INHERITANCE) log.info("[LYNG_DEBUG] Candidate '$c.$ident' resolved via inheritance to $owner.${member.name}")
|
||||||
return when (member) {
|
return when (member) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, member)
|
||||||
is MiniInitDecl -> null
|
is MiniInitDecl -> null
|
||||||
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
|
is MiniTypeAliasDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -431,6 +500,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
return when (m) {
|
return when (m) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc("String", m)
|
is MiniMemberFunDecl -> renderMemberFunDoc("String", m)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc("String", m)
|
is MiniMemberValDecl -> renderMemberValDoc("String", m)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc("String", m)
|
||||||
is MiniInitDecl -> null
|
is MiniInitDecl -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -441,11 +511,13 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
return when (member) {
|
return when (member) {
|
||||||
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
is MiniMemberFunDecl -> renderMemberFunDoc(owner, member)
|
||||||
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
is MiniMemberValDecl -> renderMemberValDoc(owner, member)
|
||||||
|
is MiniMemberTypeAliasDecl -> renderMemberTypeAliasDoc(owner, member)
|
||||||
is MiniInitDecl -> null
|
is MiniInitDecl -> null
|
||||||
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniFunDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniValDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniClassDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
is MiniEnumDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
|
is MiniTypeAliasDecl -> renderDeclDoc(member, text, mini, importedModules)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -512,6 +584,7 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
|
is MiniFunDecl -> "function ${d.name}${signatureOf(d)}"
|
||||||
is MiniClassDecl -> "class ${d.name}"
|
is MiniClassDecl -> "class ${d.name}"
|
||||||
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }"
|
is MiniEnumDecl -> "enum ${d.name} { ${d.entries.joinToString(", ")} }"
|
||||||
|
is MiniTypeAliasDecl -> "type ${d.name}${typeAliasSuffix(d)}"
|
||||||
is MiniValDecl -> {
|
is MiniValDecl -> {
|
||||||
val t = d.type ?: DocLookupUtils.inferTypeRefForVal(d, text, imported, mini)
|
val t = d.type ?: DocLookupUtils.inferTypeRefForVal(d, text, imported, mini)
|
||||||
val typeStr = if (t == null) ": Object?" else typeOf(t)
|
val typeStr = if (t == null) ": Object?" else typeOf(t)
|
||||||
@ -524,6 +597,73 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
return sb.toString()
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun renderDocFromInfo(info: LyngSymbolInfo): String? {
|
||||||
|
val kind = when (info.target.kind) {
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.Function -> "function"
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.Class -> "class"
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.Enum -> "enum"
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.TypeAlias -> "type"
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.Value -> "val"
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.Variable -> "var"
|
||||||
|
net.sergeych.lyng.binding.SymbolKind.Parameter -> "parameter"
|
||||||
|
}
|
||||||
|
val title = info.signature ?: "$kind ${info.target.name}"
|
||||||
|
if (title.isBlank() && info.doc == null) return null
|
||||||
|
val sb = StringBuilder()
|
||||||
|
sb.append(renderTitle(title))
|
||||||
|
sb.append(renderDocBody(info.doc))
|
||||||
|
return sb.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findDocInDeclarationFiles(file: PsiFile, container: String?, name: String): MiniDoc? {
|
||||||
|
val declFiles = LyngAstManager.getDeclarationFiles(file)
|
||||||
|
if (declFiles.isEmpty()) return null
|
||||||
|
|
||||||
|
fun findInMini(mini: MiniScript): MiniDoc? {
|
||||||
|
if (container == null) {
|
||||||
|
mini.declarations.firstOrNull { it.name == name }?.let { return it.doc }
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val cls = mini.declarations.filterIsInstance<MiniClassDecl>().firstOrNull { it.name == container } ?: return null
|
||||||
|
cls.members.firstOrNull { it.name == name }?.let { return it.doc }
|
||||||
|
cls.ctorFields.firstOrNull { it.name == name }?.let { return null }
|
||||||
|
cls.classFields.firstOrNull { it.name == name }?.let { return null }
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
for (df in declFiles) {
|
||||||
|
val mini = LyngAstManager.getMiniAst(df)
|
||||||
|
?: run {
|
||||||
|
try {
|
||||||
|
val res = runBlocking {
|
||||||
|
LyngLanguageTools.analyze(df.text, df.name)
|
||||||
|
}
|
||||||
|
res.mini
|
||||||
|
} catch (_: Throwable) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (mini != null) {
|
||||||
|
val doc = findInMini(mini)
|
||||||
|
if (doc != null) return doc
|
||||||
|
}
|
||||||
|
// Text fallback: parse preceding doc comment for the symbol
|
||||||
|
val parsed = parseDocFromText(df.text, name)
|
||||||
|
if (parsed != null) return parsed
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseDocFromText(text: String, name: String): MiniDoc? {
|
||||||
|
if (text.isBlank()) return null
|
||||||
|
val pattern = Regex("/\\*\\*([\\s\\S]*?)\\*/\\s*(?:public|private|protected|static|abstract|extern|open|closed|override\\s+)*\\s*(?:fun|val|var|class|interface|enum|type)\\s+$name\\b")
|
||||||
|
val m = pattern.find(text) ?: return null
|
||||||
|
val raw = m.groupValues.getOrNull(1)?.trim() ?: return null
|
||||||
|
if (raw.isBlank()) return null
|
||||||
|
val src = net.sergeych.lyng.Source("<doc>", raw)
|
||||||
|
return MiniDoc.parse(MiniRange(src.startPos, src.startPos), raw.lines())
|
||||||
|
}
|
||||||
|
|
||||||
private fun renderParamDoc(fn: MiniFunDecl, p: MiniParam): String {
|
private fun renderParamDoc(fn: MiniFunDecl, p: MiniParam): String {
|
||||||
val title = "parameter ${p.name}${typeOf(p.type)} in ${fn.name}${signatureOf(fn)}"
|
val title = "parameter ${p.name}${typeOf(p.type)} in ${fn.name}${signatureOf(fn)}"
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
@ -565,6 +705,25 @@ class LyngDocumentationProvider : AbstractDocumentationProvider() {
|
|||||||
return sb.toString()
|
return sb.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun renderMemberTypeAliasDoc(className: String, m: MiniMemberTypeAliasDecl): String {
|
||||||
|
val tp = if (m.typeParams.isEmpty()) "" else "<" + m.typeParams.joinToString(", ") + ">"
|
||||||
|
val body = typeOf(m.target)
|
||||||
|
val rhs = if (body.isBlank()) "" else " = ${body.removePrefix(": ")}"
|
||||||
|
val staticStr = if (m.isStatic) "static " else ""
|
||||||
|
val title = "${staticStr}type $className.${m.name}$tp$rhs"
|
||||||
|
val sb = StringBuilder()
|
||||||
|
sb.append(renderTitle(title))
|
||||||
|
sb.append(renderDocBody(m.doc))
|
||||||
|
return sb.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun typeAliasSuffix(d: MiniTypeAliasDecl): String {
|
||||||
|
val tp = if (d.typeParams.isEmpty()) "" else "<" + d.typeParams.joinToString(", ") + ">"
|
||||||
|
val body = typeOf(d.target)
|
||||||
|
val rhs = if (body.isBlank()) "" else " = ${body.removePrefix(": ")}"
|
||||||
|
return "$tp$rhs"
|
||||||
|
}
|
||||||
|
|
||||||
private fun typeOf(t: MiniTypeRef?): String {
|
private fun typeOf(t: MiniTypeRef?): String {
|
||||||
val s = DocLookupUtils.typeOf(t)
|
val s = DocLookupUtils.typeOf(t)
|
||||||
return if (s.isEmpty()) (if (t == null) ": Object?" else "") else ": $s"
|
return if (s.isEmpty()) (if (t == null) ": Object?" else "") else ": $s"
|
||||||
|
|||||||
@ -36,7 +36,7 @@ class LyngLexer : LexerBase() {
|
|||||||
"abstract", "closed", "override", "static", "extern", "open", "private", "protected",
|
"abstract", "closed", "override", "static", "extern", "open", "private", "protected",
|
||||||
"if", "else", "for", "while", "return", "true", "false", "null",
|
"if", "else", "for", "while", "return", "true", "false", "null",
|
||||||
"when", "in", "is", "break", "continue", "try", "catch", "finally",
|
"when", "in", "is", "break", "continue", "try", "catch", "finally",
|
||||||
"get", "set", "object", "enum", "init", "by", "property", "constructor"
|
"get", "set", "object", "enum", "init", "by", "step", "property", "constructor"
|
||||||
)
|
)
|
||||||
|
|
||||||
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
|
override fun start(buffer: CharSequence, startOffset: Int, endOffset: Int, initialState: Int) {
|
||||||
|
|||||||
@ -20,12 +20,18 @@ package net.sergeych.lyng.idea.navigation
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
|
import com.intellij.psi.search.FileTypeIndex
|
||||||
import com.intellij.psi.search.FilenameIndex
|
import com.intellij.psi.search.FilenameIndex
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import net.sergeych.lyng.highlight.offsetOf
|
import net.sergeych.lyng.highlight.offsetOf
|
||||||
|
import net.sergeych.lyng.idea.LyngFileType
|
||||||
import net.sergeych.lyng.idea.util.LyngAstManager
|
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||||
import net.sergeych.lyng.idea.util.TextCtx
|
import net.sergeych.lyng.idea.util.TextCtx
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.miniast.*
|
||||||
|
import net.sergeych.lyng.tools.IdeLenientImportProvider
|
||||||
|
import net.sergeych.lyng.tools.LyngAnalysisRequest
|
||||||
|
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||||
|
|
||||||
class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength)) {
|
class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiElement>(element, TextRange(0, element.textLength)) {
|
||||||
|
|
||||||
@ -36,9 +42,10 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
val name = element.text ?: ""
|
val name = element.text ?: ""
|
||||||
val results = mutableListOf<ResolveResult>()
|
val results = mutableListOf<ResolveResult>()
|
||||||
|
|
||||||
val mini = LyngAstManager.getMiniAst(file) ?: return emptyArray()
|
val analysis = LyngAstManager.getAnalysis(file) ?: return emptyArray()
|
||||||
val binding = LyngAstManager.getBinding(file)
|
val mini = analysis.mini ?: return emptyArray()
|
||||||
val imported = DocLookupUtils.canonicalImportedModules(mini, text).toSet()
|
val binding = analysis.binding
|
||||||
|
val imported = analysis.importedModules.toSet()
|
||||||
val currentPackage = getPackageName(file)
|
val currentPackage = getPackageName(file)
|
||||||
val allowedPackages = if (currentPackage != null) imported + currentPackage else imported
|
val allowedPackages = if (currentPackage != null) imported + currentPackage else imported
|
||||||
|
|
||||||
@ -47,16 +54,17 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
if (dotPos != null) {
|
if (dotPos != null) {
|
||||||
val receiverClass = DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, imported.toList(), binding)
|
val receiverClass = DocLookupUtils.guessReceiverClassViaMini(mini, text, dotPos, imported.toList(), binding)
|
||||||
?: DocLookupUtils.guessReceiverClass(text, dotPos, imported.toList(), mini)
|
?: DocLookupUtils.guessReceiverClass(text, dotPos, imported.toList(), mini)
|
||||||
|
val staticOnly = DocLookupUtils.isStaticReceiver(mini, text, dotPos, imported.toList(), binding)
|
||||||
|
|
||||||
if (receiverClass != null) {
|
if (receiverClass != null) {
|
||||||
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported.toList(), receiverClass, name, mini)
|
val resolved = DocLookupUtils.resolveMemberWithInheritance(imported.toList(), receiverClass, name, mini, staticOnly = staticOnly)
|
||||||
if (resolved != null) {
|
if (resolved != null) {
|
||||||
val owner = resolved.first
|
val owner = resolved.first
|
||||||
val member = resolved.second
|
val member = resolved.second
|
||||||
|
|
||||||
// We need to find the actual PSI element for this member
|
// We need to find the actual PSI element for this member
|
||||||
val targetFile = findFileForClass(file.project, owner) ?: file
|
val targetFile = findFileForClass(file.project, owner) ?: file
|
||||||
val targetMini = LyngAstManager.getMiniAst(targetFile)
|
val targetMini = loadMini(targetFile)
|
||||||
if (targetMini != null) {
|
if (targetMini != null) {
|
||||||
val targetSrc = targetMini.range.start.source
|
val targetSrc = targetMini.range.start.source
|
||||||
val off = targetSrc.offsetOf(member.nameStart)
|
val off = targetSrc.offsetOf(member.nameStart)
|
||||||
@ -64,11 +72,13 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
val kind = when(member) {
|
val kind = when(member) {
|
||||||
is MiniMemberFunDecl -> "Function"
|
is MiniMemberFunDecl -> "Function"
|
||||||
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
|
is MiniMemberValDecl -> if (member.mutable) "Variable" else "Value"
|
||||||
|
is MiniMemberTypeAliasDecl -> "TypeAlias"
|
||||||
is MiniInitDecl -> "Initializer"
|
is MiniInitDecl -> "Initializer"
|
||||||
is MiniFunDecl -> "Function"
|
is MiniFunDecl -> "Function"
|
||||||
is MiniValDecl -> if (member.mutable) "Variable" else "Value"
|
is MiniValDecl -> if (member.mutable) "Variable" else "Value"
|
||||||
is MiniClassDecl -> "Class"
|
is MiniClassDecl -> "Class"
|
||||||
is MiniEnumDecl -> "Enum"
|
is MiniEnumDecl -> "Enum"
|
||||||
|
is MiniTypeAliasDecl -> "TypeAlias"
|
||||||
}
|
}
|
||||||
results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
|
results.add(PsiElementResolveResult(LyngDeclarationElement(it, member.name, kind)))
|
||||||
}
|
}
|
||||||
@ -119,24 +129,37 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun findFileForClass(project: Project, className: String): PsiFile? {
|
private fun findFileForClass(project: Project, className: String): PsiFile? {
|
||||||
val psiManager = PsiManager.getInstance(project)
|
|
||||||
|
|
||||||
// 1. Try file with matching name first (optimization)
|
// 1. Try file with matching name first (optimization)
|
||||||
val matchingFiles = FilenameIndex.getFilesByName(project, "$className.lyng", GlobalSearchScope.projectScope(project))
|
val scope = GlobalSearchScope.projectScope(project)
|
||||||
|
val psiManager = PsiManager.getInstance(project)
|
||||||
|
val matchingFiles = FileTypeIndex.getFiles(LyngFileType, scope)
|
||||||
|
.asSequence()
|
||||||
|
.filter { it.name == "$className.lyng" }
|
||||||
|
.mapNotNull { psiManager.findFile(it) }
|
||||||
|
.toList()
|
||||||
|
val matchingDeclFiles = FileTypeIndex.getFiles(LyngFileType, scope)
|
||||||
|
.asSequence()
|
||||||
|
.filter { it.name == "$className.lyng.d" }
|
||||||
|
.mapNotNull { psiManager.findFile(it) }
|
||||||
|
.toList()
|
||||||
for (file in matchingFiles) {
|
for (file in matchingFiles) {
|
||||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
val mini = loadMini(file) ?: continue
|
||||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
if (mini.declarations.any { isLocalDecl(mini, it) && ((it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className)) }) {
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (file in matchingDeclFiles) {
|
||||||
|
val mini = loadMini(file) ?: continue
|
||||||
|
if (mini.declarations.any { isLocalDecl(mini, it) && ((it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className)) }) {
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Fallback to full project scan
|
// 2. Fallback to full project scan
|
||||||
val allFiles = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
for (file in collectLyngFiles(project)) {
|
||||||
for (vFile in allFiles) {
|
if (matchingFiles.contains(file) || matchingDeclFiles.contains(file)) continue // already checked
|
||||||
val file = psiManager.findFile(vFile) ?: continue
|
val mini = loadMini(file) ?: continue
|
||||||
if (matchingFiles.contains(file)) continue // already checked
|
if (mini.declarations.any { isLocalDecl(mini, it) && ((it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className)) }) {
|
||||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
|
||||||
if (mini.declarations.any { (it is MiniClassDecl && it.name == className) || (it is MiniEnumDecl && it.name == className) }) {
|
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,7 +167,7 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getPackageName(file: PsiFile): String? {
|
private fun getPackageName(file: PsiFile): String? {
|
||||||
val mini = LyngAstManager.getMiniAst(file) ?: return null
|
val mini = loadMini(file) ?: return null
|
||||||
return try {
|
return try {
|
||||||
val pkg = mini.range.start.source.extractPackageName()
|
val pkg = mini.range.start.source.extractPackageName()
|
||||||
if (pkg.startsWith("lyng.")) pkg else "lyng.$pkg"
|
if (pkg.startsWith("lyng.")) pkg else "lyng.$pkg"
|
||||||
@ -168,19 +191,19 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
|
|
||||||
private fun resolveGlobally(project: Project, name: String, membersOnly: Boolean = false, allowedPackages: Set<String>? = null): List<ResolveResult> {
|
private fun resolveGlobally(project: Project, name: String, membersOnly: Boolean = false, allowedPackages: Set<String>? = null): List<ResolveResult> {
|
||||||
val results = mutableListOf<ResolveResult>()
|
val results = mutableListOf<ResolveResult>()
|
||||||
val files = FilenameIndex.getAllFilesByExt(project, "lyng", GlobalSearchScope.projectScope(project))
|
|
||||||
val psiManager = PsiManager.getInstance(project)
|
val psiManager = PsiManager.getInstance(project)
|
||||||
|
|
||||||
for (vFile in files) {
|
for (file in collectLyngFiles(project)) {
|
||||||
val file = psiManager.findFile(vFile) ?: continue
|
|
||||||
|
|
||||||
// Filter by package if requested
|
// Filter by package if requested
|
||||||
if (allowedPackages != null) {
|
if (allowedPackages != null) {
|
||||||
val pkg = getPackageName(file)
|
val pkg = getPackageName(file)
|
||||||
if (pkg == null || pkg !in allowedPackages) continue
|
if (pkg == null) {
|
||||||
|
if (!file.name.endsWith(".lyng.d")) continue
|
||||||
|
} else if (pkg !in allowedPackages) continue
|
||||||
}
|
}
|
||||||
|
|
||||||
val mini = LyngAstManager.getMiniAst(file) ?: continue
|
val mini = loadMini(file) ?: continue
|
||||||
val src = mini.range.start.source
|
val src = mini.range.start.source
|
||||||
|
|
||||||
fun addIfMatch(dName: String, nameStart: net.sergeych.lyng.Pos, dKind: String) {
|
fun addIfMatch(dName: String, nameStart: net.sergeych.lyng.Pos, dKind: String) {
|
||||||
@ -193,12 +216,14 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (d in mini.declarations) {
|
for (d in mini.declarations) {
|
||||||
|
if (!isLocalDecl(mini, d)) continue
|
||||||
if (!membersOnly) {
|
if (!membersOnly) {
|
||||||
val dKind = when(d) {
|
val dKind = when(d) {
|
||||||
is net.sergeych.lyng.miniast.MiniFunDecl -> "Function"
|
is net.sergeych.lyng.miniast.MiniFunDecl -> "Function"
|
||||||
is net.sergeych.lyng.miniast.MiniClassDecl -> "Class"
|
is net.sergeych.lyng.miniast.MiniClassDecl -> "Class"
|
||||||
is net.sergeych.lyng.miniast.MiniEnumDecl -> "Enum"
|
is net.sergeych.lyng.miniast.MiniEnumDecl -> "Enum"
|
||||||
is net.sergeych.lyng.miniast.MiniValDecl -> if (d.mutable) "Variable" else "Value"
|
is net.sergeych.lyng.miniast.MiniValDecl -> if (d.mutable) "Variable" else "Value"
|
||||||
|
is net.sergeych.lyng.miniast.MiniTypeAliasDecl -> "TypeAlias"
|
||||||
}
|
}
|
||||||
addIfMatch(d.name, d.nameStart, dKind)
|
addIfMatch(d.name, d.nameStart, dKind)
|
||||||
}
|
}
|
||||||
@ -211,9 +236,11 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (m in members) {
|
for (m in members) {
|
||||||
|
if (m.range.start.source != src) continue
|
||||||
val mKind = when(m) {
|
val mKind = when(m) {
|
||||||
is net.sergeych.lyng.miniast.MiniMemberFunDecl -> "Function"
|
is net.sergeych.lyng.miniast.MiniMemberFunDecl -> "Function"
|
||||||
is net.sergeych.lyng.miniast.MiniMemberValDecl -> if (m.mutable) "Variable" else "Value"
|
is net.sergeych.lyng.miniast.MiniMemberValDecl -> if (m.mutable) "Variable" else "Value"
|
||||||
|
is net.sergeych.lyng.miniast.MiniMemberTypeAliasDecl -> "TypeAlias"
|
||||||
is net.sergeych.lyng.miniast.MiniInitDecl -> "Initializer"
|
is net.sergeych.lyng.miniast.MiniInitDecl -> "Initializer"
|
||||||
}
|
}
|
||||||
addIfMatch(m.name, m.nameStart, mKind)
|
addIfMatch(m.name, m.nameStart, mKind)
|
||||||
@ -223,5 +250,42 @@ class LyngPsiReference(element: PsiElement) : PsiPolyVariantReferenceBase<PsiEle
|
|||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun collectLyngFiles(project: Project): List<PsiFile> {
|
||||||
|
val scope = GlobalSearchScope.projectScope(project)
|
||||||
|
val psiManager = PsiManager.getInstance(project)
|
||||||
|
val out = LinkedHashSet<PsiFile>()
|
||||||
|
|
||||||
|
val lyngFiles = FilenameIndex.getAllFilesByExt(project, "lyng", scope)
|
||||||
|
for (vFile in lyngFiles) {
|
||||||
|
psiManager.findFile(vFile)?.let { out.add(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include declaration files (*.lyng.d) which are indexed as extension "d".
|
||||||
|
val dFiles = FilenameIndex.getAllFilesByExt(project, "d", scope)
|
||||||
|
for (vFile in dFiles) {
|
||||||
|
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||||
|
psiManager.findFile(vFile)?.let { out.add(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadMini(file: PsiFile): MiniScript? {
|
||||||
|
LyngAstManager.getMiniAst(file)?.let { return it }
|
||||||
|
return try {
|
||||||
|
val provider = IdeLenientImportProvider.create()
|
||||||
|
runBlocking {
|
||||||
|
LyngLanguageTools.analyze(
|
||||||
|
LyngAnalysisRequest(text = file.text, fileName = file.name, importProvider = provider)
|
||||||
|
)
|
||||||
|
}.mini
|
||||||
|
} catch (_: Throwable) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isLocalDecl(mini: MiniScript, decl: MiniDecl): Boolean =
|
||||||
|
decl.range.start.source == mini.range.start.source
|
||||||
|
|
||||||
override fun getVariants(): Array<Any> = emptyArray()
|
override fun getVariants(): Array<Any> = emptyArray()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,56 +21,31 @@ import com.intellij.openapi.application.runReadAction
|
|||||||
import com.intellij.openapi.util.Key
|
import com.intellij.openapi.util.Key
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.psi.PsiManager
|
import com.intellij.psi.PsiManager
|
||||||
|
import com.intellij.psi.search.FileTypeIndex
|
||||||
|
import com.intellij.psi.search.FilenameIndex
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import net.sergeych.lyng.Compiler
|
|
||||||
import net.sergeych.lyng.Source
|
|
||||||
import net.sergeych.lyng.binding.Binder
|
|
||||||
import net.sergeych.lyng.binding.BindingSnapshot
|
import net.sergeych.lyng.binding.BindingSnapshot
|
||||||
import net.sergeych.lyng.miniast.MiniAstBuilder
|
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||||
|
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||||
|
import net.sergeych.lyng.miniast.MiniEnumDecl
|
||||||
|
import net.sergeych.lyng.miniast.MiniRange
|
||||||
import net.sergeych.lyng.miniast.MiniScript
|
import net.sergeych.lyng.miniast.MiniScript
|
||||||
|
import net.sergeych.lyng.tools.IdeLenientImportProvider
|
||||||
|
import net.sergeych.lyng.tools.LyngAnalysisRequest
|
||||||
|
import net.sergeych.lyng.tools.LyngAnalysisResult
|
||||||
|
import net.sergeych.lyng.tools.LyngDiagnostic
|
||||||
|
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||||
|
import net.sergeych.lyng.idea.LyngFileType
|
||||||
|
|
||||||
object LyngAstManager {
|
object LyngAstManager {
|
||||||
private val MINI_KEY = Key.create<MiniScript>("lyng.mini.cache")
|
private val MINI_KEY = Key.create<MiniScript>("lyng.mini.cache")
|
||||||
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
|
private val BINDING_KEY = Key.create<BindingSnapshot>("lyng.binding.cache")
|
||||||
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
|
private val STAMP_KEY = Key.create<Long>("lyng.mini.cache.stamp")
|
||||||
|
private val ANALYSIS_KEY = Key.create<LyngAnalysisResult>("lyng.analysis.cache")
|
||||||
|
|
||||||
fun getMiniAst(file: PsiFile): MiniScript? = runReadAction {
|
fun getMiniAst(file: PsiFile): MiniScript? = runReadAction {
|
||||||
val vFile = file.virtualFile ?: return@runReadAction null
|
getAnalysis(file)?.mini
|
||||||
val combinedStamp = getCombinedStamp(file)
|
|
||||||
|
|
||||||
val prevStamp = file.getUserData(STAMP_KEY)
|
|
||||||
val cached = file.getUserData(MINI_KEY)
|
|
||||||
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached
|
|
||||||
|
|
||||||
val text = file.viewProvider.contents.toString()
|
|
||||||
val sink = MiniAstBuilder()
|
|
||||||
val built = try {
|
|
||||||
val provider = IdeLenientImportProvider.create()
|
|
||||||
val src = Source(file.name, text)
|
|
||||||
runBlocking { Compiler.compileWithMini(src, provider, sink) }
|
|
||||||
val script = sink.build()
|
|
||||||
if (script != null && !file.name.endsWith(".lyng.d")) {
|
|
||||||
val dFiles = collectDeclarationFiles(file)
|
|
||||||
for (df in dFiles) {
|
|
||||||
val scriptD = getMiniAst(df)
|
|
||||||
if (scriptD != null) {
|
|
||||||
script.declarations.addAll(scriptD.declarations)
|
|
||||||
script.imports.addAll(scriptD.imports)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
script
|
|
||||||
} catch (_: Throwable) {
|
|
||||||
sink.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (built != null) {
|
|
||||||
file.putUserData(MINI_KEY, built)
|
|
||||||
file.putUserData(STAMP_KEY, combinedStamp)
|
|
||||||
// Invalidate binding too
|
|
||||||
file.putUserData(BINDING_KEY, null)
|
|
||||||
}
|
|
||||||
built
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getCombinedStamp(file: PsiFile): Long = runReadAction {
|
fun getCombinedStamp(file: PsiFile): Long = runReadAction {
|
||||||
@ -85,49 +60,172 @@ object LyngAstManager {
|
|||||||
|
|
||||||
private fun collectDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
|
private fun collectDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
|
||||||
val psiManager = PsiManager.getInstance(file.project)
|
val psiManager = PsiManager.getInstance(file.project)
|
||||||
var current = file.virtualFile?.parent
|
|
||||||
val seen = mutableSetOf<String>()
|
val seen = mutableSetOf<String>()
|
||||||
val result = mutableListOf<PsiFile>()
|
val result = mutableListOf<PsiFile>()
|
||||||
|
|
||||||
while (current != null) {
|
var currentDir = file.containingDirectory
|
||||||
for (child in current.children) {
|
while (currentDir != null) {
|
||||||
if (child.name.endsWith(".lyng.d") && child != file.virtualFile && seen.add(child.path)) {
|
for (child in currentDir.files) {
|
||||||
val psiD = psiManager.findFile(child) ?: continue
|
if (child.name.endsWith(".lyng.d") && child != file && seen.add(child.virtualFile.path)) {
|
||||||
result.add(psiD)
|
result.add(child)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current = current.parent
|
currentDir = currentDir.parentDirectory
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.isNotEmpty()) return@runReadAction result
|
||||||
|
|
||||||
|
// Fallback for virtual/light files without a stable parent chain (e.g., tests)
|
||||||
|
val basePath = file.virtualFile?.path ?: return@runReadAction result
|
||||||
|
val scope = GlobalSearchScope.projectScope(file.project)
|
||||||
|
val dFiles = FilenameIndex.getAllFilesByExt(file.project, "d", scope)
|
||||||
|
for (vFile in dFiles) {
|
||||||
|
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||||
|
if (vFile.path == basePath) continue
|
||||||
|
val parentPath = vFile.parent?.path ?: continue
|
||||||
|
if (basePath == parentPath || basePath.startsWith(parentPath.trimEnd('/') + "/")) {
|
||||||
|
if (seen.add(vFile.path)) {
|
||||||
|
psiManager.findFile(vFile)?.let { result.add(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.isNotEmpty()) return@runReadAction result
|
||||||
|
|
||||||
|
// Fallback: scan all Lyng files in project index and filter by .lyng.d
|
||||||
|
val lyngFiles = FileTypeIndex.getFiles(LyngFileType, scope)
|
||||||
|
for (vFile in lyngFiles) {
|
||||||
|
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||||
|
if (vFile.path == basePath) continue
|
||||||
|
if (seen.add(vFile.path)) {
|
||||||
|
psiManager.findFile(vFile)?.let { result.add(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.isNotEmpty()) return@runReadAction result
|
||||||
|
|
||||||
|
// Final fallback: include all .lyng.d files in project scope
|
||||||
|
for (vFile in dFiles) {
|
||||||
|
if (!vFile.name.endsWith(".lyng.d")) continue
|
||||||
|
if (vFile.path == basePath) continue
|
||||||
|
if (seen.add(vFile.path)) {
|
||||||
|
psiManager.findFile(vFile)?.let { result.add(it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getBinding(file: PsiFile): BindingSnapshot? = runReadAction {
|
fun getDeclarationFiles(file: PsiFile): List<PsiFile> = runReadAction {
|
||||||
val vFile = file.virtualFile ?: return@runReadAction null
|
collectDeclarationFiles(file)
|
||||||
var combinedStamp = file.viewProvider.modificationStamp
|
|
||||||
|
|
||||||
val dFiles = if (!file.name.endsWith(".lyng.d")) collectDeclarationFiles(file) else emptyList()
|
|
||||||
for (df in dFiles) {
|
|
||||||
combinedStamp += df.viewProvider.modificationStamp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val prevStamp = file.getUserData(STAMP_KEY)
|
fun getBinding(file: PsiFile): BindingSnapshot? = runReadAction {
|
||||||
val cached = file.getUserData(BINDING_KEY)
|
getAnalysis(file)?.binding
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getAnalysis(file: PsiFile): LyngAnalysisResult? = runReadAction {
|
||||||
|
val vFile = file.virtualFile ?: return@runReadAction null
|
||||||
|
val combinedStamp = getCombinedStamp(file)
|
||||||
|
val prevStamp = file.getUserData(STAMP_KEY)
|
||||||
|
val cached = file.getUserData(ANALYSIS_KEY)
|
||||||
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached
|
if (cached != null && prevStamp != null && prevStamp == combinedStamp) return@runReadAction cached
|
||||||
|
|
||||||
val mini = getMiniAst(file) ?: return@runReadAction null
|
|
||||||
val text = file.viewProvider.contents.toString()
|
val text = file.viewProvider.contents.toString()
|
||||||
val binding = try {
|
val built = try {
|
||||||
Binder.bind(text, mini)
|
val provider = IdeLenientImportProvider.create()
|
||||||
|
runBlocking {
|
||||||
|
LyngLanguageTools.analyze(
|
||||||
|
LyngAnalysisRequest(text = text, fileName = file.name, importProvider = provider)
|
||||||
|
)
|
||||||
|
}
|
||||||
} catch (_: Throwable) {
|
} catch (_: Throwable) {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (binding != null) {
|
if (built != null) {
|
||||||
file.putUserData(BINDING_KEY, binding)
|
val isDecl = file.name.endsWith(".lyng.d")
|
||||||
// stamp is already set by getMiniAst or we set it here if getMiniAst was cached
|
val merged = if (!isDecl && built.mini == null) {
|
||||||
|
MiniScript(MiniRange(built.source.startPos, built.source.startPos))
|
||||||
|
} else {
|
||||||
|
built.mini
|
||||||
|
}
|
||||||
|
if (merged != null && !isDecl) {
|
||||||
|
val dFiles = collectDeclarationFiles(file)
|
||||||
|
for (df in dFiles) {
|
||||||
|
val dMini = getAnalysis(df)?.mini ?: run {
|
||||||
|
val dText = df.viewProvider.contents.toString()
|
||||||
|
try {
|
||||||
|
val provider = IdeLenientImportProvider.create()
|
||||||
|
runBlocking {
|
||||||
|
LyngLanguageTools.analyze(
|
||||||
|
LyngAnalysisRequest(text = dText, fileName = df.name, importProvider = provider)
|
||||||
|
)
|
||||||
|
}.mini
|
||||||
|
} catch (_: Throwable) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
} ?: continue
|
||||||
|
merged.declarations.addAll(dMini.declarations)
|
||||||
|
merged.imports.addAll(dMini.imports)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val finalAnalysis = if (merged != null) {
|
||||||
|
val mergedImports = DocLookupUtils.canonicalImportedModules(merged, text)
|
||||||
|
built.copy(
|
||||||
|
mini = merged,
|
||||||
|
importedModules = mergedImports,
|
||||||
|
diagnostics = filterDiagnostics(built.diagnostics, merged, text, mergedImports)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
built
|
||||||
|
}
|
||||||
|
file.putUserData(ANALYSIS_KEY, finalAnalysis)
|
||||||
|
file.putUserData(MINI_KEY, finalAnalysis.mini)
|
||||||
|
file.putUserData(BINDING_KEY, finalAnalysis.binding)
|
||||||
file.putUserData(STAMP_KEY, combinedStamp)
|
file.putUserData(STAMP_KEY, combinedStamp)
|
||||||
|
return@runReadAction finalAnalysis
|
||||||
|
}
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun filterDiagnostics(
|
||||||
|
diagnostics: List<LyngDiagnostic>,
|
||||||
|
merged: MiniScript,
|
||||||
|
text: String,
|
||||||
|
importedModules: List<String>
|
||||||
|
): List<LyngDiagnostic> {
|
||||||
|
if (diagnostics.isEmpty()) return diagnostics
|
||||||
|
val declaredTopLevel = merged.declarations.map { it.name }.toSet()
|
||||||
|
|
||||||
|
val declaredMembers = linkedSetOf<String>()
|
||||||
|
val aggregatedClasses = DocLookupUtils.aggregateClasses(importedModules, merged)
|
||||||
|
for (cls in aggregatedClasses.values) {
|
||||||
|
cls.members.forEach { declaredMembers.add(it.name) }
|
||||||
|
cls.ctorFields.forEach { declaredMembers.add(it.name) }
|
||||||
|
cls.classFields.forEach { declaredMembers.add(it.name) }
|
||||||
|
}
|
||||||
|
merged.declarations.filterIsInstance<MiniEnumDecl>().forEach { en ->
|
||||||
|
DocLookupUtils.enumToSyntheticClass(en).members.forEach { declaredMembers.add(it.name) }
|
||||||
|
}
|
||||||
|
|
||||||
|
val builtinTopLevel = linkedSetOf<String>()
|
||||||
|
for (mod in importedModules) {
|
||||||
|
BuiltinDocRegistry.docsForModule(mod).forEach { builtinTopLevel.add(it.name) }
|
||||||
|
}
|
||||||
|
|
||||||
|
return diagnostics.filterNot { diag ->
|
||||||
|
val msg = diag.message
|
||||||
|
if (msg.startsWith("unresolved name: ")) {
|
||||||
|
val name = msg.removePrefix("unresolved name: ").trim()
|
||||||
|
name in declaredTopLevel || name in builtinTopLevel
|
||||||
|
} else if (msg.startsWith("unresolved member: ")) {
|
||||||
|
val name = msg.removePrefix("unresolved member: ").trim()
|
||||||
|
val range = diag.range
|
||||||
|
val dotLeft = if (range != null) DocLookupUtils.findDotLeft(text, range.start) else null
|
||||||
|
dotLeft != null && name in declaredMembers
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
binding
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,127 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.idea.definitions
|
||||||
|
|
||||||
|
import com.intellij.testFramework.fixtures.BasePlatformTestCase
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import net.sergeych.lyng.idea.docs.LyngDocumentationProvider
|
||||||
|
import net.sergeych.lyng.idea.navigation.LyngPsiReference
|
||||||
|
import net.sergeych.lyng.idea.settings.LyngFormatterSettings
|
||||||
|
import net.sergeych.lyng.idea.util.LyngAstManager
|
||||||
|
import net.sergeych.lyng.miniast.CompletionEngineLight
|
||||||
|
|
||||||
|
class LyngDefinitionFilesTest : BasePlatformTestCase() {
|
||||||
|
|
||||||
|
override fun getTestDataPath(): String = ""
|
||||||
|
|
||||||
|
private fun enableCompletion() {
|
||||||
|
LyngFormatterSettings.getInstance(project).enableLyngCompletionExperimental = true
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun addDefinitionsFile() {
|
||||||
|
val defs = """
|
||||||
|
/** Utilities exposed via .lyng.d */
|
||||||
|
class Declared(val name: String) {
|
||||||
|
/** Size property */
|
||||||
|
val size: Int = 0
|
||||||
|
|
||||||
|
/** Returns greeting. */
|
||||||
|
fun greet(who: String): String = "hi " + who
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Top-level function. */
|
||||||
|
fun topFun(x: Int): Int = x + 1
|
||||||
|
""".trimIndent()
|
||||||
|
myFixture.addFileToProject("api.lyng.d", defs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test_CompletionsIncludeDefinitions() {
|
||||||
|
addDefinitionsFile()
|
||||||
|
enableCompletion()
|
||||||
|
run {
|
||||||
|
val code = """
|
||||||
|
val v = top<caret>
|
||||||
|
""".trimIndent()
|
||||||
|
myFixture.configureByText("main.lyng", code)
|
||||||
|
val text = myFixture.editor.document.text
|
||||||
|
val caret = myFixture.caretOffset
|
||||||
|
val analysis = LyngAstManager.getAnalysis(myFixture.file)
|
||||||
|
val engine = runBlocking { CompletionEngineLight.completeSuspend(text, caret, analysis?.mini, analysis?.binding).map { it.name } }
|
||||||
|
assertTrue("Expected topFun from .lyng.d; got=$engine", engine.contains("topFun"))
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val code = """
|
||||||
|
<caret>
|
||||||
|
""".trimIndent()
|
||||||
|
myFixture.configureByText("other.lyng", code)
|
||||||
|
val text = myFixture.editor.document.text
|
||||||
|
val caret = myFixture.caretOffset
|
||||||
|
val analysis = LyngAstManager.getAnalysis(myFixture.file)
|
||||||
|
val engine = runBlocking { CompletionEngineLight.completeSuspend(text, caret, analysis?.mini, analysis?.binding).map { it.name } }
|
||||||
|
assertTrue("Expected Declared from .lyng.d; got=$engine", engine.contains("Declared"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test_GotoDefinitionResolvesToDefinitionFile() {
|
||||||
|
addDefinitionsFile()
|
||||||
|
val code = """
|
||||||
|
val x = topFun(1)
|
||||||
|
val y = Declared("x")
|
||||||
|
y.gre<caret>et("me")
|
||||||
|
""".trimIndent()
|
||||||
|
myFixture.configureByText("main.lyng", code)
|
||||||
|
val offset = myFixture.caretOffset
|
||||||
|
val element = myFixture.file.findElementAt(offset) ?: myFixture.file.findElementAt((offset - 1).coerceAtLeast(0))
|
||||||
|
assertNotNull("Expected element at caret for resolve", element)
|
||||||
|
val ref = LyngPsiReference(element!!)
|
||||||
|
val resolved = ref.resolve()
|
||||||
|
assertNotNull("Expected reference to resolve", resolved)
|
||||||
|
assertTrue("Expected .lyng.d target; got=${resolved!!.containingFile.name}", resolved.containingFile.name.endsWith(".lyng.d"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test_QuickDocUsesDefinitionDocs() {
|
||||||
|
addDefinitionsFile()
|
||||||
|
val code = """
|
||||||
|
val y = Declared("x")
|
||||||
|
y.gre<caret>et("me")
|
||||||
|
""".trimIndent()
|
||||||
|
myFixture.configureByText("main.lyng", code)
|
||||||
|
val provider = LyngDocumentationProvider()
|
||||||
|
val offset = myFixture.caretOffset
|
||||||
|
val element = myFixture.file.findElementAt(offset) ?: myFixture.file.findElementAt((offset - 1).coerceAtLeast(0))
|
||||||
|
assertNotNull("Expected element at caret for doc", element)
|
||||||
|
val doc = provider.generateDoc(element, element)
|
||||||
|
assertNotNull("Expected Quick Doc", doc)
|
||||||
|
assertTrue("Doc should include summary; got=$doc", doc!!.contains("Returns greeting"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test_DiagnosticsIgnoreDefinitionSymbols() {
|
||||||
|
addDefinitionsFile()
|
||||||
|
val code = """
|
||||||
|
val x = topFun(1)
|
||||||
|
val y = Declared("x")
|
||||||
|
y.greet("me")
|
||||||
|
""".trimIndent()
|
||||||
|
myFixture.configureByText("main.lyng", code)
|
||||||
|
val analysis = LyngAstManager.getAnalysis(myFixture.file)
|
||||||
|
val messages = analysis?.diagnostics?.map { it.message } ?: emptyList()
|
||||||
|
assertTrue("Should not report unresolved name for topFun", messages.none { it.contains("unresolved name: topFun") })
|
||||||
|
assertTrue("Should not report unresolved name for Declared", messages.none { it.contains("unresolved name: Declared") })
|
||||||
|
assertTrue("Should not report unresolved member for greet", messages.none { it.contains("unresolved member: greet") })
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -27,6 +27,7 @@ import com.github.ajalt.clikt.parameters.arguments.optional
|
|||||||
import com.github.ajalt.clikt.parameters.options.flag
|
import com.github.ajalt.clikt.parameters.options.flag
|
||||||
import com.github.ajalt.clikt.parameters.options.option
|
import com.github.ajalt.clikt.parameters.options.option
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import net.sergeych.lyng.Compiler
|
||||||
import net.sergeych.lyng.LyngVersion
|
import net.sergeych.lyng.LyngVersion
|
||||||
import net.sergeych.lyng.Script
|
import net.sergeych.lyng.Script
|
||||||
import net.sergeych.lyng.ScriptError
|
import net.sergeych.lyng.ScriptError
|
||||||
@ -167,7 +168,7 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
|
|||||||
|
|
||||||
override fun help(context: Context): String =
|
override fun help(context: Context): String =
|
||||||
"""
|
"""
|
||||||
The Lyng script language interpreter, language version is $LyngVersion.
|
The Lyng script language runtime, language version is $LyngVersion.
|
||||||
|
|
||||||
Please refer form more information to the project site:
|
Please refer form more information to the project site:
|
||||||
https://gitea.sergeych.net/SergeychWorks/lyng
|
https://gitea.sergeych.net/SergeychWorks/lyng
|
||||||
@ -198,7 +199,12 @@ private class Lyng(val launcher: (suspend () -> Unit) -> Unit) : CliktCommand()
|
|||||||
launcher {
|
launcher {
|
||||||
// there is no script name, it is a first argument instead:
|
// there is no script name, it is a first argument instead:
|
||||||
processErrors {
|
processErrors {
|
||||||
baseScope.eval(execute!!)
|
val script = Compiler.compileWithResolution(
|
||||||
|
Source("<eval>", execute!!),
|
||||||
|
baseScope.currentImportProvider,
|
||||||
|
seedScope = baseScope
|
||||||
|
)
|
||||||
|
script.execute(baseScope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,7 +242,13 @@ suspend fun executeFile(fileName: String) {
|
|||||||
text = text.substring(pos + 1)
|
text = text.substring(pos + 1)
|
||||||
}
|
}
|
||||||
processErrors {
|
processErrors {
|
||||||
baseScopeDefer.await().eval(Source(fileName, text))
|
val scope = baseScopeDefer.await()
|
||||||
|
val script = Compiler.compileWithResolution(
|
||||||
|
Source(fileName, text),
|
||||||
|
scope.currentImportProvider,
|
||||||
|
seedScope = scope
|
||||||
|
)
|
||||||
|
script.execute(scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -56,7 +56,7 @@ class FsIntegrationJvmTest {
|
|||||||
"""
|
"""
|
||||||
import lyng.io.fs
|
import lyng.io.fs
|
||||||
// list current folder files
|
// list current folder files
|
||||||
println( Path(".").list().toList() )
|
println( Path(".").list() )
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -23,9 +23,11 @@ package net.sergeych.lyng.io.fs
|
|||||||
|
|
||||||
import net.sergeych.lyng.ModuleScope
|
import net.sergeych.lyng.ModuleScope
|
||||||
import net.sergeych.lyng.Scope
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.ScopeFacade
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.miniast.*
|
||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
import net.sergeych.lyng.pacman.ImportManager
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
|
import net.sergeych.lyng.requireScope
|
||||||
import net.sergeych.lyngio.fs.LyngFS
|
import net.sergeych.lyngio.fs.LyngFS
|
||||||
import net.sergeych.lyngio.fs.LyngFs
|
import net.sergeych.lyngio.fs.LyngFs
|
||||||
import net.sergeych.lyngio.fs.LyngPath
|
import net.sergeych.lyngio.fs.LyngPath
|
||||||
@ -189,7 +191,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
|||||||
fsGuard {
|
fsGuard {
|
||||||
val self = this.thisObj as ObjPath
|
val self = this.thisObj as ObjPath
|
||||||
val m = self.ensureMetadata()
|
val m = self.ensureMetadata()
|
||||||
m.modifiedAtMillis?.let { ObjInstant(kotlinx.datetime.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
m.modifiedAtMillis?.let { ObjInstant(kotlin.time.Instant.fromEpochMilliseconds(it)) } ?: ObjNull
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// modifiedAtMillis(): Int? — milliseconds since epoch or null
|
// modifiedAtMillis(): Int? — milliseconds since epoch or null
|
||||||
@ -312,9 +314,9 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
|||||||
ObjMap(mutableMapOf(
|
ObjMap(mutableMapOf(
|
||||||
ObjString("isFile") to ObjBool(m.isRegularFile),
|
ObjString("isFile") to ObjBool(m.isRegularFile),
|
||||||
ObjString("isDirectory") to ObjBool(m.isDirectory),
|
ObjString("isDirectory") to ObjBool(m.isDirectory),
|
||||||
ObjString("size") to (m.size?.toLong() ?: 0L).toObj(),
|
ObjString("size") to (m.size ?: 0L).toObj(),
|
||||||
ObjString("createdAtMillis") to ((m.createdAtMillis ?: 0L)).toObj(),
|
ObjString("createdAtMillis") to (m.createdAtMillis ?: 0L).toObj(),
|
||||||
ObjString("modifiedAtMillis") to ((m.modifiedAtMillis ?: 0L)).toObj(),
|
ObjString("modifiedAtMillis") to (m.modifiedAtMillis ?: 0L).toObj(),
|
||||||
ObjString("isSymlink") to ObjBool(m.isSymlink),
|
ObjString("isSymlink") to ObjBool(m.isSymlink),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@ -437,7 +439,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
|||||||
moduleName = module.packageName
|
moduleName = module.packageName
|
||||||
) {
|
) {
|
||||||
fsGuard {
|
fsGuard {
|
||||||
val chunkIt = thisObj.invokeInstanceMethod(this, "readUtf8Chunks")
|
val chunkIt = thisObj.invokeInstanceMethod(requireScope(), "readUtf8Chunks")
|
||||||
ObjFsLinesIterator(chunkIt)
|
ObjFsLinesIterator(chunkIt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -463,7 +465,7 @@ private suspend fun buildFsModule(module: ModuleScope, policy: FsAccessPolicy) {
|
|||||||
|
|
||||||
// --- Helper classes and utilities ---
|
// --- Helper classes and utilities ---
|
||||||
|
|
||||||
private fun parsePathArg(scope: Scope, self: ObjPath, arg: Obj): LyngPath {
|
private fun parsePathArg(scope: ScopeFacade, self: ObjPath, arg: Obj): LyngPath {
|
||||||
return when (arg) {
|
return when (arg) {
|
||||||
is ObjString -> arg.value.toPath()
|
is ObjString -> arg.value.toPath()
|
||||||
is ObjPath -> arg.path
|
is ObjPath -> arg.path
|
||||||
@ -472,11 +474,11 @@ private fun parsePathArg(scope: Scope, self: ObjPath, arg: Obj): LyngPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Map Fs access denials to Lyng runtime exceptions for script-friendly errors
|
// Map Fs access denials to Lyng runtime exceptions for script-friendly errors
|
||||||
private suspend inline fun Scope.fsGuard(crossinline block: suspend () -> Obj): Obj {
|
private suspend inline fun ScopeFacade.fsGuard(crossinline block: suspend () -> Obj): Obj {
|
||||||
return try {
|
return try {
|
||||||
block()
|
block()
|
||||||
} catch (e: AccessDeniedException) {
|
} catch (e: AccessDeniedException) {
|
||||||
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "access denied"))
|
raiseIllegalOperation(e.reasonDetail ?: "access denied")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -668,16 +670,17 @@ class ObjFsLinesIterator(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun ensureBufferFilled(scope: Scope) {
|
private suspend fun ensureBufferFilled(scope: ScopeFacade) {
|
||||||
if (buffer.contains('\n') || exhausted) return
|
if (buffer.contains('\n') || exhausted) return
|
||||||
|
val actualScope = scope.requireScope()
|
||||||
// Pull next chunk from the underlying iterator
|
// Pull next chunk from the underlying iterator
|
||||||
val it = chunksIterator.invokeInstanceMethod(scope, "iterator")
|
val it = chunksIterator.invokeInstanceMethod(actualScope, "iterator")
|
||||||
val hasNext = it.invokeInstanceMethod(scope, "hasNext").toBool()
|
val hasNext = it.invokeInstanceMethod(actualScope, "hasNext").toBool()
|
||||||
if (!hasNext) {
|
if (!hasNext) {
|
||||||
exhausted = true
|
exhausted = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val next = it.invokeInstanceMethod(scope, "next")
|
val next = it.invokeInstanceMethod(actualScope, "next")
|
||||||
buffer += next.toString()
|
buffer += next.toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,10 +20,11 @@ package net.sergeych.lyng.io.process
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import net.sergeych.lyng.ModuleScope
|
import net.sergeych.lyng.ModuleScope
|
||||||
import net.sergeych.lyng.Scope
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.ScopeFacade
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.miniast.*
|
||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
import net.sergeych.lyng.pacman.ImportManager
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
import net.sergeych.lyng.statement
|
import net.sergeych.lyng.requireScope
|
||||||
import net.sergeych.lyngio.process.*
|
import net.sergeych.lyngio.process.*
|
||||||
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
|
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
|
||||||
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
|
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
|
||||||
@ -205,20 +206,21 @@ class ObjRunningProcess(
|
|||||||
override fun toString(): String = "RunningProcess($process)"
|
override fun toString(): String = "RunningProcess($process)"
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend inline fun Scope.processGuard(crossinline block: suspend () -> Obj): Obj {
|
private suspend inline fun ScopeFacade.processGuard(crossinline block: suspend () -> Obj): Obj {
|
||||||
return try {
|
return try {
|
||||||
block()
|
block()
|
||||||
} catch (e: ProcessAccessDeniedException) {
|
} catch (e: ProcessAccessDeniedException) {
|
||||||
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "process access denied"))
|
raiseIllegalOperation(e.reasonDetail ?: "process access denied")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
raiseError(ObjIllegalOperationException(this, e.message ?: "process error"))
|
raiseIllegalOperation(e.message ?: "process error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
|
private fun Flow<String>.toLyngFlow(flowScope: ScopeFacade): ObjFlow {
|
||||||
val producer = statement {
|
val producer = net.sergeych.lyng.obj.ObjExternCallable.fromBridge {
|
||||||
val builder = (this as? net.sergeych.lyng.ClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
|
val scope = requireScope()
|
||||||
?: this.thisObj as? ObjFlowBuilder
|
val builder = (scope as? net.sergeych.lyng.BytecodeClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
|
||||||
|
?: scope.thisObj as? ObjFlowBuilder
|
||||||
|
|
||||||
this@toLyngFlow.collect {
|
this@toLyngFlow.collect {
|
||||||
try {
|
try {
|
||||||
@ -230,5 +232,5 @@ private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
|
|||||||
}
|
}
|
||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
return ObjFlow(producer, flowScope)
|
return ObjFlow(producer, flowScope.requireScope())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
|||||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
group = "net.sergeych"
|
group = "net.sergeych"
|
||||||
version = "1.2.1-SNAPSHOT"
|
version = "1.5.0-SNAPSHOT"
|
||||||
|
|
||||||
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,7 @@ package net.sergeych.lyng
|
|||||||
import net.sergeych.lyng.miniast.MiniTypeRef
|
import net.sergeych.lyng.miniast.MiniTypeRef
|
||||||
import net.sergeych.lyng.obj.Obj
|
import net.sergeych.lyng.obj.Obj
|
||||||
import net.sergeych.lyng.obj.ObjList
|
import net.sergeych.lyng.obj.ObjList
|
||||||
|
import net.sergeych.lyng.obj.ObjNull
|
||||||
import net.sergeych.lyng.obj.ObjRecord
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,30 +62,59 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hasComplex) {
|
if (!hasComplex) {
|
||||||
if (arguments.list.size != params.size)
|
if (arguments.list.size > params.size)
|
||||||
scope.raiseIllegalArgument("expected ${params.size} arguments, got ${arguments.list.size}")
|
scope.raiseIllegalArgument("expected ${params.size} arguments, got ${arguments.list.size}")
|
||||||
|
if (arguments.list.size < params.size) {
|
||||||
|
for (i in arguments.list.size until params.size) {
|
||||||
|
val a = params[i]
|
||||||
|
if (!a.type.isNullable) {
|
||||||
|
scope.raiseIllegalArgument("expected ${params.size} arguments, got ${arguments.list.size}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (i in params.indices) {
|
for (i in params.indices) {
|
||||||
val a = params[i]
|
val a = params[i]
|
||||||
val value = arguments.list[i]
|
val value = if (i < arguments.list.size) arguments.list[i] else ObjNull
|
||||||
scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable,
|
val recordType = if (declaringClass != null && a.accessType != null) {
|
||||||
|
ObjRecord.Type.ConstructorField
|
||||||
|
} else {
|
||||||
|
ObjRecord.Type.Argument
|
||||||
|
}
|
||||||
|
scope.addItem(
|
||||||
|
a.name,
|
||||||
|
(a.accessType ?: defaultAccessType).isMutable,
|
||||||
value.byValueCopy(),
|
value.byValueCopy(),
|
||||||
a.visibility ?: defaultVisibility,
|
a.visibility ?: defaultVisibility,
|
||||||
recordType = ObjRecord.Type.Argument,
|
recordType = recordType,
|
||||||
declaringClass = declaringClass,
|
declaringClass = declaringClass,
|
||||||
isTransient = a.isTransient)
|
isTransient = a.isTransient
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun assign(a: Item, value: Obj) {
|
fun assign(a: Item, value: Obj) {
|
||||||
scope.addItem(a.name, (a.accessType ?: defaultAccessType).isMutable,
|
val recordType = if (declaringClass != null && a.accessType != null) {
|
||||||
|
ObjRecord.Type.ConstructorField
|
||||||
|
} else {
|
||||||
|
ObjRecord.Type.Argument
|
||||||
|
}
|
||||||
|
scope.addItem(
|
||||||
|
a.name,
|
||||||
|
(a.accessType ?: defaultAccessType).isMutable,
|
||||||
value.byValueCopy(),
|
value.byValueCopy(),
|
||||||
a.visibility ?: defaultVisibility,
|
a.visibility ?: defaultVisibility,
|
||||||
recordType = ObjRecord.Type.Argument,
|
recordType = recordType,
|
||||||
declaringClass = declaringClass,
|
declaringClass = declaringClass,
|
||||||
isTransient = a.isTransient)
|
isTransient = a.isTransient
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun missingValue(a: Item, error: String): Obj {
|
||||||
|
return a.defaultValue?.callOn(scope)
|
||||||
|
?: if (a.type.isNullable) ObjNull else scope.raiseIllegalArgument(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare positional args and parameter count, handle tail-block binding
|
// Prepare positional args and parameter count, handle tail-block binding
|
||||||
@ -165,8 +195,7 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
|||||||
assign(a, namedValues[i]!!)
|
assign(a, namedValues[i]!!)
|
||||||
} else {
|
} else {
|
||||||
val value = if (hp < callArgs.size) callArgs[hp++]
|
val value = if (hp < callArgs.size) callArgs[hp++]
|
||||||
else a.defaultValue?.execute(scope)
|
else missingValue(a, "too few arguments for the call (missing ${a.name})")
|
||||||
?: scope.raiseIllegalArgument("too few arguments for the call (missing ${a.name})")
|
|
||||||
assign(a, value)
|
assign(a, value)
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
@ -186,8 +215,7 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
|||||||
assign(a, namedValues[i]!!)
|
assign(a, namedValues[i]!!)
|
||||||
} else {
|
} else {
|
||||||
val value = if (tp >= headPosBound) callArgs[tp--]
|
val value = if (tp >= headPosBound) callArgs[tp--]
|
||||||
else a.defaultValue?.execute(scope)
|
else missingValue(a, "too few arguments for the call")
|
||||||
?: scope.raiseIllegalArgument("too few arguments for the call")
|
|
||||||
assign(a, value)
|
assign(a, value)
|
||||||
}
|
}
|
||||||
i--
|
i--
|
||||||
@ -222,10 +250,194 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign arguments directly into frame slots using [paramSlotPlan] without creating scope locals.
|
||||||
|
* Default expressions must resolve through frame slots (no scope mirroring).
|
||||||
|
*/
|
||||||
|
suspend fun assignToFrame(
|
||||||
|
scope: Scope,
|
||||||
|
arguments: Arguments = scope.args,
|
||||||
|
paramSlotPlan: Map<String, Int>,
|
||||||
|
frame: FrameAccess,
|
||||||
|
slotOffset: Int = 0
|
||||||
|
) {
|
||||||
|
fun slotFor(name: String): Int {
|
||||||
|
val full = paramSlotPlan[name] ?: scope.raiseIllegalState("parameter slot for '$name' is missing")
|
||||||
|
val slot = full - slotOffset
|
||||||
|
if (slot < 0) scope.raiseIllegalState("parameter slot for '$name' is out of range")
|
||||||
|
return slot
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setFrameValue(slot: Int, value: Obj) {
|
||||||
|
when (value) {
|
||||||
|
is net.sergeych.lyng.obj.ObjInt -> frame.setInt(slot, value.value)
|
||||||
|
is net.sergeych.lyng.obj.ObjReal -> frame.setReal(slot, value.value)
|
||||||
|
is net.sergeych.lyng.obj.ObjBool -> frame.setBool(slot, value.value)
|
||||||
|
else -> frame.setObj(slot, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assign(a: Item, value: Obj) {
|
||||||
|
val slot = slotFor(a.name)
|
||||||
|
setFrameValue(slot, value.byValueCopy())
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun missingValue(a: Item, error: String): Obj {
|
||||||
|
return a.defaultValue?.callOn(scope)
|
||||||
|
?: if (a.type.isNullable) ObjNull else scope.raiseIllegalArgument(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fast path for simple positional-only calls with no ellipsis and no defaults
|
||||||
|
if (arguments.named.isEmpty() && !arguments.tailBlockMode) {
|
||||||
|
var hasComplex = false
|
||||||
|
for (p in params) {
|
||||||
|
if (p.isEllipsis || p.defaultValue != null) {
|
||||||
|
hasComplex = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hasComplex) {
|
||||||
|
if (arguments.list.size > params.size)
|
||||||
|
scope.raiseIllegalArgument("expected ${params.size} arguments, got ${arguments.list.size}")
|
||||||
|
if (arguments.list.size < params.size) {
|
||||||
|
for (i in arguments.list.size until params.size) {
|
||||||
|
val a = params[i]
|
||||||
|
if (!a.type.isNullable) {
|
||||||
|
scope.raiseIllegalArgument("expected ${params.size} arguments, got ${arguments.list.size}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i in params.indices) {
|
||||||
|
val a = params[i]
|
||||||
|
val value = if (i < arguments.list.size) arguments.list[i] else ObjNull
|
||||||
|
assign(a, value)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare positional args and parameter count, handle tail-block binding
|
||||||
|
val callArgs: List<Obj>
|
||||||
|
val paramsSize: Int
|
||||||
|
if (arguments.tailBlockMode) {
|
||||||
|
val lastParam = params.last()
|
||||||
|
if (arguments.named.containsKey(lastParam.name))
|
||||||
|
scope.raiseIllegalArgument("trailing block cannot be used when the last parameter is already assigned by a named argument")
|
||||||
|
paramsSize = params.size - 1
|
||||||
|
assign(lastParam, arguments.list.last())
|
||||||
|
callArgs = arguments.list.dropLast(1)
|
||||||
|
} else {
|
||||||
|
paramsSize = params.size
|
||||||
|
callArgs = arguments.list
|
||||||
|
}
|
||||||
|
|
||||||
|
val coveredByPositional = BooleanArray(paramsSize)
|
||||||
|
run {
|
||||||
|
var headRequired = 0
|
||||||
|
var tailRequired = 0
|
||||||
|
val ellipsisIdx = params.subList(0, paramsSize).indexOfFirst { it.isEllipsis }
|
||||||
|
if (ellipsisIdx >= 0) {
|
||||||
|
for (i in 0 until ellipsisIdx) if (!params[i].isEllipsis && params[i].defaultValue == null) headRequired++
|
||||||
|
for (i in paramsSize - 1 downTo ellipsisIdx + 1) if (params[i].defaultValue == null) tailRequired++
|
||||||
|
} else {
|
||||||
|
for (i in 0 until paramsSize) if (params[i].defaultValue == null) headRequired++
|
||||||
|
}
|
||||||
|
val P = callArgs.size
|
||||||
|
if (ellipsisIdx < 0) {
|
||||||
|
val k = minOf(P, paramsSize)
|
||||||
|
for (i in 0 until k) coveredByPositional[i] = true
|
||||||
|
} else {
|
||||||
|
val headTake = minOf(P, headRequired)
|
||||||
|
for (i in 0 until headTake) coveredByPositional[i] = true
|
||||||
|
val remaining = P - headTake
|
||||||
|
val tailTake = minOf(remaining, tailRequired)
|
||||||
|
var j = paramsSize - 1
|
||||||
|
var taken = 0
|
||||||
|
while (j > ellipsisIdx && taken < tailTake) {
|
||||||
|
coveredByPositional[j] = true
|
||||||
|
j--
|
||||||
|
taken++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val assignedByName = BooleanArray(paramsSize)
|
||||||
|
val namedValues = arrayOfNulls<Obj>(paramsSize)
|
||||||
|
if (arguments.named.isNotEmpty()) {
|
||||||
|
for ((k, v) in arguments.named) {
|
||||||
|
val idx = params.subList(0, paramsSize).indexOfFirst { it.name == k }
|
||||||
|
if (idx < 0) scope.raiseIllegalArgument("unknown parameter '$k'")
|
||||||
|
if (params[idx].isEllipsis) scope.raiseIllegalArgument("ellipsis (variadic) parameter cannot be assigned by name: '$k'")
|
||||||
|
if (coveredByPositional[idx]) scope.raiseIllegalArgument("argument '$k' is already set by positional argument")
|
||||||
|
if (assignedByName[idx]) scope.raiseIllegalArgument("argument '$k' is already set")
|
||||||
|
assignedByName[idx] = true
|
||||||
|
namedValues[idx] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun processHead(index: Int, headPos: Int): Pair<Int, Int> {
|
||||||
|
var i = index
|
||||||
|
var hp = headPos
|
||||||
|
while (i < paramsSize) {
|
||||||
|
val a = params[i]
|
||||||
|
if (a.isEllipsis) break
|
||||||
|
if (assignedByName[i]) {
|
||||||
|
assign(a, namedValues[i]!!)
|
||||||
|
} else {
|
||||||
|
val value = if (hp < callArgs.size) callArgs[hp++]
|
||||||
|
else missingValue(a, "too few arguments for the call (missing ${a.name})")
|
||||||
|
assign(a, value)
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return i to hp
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun processTail(startExclusive: Int, tailStart: Int, headPosBound: Int): Int {
|
||||||
|
var i = paramsSize - 1
|
||||||
|
var tp = tailStart
|
||||||
|
while (i > startExclusive) {
|
||||||
|
val a = params[i]
|
||||||
|
if (a.isEllipsis) break
|
||||||
|
if (i < assignedByName.size && assignedByName[i]) {
|
||||||
|
assign(a, namedValues[i]!!)
|
||||||
|
} else {
|
||||||
|
val value = if (tp >= headPosBound) callArgs[tp--]
|
||||||
|
else missingValue(a, "too few arguments for the call")
|
||||||
|
assign(a, value)
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
return tp
|
||||||
|
}
|
||||||
|
|
||||||
|
fun processEllipsis(index: Int, headPos: Int, tailPos: Int) {
|
||||||
|
val a = params[index]
|
||||||
|
val from = headPos
|
||||||
|
val to = tailPos
|
||||||
|
val l = if (from > to) ObjList()
|
||||||
|
else ObjList(callArgs.subList(from, to + 1).toMutableList())
|
||||||
|
assign(a, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
val ellipsisIndex = params.subList(0, paramsSize).indexOfFirst { it.isEllipsis }
|
||||||
|
|
||||||
|
if (ellipsisIndex >= 0) {
|
||||||
|
val (_, headConsumedTo) = processHead(0, 0)
|
||||||
|
val tailConsumedFrom = processTail(ellipsisIndex, callArgs.size - 1, headConsumedTo)
|
||||||
|
processEllipsis(ellipsisIndex, headConsumedTo, tailConsumedFrom)
|
||||||
|
} else {
|
||||||
|
val (_, headConsumedTo) = processHead(0, 0)
|
||||||
|
if (headConsumedTo != callArgs.size)
|
||||||
|
scope.raiseIllegalArgument("too many arguments for the call")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Single argument declaration descriptor.
|
* Single argument declaration descriptor.
|
||||||
*
|
*
|
||||||
* @param defaultValue default value, if set, can't be an [Obj] as it can depend on the call site, call args, etc.
|
* @param defaultValue default value, callable evaluated at call site.
|
||||||
* If not null, could be executed on __caller context__ only.
|
* If not null, could be executed on __caller context__ only.
|
||||||
*/
|
*/
|
||||||
data class Item(
|
data class Item(
|
||||||
@ -238,7 +450,7 @@ data class ArgsDeclaration(val params: List<Item>, val endTokenType: Token.Type)
|
|||||||
* Default value, if set, can't be an [Obj] as it can depend on the call site, call args, etc.
|
* Default value, if set, can't be an [Obj] as it can depend on the call site, call args, etc.
|
||||||
* So it is a [Statement] that must be executed on __caller context__.
|
* So it is a [Statement] that must be executed on __caller context__.
|
||||||
*/
|
*/
|
||||||
val defaultValue: Statement? = null,
|
val defaultValue: Obj? = null,
|
||||||
val accessType: AccessType? = null,
|
val accessType: AccessType? = null,
|
||||||
val visibility: Visibility? = null,
|
val visibility: Visibility? = null,
|
||||||
val isTransient: Boolean = false,
|
val isTransient: Boolean = false,
|
||||||
|
|||||||
@ -20,7 +20,7 @@ package net.sergeych.lyng
|
|||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
|
|
||||||
data class ParsedArgument(
|
data class ParsedArgument(
|
||||||
val value: Statement,
|
val value: Obj,
|
||||||
val pos: Pos,
|
val pos: Pos,
|
||||||
val isSplat: Boolean = false,
|
val isSplat: Boolean = false,
|
||||||
val name: String? = null,
|
val name: String? = null,
|
||||||
@ -40,115 +40,115 @@ data class ParsedArgument(
|
|||||||
if (!hasSplatOrNamed && count == this.size) {
|
if (!hasSplatOrNamed && count == this.size) {
|
||||||
val quick = when (count) {
|
val quick = when (count) {
|
||||||
0 -> Arguments.EMPTY
|
0 -> Arguments.EMPTY
|
||||||
1 -> Arguments(listOf(this.elementAt(0).value.execute(scope)), tailBlockMode)
|
1 -> Arguments(listOf(this.elementAt(0).value.callOn(scope)), tailBlockMode)
|
||||||
2 -> {
|
2 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1), tailBlockMode)
|
Arguments(listOf(a0, a1), tailBlockMode)
|
||||||
}
|
}
|
||||||
3 -> {
|
3 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2), tailBlockMode)
|
Arguments(listOf(a0, a1, a2), tailBlockMode)
|
||||||
}
|
}
|
||||||
4 -> {
|
4 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3), tailBlockMode)
|
||||||
}
|
}
|
||||||
5 -> {
|
5 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4), tailBlockMode)
|
||||||
}
|
}
|
||||||
6 -> {
|
6 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5), tailBlockMode)
|
||||||
}
|
}
|
||||||
7 -> {
|
7 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
val a6 = this.elementAt(6).value.execute(scope)
|
val a6 = this.elementAt(6).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6), tailBlockMode)
|
||||||
}
|
}
|
||||||
8 -> {
|
8 -> {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
val a6 = this.elementAt(6).value.execute(scope)
|
val a6 = this.elementAt(6).value.callOn(scope)
|
||||||
val a7 = this.elementAt(7).value.execute(scope)
|
val a7 = this.elementAt(7).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7), tailBlockMode)
|
||||||
}
|
}
|
||||||
9 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
9 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
val a6 = this.elementAt(6).value.execute(scope)
|
val a6 = this.elementAt(6).value.callOn(scope)
|
||||||
val a7 = this.elementAt(7).value.execute(scope)
|
val a7 = this.elementAt(7).value.callOn(scope)
|
||||||
val a8 = this.elementAt(8).value.execute(scope)
|
val a8 = this.elementAt(8).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8), tailBlockMode)
|
||||||
} else null
|
} else null
|
||||||
10 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
10 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
val a6 = this.elementAt(6).value.execute(scope)
|
val a6 = this.elementAt(6).value.callOn(scope)
|
||||||
val a7 = this.elementAt(7).value.execute(scope)
|
val a7 = this.elementAt(7).value.callOn(scope)
|
||||||
val a8 = this.elementAt(8).value.execute(scope)
|
val a8 = this.elementAt(8).value.callOn(scope)
|
||||||
val a9 = this.elementAt(9).value.execute(scope)
|
val a9 = this.elementAt(9).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9), tailBlockMode)
|
||||||
} else null
|
} else null
|
||||||
11 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
11 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
val a6 = this.elementAt(6).value.execute(scope)
|
val a6 = this.elementAt(6).value.callOn(scope)
|
||||||
val a7 = this.elementAt(7).value.execute(scope)
|
val a7 = this.elementAt(7).value.callOn(scope)
|
||||||
val a8 = this.elementAt(8).value.execute(scope)
|
val a8 = this.elementAt(8).value.callOn(scope)
|
||||||
val a9 = this.elementAt(9).value.execute(scope)
|
val a9 = this.elementAt(9).value.callOn(scope)
|
||||||
val a10 = this.elementAt(10).value.execute(scope)
|
val a10 = this.elementAt(10).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10), tailBlockMode)
|
||||||
} else null
|
} else null
|
||||||
12 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
12 -> if (PerfFlags.ARG_SMALL_ARITY_12) {
|
||||||
val a0 = this.elementAt(0).value.execute(scope)
|
val a0 = this.elementAt(0).value.callOn(scope)
|
||||||
val a1 = this.elementAt(1).value.execute(scope)
|
val a1 = this.elementAt(1).value.callOn(scope)
|
||||||
val a2 = this.elementAt(2).value.execute(scope)
|
val a2 = this.elementAt(2).value.callOn(scope)
|
||||||
val a3 = this.elementAt(3).value.execute(scope)
|
val a3 = this.elementAt(3).value.callOn(scope)
|
||||||
val a4 = this.elementAt(4).value.execute(scope)
|
val a4 = this.elementAt(4).value.callOn(scope)
|
||||||
val a5 = this.elementAt(5).value.execute(scope)
|
val a5 = this.elementAt(5).value.callOn(scope)
|
||||||
val a6 = this.elementAt(6).value.execute(scope)
|
val a6 = this.elementAt(6).value.callOn(scope)
|
||||||
val a7 = this.elementAt(7).value.execute(scope)
|
val a7 = this.elementAt(7).value.callOn(scope)
|
||||||
val a8 = this.elementAt(8).value.execute(scope)
|
val a8 = this.elementAt(8).value.callOn(scope)
|
||||||
val a9 = this.elementAt(9).value.execute(scope)
|
val a9 = this.elementAt(9).value.callOn(scope)
|
||||||
val a10 = this.elementAt(10).value.execute(scope)
|
val a10 = this.elementAt(10).value.callOn(scope)
|
||||||
val a11 = this.elementAt(11).value.execute(scope)
|
val a11 = this.elementAt(11).value.callOn(scope)
|
||||||
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11), tailBlockMode)
|
Arguments(listOf(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11), tailBlockMode)
|
||||||
} else null
|
} else null
|
||||||
else -> null
|
else -> null
|
||||||
@ -166,12 +166,12 @@ data class ParsedArgument(
|
|||||||
// Named argument
|
// Named argument
|
||||||
if (named == null) named = linkedMapOf()
|
if (named == null) named = linkedMapOf()
|
||||||
if (named.containsKey(x.name)) scope.raiseIllegalArgument("argument '${x.name}' is already set")
|
if (named.containsKey(x.name)) scope.raiseIllegalArgument("argument '${x.name}' is already set")
|
||||||
val v = x.value.execute(scope)
|
val v = x.value.callOn(scope)
|
||||||
named[x.name] = v
|
named[x.name] = v
|
||||||
namedSeen = true
|
namedSeen = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val value = x.value.execute(scope)
|
val value = x.value.callOn(scope)
|
||||||
if (x.isSplat) {
|
if (x.isSplat) {
|
||||||
when {
|
when {
|
||||||
// IMPORTANT: handle ObjMap BEFORE generic Iterable to ensure map splats
|
// IMPORTANT: handle ObjMap BEFORE generic Iterable to ensure map splats
|
||||||
|
|||||||
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
|
||||||
|
class BlockStatement(
|
||||||
|
val block: Script,
|
||||||
|
val slotPlan: Map<String, Int>,
|
||||||
|
val scopeId: Int,
|
||||||
|
val captureSlots: List<CaptureSlot> = emptyList(),
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "block statement")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun statements(): List<Statement> = block.debugStatements()
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.bytecode.BytecodeStatement
|
||||||
|
|
||||||
|
interface BytecodeBodyProvider {
|
||||||
|
fun bytecodeBody(): BytecodeStatement?
|
||||||
|
}
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
interface BytecodeCallable
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.bytecode.BytecodeStatement
|
||||||
|
import net.sergeych.lyng.bytecode.CmdVm
|
||||||
|
import net.sergeych.lyng.bytecode.seedFrameLocalsFromScope
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
|
||||||
|
internal suspend fun executeBytecodeWithSeed(scope: Scope, stmt: Statement, label: String): Obj {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
} ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
scope.pos = bytecode.pos
|
||||||
|
return CmdVm().execute(bytecode.bytecodeFunction(), scope, scope.args) { frame, _ ->
|
||||||
|
seedFrameLocalsFromScope(frame, scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile-time call metadata for known functions. Used to select lambda receiver semantics.
|
||||||
|
*/
|
||||||
|
data class CallSignature(
|
||||||
|
val tailBlockReceiverType: String? = null
|
||||||
|
)
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
data class CaptureSlot(
|
||||||
|
val name: String,
|
||||||
|
val ownerScopeId: Int? = null,
|
||||||
|
val ownerSlot: Int? = null,
|
||||||
|
)
|
||||||
@ -0,0 +1,202 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.*
|
||||||
|
|
||||||
|
data class ClassDeclBaseSpec(
|
||||||
|
val name: String,
|
||||||
|
val args: List<ParsedArgument>?
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ClassDeclSpec(
|
||||||
|
val declaredName: String?,
|
||||||
|
val className: String,
|
||||||
|
val typeName: String,
|
||||||
|
val startPos: Pos,
|
||||||
|
val isExtern: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isObject: Boolean,
|
||||||
|
val isAnonymous: Boolean,
|
||||||
|
val baseSpecs: List<ClassDeclBaseSpec>,
|
||||||
|
val constructorArgs: ArgsDeclaration?,
|
||||||
|
val constructorFieldIds: Map<String, Int>?,
|
||||||
|
val bodyInit: Statement?,
|
||||||
|
val initScope: List<Statement>,
|
||||||
|
)
|
||||||
|
|
||||||
|
internal suspend fun executeClassDecl(
|
||||||
|
scope: Scope,
|
||||||
|
spec: ClassDeclSpec,
|
||||||
|
bodyCaptureRecords: List<ObjRecord>? = null,
|
||||||
|
bodyCaptureNames: List<String>? = null
|
||||||
|
): Obj {
|
||||||
|
fun checkClosedParents(parents: List<ObjClass>, pos: Pos) {
|
||||||
|
val closedParent = parents.firstOrNull { it.isClosed } ?: return
|
||||||
|
throw ScriptError(pos, "can't inherit from closed class ${closedParent.className}")
|
||||||
|
}
|
||||||
|
if (spec.isObject) {
|
||||||
|
val parentClasses = spec.baseSpecs.map { baseSpec ->
|
||||||
|
val rec = scope[baseSpec.name] ?: throw ScriptError(spec.startPos, "unknown base class: ${baseSpec.name}")
|
||||||
|
(rec.value as? ObjClass) ?: throw ScriptError(spec.startPos, "${baseSpec.name} is not a class")
|
||||||
|
}
|
||||||
|
checkClosedParents(parentClasses, spec.startPos)
|
||||||
|
|
||||||
|
val newClass = ObjInstanceClass(spec.className, *parentClasses.toTypedArray())
|
||||||
|
newClass.isAnonymous = spec.isAnonymous
|
||||||
|
newClass.constructorMeta = ArgsDeclaration(emptyList(), Token.Type.RPAREN)
|
||||||
|
for (i in parentClasses.indices) {
|
||||||
|
val argsList = spec.baseSpecs[i].args
|
||||||
|
if (argsList != null) newClass.directParentArgs[parentClasses[i]] = argsList
|
||||||
|
}
|
||||||
|
|
||||||
|
val classScope = scope.createChildScope(newThisObj = newClass)
|
||||||
|
if (!bodyCaptureRecords.isNullOrEmpty() && !bodyCaptureNames.isNullOrEmpty()) {
|
||||||
|
classScope.captureRecords = bodyCaptureRecords
|
||||||
|
classScope.captureNames = bodyCaptureNames
|
||||||
|
}
|
||||||
|
classScope.currentClassCtx = newClass
|
||||||
|
newClass.classScope = classScope
|
||||||
|
classScope.addConst("object", newClass)
|
||||||
|
|
||||||
|
spec.bodyInit?.let { executeBytecodeWithSeed(classScope, it, "object body init") }
|
||||||
|
|
||||||
|
val instance = newClass.callOn(scope.createChildScope(Arguments.EMPTY))
|
||||||
|
if (spec.declaredName != null) {
|
||||||
|
scope.addItem(spec.declaredName, false, instance)
|
||||||
|
}
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spec.isExtern) {
|
||||||
|
val rec = scope[spec.className]
|
||||||
|
val existing = rec?.value as? ObjClass
|
||||||
|
val resolved = if (existing != null) {
|
||||||
|
existing
|
||||||
|
} else if (spec.className.contains('.')) {
|
||||||
|
scope.resolveQualifiedIdentifier(spec.className) as? ObjClass
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
val stub = resolved ?: ObjInstanceClass(spec.className).apply { this.isAbstract = true }
|
||||||
|
spec.declaredName?.let { scope.addItem(it, false, stub) }
|
||||||
|
return stub
|
||||||
|
}
|
||||||
|
|
||||||
|
val parentClasses = spec.baseSpecs.map { baseSpec ->
|
||||||
|
val rec = scope[baseSpec.name]
|
||||||
|
val cls = rec?.value as? ObjClass
|
||||||
|
if (cls != null) return@map cls
|
||||||
|
if (baseSpec.name == "Exception") return@map ObjException.Root
|
||||||
|
if (rec == null) throw ScriptError(spec.startPos, "unknown base class: ${baseSpec.name}")
|
||||||
|
throw ScriptError(spec.startPos, "${baseSpec.name} is not a class")
|
||||||
|
}
|
||||||
|
checkClosedParents(parentClasses, spec.startPos)
|
||||||
|
|
||||||
|
val constructorCode = object : Statement() {
|
||||||
|
override val pos: Pos = spec.startPos
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
val instance = scope.thisObj as ObjInstance
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val newClass = ObjInstanceClass(spec.className, *parentClasses.toTypedArray()).also {
|
||||||
|
it.isAbstract = spec.isAbstract
|
||||||
|
it.isClosed = spec.isClosed
|
||||||
|
it.instanceConstructor = constructorCode
|
||||||
|
it.constructorMeta = spec.constructorArgs
|
||||||
|
for (i in parentClasses.indices) {
|
||||||
|
val argsList = spec.baseSpecs[i].args
|
||||||
|
if (argsList != null) it.directParentArgs[parentClasses[i]] = argsList
|
||||||
|
}
|
||||||
|
spec.constructorArgs?.params?.forEach { p ->
|
||||||
|
if (p.accessType != null) {
|
||||||
|
it.createField(
|
||||||
|
p.name,
|
||||||
|
ObjNull,
|
||||||
|
isMutable = p.accessType == AccessType.Var,
|
||||||
|
visibility = p.visibility ?: Visibility.Public,
|
||||||
|
declaringClass = it,
|
||||||
|
pos = Pos.builtIn,
|
||||||
|
isTransient = p.isTransient,
|
||||||
|
type = ObjRecord.Type.ConstructorField,
|
||||||
|
fieldId = spec.constructorFieldIds?.get(p.name)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spec.declaredName?.let { name ->
|
||||||
|
scope.addItem(name, false, newClass)
|
||||||
|
val module = scope as? ModuleScope
|
||||||
|
val frame = module?.moduleFrame
|
||||||
|
if (module != null && frame != null) {
|
||||||
|
val idx = module.moduleFrameLocalSlotNames.indexOf(name)
|
||||||
|
if (idx >= 0) {
|
||||||
|
frame.setObj(idx, newClass)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val classScope = scope.createChildScope(newThisObj = newClass)
|
||||||
|
if (!bodyCaptureRecords.isNullOrEmpty() && !bodyCaptureNames.isNullOrEmpty()) {
|
||||||
|
classScope.captureRecords = bodyCaptureRecords
|
||||||
|
classScope.captureNames = bodyCaptureNames
|
||||||
|
}
|
||||||
|
classScope.currentClassCtx = newClass
|
||||||
|
newClass.classScope = classScope
|
||||||
|
spec.bodyInit?.let { executeBytecodeWithSeed(classScope, it, "class body init") }
|
||||||
|
if (spec.initScope.isNotEmpty()) {
|
||||||
|
for (s in spec.initScope) {
|
||||||
|
executeBytecodeWithSeed(classScope, s, "class init")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newClass.checkAbstractSatisfaction(spec.startPos)
|
||||||
|
return newClass
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun requireBytecodeBody(
|
||||||
|
scope: Scope,
|
||||||
|
stmt: Statement,
|
||||||
|
label: String
|
||||||
|
): net.sergeych.lyng.bytecode.BytecodeStatement {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is net.sergeych.lyng.bytecode.BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
return bytecode ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClassDeclStatement(
|
||||||
|
val spec: ClassDeclSpec,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = spec.startPos
|
||||||
|
val declaredName: String? get() = spec.declaredName
|
||||||
|
val typeName: String get() = spec.typeName
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return executeClassDecl(scope, spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun callOn(scope: Scope): Obj {
|
||||||
|
val target = scope.parent ?: scope
|
||||||
|
return executeClassDecl(target, spec)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjProperty
|
||||||
|
|
||||||
|
class ClassInstanceInitDeclStatement(
|
||||||
|
val initStatement: Statement,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "class instance init declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClassInstanceFieldDeclStatement(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val fieldId: Int?,
|
||||||
|
val initStatement: Statement?,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "class instance field declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClassInstancePropertyDeclStatement(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val prop: ObjProperty,
|
||||||
|
val methodId: Int?,
|
||||||
|
val initStatement: Statement?,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "class instance property declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ClassInstanceDelegatedDeclStatement(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val methodId: Int?,
|
||||||
|
val initStatement: Statement?,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "class instance delegated declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
import net.sergeych.lyng.obj.ObjNull
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
import net.sergeych.lyng.obj.ObjString
|
||||||
|
import net.sergeych.lyng.obj.ObjUnset
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
|
||||||
|
class ClassStaticFieldInitStatement(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val initializer: Statement?,
|
||||||
|
val isDelegated: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
val initValue = initializer?.let { execBytecodeOnly(scope, it, "class static field init") }?.byValueCopy()
|
||||||
|
?: ObjNull
|
||||||
|
val cls = scope.thisObj as? ObjClass
|
||||||
|
?: scope.raiseIllegalState("static field init requires class scope")
|
||||||
|
return if (isDelegated) {
|
||||||
|
val accessTypeStr = if (isMutable) "Var" else "Val"
|
||||||
|
val accessType = ObjString(accessTypeStr)
|
||||||
|
val finalDelegate = try {
|
||||||
|
initValue.invokeInstanceMethod(
|
||||||
|
scope,
|
||||||
|
"bind",
|
||||||
|
Arguments(ObjString(name), accessType, scope.thisObj)
|
||||||
|
)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
initValue
|
||||||
|
}
|
||||||
|
cls.createClassField(
|
||||||
|
name,
|
||||||
|
ObjUnset,
|
||||||
|
isMutable,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
startPos,
|
||||||
|
isTransient = isTransient,
|
||||||
|
type = ObjRecord.Type.Delegated
|
||||||
|
).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
scope.addItem(
|
||||||
|
name,
|
||||||
|
isMutable,
|
||||||
|
ObjUnset,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
recordType = ObjRecord.Type.Delegated,
|
||||||
|
isTransient = isTransient
|
||||||
|
).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
finalDelegate
|
||||||
|
} else {
|
||||||
|
cls.createClassField(
|
||||||
|
name,
|
||||||
|
initValue,
|
||||||
|
isMutable,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
startPos,
|
||||||
|
isTransient = isTransient
|
||||||
|
)
|
||||||
|
scope.addItem(
|
||||||
|
name,
|
||||||
|
isMutable,
|
||||||
|
initValue,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
recordType = ObjRecord.Type.Field,
|
||||||
|
isTransient = isTransient
|
||||||
|
)
|
||||||
|
initValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun execBytecodeOnly(scope: Scope, stmt: Statement, label: String): Obj {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is net.sergeych.lyng.bytecode.BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
} ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
return bytecode.execute(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,67 +18,62 @@
|
|||||||
package net.sergeych.lyng
|
package net.sergeych.lyng
|
||||||
|
|
||||||
import net.sergeych.lyng.obj.Obj
|
import net.sergeych.lyng.obj.Obj
|
||||||
import net.sergeych.lyng.obj.ObjClass
|
|
||||||
import net.sergeych.lyng.obj.ObjRecord
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scope that adds a "closure" to caller; most often it is used to apply class instance to caller scope.
|
* Bytecode-oriented closure scope that keeps the call scope parent chain for stack traces
|
||||||
* Inherits [Scope.args] and [Scope.thisObj] from [callScope] and adds lookup for symbols
|
* while carrying the lexical closure for `this` variants and module resolution.
|
||||||
* from [closureScope] with proper precedence
|
* Unlike legacy closure scopes, it does not override name lookup.
|
||||||
*/
|
*/
|
||||||
class ClosureScope(val callScope: Scope, val closureScope: Scope) :
|
class BytecodeClosureScope(
|
||||||
// Important: use closureScope.thisObj so unqualified members (e.g., fields) resolve to the instance
|
val callScope: Scope,
|
||||||
// we captured, not to the caller's `this` (e.g., FlowBuilder).
|
val closureScope: Scope,
|
||||||
|
private val preferredThisType: String? = null
|
||||||
|
) :
|
||||||
Scope(callScope, callScope.args, thisObj = closureScope.thisObj) {
|
Scope(callScope, callScope.args, thisObj = closureScope.thisObj) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// Preserve the lexical class context of the closure by default. This ensures that lambdas
|
val desired = preferredThisType?.let { typeName ->
|
||||||
// created inside a class method keep access to that class's private/protected members even
|
callScope.thisVariants.firstOrNull { it.objClass.className == typeName }
|
||||||
// when executed from within another object's method (e.g., Mutex.withLock), which may set
|
}
|
||||||
// its own currentClassCtx temporarily. If the closure has no class context, inherit caller's.
|
val primaryThis = when {
|
||||||
|
callScope is ApplyScope -> callScope.thisObj
|
||||||
|
desired != null -> desired
|
||||||
|
else -> closureScope.thisObj
|
||||||
|
}
|
||||||
|
val merged = ArrayList<Obj>(callScope.thisVariants.size + closureScope.thisVariants.size + 3)
|
||||||
|
desired?.let { merged.add(it) }
|
||||||
|
merged.add(callScope.thisObj)
|
||||||
|
merged.addAll(callScope.thisVariants)
|
||||||
|
if (callScope is ApplyScope) {
|
||||||
|
merged.add(callScope.applied.thisObj)
|
||||||
|
merged.addAll(callScope.applied.thisVariants)
|
||||||
|
}
|
||||||
|
merged.addAll(closureScope.thisVariants)
|
||||||
|
setThisVariants(primaryThis, merged)
|
||||||
this.currentClassCtx = closureScope.currentClassCtx ?: callScope.currentClassCtx
|
this.currentClassCtx = closureScope.currentClassCtx ?: callScope.currentClassCtx
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ApplyScope(val callScope: Scope, val applied: Scope) :
|
||||||
|
Scope(applied, callScope.args, callScope.pos, callScope.thisObj) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
// Merge applied receiver variants with the caller variants so qualified this@Type
|
||||||
|
// can see both the applied receiver and outer receivers.
|
||||||
|
val merged = ArrayList<Obj>(applied.thisVariants.size + callScope.thisVariants.size + 1)
|
||||||
|
merged.addAll(applied.thisVariants)
|
||||||
|
merged.addAll(callScope.thisVariants)
|
||||||
|
setThisVariants(callScope.thisObj, merged)
|
||||||
|
this.currentClassCtx = applied.currentClassCtx ?: callScope.currentClassCtx
|
||||||
|
}
|
||||||
|
|
||||||
override fun get(name: String): ObjRecord? {
|
override fun get(name: String): ObjRecord? {
|
||||||
if (name == "this") return thisObj.asReadonly
|
return applied.get(name) ?: callScope.get(name)
|
||||||
|
|
||||||
// 1. Current frame locals (parameters, local variables)
|
|
||||||
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
|
|
||||||
|
|
||||||
// 2. Lexical environment (captured locals from entire ancestry)
|
|
||||||
closureScope.chainLookupIgnoreClosure(name, followClosure = true, caller = currentClassCtx)?.let { return it }
|
|
||||||
|
|
||||||
// 3. Lexical this members (captured receiver)
|
|
||||||
val receiver = thisObj
|
|
||||||
val effectiveClass = receiver as? ObjClass ?: receiver.objClass
|
|
||||||
for (cls in effectiveClass.mro) {
|
|
||||||
val rec = cls.members[name] ?: cls.classScope?.objects?.get(name)
|
|
||||||
if (rec != null && !rec.isAbstract) {
|
|
||||||
if (canAccessMember(rec.visibility, rec.declaringClass ?: cls, currentClassCtx, name)) {
|
|
||||||
return rec.copy(receiver = receiver)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Finally, root object fallback
|
|
||||||
Obj.rootObjectType.members[name]?.let { rec ->
|
|
||||||
if (canAccessMember(rec.visibility, rec.declaringClass, currentClassCtx, name)) {
|
|
||||||
return rec.copy(receiver = receiver)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Call environment (caller locals, caller this, and global fallback)
|
override fun applyClosure(closure: Scope, preferredThisType: String?): Scope {
|
||||||
return callScope.get(name)
|
return BytecodeClosureScope(this, closure, preferredThisType)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ApplyScope(_parent: Scope,val applied: Scope) : Scope(_parent, thisObj = applied.thisObj) {
|
|
||||||
|
|
||||||
override fun get(name: String): ObjRecord? {
|
|
||||||
return applied.get(name) ?: super.get(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun applyClosure(closure: Scope): Scope {
|
|
||||||
return this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -19,8 +19,24 @@ package net.sergeych.lyng
|
|||||||
|
|
||||||
sealed class CodeContext {
|
sealed class CodeContext {
|
||||||
class Module(@Suppress("unused") val packageName: String?): CodeContext()
|
class Module(@Suppress("unused") val packageName: String?): CodeContext()
|
||||||
class Function(val name: String): CodeContext()
|
class Function(
|
||||||
|
val name: String,
|
||||||
|
val implicitThisMembers: Boolean = false,
|
||||||
|
val implicitThisTypeName: String? = null,
|
||||||
|
val typeParams: Set<String> = emptySet(),
|
||||||
|
val typeParamDecls: List<TypeDecl.TypeParam> = emptyList()
|
||||||
|
): CodeContext()
|
||||||
class ClassBody(val name: String, val isExtern: Boolean = false): CodeContext() {
|
class ClassBody(val name: String, val isExtern: Boolean = false): CodeContext() {
|
||||||
|
var typeParams: Set<String> = emptySet()
|
||||||
|
var typeParamDecls: List<TypeDecl.TypeParam> = emptyList()
|
||||||
val pendingInitializations = mutableMapOf<String, Pos>()
|
val pendingInitializations = mutableMapOf<String, Pos>()
|
||||||
|
val declaredMembers = mutableSetOf<String>()
|
||||||
|
val classScopeMembers = mutableSetOf<String>()
|
||||||
|
val memberOverrides = mutableMapOf<String, Boolean>()
|
||||||
|
val memberFieldIds = mutableMapOf<String, Int>()
|
||||||
|
val memberMethodIds = mutableMapOf<String, Int>()
|
||||||
|
var nextFieldId: Int = 0
|
||||||
|
var nextMethodId: Int = 0
|
||||||
|
var slotPlanId: Int? = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
|
||||||
|
class DelegatedVarDeclStatement(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val initializer: Statement,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val slotIndex: Int?,
|
||||||
|
val scopeId: Int?,
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "delegated var declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.ListLiteralRef
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
|
||||||
|
class DestructuringVarDeclStatement(
|
||||||
|
val pattern: ListLiteralRef,
|
||||||
|
val names: List<String>,
|
||||||
|
val initializer: Statement,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "destructuring declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
|
||||||
|
class EnumDeclStatement(
|
||||||
|
val declaredName: String,
|
||||||
|
val qualifiedName: String,
|
||||||
|
val entries: List<String>,
|
||||||
|
val lifted: Boolean,
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "enum declaration")
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun callOn(scope: Scope): Obj {
|
||||||
|
val target = scope.parent ?: scope
|
||||||
|
return execute(target)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
internal fun extensionCallableName(typeName: String, memberName: String): String {
|
||||||
|
return "__ext__${sanitizeExtensionTypeName(typeName)}__${memberName}"
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun extensionPropertyGetterName(typeName: String, memberName: String): String {
|
||||||
|
return "__ext_get__${sanitizeExtensionTypeName(typeName)}__${memberName}"
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun extensionPropertySetterName(typeName: String, memberName: String): String {
|
||||||
|
return "__ext_set__${sanitizeExtensionTypeName(typeName)}__${memberName}"
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sanitizeExtensionTypeName(typeName: String): String {
|
||||||
|
return typeName.replace('.', '_')
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjProperty
|
||||||
|
|
||||||
|
class ExtensionPropertyDeclStatement(
|
||||||
|
val extTypeName: String,
|
||||||
|
val property: ObjProperty,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val setterVisibility: Visibility?,
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "extension property declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
196
lynglib/src/commonMain/kotlin/net/sergeych/lyng/FrameAccess.kt
Normal file
196
lynglib/src/commonMain/kotlin/net/sergeych/lyng/FrameAccess.kt
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.bytecode.SlotType
|
||||||
|
import net.sergeych.lyng.obj.*
|
||||||
|
|
||||||
|
interface FrameAccess {
|
||||||
|
fun getSlotTypeCode(slot: Int): Byte
|
||||||
|
fun getObj(slot: Int): Obj
|
||||||
|
fun getInt(slot: Int): Long
|
||||||
|
fun getReal(slot: Int): Double
|
||||||
|
fun getBool(slot: Int): Boolean
|
||||||
|
fun setObj(slot: Int, value: Obj)
|
||||||
|
fun setInt(slot: Int, value: Long)
|
||||||
|
fun setReal(slot: Int, value: Double)
|
||||||
|
fun setBool(slot: Int, value: Boolean)
|
||||||
|
}
|
||||||
|
|
||||||
|
class FrameSlotRef(
|
||||||
|
private val frame: FrameAccess,
|
||||||
|
private val slot: Int,
|
||||||
|
) : net.sergeych.lyng.obj.Obj() {
|
||||||
|
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||||
|
val resolvedOther = when (other) {
|
||||||
|
is FrameSlotRef -> other.read()
|
||||||
|
is RecordSlotRef -> other.read()
|
||||||
|
else -> other
|
||||||
|
}
|
||||||
|
return read().compareTo(scope, resolvedOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun read(): Obj {
|
||||||
|
val typeCode = frame.getSlotTypeCode(slot)
|
||||||
|
return when (typeCode) {
|
||||||
|
SlotType.INT.code -> ObjInt.of(frame.getInt(slot))
|
||||||
|
SlotType.REAL.code -> ObjReal.of(frame.getReal(slot))
|
||||||
|
SlotType.BOOL.code -> if (frame.getBool(slot)) ObjTrue else ObjFalse
|
||||||
|
SlotType.OBJ.code -> frame.getObj(slot)
|
||||||
|
else -> frame.getObj(slot)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun callOn(scope: Scope): Obj {
|
||||||
|
val resolved = read()
|
||||||
|
if (resolved === this) {
|
||||||
|
scope.raiseNotImplemented("call on unresolved frame slot")
|
||||||
|
}
|
||||||
|
return resolved.callOn(scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun refersTo(frame: FrameAccess, slot: Int): Boolean {
|
||||||
|
return this.frame === frame && this.slot == slot
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun peekValue(): Obj? {
|
||||||
|
val bytecodeFrame = frame as? net.sergeych.lyng.bytecode.BytecodeFrame ?: return read()
|
||||||
|
val raw = bytecodeFrame.getRawObj(slot) ?: return null
|
||||||
|
if (raw is FrameSlotRef && raw.refersTo(bytecodeFrame, slot)) return null
|
||||||
|
return when (raw) {
|
||||||
|
is FrameSlotRef -> raw.peekValue()
|
||||||
|
is RecordSlotRef -> raw.peekValue()
|
||||||
|
else -> raw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun write(value: Obj) {
|
||||||
|
when (value) {
|
||||||
|
is ObjInt -> frame.setInt(slot, value.value)
|
||||||
|
is ObjReal -> frame.setReal(slot, value.value)
|
||||||
|
is ObjBool -> frame.setBool(slot, value.value)
|
||||||
|
else -> frame.setObj(slot, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ScopeSlotRef(
|
||||||
|
private val scope: Scope,
|
||||||
|
private val slot: Int,
|
||||||
|
private val name: String? = null,
|
||||||
|
) : net.sergeych.lyng.obj.Obj() {
|
||||||
|
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||||
|
val resolvedOther = when (other) {
|
||||||
|
is FrameSlotRef -> other.read()
|
||||||
|
is RecordSlotRef -> other.read()
|
||||||
|
is ScopeSlotRef -> other.read()
|
||||||
|
else -> other
|
||||||
|
}
|
||||||
|
return read().compareTo(scope, resolvedOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun read(): Obj {
|
||||||
|
val record = scope.getSlotRecord(slot)
|
||||||
|
val direct = record.value
|
||||||
|
if (direct is FrameSlotRef) return direct.read()
|
||||||
|
if (direct is RecordSlotRef) return direct.read()
|
||||||
|
if (direct is ScopeSlotRef) return direct.read()
|
||||||
|
if (direct !== ObjUnset) {
|
||||||
|
return direct
|
||||||
|
}
|
||||||
|
if (name == null) return record.value
|
||||||
|
val resolved = scope.get(name) ?: return record.value
|
||||||
|
if (resolved.value !== ObjUnset) {
|
||||||
|
scope.updateSlotFor(name, resolved)
|
||||||
|
}
|
||||||
|
return resolved.value
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun peekValue(): Obj? {
|
||||||
|
val record = scope.getSlotRecord(slot)
|
||||||
|
val direct = record.value
|
||||||
|
return when (direct) {
|
||||||
|
is FrameSlotRef -> direct.peekValue()
|
||||||
|
is RecordSlotRef -> direct.peekValue()
|
||||||
|
is ScopeSlotRef -> direct.peekValue()
|
||||||
|
else -> direct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun write(value: Obj) {
|
||||||
|
scope.setSlotValue(slot, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun callOn(scope: Scope): Obj {
|
||||||
|
val resolved = read()
|
||||||
|
if (resolved === this) {
|
||||||
|
scope.raiseNotImplemented("call on unresolved scope slot")
|
||||||
|
}
|
||||||
|
return resolved.callOn(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RecordSlotRef(
|
||||||
|
private val record: ObjRecord,
|
||||||
|
) : net.sergeych.lyng.obj.Obj() {
|
||||||
|
override suspend fun compareTo(scope: Scope, other: Obj): Int {
|
||||||
|
val resolvedOther = when (other) {
|
||||||
|
is FrameSlotRef -> other.read()
|
||||||
|
is RecordSlotRef -> other.read()
|
||||||
|
is ScopeSlotRef -> other.read()
|
||||||
|
else -> other
|
||||||
|
}
|
||||||
|
return read().compareTo(scope, resolvedOther)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun read(): Obj {
|
||||||
|
val direct = record.value
|
||||||
|
return when (direct) {
|
||||||
|
is FrameSlotRef -> direct.read()
|
||||||
|
is ScopeSlotRef -> direct.read()
|
||||||
|
else -> direct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun callOn(scope: Scope): Obj {
|
||||||
|
val resolved = read()
|
||||||
|
if (resolved === this) {
|
||||||
|
scope.raiseNotImplemented("call on unresolved record slot")
|
||||||
|
}
|
||||||
|
return resolved.callOn(scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun peekValue(): Obj? {
|
||||||
|
val direct = record.value
|
||||||
|
return when (direct) {
|
||||||
|
is FrameSlotRef -> direct.peekValue()
|
||||||
|
is RecordSlotRef -> direct.peekValue()
|
||||||
|
is ScopeSlotRef -> direct.peekValue()
|
||||||
|
else -> direct
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun write(value: Obj) {
|
||||||
|
val direct = record.value
|
||||||
|
if (direct is ScopeSlotRef) {
|
||||||
|
direct.write(value)
|
||||||
|
} else {
|
||||||
|
record.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,251 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
import net.sergeych.lyng.obj.ObjExternCallable
|
||||||
|
import net.sergeych.lyng.obj.ObjExtensionMethodCallable
|
||||||
|
import net.sergeych.lyng.obj.ObjInstance
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
import net.sergeych.lyng.obj.ObjString
|
||||||
|
import net.sergeych.lyng.obj.ObjUnset
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
|
||||||
|
class FunctionClosureBox(
|
||||||
|
var closure: Scope? = null,
|
||||||
|
var captureContext: Scope? = null,
|
||||||
|
var captureRecords: List<ObjRecord>? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class FunctionDeclSpec(
|
||||||
|
val name: String,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isStatic: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isDelegated: Boolean,
|
||||||
|
val delegateExpression: Statement?,
|
||||||
|
val delegateInitStatement: Statement?,
|
||||||
|
val extTypeName: String?,
|
||||||
|
val extensionWrapperName: String?,
|
||||||
|
val memberMethodId: Int?,
|
||||||
|
val actualExtern: Boolean,
|
||||||
|
val parentIsClassBody: Boolean,
|
||||||
|
val externCallSignature: CallSignature?,
|
||||||
|
val annotation: (suspend (Scope, ObjString, Statement) -> Statement)?,
|
||||||
|
val fnBody: Statement,
|
||||||
|
val closureBox: FunctionClosureBox,
|
||||||
|
val captureSlots: List<CaptureSlot>,
|
||||||
|
val slotIndex: Int?,
|
||||||
|
val scopeId: Int?,
|
||||||
|
val startPos: Pos,
|
||||||
|
)
|
||||||
|
|
||||||
|
internal suspend fun executeFunctionDecl(
|
||||||
|
scope: Scope,
|
||||||
|
spec: FunctionDeclSpec,
|
||||||
|
captureRecords: List<ObjRecord>? = null
|
||||||
|
): Obj {
|
||||||
|
spec.closureBox.captureRecords = captureRecords
|
||||||
|
if (spec.actualExtern && spec.extTypeName == null && !spec.parentIsClassBody) {
|
||||||
|
val existing = scope.get(spec.name)
|
||||||
|
if (existing != null) {
|
||||||
|
val value = (existing.value as? ObjExternCallable) ?: ObjExternCallable.wrap(existing.value)
|
||||||
|
scope.addItem(
|
||||||
|
spec.name,
|
||||||
|
false,
|
||||||
|
value,
|
||||||
|
spec.visibility,
|
||||||
|
callSignature = existing.callSignature
|
||||||
|
)
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spec.isDelegated) {
|
||||||
|
val delegateExpr = spec.delegateExpression ?: scope.raiseError("delegated function missing delegate")
|
||||||
|
val accessType = ObjString("Callable")
|
||||||
|
val initValue = executeBytecodeWithSeed(scope, delegateExpr, "delegated function")
|
||||||
|
val finalDelegate = try {
|
||||||
|
initValue.invokeInstanceMethod(scope, "bind", Arguments(ObjString(spec.name), accessType, scope.thisObj))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
initValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spec.extTypeName != null) {
|
||||||
|
val type = scope[spec.extTypeName]?.value ?: scope.raiseSymbolNotFound("class ${spec.extTypeName} not found")
|
||||||
|
if (type !is ObjClass) scope.raiseClassCastError("${spec.extTypeName} is not the class instance")
|
||||||
|
scope.addExtension(
|
||||||
|
type,
|
||||||
|
spec.name,
|
||||||
|
ObjRecord(ObjUnset, isMutable = false, visibility = spec.visibility, declaringClass = null, type = ObjRecord.Type.Delegated).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return finalDelegate
|
||||||
|
}
|
||||||
|
|
||||||
|
val th = scope.thisObj
|
||||||
|
if (spec.isStatic) {
|
||||||
|
(th as ObjClass).createClassField(
|
||||||
|
spec.name,
|
||||||
|
ObjUnset,
|
||||||
|
false,
|
||||||
|
spec.visibility,
|
||||||
|
null,
|
||||||
|
spec.startPos,
|
||||||
|
isTransient = spec.isTransient,
|
||||||
|
type = ObjRecord.Type.Delegated
|
||||||
|
).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
scope.addItem(
|
||||||
|
spec.name,
|
||||||
|
false,
|
||||||
|
ObjUnset,
|
||||||
|
spec.visibility,
|
||||||
|
recordType = ObjRecord.Type.Delegated,
|
||||||
|
isTransient = spec.isTransient
|
||||||
|
).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
} else if (th is ObjClass) {
|
||||||
|
val cls: ObjClass = th
|
||||||
|
cls.createField(
|
||||||
|
spec.name,
|
||||||
|
ObjUnset,
|
||||||
|
false,
|
||||||
|
spec.visibility,
|
||||||
|
null,
|
||||||
|
spec.startPos,
|
||||||
|
declaringClass = cls,
|
||||||
|
isAbstract = spec.isAbstract,
|
||||||
|
isClosed = spec.isClosed,
|
||||||
|
isOverride = spec.isOverride,
|
||||||
|
isTransient = spec.isTransient,
|
||||||
|
type = ObjRecord.Type.Delegated,
|
||||||
|
methodId = spec.memberMethodId
|
||||||
|
)
|
||||||
|
val initStmt = spec.delegateInitStatement
|
||||||
|
?: scope.raiseIllegalState("missing delegated init statement for ${spec.name}")
|
||||||
|
cls.instanceInitializers += requireBytecodeBody(scope, initStmt, "delegated function init")
|
||||||
|
} else {
|
||||||
|
scope.addItem(
|
||||||
|
spec.name,
|
||||||
|
false,
|
||||||
|
ObjUnset,
|
||||||
|
spec.visibility,
|
||||||
|
recordType = ObjRecord.Type.Delegated,
|
||||||
|
isTransient = spec.isTransient
|
||||||
|
).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return finalDelegate
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spec.isStatic || !spec.parentIsClassBody) {
|
||||||
|
spec.closureBox.closure = scope
|
||||||
|
}
|
||||||
|
if (spec.parentIsClassBody) {
|
||||||
|
spec.closureBox.captureContext = scope
|
||||||
|
}
|
||||||
|
|
||||||
|
val annotatedFnBody = spec.annotation?.invoke(scope, ObjString(spec.name), spec.fnBody) ?: spec.fnBody
|
||||||
|
val compiledFnBody = annotatedFnBody
|
||||||
|
|
||||||
|
spec.extTypeName?.let { typeName ->
|
||||||
|
val type = scope[typeName]?.value ?: scope.raiseSymbolNotFound("class $typeName not found")
|
||||||
|
if (type !is ObjClass) scope.raiseClassCastError("$typeName is not the class instance")
|
||||||
|
scope.addExtension(
|
||||||
|
type,
|
||||||
|
spec.name,
|
||||||
|
ObjRecord(
|
||||||
|
compiledFnBody,
|
||||||
|
isMutable = false,
|
||||||
|
visibility = spec.visibility,
|
||||||
|
declaringClass = null,
|
||||||
|
type = ObjRecord.Type.Fun
|
||||||
|
)
|
||||||
|
)
|
||||||
|
val wrapperName = spec.extensionWrapperName ?: extensionCallableName(typeName, spec.name)
|
||||||
|
val wrapper = ObjExtensionMethodCallable(spec.name, compiledFnBody)
|
||||||
|
scope.addItem(wrapperName, false, wrapper, spec.visibility, recordType = ObjRecord.Type.Fun)
|
||||||
|
} ?: run {
|
||||||
|
val th = scope.thisObj
|
||||||
|
if (!spec.isStatic && th is ObjClass) {
|
||||||
|
val cls: ObjClass = th
|
||||||
|
cls.createField(
|
||||||
|
spec.name,
|
||||||
|
compiledFnBody,
|
||||||
|
isMutable = true,
|
||||||
|
visibility = spec.visibility,
|
||||||
|
pos = spec.startPos,
|
||||||
|
declaringClass = cls,
|
||||||
|
isAbstract = spec.isAbstract,
|
||||||
|
isClosed = spec.isClosed,
|
||||||
|
isOverride = spec.isOverride,
|
||||||
|
type = ObjRecord.Type.Fun,
|
||||||
|
methodId = spec.memberMethodId
|
||||||
|
)
|
||||||
|
val memberValue = cls.members[spec.name]?.value ?: compiledFnBody
|
||||||
|
scope.addItem(spec.name, false, memberValue, spec.visibility, callSignature = spec.externCallSignature)
|
||||||
|
compiledFnBody
|
||||||
|
} else {
|
||||||
|
scope.addItem(
|
||||||
|
spec.name,
|
||||||
|
false,
|
||||||
|
compiledFnBody,
|
||||||
|
spec.visibility,
|
||||||
|
recordType = ObjRecord.Type.Fun,
|
||||||
|
callSignature = spec.externCallSignature
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return annotatedFnBody
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun requireBytecodeBody(
|
||||||
|
scope: Scope,
|
||||||
|
stmt: Statement,
|
||||||
|
label: String
|
||||||
|
): net.sergeych.lyng.bytecode.BytecodeStatement {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is net.sergeych.lyng.bytecode.BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
return bytecode ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
}
|
||||||
|
|
||||||
|
class FunctionDeclStatement(
|
||||||
|
val spec: FunctionDeclSpec,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = spec.startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return executeFunctionDecl(scope, spec)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun callOn(scope: Scope): Obj {
|
||||||
|
val target = scope.parent ?: scope
|
||||||
|
return executeFunctionDecl(target, spec)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
sealed class ImportBindingSource {
|
||||||
|
data class Module(val name: String, val pos: Pos) : ImportBindingSource()
|
||||||
|
object Root : ImportBindingSource()
|
||||||
|
object Seed : ImportBindingSource()
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ImportBinding(
|
||||||
|
val symbol: String,
|
||||||
|
val source: ImportBindingSource,
|
||||||
|
)
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
|
||||||
|
class InlineBlockStatement(
|
||||||
|
private val statements: List<Statement>,
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
var last: Obj = ObjVoid
|
||||||
|
for (stmt in statements) {
|
||||||
|
last = requireBytecodeBody(scope, stmt, "inline block").execute(scope)
|
||||||
|
}
|
||||||
|
return last
|
||||||
|
}
|
||||||
|
|
||||||
|
fun statements(): List<Statement> = statements
|
||||||
|
|
||||||
|
private suspend fun requireBytecodeBody(scope: Scope, stmt: Statement, label: String): net.sergeych.lyng.bytecode.BytecodeStatement {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is net.sergeych.lyng.bytecode.BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
return bytecode ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,139 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjNull
|
||||||
|
import net.sergeych.lyng.obj.ObjProperty
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
import net.sergeych.lyng.obj.ObjString
|
||||||
|
import net.sergeych.lyng.obj.ObjUnset
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
|
||||||
|
class InstanceFieldInitStatement(
|
||||||
|
val storageName: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isLateInitVal: Boolean,
|
||||||
|
val initializer: Statement?,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
val initValue = initializer?.let { execBytecodeOnly(scope, it, "instance field init") }?.byValueCopy()
|
||||||
|
?: if (isLateInitVal) ObjUnset else ObjNull
|
||||||
|
scope.addItem(
|
||||||
|
storageName,
|
||||||
|
isMutable,
|
||||||
|
initValue,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
recordType = ObjRecord.Type.Field,
|
||||||
|
isAbstract = isAbstract,
|
||||||
|
isClosed = isClosed,
|
||||||
|
isOverride = isOverride,
|
||||||
|
isTransient = isTransient
|
||||||
|
)
|
||||||
|
return ObjVoid
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun execBytecodeOnly(scope: Scope, stmt: Statement, label: String): Obj {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is net.sergeych.lyng.bytecode.BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
} ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
return bytecode.execute(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class InstancePropertyInitStatement(
|
||||||
|
val storageName: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val prop: ObjProperty,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
scope.addItem(
|
||||||
|
storageName,
|
||||||
|
isMutable,
|
||||||
|
prop,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
recordType = ObjRecord.Type.Property,
|
||||||
|
isAbstract = isAbstract,
|
||||||
|
isClosed = isClosed,
|
||||||
|
isOverride = isOverride,
|
||||||
|
isTransient = isTransient
|
||||||
|
)
|
||||||
|
return ObjVoid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class InstanceDelegatedInitStatement(
|
||||||
|
val storageName: String,
|
||||||
|
val memberName: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val accessTypeLabel: String,
|
||||||
|
val initializer: Statement,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
val initValue = executeBytecodeWithSeed(scope, initializer, "instance delegated init")
|
||||||
|
val accessType = ObjString(accessTypeLabel)
|
||||||
|
val finalDelegate = try {
|
||||||
|
initValue.invokeInstanceMethod(
|
||||||
|
scope,
|
||||||
|
"bind",
|
||||||
|
Arguments(ObjString(memberName), accessType, scope.thisObj)
|
||||||
|
)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
initValue
|
||||||
|
}
|
||||||
|
scope.addItem(
|
||||||
|
storageName,
|
||||||
|
isMutable,
|
||||||
|
ObjUnset,
|
||||||
|
visibility,
|
||||||
|
writeVisibility,
|
||||||
|
recordType = ObjRecord.Type.Delegated,
|
||||||
|
isAbstract = isAbstract,
|
||||||
|
isClosed = isClosed,
|
||||||
|
isOverride = isOverride,
|
||||||
|
isTransient = isTransient
|
||||||
|
).apply {
|
||||||
|
delegate = finalDelegate
|
||||||
|
}
|
||||||
|
return ObjVoid
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
package net.sergeych.lyng
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.bytecode.BytecodeFrame
|
||||||
|
import net.sergeych.lyng.bytecode.CmdFunction
|
||||||
import net.sergeych.lyng.obj.ObjRecord
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
import net.sergeych.lyng.obj.ObjString
|
import net.sergeych.lyng.obj.ObjString
|
||||||
import net.sergeych.lyng.pacman.ImportProvider
|
import net.sergeych.lyng.pacman.ImportProvider
|
||||||
@ -33,6 +35,46 @@ class ModuleScope(
|
|||||||
|
|
||||||
constructor(importProvider: ImportProvider, source: Source) : this(importProvider, source.startPos, source.fileName)
|
constructor(importProvider: ImportProvider, source: Source) : this(importProvider, source.startPos, source.fileName)
|
||||||
|
|
||||||
|
internal var importedModules: List<ModuleScope> = emptyList()
|
||||||
|
internal var moduleFrame: BytecodeFrame? = null
|
||||||
|
internal var moduleFrameLocalCount: Int = -1
|
||||||
|
internal var moduleFrameLocalSlotNames: Array<String?> = emptyArray()
|
||||||
|
internal var moduleFrameLocalSlotMutables: BooleanArray = BooleanArray(0)
|
||||||
|
internal var moduleFrameLocalSlotDelegated: BooleanArray = BooleanArray(0)
|
||||||
|
|
||||||
|
internal fun ensureModuleFrame(fn: CmdFunction): BytecodeFrame {
|
||||||
|
val current = moduleFrame
|
||||||
|
val frame = if (current == null) {
|
||||||
|
BytecodeFrame(fn.localCount, 0).also {
|
||||||
|
moduleFrame = it
|
||||||
|
moduleFrameLocalCount = fn.localCount
|
||||||
|
}
|
||||||
|
} else if (fn.localCount > moduleFrameLocalCount) {
|
||||||
|
val next = BytecodeFrame(fn.localCount, 0)
|
||||||
|
current.copyTo(next)
|
||||||
|
moduleFrame = next
|
||||||
|
moduleFrameLocalCount = fn.localCount
|
||||||
|
// Retarget frame-based locals to the new frame instance.
|
||||||
|
val localNames = fn.localSlotNames
|
||||||
|
for (i in localNames.indices) {
|
||||||
|
val name = localNames[i] ?: continue
|
||||||
|
val record = objects[name] ?: localBindings[name] ?: continue
|
||||||
|
val value = record.value
|
||||||
|
if (value is FrameSlotRef && value.refersTo(current, i)) {
|
||||||
|
record.value = FrameSlotRef(next, i)
|
||||||
|
updateSlotFor(name, record)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next
|
||||||
|
} else {
|
||||||
|
current
|
||||||
|
}
|
||||||
|
moduleFrameLocalSlotNames = fn.localSlotNames
|
||||||
|
moduleFrameLocalSlotMutables = fn.localSlotMutables
|
||||||
|
moduleFrameLocalSlotDelegated = fn.localSlotDelegated
|
||||||
|
return frame
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import symbols into the scope. It _is called_ after the module is imported by [ImportProvider.prepareImport]
|
* Import symbols into the scope. It _is called_ after the module is imported by [ImportProvider.prepareImport]
|
||||||
* which checks symbol availability and accessibility prior to execution.
|
* which checks symbol availability and accessibility prior to execution.
|
||||||
@ -59,6 +101,7 @@ class ModuleScope(
|
|||||||
// when importing records, we keep track of its package (not otherwise needed)
|
// when importing records, we keep track of its package (not otherwise needed)
|
||||||
if (record.importedFrom == null) record.importedFrom = this
|
if (record.importedFrom == null) record.importedFrom = this
|
||||||
scope.objects[newName] = record
|
scope.objects[newName] = record
|
||||||
|
scope.updateSlotFor(newName, record)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -91,5 +134,3 @@ class ModuleScope(
|
|||||||
super.get(name)
|
super.get(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -358,6 +358,7 @@ private class Parser(fromPos: Pos) {
|
|||||||
"in" -> Token("in", from, Token.Type.IN)
|
"in" -> Token("in", from, Token.Type.IN)
|
||||||
"is" -> Token("is", from, Token.Type.IS)
|
"is" -> Token("is", from, Token.Type.IS)
|
||||||
"by" -> Token("by", from, Token.Type.BY)
|
"by" -> Token("by", from, Token.Type.BY)
|
||||||
|
"step" -> Token("step", from, Token.Type.STEP)
|
||||||
"object" -> Token("object", from, Token.Type.OBJECT)
|
"object" -> Token("object", from, Token.Type.OBJECT)
|
||||||
"as" -> {
|
"as" -> {
|
||||||
// support both `as` and tight `as?` without spaces
|
// support both `as` and tight `as?` without spaces
|
||||||
|
|||||||
@ -72,4 +72,5 @@ object PerfFlags {
|
|||||||
|
|
||||||
// Specialized non-allocating integer range iteration in hot loops
|
// Specialized non-allocating integer range iteration in hot loops
|
||||||
var RANGE_FAST_ITER: Boolean = PerfDefaults.RANGE_FAST_ITER
|
var RANGE_FAST_ITER: Boolean = PerfDefaults.RANGE_FAST_ITER
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -19,7 +19,8 @@ package net.sergeych.lyng
|
|||||||
|
|
||||||
data class Pos(val source: Source, val line: Int, val column: Int) {
|
data class Pos(val source: Source, val line: Int, val column: Int) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "${source.fileName}:${line+1}:${column}"
|
val col = if (column >= 0) column + 1 else column
|
||||||
|
return "${source.fileName}:${line+1}:$col"
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
|
|||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.bytecode.CmdFrame
|
||||||
|
import net.sergeych.lyng.bytecode.CmdVm
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjNull
|
||||||
|
|
||||||
|
class PropertyAccessorStatement(
|
||||||
|
val body: Statement,
|
||||||
|
val argName: String?,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
if (argName != null) {
|
||||||
|
val prev = scope.skipScopeCreation
|
||||||
|
scope.skipScopeCreation = true
|
||||||
|
return try {
|
||||||
|
val bytecodeStmt = requireBytecodeBody(scope, body, "property accessor")
|
||||||
|
val fn = bytecodeStmt.bytecodeFunction()
|
||||||
|
val binder: suspend (CmdFrame, Arguments) -> Unit = { frame, arguments ->
|
||||||
|
val slotPlan = fn.localSlotPlanByName()
|
||||||
|
val slotIndex = slotPlan[argName]
|
||||||
|
?: scope.raiseIllegalState("property accessor argument $argName missing from slot plan")
|
||||||
|
val argValue = arguments.list.firstOrNull() ?: ObjNull
|
||||||
|
frame.frame.setObj(slotIndex, argValue)
|
||||||
|
}
|
||||||
|
scope.pos = pos
|
||||||
|
CmdVm().execute(fn, scope, scope.args, binder)
|
||||||
|
} finally {
|
||||||
|
scope.skipScopeCreation = prev
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return requireBytecodeBody(scope, body, "property accessor").execute(scope)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun requireBytecodeBody(scope: Scope, stmt: Statement, label: String): net.sergeych.lyng.bytecode.BytecodeStatement {
|
||||||
|
val bytecode = when (stmt) {
|
||||||
|
is net.sergeych.lyng.bytecode.BytecodeStatement -> stmt
|
||||||
|
is BytecodeBodyProvider -> stmt.bytecodeBody()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
return bytecode ?: scope.raiseIllegalState("$label requires bytecode statement")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -20,7 +20,7 @@ package net.sergeych.lyng
|
|||||||
/**
|
/**
|
||||||
* Tiny, size-bounded cache for compiled Regex patterns. Activated only when [PerfFlags.REGEX_CACHE] is true.
|
* Tiny, size-bounded cache for compiled Regex patterns. Activated only when [PerfFlags.REGEX_CACHE] is true.
|
||||||
* This is a very simple FIFO-ish cache sufficient for micro-benchmarks and common repeated patterns.
|
* This is a very simple FIFO-ish cache sufficient for micro-benchmarks and common repeated patterns.
|
||||||
* Not thread-safe by design; the interpreter typically runs scripts on confined executors.
|
* Not thread-safe by design; the runtime typically runs scripts on confined executors.
|
||||||
*/
|
*/
|
||||||
object RegexCache {
|
object RegexCache {
|
||||||
private const val MAX = 64
|
private const val MAX = 64
|
||||||
|
|||||||
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
package net.sergeych.lyng
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.bytecode.BytecodeStatement
|
||||||
|
import net.sergeych.lyng.bytecode.CmdDisassembler
|
||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
import net.sergeych.lyng.pacman.ImportManager
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
import net.sergeych.lyng.pacman.ImportProvider
|
import net.sergeych.lyng.pacman.ImportProvider
|
||||||
@ -37,7 +39,7 @@ fun nextFrameId(): Long = FrameIdGen.nextId()
|
|||||||
*
|
*
|
||||||
* There are special types of scopes:
|
* There are special types of scopes:
|
||||||
*
|
*
|
||||||
* - [ClosureScope] - scope used to apply a closure to some thisObj scope
|
* - [BytecodeClosureScope] - scope used to apply a closure to some thisObj scope
|
||||||
*/
|
*/
|
||||||
open class Scope(
|
open class Scope(
|
||||||
var parent: Scope?,
|
var parent: Scope?,
|
||||||
@ -50,11 +52,15 @@ open class Scope(
|
|||||||
var currentClassCtx: net.sergeych.lyng.obj.ObjClass? = parent?.currentClassCtx
|
var currentClassCtx: net.sergeych.lyng.obj.ObjClass? = parent?.currentClassCtx
|
||||||
// Unique id per scope frame for PICs; regenerated on each borrow from the pool.
|
// Unique id per scope frame for PICs; regenerated on each borrow from the pool.
|
||||||
var frameId: Long = nextFrameId()
|
var frameId: Long = nextFrameId()
|
||||||
|
@PublishedApi
|
||||||
|
internal val thisVariants: MutableList<Obj> = mutableListOf()
|
||||||
|
|
||||||
// Fast-path storage for local variables/arguments accessed by slot index.
|
// Fast-path storage for local variables/arguments accessed by slot index.
|
||||||
// Enabled by default for child scopes; module/class scopes can ignore it.
|
// Enabled by default for child scopes; module/class scopes can ignore it.
|
||||||
private val slots: MutableList<ObjRecord> = mutableListOf()
|
private val slots: MutableList<ObjRecord> = mutableListOf()
|
||||||
private val nameToSlot: MutableMap<String, Int> = mutableMapOf()
|
private val nameToSlot: MutableMap<String, Int> = mutableMapOf()
|
||||||
|
internal var captureRecords: List<ObjRecord>? = null
|
||||||
|
internal var captureNames: List<String>? = null
|
||||||
/**
|
/**
|
||||||
* Auxiliary per-frame map of local bindings (locals declared in this frame).
|
* Auxiliary per-frame map of local bindings (locals declared in this frame).
|
||||||
* This helps resolving locals across suspension when slot ownership isn't
|
* This helps resolving locals across suspension when slot ownership isn't
|
||||||
@ -64,6 +70,31 @@ open class Scope(
|
|||||||
|
|
||||||
internal val extensions: MutableMap<ObjClass, MutableMap<String, ObjRecord>> = mutableMapOf()
|
internal val extensions: MutableMap<ObjClass, MutableMap<String, ObjRecord>> = mutableMapOf()
|
||||||
|
|
||||||
|
init {
|
||||||
|
setThisVariants(thisObj, parent?.thisVariants ?: emptyList())
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun setThisVariants(primary: Obj, extras: List<Obj>) {
|
||||||
|
thisObj = primary
|
||||||
|
val extrasSnapshot = when {
|
||||||
|
extras.isEmpty() -> emptyList()
|
||||||
|
else -> {
|
||||||
|
try {
|
||||||
|
extras.toList()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
thisVariants.clear()
|
||||||
|
thisVariants.add(primary)
|
||||||
|
for (obj in extrasSnapshot) {
|
||||||
|
if (obj !== primary && !thisVariants.contains(obj)) {
|
||||||
|
thisVariants.add(obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun addExtension(cls: ObjClass, name: String, record: ObjRecord) {
|
fun addExtension(cls: ObjClass, name: String, record: ObjRecord) {
|
||||||
extensions.getOrPut(cls) { mutableMapOf() }[name] = record
|
extensions.getOrPut(cls) { mutableMapOf() }[name] = record
|
||||||
}
|
}
|
||||||
@ -77,7 +108,7 @@ open class Scope(
|
|||||||
for (cls in receiverClass.mro) {
|
for (cls in receiverClass.mro) {
|
||||||
s.extensions[cls]?.get(name)?.let { return it }
|
s.extensions[cls]?.get(name)?.let { return it }
|
||||||
}
|
}
|
||||||
if (s is ClosureScope) {
|
if (s is BytecodeClosureScope) {
|
||||||
s.closureScope.findExtension(receiverClass, name)?.let { return it }
|
s.closureScope.findExtension(receiverClass, name)?.let { return it }
|
||||||
}
|
}
|
||||||
s = s.parent
|
s = s.parent
|
||||||
@ -101,7 +132,7 @@ open class Scope(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal lookup helpers that deliberately avoid invoking overridden `get` implementations
|
* Internal lookup helpers that deliberately avoid invoking overridden `get` implementations
|
||||||
* (notably in ClosureScope) to prevent accidental ping-pong and infinite recursion across
|
* (notably in BytecodeClosureScope) to prevent accidental ping-pong and infinite recursion across
|
||||||
* intertwined closure frames. They traverse the plain parent chain and consult only locals
|
* intertwined closure frames. They traverse the plain parent chain and consult only locals
|
||||||
* and bindings of each frame. Instance/class member fallback must be decided by the caller.
|
* and bindings of each frame. Instance/class member fallback must be decided by the caller.
|
||||||
*/
|
*/
|
||||||
@ -124,6 +155,14 @@ open class Scope(
|
|||||||
}
|
}
|
||||||
s.getSlotIndexOf(name)?.let { idx ->
|
s.getSlotIndexOf(name)?.let { idx ->
|
||||||
val rec = s.getSlotRecord(idx)
|
val rec = s.getSlotRecord(idx)
|
||||||
|
val hasDirectBinding =
|
||||||
|
s.objects.containsKey(name) ||
|
||||||
|
s.localBindings.containsKey(name) ||
|
||||||
|
(caller?.let { ctx ->
|
||||||
|
s.objects.containsKey(ctx.mangledName(name)) ||
|
||||||
|
s.localBindings.containsKey(ctx.mangledName(name))
|
||||||
|
} ?: false)
|
||||||
|
if (!hasDirectBinding && rec.value === ObjUnset) return null
|
||||||
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller, name)) return rec
|
if (rec.declaringClass == null || canAccessMember(rec.visibility, rec.declaringClass, caller, name)) return rec
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
@ -136,7 +175,7 @@ open class Scope(
|
|||||||
val effectiveCaller = caller ?: currentClassCtx
|
val effectiveCaller = caller ?: currentClassCtx
|
||||||
while (s != null && hops++ < 1024) {
|
while (s != null && hops++ < 1024) {
|
||||||
tryGetLocalRecord(s, name, effectiveCaller)?.let { return it }
|
tryGetLocalRecord(s, name, effectiveCaller)?.let { return it }
|
||||||
s = if (followClosure && s is ClosureScope) s.closureScope else s.parent
|
s = if (followClosure && s is BytecodeClosureScope) s.closureScope else s.parent
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@ -145,7 +184,7 @@ open class Scope(
|
|||||||
* Perform base Scope.get semantics for this frame without delegating into parent.get
|
* Perform base Scope.get semantics for this frame without delegating into parent.get
|
||||||
* virtual dispatch. This checks:
|
* virtual dispatch. This checks:
|
||||||
* - locals/bindings in this frame
|
* - locals/bindings in this frame
|
||||||
* - walks raw parent chain for locals/bindings (ignoring ClosureScope-specific overrides)
|
* - walks raw parent chain for locals/bindings (ignoring BytecodeClosureScope-specific overrides)
|
||||||
* - finally falls back to this frame's `thisObj` instance/class members
|
* - finally falls back to this frame's `thisObj` instance/class members
|
||||||
*/
|
*/
|
||||||
internal fun baseGetIgnoreClosure(name: String): ObjRecord? {
|
internal fun baseGetIgnoreClosure(name: String): ObjRecord? {
|
||||||
@ -175,7 +214,7 @@ open class Scope(
|
|||||||
* - locals/bindings of each frame
|
* - locals/bindings of each frame
|
||||||
* - then instance/class members of each frame's `thisObj`.
|
* - then instance/class members of each frame's `thisObj`.
|
||||||
* This completely avoids invoking overridden `get` implementations, preventing
|
* This completely avoids invoking overridden `get` implementations, preventing
|
||||||
* ping-pong recursion between `ClosureScope` frames.
|
* ping-pong recursion between `BytecodeClosureScope` frames.
|
||||||
*/
|
*/
|
||||||
internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx, followClosure: Boolean = false): ObjRecord? {
|
internal fun chainLookupWithMembers(name: String, caller: net.sergeych.lyng.obj.ObjClass? = currentClassCtx, followClosure: Boolean = false): ObjRecord? {
|
||||||
var s: Scope? = this
|
var s: Scope? = this
|
||||||
@ -192,7 +231,7 @@ open class Scope(
|
|||||||
} else return rec
|
} else return rec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s = if (followClosure && s is ClosureScope) s.closureScope else s.parent
|
s = if (followClosure && s is BytecodeClosureScope) s.closureScope else s.parent
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@ -326,19 +365,25 @@ open class Scope(
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T : Obj> thisAs(): T {
|
inline fun <reified T : Obj> thisAs(): T {
|
||||||
var s: Scope? = this
|
for (obj in thisVariants) {
|
||||||
while (s != null) {
|
if (obj is T) return obj
|
||||||
val t = s.thisObj
|
|
||||||
if (t is T) return t
|
|
||||||
s = s.parent
|
|
||||||
}
|
}
|
||||||
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
|
raiseClassCastError("Cannot cast ${thisObj.objClass.className} to ${T::class.simpleName}")
|
||||||
}
|
}
|
||||||
|
|
||||||
internal val objects = mutableMapOf<String, ObjRecord>()
|
internal val objects = mutableMapOf<String, ObjRecord>()
|
||||||
|
|
||||||
|
internal fun getLocalRecordDirect(name: String): ObjRecord? = objects[name]
|
||||||
|
|
||||||
open operator fun get(name: String): ObjRecord? {
|
open operator fun get(name: String): ObjRecord? {
|
||||||
if (name == "this") return thisObj.asReadonly
|
if (name == "this") return thisObj.asReadonly
|
||||||
|
if (name == "__PACKAGE__") {
|
||||||
|
var s: Scope? = this
|
||||||
|
while (s != null) {
|
||||||
|
if (s is ModuleScope) return s.packageNameObj
|
||||||
|
s = s.parent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 1. Prefer direct locals/bindings declared in this frame
|
// 1. Prefer direct locals/bindings declared in this frame
|
||||||
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
|
tryGetLocalRecord(this, name, currentClassCtx)?.let { return it }
|
||||||
@ -377,10 +422,20 @@ open class Scope(
|
|||||||
// Slot fast-path API
|
// Slot fast-path API
|
||||||
fun getSlotRecord(index: Int): ObjRecord = slots[index]
|
fun getSlotRecord(index: Int): ObjRecord = slots[index]
|
||||||
fun setSlotValue(index: Int, newValue: Obj) {
|
fun setSlotValue(index: Int, newValue: Obj) {
|
||||||
slots[index].value = newValue
|
val record = slots[index]
|
||||||
|
val value = record.value
|
||||||
|
if (value is FrameSlotRef) {
|
||||||
|
value.write(newValue)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
record.value = newValue
|
||||||
|
}
|
||||||
|
val slotCount: Int
|
||||||
|
get() = slots.size
|
||||||
|
|
||||||
fun getSlotIndexOf(name: String): Int? = nameToSlot[name]
|
fun getSlotIndexOf(name: String): Int? = nameToSlot[name]
|
||||||
|
|
||||||
|
internal fun slotNameToIndexSnapshot(): Map<String, Int> = nameToSlot.toMap()
|
||||||
fun allocateSlotFor(name: String, record: ObjRecord): Int {
|
fun allocateSlotFor(name: String, record: ObjRecord): Int {
|
||||||
val idx = slots.size
|
val idx = slots.size
|
||||||
slots.add(record)
|
slots.add(record)
|
||||||
@ -390,6 +445,12 @@ open class Scope(
|
|||||||
|
|
||||||
fun updateSlotFor(name: String, record: ObjRecord) {
|
fun updateSlotFor(name: String, record: ObjRecord) {
|
||||||
nameToSlot[name]?.let { slots[it] = record }
|
nameToSlot[name]?.let { slots[it] = record }
|
||||||
|
if (objects[name] == null) {
|
||||||
|
objects[name] = record
|
||||||
|
}
|
||||||
|
if (localBindings[name] == null) {
|
||||||
|
localBindings[name] = record
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -410,6 +471,66 @@ open class Scope(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun applySlotPlanReset(plan: Map<String, Int>, records: Map<String, ObjRecord>) {
|
||||||
|
if (plan.isEmpty()) return
|
||||||
|
slots.clear()
|
||||||
|
nameToSlot.clear()
|
||||||
|
val maxIndex = plan.values.maxOrNull() ?: return
|
||||||
|
val targetSize = maxIndex + 1
|
||||||
|
repeat(targetSize) {
|
||||||
|
slots.add(ObjRecord(ObjUnset, isMutable = true))
|
||||||
|
}
|
||||||
|
for ((name, idx) in plan) {
|
||||||
|
nameToSlot[name] = idx
|
||||||
|
val record = records[name]
|
||||||
|
if (record != null && record.value !== ObjUnset) {
|
||||||
|
slots[idx] = record
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun applySlotPlanWithSnapshot(plan: Map<String, Int>): Map<String, Int?> {
|
||||||
|
if (plan.isEmpty()) return emptyMap()
|
||||||
|
val maxIndex = plan.values.maxOrNull() ?: return emptyMap()
|
||||||
|
if (slots.size <= maxIndex) {
|
||||||
|
val targetSize = maxIndex + 1
|
||||||
|
while (slots.size < targetSize) {
|
||||||
|
slots.add(ObjRecord(ObjUnset, isMutable = true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val snapshot = LinkedHashMap<String, Int?>(plan.size)
|
||||||
|
for ((name, idx) in plan) {
|
||||||
|
snapshot[name] = nameToSlot[name]
|
||||||
|
nameToSlot[name] = idx
|
||||||
|
}
|
||||||
|
return snapshot
|
||||||
|
}
|
||||||
|
|
||||||
|
fun restoreSlotPlan(snapshot: Map<String, Int?>) {
|
||||||
|
if (snapshot.isEmpty()) return
|
||||||
|
for ((name, idx) in snapshot) {
|
||||||
|
if (idx == null) {
|
||||||
|
nameToSlot.remove(name)
|
||||||
|
} else {
|
||||||
|
nameToSlot[name] = idx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun hasSlotPlanConflict(plan: Map<String, Int>): Boolean {
|
||||||
|
if (plan.isEmpty() || nameToSlot.isEmpty()) return false
|
||||||
|
val planIndexToNames = HashMap<Int, HashSet<String>>(plan.size)
|
||||||
|
for ((name, idx) in plan) {
|
||||||
|
val names = planIndexToNames.getOrPut(idx) { HashSet(2) }
|
||||||
|
names.add(name)
|
||||||
|
}
|
||||||
|
for ((existingName, existingIndex) in nameToSlot) {
|
||||||
|
val plannedNames = planIndexToNames[existingIndex] ?: continue
|
||||||
|
if (!plannedNames.contains(existingName)) return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all references and maps to prevent memory leaks when pooled.
|
* Clear all references and maps to prevent memory leaks when pooled.
|
||||||
*/
|
*/
|
||||||
@ -417,6 +538,7 @@ open class Scope(
|
|||||||
this.parent = null
|
this.parent = null
|
||||||
this.skipScopeCreation = false
|
this.skipScopeCreation = false
|
||||||
this.currentClassCtx = null
|
this.currentClassCtx = null
|
||||||
|
thisVariants.clear()
|
||||||
objects.clear()
|
objects.clear()
|
||||||
slots.clear()
|
slots.clear()
|
||||||
nameToSlot.clear()
|
nameToSlot.clear()
|
||||||
@ -447,7 +569,7 @@ open class Scope(
|
|||||||
this.parent = parent
|
this.parent = parent
|
||||||
this.args = args
|
this.args = args
|
||||||
this.pos = pos
|
this.pos = pos
|
||||||
this.thisObj = thisObj
|
setThisVariants(thisObj, parent?.thisVariants ?: emptyList())
|
||||||
// Pre-size local slots for upcoming parameter assignment where possible
|
// Pre-size local slots for upcoming parameter assignment where possible
|
||||||
reserveLocalCapacity(args.list.size + 4)
|
reserveLocalCapacity(args.list.size + 4)
|
||||||
}
|
}
|
||||||
@ -513,15 +635,16 @@ open class Scope(
|
|||||||
objects[name]?.let {
|
objects[name]?.let {
|
||||||
if( !it.isMutable )
|
if( !it.isMutable )
|
||||||
raiseIllegalAssignment("symbol is readonly: $name")
|
raiseIllegalAssignment("symbol is readonly: $name")
|
||||||
it.value = value
|
when (val current = it.value) {
|
||||||
|
is FrameSlotRef -> current.write(value)
|
||||||
|
is RecordSlotRef -> current.write(value)
|
||||||
|
else -> it.value = value
|
||||||
|
}
|
||||||
// keep local binding index consistent within the frame
|
// keep local binding index consistent within the frame
|
||||||
localBindings[name] = it
|
localBindings[name] = it
|
||||||
// If we are a ClosureScope, mirror binding into the caller frame to keep it discoverable
|
|
||||||
// across suspension when resumed on the call frame
|
|
||||||
if (this is ClosureScope) {
|
|
||||||
callScope.localBindings[name] = it
|
|
||||||
}
|
|
||||||
bumpClassLayoutIfNeeded(name, value, recordType)
|
bumpClassLayoutIfNeeded(name, value, recordType)
|
||||||
|
updateSlotFor(name, it)
|
||||||
|
syncModuleFrameSlot(name, value)
|
||||||
it
|
it
|
||||||
} ?: addItem(name, true, value, visibility, writeVisibility, recordType, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride)
|
} ?: addItem(name, true, value, visibility, writeVisibility, recordType, isAbstract = isAbstract, isClosed = isClosed, isOverride = isOverride)
|
||||||
|
|
||||||
@ -536,7 +659,11 @@ open class Scope(
|
|||||||
isAbstract: Boolean = false,
|
isAbstract: Boolean = false,
|
||||||
isClosed: Boolean = false,
|
isClosed: Boolean = false,
|
||||||
isOverride: Boolean = false,
|
isOverride: Boolean = false,
|
||||||
isTransient: Boolean = false
|
isTransient: Boolean = false,
|
||||||
|
callSignature: CallSignature? = null,
|
||||||
|
typeDecl: TypeDecl? = null,
|
||||||
|
fieldId: Int? = null,
|
||||||
|
methodId: Int? = null
|
||||||
): ObjRecord {
|
): ObjRecord {
|
||||||
val rec = ObjRecord(
|
val rec = ObjRecord(
|
||||||
value, isMutable, visibility, writeVisibility,
|
value, isMutable, visibility, writeVisibility,
|
||||||
@ -545,15 +672,20 @@ open class Scope(
|
|||||||
isAbstract = isAbstract,
|
isAbstract = isAbstract,
|
||||||
isClosed = isClosed,
|
isClosed = isClosed,
|
||||||
isOverride = isOverride,
|
isOverride = isOverride,
|
||||||
isTransient = isTransient
|
isTransient = isTransient,
|
||||||
|
callSignature = callSignature,
|
||||||
|
typeDecl = typeDecl,
|
||||||
|
memberName = name,
|
||||||
|
fieldId = fieldId,
|
||||||
|
methodId = methodId
|
||||||
)
|
)
|
||||||
objects[name] = rec
|
objects[name] = rec
|
||||||
bumpClassLayoutIfNeeded(name, value, recordType)
|
bumpClassLayoutIfNeeded(name, value, recordType)
|
||||||
if (recordType == ObjRecord.Type.Field || recordType == ObjRecord.Type.ConstructorField) {
|
if (recordType == ObjRecord.Type.Field || recordType == ObjRecord.Type.ConstructorField) {
|
||||||
val inst = thisObj as? net.sergeych.lyng.obj.ObjInstance
|
val inst = thisObj as? net.sergeych.lyng.obj.ObjInstance
|
||||||
if (inst != null) {
|
if (inst != null) {
|
||||||
val slot = inst.objClass.fieldSlotForKey(name)
|
val slotId = rec.fieldId ?: inst.objClass.fieldSlotForKey(name)?.slot
|
||||||
if (slot != null) inst.setFieldSlotRecord(slot.slot, rec)
|
if (slotId != null) inst.setFieldSlotRecord(slotId, rec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (value is Statement ||
|
if (value is Statement ||
|
||||||
@ -562,25 +694,12 @@ open class Scope(
|
|||||||
recordType == ObjRecord.Type.Property) {
|
recordType == ObjRecord.Type.Property) {
|
||||||
val inst = thisObj as? net.sergeych.lyng.obj.ObjInstance
|
val inst = thisObj as? net.sergeych.lyng.obj.ObjInstance
|
||||||
if (inst != null) {
|
if (inst != null) {
|
||||||
val slot = inst.objClass.methodSlotForKey(name)
|
val slotId = rec.methodId ?: inst.objClass.methodSlotForKey(name)?.slot
|
||||||
if (slot != null) inst.setMethodSlotRecord(slot.slot, rec)
|
if (slotId != null) inst.setMethodSlotRecord(slotId, rec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Index this binding within the current frame to help resolve locals across suspension
|
// Index this binding within the current frame to help resolve locals across suspension
|
||||||
localBindings[name] = rec
|
localBindings[name] = rec
|
||||||
// If we are a ClosureScope, mirror binding into the caller frame to keep it discoverable
|
|
||||||
// across suspension when resumed on the call frame
|
|
||||||
if (this is ClosureScope) {
|
|
||||||
callScope.localBindings[name] = rec
|
|
||||||
// Additionally, expose the binding in caller's objects and slot map so identifier
|
|
||||||
// resolution after suspension can still find it even if the active scope is a child
|
|
||||||
// of the callScope (e.g., due to internal withChildFrame usage).
|
|
||||||
// This keeps visibility within the method body but prevents leaking outside the caller frame.
|
|
||||||
callScope.objects[name] = rec
|
|
||||||
if (callScope.getSlotIndexOf(name) == null) {
|
|
||||||
callScope.allocateSlotFor(name, rec)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Map to a slot for fast local access (ensure consistency)
|
// Map to a slot for fast local access (ensure consistency)
|
||||||
if (nameToSlot.isEmpty()) {
|
if (nameToSlot.isEmpty()) {
|
||||||
allocateSlotFor(name, rec)
|
allocateSlotFor(name, rec)
|
||||||
@ -592,6 +711,7 @@ open class Scope(
|
|||||||
slots[idx] = rec
|
slots[idx] = rec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
syncModuleFrameSlot(name, value)
|
||||||
return rec
|
return rec
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -608,41 +728,89 @@ open class Scope(
|
|||||||
return ns.objClass
|
return ns.objClass
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun addVoidFn(vararg names: String, crossinline fn: suspend Scope.() -> Unit) {
|
inline fun addVoidFn(vararg names: String, crossinline fn: suspend ScopeFacade.() -> Unit) {
|
||||||
addFn(*names) {
|
addFn(*names) {
|
||||||
fn(this)
|
fn(this)
|
||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addFn(vararg names: String, fn: suspend Scope.() -> Obj) {
|
fun disassembleSymbol(name: String): String {
|
||||||
val newFn = object : Statement() {
|
val record = get(name) ?: return "$name is not found"
|
||||||
override val pos: Pos = Pos.builtIn
|
val stmt = record.value as? Statement ?: return "$name is not a compiled body"
|
||||||
|
val bytecode = (stmt as? BytecodeStatement)?.bytecodeFunction()
|
||||||
override suspend fun execute(scope: Scope): Obj = scope.fn()
|
?: (stmt as? BytecodeBodyProvider)?.bytecodeBody()?.bytecodeFunction()
|
||||||
|
?: return "$name is not a compiled body"
|
||||||
|
return CmdDisassembler.disassemble(bytecode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addFn(vararg names: String, callSignature: CallSignature? = null, fn: suspend ScopeFacade.() -> Obj) {
|
||||||
|
val newFn = net.sergeych.lyng.obj.ObjExternCallable.fromBridge { fn() }
|
||||||
for (name in names) {
|
for (name in names) {
|
||||||
addItem(
|
addItem(
|
||||||
name,
|
name,
|
||||||
false,
|
false,
|
||||||
newFn
|
newFn,
|
||||||
|
recordType = ObjRecord.Type.Fun,
|
||||||
|
callSignature = callSignature
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- removed doc-aware overloads to keep runtime lean ---
|
// --- removed doc-aware overloads to keep runtime lean ---
|
||||||
|
|
||||||
fun addConst(name: String, value: Obj) = addItem(name, false, value)
|
fun addConst(name: String, value: Obj): ObjRecord {
|
||||||
|
val existing = objects[name]
|
||||||
|
if (existing != null) {
|
||||||
|
when (val current = existing.value) {
|
||||||
|
is FrameSlotRef -> current.write(value)
|
||||||
|
is RecordSlotRef -> current.write(value)
|
||||||
|
else -> existing.value = value
|
||||||
|
}
|
||||||
|
bumpClassLayoutIfNeeded(name, value, existing.type)
|
||||||
|
updateSlotFor(name, existing)
|
||||||
|
syncModuleFrameSlot(name, value)
|
||||||
|
return existing
|
||||||
|
}
|
||||||
|
val slotIndex = getSlotIndexOf(name)
|
||||||
|
if (slotIndex != null) {
|
||||||
|
val record = getSlotRecord(slotIndex)
|
||||||
|
when (val current = record.value) {
|
||||||
|
is FrameSlotRef -> current.write(value)
|
||||||
|
is RecordSlotRef -> current.write(value)
|
||||||
|
else -> record.value = value
|
||||||
|
}
|
||||||
|
bumpClassLayoutIfNeeded(name, value, record.type)
|
||||||
|
updateSlotFor(name, record)
|
||||||
|
syncModuleFrameSlot(name, value)
|
||||||
|
return record
|
||||||
|
}
|
||||||
|
val record = addItem(name, false, value)
|
||||||
|
syncModuleFrameSlot(name, value)
|
||||||
|
return record
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun syncModuleFrameSlot(name: String, value: Obj) {
|
||||||
|
val module = this as? ModuleScope ?: return
|
||||||
|
val frame = module.moduleFrame ?: return
|
||||||
|
val localNames = module.moduleFrameLocalSlotNames
|
||||||
|
if (localNames.isEmpty()) return
|
||||||
|
for (i in localNames.indices) {
|
||||||
|
if (localNames[i] == name) {
|
||||||
|
frame.setObj(i, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
suspend fun eval(code: String): Obj =
|
suspend fun eval(code: String): Obj =
|
||||||
eval(code.toSource())
|
eval(code.toSource())
|
||||||
|
|
||||||
suspend fun eval(source: Source): Obj {
|
suspend fun eval(source: Source): Obj {
|
||||||
return Compiler.compile(
|
return Compiler.compileWithResolution(
|
||||||
source,
|
source,
|
||||||
currentImportProvider
|
currentImportProvider,
|
||||||
|
seedScope = this
|
||||||
).execute(this)
|
).execute(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -695,36 +863,58 @@ open class Scope(
|
|||||||
println("--------------------")
|
println("--------------------")
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun applyClosure(closure: Scope): Scope = ClosureScope(this, closure)
|
open fun applyClosure(closure: Scope, preferredThisType: String? = null): Scope =
|
||||||
|
BytecodeClosureScope(this, closure, preferredThisType)
|
||||||
|
|
||||||
|
internal fun applyClosureForBytecode(closure: Scope, preferredThisType: String? = null): Scope {
|
||||||
|
return BytecodeClosureScope(this, closure, preferredThisType)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve and evaluate a qualified identifier exactly as compiled code would.
|
* Resolve and evaluate a qualified identifier (e.g. `A.B.C`) using the same name/field
|
||||||
* For input like `A.B.C`, it builds the same ObjRef chain the compiler emits:
|
* resolution rules as compiled code, without invoking the compiler or ObjRef evaluation.
|
||||||
* `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 {
|
suspend fun resolveQualifiedIdentifier(qualifiedName: String): Obj {
|
||||||
val trimmed = qualifiedName.trim()
|
val trimmed = qualifiedName.trim()
|
||||||
if (trimmed.isEmpty()) raiseSymbolNotFound("empty identifier")
|
if (trimmed.isEmpty()) raiseSymbolNotFound("empty identifier")
|
||||||
val parts = trimmed.split('.')
|
val parts = trimmed.split('.')
|
||||||
var ref: ObjRef = LocalVarRef(parts[0], Pos.builtIn)
|
val first = parts[0]
|
||||||
for (i in 1 until parts.size) {
|
val base: Obj = if (first == "this") {
|
||||||
ref = FieldRef(ref, parts[i], false)
|
thisObj
|
||||||
|
} else {
|
||||||
|
val rec = get(first) ?: raiseSymbolNotFound(first)
|
||||||
|
resolve(rec, first)
|
||||||
}
|
}
|
||||||
return ref.evalValue(this)
|
var current = base
|
||||||
|
for (i in 1 until parts.size) {
|
||||||
|
val name = parts[i]
|
||||||
|
val rec = current.readField(this, name)
|
||||||
|
current = resolve(rec, name)
|
||||||
|
}
|
||||||
|
return current
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun resolve(rec: ObjRecord, name: String): Obj {
|
suspend fun resolve(rec: ObjRecord, name: String): Obj {
|
||||||
|
val value = rec.value
|
||||||
|
if (value is FrameSlotRef) {
|
||||||
|
return value.read()
|
||||||
|
}
|
||||||
val receiver = rec.receiver ?: thisObj
|
val receiver = rec.receiver ?: thisObj
|
||||||
return receiver.resolveRecord(this, rec, name, rec.declaringClass).value
|
return receiver.resolveRecord(this, rec, name, rec.declaringClass).value
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
|
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) {
|
||||||
|
val value = rec.value
|
||||||
|
if (value is FrameSlotRef) {
|
||||||
|
if (!rec.isMutable && value.read() !== ObjUnset) raiseIllegalAssignment("can't reassign val $name")
|
||||||
|
value.write(newValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
if (rec.type == ObjRecord.Type.Delegated) {
|
if (rec.type == ObjRecord.Type.Delegated) {
|
||||||
val receiver = rec.receiver ?: thisObj
|
val receiver = rec.receiver ?: thisObj
|
||||||
val del = rec.delegate ?: run {
|
val del = rec.delegate ?: run {
|
||||||
if (receiver is ObjInstance) {
|
if (receiver is ObjInstance) {
|
||||||
(receiver as ObjInstance).writeField(this, name, newValue)
|
receiver.writeField(this, name, newValue)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
raiseError("Internal error: delegated property $name has no delegate")
|
raiseError("Internal error: delegated property $name has no delegate")
|
||||||
@ -752,4 +942,10 @@ open class Scope(
|
|||||||
fun new(): Scope =
|
fun new(): Scope =
|
||||||
Script.defaultImportManager.copy().newModuleAt(Pos.builtIn)
|
Script.defaultImportManager.copy().newModuleAt(Pos.builtIn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun requireClass(name: String): net.sergeych.lyng.obj.ObjClass {
|
||||||
|
val rec = get(name) ?: raiseSymbolNotFound(name)
|
||||||
|
return rec.value as? net.sergeych.lyng.obj.ObjClass
|
||||||
|
?: raiseClassCastError("Expected class $name, got ${rec.value.objClass.className}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
135
lynglib/src/commonMain/kotlin/net/sergeych/lyng/ScopeFacade.kt
Normal file
135
lynglib/src/commonMain/kotlin/net/sergeych/lyng/ScopeFacade.kt
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limited facade for Kotlin bridge callables.
|
||||||
|
* Exposes only the minimal API needed to read/write vars and invoke methods.
|
||||||
|
*/
|
||||||
|
interface ScopeFacade {
|
||||||
|
val args: Arguments
|
||||||
|
var pos: Pos
|
||||||
|
var thisObj: Obj
|
||||||
|
operator fun get(name: String): ObjRecord?
|
||||||
|
suspend fun resolve(rec: ObjRecord, name: String): Obj
|
||||||
|
suspend fun assign(rec: ObjRecord, name: String, newValue: Obj)
|
||||||
|
fun raiseError(message: String): Nothing
|
||||||
|
fun raiseError(obj: net.sergeych.lyng.obj.ObjException): Nothing
|
||||||
|
fun raiseClassCastError(message: String): Nothing
|
||||||
|
fun raiseIllegalArgument(message: String): Nothing
|
||||||
|
fun raiseNoSuchElement(message: String = "No such element"): Nothing
|
||||||
|
fun raiseSymbolNotFound(name: String): Nothing
|
||||||
|
fun raiseIllegalState(message: String = "Illegal argument error"): Nothing
|
||||||
|
fun raiseNotImplemented(what: String = "operation"): Nothing
|
||||||
|
suspend fun call(callee: Obj, args: Arguments = Arguments.EMPTY, newThisObj: Obj? = null): Obj
|
||||||
|
suspend fun toStringOf(obj: Obj, forInspect: Boolean = false): ObjString
|
||||||
|
suspend fun inspect(obj: Obj): String
|
||||||
|
fun trace(text: String = "")
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class ScopeBridge(internal val scope: Scope) : ScopeFacade {
|
||||||
|
override val args: Arguments
|
||||||
|
get() = scope.args
|
||||||
|
override var pos: Pos
|
||||||
|
get() = scope.pos
|
||||||
|
set(value) { scope.pos = value }
|
||||||
|
override var thisObj: Obj
|
||||||
|
get() = scope.thisObj
|
||||||
|
set(value) { scope.thisObj = value }
|
||||||
|
override fun get(name: String): ObjRecord? = scope[name]
|
||||||
|
override suspend fun resolve(rec: ObjRecord, name: String): Obj = scope.resolve(rec, name)
|
||||||
|
override suspend fun assign(rec: ObjRecord, name: String, newValue: Obj) = scope.assign(rec, name, newValue)
|
||||||
|
override fun raiseError(message: String): Nothing = scope.raiseError(message)
|
||||||
|
override fun raiseError(obj: net.sergeych.lyng.obj.ObjException): Nothing = scope.raiseError(obj)
|
||||||
|
override fun raiseClassCastError(message: String): Nothing = scope.raiseClassCastError(message)
|
||||||
|
override fun raiseIllegalArgument(message: String): Nothing = scope.raiseIllegalArgument(message)
|
||||||
|
override fun raiseNoSuchElement(message: String): Nothing = scope.raiseNoSuchElement(message)
|
||||||
|
override fun raiseSymbolNotFound(name: String): Nothing = scope.raiseSymbolNotFound(name)
|
||||||
|
override fun raiseIllegalState(message: String): Nothing = scope.raiseIllegalState(message)
|
||||||
|
override fun raiseNotImplemented(what: String): Nothing = scope.raiseNotImplemented(what)
|
||||||
|
override suspend fun call(callee: Obj, args: Arguments, newThisObj: Obj?): Obj {
|
||||||
|
return callee.callOn(scope.createChildScope(scope.pos, args = args, newThisObj = newThisObj))
|
||||||
|
}
|
||||||
|
override suspend fun toStringOf(obj: Obj, forInspect: Boolean): ObjString = obj.toString(scope, forInspect)
|
||||||
|
override suspend fun inspect(obj: Obj): String = obj.inspect(scope)
|
||||||
|
override fun trace(text: String) = scope.trace(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Public factory for bridge facades. */
|
||||||
|
fun Scope.asFacade(): ScopeFacade = ScopeBridge(this)
|
||||||
|
|
||||||
|
inline fun <reified T : Obj> ScopeFacade.requiredArg(index: Int): T {
|
||||||
|
if (args.list.size <= index) raiseError("Expected at least ${index + 1} argument, got ${args.list.size}")
|
||||||
|
return (args.list[index].byValueCopy() as? T)
|
||||||
|
?: raiseClassCastError("Expected type ${T::class.simpleName}, got ${args.list[index]::class.simpleName}")
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T : Obj> ScopeFacade.requireOnlyArg(): T {
|
||||||
|
if (args.list.size != 1) raiseError("Expected exactly 1 argument, got ${args.list.size}")
|
||||||
|
return requiredArg(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ScopeFacade.requireExactCount(count: Int) {
|
||||||
|
if (args.list.size != count) {
|
||||||
|
raiseError("Expected exactly $count arguments, got ${args.list.size}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ScopeFacade.requireNoArgs() {
|
||||||
|
if (args.list.isNotEmpty()) {
|
||||||
|
raiseError("This function does not accept any arguments")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T : Obj> ScopeFacade.thisAs(): T {
|
||||||
|
val obj = thisObj
|
||||||
|
return (obj as? T) ?: raiseClassCastError(
|
||||||
|
"Cannot cast ${obj.objClass.className} to ${T::class.simpleName}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ScopeFacade.requireScope(): Scope =
|
||||||
|
(this as? ScopeBridge)?.scope ?: raiseIllegalState("ScopeFacade requires ScopeBridge")
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseNPE(): Nothing = requireScope().raiseNPE()
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseIndexOutOfBounds(message: String = "Index out of bounds"): Nothing =
|
||||||
|
requireScope().raiseIndexOutOfBounds(message)
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseIllegalAssignment(message: String): Nothing =
|
||||||
|
requireScope().raiseIllegalAssignment(message)
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseUnset(message: String = "property is unset (not initialized)"): Nothing =
|
||||||
|
requireScope().raiseUnset(message)
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseNotFound(message: String = "not found"): Nothing =
|
||||||
|
requireScope().raiseNotFound(message)
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseError(obj: Obj, pos: Pos = this.pos, message: String): Nothing =
|
||||||
|
requireScope().raiseError(obj, pos, message)
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseAssertionFailed(message: String): Nothing =
|
||||||
|
raiseError(ObjAssertionFailedException(requireScope(), message))
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseIllegalOperation(message: String = "Operation is illegal"): Nothing =
|
||||||
|
raiseError(ObjIllegalOperationException(requireScope(), message))
|
||||||
|
|
||||||
|
fun ScopeFacade.raiseIterationFinished(): Nothing =
|
||||||
|
raiseError(ObjIterationFinishedException(requireScope()))
|
||||||
@ -20,6 +20,8 @@ package net.sergeych.lyng
|
|||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.yield
|
import kotlinx.coroutines.yield
|
||||||
import net.sergeych.lyng.Script.Companion.defaultImportManager
|
import net.sergeych.lyng.Script.Companion.defaultImportManager
|
||||||
|
import net.sergeych.lyng.bytecode.CmdFunction
|
||||||
|
import net.sergeych.lyng.bytecode.CmdVm
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.miniast.*
|
||||||
import net.sergeych.lyng.obj.*
|
import net.sergeych.lyng.obj.*
|
||||||
import net.sergeych.lyng.pacman.ImportManager
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
@ -32,16 +34,174 @@ import kotlin.math.*
|
|||||||
class Script(
|
class Script(
|
||||||
override val pos: Pos,
|
override val pos: Pos,
|
||||||
private val statements: List<Statement> = emptyList(),
|
private val statements: List<Statement> = emptyList(),
|
||||||
|
private val moduleSlotPlan: Map<String, Int> = emptyMap(),
|
||||||
|
private val moduleDeclaredNames: Set<String> = emptySet(),
|
||||||
|
private val importBindings: Map<String, ImportBinding> = emptyMap(),
|
||||||
|
private val importedModules: List<ImportBindingSource.Module> = emptyList(),
|
||||||
|
private val moduleBytecode: CmdFunction? = null,
|
||||||
// private val catchReturn: Boolean = false,
|
// private val catchReturn: Boolean = false,
|
||||||
) : Statement() {
|
) : Statement() {
|
||||||
|
fun statements(): List<Statement> = statements
|
||||||
override suspend fun execute(scope: Scope): Obj {
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
var lastResult: Obj = ObjVoid
|
scope.pos = pos
|
||||||
for (s in statements) {
|
val execScope = resolveModuleScope(scope) ?: scope
|
||||||
lastResult = s.execute(scope)
|
val isModuleScope = execScope is ModuleScope
|
||||||
|
val shouldSeedModule = isModuleScope || execScope.thisObj === ObjVoid
|
||||||
|
val moduleTarget = (execScope as? ModuleScope) ?: execScope.parent as? ModuleScope ?: execScope
|
||||||
|
if (shouldSeedModule) {
|
||||||
|
seedModuleSlots(moduleTarget, scope)
|
||||||
}
|
}
|
||||||
return lastResult
|
moduleBytecode?.let { fn ->
|
||||||
|
if (execScope is ModuleScope) {
|
||||||
|
execScope.ensureModuleFrame(fn)
|
||||||
}
|
}
|
||||||
|
var execFrame: net.sergeych.lyng.bytecode.CmdFrame? = null
|
||||||
|
val result = CmdVm().execute(fn, execScope, scope.args) { frame, _ ->
|
||||||
|
execFrame = frame
|
||||||
|
seedModuleLocals(frame, moduleTarget, scope)
|
||||||
|
}
|
||||||
|
if (execScope !is ModuleScope) {
|
||||||
|
execFrame?.let { syncFrameLocalsToScope(it, execScope) }
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
if (statements.isNotEmpty()) {
|
||||||
|
scope.raiseIllegalState("bytecode-only execution is required; missing module bytecode")
|
||||||
|
}
|
||||||
|
return ObjVoid
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun seedModuleSlots(scope: Scope, seedScope: Scope) {
|
||||||
|
if (importBindings.isEmpty() && importedModules.isEmpty()) return
|
||||||
|
seedImportBindings(scope, seedScope)
|
||||||
|
if (moduleSlotPlan.isNotEmpty()) {
|
||||||
|
scope.applySlotPlan(moduleSlotPlan)
|
||||||
|
for (name in moduleSlotPlan.keys) {
|
||||||
|
val record = scope.objects[name] ?: scope.localBindings[name] ?: continue
|
||||||
|
scope.updateSlotFor(name, record)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun seedModuleLocals(
|
||||||
|
frame: net.sergeych.lyng.bytecode.CmdFrame,
|
||||||
|
scope: Scope,
|
||||||
|
seedScope: Scope
|
||||||
|
) {
|
||||||
|
val localNames = frame.fn.localSlotNames
|
||||||
|
if (localNames.isEmpty()) return
|
||||||
|
val values = HashMap<String, Obj>()
|
||||||
|
val base = frame.fn.scopeSlotCount
|
||||||
|
for (i in localNames.indices) {
|
||||||
|
val name = localNames[i] ?: continue
|
||||||
|
if (moduleDeclaredNames.contains(name)) continue
|
||||||
|
val record = seedScope.getLocalRecordDirect(name) ?: findSeedRecord(seedScope, name) ?: continue
|
||||||
|
val value = if (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property) {
|
||||||
|
scope.resolve(record, name)
|
||||||
|
} else {
|
||||||
|
val raw = record.value
|
||||||
|
when (raw) {
|
||||||
|
is FrameSlotRef -> {
|
||||||
|
if (raw.refersTo(frame.frame, i)) {
|
||||||
|
raw.peekValue() ?: continue
|
||||||
|
} else if (seedScope !is ModuleScope) {
|
||||||
|
raw
|
||||||
|
} else {
|
||||||
|
raw.read()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is RecordSlotRef -> {
|
||||||
|
if (seedScope !is ModuleScope) raw else raw.read()
|
||||||
|
}
|
||||||
|
else -> raw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
frame.setObjUnchecked(base + i, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun syncFrameLocalsToScope(frame: net.sergeych.lyng.bytecode.CmdFrame, scope: Scope) {
|
||||||
|
val localNames = frame.fn.localSlotNames
|
||||||
|
if (localNames.isEmpty()) return
|
||||||
|
for (i in localNames.indices) {
|
||||||
|
val name = localNames[i] ?: continue
|
||||||
|
val record = scope.getLocalRecordDirect(name) ?: scope.localBindings[name] ?: scope.objects[name] ?: continue
|
||||||
|
val value = frame.readLocalObj(i)
|
||||||
|
when (val current = record.value) {
|
||||||
|
is FrameSlotRef -> current.write(value)
|
||||||
|
is RecordSlotRef -> current.write(value)
|
||||||
|
else -> record.value = value
|
||||||
|
}
|
||||||
|
scope.updateSlotFor(name, record)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun seedImportBindings(scope: Scope, seedScope: Scope) {
|
||||||
|
val provider = scope.currentImportProvider
|
||||||
|
val importedModules = LinkedHashSet<ModuleScope>()
|
||||||
|
for (moduleRef in this.importedModules) {
|
||||||
|
importedModules.add(provider.prepareImport(moduleRef.pos, moduleRef.name, null))
|
||||||
|
}
|
||||||
|
if (scope is ModuleScope) {
|
||||||
|
scope.importedModules = importedModules.toList()
|
||||||
|
}
|
||||||
|
for (module in importedModules) {
|
||||||
|
module.importInto(scope, null)
|
||||||
|
}
|
||||||
|
for ((name, binding) in importBindings) {
|
||||||
|
val record = when (val source = binding.source) {
|
||||||
|
is ImportBindingSource.Module -> {
|
||||||
|
val module = provider.prepareImport(source.pos, source.name, null)
|
||||||
|
importedModules.add(module)
|
||||||
|
module.objects[binding.symbol]?.takeIf { it.visibility.isPublic }
|
||||||
|
?: scope.raiseSymbolNotFound("symbol ${source.name}.${binding.symbol} not found")
|
||||||
|
}
|
||||||
|
ImportBindingSource.Root -> {
|
||||||
|
provider.rootScope.objects[binding.symbol]?.takeIf { it.visibility.isPublic }
|
||||||
|
?: scope.raiseSymbolNotFound("symbol ${binding.symbol} not found")
|
||||||
|
}
|
||||||
|
ImportBindingSource.Seed -> {
|
||||||
|
findSeedRecord(seedScope, binding.symbol)
|
||||||
|
?: scope.raiseSymbolNotFound("symbol ${binding.symbol} not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (name == "Exception" && record.value !is ObjClass) {
|
||||||
|
scope.updateSlotFor(name, ObjRecord(ObjException.Root, isMutable = false))
|
||||||
|
} else {
|
||||||
|
scope.updateSlotFor(name, record)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (module in importedModules) {
|
||||||
|
for ((cls, map) in module.extensions) {
|
||||||
|
for ((symbol, record) in map) {
|
||||||
|
if (record.visibility.isPublic) {
|
||||||
|
scope.addExtension(cls, symbol, record)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findSeedRecord(scope: Scope?, name: String): ObjRecord? {
|
||||||
|
var s = scope
|
||||||
|
var hops = 0
|
||||||
|
while (s != null && hops++ < 1024) {
|
||||||
|
s.objects[name]?.let { return it }
|
||||||
|
s.localBindings[name]?.let { return it }
|
||||||
|
s.getSlotIndexOf(name)?.let { idx ->
|
||||||
|
val rec = s.getSlotRecord(idx)
|
||||||
|
if (rec.value !== ObjUnset) return rec
|
||||||
|
}
|
||||||
|
s = s.parent
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resolveModuleScope(scope: Scope): ModuleScope? {
|
||||||
|
return scope as? ModuleScope
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun debugStatements(): List<Statement> = statements
|
||||||
|
|
||||||
suspend fun execute() = execute(
|
suspend fun execute() = execute(
|
||||||
defaultImportManager.newStdScope()
|
defaultImportManager.newStdScope()
|
||||||
@ -50,8 +210,8 @@ class Script(
|
|||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new scope using standard safe set of modules, using [defaultImportManager]. It is
|
* Create new scope using a standard safe set of modules, using [defaultImportManager]. It is
|
||||||
* suspended as first time calls requires compilation of standard library or other
|
* suspended as first time invocation requires compilation of standard library or other
|
||||||
* asynchronous initialization.
|
* asynchronous initialization.
|
||||||
*/
|
*/
|
||||||
suspend fun newScope(pos: Pos = Pos.builtIn) = defaultImportManager.newStdScope(pos)
|
suspend fun newScope(pos: Pos = Pos.builtIn) = defaultImportManager.newStdScope(pos)
|
||||||
@ -61,19 +221,29 @@ class Script(
|
|||||||
addConst("Unset", ObjUnset)
|
addConst("Unset", ObjUnset)
|
||||||
addFn("print") {
|
addFn("print") {
|
||||||
for ((i, a) in args.withIndex()) {
|
for ((i, a) in args.withIndex()) {
|
||||||
if (i > 0) print(' ' + a.toString(this).value)
|
if (i > 0) print(' ' + toStringOf(a).value)
|
||||||
else print(a.toString(this).value)
|
else print(toStringOf(a).value)
|
||||||
}
|
}
|
||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
addFn("println") {
|
addFn("println") {
|
||||||
for ((i, a) in args.withIndex()) {
|
for ((i, a) in args.withIndex()) {
|
||||||
if (i > 0) print(' ' + a.toString(this).value)
|
if (i > 0) print(' ' + toStringOf(a).value)
|
||||||
else print(a.toString(this).value)
|
else print(toStringOf(a).value)
|
||||||
}
|
}
|
||||||
println()
|
println()
|
||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
|
addFn("call") {
|
||||||
|
val callee = args.list.firstOrNull()
|
||||||
|
?: raiseError("call requires a callable as the first argument")
|
||||||
|
val rest = if (args.list.size > 1) {
|
||||||
|
Arguments(args.list.drop(1))
|
||||||
|
} else {
|
||||||
|
Arguments.EMPTY
|
||||||
|
}
|
||||||
|
call(callee, rest)
|
||||||
|
}
|
||||||
addFn("floor") {
|
addFn("floor") {
|
||||||
val x = args.firstAndOnly()
|
val x = args.firstAndOnly()
|
||||||
(if (x is ObjInt) x
|
(if (x is ObjInt) x
|
||||||
@ -170,12 +340,12 @@ class Script(
|
|||||||
|
|
||||||
var result = value
|
var result = value
|
||||||
if (range.start != null && !range.start.isNull) {
|
if (range.start != null && !range.start.isNull) {
|
||||||
if (result.compareTo(this, range.start) < 0) {
|
if (result.compareTo(requireScope(), range.start) < 0) {
|
||||||
result = range.start
|
result = range.start
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (range.end != null && !range.end.isNull) {
|
if (range.end != null && !range.end.isNull) {
|
||||||
val cmp = range.end.compareTo(this, result)
|
val cmp = range.end.compareTo(requireScope(), result)
|
||||||
if (range.isEndInclusive) {
|
if (range.isEndInclusive) {
|
||||||
if (cmp < 0) result = range.end
|
if (cmp < 0) result = range.end
|
||||||
} else {
|
} else {
|
||||||
@ -198,45 +368,40 @@ class Script(
|
|||||||
addVoidFn("assert") {
|
addVoidFn("assert") {
|
||||||
val cond = requiredArg<ObjBool>(0)
|
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
|
": " + toStringOf(call(args[1])).value
|
||||||
else ""
|
else ""
|
||||||
if (!cond.value == true)
|
if (!cond.value == true)
|
||||||
raiseError(ObjAssertionFailedException(this, "Assertion failed$message"))
|
raiseAssertionFailed("Assertion failed$message")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unwrapCompareArg(value: Obj): Obj {
|
||||||
|
val resolved = when (value) {
|
||||||
|
is FrameSlotRef -> value.read()
|
||||||
|
is RecordSlotRef -> value.read()
|
||||||
|
else -> value
|
||||||
|
}
|
||||||
|
return if (resolved === ObjUnset.objClass) ObjUnset else resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
addVoidFn("assertEquals") {
|
addVoidFn("assertEquals") {
|
||||||
val a = requiredArg<Obj>(0)
|
val a = unwrapCompareArg(requiredArg(0))
|
||||||
val b = requiredArg<Obj>(1)
|
val b = unwrapCompareArg(requiredArg(1))
|
||||||
if (a.compareTo(this, b) != 0)
|
if (a.compareTo(requireScope(), b) != 0) {
|
||||||
raiseError(
|
raiseAssertionFailed("Assertion failed: ${inspect(a)} == ${inspect(b)}")
|
||||||
ObjAssertionFailedException(
|
}
|
||||||
this,
|
|
||||||
"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
// alias used in tests
|
// alias used in tests
|
||||||
addVoidFn("assertEqual") {
|
addVoidFn("assertEqual") {
|
||||||
val a = requiredArg<Obj>(0)
|
val a = unwrapCompareArg(requiredArg(0))
|
||||||
val b = requiredArg<Obj>(1)
|
val b = unwrapCompareArg(requiredArg(1))
|
||||||
if (a.compareTo(this, b) != 0)
|
if (a.compareTo(requireScope(), b) != 0)
|
||||||
raiseError(
|
raiseAssertionFailed("Assertion failed: ${inspect(a)} == ${inspect(b)}")
|
||||||
ObjAssertionFailedException(
|
|
||||||
this,
|
|
||||||
"Assertion failed: ${a.inspect(this)} == ${b.inspect(this)}"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
addVoidFn("assertNotEquals") {
|
addVoidFn("assertNotEquals") {
|
||||||
val a = requiredArg<Obj>(0)
|
val a = unwrapCompareArg(requiredArg(0))
|
||||||
val b = requiredArg<Obj>(1)
|
val b = unwrapCompareArg(requiredArg(1))
|
||||||
if (a.compareTo(this, b) == 0)
|
if (a.compareTo(requireScope(), b) == 0)
|
||||||
raiseError(
|
raiseAssertionFailed("Assertion failed: ${inspect(a)} != ${inspect(b)}")
|
||||||
ObjAssertionFailedException(
|
|
||||||
this,
|
|
||||||
"Assertion failed: ${a.inspect(this)} != ${b.inspect(this)}"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
addFnDoc(
|
addFnDoc(
|
||||||
"assertThrows",
|
"assertThrows",
|
||||||
@ -251,35 +416,30 @@ class Script(
|
|||||||
will be accepted.
|
will be accepted.
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
) {
|
) {
|
||||||
val code: Statement
|
val code: Obj
|
||||||
val expectedClass: ObjClass?
|
val expectedClass: ObjClass?
|
||||||
when (args.size) {
|
when (args.size) {
|
||||||
1 -> {
|
1 -> {
|
||||||
code = requiredArg<Statement>(0)
|
code = requiredArg<Obj>(0)
|
||||||
expectedClass = null
|
expectedClass = null
|
||||||
}
|
}
|
||||||
|
|
||||||
2 -> {
|
2 -> {
|
||||||
code = requiredArg<Statement>(1)
|
code = requiredArg<Obj>(1)
|
||||||
expectedClass = requiredArg<ObjClass>(0)
|
expectedClass = requiredArg<ObjClass>(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> raiseIllegalArgument("Expected 1 or 2 arguments, got ${args.size}")
|
else -> raiseIllegalArgument("Expected 1 or 2 arguments, got ${args.size}")
|
||||||
}
|
}
|
||||||
val result = try {
|
val result = try {
|
||||||
code.execute(this)
|
call(code)
|
||||||
null
|
null
|
||||||
} catch (e: ExecutionError) {
|
} catch (e: ExecutionError) {
|
||||||
e.errorObject
|
e.errorObject
|
||||||
} catch (_: ScriptError) {
|
} catch (_: ScriptError) {
|
||||||
ObjNull
|
ObjNull
|
||||||
}
|
}
|
||||||
if (result == null) raiseError(
|
if (result == null) raiseAssertionFailed("Expected exception but nothing was thrown")
|
||||||
ObjAssertionFailedException(
|
|
||||||
this,
|
|
||||||
"Expected exception but nothing was thrown"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
expectedClass?.let {
|
expectedClass?.let {
|
||||||
if (!result.isInstanceOf(it)) {
|
if (!result.isInstanceOf(it)) {
|
||||||
val actual = if (result is ObjException) result.exceptionClass else result.objClass
|
val actual = if (result is ObjException) result.exceptionClass else result.objClass
|
||||||
@ -289,8 +449,8 @@ class Script(
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
addFn("dynamic") {
|
addFn("dynamic", callSignature = CallSignature(tailBlockReceiverType = "DelegateContext")) {
|
||||||
ObjDynamic.create(this, requireOnlyArg())
|
ObjDynamic.create(requireScope(), requireOnlyArg())
|
||||||
}
|
}
|
||||||
|
|
||||||
val root = this
|
val root = this
|
||||||
@ -307,7 +467,7 @@ class Script(
|
|||||||
val condition = requiredArg<ObjBool>(0)
|
val condition = requiredArg<ObjBool>(0)
|
||||||
if (!condition.value) {
|
if (!condition.value) {
|
||||||
var message = args.list.getOrNull(1)
|
var message = args.list.getOrNull(1)
|
||||||
if (message is Statement) message = message.execute(this)
|
if (message is Obj && message.objClass == Statement.type) message = call(message)
|
||||||
raiseIllegalArgument(message?.toString() ?: "requirement not met")
|
raiseIllegalArgument(message?.toString() ?: "requirement not met")
|
||||||
}
|
}
|
||||||
ObjVoid
|
ObjVoid
|
||||||
@ -316,23 +476,42 @@ class Script(
|
|||||||
val condition = requiredArg<ObjBool>(0)
|
val condition = requiredArg<ObjBool>(0)
|
||||||
if (!condition.value) {
|
if (!condition.value) {
|
||||||
var message = args.list.getOrNull(1)
|
var message = args.list.getOrNull(1)
|
||||||
if (message is Statement) message = message.execute(this)
|
if (message is Obj && message.objClass == Statement.type) message = call(message)
|
||||||
raiseIllegalState(message?.toString() ?: "check failed")
|
raiseIllegalState(message?.toString() ?: "check failed")
|
||||||
}
|
}
|
||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
addFn("traceScope") {
|
addFn("traceScope") {
|
||||||
this.trace(args.getOrNull(0)?.toString() ?: "")
|
trace(args.getOrNull(0)?.toString() ?: "")
|
||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
|
addFn("run") {
|
||||||
|
call(requireOnlyArg())
|
||||||
|
}
|
||||||
|
addFn("cached") {
|
||||||
|
val builder = requireOnlyArg<Obj>()
|
||||||
|
val capturedScope = this
|
||||||
|
var calculated = false
|
||||||
|
var cachedValue: Obj = ObjVoid
|
||||||
|
net.sergeych.lyng.obj.ObjExternCallable.fromBridge {
|
||||||
|
if (!calculated) {
|
||||||
|
cachedValue = capturedScope.call(builder)
|
||||||
|
calculated = true
|
||||||
|
}
|
||||||
|
cachedValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addFn("lazy") {
|
||||||
|
val builder = requireOnlyArg<Obj>()
|
||||||
|
ObjLazyDelegate(builder, requireScope())
|
||||||
|
}
|
||||||
addVoidFn("delay") {
|
addVoidFn("delay") {
|
||||||
val a = args.firstAndOnly()
|
val a = args.firstAndOnly()
|
||||||
when (a) {
|
when (a) {
|
||||||
is ObjInt -> delay(a.value)
|
is ObjInt -> delay(a.value)
|
||||||
is ObjReal -> delay((a.value * 1000).roundToLong())
|
is ObjReal -> delay((a.value * 1000).roundToLong())
|
||||||
is ObjDuration -> delay(a.duration)
|
is ObjDuration -> delay(a.duration)
|
||||||
else -> raiseIllegalArgument("Expected Int, Real or Duration, got ${a.inspect(this)}")
|
else -> raiseIllegalArgument("Expected Int, Real or Duration, got ${inspect(a)}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -352,6 +531,7 @@ class Script(
|
|||||||
// interfaces
|
// interfaces
|
||||||
addConst("Iterable", ObjIterable)
|
addConst("Iterable", ObjIterable)
|
||||||
addConst("Collection", ObjCollection)
|
addConst("Collection", ObjCollection)
|
||||||
|
addConst("Iterator", ObjIterator)
|
||||||
addConst("Array", ObjArray)
|
addConst("Array", ObjArray)
|
||||||
addConst("RingBuffer", ObjRingBuffer.type)
|
addConst("RingBuffer", ObjRingBuffer.type)
|
||||||
addConst("Class", ObjClassType)
|
addConst("Class", ObjClassType)
|
||||||
@ -360,13 +540,19 @@ class Script(
|
|||||||
addConst("CompletableDeferred", ObjCompletableDeferred.type)
|
addConst("CompletableDeferred", ObjCompletableDeferred.type)
|
||||||
addConst("Mutex", ObjMutex.type)
|
addConst("Mutex", ObjMutex.type)
|
||||||
addConst("Flow", ObjFlow.type)
|
addConst("Flow", ObjFlow.type)
|
||||||
|
addConst("FlowBuilder", ObjFlowBuilder.type)
|
||||||
|
addConst("Delegate", ObjDynamic.type)
|
||||||
|
addConst("DelegateContext", ObjDynamicContext.type)
|
||||||
|
|
||||||
addConst("Regex", ObjRegex.type)
|
addConst("Regex", ObjRegex.type)
|
||||||
|
addConst("RegexMatch", ObjRegexMatch.type)
|
||||||
|
addConst("MapEntry", ObjMapEntry.type)
|
||||||
|
|
||||||
addFn("launch") {
|
addFn("launch") {
|
||||||
val callable = requireOnlyArg<Statement>()
|
val callable = requireOnlyArg<Obj>()
|
||||||
|
val captured = this
|
||||||
ObjDeferred(globalDefer {
|
ObjDeferred(globalDefer {
|
||||||
callable.execute(this@addFn)
|
captured.call(callable)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -375,10 +561,10 @@ class Script(
|
|||||||
ObjVoid
|
ObjVoid
|
||||||
}
|
}
|
||||||
|
|
||||||
addFn("flow") {
|
addFn("flow", callSignature = CallSignature(tailBlockReceiverType = "FlowBuilder")) {
|
||||||
// important is: current context contains closure often used in call;
|
// important is: current context contains closure often used in call;
|
||||||
// we'll need it for the producer
|
// we'll need it for the producer
|
||||||
ObjFlow(requireOnlyArg<Statement>(), this)
|
ObjFlow(requireOnlyArg<Obj>(), requireScope())
|
||||||
}
|
}
|
||||||
|
|
||||||
val pi = ObjReal(PI)
|
val pi = ObjReal(PI)
|
||||||
@ -400,9 +586,10 @@ class Script(
|
|||||||
|
|
||||||
val defaultImportManager: ImportManager by lazy {
|
val defaultImportManager: ImportManager by lazy {
|
||||||
ImportManager(rootScope, SecurityManager.allowAll).apply {
|
ImportManager(rootScope, SecurityManager.allowAll).apply {
|
||||||
addTextPackages(
|
addPackage("lyng.stdlib") { module ->
|
||||||
rootLyng
|
module.eval(Source("lyng.stdlib", rootLyng))
|
||||||
)
|
ObjKotlinIterator.bindTo(module.requireClass("KotlinIterator"))
|
||||||
|
}
|
||||||
addPackage("lyng.buffer") {
|
addPackage("lyng.buffer") {
|
||||||
it.addConstDoc(
|
it.addConstDoc(
|
||||||
name = "Buffer",
|
name = "Buffer",
|
||||||
@ -453,7 +640,7 @@ class Script(
|
|||||||
is ObjInt -> delay(a.value * 1000)
|
is ObjInt -> delay(a.value * 1000)
|
||||||
is ObjReal -> delay((a.value * 1000).roundToLong())
|
is ObjReal -> delay((a.value * 1000).roundToLong())
|
||||||
is ObjDuration -> delay(a.duration)
|
is ObjDuration -> delay(a.duration)
|
||||||
else -> raiseIllegalArgument("Expected Duration, Int or Real, got ${a.inspect(this)}")
|
else -> raiseIllegalArgument("Expected Duration, Int or Real, got ${inspect(a)}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,7 +36,7 @@ data class Token(val value: String, val pos: Pos, val type: Type) {
|
|||||||
PLUS, MINUS, STAR, SLASH, PERCENT,
|
PLUS, MINUS, STAR, SLASH, PERCENT,
|
||||||
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN,
|
ASSIGN, PLUSASSIGN, MINUSASSIGN, STARASSIGN, SLASHASSIGN, PERCENTASSIGN, IFNULLASSIGN,
|
||||||
PLUS2, MINUS2,
|
PLUS2, MINUS2,
|
||||||
IN, NOTIN, IS, NOTIS, BY,
|
IN, NOTIN, IS, NOTIS, BY, STEP,
|
||||||
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
|
EQ, NEQ, LT, LTE, GT, GTE, REF_EQ, REF_NEQ, MATCH, NOTMATCH,
|
||||||
SHUTTLE,
|
SHUTTLE,
|
||||||
AND, BITAND, OR, BITOR, BITXOR, NOT, BITNOT, DOT, ARROW, EQARROW, QUESTION, COLONCOLON,
|
AND, BITAND, OR, BITOR, BITXOR, NOT, BITNOT, DOT, ARROW, EQARROW, QUESTION, COLONCOLON,
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
import net.sergeych.lyng.obj.ObjException
|
||||||
|
|
||||||
|
class TryStatement(
|
||||||
|
val body: Statement,
|
||||||
|
val catches: List<CatchBlock>,
|
||||||
|
val finallyClause: Statement?,
|
||||||
|
private val startPos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
data class CatchBlock(
|
||||||
|
val catchVarName: String,
|
||||||
|
val catchVarPos: Pos,
|
||||||
|
val classNames: List<String>,
|
||||||
|
val block: Statement
|
||||||
|
)
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "try statement")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resolveExceptionClass(scope: Scope, name: String): ObjClass {
|
||||||
|
val rec = scope[name]
|
||||||
|
val cls = rec?.value as? ObjClass
|
||||||
|
if (cls != null) return cls
|
||||||
|
if (name == "Exception") return ObjException.Root
|
||||||
|
scope.raiseSymbolNotFound("error class does not exist or is not a class: $name")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -22,8 +22,27 @@ package net.sergeych.lyng
|
|||||||
// this is highly experimental and subject to complete redesign
|
// this is highly experimental and subject to complete redesign
|
||||||
// very soon
|
// very soon
|
||||||
sealed class TypeDecl(val isNullable:Boolean = false) {
|
sealed class TypeDecl(val isNullable:Boolean = false) {
|
||||||
|
enum class Variance { In, Out, Invariant }
|
||||||
// ??
|
// ??
|
||||||
// data class Fn(val argTypes: List<ArgsDeclaration.Item>, val retType: TypeDecl) : TypeDecl()
|
data class Function(
|
||||||
|
val receiver: TypeDecl?,
|
||||||
|
val params: List<TypeDecl>,
|
||||||
|
val returnType: TypeDecl,
|
||||||
|
val nullable: Boolean = false
|
||||||
|
) : TypeDecl(nullable)
|
||||||
|
data class Ellipsis(
|
||||||
|
val elementType: TypeDecl,
|
||||||
|
val nullable: Boolean = false
|
||||||
|
) : TypeDecl(nullable)
|
||||||
|
data class TypeVar(val name: String, val nullable: Boolean = false) : TypeDecl(nullable)
|
||||||
|
data class Union(val options: List<TypeDecl>, val nullable: Boolean = false) : TypeDecl(nullable)
|
||||||
|
data class Intersection(val options: List<TypeDecl>, val nullable: Boolean = false) : TypeDecl(nullable)
|
||||||
|
data class TypeParam(
|
||||||
|
val name: String,
|
||||||
|
val variance: Variance = Variance.Invariant,
|
||||||
|
val bound: TypeDecl? = null,
|
||||||
|
val defaultType: TypeDecl? = null
|
||||||
|
)
|
||||||
object TypeAny : TypeDecl()
|
object TypeAny : TypeDecl()
|
||||||
object TypeNullableAny : TypeDecl(true)
|
object TypeNullableAny : TypeDecl(true)
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
|
||||||
|
class VarDeclStatement(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val initializer: Statement?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val typeDecl: TypeDecl?,
|
||||||
|
val slotIndex: Int?,
|
||||||
|
val scopeId: Int?,
|
||||||
|
private val startPos: Pos,
|
||||||
|
val initializerObjClass: ObjClass? = null,
|
||||||
|
) : Statement() {
|
||||||
|
override val pos: Pos = startPos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "var declaration")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng
|
||||||
|
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
|
||||||
|
sealed class WhenCondition(open val expr: Statement, open val pos: Pos) {
|
||||||
|
protected fun bytecodeOnly(scope: Scope): Nothing {
|
||||||
|
return scope.raiseIllegalState("bytecode-only execution is required; when condition needs compiled bytecode")
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract suspend fun matches(scope: Scope, value: Obj): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
class WhenEqualsCondition(
|
||||||
|
override val expr: Statement,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : WhenCondition(expr, pos) {
|
||||||
|
override suspend fun matches(scope: Scope, value: Obj): Boolean {
|
||||||
|
return bytecodeOnly(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class WhenInCondition(
|
||||||
|
override val expr: Statement,
|
||||||
|
val negated: Boolean,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : WhenCondition(expr, pos) {
|
||||||
|
override suspend fun matches(scope: Scope, value: Obj): Boolean {
|
||||||
|
return bytecodeOnly(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class WhenIsCondition(
|
||||||
|
override val expr: Statement,
|
||||||
|
val negated: Boolean,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : WhenCondition(expr, pos) {
|
||||||
|
override suspend fun matches(scope: Scope, value: Obj): Boolean {
|
||||||
|
return bytecodeOnly(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data class WhenCase(val conditions: List<WhenCondition>, val block: Statement)
|
||||||
|
|
||||||
|
class WhenStatement(
|
||||||
|
val value: Statement,
|
||||||
|
val cases: List<WhenCase>,
|
||||||
|
val elseCase: Statement?,
|
||||||
|
override val pos: Pos,
|
||||||
|
) : Statement() {
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
return bytecodeOnly(scope, "when statement")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -27,7 +27,7 @@ import net.sergeych.lyng.highlight.SimpleLyngHighlighter
|
|||||||
import net.sergeych.lyng.highlight.offsetOf
|
import net.sergeych.lyng.highlight.offsetOf
|
||||||
import net.sergeych.lyng.miniast.*
|
import net.sergeych.lyng.miniast.*
|
||||||
|
|
||||||
enum class SymbolKind { Class, Enum, Function, Value, Variable, Parameter }
|
enum class SymbolKind { Class, Enum, TypeAlias, Function, Value, Variable, Parameter }
|
||||||
|
|
||||||
data class Symbol(
|
data class Symbol(
|
||||||
val id: Int,
|
val id: Int,
|
||||||
@ -126,7 +126,8 @@ object Binder {
|
|||||||
}
|
}
|
||||||
// Members (including fields and methods)
|
// Members (including fields and methods)
|
||||||
for (m in d.members) {
|
for (m in d.members) {
|
||||||
if (m is MiniMemberValDecl) {
|
when (m) {
|
||||||
|
is MiniMemberValDecl -> {
|
||||||
val fs = source.offsetOf(m.nameStart)
|
val fs = source.offsetOf(m.nameStart)
|
||||||
val fe = fs + m.name.length
|
val fe = fs + m.name.length
|
||||||
val kind = if (m.mutable) SymbolKind.Variable else SymbolKind.Value
|
val kind = if (m.mutable) SymbolKind.Variable else SymbolKind.Value
|
||||||
@ -134,6 +135,14 @@ object Binder {
|
|||||||
symbols += fieldSym
|
symbols += fieldSym
|
||||||
classScope.fields += fieldSym.id
|
classScope.fields += fieldSym.id
|
||||||
}
|
}
|
||||||
|
is MiniMemberTypeAliasDecl -> {
|
||||||
|
val fs = source.offsetOf(m.nameStart)
|
||||||
|
val fe = fs + m.name.length
|
||||||
|
val aliasSym = Symbol(nextId++, m.name, SymbolKind.TypeAlias, fs, fe, containerId = sym.id, type = DocLookupUtils.typeOf(m.target))
|
||||||
|
symbols += aliasSym
|
||||||
|
}
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,6 +206,12 @@ object Binder {
|
|||||||
symbols += sym
|
symbols += sym
|
||||||
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
||||||
}
|
}
|
||||||
|
is MiniTypeAliasDecl -> {
|
||||||
|
val (s, e) = nameOffsets(d.nameStart, d.name)
|
||||||
|
val sym = Symbol(nextId++, d.name, SymbolKind.TypeAlias, s, e, containerId = null, type = DocLookupUtils.typeOf(d.target))
|
||||||
|
symbols += sym
|
||||||
|
topLevelByName.getOrPut(d.name) { mutableListOf() }.add(sym.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,614 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Kotlin bridge reflection facade: handle-based access for fast get/set/call.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bridge
|
||||||
|
|
||||||
|
import net.sergeych.lyng.*
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
import net.sergeych.lyng.obj.ObjIllegalAccessException
|
||||||
|
import net.sergeych.lyng.obj.ObjInstance
|
||||||
|
import net.sergeych.lyng.obj.ObjProperty
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
import net.sergeych.lyng.obj.ObjString
|
||||||
|
import net.sergeych.lyng.obj.ObjUnset
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
import net.sergeych.lyng.requireScope
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Where a bridge resolver should search for names.
|
||||||
|
*
|
||||||
|
* Used by [LookupSpec] to control reflection scope for Kotlin-side tooling and bindings.
|
||||||
|
*/
|
||||||
|
enum class LookupTarget {
|
||||||
|
/** Resolve from the current frame only (locals/params declared in the active scope). */
|
||||||
|
CurrentFrame,
|
||||||
|
/** Resolve by walking the raw parent chain of frames (locals only, no member fallback). */
|
||||||
|
ParentChain,
|
||||||
|
/** Resolve against the module frame (top-level declarations in the module). */
|
||||||
|
ModuleFrame
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explicit receiver view, similar to `this@Base` in Lyng.
|
||||||
|
*
|
||||||
|
* When provided, the resolver will treat `this` as the requested type
|
||||||
|
* for member resolution and visibility checks.
|
||||||
|
*/
|
||||||
|
data class ReceiverView(
|
||||||
|
val type: ObjClass? = null,
|
||||||
|
val typeName: String? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup rules for bridge resolution.
|
||||||
|
*
|
||||||
|
* @property targets where to resolve names from
|
||||||
|
* @property receiverView optional explicit receiver for member lookup (like `this@Base`)
|
||||||
|
*/
|
||||||
|
data class LookupSpec(
|
||||||
|
val targets: Set<LookupTarget> = setOf(LookupTarget.CurrentFrame, LookupTarget.ModuleFrame),
|
||||||
|
val receiverView: ReceiverView? = null
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base handle type returned by the Kotlin reflection bridge.
|
||||||
|
*
|
||||||
|
* Handles are inexpensive to keep and cache; they resolve lazily and
|
||||||
|
* may internally cache slots/records once a frame is known.
|
||||||
|
*/
|
||||||
|
sealed interface BridgeHandle {
|
||||||
|
/** Name of the underlying symbol (as written in Lyng). */
|
||||||
|
val name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read-only value handle resolved in a [ScopeFacade]. */
|
||||||
|
interface ValHandle : BridgeHandle {
|
||||||
|
/** Read the current value. */
|
||||||
|
suspend fun get(scope: ScopeFacade): Obj
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read/write value handle resolved in a [ScopeFacade]. */
|
||||||
|
interface VarHandle : ValHandle {
|
||||||
|
/** Assign a new value. */
|
||||||
|
suspend fun set(scope: ScopeFacade, value: Obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Callable handle (function/closure/method). */
|
||||||
|
interface CallableHandle : BridgeHandle {
|
||||||
|
/**
|
||||||
|
* Call the target with optional [args].
|
||||||
|
*
|
||||||
|
* @param newThisObj overrides receiver for member calls (defaults to current `this`/record receiver).
|
||||||
|
*/
|
||||||
|
suspend fun call(scope: ScopeFacade, args: Arguments = Arguments.EMPTY, newThisObj: Obj? = null): Obj
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Member handle resolved against an instance or receiver view. */
|
||||||
|
interface MemberHandle : BridgeHandle {
|
||||||
|
/** Declaring class resolved for the last call/get/set (if known). */
|
||||||
|
val declaringClass: ObjClass?
|
||||||
|
/** Explicit receiver view used for resolution (if any). */
|
||||||
|
val receiverView: ReceiverView?
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Member field/property (read-only). */
|
||||||
|
interface MemberValHandle : MemberHandle, ValHandle
|
||||||
|
|
||||||
|
/** Member var/property with write access. */
|
||||||
|
interface MemberVarHandle : MemberHandle, VarHandle
|
||||||
|
|
||||||
|
/** Member callable (method or extension). */
|
||||||
|
interface MemberCallableHandle : MemberHandle, CallableHandle
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Direct record handle (debug/inspection).
|
||||||
|
*
|
||||||
|
* Exposes raw [ObjRecord] access and should be used only in tooling.
|
||||||
|
*/
|
||||||
|
interface RecordHandle : BridgeHandle {
|
||||||
|
/** Resolve and return the raw [ObjRecord]. */
|
||||||
|
fun record(): ObjRecord
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bridge resolver API (entry point for Kotlin reflection and bindings).
|
||||||
|
*
|
||||||
|
* Obtain via [ScopeFacade.resolver] and reuse for multiple lookups.
|
||||||
|
* Resolver methods return handles that can be cached and reused across calls.
|
||||||
|
*/
|
||||||
|
interface BridgeResolver {
|
||||||
|
/** Source position used for error reporting. */
|
||||||
|
val pos: Pos
|
||||||
|
|
||||||
|
/** Treat `this` as [type] for member lookup (like `this@Type`). */
|
||||||
|
fun selfAs(type: ObjClass): BridgeResolver
|
||||||
|
/** Treat `this` as [typeName] for member lookup (like `this@Type`). */
|
||||||
|
fun selfAs(typeName: String): BridgeResolver
|
||||||
|
|
||||||
|
/** Resolve a read-only value by name using [lookup]. */
|
||||||
|
fun resolveVal(name: String, lookup: LookupSpec = LookupSpec()): ValHandle
|
||||||
|
/** Resolve a mutable value by name using [lookup]. */
|
||||||
|
fun resolveVar(name: String, lookup: LookupSpec = LookupSpec()): VarHandle
|
||||||
|
/** Resolve a callable by name using [lookup]. */
|
||||||
|
fun resolveCallable(name: String, lookup: LookupSpec = LookupSpec()): CallableHandle
|
||||||
|
|
||||||
|
/** Resolve a member value on [receiver]. */
|
||||||
|
fun resolveMemberVal(
|
||||||
|
receiver: Obj,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec = LookupSpec()
|
||||||
|
): MemberValHandle
|
||||||
|
|
||||||
|
/** Resolve a mutable member on [receiver]. */
|
||||||
|
fun resolveMemberVar(
|
||||||
|
receiver: Obj,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec = LookupSpec()
|
||||||
|
): MemberVarHandle
|
||||||
|
|
||||||
|
/** Resolve a member callable on [receiver]. */
|
||||||
|
fun resolveMemberCallable(
|
||||||
|
receiver: Obj,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec = LookupSpec()
|
||||||
|
): MemberCallableHandle
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve an extension function treated as a member for reflection.
|
||||||
|
*
|
||||||
|
* This uses the extension wrapper name (same rules as Lyng compiler).
|
||||||
|
*/
|
||||||
|
fun resolveExtensionCallable(
|
||||||
|
receiverClass: ObjClass,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec = LookupSpec()
|
||||||
|
): MemberCallableHandle
|
||||||
|
|
||||||
|
/** Debug: resolve locals by name (optional, for tooling). */
|
||||||
|
fun resolveLocalVal(name: String): ValHandle
|
||||||
|
/** Debug: resolve mutable locals by name (optional, for tooling). */
|
||||||
|
fun resolveLocalVar(name: String): VarHandle
|
||||||
|
|
||||||
|
/** Debug: access raw record handles if needed. */
|
||||||
|
fun resolveRecord(name: String, lookup: LookupSpec = LookupSpec()): RecordHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convenience: call by name with implicit caching in resolver implementation.
|
||||||
|
*
|
||||||
|
* Implemented by the default resolver; useful for lightweight call-by-name flows.
|
||||||
|
*/
|
||||||
|
interface BridgeCallByName {
|
||||||
|
/** Resolve and call [name] with [args] using [lookup]. */
|
||||||
|
suspend fun callByName(
|
||||||
|
scope: ScopeFacade,
|
||||||
|
name: String,
|
||||||
|
args: Arguments = Arguments.EMPTY,
|
||||||
|
lookup: LookupSpec = LookupSpec()
|
||||||
|
): Obj
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional typed wrapper (sugar) around [ValHandle].
|
||||||
|
*
|
||||||
|
* Performs a runtime cast to [T] and raises a class cast error on mismatch.
|
||||||
|
*/
|
||||||
|
interface TypedHandle<T : Obj> : ValHandle {
|
||||||
|
/** Read value and cast it to [T]. */
|
||||||
|
suspend fun getTyped(scope: ScopeFacade): T
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for bridge resolver.
|
||||||
|
*
|
||||||
|
* Prefer this over ad-hoc lookups when writing Kotlin extensions or tooling.
|
||||||
|
*/
|
||||||
|
fun ScopeFacade.resolver(): BridgeResolver = BridgeResolverImpl(this)
|
||||||
|
|
||||||
|
private class BridgeResolverImpl(
|
||||||
|
private val facade: ScopeFacade,
|
||||||
|
private val receiverView: ReceiverView? = null
|
||||||
|
) : BridgeResolver, BridgeCallByName {
|
||||||
|
private val cachedCallables: MutableMap<String, CallableHandle> = LinkedHashMap()
|
||||||
|
|
||||||
|
override val pos: Pos
|
||||||
|
get() = facade.pos
|
||||||
|
|
||||||
|
override fun selfAs(type: ObjClass): BridgeResolver = BridgeResolverImpl(facade, ReceiverView(type = type))
|
||||||
|
|
||||||
|
override fun selfAs(typeName: String): BridgeResolver = BridgeResolverImpl(facade, ReceiverView(typeName = typeName))
|
||||||
|
|
||||||
|
override fun resolveVal(name: String, lookup: LookupSpec): ValHandle =
|
||||||
|
LocalValHandle(this, name, lookup)
|
||||||
|
|
||||||
|
override fun resolveVar(name: String, lookup: LookupSpec): VarHandle =
|
||||||
|
LocalVarHandle(this, name, lookup)
|
||||||
|
|
||||||
|
override fun resolveCallable(name: String, lookup: LookupSpec): CallableHandle =
|
||||||
|
LocalCallableHandle(this, name, lookup)
|
||||||
|
|
||||||
|
override fun resolveMemberVal(receiver: Obj, name: String, lookup: LookupSpec): MemberValHandle =
|
||||||
|
MemberValHandleImpl(this, receiver, name, lookup.receiverView ?: receiverView)
|
||||||
|
|
||||||
|
override fun resolveMemberVar(receiver: Obj, name: String, lookup: LookupSpec): MemberVarHandle =
|
||||||
|
MemberVarHandleImpl(this, receiver, name, lookup.receiverView ?: receiverView)
|
||||||
|
|
||||||
|
override fun resolveMemberCallable(receiver: Obj, name: String, lookup: LookupSpec): MemberCallableHandle =
|
||||||
|
MemberCallableHandleImpl(this, receiver, name, lookup.receiverView ?: receiverView)
|
||||||
|
|
||||||
|
override fun resolveExtensionCallable(receiverClass: ObjClass, name: String, lookup: LookupSpec): MemberCallableHandle =
|
||||||
|
ExtensionCallableHandleImpl(this, receiverClass, name, lookup)
|
||||||
|
|
||||||
|
override fun resolveLocalVal(name: String): ValHandle =
|
||||||
|
LocalValHandle(this, name, LookupSpec(targets = setOf(LookupTarget.CurrentFrame)))
|
||||||
|
|
||||||
|
override fun resolveLocalVar(name: String): VarHandle =
|
||||||
|
LocalVarHandle(this, name, LookupSpec(targets = setOf(LookupTarget.CurrentFrame)))
|
||||||
|
|
||||||
|
override fun resolveRecord(name: String, lookup: LookupSpec): RecordHandle =
|
||||||
|
RecordHandleImpl(this, name, lookup)
|
||||||
|
|
||||||
|
override suspend fun callByName(scope: ScopeFacade, name: String, args: Arguments, lookup: LookupSpec): Obj {
|
||||||
|
val handle = cachedCallables.getOrPut(name) { resolveCallable(name, lookup) }
|
||||||
|
return handle.call(scope, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun facade(): ScopeFacade = facade
|
||||||
|
|
||||||
|
fun resolveLocalRecord(scope: Scope, name: String, lookup: LookupSpec): ObjRecord {
|
||||||
|
val caller = scope.currentClassCtx
|
||||||
|
if (LookupTarget.CurrentFrame in lookup.targets) {
|
||||||
|
scope.tryGetLocalRecord(scope, name, caller)?.let { return it }
|
||||||
|
}
|
||||||
|
if (LookupTarget.ParentChain in lookup.targets) {
|
||||||
|
scope.chainLookupIgnoreClosure(name, followClosure = false, caller = caller)?.let { return it }
|
||||||
|
}
|
||||||
|
if (LookupTarget.ModuleFrame in lookup.targets) {
|
||||||
|
findModuleScope(scope)?.let { module ->
|
||||||
|
module.tryGetLocalRecord(module, name, caller)?.let { return it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
facade.raiseSymbolNotFound(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resolveReceiver(scope: Scope, receiver: Obj, view: ReceiverView?): Obj {
|
||||||
|
if (view == null) return receiver
|
||||||
|
if (receiver !== scope.thisObj) return receiver
|
||||||
|
val target = when {
|
||||||
|
view.type != null -> scope.thisVariants.firstOrNull { it.isInstanceOf(view.type) }
|
||||||
|
view.typeName != null -> scope.thisVariants.firstOrNull { it.isInstanceOf(view.typeName) }
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
return target ?: facade.raiseSymbolNotFound(view.typeName ?: view.type?.className ?: "<receiver>")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun resolveMemberRecord(scope: Scope, receiver: Obj, name: String): MemberResolution {
|
||||||
|
if (receiver is ObjClass) {
|
||||||
|
val rec = receiver.classScope?.objects?.get(name) ?: receiver.members[name]
|
||||||
|
?: facade.raiseSymbolNotFound("member $name not found on ${receiver.className}")
|
||||||
|
val decl = rec.declaringClass ?: receiver
|
||||||
|
if (!canAccessMember(rec.visibility, decl, scope.currentClassCtx, name)) {
|
||||||
|
facade.raiseError(
|
||||||
|
ObjIllegalAccessException(
|
||||||
|
scope,
|
||||||
|
"can't access ${name}: not visible (declared in ${decl.className}, caller ${scope.currentClassCtx?.className ?: "?"})"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return MemberResolution(rec, decl, receiver, rec.fieldId, rec.methodId)
|
||||||
|
}
|
||||||
|
val cls = receiver.objClass
|
||||||
|
val resolved = cls.resolveInstanceMember(name)
|
||||||
|
?: facade.raiseSymbolNotFound("member $name not found on ${cls.className}")
|
||||||
|
val decl = resolved.declaringClass
|
||||||
|
if (!canAccessMember(resolved.record.visibility, decl, scope.currentClassCtx, name)) {
|
||||||
|
facade.raiseError(
|
||||||
|
ObjIllegalAccessException(
|
||||||
|
scope,
|
||||||
|
"can't access ${name}: not visible (declared in ${decl.className}, caller ${scope.currentClassCtx?.className ?: "?"})"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val fieldId = if (resolved.record.type == ObjRecord.Type.Field ||
|
||||||
|
resolved.record.type == ObjRecord.Type.ConstructorField
|
||||||
|
) {
|
||||||
|
resolved.record.fieldId ?: cls.instanceFieldIdMap()[name]
|
||||||
|
} else null
|
||||||
|
val methodId = if (resolved.record.type == ObjRecord.Type.Fun ||
|
||||||
|
resolved.record.type == ObjRecord.Type.Property ||
|
||||||
|
resolved.record.type == ObjRecord.Type.Delegated
|
||||||
|
) {
|
||||||
|
resolved.record.methodId ?: cls.instanceMethodIdMap(includeAbstract = true)[name]
|
||||||
|
} else null
|
||||||
|
return MemberResolution(resolved.record, decl, receiver.objClass, fieldId, methodId)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findModuleScope(scope: Scope): ModuleScope? {
|
||||||
|
var s: Scope? = scope
|
||||||
|
var hops = 0
|
||||||
|
while (s != null && hops++ < 1024) {
|
||||||
|
if (s is ModuleScope) return s
|
||||||
|
s = s.parent
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class LocalResolution(
|
||||||
|
val record: ObjRecord,
|
||||||
|
val frameId: Long
|
||||||
|
)
|
||||||
|
|
||||||
|
private data class MemberResolution(
|
||||||
|
val record: ObjRecord,
|
||||||
|
val declaringClass: ObjClass,
|
||||||
|
val receiverClass: ObjClass,
|
||||||
|
val fieldId: Int?,
|
||||||
|
val methodId: Int?
|
||||||
|
)
|
||||||
|
|
||||||
|
private abstract class LocalHandleBase(
|
||||||
|
protected val resolver: BridgeResolverImpl,
|
||||||
|
override val name: String,
|
||||||
|
private val lookup: LookupSpec
|
||||||
|
) : BridgeHandle {
|
||||||
|
private var cached: LocalResolution? = null
|
||||||
|
|
||||||
|
protected fun resolve(scope: Scope): ObjRecord {
|
||||||
|
val cachedLocal = cached
|
||||||
|
if (cachedLocal != null && cachedLocal.frameId == scope.frameId) {
|
||||||
|
return cachedLocal.record
|
||||||
|
}
|
||||||
|
val rec = resolver.resolveLocalRecord(scope, name, lookup)
|
||||||
|
cached = LocalResolution(rec, scope.frameId)
|
||||||
|
return rec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class LocalValHandle(
|
||||||
|
resolver: BridgeResolverImpl,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec
|
||||||
|
) : LocalHandleBase(resolver, name, lookup), ValHandle {
|
||||||
|
override suspend fun get(scope: ScopeFacade): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val rec = resolve(real)
|
||||||
|
return real.resolve(rec, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class LocalVarHandle(
|
||||||
|
resolver: BridgeResolverImpl,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec
|
||||||
|
) : LocalHandleBase(resolver, name, lookup), VarHandle {
|
||||||
|
override suspend fun get(scope: ScopeFacade): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val rec = resolve(real)
|
||||||
|
return real.resolve(rec, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(scope: ScopeFacade, value: Obj) {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val rec = resolve(real)
|
||||||
|
real.assign(rec, name, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class LocalCallableHandle(
|
||||||
|
resolver: BridgeResolverImpl,
|
||||||
|
name: String,
|
||||||
|
lookup: LookupSpec
|
||||||
|
) : LocalHandleBase(resolver, name, lookup), CallableHandle {
|
||||||
|
override suspend fun call(scope: ScopeFacade, args: Arguments, newThisObj: Obj?): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val rec = resolve(real)
|
||||||
|
val callee = rec.value
|
||||||
|
return scope.call(callee, args, newThisObj ?: rec.receiver)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private abstract class MemberHandleBase(
|
||||||
|
protected val resolver: BridgeResolverImpl,
|
||||||
|
receiver: Obj,
|
||||||
|
override val name: String,
|
||||||
|
override val receiverView: ReceiverView?
|
||||||
|
) : MemberHandle {
|
||||||
|
private val baseReceiver: Obj = receiver
|
||||||
|
private var cachedResolution: MemberResolution? = null
|
||||||
|
private var cachedDeclaringClass: ObjClass? = null
|
||||||
|
|
||||||
|
protected fun resolve(scope: Scope): Pair<Obj, MemberResolution> {
|
||||||
|
val resolvedReceiver = resolver.resolveReceiver(scope, baseReceiver, receiverView)
|
||||||
|
val cached = cachedResolution
|
||||||
|
if (cached != null && resolvedReceiver.objClass === cached.receiverClass) {
|
||||||
|
cachedDeclaringClass = cached.declaringClass
|
||||||
|
return Pair(resolvedReceiver, cached)
|
||||||
|
}
|
||||||
|
val res = resolver.resolveMemberRecord(scope, resolvedReceiver, name)
|
||||||
|
cachedResolution = res
|
||||||
|
cachedDeclaringClass = res.declaringClass
|
||||||
|
return Pair(resolvedReceiver, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun declaringClass(): ObjClass? = cachedDeclaringClass
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MemberValHandleImpl(
|
||||||
|
resolver: BridgeResolverImpl,
|
||||||
|
receiver: Obj,
|
||||||
|
name: String,
|
||||||
|
receiverView: ReceiverView?
|
||||||
|
) : MemberHandleBase(resolver, receiver, name, receiverView), MemberValHandle {
|
||||||
|
override val declaringClass: ObjClass?
|
||||||
|
get() = declaringClass()
|
||||||
|
|
||||||
|
override suspend fun get(scope: ScopeFacade): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val (receiver, res) = resolve(real)
|
||||||
|
val rec = resolveMemberRecordFast(receiver, res)
|
||||||
|
return receiver.resolveRecord(real, rec, name, res.declaringClass).value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MemberVarHandleImpl(
|
||||||
|
resolver: BridgeResolverImpl,
|
||||||
|
receiver: Obj,
|
||||||
|
name: String,
|
||||||
|
receiverView: ReceiverView?
|
||||||
|
) : MemberHandleBase(resolver, receiver, name, receiverView), MemberVarHandle {
|
||||||
|
override val declaringClass: ObjClass?
|
||||||
|
get() = declaringClass()
|
||||||
|
|
||||||
|
override suspend fun get(scope: ScopeFacade): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val (receiver, res) = resolve(real)
|
||||||
|
val rec = resolveMemberRecordFast(receiver, res)
|
||||||
|
return receiver.resolveRecord(real, rec, name, res.declaringClass).value
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(scope: ScopeFacade, value: Obj) {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val (receiver, res) = resolve(real)
|
||||||
|
val rec = resolveMemberRecordFast(receiver, res)
|
||||||
|
assignMemberRecord(real, receiver, res.declaringClass, rec, name, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MemberCallableHandleImpl(
|
||||||
|
resolver: BridgeResolverImpl,
|
||||||
|
receiver: Obj,
|
||||||
|
name: String,
|
||||||
|
receiverView: ReceiverView?
|
||||||
|
) : MemberHandleBase(resolver, receiver, name, receiverView), MemberCallableHandle {
|
||||||
|
override val declaringClass: ObjClass?
|
||||||
|
get() = declaringClass()
|
||||||
|
|
||||||
|
override suspend fun call(scope: ScopeFacade, args: Arguments, newThisObj: Obj?): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val (receiver, res) = resolve(real)
|
||||||
|
val rec = resolveMemberRecordFast(receiver, res)
|
||||||
|
if (rec.type != ObjRecord.Type.Fun) {
|
||||||
|
scope.raiseError("member $name is not callable")
|
||||||
|
}
|
||||||
|
return rec.value.invoke(real, receiver, args, res.declaringClass)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ExtensionCallableHandleImpl(
|
||||||
|
private val resolver: BridgeResolverImpl,
|
||||||
|
private val receiverClass: ObjClass,
|
||||||
|
override val name: String,
|
||||||
|
private val lookup: LookupSpec
|
||||||
|
) : MemberCallableHandle {
|
||||||
|
override val receiverView: ReceiverView?
|
||||||
|
get() = null
|
||||||
|
override val declaringClass: ObjClass?
|
||||||
|
get() = receiverClass
|
||||||
|
|
||||||
|
override suspend fun call(scope: ScopeFacade, args: Arguments, newThisObj: Obj?): Obj {
|
||||||
|
val real = scope.requireScope()
|
||||||
|
val wrapperName = extensionCallableName(receiverClass.className, name)
|
||||||
|
val rec = resolver.resolveLocalRecord(real, wrapperName, lookup)
|
||||||
|
val receiver = newThisObj ?: real.thisObj
|
||||||
|
val callArgs = Arguments(listOf(receiver) + args.list)
|
||||||
|
return scope.call(rec.value, callArgs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class RecordHandleImpl(
|
||||||
|
private val resolver: BridgeResolverImpl,
|
||||||
|
override val name: String,
|
||||||
|
private val lookup: LookupSpec
|
||||||
|
) : RecordHandle {
|
||||||
|
override fun record(): ObjRecord {
|
||||||
|
val scope = resolver.facade().requireScope()
|
||||||
|
return resolver.resolveLocalRecord(scope, name, lookup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TypedHandleImpl<T : Obj>(
|
||||||
|
private val inner: ValHandle,
|
||||||
|
private val clazzName: String
|
||||||
|
) : TypedHandle<T> {
|
||||||
|
override val name: String
|
||||||
|
get() = inner.name
|
||||||
|
|
||||||
|
override suspend fun get(scope: ScopeFacade): Obj = inner.get(scope)
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
override suspend fun getTyped(scope: ScopeFacade): T {
|
||||||
|
val value = inner.get(scope)
|
||||||
|
return (value as? T)
|
||||||
|
?: scope.raiseClassCastError("Expected $clazzName, got ${value.objClass.className}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resolveMemberRecordFast(receiver: Obj, res: MemberResolution): ObjRecord {
|
||||||
|
val inst = receiver as? ObjInstance
|
||||||
|
if (inst != null) {
|
||||||
|
res.fieldId?.let { inst.fieldRecordForId(it)?.let { return it } }
|
||||||
|
res.methodId?.let { inst.methodRecordForId(it)?.let { return it } }
|
||||||
|
}
|
||||||
|
return res.record
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun assignMemberRecord(
|
||||||
|
scope: Scope,
|
||||||
|
receiver: Obj,
|
||||||
|
declaringClass: ObjClass,
|
||||||
|
rec: ObjRecord,
|
||||||
|
name: String,
|
||||||
|
value: Obj
|
||||||
|
) {
|
||||||
|
val caller = scope.currentClassCtx
|
||||||
|
if (!canAccessMember(rec.effectiveWriteVisibility, declaringClass, caller, name)) {
|
||||||
|
scope.raiseError(
|
||||||
|
ObjIllegalAccessException(
|
||||||
|
scope,
|
||||||
|
"can't assign ${name}: not visible (declared in ${declaringClass.className}, caller ${caller?.className ?: "?"})"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
when {
|
||||||
|
rec.type == ObjRecord.Type.Delegated -> {
|
||||||
|
val del = rec.delegate ?: scope.raiseError("Internal error: delegated property $name has no delegate")
|
||||||
|
val th = if (receiver === ObjVoid) net.sergeych.lyng.obj.ObjNull else receiver
|
||||||
|
del.invokeInstanceMethod(scope, "setValue", Arguments(th, ObjString(name), value))
|
||||||
|
}
|
||||||
|
rec.value is ObjProperty || rec.type == ObjRecord.Type.Property -> {
|
||||||
|
val prop = rec.value as? ObjProperty
|
||||||
|
?: scope.raiseError("Expected ObjProperty for property member $name")
|
||||||
|
prop.callSetter(scope, receiver, value, declaringClass)
|
||||||
|
}
|
||||||
|
rec.isMutable -> {
|
||||||
|
val slotRef = rec.value
|
||||||
|
if (slotRef is net.sergeych.lyng.FrameSlotRef) {
|
||||||
|
if (!rec.isMutable && slotRef.read() !== ObjUnset) scope.raiseError("can't reassign val $name")
|
||||||
|
slotRef.write(value)
|
||||||
|
} else {
|
||||||
|
rec.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> scope.raiseError("can't assign to read-only field: $name")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,637 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Kotlin bridge bindings for Lyng classes (Lyng-first workflow).
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bridge
|
||||||
|
|
||||||
|
import net.sergeych.lyng.*
|
||||||
|
import net.sergeych.lyng.bytecode.BytecodeStatement
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
import net.sergeych.lyng.obj.ObjExternCallable
|
||||||
|
import net.sergeych.lyng.obj.ObjInstance
|
||||||
|
import net.sergeych.lyng.obj.ObjProperty
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
import net.sergeych.lyng.obj.ObjVoid
|
||||||
|
import net.sergeych.lyng.pacman.ImportManager
|
||||||
|
import net.sergeych.lyng.requiredArg
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per-instance bridge context passed to init hooks.
|
||||||
|
*
|
||||||
|
* Exposes the underlying [instance] and a mutable [data] slot for Kotlin-side state.
|
||||||
|
*/
|
||||||
|
interface BridgeInstanceContext {
|
||||||
|
/** The Lyng instance being initialized. */
|
||||||
|
val instance: Obj
|
||||||
|
/** Arbitrary Kotlin-side data attached to the instance. */
|
||||||
|
var data: Any?
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binder DSL for attaching Kotlin implementations to a declared Lyng class.
|
||||||
|
*
|
||||||
|
* Use [LyngClassBridge.bind] to obtain a binder and register implementations.
|
||||||
|
* Bindings must happen before the first instance of the class is created.
|
||||||
|
*
|
||||||
|
* Important: members you bind here must be declared as `extern` in Lyng so the
|
||||||
|
* compiler emits the ABI slots that Kotlin bindings attach to.
|
||||||
|
*/
|
||||||
|
interface ClassBridgeBinder {
|
||||||
|
/** Arbitrary Kotlin-side data attached to the class. */
|
||||||
|
var classData: Any?
|
||||||
|
/** Register an initialization hook that runs for each instance. */
|
||||||
|
fun init(block: suspend BridgeInstanceContext.(ScopeFacade) -> Unit)
|
||||||
|
/** Register an initialization hook with direct access to the instance. */
|
||||||
|
fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit)
|
||||||
|
/** Bind a Lyng function/member to a Kotlin implementation (requires `extern` in Lyng). */
|
||||||
|
fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj)
|
||||||
|
/** Bind a read-only member (val/property getter) declared as `extern`. */
|
||||||
|
fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj)
|
||||||
|
/** Bind a mutable member (var/property getter/setter) declared as `extern`. */
|
||||||
|
fun addVar(
|
||||||
|
name: String,
|
||||||
|
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||||
|
set: suspend (ScopeFacade, Obj, Obj) -> Unit
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entry point for Kotlin bindings to declared Lyng classes.
|
||||||
|
*
|
||||||
|
* The workflow is Lyng-first: declare the class and its members in Lyng,
|
||||||
|
* then bind the implementations from Kotlin. Bound members must be marked
|
||||||
|
* `extern` so the compiler emits the ABI slots for Kotlin to attach to.
|
||||||
|
*/
|
||||||
|
object LyngClassBridge {
|
||||||
|
/**
|
||||||
|
* Resolve a Lyng class by [className] and bind Kotlin implementations.
|
||||||
|
*
|
||||||
|
* @param module module name used for resolution (required when [module] scope is not provided)
|
||||||
|
* @param importManager import manager used to resolve the module
|
||||||
|
*/
|
||||||
|
suspend fun bind(
|
||||||
|
className: String,
|
||||||
|
module: String? = null,
|
||||||
|
importManager: ImportManager = Script.defaultImportManager,
|
||||||
|
block: ClassBridgeBinder.() -> Unit
|
||||||
|
): ObjClass {
|
||||||
|
val cls = resolveClass(className, module, null, importManager)
|
||||||
|
return bind(cls, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve a Lyng class within an existing [moduleScope] and bind Kotlin implementations.
|
||||||
|
*/
|
||||||
|
suspend fun bind(
|
||||||
|
moduleScope: ModuleScope,
|
||||||
|
className: String,
|
||||||
|
block: ClassBridgeBinder.() -> Unit
|
||||||
|
): ObjClass {
|
||||||
|
val cls = resolveClass(className, null, moduleScope, Script.defaultImportManager)
|
||||||
|
return bind(cls, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind Kotlin implementations to an already resolved [clazz].
|
||||||
|
*
|
||||||
|
* This must run before the first instance is created.
|
||||||
|
*/
|
||||||
|
fun bind(clazz: ObjClass, block: ClassBridgeBinder.() -> Unit): ObjClass {
|
||||||
|
val binder = ClassBridgeBinderImpl(clazz)
|
||||||
|
binder.block()
|
||||||
|
binder.commit()
|
||||||
|
return clazz
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entry point for Kotlin bindings to declared Lyng objects (singleton instances).
|
||||||
|
*
|
||||||
|
* Works similarly to [LyngClassBridge], but targets an already created object instance.
|
||||||
|
*/
|
||||||
|
object LyngObjectBridge {
|
||||||
|
/**
|
||||||
|
* Resolve a Lyng object by [objectName] and bind Kotlin implementations.
|
||||||
|
*
|
||||||
|
* @param module module name used for resolution (required when [module] scope is not provided)
|
||||||
|
* @param importManager import manager used to resolve the module
|
||||||
|
*/
|
||||||
|
suspend fun bind(
|
||||||
|
objectName: String,
|
||||||
|
module: String? = null,
|
||||||
|
importManager: ImportManager = Script.defaultImportManager,
|
||||||
|
block: ClassBridgeBinder.() -> Unit
|
||||||
|
): ObjInstance {
|
||||||
|
val obj = resolveObject(objectName, module, null, importManager)
|
||||||
|
return bind(obj, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve a Lyng object within an existing [moduleScope] and bind Kotlin implementations.
|
||||||
|
*/
|
||||||
|
suspend fun bind(
|
||||||
|
moduleScope: ModuleScope,
|
||||||
|
objectName: String,
|
||||||
|
block: ClassBridgeBinder.() -> Unit
|
||||||
|
): ObjInstance {
|
||||||
|
val obj = resolveObject(objectName, null, moduleScope, Script.defaultImportManager)
|
||||||
|
return bind(obj, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind Kotlin implementations directly to an already resolved object [instance].
|
||||||
|
*/
|
||||||
|
suspend fun bind(instance: ObjInstance, block: ClassBridgeBinder.() -> Unit): ObjInstance {
|
||||||
|
val binder = ObjectBridgeBinderImpl(instance)
|
||||||
|
binder.block()
|
||||||
|
binder.commit()
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sugar for [LyngClassBridge.bind] on a module scope.
|
||||||
|
*
|
||||||
|
* Bound members must be declared as `extern` in Lyng.
|
||||||
|
*/
|
||||||
|
suspend fun ModuleScope.bind(
|
||||||
|
className: String,
|
||||||
|
block: ClassBridgeBinder.() -> Unit
|
||||||
|
): ObjClass = LyngClassBridge.bind(this, className, block)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sugar for [LyngObjectBridge.bind] on a module scope.
|
||||||
|
*
|
||||||
|
* Bound members must be declared as `extern` in Lyng.
|
||||||
|
*/
|
||||||
|
suspend fun ModuleScope.bindObject(
|
||||||
|
objectName: String,
|
||||||
|
block: ClassBridgeBinder.() -> Unit
|
||||||
|
): ObjInstance = LyngObjectBridge.bind(this, objectName, block)
|
||||||
|
|
||||||
|
/** Kotlin-side data slot attached to a Lyng instance. */
|
||||||
|
var ObjInstance.data: Any?
|
||||||
|
get() = kotlinInstanceData
|
||||||
|
set(value) { kotlinInstanceData = value }
|
||||||
|
|
||||||
|
/** Kotlin-side data slot attached to a Lyng class. */
|
||||||
|
var ObjClass.classData: Any?
|
||||||
|
get() = kotlinClassData
|
||||||
|
set(value) { kotlinClassData = value }
|
||||||
|
|
||||||
|
private enum class MemberKind { Instance, Static }
|
||||||
|
|
||||||
|
private data class MemberTarget(
|
||||||
|
val name: String,
|
||||||
|
val record: ObjRecord,
|
||||||
|
val kind: MemberKind,
|
||||||
|
val mirrorClassScope: Boolean = false
|
||||||
|
)
|
||||||
|
|
||||||
|
private class BridgeInstanceContextImpl(
|
||||||
|
override val instance: Obj
|
||||||
|
) : BridgeInstanceContext {
|
||||||
|
private fun instanceObj(): ObjInstance =
|
||||||
|
instance as? ObjInstance ?: error("Bridge instance is not an ObjInstance")
|
||||||
|
|
||||||
|
override var data: Any?
|
||||||
|
get() = instanceObj().kotlinInstanceData
|
||||||
|
set(value) { instanceObj().kotlinInstanceData = value }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ClassBridgeBinderImpl(
|
||||||
|
private val cls: ObjClass
|
||||||
|
) : ClassBridgeBinder {
|
||||||
|
private val initHooks = mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>()
|
||||||
|
private var checkedTemplate = false
|
||||||
|
|
||||||
|
override var classData: Any?
|
||||||
|
get() = cls.kotlinClassData
|
||||||
|
set(value) { cls.kotlinClassData = value }
|
||||||
|
|
||||||
|
override fun init(block: suspend BridgeInstanceContext.(ScopeFacade) -> Unit) {
|
||||||
|
initHooks.add { scope, inst ->
|
||||||
|
val ctx = BridgeInstanceContextImpl(inst)
|
||||||
|
ctx.block(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit) {
|
||||||
|
initHooks.add { scope, inst ->
|
||||||
|
block(scope, inst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj) {
|
||||||
|
ensureTemplateNotBuilt()
|
||||||
|
val target = findMember(name)
|
||||||
|
val callable = ObjExternCallable.fromBridge {
|
||||||
|
impl(this, thisObj, args)
|
||||||
|
}
|
||||||
|
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||||
|
val newRecord = target.record.copy(
|
||||||
|
value = callable,
|
||||||
|
type = ObjRecord.Type.Fun,
|
||||||
|
methodId = methodId
|
||||||
|
)
|
||||||
|
replaceMember(target, newRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj) {
|
||||||
|
ensureTemplateNotBuilt()
|
||||||
|
val target = findMember(name)
|
||||||
|
if (target.record.isMutable) {
|
||||||
|
throw ScriptError(Pos.builtIn, "extern val $name is mutable in class ${cls.className}")
|
||||||
|
}
|
||||||
|
val getter = ObjExternCallable.fromBridge {
|
||||||
|
impl(this, thisObj)
|
||||||
|
}
|
||||||
|
val prop = ObjProperty(name, getter, null)
|
||||||
|
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||||
|
target.record.type == ObjRecord.Type.ConstructorField
|
||||||
|
val newRecord = if (isFieldLike) {
|
||||||
|
removeFieldInitializersFor(name)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = target.record.type,
|
||||||
|
fieldId = target.record.fieldId,
|
||||||
|
methodId = target.record.methodId
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = ObjRecord.Type.Property,
|
||||||
|
methodId = methodId,
|
||||||
|
fieldId = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
replaceMember(target, newRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addVar(
|
||||||
|
name: String,
|
||||||
|
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||||
|
set: suspend (ScopeFacade, Obj, Obj) -> Unit
|
||||||
|
) {
|
||||||
|
ensureTemplateNotBuilt()
|
||||||
|
val target = findMember(name)
|
||||||
|
if (!target.record.isMutable) {
|
||||||
|
throw ScriptError(Pos.builtIn, "extern var $name is readonly in class ${cls.className}")
|
||||||
|
}
|
||||||
|
val getter = ObjExternCallable.fromBridge {
|
||||||
|
get(this, thisObj)
|
||||||
|
}
|
||||||
|
val setter = ObjExternCallable.fromBridge {
|
||||||
|
val value = requiredArg<Obj>(0)
|
||||||
|
set(this, thisObj, value)
|
||||||
|
ObjVoid
|
||||||
|
}
|
||||||
|
val prop = ObjProperty(name, getter, setter)
|
||||||
|
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||||
|
target.record.type == ObjRecord.Type.ConstructorField
|
||||||
|
val newRecord = if (isFieldLike) {
|
||||||
|
removeFieldInitializersFor(name)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = target.record.type,
|
||||||
|
fieldId = target.record.fieldId,
|
||||||
|
methodId = target.record.methodId
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = ObjRecord.Type.Property,
|
||||||
|
methodId = methodId,
|
||||||
|
fieldId = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
replaceMember(target, newRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun commit() {
|
||||||
|
if (initHooks.isNotEmpty()) {
|
||||||
|
val target = cls.bridgeInitHooks ?: mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>().also {
|
||||||
|
cls.bridgeInitHooks = it
|
||||||
|
}
|
||||||
|
target.addAll(initHooks)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun ensureTemplateNotBuilt() {
|
||||||
|
if (!checkedTemplate) {
|
||||||
|
if (cls.instanceTemplateBuilt) {
|
||||||
|
throw ScriptError(
|
||||||
|
Pos.builtIn,
|
||||||
|
"bridge binding for ${cls.className} must happen before first instance is created"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
checkedTemplate = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun replaceMember(target: MemberTarget, newRecord: ObjRecord) {
|
||||||
|
when (target.kind) {
|
||||||
|
MemberKind.Instance -> {
|
||||||
|
cls.replaceMemberForBridge(target.name, newRecord)
|
||||||
|
if (target.mirrorClassScope && cls.classScope?.objects?.containsKey(target.name) == true) {
|
||||||
|
cls.replaceClassScopeMemberForBridge(target.name, newRecord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MemberKind.Static -> cls.replaceClassScopeMemberForBridge(target.name, newRecord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findMember(name: String): MemberTarget {
|
||||||
|
val inst = cls.members[name]
|
||||||
|
val stat = cls.classScope?.objects?.get(name)
|
||||||
|
if (inst != null) {
|
||||||
|
return MemberTarget(name, inst, MemberKind.Instance, mirrorClassScope = stat != null)
|
||||||
|
}
|
||||||
|
if (stat != null) return MemberTarget(name, stat, MemberKind.Static)
|
||||||
|
throw ScriptError(Pos.builtIn, "extern member $name not found in class ${cls.className}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeFieldInitializersFor(name: String) {
|
||||||
|
if (cls.instanceInitializers.isEmpty()) return
|
||||||
|
val storageName = cls.mangledName(name)
|
||||||
|
cls.instanceInitializers.removeAll { init ->
|
||||||
|
val stmt = init as? Statement ?: return@removeAll false
|
||||||
|
val original = (stmt as? BytecodeStatement)?.original ?: stmt
|
||||||
|
original is InstanceFieldInitStatement && original.storageName == storageName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ObjectBridgeBinderImpl(
|
||||||
|
private val instance: ObjInstance
|
||||||
|
) : ClassBridgeBinder {
|
||||||
|
private val cls: ObjClass = instance.objClass
|
||||||
|
private val initHooks = mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>()
|
||||||
|
|
||||||
|
override var classData: Any?
|
||||||
|
get() = cls.kotlinClassData
|
||||||
|
set(value) { cls.kotlinClassData = value }
|
||||||
|
|
||||||
|
override fun init(block: suspend BridgeInstanceContext.(ScopeFacade) -> Unit) {
|
||||||
|
initHooks.add { scope, inst ->
|
||||||
|
val ctx = BridgeInstanceContextImpl(inst)
|
||||||
|
ctx.block(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun initWithInstance(block: suspend (ScopeFacade, Obj) -> Unit) {
|
||||||
|
initHooks.add { scope, inst ->
|
||||||
|
block(scope, inst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addFun(name: String, impl: suspend (ScopeFacade, Obj, Arguments) -> Obj) {
|
||||||
|
val target = findMember(name)
|
||||||
|
val callable = ObjExternCallable.fromBridge {
|
||||||
|
impl(this, thisObj, args)
|
||||||
|
}
|
||||||
|
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||||
|
val newRecord = target.record.copy(
|
||||||
|
value = callable,
|
||||||
|
type = ObjRecord.Type.Fun,
|
||||||
|
methodId = methodId
|
||||||
|
)
|
||||||
|
replaceMember(target, newRecord)
|
||||||
|
updateInstanceMember(target, newRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addVal(name: String, impl: suspend (ScopeFacade, Obj) -> Obj) {
|
||||||
|
val target = findMember(name)
|
||||||
|
if (target.record.isMutable) {
|
||||||
|
throw ScriptError(Pos.builtIn, "extern val $name is mutable in class ${cls.className}")
|
||||||
|
}
|
||||||
|
val getter = ObjExternCallable.fromBridge {
|
||||||
|
impl(this, thisObj)
|
||||||
|
}
|
||||||
|
val prop = ObjProperty(name, getter, null)
|
||||||
|
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||||
|
target.record.type == ObjRecord.Type.ConstructorField
|
||||||
|
val newRecord = if (isFieldLike) {
|
||||||
|
removeFieldInitializersFor(name)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = target.record.type,
|
||||||
|
fieldId = target.record.fieldId,
|
||||||
|
methodId = target.record.methodId
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = ObjRecord.Type.Property,
|
||||||
|
methodId = methodId,
|
||||||
|
fieldId = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
replaceMember(target, newRecord)
|
||||||
|
updateInstanceMember(target, newRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addVar(
|
||||||
|
name: String,
|
||||||
|
get: suspend (ScopeFacade, Obj) -> Obj,
|
||||||
|
set: suspend (ScopeFacade, Obj, Obj) -> Unit
|
||||||
|
) {
|
||||||
|
val target = findMember(name)
|
||||||
|
if (!target.record.isMutable) {
|
||||||
|
throw ScriptError(Pos.builtIn, "extern var $name is readonly in class ${cls.className}")
|
||||||
|
}
|
||||||
|
val getter = ObjExternCallable.fromBridge {
|
||||||
|
get(this, thisObj)
|
||||||
|
}
|
||||||
|
val setter = ObjExternCallable.fromBridge {
|
||||||
|
val value = requiredArg<Obj>(0)
|
||||||
|
set(this, thisObj, value)
|
||||||
|
ObjVoid
|
||||||
|
}
|
||||||
|
val prop = ObjProperty(name, getter, setter)
|
||||||
|
val isFieldLike = target.record.type == ObjRecord.Type.Field ||
|
||||||
|
target.record.type == ObjRecord.Type.ConstructorField
|
||||||
|
val newRecord = if (isFieldLike) {
|
||||||
|
removeFieldInitializersFor(name)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = target.record.type,
|
||||||
|
fieldId = target.record.fieldId,
|
||||||
|
methodId = target.record.methodId
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val methodId = cls.ensureMethodIdForBridge(name, target.record)
|
||||||
|
target.record.copy(
|
||||||
|
value = prop,
|
||||||
|
type = ObjRecord.Type.Property,
|
||||||
|
methodId = methodId,
|
||||||
|
fieldId = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
replaceMember(target, newRecord)
|
||||||
|
updateInstanceMember(target, newRecord)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun commit() {
|
||||||
|
if (initHooks.isNotEmpty()) {
|
||||||
|
val target = cls.bridgeInitHooks ?: mutableListOf<suspend (ScopeFacade, ObjInstance) -> Unit>().also {
|
||||||
|
cls.bridgeInitHooks = it
|
||||||
|
}
|
||||||
|
target.addAll(initHooks)
|
||||||
|
val facade = instance.instanceScope.asFacade()
|
||||||
|
for (hook in initHooks) {
|
||||||
|
hook(facade, instance)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun replaceMember(target: MemberTarget, newRecord: ObjRecord) {
|
||||||
|
when (target.kind) {
|
||||||
|
MemberKind.Instance -> {
|
||||||
|
cls.replaceMemberForBridge(target.name, newRecord)
|
||||||
|
if (target.mirrorClassScope && cls.classScope?.objects?.containsKey(target.name) == true) {
|
||||||
|
cls.replaceClassScopeMemberForBridge(target.name, newRecord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MemberKind.Static -> cls.replaceClassScopeMemberForBridge(target.name, newRecord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateInstanceMember(target: MemberTarget, newRecord: ObjRecord) {
|
||||||
|
val key = instanceStorageKey(target, newRecord) ?: return
|
||||||
|
ensureInstanceSlotCapacity()
|
||||||
|
instance.instanceScope.objects[key] = newRecord
|
||||||
|
cls.fieldSlotForKey(key)?.let { slot ->
|
||||||
|
instance.setFieldSlotRecord(slot.slot, newRecord)
|
||||||
|
}
|
||||||
|
cls.methodSlotForKey(key)?.let { slot ->
|
||||||
|
instance.setMethodSlotRecord(slot.slot, newRecord)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun instanceStorageKey(target: MemberTarget, rec: ObjRecord): String? = when (target.kind) {
|
||||||
|
MemberKind.Instance -> {
|
||||||
|
if (rec.visibility == Visibility.Private ||
|
||||||
|
rec.type == ObjRecord.Type.Field ||
|
||||||
|
rec.type == ObjRecord.Type.ConstructorField ||
|
||||||
|
rec.type == ObjRecord.Type.Delegated) {
|
||||||
|
cls.mangledName(target.name)
|
||||||
|
} else {
|
||||||
|
target.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MemberKind.Static -> {
|
||||||
|
if (rec.type != ObjRecord.Type.Fun &&
|
||||||
|
rec.type != ObjRecord.Type.Property &&
|
||||||
|
rec.type != ObjRecord.Type.Delegated) {
|
||||||
|
null
|
||||||
|
} else if (rec.visibility == Visibility.Private || rec.type == ObjRecord.Type.Delegated) {
|
||||||
|
cls.mangledName(target.name)
|
||||||
|
} else {
|
||||||
|
target.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun ensureInstanceSlotCapacity() {
|
||||||
|
val fieldCount = cls.fieldSlotCount()
|
||||||
|
if (instance.fieldSlots.size < fieldCount) {
|
||||||
|
val newSlots = arrayOfNulls<ObjRecord>(fieldCount)
|
||||||
|
instance.fieldSlots.copyInto(newSlots, 0, 0, instance.fieldSlots.size)
|
||||||
|
instance.fieldSlots = newSlots
|
||||||
|
}
|
||||||
|
val methodCount = cls.methodSlotCount()
|
||||||
|
if (instance.methodSlots.size < methodCount) {
|
||||||
|
val newSlots = arrayOfNulls<ObjRecord>(methodCount)
|
||||||
|
instance.methodSlots.copyInto(newSlots, 0, 0, instance.methodSlots.size)
|
||||||
|
instance.methodSlots = newSlots
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findMember(name: String): MemberTarget {
|
||||||
|
val inst = cls.members[name]
|
||||||
|
val stat = cls.classScope?.objects?.get(name)
|
||||||
|
if (inst != null) {
|
||||||
|
return MemberTarget(name, inst, MemberKind.Instance, mirrorClassScope = stat != null)
|
||||||
|
}
|
||||||
|
if (stat != null) return MemberTarget(name, stat, MemberKind.Static)
|
||||||
|
throw ScriptError(Pos.builtIn, "extern member $name not found in class ${cls.className}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeFieldInitializersFor(name: String) {
|
||||||
|
if (cls.instanceInitializers.isEmpty()) return
|
||||||
|
val storageName = cls.mangledName(name)
|
||||||
|
cls.instanceInitializers.removeAll { init ->
|
||||||
|
val stmt = init as? Statement ?: return@removeAll false
|
||||||
|
val original = (stmt as? BytecodeStatement)?.original ?: stmt
|
||||||
|
original is InstanceFieldInitStatement && original.storageName == storageName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun resolveClass(
|
||||||
|
className: String,
|
||||||
|
module: String?,
|
||||||
|
moduleScope: ModuleScope?,
|
||||||
|
importManager: ImportManager
|
||||||
|
): ObjClass {
|
||||||
|
val scope = moduleScope ?: run {
|
||||||
|
if (module == null) {
|
||||||
|
throw ScriptError(Pos.builtIn, "module is required to resolve $className")
|
||||||
|
}
|
||||||
|
importManager.createModuleScope(Pos.builtIn, module)
|
||||||
|
}
|
||||||
|
val rec = scope.get(className)
|
||||||
|
val direct = rec?.value as? ObjClass
|
||||||
|
if (direct != null) return direct
|
||||||
|
if (className.contains('.')) {
|
||||||
|
val resolved = scope.resolveQualifiedIdentifier(className)
|
||||||
|
val cls = resolved as? ObjClass
|
||||||
|
if (cls != null) return cls
|
||||||
|
}
|
||||||
|
throw ScriptError(Pos.builtIn, "class $className not found in module ${scope.packageName}")
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun resolveObject(
|
||||||
|
objectName: String,
|
||||||
|
module: String?,
|
||||||
|
moduleScope: ModuleScope?,
|
||||||
|
importManager: ImportManager
|
||||||
|
): ObjInstance {
|
||||||
|
val scope = moduleScope ?: run {
|
||||||
|
if (module == null) {
|
||||||
|
throw ScriptError(Pos.builtIn, "module is required to resolve $objectName")
|
||||||
|
}
|
||||||
|
importManager.createModuleScope(Pos.builtIn, module)
|
||||||
|
}
|
||||||
|
val rec = scope.get(objectName)
|
||||||
|
val direct = rec?.value as? ObjInstance
|
||||||
|
if (direct != null) return direct
|
||||||
|
if (objectName.contains('.')) {
|
||||||
|
val resolved = scope.resolveQualifiedIdentifier(objectName)
|
||||||
|
val inst = resolved as? ObjInstance
|
||||||
|
if (inst != null) return inst
|
||||||
|
}
|
||||||
|
throw ScriptError(Pos.builtIn, "object $objectName not found in module ${scope.packageName}")
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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.bytecode
|
||||||
|
|
||||||
|
import net.sergeych.lyng.Pos
|
||||||
|
|
||||||
|
class BytecodeCompileException(
|
||||||
|
message: String,
|
||||||
|
val pos: Pos? = null,
|
||||||
|
) : RuntimeException(message) {
|
||||||
|
override fun toString(): String =
|
||||||
|
pos?.let { "${super.toString()} at $it" } ?: super.toString()
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
import net.sergeych.lyng.ArgsDeclaration
|
||||||
|
import net.sergeych.lyng.Pos
|
||||||
|
import net.sergeych.lyng.TypeDecl
|
||||||
|
import net.sergeych.lyng.Visibility
|
||||||
|
import net.sergeych.lyng.obj.ListLiteralRef
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjProperty
|
||||||
|
|
||||||
|
sealed class BytecodeConst {
|
||||||
|
object Null : BytecodeConst()
|
||||||
|
data class Bool(val value: Boolean) : BytecodeConst()
|
||||||
|
data class IntVal(val value: Long) : BytecodeConst()
|
||||||
|
data class RealVal(val value: Double) : BytecodeConst()
|
||||||
|
data class StringVal(val value: String) : BytecodeConst()
|
||||||
|
data class PosVal(val pos: Pos) : BytecodeConst()
|
||||||
|
data class ObjRef(val value: Obj) : BytecodeConst()
|
||||||
|
data class ListLiteralPlan(val spreads: List<Boolean>) : BytecodeConst()
|
||||||
|
data class LambdaFn(
|
||||||
|
val fn: CmdFunction,
|
||||||
|
val captureTableId: Int?,
|
||||||
|
val captureNames: List<String>,
|
||||||
|
val paramSlotPlan: Map<String, Int>,
|
||||||
|
val argsDeclaration: ArgsDeclaration?,
|
||||||
|
val preferredThisType: String?,
|
||||||
|
val wrapAsExtensionCallable: Boolean,
|
||||||
|
val returnLabels: Set<String>,
|
||||||
|
val pos: Pos,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class EnumDecl(
|
||||||
|
val declaredName: String,
|
||||||
|
val qualifiedName: String,
|
||||||
|
val entries: List<String>,
|
||||||
|
val lifted: Boolean,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class FunctionDecl(
|
||||||
|
val spec: net.sergeych.lyng.FunctionDeclSpec,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassDecl(
|
||||||
|
val spec: net.sergeych.lyng.ClassDeclSpec,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class SlotPlan(val plan: Map<String, Int>, val captures: List<String> = emptyList()) : BytecodeConst()
|
||||||
|
data class CaptureTable(val entries: List<BytecodeCaptureEntry>) : BytecodeConst()
|
||||||
|
data class ExtensionPropertyDecl(
|
||||||
|
val extTypeName: String,
|
||||||
|
val property: ObjProperty,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val setterVisibility: Visibility?,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class LocalDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val typeDecl: TypeDecl?,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class DelegatedDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassFieldDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassDelegatedDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassInstanceInitDecl(
|
||||||
|
val initStatement: Obj,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassInstanceFieldDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val fieldId: Int?,
|
||||||
|
val initStatement: Obj?,
|
||||||
|
val pos: Pos,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassInstancePropertyDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val prop: ObjProperty,
|
||||||
|
val methodId: Int?,
|
||||||
|
val initStatement: Obj?,
|
||||||
|
val pos: Pos,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class ClassInstanceDelegatedDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val methodId: Int?,
|
||||||
|
val initStatement: Obj?,
|
||||||
|
val pos: Pos,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class InstanceFieldDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class InstancePropertyDecl(
|
||||||
|
val name: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class InstanceDelegatedDecl(
|
||||||
|
val storageName: String,
|
||||||
|
val memberName: String,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val writeVisibility: Visibility?,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val isAbstract: Boolean,
|
||||||
|
val isClosed: Boolean,
|
||||||
|
val isOverride: Boolean,
|
||||||
|
val accessTypeLabel: String,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class DestructureDecl(
|
||||||
|
val pattern: ListLiteralRef,
|
||||||
|
val names: List<String>,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val visibility: Visibility,
|
||||||
|
val isTransient: Boolean,
|
||||||
|
val pos: Pos,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class DestructureAssign(
|
||||||
|
val pattern: ListLiteralRef,
|
||||||
|
val pos: Pos,
|
||||||
|
) : BytecodeConst()
|
||||||
|
data class CallArgsPlan(val tailBlock: Boolean, val specs: List<CallArgSpec>) : BytecodeConst()
|
||||||
|
data class CallArgSpec(val name: String?, val isSplat: Boolean)
|
||||||
|
}
|
||||||
@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
import net.sergeych.lyng.FrameAccess
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjNull
|
||||||
|
|
||||||
|
class BytecodeFrame(
|
||||||
|
val localCount: Int,
|
||||||
|
val argCount: Int,
|
||||||
|
) : FrameAccess {
|
||||||
|
val slotCount: Int = localCount + argCount
|
||||||
|
val argBase: Int = localCount
|
||||||
|
|
||||||
|
private val slotTypes: ByteArray = ByteArray(slotCount) { SlotType.UNKNOWN.code }
|
||||||
|
private val objSlots: Array<Obj?> = arrayOfNulls(slotCount)
|
||||||
|
private val intSlots: LongArray = LongArray(slotCount)
|
||||||
|
private val realSlots: DoubleArray = DoubleArray(slotCount)
|
||||||
|
private val boolSlots: BooleanArray = BooleanArray(slotCount)
|
||||||
|
|
||||||
|
internal fun copyTo(target: BytecodeFrame) {
|
||||||
|
val limit = minOf(slotCount, target.slotCount)
|
||||||
|
for (i in 0 until limit) {
|
||||||
|
target.slotTypes[i] = slotTypes[i]
|
||||||
|
target.objSlots[i] = objSlots[i]
|
||||||
|
target.intSlots[i] = intSlots[i]
|
||||||
|
target.realSlots[i] = realSlots[i]
|
||||||
|
target.boolSlots[i] = boolSlots[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getSlotType(slot: Int): SlotType = SlotType.values().first { it.code == slotTypes[slot] }
|
||||||
|
override fun getSlotTypeCode(slot: Int): Byte = slotTypes[slot]
|
||||||
|
fun setSlotType(slot: Int, type: SlotType) {
|
||||||
|
slotTypes[slot] = type.code
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getObj(slot: Int): Obj {
|
||||||
|
val value = objSlots[slot] ?: return ObjNull
|
||||||
|
return when (value) {
|
||||||
|
is net.sergeych.lyng.FrameSlotRef -> value.read()
|
||||||
|
is net.sergeych.lyng.RecordSlotRef -> value.read()
|
||||||
|
else -> value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun getRawObj(slot: Int): Obj? = objSlots[slot]
|
||||||
|
|
||||||
|
override fun setObj(slot: Int, value: Obj) {
|
||||||
|
when (val current = objSlots[slot]) {
|
||||||
|
is net.sergeych.lyng.FrameSlotRef -> {
|
||||||
|
if (current.refersTo(this, slot)) {
|
||||||
|
objSlots[slot] = value
|
||||||
|
} else {
|
||||||
|
current.write(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.RecordSlotRef -> current.write(value)
|
||||||
|
else -> objSlots[slot] = value
|
||||||
|
}
|
||||||
|
slotTypes[slot] = SlotType.OBJ.code
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getInt(slot: Int): Long = intSlots[slot]
|
||||||
|
override fun setInt(slot: Int, value: Long) {
|
||||||
|
intSlots[slot] = value
|
||||||
|
slotTypes[slot] = SlotType.INT.code
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getReal(slot: Int): Double = realSlots[slot]
|
||||||
|
override fun setReal(slot: Int, value: Double) {
|
||||||
|
realSlots[slot] = value
|
||||||
|
slotTypes[slot] = SlotType.REAL.code
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getBool(slot: Int): Boolean = boolSlots[slot]
|
||||||
|
override fun setBool(slot: Int, value: Boolean) {
|
||||||
|
boolSlots[slot] = value
|
||||||
|
slotTypes[slot] = SlotType.BOOL.code
|
||||||
|
}
|
||||||
|
|
||||||
|
fun clearSlot(slot: Int) {
|
||||||
|
slotTypes[slot] = SlotType.UNKNOWN.code
|
||||||
|
objSlots[slot] = null
|
||||||
|
intSlots[slot] = 0L
|
||||||
|
realSlots[slot] = 0.0
|
||||||
|
boolSlots[slot] = false
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,448 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
import net.sergeych.lyng.*
|
||||||
|
import net.sergeych.lyng.obj.Obj
|
||||||
|
import net.sergeych.lyng.obj.ObjClass
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
import net.sergeych.lyng.obj.ValueFnRef
|
||||||
|
|
||||||
|
class BytecodeStatement private constructor(
|
||||||
|
val original: Statement,
|
||||||
|
private val function: CmdFunction,
|
||||||
|
) : Statement(original.isStaticConst, original.isConst, original.returnType) {
|
||||||
|
override val pos: Pos = original.pos
|
||||||
|
|
||||||
|
override suspend fun execute(scope: Scope): Obj {
|
||||||
|
scope.pos = pos
|
||||||
|
val declaredNames = function.constants
|
||||||
|
.mapNotNull { it as? BytecodeConst.LocalDecl }
|
||||||
|
.mapTo(mutableSetOf()) { it.name }
|
||||||
|
val binder: suspend (CmdFrame, Arguments) -> Unit = { frame, _ ->
|
||||||
|
val localNames = frame.fn.localSlotNames
|
||||||
|
for (i in localNames.indices) {
|
||||||
|
val name = localNames[i] ?: continue
|
||||||
|
if (declaredNames.contains(name)) continue
|
||||||
|
val slotType = frame.getLocalSlotTypeCode(i)
|
||||||
|
if (slotType != SlotType.UNKNOWN.code && slotType != SlotType.OBJ.code) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (slotType == SlotType.OBJ.code && frame.frame.getRawObj(i) != null) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val record = scope.getLocalRecordDirect(name)
|
||||||
|
?: scope.parent?.get(name)
|
||||||
|
?: scope.get(name)
|
||||||
|
?: continue
|
||||||
|
val value = if (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property) {
|
||||||
|
scope.resolve(record, name)
|
||||||
|
} else {
|
||||||
|
record.value
|
||||||
|
}
|
||||||
|
frame.frame.setObj(i, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CmdVm().execute(function, scope, scope.args, binder)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun bytecodeFunction(): CmdFunction = function
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun wrap(
|
||||||
|
statement: Statement,
|
||||||
|
nameHint: String,
|
||||||
|
allowLocalSlots: Boolean,
|
||||||
|
returnLabels: Set<String> = emptySet(),
|
||||||
|
rangeLocalNames: Set<String> = emptySet(),
|
||||||
|
allowedScopeNames: Set<String>? = null,
|
||||||
|
scopeSlotNameSet: Set<String>? = null,
|
||||||
|
moduleScopeId: Int? = null,
|
||||||
|
forcedLocalSlots: Map<String, Int> = emptyMap(),
|
||||||
|
forcedLocalScopeId: Int? = null,
|
||||||
|
forcedLocalSlotInfo: Map<String, ForcedLocalSlotInfo> = emptyMap(),
|
||||||
|
globalSlotInfo: Map<String, ForcedLocalSlotInfo> = emptyMap(),
|
||||||
|
globalSlotScopeId: Int? = null,
|
||||||
|
slotTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(),
|
||||||
|
knownNameObjClass: Map<String, ObjClass> = emptyMap(),
|
||||||
|
knownClassNames: Set<String> = emptySet(),
|
||||||
|
knownObjectNames: Set<String> = emptySet(),
|
||||||
|
classFieldTypesByName: Map<String, Map<String, ObjClass>> = emptyMap(),
|
||||||
|
enumEntriesByName: Map<String, List<String>> = emptyMap(),
|
||||||
|
callableReturnTypeByScopeId: Map<Int, Map<Int, ObjClass>> = emptyMap(),
|
||||||
|
callableReturnTypeByName: Map<String, ObjClass> = emptyMap(),
|
||||||
|
externCallableNames: Set<String> = emptySet(),
|
||||||
|
lambdaCaptureEntriesByRef: Map<ValueFnRef, List<LambdaCaptureEntry>> = emptyMap(),
|
||||||
|
slotTypeDeclByScopeId: Map<Int, Map<Int, TypeDecl>> = emptyMap(),
|
||||||
|
): Statement {
|
||||||
|
if (statement is BytecodeStatement) return statement
|
||||||
|
val hasUnsupported = containsUnsupportedStatement(statement)
|
||||||
|
if (hasUnsupported) {
|
||||||
|
val statementName = statement.toString()
|
||||||
|
throw BytecodeCompileException(
|
||||||
|
"Bytecode compile error: unsupported statement $statementName in '$nameHint'",
|
||||||
|
statement.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val safeLocals = allowLocalSlots
|
||||||
|
val compiler = BytecodeCompiler(
|
||||||
|
allowLocalSlots = safeLocals,
|
||||||
|
returnLabels = returnLabels,
|
||||||
|
rangeLocalNames = rangeLocalNames,
|
||||||
|
allowedScopeNames = allowedScopeNames,
|
||||||
|
scopeSlotNameSet = scopeSlotNameSet,
|
||||||
|
moduleScopeId = moduleScopeId,
|
||||||
|
forcedLocalSlots = forcedLocalSlots,
|
||||||
|
forcedLocalScopeId = forcedLocalScopeId,
|
||||||
|
forcedLocalSlotInfo = forcedLocalSlotInfo,
|
||||||
|
globalSlotInfo = globalSlotInfo,
|
||||||
|
globalSlotScopeId = globalSlotScopeId,
|
||||||
|
slotTypeByScopeId = slotTypeByScopeId,
|
||||||
|
slotTypeDeclByScopeId = slotTypeDeclByScopeId,
|
||||||
|
knownNameObjClass = knownNameObjClass,
|
||||||
|
knownClassNames = knownClassNames,
|
||||||
|
knownObjectNames = knownObjectNames,
|
||||||
|
classFieldTypesByName = classFieldTypesByName,
|
||||||
|
enumEntriesByName = enumEntriesByName,
|
||||||
|
callableReturnTypeByScopeId = callableReturnTypeByScopeId,
|
||||||
|
callableReturnTypeByName = callableReturnTypeByName,
|
||||||
|
externCallableNames = externCallableNames,
|
||||||
|
lambdaCaptureEntriesByRef = lambdaCaptureEntriesByRef
|
||||||
|
)
|
||||||
|
val compiled = compiler.compileStatement(nameHint, statement)
|
||||||
|
val fn = compiled ?: throw BytecodeCompileException(
|
||||||
|
"Bytecode compile error: failed to compile '$nameHint'",
|
||||||
|
statement.pos
|
||||||
|
)
|
||||||
|
return BytecodeStatement(statement, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun containsUnsupportedStatement(stmt: Statement): Boolean {
|
||||||
|
val target = if (stmt is BytecodeStatement) stmt.original else stmt
|
||||||
|
return when (target) {
|
||||||
|
is net.sergeych.lyng.ExpressionStatement -> {
|
||||||
|
val ref = target.ref
|
||||||
|
if (ref is net.sergeych.lyng.obj.StatementRef) {
|
||||||
|
containsUnsupportedStatement(ref.statement)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.IfStatement -> {
|
||||||
|
containsUnsupportedStatement(target.condition) ||
|
||||||
|
containsUnsupportedStatement(target.ifBody) ||
|
||||||
|
(target.elseBody?.let { containsUnsupportedStatement(it) } ?: false)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ForInStatement -> {
|
||||||
|
val unsupported = containsUnsupportedStatement(target.source) ||
|
||||||
|
containsUnsupportedStatement(target.body) ||
|
||||||
|
(target.elseStatement?.let { containsUnsupportedStatement(it) } ?: false)
|
||||||
|
unsupported
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.WhileStatement -> {
|
||||||
|
containsUnsupportedStatement(target.condition) ||
|
||||||
|
containsUnsupportedStatement(target.body) ||
|
||||||
|
(target.elseStatement?.let { containsUnsupportedStatement(it) } ?: false)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.DoWhileStatement -> {
|
||||||
|
containsUnsupportedStatement(target.body) ||
|
||||||
|
containsUnsupportedStatement(target.condition) ||
|
||||||
|
(target.elseStatement?.let { containsUnsupportedStatement(it) } ?: false)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.BlockStatement ->
|
||||||
|
target.statements().any { containsUnsupportedStatement(it) }
|
||||||
|
is net.sergeych.lyng.InlineBlockStatement ->
|
||||||
|
target.statements().any { containsUnsupportedStatement(it) }
|
||||||
|
is net.sergeych.lyng.VarDeclStatement ->
|
||||||
|
target.initializer?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.DelegatedVarDeclStatement ->
|
||||||
|
containsUnsupportedStatement(target.initializer)
|
||||||
|
is net.sergeych.lyng.DestructuringVarDeclStatement ->
|
||||||
|
containsUnsupportedStatement(target.initializer)
|
||||||
|
is net.sergeych.lyng.BreakStatement ->
|
||||||
|
target.resultExpr?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.ContinueStatement -> false
|
||||||
|
is net.sergeych.lyng.ReturnStatement ->
|
||||||
|
target.resultExpr?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.ThrowStatement ->
|
||||||
|
containsUnsupportedStatement(target.throwExpr)
|
||||||
|
is net.sergeych.lyng.NopStatement -> false
|
||||||
|
is net.sergeych.lyng.ExtensionPropertyDeclStatement -> false
|
||||||
|
is net.sergeych.lyng.ClassDeclStatement -> false
|
||||||
|
is net.sergeych.lyng.FunctionDeclStatement -> false
|
||||||
|
is net.sergeych.lyng.EnumDeclStatement -> false
|
||||||
|
is net.sergeych.lyng.ClassStaticFieldInitStatement ->
|
||||||
|
target.initializer?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.ClassInstanceInitDeclStatement ->
|
||||||
|
containsUnsupportedStatement(target.initStatement)
|
||||||
|
is net.sergeych.lyng.ClassInstanceFieldDeclStatement ->
|
||||||
|
target.initStatement?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.ClassInstancePropertyDeclStatement ->
|
||||||
|
target.initStatement?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.ClassInstanceDelegatedDeclStatement ->
|
||||||
|
target.initStatement?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.InstanceFieldInitStatement ->
|
||||||
|
target.initializer?.let { containsUnsupportedStatement(it) } ?: false
|
||||||
|
is net.sergeych.lyng.InstancePropertyInitStatement -> false
|
||||||
|
is net.sergeych.lyng.InstanceDelegatedInitStatement ->
|
||||||
|
containsUnsupportedStatement(target.initializer)
|
||||||
|
is net.sergeych.lyng.TryStatement -> {
|
||||||
|
containsUnsupportedStatement(target.body) ||
|
||||||
|
target.catches.any { containsUnsupportedStatement(it.block) } ||
|
||||||
|
(target.finallyClause?.let { containsUnsupportedStatement(it) } ?: false)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.WhenStatement -> {
|
||||||
|
containsUnsupportedStatement(target.value) ||
|
||||||
|
target.cases.any { case ->
|
||||||
|
case.conditions.any { cond -> containsUnsupportedStatement(cond.expr) } ||
|
||||||
|
containsUnsupportedStatement(case.block)
|
||||||
|
} ||
|
||||||
|
(target.elseCase?.let { containsUnsupportedStatement(it) } ?: false)
|
||||||
|
}
|
||||||
|
else -> true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun unwrapDeep(stmt: Statement): Statement {
|
||||||
|
return when (stmt) {
|
||||||
|
is BytecodeStatement -> unwrapDeep(stmt.original)
|
||||||
|
is net.sergeych.lyng.BlockStatement -> {
|
||||||
|
val unwrapped = stmt.statements().map { unwrapDeep(it) }
|
||||||
|
net.sergeych.lyng.BlockStatement(
|
||||||
|
net.sergeych.lyng.Script(stmt.pos, unwrapped),
|
||||||
|
stmt.slotPlan,
|
||||||
|
stmt.scopeId,
|
||||||
|
stmt.captureSlots,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.VarDeclStatement -> {
|
||||||
|
net.sergeych.lyng.VarDeclStatement(
|
||||||
|
stmt.name,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.initializer?.let { unwrapDeep(it) },
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.typeDecl,
|
||||||
|
stmt.slotIndex,
|
||||||
|
stmt.scopeId,
|
||||||
|
stmt.pos,
|
||||||
|
stmt.initializerObjClass
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.DestructuringVarDeclStatement -> {
|
||||||
|
net.sergeych.lyng.DestructuringVarDeclStatement(
|
||||||
|
stmt.pattern,
|
||||||
|
stmt.names,
|
||||||
|
unwrapDeep(stmt.initializer),
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.IfStatement -> {
|
||||||
|
net.sergeych.lyng.IfStatement(
|
||||||
|
unwrapDeep(stmt.condition),
|
||||||
|
unwrapDeep(stmt.ifBody),
|
||||||
|
stmt.elseBody?.let { unwrapDeep(it) },
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ForInStatement -> {
|
||||||
|
net.sergeych.lyng.ForInStatement(
|
||||||
|
stmt.loopVarName,
|
||||||
|
unwrapDeep(stmt.source),
|
||||||
|
stmt.constRange,
|
||||||
|
unwrapDeep(stmt.body),
|
||||||
|
stmt.elseStatement?.let { unwrapDeep(it) },
|
||||||
|
stmt.label,
|
||||||
|
stmt.canBreak,
|
||||||
|
stmt.loopSlotPlan,
|
||||||
|
stmt.loopScopeId,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.WhileStatement -> {
|
||||||
|
net.sergeych.lyng.WhileStatement(
|
||||||
|
unwrapDeep(stmt.condition),
|
||||||
|
unwrapDeep(stmt.body),
|
||||||
|
stmt.elseStatement?.let { unwrapDeep(it) },
|
||||||
|
stmt.label,
|
||||||
|
stmt.canBreak,
|
||||||
|
stmt.loopSlotPlan,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.DoWhileStatement -> {
|
||||||
|
net.sergeych.lyng.DoWhileStatement(
|
||||||
|
unwrapDeep(stmt.body),
|
||||||
|
unwrapDeep(stmt.condition),
|
||||||
|
stmt.elseStatement?.let { unwrapDeep(it) },
|
||||||
|
stmt.label,
|
||||||
|
stmt.loopSlotPlan,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.BreakStatement -> {
|
||||||
|
val resultExpr = stmt.resultExpr?.let { unwrapDeep(it) }
|
||||||
|
net.sergeych.lyng.BreakStatement(stmt.label, resultExpr, stmt.pos)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ContinueStatement ->
|
||||||
|
net.sergeych.lyng.ContinueStatement(stmt.label, stmt.pos)
|
||||||
|
is net.sergeych.lyng.ReturnStatement -> {
|
||||||
|
val resultExpr = stmt.resultExpr?.let { unwrapDeep(it) }
|
||||||
|
net.sergeych.lyng.ReturnStatement(stmt.label, resultExpr, stmt.pos)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ThrowStatement ->
|
||||||
|
net.sergeych.lyng.ThrowStatement(unwrapDeep(stmt.throwExpr), stmt.pos)
|
||||||
|
is net.sergeych.lyng.WhenStatement -> {
|
||||||
|
net.sergeych.lyng.WhenStatement(
|
||||||
|
unwrapDeep(stmt.value),
|
||||||
|
stmt.cases.map { case ->
|
||||||
|
net.sergeych.lyng.WhenCase(
|
||||||
|
case.conditions.map { unwrapWhenCondition(it) },
|
||||||
|
unwrapDeep(case.block)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
stmt.elseCase?.let { unwrapDeep(it) },
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ClassStaticFieldInitStatement -> {
|
||||||
|
net.sergeych.lyng.ClassStaticFieldInitStatement(
|
||||||
|
stmt.name,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.initializer?.let { unwrapDeep(it) },
|
||||||
|
stmt.isDelegated,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ClassInstanceInitDeclStatement -> {
|
||||||
|
net.sergeych.lyng.ClassInstanceInitDeclStatement(
|
||||||
|
unwrapDeep(stmt.initStatement),
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ClassInstanceFieldDeclStatement -> {
|
||||||
|
net.sergeych.lyng.ClassInstanceFieldDeclStatement(
|
||||||
|
stmt.name,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.isAbstract,
|
||||||
|
stmt.isClosed,
|
||||||
|
stmt.isOverride,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.fieldId,
|
||||||
|
stmt.initStatement?.let { unwrapDeep(it) },
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ClassInstancePropertyDeclStatement -> {
|
||||||
|
net.sergeych.lyng.ClassInstancePropertyDeclStatement(
|
||||||
|
stmt.name,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.isAbstract,
|
||||||
|
stmt.isClosed,
|
||||||
|
stmt.isOverride,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.prop,
|
||||||
|
stmt.methodId,
|
||||||
|
stmt.initStatement?.let { unwrapDeep(it) },
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.ClassInstanceDelegatedDeclStatement -> {
|
||||||
|
net.sergeych.lyng.ClassInstanceDelegatedDeclStatement(
|
||||||
|
stmt.name,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.isAbstract,
|
||||||
|
stmt.isClosed,
|
||||||
|
stmt.isOverride,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.methodId,
|
||||||
|
stmt.initStatement?.let { unwrapDeep(it) },
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.InstanceFieldInitStatement -> {
|
||||||
|
net.sergeych.lyng.InstanceFieldInitStatement(
|
||||||
|
stmt.storageName,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.isAbstract,
|
||||||
|
stmt.isClosed,
|
||||||
|
stmt.isOverride,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.isLateInitVal,
|
||||||
|
stmt.initializer?.let { unwrapDeep(it) },
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.InstancePropertyInitStatement -> {
|
||||||
|
net.sergeych.lyng.InstancePropertyInitStatement(
|
||||||
|
stmt.storageName,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.isAbstract,
|
||||||
|
stmt.isClosed,
|
||||||
|
stmt.isOverride,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.prop,
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
is net.sergeych.lyng.InstanceDelegatedInitStatement -> {
|
||||||
|
net.sergeych.lyng.InstanceDelegatedInitStatement(
|
||||||
|
stmt.storageName,
|
||||||
|
stmt.memberName,
|
||||||
|
stmt.isMutable,
|
||||||
|
stmt.visibility,
|
||||||
|
stmt.writeVisibility,
|
||||||
|
stmt.isAbstract,
|
||||||
|
stmt.isClosed,
|
||||||
|
stmt.isOverride,
|
||||||
|
stmt.isTransient,
|
||||||
|
stmt.accessTypeLabel,
|
||||||
|
unwrapDeep(stmt.initializer),
|
||||||
|
stmt.pos
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else -> stmt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun unwrapWhenCondition(cond: WhenCondition): WhenCondition {
|
||||||
|
return when (cond) {
|
||||||
|
is WhenEqualsCondition -> WhenEqualsCondition(unwrapDeep(cond.expr), cond.pos)
|
||||||
|
is WhenInCondition -> WhenInCondition(unwrapDeep(cond.expr), cond.negated, cond.pos)
|
||||||
|
is WhenIsCondition -> WhenIsCondition(unwrapDeep(cond.expr), cond.negated, cond.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,832 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
class CmdBuilder {
|
||||||
|
sealed interface Operand {
|
||||||
|
data class IntVal(val value: Int) : Operand
|
||||||
|
data class LabelRef(val label: Label) : Operand
|
||||||
|
}
|
||||||
|
|
||||||
|
data class Label(val id: Int)
|
||||||
|
|
||||||
|
data class Instr(val op: Opcode, val operands: List<Operand>)
|
||||||
|
|
||||||
|
private val instructions = mutableListOf<Instr>()
|
||||||
|
private val posByInstr = mutableListOf<net.sergeych.lyng.Pos?>()
|
||||||
|
private val constPool = mutableListOf<BytecodeConst>()
|
||||||
|
private val labelPositions = mutableMapOf<Label, Int>()
|
||||||
|
private var nextLabelId = 0
|
||||||
|
private var currentPos: net.sergeych.lyng.Pos? = null
|
||||||
|
|
||||||
|
fun addConst(c: BytecodeConst): Int {
|
||||||
|
constPool += c
|
||||||
|
return constPool.lastIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
fun emit(op: Opcode, vararg operands: Int) {
|
||||||
|
instructions += Instr(op, operands.map { Operand.IntVal(it) })
|
||||||
|
posByInstr += currentPos
|
||||||
|
}
|
||||||
|
|
||||||
|
fun emit(op: Opcode, operands: List<Operand>) {
|
||||||
|
instructions += Instr(op, operands)
|
||||||
|
posByInstr += currentPos
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setPos(pos: net.sergeych.lyng.Pos?) {
|
||||||
|
currentPos = pos
|
||||||
|
}
|
||||||
|
|
||||||
|
fun label(): Label = Label(nextLabelId++)
|
||||||
|
|
||||||
|
fun mark(label: Label) {
|
||||||
|
labelPositions[label] = instructions.size
|
||||||
|
}
|
||||||
|
|
||||||
|
fun build(
|
||||||
|
name: String,
|
||||||
|
localCount: Int,
|
||||||
|
addrCount: Int = 0,
|
||||||
|
returnLabels: Set<String> = emptySet(),
|
||||||
|
scopeSlotIndices: IntArray = IntArray(0),
|
||||||
|
scopeSlotNames: Array<String?> = emptyArray(),
|
||||||
|
scopeSlotIsModule: BooleanArray = BooleanArray(0),
|
||||||
|
localSlotNames: Array<String?> = emptyArray(),
|
||||||
|
localSlotMutables: BooleanArray = BooleanArray(0),
|
||||||
|
localSlotDelegated: BooleanArray = BooleanArray(0),
|
||||||
|
localSlotCaptures: BooleanArray = BooleanArray(0)
|
||||||
|
): CmdFunction {
|
||||||
|
val scopeSlotCount = scopeSlotIndices.size
|
||||||
|
require(scopeSlotNames.isEmpty() || scopeSlotNames.size == scopeSlotCount) {
|
||||||
|
"scope slot name mapping size mismatch"
|
||||||
|
}
|
||||||
|
require(scopeSlotIsModule.isEmpty() || scopeSlotIsModule.size == scopeSlotCount) {
|
||||||
|
"scope slot module mapping size mismatch"
|
||||||
|
}
|
||||||
|
require(localSlotNames.size == localSlotMutables.size) { "local slot metadata size mismatch" }
|
||||||
|
require(localSlotNames.size == localSlotDelegated.size) { "local slot delegation size mismatch" }
|
||||||
|
require(localSlotNames.size == localSlotCaptures.size) { "local slot capture size mismatch" }
|
||||||
|
val labelIps = mutableMapOf<Label, Int>()
|
||||||
|
for ((label, idx) in labelPositions) {
|
||||||
|
labelIps[label] = idx
|
||||||
|
}
|
||||||
|
val cmds = ArrayList<Cmd>(instructions.size)
|
||||||
|
for (ins in instructions) {
|
||||||
|
val kinds = operandKinds(ins.op)
|
||||||
|
if (kinds.size != ins.operands.size) {
|
||||||
|
error("Operand count mismatch for ${ins.op}: expected ${kinds.size}, got ${ins.operands.size}")
|
||||||
|
}
|
||||||
|
val operands = IntArray(kinds.size)
|
||||||
|
for (i in kinds.indices) {
|
||||||
|
val operand = ins.operands[i]
|
||||||
|
val v = when (operand) {
|
||||||
|
is Operand.IntVal -> operand.value
|
||||||
|
is Operand.LabelRef -> labelIps[operand.label]
|
||||||
|
?: error("Unknown label ${operand.label.id} for ${ins.op}")
|
||||||
|
}
|
||||||
|
operands[i] = v
|
||||||
|
}
|
||||||
|
cmds.add(createCmd(ins.op, operands, scopeSlotCount, localSlotCaptures))
|
||||||
|
}
|
||||||
|
return CmdFunction(
|
||||||
|
name = name,
|
||||||
|
localCount = localCount,
|
||||||
|
addrCount = addrCount,
|
||||||
|
returnLabels = returnLabels,
|
||||||
|
scopeSlotCount = scopeSlotCount,
|
||||||
|
scopeSlotIndices = scopeSlotIndices,
|
||||||
|
scopeSlotNames = if (scopeSlotNames.isEmpty()) Array(scopeSlotCount) { null } else scopeSlotNames,
|
||||||
|
scopeSlotIsModule = if (scopeSlotIsModule.isEmpty()) BooleanArray(scopeSlotCount) else scopeSlotIsModule,
|
||||||
|
localSlotNames = localSlotNames,
|
||||||
|
localSlotMutables = localSlotMutables,
|
||||||
|
localSlotDelegated = localSlotDelegated,
|
||||||
|
localSlotCaptures = localSlotCaptures,
|
||||||
|
constants = constPool.toList(),
|
||||||
|
cmds = cmds.toTypedArray(),
|
||||||
|
posByIp = posByInstr.toTypedArray()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun operandKinds(op: Opcode): List<OperandKind> {
|
||||||
|
return when (op) {
|
||||||
|
Opcode.NOP, Opcode.RET_VOID, Opcode.POP_SCOPE, Opcode.POP_SLOT_PLAN, Opcode.POP_TRY,
|
||||||
|
Opcode.CLEAR_PENDING_THROWABLE, Opcode.RETHROW_PENDING -> emptyList()
|
||||||
|
Opcode.MOVE_OBJ, Opcode.MOVE_INT, Opcode.MOVE_REAL, Opcode.MOVE_BOOL, Opcode.BOX_OBJ,
|
||||||
|
Opcode.UNBOX_INT_OBJ, Opcode.UNBOX_REAL_OBJ,
|
||||||
|
Opcode.INT_TO_REAL, Opcode.REAL_TO_INT, Opcode.BOOL_TO_INT, Opcode.INT_TO_BOOL,
|
||||||
|
Opcode.OBJ_TO_BOOL, Opcode.GET_OBJ_CLASS,
|
||||||
|
Opcode.NEG_INT, Opcode.NEG_REAL, Opcode.NOT_BOOL, Opcode.INV_INT,
|
||||||
|
Opcode.ASSERT_IS ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.CHECK_IS, Opcode.MAKE_QUALIFIED_VIEW ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.RANGE_INT_BOUNDS ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.RET_LABEL, Opcode.THROW ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.RESOLVE_SCOPE_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ADDR)
|
||||||
|
Opcode.DELEGATED_GET_LOCAL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.DELEGATED_SET_LOCAL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.BIND_DELEGATE_LOCAL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.LOAD_OBJ_ADDR, Opcode.LOAD_INT_ADDR, Opcode.LOAD_REAL_ADDR, Opcode.LOAD_BOOL_ADDR ->
|
||||||
|
listOf(OperandKind.ADDR, OperandKind.SLOT)
|
||||||
|
Opcode.STORE_OBJ_ADDR, Opcode.STORE_INT_ADDR, Opcode.STORE_REAL_ADDR, Opcode.STORE_BOOL_ADDR ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ADDR)
|
||||||
|
Opcode.CONST_NULL ->
|
||||||
|
listOf(OperandKind.SLOT)
|
||||||
|
Opcode.CONST_OBJ, Opcode.CONST_INT, Opcode.CONST_REAL, Opcode.CONST_BOOL,
|
||||||
|
Opcode.MAKE_LAMBDA_FN ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.PUSH_SCOPE, Opcode.PUSH_SLOT_PLAN ->
|
||||||
|
listOf(OperandKind.CONST)
|
||||||
|
Opcode.PUSH_TRY ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.IP, OperandKind.IP)
|
||||||
|
Opcode.DECL_LOCAL, Opcode.DECL_EXT_PROPERTY, Opcode.DECL_DELEGATED, Opcode.DECL_DESTRUCTURE,
|
||||||
|
Opcode.DECL_ENUM, Opcode.DECL_FUNCTION, Opcode.DECL_CLASS, Opcode.DECL_CLASS_FIELD,
|
||||||
|
Opcode.DECL_CLASS_DELEGATED, Opcode.DECL_CLASS_INSTANCE_INIT, Opcode.DECL_CLASS_INSTANCE_FIELD,
|
||||||
|
Opcode.DECL_CLASS_INSTANCE_PROPERTY, Opcode.DECL_CLASS_INSTANCE_DELEGATED, Opcode.DECL_INSTANCE_FIELD,
|
||||||
|
Opcode.DECL_INSTANCE_PROPERTY, Opcode.DECL_INSTANCE_DELEGATED,
|
||||||
|
Opcode.ASSIGN_DESTRUCTURE ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.ADD_INT, Opcode.SUB_INT, Opcode.MUL_INT, Opcode.DIV_INT, Opcode.MOD_INT,
|
||||||
|
Opcode.ADD_REAL, Opcode.SUB_REAL, Opcode.MUL_REAL, Opcode.DIV_REAL,
|
||||||
|
Opcode.AND_INT, Opcode.OR_INT, Opcode.XOR_INT, Opcode.SHL_INT, Opcode.SHR_INT, Opcode.USHR_INT,
|
||||||
|
Opcode.CMP_EQ_INT, Opcode.CMP_NEQ_INT, Opcode.CMP_LT_INT, Opcode.CMP_LTE_INT,
|
||||||
|
Opcode.CMP_GT_INT, Opcode.CMP_GTE_INT,
|
||||||
|
Opcode.CMP_EQ_REAL, Opcode.CMP_NEQ_REAL, Opcode.CMP_LT_REAL, Opcode.CMP_LTE_REAL,
|
||||||
|
Opcode.CMP_GT_REAL, Opcode.CMP_GTE_REAL,
|
||||||
|
Opcode.CMP_EQ_BOOL, Opcode.CMP_NEQ_BOOL,
|
||||||
|
Opcode.CMP_EQ_INT_REAL, Opcode.CMP_EQ_REAL_INT, Opcode.CMP_LT_INT_REAL, Opcode.CMP_LT_REAL_INT,
|
||||||
|
Opcode.CMP_LTE_INT_REAL, Opcode.CMP_LTE_REAL_INT, Opcode.CMP_GT_INT_REAL, Opcode.CMP_GT_REAL_INT,
|
||||||
|
Opcode.CMP_GTE_INT_REAL, Opcode.CMP_GTE_REAL_INT, Opcode.CMP_NEQ_INT_REAL, Opcode.CMP_NEQ_REAL_INT,
|
||||||
|
Opcode.CMP_EQ_OBJ, Opcode.CMP_NEQ_OBJ, Opcode.CMP_REF_EQ_OBJ, Opcode.CMP_REF_NEQ_OBJ,
|
||||||
|
Opcode.CMP_EQ_STR, Opcode.CMP_NEQ_STR, Opcode.CMP_LT_STR, Opcode.CMP_LTE_STR,
|
||||||
|
Opcode.CMP_GT_STR, Opcode.CMP_GTE_STR,
|
||||||
|
Opcode.CMP_EQ_INT_OBJ, Opcode.CMP_NEQ_INT_OBJ, Opcode.CMP_LT_INT_OBJ, Opcode.CMP_LTE_INT_OBJ,
|
||||||
|
Opcode.CMP_GT_INT_OBJ, Opcode.CMP_GTE_INT_OBJ, Opcode.CMP_EQ_REAL_OBJ, Opcode.CMP_NEQ_REAL_OBJ,
|
||||||
|
Opcode.CMP_LT_REAL_OBJ, Opcode.CMP_LTE_REAL_OBJ, Opcode.CMP_GT_REAL_OBJ, Opcode.CMP_GTE_REAL_OBJ,
|
||||||
|
Opcode.CMP_LT_OBJ, Opcode.CMP_LTE_OBJ, Opcode.CMP_GT_OBJ, Opcode.CMP_GTE_OBJ,
|
||||||
|
Opcode.ADD_INT_OBJ, Opcode.SUB_INT_OBJ, Opcode.MUL_INT_OBJ, Opcode.DIV_INT_OBJ, Opcode.MOD_INT_OBJ,
|
||||||
|
Opcode.ADD_REAL_OBJ, Opcode.SUB_REAL_OBJ, Opcode.MUL_REAL_OBJ, Opcode.DIV_REAL_OBJ, Opcode.MOD_REAL_OBJ,
|
||||||
|
Opcode.ADD_OBJ, Opcode.SUB_OBJ, Opcode.MUL_OBJ, Opcode.DIV_OBJ, Opcode.MOD_OBJ, Opcode.CONTAINS_OBJ,
|
||||||
|
Opcode.AND_BOOL, Opcode.OR_BOOL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.ASSIGN_OP_OBJ ->
|
||||||
|
listOf(OperandKind.ID, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.CONST)
|
||||||
|
Opcode.INC_INT, Opcode.DEC_INT, Opcode.RET, Opcode.LOAD_THIS ->
|
||||||
|
listOf(OperandKind.SLOT)
|
||||||
|
Opcode.LOAD_THIS_VARIANT ->
|
||||||
|
listOf(OperandKind.ID, OperandKind.SLOT)
|
||||||
|
Opcode.JMP ->
|
||||||
|
listOf(OperandKind.IP)
|
||||||
|
Opcode.JMP_IF_TRUE, Opcode.JMP_IF_FALSE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.IP)
|
||||||
|
Opcode.JMP_IF_EQ_INT, Opcode.JMP_IF_NEQ_INT,
|
||||||
|
Opcode.JMP_IF_LT_INT, Opcode.JMP_IF_LTE_INT,
|
||||||
|
Opcode.JMP_IF_GT_INT, Opcode.JMP_IF_GTE_INT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.IP)
|
||||||
|
Opcode.CALL_DIRECT ->
|
||||||
|
listOf(OperandKind.ID, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.CALL_MEMBER_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ID, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.CALL_SLOT, Opcode.CALL_BRIDGE_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.CALL_DYNAMIC_MEMBER ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.GET_INDEX ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.SET_INDEX ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.MAKE_RANGE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.LIST_LITERAL ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.GET_MEMBER_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ID, OperandKind.ID, OperandKind.SLOT)
|
||||||
|
Opcode.SET_MEMBER_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ID, OperandKind.ID, OperandKind.SLOT)
|
||||||
|
Opcode.GET_CLASS_SCOPE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.SET_CLASS_SCOPE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.GET_DYNAMIC_MEMBER ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.SET_DYNAMIC_MEMBER ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.ITER_PUSH ->
|
||||||
|
listOf(OperandKind.SLOT)
|
||||||
|
Opcode.ITER_POP, Opcode.ITER_CANCEL ->
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum class OperandKind {
|
||||||
|
SLOT,
|
||||||
|
ADDR,
|
||||||
|
CONST,
|
||||||
|
IP,
|
||||||
|
COUNT,
|
||||||
|
ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createCmd(
|
||||||
|
op: Opcode,
|
||||||
|
operands: IntArray,
|
||||||
|
scopeSlotCount: Int,
|
||||||
|
localSlotCaptures: BooleanArray
|
||||||
|
): Cmd {
|
||||||
|
fun isFastLocal(slot: Int): Boolean {
|
||||||
|
if (slot < scopeSlotCount) return false
|
||||||
|
val localIndex = slot - scopeSlotCount
|
||||||
|
return localSlotCaptures.getOrNull(localIndex) != true
|
||||||
|
}
|
||||||
|
return when (op) {
|
||||||
|
Opcode.NOP -> CmdNop()
|
||||||
|
Opcode.MOVE_OBJ -> CmdMoveObj(operands[0], operands[1])
|
||||||
|
Opcode.MOVE_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdMoveIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMoveInt(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.MOVE_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdMoveRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMoveReal(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.MOVE_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdMoveBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMoveBool(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.CONST_OBJ -> CmdConstObj(operands[0], operands[1])
|
||||||
|
Opcode.CONST_INT -> if (isFastLocal(operands[1])) {
|
||||||
|
CmdConstIntLocal(operands[0], operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdConstInt(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.CONST_REAL -> CmdConstReal(operands[0], operands[1])
|
||||||
|
Opcode.CONST_BOOL -> CmdConstBool(operands[0], operands[1])
|
||||||
|
Opcode.CONST_NULL -> CmdConstNull(operands[0])
|
||||||
|
Opcode.MAKE_LAMBDA_FN -> CmdMakeLambda(operands[0], operands[1])
|
||||||
|
Opcode.BOX_OBJ -> CmdBoxObj(operands[0], operands[1])
|
||||||
|
Opcode.UNBOX_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdUnboxIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdUnboxIntObj(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.UNBOX_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdUnboxRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdUnboxRealObj(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.OBJ_TO_BOOL -> CmdObjToBool(operands[0], operands[1])
|
||||||
|
Opcode.GET_OBJ_CLASS -> CmdGetObjClass(operands[0], operands[1])
|
||||||
|
Opcode.RANGE_INT_BOUNDS -> CmdRangeIntBounds(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.LOAD_THIS -> CmdLoadThis(operands[0])
|
||||||
|
Opcode.LOAD_THIS_VARIANT -> CmdLoadThisVariant(operands[0], operands[1])
|
||||||
|
Opcode.MAKE_RANGE -> CmdMakeRange(operands[0], operands[1], operands[2], operands[3], operands[4])
|
||||||
|
Opcode.CHECK_IS -> CmdCheckIs(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.ASSERT_IS -> CmdAssertIs(operands[0], operands[1])
|
||||||
|
Opcode.MAKE_QUALIFIED_VIEW -> CmdMakeQualifiedView(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.RET_LABEL -> CmdRetLabel(operands[0], operands[1])
|
||||||
|
Opcode.THROW -> CmdThrow(operands[0], operands[1])
|
||||||
|
Opcode.RETHROW_PENDING -> CmdRethrowPending()
|
||||||
|
Opcode.RESOLVE_SCOPE_SLOT -> CmdResolveScopeSlot(operands[0], operands[1])
|
||||||
|
Opcode.DELEGATED_GET_LOCAL -> CmdDelegatedGetLocal(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.DELEGATED_SET_LOCAL -> CmdDelegatedSetLocal(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.BIND_DELEGATE_LOCAL -> CmdBindDelegateLocal(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.LOAD_OBJ_ADDR -> CmdLoadObjAddr(operands[0], operands[1])
|
||||||
|
Opcode.STORE_OBJ_ADDR -> CmdStoreObjAddr(operands[0], operands[1])
|
||||||
|
Opcode.LOAD_INT_ADDR -> CmdLoadIntAddr(operands[0], operands[1])
|
||||||
|
Opcode.STORE_INT_ADDR -> CmdStoreIntAddr(operands[0], operands[1])
|
||||||
|
Opcode.LOAD_REAL_ADDR -> CmdLoadRealAddr(operands[0], operands[1])
|
||||||
|
Opcode.STORE_REAL_ADDR -> CmdStoreRealAddr(operands[0], operands[1])
|
||||||
|
Opcode.LOAD_BOOL_ADDR -> CmdLoadBoolAddr(operands[0], operands[1])
|
||||||
|
Opcode.STORE_BOOL_ADDR -> CmdStoreBoolAddr(operands[0], operands[1])
|
||||||
|
Opcode.INT_TO_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdIntToRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdIntToReal(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.REAL_TO_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdRealToIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdRealToInt(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.BOOL_TO_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdBoolToIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdBoolToInt(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.INT_TO_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdIntToBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdIntToBool(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.ADD_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdAddIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdAddInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.SUB_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdSubIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdSubInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MUL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdMulIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMulInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.DIV_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdDivIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdDivInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MOD_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdModIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdModInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.NEG_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdNegIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdNegInt(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.INC_INT -> if (isFastLocal(operands[0])) {
|
||||||
|
CmdIncIntLocal(operands[0] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdIncInt(operands[0])
|
||||||
|
}
|
||||||
|
Opcode.DEC_INT -> if (isFastLocal(operands[0])) {
|
||||||
|
CmdDecIntLocal(operands[0] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdDecInt(operands[0])
|
||||||
|
}
|
||||||
|
Opcode.ADD_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdAddRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdAddReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.SUB_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdSubRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdSubReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MUL_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdMulRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMulReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.DIV_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdDivRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdDivReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.NEG_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdNegRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdNegReal(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.AND_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdAndIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdAndInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.OR_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdOrIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdOrInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.XOR_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdXorIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdXorInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.SHL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdShlIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdShlInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.SHR_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdShrIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdShrInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.USHR_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdUshrIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdUshrInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.INV_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdInvIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdInvInt(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqBool(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqBool(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_INT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqIntRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqIntReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_REAL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqRealIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqRealInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_INT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtIntRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtIntReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_REAL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtRealIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtRealInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_INT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteIntRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteIntReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_REAL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteRealIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteRealInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_INT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtIntRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtIntReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_REAL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtRealIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtRealInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_INT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteIntRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteIntReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_REAL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteRealIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteRealInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_INT_REAL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqIntRealLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqIntReal(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_REAL_INT -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqRealIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqRealInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_OBJ -> CmdCmpEqObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_NEQ_OBJ -> CmdCmpNeqObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_REF_EQ_OBJ -> CmdCmpRefEqObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_REF_NEQ_OBJ -> CmdCmpRefNeqObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_EQ_STR -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqStrLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqStr(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_STR -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqStrLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqStr(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_STR -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtStrLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtStr(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_STR -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteStrLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteStr(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_STR -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtStrLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtStr(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_STR -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteStrLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteStr(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_EQ_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpEqRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpEqRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_NEQ_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpNeqRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpNeqRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLtRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLtRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LTE_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpLteRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpLteRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GT_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGtRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGtRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_GTE_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdCmpGteRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdCmpGteRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.NOT_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1])) {
|
||||||
|
CmdNotBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdNotBool(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.AND_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdAndBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdAndBool(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.OR_BOOL -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdOrBoolLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdOrBool(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.CMP_LT_OBJ -> CmdCmpLtObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_LTE_OBJ -> CmdCmpLteObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_GT_OBJ -> CmdCmpGtObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CMP_GTE_OBJ -> CmdCmpGteObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.ADD_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdAddIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdAddIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.SUB_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdSubIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdSubIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MUL_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdMulIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMulIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.DIV_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdDivIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdDivIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MOD_INT_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdModIntObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdModIntObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.ADD_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdAddRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdAddRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.SUB_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdSubRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdSubRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MUL_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdMulRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdMulRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.DIV_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdDivRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdDivRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.MOD_REAL_OBJ -> if (isFastLocal(operands[0]) && isFastLocal(operands[1]) && isFastLocal(operands[2])) {
|
||||||
|
CmdModRealObjLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2] - scopeSlotCount)
|
||||||
|
} else {
|
||||||
|
CmdModRealObj(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.ADD_OBJ -> CmdAddObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.SUB_OBJ -> CmdSubObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.MUL_OBJ -> CmdMulObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.DIV_OBJ -> CmdDivObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.MOD_OBJ -> CmdModObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.CONTAINS_OBJ -> CmdContainsObj(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.ASSIGN_OP_OBJ -> CmdAssignOpObj(operands[0], operands[1], operands[2], operands[3], operands[4])
|
||||||
|
Opcode.JMP -> CmdJmp(operands[0])
|
||||||
|
Opcode.JMP_IF_TRUE -> if (operands[0] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfTrueLocal(operands[0] - scopeSlotCount, operands[1])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfTrue(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_FALSE -> if (operands[0] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfFalseLocal(operands[0] - scopeSlotCount, operands[1])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfFalse(operands[0], operands[1])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_EQ_INT -> if (operands[0] >= scopeSlotCount && operands[1] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfEqIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfEqInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_NEQ_INT -> if (operands[0] >= scopeSlotCount && operands[1] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfNeqIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfNeqInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_LT_INT -> if (operands[0] >= scopeSlotCount && operands[1] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfLtIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfLtInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_LTE_INT -> if (operands[0] >= scopeSlotCount && operands[1] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfLteIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfLteInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_GT_INT -> if (operands[0] >= scopeSlotCount && operands[1] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfGtIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfGtInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.JMP_IF_GTE_INT -> if (operands[0] >= scopeSlotCount && operands[1] >= scopeSlotCount) {
|
||||||
|
CmdJmpIfGteIntLocal(operands[0] - scopeSlotCount, operands[1] - scopeSlotCount, operands[2])
|
||||||
|
} else {
|
||||||
|
CmdJmpIfGteInt(operands[0], operands[1], operands[2])
|
||||||
|
}
|
||||||
|
Opcode.RET -> CmdRet(operands[0])
|
||||||
|
Opcode.RET_VOID -> CmdRetVoid()
|
||||||
|
Opcode.PUSH_SCOPE -> CmdPushScope(operands[0])
|
||||||
|
Opcode.POP_SCOPE -> CmdPopScope()
|
||||||
|
Opcode.PUSH_SLOT_PLAN -> CmdPushSlotPlan(operands[0])
|
||||||
|
Opcode.POP_SLOT_PLAN -> CmdPopSlotPlan()
|
||||||
|
Opcode.PUSH_TRY -> CmdPushTry(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.POP_TRY -> CmdPopTry()
|
||||||
|
Opcode.CLEAR_PENDING_THROWABLE -> CmdClearPendingThrowable()
|
||||||
|
Opcode.DECL_LOCAL -> CmdDeclLocal(operands[0], operands[1])
|
||||||
|
Opcode.DECL_DELEGATED -> CmdDeclDelegated(operands[0], operands[1])
|
||||||
|
Opcode.DECL_DESTRUCTURE -> CmdDeclDestructure(operands[0], operands[1])
|
||||||
|
Opcode.DECL_ENUM -> CmdDeclEnum(operands[0], operands[1])
|
||||||
|
Opcode.DECL_FUNCTION -> CmdDeclFunction(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS -> CmdDeclClass(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS_FIELD -> CmdDeclClassField(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS_DELEGATED -> CmdDeclClassDelegated(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS_INSTANCE_INIT -> CmdDeclClassInstanceInit(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS_INSTANCE_FIELD -> CmdDeclClassInstanceField(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS_INSTANCE_PROPERTY -> CmdDeclClassInstanceProperty(operands[0], operands[1])
|
||||||
|
Opcode.DECL_CLASS_INSTANCE_DELEGATED -> CmdDeclClassInstanceDelegated(operands[0], operands[1])
|
||||||
|
Opcode.DECL_INSTANCE_FIELD -> CmdDeclInstanceField(operands[0], operands[1])
|
||||||
|
Opcode.DECL_INSTANCE_PROPERTY -> CmdDeclInstanceProperty(operands[0], operands[1])
|
||||||
|
Opcode.DECL_INSTANCE_DELEGATED -> CmdDeclInstanceDelegated(operands[0], operands[1])
|
||||||
|
Opcode.DECL_EXT_PROPERTY -> CmdDeclExtProperty(operands[0], operands[1])
|
||||||
|
Opcode.CALL_DIRECT -> CmdCallDirect(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.ASSIGN_DESTRUCTURE -> CmdAssignDestructure(operands[0], operands[1])
|
||||||
|
Opcode.CALL_MEMBER_SLOT -> CmdCallMemberSlot(operands[0], operands[1], operands[2], operands[3], operands[4])
|
||||||
|
Opcode.CALL_SLOT -> CmdCallSlot(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.CALL_BRIDGE_SLOT -> CmdCallBridgeSlot(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.CALL_DYNAMIC_MEMBER -> CmdCallDynamicMember(operands[0], operands[1], operands[2], operands[3], operands[4])
|
||||||
|
Opcode.GET_INDEX -> CmdGetIndex(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.SET_INDEX -> CmdSetIndex(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.LIST_LITERAL -> CmdListLiteral(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.GET_MEMBER_SLOT -> CmdGetMemberSlot(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.SET_MEMBER_SLOT -> CmdSetMemberSlot(operands[0], operands[1], operands[2], operands[3])
|
||||||
|
Opcode.GET_CLASS_SCOPE -> CmdGetClassScope(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.SET_CLASS_SCOPE -> CmdSetClassScope(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.GET_DYNAMIC_MEMBER -> CmdGetDynamicMember(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.SET_DYNAMIC_MEMBER -> CmdSetDynamicMember(operands[0], operands[1], operands[2])
|
||||||
|
Opcode.ITER_PUSH -> CmdIterPush(operands[0])
|
||||||
|
Opcode.ITER_POP -> CmdIterPop()
|
||||||
|
Opcode.ITER_CANCEL -> CmdIterCancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,630 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
object CmdDisassembler {
|
||||||
|
fun disassemble(fn: CmdFunction): String {
|
||||||
|
val out = StringBuilder()
|
||||||
|
val cmds = fn.cmds
|
||||||
|
for (i in cmds.indices) {
|
||||||
|
val (op, opValues) = opAndOperands(fn, cmds[i])
|
||||||
|
val kinds = operandKinds(op)
|
||||||
|
val operands = ArrayList<String>(kinds.size)
|
||||||
|
for (k in kinds.indices) {
|
||||||
|
val v = opValues.getOrElse(k) { 0 }
|
||||||
|
when (kinds[k]) {
|
||||||
|
OperandKind.SLOT -> {
|
||||||
|
val name = when {
|
||||||
|
v < fn.scopeSlotCount -> fn.scopeSlotNames[v]
|
||||||
|
else -> {
|
||||||
|
val localIndex = v - fn.scopeSlotCount
|
||||||
|
fn.localSlotNames.getOrNull(localIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
operands += if (name != null) "s$v($name)" else "s$v"
|
||||||
|
}
|
||||||
|
OperandKind.ADDR -> operands += "a$v"
|
||||||
|
OperandKind.CONST -> operands += "k$v"
|
||||||
|
OperandKind.IP -> operands += "ip$v"
|
||||||
|
OperandKind.COUNT -> operands += "n$v"
|
||||||
|
OperandKind.ID -> operands += "#$v"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.append(i).append(": ").append(op.name)
|
||||||
|
if (operands.isNotEmpty()) {
|
||||||
|
out.append(' ').append(operands.joinToString(", "))
|
||||||
|
}
|
||||||
|
out.append('\n')
|
||||||
|
}
|
||||||
|
val captureConsts = fn.constants.withIndex().mapNotNull { (idx, constVal) ->
|
||||||
|
val table = constVal as? BytecodeConst.CaptureTable ?: return@mapNotNull null
|
||||||
|
idx to table
|
||||||
|
}
|
||||||
|
if (captureConsts.isNotEmpty()) {
|
||||||
|
out.append("consts:\n")
|
||||||
|
for ((idx, table) in captureConsts) {
|
||||||
|
val entries = if (table.entries.isEmpty()) {
|
||||||
|
"[]"
|
||||||
|
} else {
|
||||||
|
table.entries.joinToString(prefix = "[", postfix = "]") { entry ->
|
||||||
|
"${entry.ownerKind}#${entry.ownerScopeId}:${entry.ownerSlotId}@s${entry.slotIndex}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.append("k").append(idx).append(" CAPTURE_TABLE ").append(entries).append('\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun opAndOperands(fn: CmdFunction, cmd: Cmd): Pair<Opcode, IntArray> {
|
||||||
|
return when (cmd) {
|
||||||
|
is CmdNop -> Opcode.NOP to intArrayOf()
|
||||||
|
is CmdMoveObj -> Opcode.MOVE_OBJ to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdMoveInt -> Opcode.MOVE_INT to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdMoveIntLocal -> Opcode.MOVE_INT to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdMoveRealLocal -> Opcode.MOVE_REAL to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdMoveBoolLocal -> Opcode.MOVE_BOOL to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdMoveReal -> Opcode.MOVE_REAL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdMoveBool -> Opcode.MOVE_BOOL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdConstObj -> Opcode.CONST_OBJ to intArrayOf(cmd.constId, cmd.dst)
|
||||||
|
is CmdConstInt -> Opcode.CONST_INT to intArrayOf(cmd.constId, cmd.dst)
|
||||||
|
is CmdConstIntLocal -> Opcode.CONST_INT to intArrayOf(cmd.constId, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdConstReal -> Opcode.CONST_REAL to intArrayOf(cmd.constId, cmd.dst)
|
||||||
|
is CmdConstBool -> Opcode.CONST_BOOL to intArrayOf(cmd.constId, cmd.dst)
|
||||||
|
is CmdLoadThis -> Opcode.LOAD_THIS to intArrayOf(cmd.dst)
|
||||||
|
is CmdLoadThisVariant -> Opcode.LOAD_THIS_VARIANT to intArrayOf(cmd.typeId, cmd.dst)
|
||||||
|
is CmdConstNull -> Opcode.CONST_NULL to intArrayOf(cmd.dst)
|
||||||
|
is CmdMakeLambda -> Opcode.MAKE_LAMBDA_FN to intArrayOf(cmd.id, cmd.dst)
|
||||||
|
is CmdBoxObj -> Opcode.BOX_OBJ to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdObjToBool -> Opcode.OBJ_TO_BOOL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdGetObjClass -> Opcode.GET_OBJ_CLASS to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdCheckIs -> Opcode.CHECK_IS to intArrayOf(cmd.objSlot, cmd.typeSlot, cmd.dst)
|
||||||
|
is CmdAssertIs -> Opcode.ASSERT_IS to intArrayOf(cmd.objSlot, cmd.typeSlot)
|
||||||
|
is CmdMakeQualifiedView -> Opcode.MAKE_QUALIFIED_VIEW to intArrayOf(cmd.objSlot, cmd.typeSlot, cmd.dst)
|
||||||
|
is CmdRangeIntBounds -> Opcode.RANGE_INT_BOUNDS to intArrayOf(cmd.src, cmd.startSlot, cmd.endSlot, cmd.okSlot)
|
||||||
|
is CmdMakeRange -> Opcode.MAKE_RANGE to intArrayOf(
|
||||||
|
cmd.startSlot,
|
||||||
|
cmd.endSlot,
|
||||||
|
cmd.inclusiveSlot,
|
||||||
|
cmd.stepSlot,
|
||||||
|
cmd.dst
|
||||||
|
)
|
||||||
|
is CmdResolveScopeSlot -> Opcode.RESOLVE_SCOPE_SLOT to intArrayOf(cmd.scopeSlot, cmd.addrSlot)
|
||||||
|
is CmdDelegatedGetLocal -> Opcode.DELEGATED_GET_LOCAL to intArrayOf(cmd.delegateSlot, cmd.nameId, cmd.dst)
|
||||||
|
is CmdDelegatedSetLocal -> Opcode.DELEGATED_SET_LOCAL to intArrayOf(cmd.delegateSlot, cmd.nameId, cmd.valueSlot)
|
||||||
|
is CmdBindDelegateLocal -> Opcode.BIND_DELEGATE_LOCAL to intArrayOf(cmd.delegateSlot, cmd.nameId, cmd.accessId, cmd.dst)
|
||||||
|
is CmdLoadObjAddr -> Opcode.LOAD_OBJ_ADDR to intArrayOf(cmd.addrSlot, cmd.dst)
|
||||||
|
is CmdStoreObjAddr -> Opcode.STORE_OBJ_ADDR to intArrayOf(cmd.src, cmd.addrSlot)
|
||||||
|
is CmdLoadIntAddr -> Opcode.LOAD_INT_ADDR to intArrayOf(cmd.addrSlot, cmd.dst)
|
||||||
|
is CmdStoreIntAddr -> Opcode.STORE_INT_ADDR to intArrayOf(cmd.src, cmd.addrSlot)
|
||||||
|
is CmdLoadRealAddr -> Opcode.LOAD_REAL_ADDR to intArrayOf(cmd.addrSlot, cmd.dst)
|
||||||
|
is CmdStoreRealAddr -> Opcode.STORE_REAL_ADDR to intArrayOf(cmd.src, cmd.addrSlot)
|
||||||
|
is CmdLoadBoolAddr -> Opcode.LOAD_BOOL_ADDR to intArrayOf(cmd.addrSlot, cmd.dst)
|
||||||
|
is CmdStoreBoolAddr -> Opcode.STORE_BOOL_ADDR to intArrayOf(cmd.src, cmd.addrSlot)
|
||||||
|
is CmdIntToReal -> Opcode.INT_TO_REAL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdIntToRealLocal -> Opcode.INT_TO_REAL to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdRealToInt -> Opcode.REAL_TO_INT to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdRealToIntLocal -> Opcode.REAL_TO_INT to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdBoolToInt -> Opcode.BOOL_TO_INT to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdBoolToIntLocal -> Opcode.BOOL_TO_INT to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdIntToBool -> Opcode.INT_TO_BOOL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdIntToBoolLocal -> Opcode.INT_TO_BOOL to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdAddInt -> Opcode.ADD_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAddIntLocal -> Opcode.ADD_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdSubInt -> Opcode.SUB_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdSubIntLocal -> Opcode.SUB_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdMulInt -> Opcode.MUL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdMulIntLocal -> Opcode.MUL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdDivInt -> Opcode.DIV_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdDivIntLocal -> Opcode.DIV_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdModInt -> Opcode.MOD_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdModIntLocal -> Opcode.MOD_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdNegInt -> Opcode.NEG_INT to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdNegIntLocal -> Opcode.NEG_INT to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdIncInt -> Opcode.INC_INT to intArrayOf(cmd.slot)
|
||||||
|
is CmdIncIntLocal -> Opcode.INC_INT to intArrayOf(cmd.slot + fn.scopeSlotCount)
|
||||||
|
is CmdDecInt -> Opcode.DEC_INT to intArrayOf(cmd.slot)
|
||||||
|
is CmdDecIntLocal -> Opcode.DEC_INT to intArrayOf(cmd.slot + fn.scopeSlotCount)
|
||||||
|
is CmdAddReal -> Opcode.ADD_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAddRealLocal -> Opcode.ADD_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdSubReal -> Opcode.SUB_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdSubRealLocal -> Opcode.SUB_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdMulReal -> Opcode.MUL_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdMulRealLocal -> Opcode.MUL_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdDivReal -> Opcode.DIV_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdDivRealLocal -> Opcode.DIV_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdNegReal -> Opcode.NEG_REAL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdNegRealLocal -> Opcode.NEG_REAL to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdAndInt -> Opcode.AND_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAndIntLocal -> Opcode.AND_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdOrInt -> Opcode.OR_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdOrIntLocal -> Opcode.OR_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdXorInt -> Opcode.XOR_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdXorIntLocal -> Opcode.XOR_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdShlInt -> Opcode.SHL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdShlIntLocal -> Opcode.SHL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdShrInt -> Opcode.SHR_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdShrIntLocal -> Opcode.SHR_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdUshrInt -> Opcode.USHR_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdUshrIntLocal -> Opcode.USHR_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdInvInt -> Opcode.INV_INT to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdInvIntLocal -> Opcode.INV_INT to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpEqInt -> Opcode.CMP_EQ_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqIntLocal -> Opcode.CMP_EQ_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpNeqInt -> Opcode.CMP_NEQ_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqIntLocal -> Opcode.CMP_NEQ_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLtInt -> Opcode.CMP_LT_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtIntLocal -> Opcode.CMP_LT_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLteInt -> Opcode.CMP_LTE_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteIntLocal -> Opcode.CMP_LTE_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGtInt -> Opcode.CMP_GT_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtIntLocal -> Opcode.CMP_GT_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGteInt -> Opcode.CMP_GTE_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteIntLocal -> Opcode.CMP_GTE_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpEqReal -> Opcode.CMP_EQ_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqRealLocal -> Opcode.CMP_EQ_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpNeqReal -> Opcode.CMP_NEQ_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqRealLocal -> Opcode.CMP_NEQ_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLtReal -> Opcode.CMP_LT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtRealLocal -> Opcode.CMP_LT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLteReal -> Opcode.CMP_LTE_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteRealLocal -> Opcode.CMP_LTE_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGtReal -> Opcode.CMP_GT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtRealLocal -> Opcode.CMP_GT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGteReal -> Opcode.CMP_GTE_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteRealLocal -> Opcode.CMP_GTE_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpEqBool -> Opcode.CMP_EQ_BOOL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqBoolLocal -> Opcode.CMP_EQ_BOOL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpNeqBool -> Opcode.CMP_NEQ_BOOL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqBoolLocal -> Opcode.CMP_NEQ_BOOL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpEqIntReal -> Opcode.CMP_EQ_INT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqIntRealLocal -> Opcode.CMP_EQ_INT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpEqRealInt -> Opcode.CMP_EQ_REAL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqRealIntLocal -> Opcode.CMP_EQ_REAL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLtIntReal -> Opcode.CMP_LT_INT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtIntRealLocal -> Opcode.CMP_LT_INT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLtRealInt -> Opcode.CMP_LT_REAL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtRealIntLocal -> Opcode.CMP_LT_REAL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLteIntReal -> Opcode.CMP_LTE_INT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteIntRealLocal -> Opcode.CMP_LTE_INT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpLteRealInt -> Opcode.CMP_LTE_REAL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteRealIntLocal -> Opcode.CMP_LTE_REAL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGtIntReal -> Opcode.CMP_GT_INT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtIntRealLocal -> Opcode.CMP_GT_INT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGtRealInt -> Opcode.CMP_GT_REAL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtRealIntLocal -> Opcode.CMP_GT_REAL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGteIntReal -> Opcode.CMP_GTE_INT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteIntRealLocal -> Opcode.CMP_GTE_INT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpGteRealInt -> Opcode.CMP_GTE_REAL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteRealIntLocal -> Opcode.CMP_GTE_REAL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpNeqIntReal -> Opcode.CMP_NEQ_INT_REAL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqIntRealLocal -> Opcode.CMP_NEQ_INT_REAL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdCmpNeqRealInt -> Opcode.CMP_NEQ_REAL_INT to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqRealIntLocal -> Opcode.CMP_NEQ_REAL_INT to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdJmpIfEqInt -> Opcode.JMP_IF_EQ_INT to intArrayOf(cmd.a, cmd.b, cmd.target)
|
||||||
|
is CmdJmpIfEqIntLocal -> Opcode.JMP_IF_EQ_INT to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.target
|
||||||
|
)
|
||||||
|
is CmdJmpIfNeqInt -> Opcode.JMP_IF_NEQ_INT to intArrayOf(cmd.a, cmd.b, cmd.target)
|
||||||
|
is CmdJmpIfNeqIntLocal -> Opcode.JMP_IF_NEQ_INT to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.target
|
||||||
|
)
|
||||||
|
is CmdJmpIfLtInt -> Opcode.JMP_IF_LT_INT to intArrayOf(cmd.a, cmd.b, cmd.target)
|
||||||
|
is CmdJmpIfLtIntLocal -> Opcode.JMP_IF_LT_INT to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.target
|
||||||
|
)
|
||||||
|
is CmdJmpIfLteInt -> Opcode.JMP_IF_LTE_INT to intArrayOf(cmd.a, cmd.b, cmd.target)
|
||||||
|
is CmdJmpIfLteIntLocal -> Opcode.JMP_IF_LTE_INT to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.target
|
||||||
|
)
|
||||||
|
is CmdJmpIfGtInt -> Opcode.JMP_IF_GT_INT to intArrayOf(cmd.a, cmd.b, cmd.target)
|
||||||
|
is CmdJmpIfGtIntLocal -> Opcode.JMP_IF_GT_INT to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.target
|
||||||
|
)
|
||||||
|
is CmdJmpIfGteInt -> Opcode.JMP_IF_GTE_INT to intArrayOf(cmd.a, cmd.b, cmd.target)
|
||||||
|
is CmdJmpIfGteIntLocal -> Opcode.JMP_IF_GTE_INT to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.target
|
||||||
|
)
|
||||||
|
is CmdCmpEqObj -> Opcode.CMP_EQ_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqObj -> Opcode.CMP_NEQ_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpRefEqObj -> Opcode.CMP_REF_EQ_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpRefNeqObj -> Opcode.CMP_REF_NEQ_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqStr -> Opcode.CMP_EQ_STR to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqStrLocal -> Opcode.CMP_EQ_STR to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpNeqStr -> Opcode.CMP_NEQ_STR to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqStrLocal -> Opcode.CMP_NEQ_STR to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLtStr -> Opcode.CMP_LT_STR to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtStrLocal -> Opcode.CMP_LT_STR to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLteStr -> Opcode.CMP_LTE_STR to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteStrLocal -> Opcode.CMP_LTE_STR to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpGtStr -> Opcode.CMP_GT_STR to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtStrLocal -> Opcode.CMP_GT_STR to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpGteStr -> Opcode.CMP_GTE_STR to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteStrLocal -> Opcode.CMP_GTE_STR to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpEqIntObj -> Opcode.CMP_EQ_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqIntObjLocal -> Opcode.CMP_EQ_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpNeqIntObj -> Opcode.CMP_NEQ_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqIntObjLocal -> Opcode.CMP_NEQ_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLtIntObj -> Opcode.CMP_LT_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtIntObjLocal -> Opcode.CMP_LT_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLteIntObj -> Opcode.CMP_LTE_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteIntObjLocal -> Opcode.CMP_LTE_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpGtIntObj -> Opcode.CMP_GT_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtIntObjLocal -> Opcode.CMP_GT_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpGteIntObj -> Opcode.CMP_GTE_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteIntObjLocal -> Opcode.CMP_GTE_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpEqRealObj -> Opcode.CMP_EQ_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpEqRealObjLocal -> Opcode.CMP_EQ_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpNeqRealObj -> Opcode.CMP_NEQ_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpNeqRealObjLocal -> Opcode.CMP_NEQ_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLtRealObj -> Opcode.CMP_LT_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLtRealObjLocal -> Opcode.CMP_LT_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLteRealObj -> Opcode.CMP_LTE_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteRealObjLocal -> Opcode.CMP_LTE_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpGtRealObj -> Opcode.CMP_GT_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtRealObjLocal -> Opcode.CMP_GT_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpGteRealObj -> Opcode.CMP_GTE_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteRealObjLocal -> Opcode.CMP_GTE_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdNotBool -> Opcode.NOT_BOOL to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdNotBoolLocal -> Opcode.NOT_BOOL to intArrayOf(cmd.src + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdAndBool -> Opcode.AND_BOOL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAndBoolLocal -> Opcode.AND_BOOL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdOrBool -> Opcode.OR_BOOL to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdOrBoolLocal -> Opcode.OR_BOOL to intArrayOf(cmd.a + fn.scopeSlotCount, cmd.b + fn.scopeSlotCount, cmd.dst + fn.scopeSlotCount)
|
||||||
|
is CmdUnboxIntObj -> Opcode.UNBOX_INT_OBJ to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdUnboxIntObjLocal -> Opcode.UNBOX_INT_OBJ to intArrayOf(
|
||||||
|
cmd.src + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdUnboxRealObj -> Opcode.UNBOX_REAL_OBJ to intArrayOf(cmd.src, cmd.dst)
|
||||||
|
is CmdUnboxRealObjLocal -> Opcode.UNBOX_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.src + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdCmpLtObj -> Opcode.CMP_LT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpLteObj -> Opcode.CMP_LTE_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGtObj -> Opcode.CMP_GT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdCmpGteObj -> Opcode.CMP_GTE_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAddIntObj -> Opcode.ADD_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAddIntObjLocal -> Opcode.ADD_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdSubIntObj -> Opcode.SUB_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdSubIntObjLocal -> Opcode.SUB_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdMulIntObj -> Opcode.MUL_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdMulIntObjLocal -> Opcode.MUL_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdDivIntObj -> Opcode.DIV_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdDivIntObjLocal -> Opcode.DIV_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdModIntObj -> Opcode.MOD_INT_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdModIntObjLocal -> Opcode.MOD_INT_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdAddRealObj -> Opcode.ADD_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdAddRealObjLocal -> Opcode.ADD_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdSubRealObj -> Opcode.SUB_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdSubRealObjLocal -> Opcode.SUB_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdMulRealObj -> Opcode.MUL_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdMulRealObjLocal -> Opcode.MUL_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdDivRealObj -> Opcode.DIV_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdDivRealObjLocal -> Opcode.DIV_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdModRealObj -> Opcode.MOD_REAL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdModRealObjLocal -> Opcode.MOD_REAL_OBJ to intArrayOf(
|
||||||
|
cmd.a + fn.scopeSlotCount,
|
||||||
|
cmd.b + fn.scopeSlotCount,
|
||||||
|
cmd.dst + fn.scopeSlotCount
|
||||||
|
)
|
||||||
|
is CmdAddObj -> Opcode.ADD_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdSubObj -> Opcode.SUB_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdMulObj -> Opcode.MUL_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdDivObj -> Opcode.DIV_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdModObj -> Opcode.MOD_OBJ to intArrayOf(cmd.a, cmd.b, cmd.dst)
|
||||||
|
is CmdContainsObj -> Opcode.CONTAINS_OBJ to intArrayOf(cmd.target, cmd.value, cmd.dst)
|
||||||
|
is CmdAssignOpObj -> Opcode.ASSIGN_OP_OBJ to intArrayOf(cmd.opId, cmd.targetSlot, cmd.valueSlot, cmd.dst, cmd.nameId)
|
||||||
|
is CmdJmp -> Opcode.JMP to intArrayOf(cmd.target)
|
||||||
|
is CmdJmpIfTrue -> Opcode.JMP_IF_TRUE to intArrayOf(cmd.cond, cmd.target)
|
||||||
|
is CmdJmpIfTrueLocal -> Opcode.JMP_IF_TRUE to intArrayOf(cmd.cond + fn.scopeSlotCount, cmd.target)
|
||||||
|
is CmdJmpIfFalse -> Opcode.JMP_IF_FALSE to intArrayOf(cmd.cond, cmd.target)
|
||||||
|
is CmdJmpIfFalseLocal -> Opcode.JMP_IF_FALSE to intArrayOf(cmd.cond + fn.scopeSlotCount, cmd.target)
|
||||||
|
is CmdRet -> Opcode.RET to intArrayOf(cmd.slot)
|
||||||
|
is CmdRetLabel -> Opcode.RET_LABEL to intArrayOf(cmd.labelId, cmd.slot)
|
||||||
|
is CmdRetVoid -> Opcode.RET_VOID to intArrayOf()
|
||||||
|
is CmdThrow -> Opcode.THROW to intArrayOf(cmd.posId, cmd.slot)
|
||||||
|
is CmdRethrowPending -> Opcode.RETHROW_PENDING to intArrayOf()
|
||||||
|
is CmdPushScope -> Opcode.PUSH_SCOPE to intArrayOf(cmd.planId)
|
||||||
|
is CmdPopScope -> Opcode.POP_SCOPE to intArrayOf()
|
||||||
|
is CmdPushSlotPlan -> Opcode.PUSH_SLOT_PLAN to intArrayOf(cmd.planId)
|
||||||
|
is CmdPopSlotPlan -> Opcode.POP_SLOT_PLAN to intArrayOf()
|
||||||
|
is CmdPushTry -> Opcode.PUSH_TRY to intArrayOf(cmd.exceptionSlot, cmd.catchIp, cmd.finallyIp)
|
||||||
|
is CmdPopTry -> Opcode.POP_TRY to intArrayOf()
|
||||||
|
is CmdClearPendingThrowable -> Opcode.CLEAR_PENDING_THROWABLE to intArrayOf()
|
||||||
|
is CmdDeclLocal -> Opcode.DECL_LOCAL to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclDelegated -> Opcode.DECL_DELEGATED to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclDestructure -> Opcode.DECL_DESTRUCTURE to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclEnum -> Opcode.DECL_ENUM to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclFunction -> Opcode.DECL_FUNCTION to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClass -> Opcode.DECL_CLASS to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClassField -> Opcode.DECL_CLASS_FIELD to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClassDelegated -> Opcode.DECL_CLASS_DELEGATED to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClassInstanceInit -> Opcode.DECL_CLASS_INSTANCE_INIT to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClassInstanceField -> Opcode.DECL_CLASS_INSTANCE_FIELD to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClassInstanceProperty -> Opcode.DECL_CLASS_INSTANCE_PROPERTY to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclClassInstanceDelegated -> Opcode.DECL_CLASS_INSTANCE_DELEGATED to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclInstanceField -> Opcode.DECL_INSTANCE_FIELD to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclInstanceProperty -> Opcode.DECL_INSTANCE_PROPERTY to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclInstanceDelegated -> Opcode.DECL_INSTANCE_DELEGATED to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdDeclExtProperty -> Opcode.DECL_EXT_PROPERTY to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdCallDirect -> Opcode.CALL_DIRECT to intArrayOf(cmd.id, cmd.argBase, cmd.argCount, cmd.dst)
|
||||||
|
is CmdAssignDestructure -> Opcode.ASSIGN_DESTRUCTURE to intArrayOf(cmd.constId, cmd.slot)
|
||||||
|
is CmdCallMemberSlot -> Opcode.CALL_MEMBER_SLOT to intArrayOf(cmd.recvSlot, cmd.methodId, cmd.argBase, cmd.argCount, cmd.dst)
|
||||||
|
is CmdCallSlot -> Opcode.CALL_SLOT to intArrayOf(cmd.calleeSlot, cmd.argBase, cmd.argCount, cmd.dst)
|
||||||
|
is CmdCallBridgeSlot -> Opcode.CALL_BRIDGE_SLOT to intArrayOf(cmd.calleeSlot, cmd.argBase, cmd.argCount, cmd.dst)
|
||||||
|
is CmdCallDynamicMember -> Opcode.CALL_DYNAMIC_MEMBER to intArrayOf(cmd.recvSlot, cmd.nameId, cmd.argBase, cmd.argCount, cmd.dst)
|
||||||
|
is CmdGetIndex -> Opcode.GET_INDEX to intArrayOf(cmd.targetSlot, cmd.indexSlot, cmd.dst)
|
||||||
|
is CmdSetIndex -> Opcode.SET_INDEX to intArrayOf(cmd.targetSlot, cmd.indexSlot, cmd.valueSlot)
|
||||||
|
is CmdListLiteral -> Opcode.LIST_LITERAL to intArrayOf(cmd.planId, cmd.baseSlot, cmd.count, cmd.dst)
|
||||||
|
is CmdGetMemberSlot -> Opcode.GET_MEMBER_SLOT to intArrayOf(cmd.recvSlot, cmd.fieldId, cmd.methodId, cmd.dst)
|
||||||
|
is CmdSetMemberSlot -> Opcode.SET_MEMBER_SLOT to intArrayOf(cmd.recvSlot, cmd.fieldId, cmd.methodId, cmd.valueSlot)
|
||||||
|
is CmdGetClassScope -> Opcode.GET_CLASS_SCOPE to intArrayOf(cmd.classSlot, cmd.nameId, cmd.dst)
|
||||||
|
is CmdSetClassScope -> Opcode.SET_CLASS_SCOPE to intArrayOf(cmd.classSlot, cmd.nameId, cmd.valueSlot)
|
||||||
|
is CmdGetDynamicMember -> Opcode.GET_DYNAMIC_MEMBER to intArrayOf(cmd.recvSlot, cmd.nameId, cmd.dst)
|
||||||
|
is CmdSetDynamicMember -> Opcode.SET_DYNAMIC_MEMBER to intArrayOf(cmd.recvSlot, cmd.nameId, cmd.valueSlot)
|
||||||
|
is CmdIterPush -> Opcode.ITER_PUSH to intArrayOf(cmd.iterSlot)
|
||||||
|
is CmdIterPop -> Opcode.ITER_POP to intArrayOf()
|
||||||
|
is CmdIterCancel -> Opcode.ITER_CANCEL to intArrayOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum class OperandKind {
|
||||||
|
SLOT,
|
||||||
|
ADDR,
|
||||||
|
CONST,
|
||||||
|
IP,
|
||||||
|
COUNT,
|
||||||
|
ID,
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun operandKinds(op: Opcode): List<OperandKind> {
|
||||||
|
return when (op) {
|
||||||
|
Opcode.NOP, Opcode.RET_VOID, Opcode.POP_SCOPE, Opcode.POP_SLOT_PLAN, Opcode.POP_TRY,
|
||||||
|
Opcode.CLEAR_PENDING_THROWABLE, Opcode.RETHROW_PENDING,
|
||||||
|
Opcode.ITER_POP, Opcode.ITER_CANCEL -> emptyList()
|
||||||
|
Opcode.MOVE_OBJ, Opcode.MOVE_INT, Opcode.MOVE_REAL, Opcode.MOVE_BOOL, Opcode.BOX_OBJ,
|
||||||
|
Opcode.UNBOX_INT_OBJ, Opcode.UNBOX_REAL_OBJ,
|
||||||
|
Opcode.INT_TO_REAL, Opcode.REAL_TO_INT, Opcode.BOOL_TO_INT, Opcode.INT_TO_BOOL,
|
||||||
|
Opcode.NEG_INT, Opcode.NEG_REAL, Opcode.NOT_BOOL, Opcode.INV_INT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.OBJ_TO_BOOL, Opcode.GET_OBJ_CLASS, Opcode.ASSERT_IS ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.CHECK_IS, Opcode.MAKE_QUALIFIED_VIEW ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.RANGE_INT_BOUNDS ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.MAKE_RANGE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.RET_LABEL, Opcode.THROW ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.RESOLVE_SCOPE_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ADDR)
|
||||||
|
Opcode.DELEGATED_GET_LOCAL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.DELEGATED_SET_LOCAL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.BIND_DELEGATE_LOCAL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.LOAD_OBJ_ADDR, Opcode.LOAD_INT_ADDR, Opcode.LOAD_REAL_ADDR, Opcode.LOAD_BOOL_ADDR ->
|
||||||
|
listOf(OperandKind.ADDR, OperandKind.SLOT)
|
||||||
|
Opcode.STORE_OBJ_ADDR, Opcode.STORE_INT_ADDR, Opcode.STORE_REAL_ADDR, Opcode.STORE_BOOL_ADDR ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ADDR)
|
||||||
|
Opcode.CONST_NULL ->
|
||||||
|
listOf(OperandKind.SLOT)
|
||||||
|
Opcode.CONST_OBJ, Opcode.CONST_INT, Opcode.CONST_REAL, Opcode.CONST_BOOL,
|
||||||
|
Opcode.MAKE_LAMBDA_FN ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.PUSH_SCOPE, Opcode.PUSH_SLOT_PLAN ->
|
||||||
|
listOf(OperandKind.CONST)
|
||||||
|
Opcode.PUSH_TRY ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.IP, OperandKind.IP)
|
||||||
|
Opcode.DECL_LOCAL, Opcode.DECL_EXT_PROPERTY, Opcode.DECL_DELEGATED, Opcode.DECL_DESTRUCTURE,
|
||||||
|
Opcode.DECL_ENUM, Opcode.DECL_FUNCTION, Opcode.DECL_CLASS, Opcode.DECL_CLASS_FIELD,
|
||||||
|
Opcode.DECL_CLASS_DELEGATED, Opcode.DECL_CLASS_INSTANCE_INIT, Opcode.DECL_CLASS_INSTANCE_FIELD,
|
||||||
|
Opcode.DECL_CLASS_INSTANCE_PROPERTY, Opcode.DECL_CLASS_INSTANCE_DELEGATED, Opcode.DECL_INSTANCE_FIELD,
|
||||||
|
Opcode.DECL_INSTANCE_PROPERTY, Opcode.DECL_INSTANCE_DELEGATED,
|
||||||
|
Opcode.ASSIGN_DESTRUCTURE ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.ADD_INT, Opcode.SUB_INT, Opcode.MUL_INT, Opcode.DIV_INT, Opcode.MOD_INT,
|
||||||
|
Opcode.ADD_REAL, Opcode.SUB_REAL, Opcode.MUL_REAL, Opcode.DIV_REAL,
|
||||||
|
Opcode.AND_INT, Opcode.OR_INT, Opcode.XOR_INT, Opcode.SHL_INT, Opcode.SHR_INT, Opcode.USHR_INT,
|
||||||
|
Opcode.CMP_EQ_INT, Opcode.CMP_NEQ_INT, Opcode.CMP_LT_INT, Opcode.CMP_LTE_INT,
|
||||||
|
Opcode.CMP_GT_INT, Opcode.CMP_GTE_INT,
|
||||||
|
Opcode.CMP_EQ_REAL, Opcode.CMP_NEQ_REAL, Opcode.CMP_LT_REAL, Opcode.CMP_LTE_REAL,
|
||||||
|
Opcode.CMP_GT_REAL, Opcode.CMP_GTE_REAL,
|
||||||
|
Opcode.CMP_EQ_BOOL, Opcode.CMP_NEQ_BOOL,
|
||||||
|
Opcode.CMP_EQ_INT_REAL, Opcode.CMP_EQ_REAL_INT, Opcode.CMP_LT_INT_REAL, Opcode.CMP_LT_REAL_INT,
|
||||||
|
Opcode.CMP_LTE_INT_REAL, Opcode.CMP_LTE_REAL_INT, Opcode.CMP_GT_INT_REAL, Opcode.CMP_GT_REAL_INT,
|
||||||
|
Opcode.CMP_GTE_INT_REAL, Opcode.CMP_GTE_REAL_INT, Opcode.CMP_NEQ_INT_REAL, Opcode.CMP_NEQ_REAL_INT,
|
||||||
|
Opcode.CMP_EQ_OBJ, Opcode.CMP_NEQ_OBJ, Opcode.CMP_REF_EQ_OBJ, Opcode.CMP_REF_NEQ_OBJ,
|
||||||
|
Opcode.CMP_EQ_STR, Opcode.CMP_NEQ_STR, Opcode.CMP_LT_STR, Opcode.CMP_LTE_STR,
|
||||||
|
Opcode.CMP_GT_STR, Opcode.CMP_GTE_STR,
|
||||||
|
Opcode.CMP_EQ_INT_OBJ, Opcode.CMP_NEQ_INT_OBJ, Opcode.CMP_LT_INT_OBJ, Opcode.CMP_LTE_INT_OBJ,
|
||||||
|
Opcode.CMP_GT_INT_OBJ, Opcode.CMP_GTE_INT_OBJ, Opcode.CMP_EQ_REAL_OBJ, Opcode.CMP_NEQ_REAL_OBJ,
|
||||||
|
Opcode.CMP_LT_REAL_OBJ, Opcode.CMP_LTE_REAL_OBJ, Opcode.CMP_GT_REAL_OBJ, Opcode.CMP_GTE_REAL_OBJ,
|
||||||
|
Opcode.CMP_LT_OBJ, Opcode.CMP_LTE_OBJ, Opcode.CMP_GT_OBJ, Opcode.CMP_GTE_OBJ,
|
||||||
|
Opcode.ADD_INT_OBJ, Opcode.SUB_INT_OBJ, Opcode.MUL_INT_OBJ, Opcode.DIV_INT_OBJ, Opcode.MOD_INT_OBJ,
|
||||||
|
Opcode.ADD_REAL_OBJ, Opcode.SUB_REAL_OBJ, Opcode.MUL_REAL_OBJ, Opcode.DIV_REAL_OBJ, Opcode.MOD_REAL_OBJ,
|
||||||
|
Opcode.ADD_OBJ, Opcode.SUB_OBJ, Opcode.MUL_OBJ, Opcode.DIV_OBJ, Opcode.MOD_OBJ, Opcode.CONTAINS_OBJ,
|
||||||
|
Opcode.AND_BOOL, Opcode.OR_BOOL ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.ASSIGN_OP_OBJ ->
|
||||||
|
listOf(OperandKind.ID, OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT, OperandKind.CONST)
|
||||||
|
Opcode.INC_INT, Opcode.DEC_INT, Opcode.RET, Opcode.ITER_PUSH, Opcode.LOAD_THIS ->
|
||||||
|
listOf(OperandKind.SLOT)
|
||||||
|
Opcode.LOAD_THIS_VARIANT ->
|
||||||
|
listOf(OperandKind.ID, OperandKind.SLOT)
|
||||||
|
Opcode.JMP ->
|
||||||
|
listOf(OperandKind.IP)
|
||||||
|
Opcode.JMP_IF_TRUE, Opcode.JMP_IF_FALSE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.IP)
|
||||||
|
Opcode.JMP_IF_EQ_INT, Opcode.JMP_IF_NEQ_INT,
|
||||||
|
Opcode.JMP_IF_LT_INT, Opcode.JMP_IF_LTE_INT,
|
||||||
|
Opcode.JMP_IF_GT_INT, Opcode.JMP_IF_GTE_INT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.IP)
|
||||||
|
Opcode.CALL_DIRECT ->
|
||||||
|
listOf(OperandKind.ID, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.CALL_SLOT, Opcode.CALL_BRIDGE_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.CALL_MEMBER_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ID, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.CALL_DYNAMIC_MEMBER ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.GET_INDEX ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.SET_INDEX ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.SLOT, OperandKind.SLOT)
|
||||||
|
Opcode.LIST_LITERAL ->
|
||||||
|
listOf(OperandKind.CONST, OperandKind.SLOT, OperandKind.COUNT, OperandKind.SLOT)
|
||||||
|
Opcode.GET_MEMBER_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ID, OperandKind.ID, OperandKind.SLOT)
|
||||||
|
Opcode.SET_MEMBER_SLOT ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.ID, OperandKind.ID, OperandKind.SLOT)
|
||||||
|
Opcode.GET_CLASS_SCOPE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.SET_CLASS_SCOPE ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.GET_DYNAMIC_MEMBER ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
Opcode.SET_DYNAMIC_MEMBER ->
|
||||||
|
listOf(OperandKind.SLOT, OperandKind.CONST, OperandKind.SLOT)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
data class CmdFunction(
|
||||||
|
val name: String,
|
||||||
|
val localCount: Int,
|
||||||
|
val addrCount: Int,
|
||||||
|
val returnLabels: Set<String>,
|
||||||
|
val scopeSlotCount: Int,
|
||||||
|
val scopeSlotIndices: IntArray,
|
||||||
|
val scopeSlotNames: Array<String?>,
|
||||||
|
val scopeSlotIsModule: BooleanArray,
|
||||||
|
val localSlotNames: Array<String?>,
|
||||||
|
val localSlotMutables: BooleanArray,
|
||||||
|
val localSlotDelegated: BooleanArray,
|
||||||
|
val localSlotCaptures: BooleanArray,
|
||||||
|
val constants: List<BytecodeConst>,
|
||||||
|
val cmds: Array<Cmd>,
|
||||||
|
val posByIp: Array<net.sergeych.lyng.Pos?>,
|
||||||
|
) {
|
||||||
|
init {
|
||||||
|
require(scopeSlotIndices.size == scopeSlotCount) { "scopeSlotIndices size mismatch" }
|
||||||
|
require(scopeSlotNames.size == scopeSlotCount) { "scopeSlotNames size mismatch" }
|
||||||
|
require(scopeSlotIsModule.size == scopeSlotCount) { "scopeSlotIsModule size mismatch" }
|
||||||
|
require(localSlotNames.size == localSlotMutables.size) { "localSlot metadata size mismatch" }
|
||||||
|
require(localSlotNames.size == localSlotDelegated.size) { "localSlot delegation size mismatch" }
|
||||||
|
require(localSlotNames.size == localSlotCaptures.size) { "localSlot capture size mismatch" }
|
||||||
|
require(localSlotNames.size <= localCount) { "localSlotNames exceed localCount" }
|
||||||
|
require(addrCount >= 0) { "addrCount must be non-negative" }
|
||||||
|
if (posByIp.isNotEmpty()) {
|
||||||
|
require(posByIp.size == cmds.size) { "posByIp size mismatch" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun localSlotPlanByName(): Map<String, Int> {
|
||||||
|
val result = LinkedHashMap<String, Int>()
|
||||||
|
for (i in localSlotNames.indices) {
|
||||||
|
val name = localSlotNames[i] ?: continue
|
||||||
|
val existing = result[name]
|
||||||
|
if (existing == null) {
|
||||||
|
result[name] = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val existingIsCapture = localSlotCaptures.getOrNull(existing) == true
|
||||||
|
val currentIsCapture = localSlotCaptures.getOrNull(i) == true
|
||||||
|
if (existingIsCapture && !currentIsCapture) {
|
||||||
|
result[name] = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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.bytecode
|
||||||
|
|
||||||
|
data class ForcedLocalSlotInfo(
|
||||||
|
val index: Int,
|
||||||
|
val isMutable: Boolean,
|
||||||
|
val isDelegated: Boolean,
|
||||||
|
)
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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.bytecode
|
||||||
|
|
||||||
|
enum class CaptureOwnerFrameKind { MODULE, LOCAL }
|
||||||
|
|
||||||
|
data class LambdaCaptureEntry(
|
||||||
|
val ownerKind: CaptureOwnerFrameKind,
|
||||||
|
val ownerScopeId: Int,
|
||||||
|
val ownerSlotId: Int,
|
||||||
|
val ownerName: String,
|
||||||
|
val ownerIsMutable: Boolean,
|
||||||
|
val ownerIsDelegated: Boolean,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class BytecodeCaptureEntry(
|
||||||
|
val ownerKind: CaptureOwnerFrameKind,
|
||||||
|
val ownerScopeId: Int,
|
||||||
|
val ownerSlotId: Int,
|
||||||
|
val slotIndex: Int,
|
||||||
|
)
|
||||||
@ -0,0 +1,224 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.sergeych.lyng.bytecode
|
||||||
|
|
||||||
|
enum class Opcode(val code: Int) {
|
||||||
|
NOP(0x00),
|
||||||
|
MOVE_OBJ(0x01),
|
||||||
|
MOVE_INT(0x02),
|
||||||
|
MOVE_REAL(0x03),
|
||||||
|
MOVE_BOOL(0x04),
|
||||||
|
CONST_OBJ(0x05),
|
||||||
|
CONST_INT(0x06),
|
||||||
|
CONST_REAL(0x07),
|
||||||
|
CONST_BOOL(0x08),
|
||||||
|
CONST_NULL(0x09),
|
||||||
|
BOX_OBJ(0x0A),
|
||||||
|
RANGE_INT_BOUNDS(0x0B),
|
||||||
|
MAKE_RANGE(0x0C),
|
||||||
|
LOAD_THIS(0x0D),
|
||||||
|
LOAD_THIS_VARIANT(0x0F),
|
||||||
|
|
||||||
|
INT_TO_REAL(0x10),
|
||||||
|
REAL_TO_INT(0x11),
|
||||||
|
BOOL_TO_INT(0x12),
|
||||||
|
INT_TO_BOOL(0x13),
|
||||||
|
OBJ_TO_BOOL(0x14),
|
||||||
|
CHECK_IS(0x15),
|
||||||
|
ASSERT_IS(0x16),
|
||||||
|
MAKE_QUALIFIED_VIEW(0x17),
|
||||||
|
MAKE_LAMBDA_FN(0x18),
|
||||||
|
GET_OBJ_CLASS(0x19),
|
||||||
|
|
||||||
|
ADD_INT(0x20),
|
||||||
|
SUB_INT(0x21),
|
||||||
|
MUL_INT(0x22),
|
||||||
|
DIV_INT(0x23),
|
||||||
|
MOD_INT(0x24),
|
||||||
|
NEG_INT(0x25),
|
||||||
|
INC_INT(0x26),
|
||||||
|
DEC_INT(0x27),
|
||||||
|
|
||||||
|
ADD_REAL(0x30),
|
||||||
|
SUB_REAL(0x31),
|
||||||
|
MUL_REAL(0x32),
|
||||||
|
DIV_REAL(0x33),
|
||||||
|
NEG_REAL(0x34),
|
||||||
|
|
||||||
|
AND_INT(0x40),
|
||||||
|
OR_INT(0x41),
|
||||||
|
XOR_INT(0x42),
|
||||||
|
SHL_INT(0x43),
|
||||||
|
SHR_INT(0x44),
|
||||||
|
USHR_INT(0x45),
|
||||||
|
INV_INT(0x46),
|
||||||
|
|
||||||
|
CMP_EQ_INT(0x50),
|
||||||
|
CMP_NEQ_INT(0x51),
|
||||||
|
CMP_LT_INT(0x52),
|
||||||
|
CMP_LTE_INT(0x53),
|
||||||
|
CMP_GT_INT(0x54),
|
||||||
|
CMP_GTE_INT(0x55),
|
||||||
|
CMP_EQ_REAL(0x56),
|
||||||
|
CMP_NEQ_REAL(0x57),
|
||||||
|
CMP_LT_REAL(0x58),
|
||||||
|
CMP_LTE_REAL(0x59),
|
||||||
|
CMP_GT_REAL(0x5A),
|
||||||
|
CMP_GTE_REAL(0x5B),
|
||||||
|
CMP_EQ_BOOL(0x5C),
|
||||||
|
CMP_NEQ_BOOL(0x5D),
|
||||||
|
|
||||||
|
CMP_EQ_INT_REAL(0x60),
|
||||||
|
CMP_EQ_REAL_INT(0x61),
|
||||||
|
CMP_LT_INT_REAL(0x62),
|
||||||
|
CMP_LT_REAL_INT(0x63),
|
||||||
|
CMP_LTE_INT_REAL(0x64),
|
||||||
|
CMP_LTE_REAL_INT(0x65),
|
||||||
|
CMP_GT_INT_REAL(0x66),
|
||||||
|
CMP_GT_REAL_INT(0x67),
|
||||||
|
CMP_GTE_INT_REAL(0x68),
|
||||||
|
CMP_GTE_REAL_INT(0x69),
|
||||||
|
CMP_NEQ_INT_REAL(0x6A),
|
||||||
|
CMP_NEQ_REAL_INT(0x6B),
|
||||||
|
CMP_EQ_OBJ(0x6C),
|
||||||
|
CMP_NEQ_OBJ(0x6D),
|
||||||
|
CMP_REF_EQ_OBJ(0x6E),
|
||||||
|
CMP_REF_NEQ_OBJ(0x6F),
|
||||||
|
CMP_EQ_STR(0xD6),
|
||||||
|
CMP_NEQ_STR(0xD7),
|
||||||
|
CMP_LT_STR(0xD8),
|
||||||
|
CMP_LTE_STR(0xD9),
|
||||||
|
CMP_GT_STR(0xDA),
|
||||||
|
CMP_GTE_STR(0xDB),
|
||||||
|
CMP_EQ_INT_OBJ(0xDC),
|
||||||
|
CMP_NEQ_INT_OBJ(0xDD),
|
||||||
|
CMP_LT_INT_OBJ(0xDE),
|
||||||
|
CMP_LTE_INT_OBJ(0xDF),
|
||||||
|
CMP_GT_INT_OBJ(0xE0),
|
||||||
|
CMP_GTE_INT_OBJ(0xE1),
|
||||||
|
CMP_EQ_REAL_OBJ(0xE2),
|
||||||
|
CMP_NEQ_REAL_OBJ(0xE3),
|
||||||
|
CMP_LT_REAL_OBJ(0xE4),
|
||||||
|
CMP_LTE_REAL_OBJ(0xE5),
|
||||||
|
CMP_GT_REAL_OBJ(0xE6),
|
||||||
|
CMP_GTE_REAL_OBJ(0xE7),
|
||||||
|
UNBOX_INT_OBJ(0xF2),
|
||||||
|
UNBOX_REAL_OBJ(0xF3),
|
||||||
|
ADD_INT_OBJ(0xE8),
|
||||||
|
SUB_INT_OBJ(0xE9),
|
||||||
|
MUL_INT_OBJ(0xEA),
|
||||||
|
DIV_INT_OBJ(0xEB),
|
||||||
|
MOD_INT_OBJ(0xEC),
|
||||||
|
ADD_REAL_OBJ(0xED),
|
||||||
|
SUB_REAL_OBJ(0xEE),
|
||||||
|
MUL_REAL_OBJ(0xEF),
|
||||||
|
DIV_REAL_OBJ(0xF0),
|
||||||
|
MOD_REAL_OBJ(0xF1),
|
||||||
|
|
||||||
|
NOT_BOOL(0x70),
|
||||||
|
AND_BOOL(0x71),
|
||||||
|
OR_BOOL(0x72),
|
||||||
|
CMP_LT_OBJ(0x73),
|
||||||
|
CMP_LTE_OBJ(0x74),
|
||||||
|
CMP_GT_OBJ(0x75),
|
||||||
|
CMP_GTE_OBJ(0x76),
|
||||||
|
ADD_OBJ(0x77),
|
||||||
|
SUB_OBJ(0x78),
|
||||||
|
MUL_OBJ(0x79),
|
||||||
|
DIV_OBJ(0x7A),
|
||||||
|
MOD_OBJ(0x7B),
|
||||||
|
CONTAINS_OBJ(0x7C),
|
||||||
|
ASSIGN_OP_OBJ(0x7D),
|
||||||
|
|
||||||
|
JMP(0x80),
|
||||||
|
JMP_IF_TRUE(0x81),
|
||||||
|
JMP_IF_FALSE(0x82),
|
||||||
|
JMP_IF_EQ_INT(0xD0),
|
||||||
|
JMP_IF_NEQ_INT(0xD1),
|
||||||
|
JMP_IF_LT_INT(0xD2),
|
||||||
|
JMP_IF_LTE_INT(0xD3),
|
||||||
|
JMP_IF_GT_INT(0xD4),
|
||||||
|
JMP_IF_GTE_INT(0xD5),
|
||||||
|
RET(0x83),
|
||||||
|
RET_VOID(0x84),
|
||||||
|
RET_LABEL(0xBA),
|
||||||
|
PUSH_SCOPE(0x85),
|
||||||
|
POP_SCOPE(0x86),
|
||||||
|
PUSH_SLOT_PLAN(0x87),
|
||||||
|
POP_SLOT_PLAN(0x88),
|
||||||
|
DECL_LOCAL(0x89),
|
||||||
|
DECL_EXT_PROPERTY(0x8A),
|
||||||
|
DECL_DELEGATED(0x8B),
|
||||||
|
DECL_DESTRUCTURE(0x8C),
|
||||||
|
PUSH_TRY(0x8D),
|
||||||
|
POP_TRY(0x8E),
|
||||||
|
CLEAR_PENDING_THROWABLE(0x8F),
|
||||||
|
|
||||||
|
CALL_DIRECT(0x90),
|
||||||
|
ASSIGN_DESTRUCTURE(0x91),
|
||||||
|
CALL_MEMBER_SLOT(0x92),
|
||||||
|
CALL_SLOT(0x93),
|
||||||
|
CALL_BRIDGE_SLOT(0x94),
|
||||||
|
|
||||||
|
GET_INDEX(0xA2),
|
||||||
|
SET_INDEX(0xA3),
|
||||||
|
LIST_LITERAL(0xA5),
|
||||||
|
GET_MEMBER_SLOT(0xA8),
|
||||||
|
SET_MEMBER_SLOT(0xA9),
|
||||||
|
GET_CLASS_SCOPE(0xAA),
|
||||||
|
SET_CLASS_SCOPE(0xAB),
|
||||||
|
GET_DYNAMIC_MEMBER(0xAC),
|
||||||
|
SET_DYNAMIC_MEMBER(0xAD),
|
||||||
|
CALL_DYNAMIC_MEMBER(0xAE),
|
||||||
|
|
||||||
|
RESOLVE_SCOPE_SLOT(0xB1),
|
||||||
|
LOAD_OBJ_ADDR(0xB2),
|
||||||
|
STORE_OBJ_ADDR(0xB3),
|
||||||
|
LOAD_INT_ADDR(0xB4),
|
||||||
|
STORE_INT_ADDR(0xB5),
|
||||||
|
LOAD_REAL_ADDR(0xB6),
|
||||||
|
STORE_REAL_ADDR(0xB7),
|
||||||
|
LOAD_BOOL_ADDR(0xB8),
|
||||||
|
STORE_BOOL_ADDR(0xB9),
|
||||||
|
THROW(0xBB),
|
||||||
|
RETHROW_PENDING(0xBC),
|
||||||
|
DECL_ENUM(0xBE),
|
||||||
|
ITER_PUSH(0xBF),
|
||||||
|
ITER_POP(0xC0),
|
||||||
|
ITER_CANCEL(0xC1),
|
||||||
|
DELEGATED_GET_LOCAL(0xC2),
|
||||||
|
DELEGATED_SET_LOCAL(0xC3),
|
||||||
|
BIND_DELEGATE_LOCAL(0xC4),
|
||||||
|
DECL_FUNCTION(0xC5),
|
||||||
|
DECL_CLASS(0xC6),
|
||||||
|
DECL_CLASS_FIELD(0xC7),
|
||||||
|
DECL_CLASS_DELEGATED(0xC8),
|
||||||
|
DECL_CLASS_INSTANCE_INIT(0xC9),
|
||||||
|
DECL_CLASS_INSTANCE_FIELD(0xCA),
|
||||||
|
DECL_CLASS_INSTANCE_PROPERTY(0xCB),
|
||||||
|
DECL_CLASS_INSTANCE_DELEGATED(0xCC),
|
||||||
|
DECL_INSTANCE_FIELD(0xCD),
|
||||||
|
DECL_INSTANCE_PROPERTY(0xCE),
|
||||||
|
DECL_INSTANCE_DELEGATED(0xCF),
|
||||||
|
;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val byCode: Map<Int, Opcode> = values().associateBy { it.code }
|
||||||
|
fun fromCode(code: Int): Opcode? = byCode[code]
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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.bytecode
|
||||||
|
|
||||||
|
import net.sergeych.lyng.Scope
|
||||||
|
import net.sergeych.lyng.obj.ObjRecord
|
||||||
|
|
||||||
|
internal suspend fun seedFrameLocalsFromScope(frame: CmdFrame, scope: Scope) {
|
||||||
|
val localNames = frame.fn.localSlotNames
|
||||||
|
if (localNames.isEmpty()) return
|
||||||
|
val base = frame.fn.scopeSlotCount
|
||||||
|
for (i in localNames.indices) {
|
||||||
|
val name = localNames[i] ?: continue
|
||||||
|
val slotType = frame.getLocalSlotTypeCode(i)
|
||||||
|
if (slotType != SlotType.UNKNOWN.code && slotType != SlotType.OBJ.code) continue
|
||||||
|
if (slotType == SlotType.OBJ.code && frame.frame.getRawObj(i) != null) continue
|
||||||
|
val record = scope.getLocalRecordDirect(name)
|
||||||
|
?: scope.chainLookupIgnoreClosure(name, followClosure = true)
|
||||||
|
?: continue
|
||||||
|
val value = if (record.type == ObjRecord.Type.Delegated || record.type == ObjRecord.Type.Property) {
|
||||||
|
scope.resolve(record, name)
|
||||||
|
} else {
|
||||||
|
record.value
|
||||||
|
}
|
||||||
|
if (value is net.sergeych.lyng.FrameSlotRef && value.refersTo(frame.frame, base + i)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
frame.setObjUnchecked(base + i, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov
|
||||||
|
*
|
||||||
|
* 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.bytecode
|
||||||
|
|
||||||
|
enum class SlotType(val code: Byte) {
|
||||||
|
UNKNOWN(0),
|
||||||
|
OBJ(1),
|
||||||
|
INT(2),
|
||||||
|
REAL(3),
|
||||||
|
BOOL(4),
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user