# Autolang — Quick Rules for AI

You are writing **Autolang** scripts. Autolang syntax is close to Kotlin/TypeScript. Follow these rules exactly.

---

## Core Rules

- No semicolons.
- Statically typed. Declare types explicitly when needed.
- File executes top-to-bottom. No `main()` entry point.
- Output ONLY via `println()` or `print()`. Anything not printed is invisible.

---

## Variables

```
val x = 10          // immutable
var y = 20          // mutable
val name: String = "Alice"
var count: Int = 0
```

Nullable: use `?` suffix on type or after `val`/`var`.

```
var email: String? = null
val id! = 42        // non-null inferred
val email? = "a@b"  // nullable inferred
```

---

## Null Safety

| Autolang | Meaning |
|----------|---------|
| `x?.method()` | safe call — returns null if x is null |
| `x ?? default` | null coalescing — use `??` NOT `?:` |
| `x!.method()` | non-null assert — crashes if null |

```
val len = name?.size() ?? 0
```

---

## Types

| Type | Notes |
|------|-------|
| `Int` | 64-bit integer |
| `Float` | 64-bit float |
| `Bool` | `true` / `false` |
| `String` | immutable |
| `Any` | any type |

Convert: `Int(3.14)` → `3`, `Float(42)` → `42.0`

---

## String Interpolation

```
println("Hello $name")
println("Sum: ${a + b}")
```

---

## Control Flow

```
// if as expression
val label = if (score >= 50) "pass" else "fail"

// when (replaces switch, no fall-through)
val day = when (n) {
    1 -> "Mon"
    2 -> "Tue"
    else -> "Other"
}

// when with conditions
val grade = when {
    score >= 90 -> "A"
    score >= 80 -> "B"
    else -> "F"
}

// loops
for (item in list) { ... }
while (condition) { ... }
```

---

## Functions

```
fun greet(name: String) { println("Hello $name") }
fun add(a: Int, b: Int): Int = a + b
fun connect(host: String = "localhost", port: Int = 8080): String = "$host:$port"
```

---

## Closures

**Always include `||` delimiters**, even with no parameters.

```
val sayHi = {|| println("Hi") }
val square = {|x: Int| x * x }
val check = {|n: Int| -> {
    if (n % 2 == 0) println("Even") else println("Odd")
}}
```

---

## Classes

```
// Primary constructor (preferred)
class Person(val name: String, var age: Int) {
    fun greet() { println("I'm $name") }
}

// Inheritance — requires secondary constructor + super() first
class Cat extends Animal {
    constructor() { super("Cat") }

    @override
    fun sound() { println("Meow") }
}
```

Access: `public` (default), `private`, `protected`.
Static: `static val VERSION = 1` → access via `Config.VERSION`.

---

## Collections

```
// Array
val nums = <Int>[1, 2, 3]
nums.add(4)
nums.filter {|v| v > 1 }
nums.forEach {|v, i| println("$i: $v") }

// Map
val m = <String, Int>{"a": 1, "b": 2}
m["a"]                        // get (returns V?)
m.getOrDefault("c", 0)

// Set
val s = <Int>{1, 2, 3}
s.contains(2)
"x" in s
```

---

## Imports

Use `@import` at the top of the file. No `import`, no `require`.

```
@import("std/math")
@import("std/date")
@import("std/json")
```

Host-registered capabilities are available by name without import — the developer binds them for you.

---

## Standard Library Highlights

**Math**: `Math.abs`, `Math.min`, `Math.max`, `Math.round`, `Math.floor`, `Math.ceil`, `Math.pow`, `Math.sqrt`, `Math.random()`

**Date**:
```
@import("std/date")
val now = Date.now()
println(now.format("%Y-%m-%d %H:%M:%S"))
println(now.getYear())
```

**Json**:
```
@import("std/json")
val data = Json.parse("{\"key\": 1}")
data.get("key").asInt()
```

---

## Common Mistakes to Avoid

| Wrong | Correct |
|-------|---------|
| `x ?: 0` | `x ?? 0` |
| `{x -> x * 2}` | `{|x: Int| x * 2}` |
| `{}` (empty closure) | `{|| }` |
| `import "std/math"` | `@import("std/math")` |
| Smart cast after `is` | Always cast explicitly with `as` |
| Return value without `println` | Always `println()` what you need |

---

## Annotations

```
@override        // mark overriding method
@no_override     // prevent subclass override
@no_extends      // seal a class
@no_constructor  // no instantiation (utility class)
```
