5.9 KiB
5.9 KiB
Lyng Language AI Specification (V1.3)
High-density specification for LLMs. Reference this for all Lyng code generation.
1. Core Philosophy & Syntax
- Everything is an Expression: Blocks,
if,when,for,while,do-whilereturn their last expression (orvoid). - Loops with
else:for,while, anddo-whilesupport an optionalelseblock.elseexecutes only if the loop finishes normally (without abreak).break <value>exits the loop and sets its return value.- Loop Return Value:
- Value from
break <value>. - Result of
elseblock (if loop finished normally andelseexists). - Result of the last iteration (if loop finished normally and no
else). void(if loop body never executed and noelse).
- Value from
- Implicit Coroutines: All functions are coroutines. No
async/await. Uselaunch { ... }(returnsDeferred) orflow { ... }. - Variables:
val(read-only),var(mutable). Supports late-initvalin classes (must be assigned ininitor body). - Serialization: Use
@Transientattribute beforeval/varor 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). - Equality:
==(equals),!=(not equals),===(ref identity),!==(ref not identity). - Comparison:
<,>,<=,>=,<=>(shuttle/spaceship, returns -1, 0, 1). - Destructuring:
val [a, b, rest...] = list. Supports nested[a, [b, c]]and splats.
2. Object-Oriented Programming (OOP)
- Multiple Inheritance: Supported with C3 MRO (Python-style). Diamond-safe.
- Header Arguments:
class Foo(a, b) : Base(a)defines fieldsa,band passesatoBase. - Members:
fun name(args) { ... },val,var,static val,static fun. - Properties (Get/Set): Pure accessors, no auto-backing fields.
var age get() = _age private set(v) { if(v >= 0) _age = v } // Laconic syntax: val area get = π * r * r - Mandatory
override: Required for all members existing in the ancestor chain. - Visibility:
public(default),protected(subclasses and ancestors for overrides),private(this class instance only).private set/protected setallowed on properties. - Disambiguation:
this@Base.member()or(obj as Base).member().asreturns a qualified view. - Abstract/Interface:
interfaceis a synonym forabstract class. Both support state and constructors. - Extensions:
fun Class.ext()orval Class.ext get = .... Scope-isolated.
3. Delegation (by)
Unified model for val, var, and fun.
val x by MyDelegate()
var y by Map() // Uses "y" as key in map
fn f(a, b) by RemoteProxy() // Calls Proxy.invoke(thisRef, "f", a, b)
Delegate Methods:
getValue(thisRef, name): forval/var.setValue(thisRef, name, val): forvar.invoke(thisRef, name, args...): forfn(called ifgetValueis absent).bind(name, access, thisRef): optional hook called at declaration/binding time.accessisDelegateAccess.Val,Var, orCallable.
4. Standard Library & Functional Built-ins
- Scope Functions:
obj.let { it... }: result of block.itisobj.obj.apply { this... }: returnsobj.thisisobj.obj.also { it... }: returnsobj.itisobj.obj.run { this... }: result of block.thisisobj.with(obj, { ... }): result of block.thisisobj.
- Functional:
forEach,map,filter,any,all,sum,count,sortedBy,flatten,flatMap,associateBy. - Lazy:
val x = cached { expensive() }(call asx()) orval x by lazy { ... }. - Collections:
List([a, b]),Map(Map(k => v)),Set(Set(a, b)).MapEntry(k => v).
5. Patterns & Shorthands
- Map Literals:
{ key: value, identifier: }(identifier shorthandx:isx: x). Empty map isMap(). - Named Arguments:
fun(y: 10, x: 5). Shorthand:Point(x:, y:). - Varargs & Splats:
fun f(args...),f(...otherList). - Labels:
loop@ for(x in list) { if(x == 0) break@loop }. - Dynamic:
val d = dynamic { get { name -> ... } }allowsd.anyName.
6. Operators & Methods to Overload
| Op | Method | Op | Method |
|---|---|---|---|
+ |
plus |
== |
equals |
- |
minus |
<=> |
compareTo |
* |
mul |
[] |
getAt / putAt |
/ |
div |
! |
logicalNot |
% |
mod |
- |
negate (unary) |
=~ |
operatorMatch |
+= |
plusAssign |
7. Common Snippets
// Multiple Inheritance and Properties
class Warrior(id, hp) : Character(id), HealthPool(hp) {
override fun toString() = "Warrior #%s (%s HP)"(id, hp)
}
// Map entry and merging
val m = Map("a" => 1) + ("b" => 2)
m += "c" => 3
// Destructuring with splat
val [first, middle..., last] = [1, 2, 3, 4, 5]
// Safe Navigation and Elvis
val companyName = person?.job?.company?.name ?: "Freelancer"
8. Standard Library Discovery
To collect data on the standard library and available APIs, AI should inspect:
- Global Symbols:
lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt(root functions likeprintln,sqrt,assert). - Core Type Members:
lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/*.kt(e.g.,ObjList.kt,ObjString.kt,ObjMap.kt) for methods on built-in types. - Lyng-side Extensions:
lynglib/stdlib/lyng/root.lyngfor high-level functional APIs (e.g.,map,filter,any,lazy). - I/O & Processes:
lyngio/src/commonMain/kotlin/net/sergeych/lyng/io/forfsandprocessmodules. - Documentation:
docs/*.md(e.g.,tutorial.md,lyngio.md) for high-level usage and module overviews.