latch-lang 0.4.3

Latch — a minimal scripting language for local automation and tool orchestration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<p align="center">
  <img src="icon.png" width="120" alt="Latch Logo" />
</p>

<h1 align="center">Latch</h1>

<p align="center">
  <strong>A fast, lightweight scripting language for local automation, file operations, and task orchestration.</strong>
</p>

<p align="center">
  <a href="https://crates.io/crates/latch-lang"><img src="https://img.shields.io/crates/v/latch-lang.svg" alt="crates.io" /></a>
  <a href="https://github.com/kaelvalen/latch-lang/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT" /></a>
  <a href="#features"><img src="https://img.shields.io/badge/batteries-included-brightgreen.svg" alt="Batteries Included" /></a>
</p>

---

## Why Latch?

Latch is built to **replace shell scripts and Makefiles** for your automation tasks. It provides:

- **Zero dependencies** — Single binary, instant startup
- **Clear syntax** — Readable even for non-programmers
- **Built-in power** — File I/O, processes, HTTP, JSON, regex, parallel tasks
- **Error handling** — Try-catch, fallback values, defensive coalescing
- **Type safety** — Optional type annotations, caught at parse-time

### Write automation scripts faster

```latch
# Deploy app to production
deploy := fn(target) {
    config := json.parse(fs.read("config.json")) or {}
    
    files := fs.glob("dist/**/*")
    parallel f in files workers=8 {
        proc.exec("cp ${f} /opt/app/${f}")
    }
    
    print("✓ Deployed ${len(files)} files to ${target}")
}

deploy("production")
```

## Install

### With Cargo (recommended)

```sh
cargo install latch-lang
```

Then use the `latch` command:

```sh
latch version   # Show version
latch run script.lt  # Execute script
```

### From Source

```sh
git clone https://github.com/kaelvalen/latch-lang.git
cd latch-lang
cargo install --path .
```

### Verify Installation

```sh
$ latch version
latch v0.4.3
```

## Quick Start — Your First Script

Create `greet.lt`:

```latch
name := "Latch"
version := 0.4

# String interpolation
print("Welcome to ${name} v${version}!")

# List iteration
features := ["automation", "scripting", "orchestration"]
for feature in features {
    print("  • ${feature}")
}

# File operations
fs.write("log.txt", "Script ran at ${time.now()}")

# Process execution
result := proc.exec("echo Done!")
print(result.stdout)
```

Run it:

```sh
$ latch run greet.lt
Welcome to Latch v0.4!
  • automation
  • scripting
  • orchestration
Script ran at 2026-04-02 12:10:30
Done!
```

## Language Features at a Glance

| Category | Features |
|----------|----------|
| **Basics** | Variables, type annotations, string interpolation, comments |
| **Types** | `null`, `bool`, `int`, `float`, `string`, `list`, `dict`, `fn` |
| **Collections** | Lists `[1, 2, 3]`, Dicts `{"key": "val"}`, Ranges `1..10` |
| **Operators** | Arithmetic `+ - * / %`, Comparison `== != < > <= >=`, Logical `&& \|\| !` |
| **Smart Operators** | Null coalesce `??`, Error fallback `or`, Optional access `?.` |
| **Control Flow** | `if`/`else`, `for`/`in`, range loops `for i in 0..10` |
| **Functions** | Named functions, anonymous functions, parameters, return types |
| **Parallel** | `parallel` blocks with configurable worker pools |
| **Error Handling** | Try-catch-finally, error propagation, graceful defaults |
| **Built-ins** | 50+ functions for strings, lists, dicts, math, I/O |
| **Modules** | `fs`, `proc`, `http`, `json`, `csv`, `regex`, `time`, `hash`, `base64` and more |

## Common Tasks

### Read and Process a File

```latch
# Read JSON config
config := json.parse(fs.read("config.json")) or {"port": 8080}

# Search for patterns
lines := fs.read("data.txt") |> split("\n")
errors := filter(lines, fn(l) { return contains(l, "ERROR") })

print("Found ${len(errors)} errors")
for error in errors {
    print("  → ${error}")
}
```

### Run Shell Commands and Process Output

```latch
# Execute git command
result := proc.exec("git log --oneline -5")
commits := split(trim(result.stdout), "\n")

print("Latest 5 commits:")
for commit in commits {
    print("  ${commit}")
}
```

### Make HTTP Requests

```latch
# Fetch JSON from API
response := http.get("https://api.example.com/data")
if response.status == 200 {
    data := json.parse(response.body) or {}
    print("API Response: ${data}")
} else {
    print("Error: HTTP ${response.status}")
}
```

### Parallel File Processing

```latch
# Process many files in parallel with 4 workers
files := fs.glob("logs/*.txt")
results := []

parallel file in files workers=4 {
    content := fs.read(file)
    fs.write("${file}.processed", upper(content))
}

print("✓ Processed ${len(files)} files")
```

### Run Checks with Error Handling

```latch
# CI-style checks with try-catch
failed := false

try {
    # Check 1: Required files exist
    assert(fs.exists("Cargo.toml"), "Missing Cargo.toml")
    
    # Check 2: Tests pass
    result := proc.exec("cargo test")
    assert(result.exit_code == 0, "Tests failed")
    
    print("✓ All checks passed!")
} catch e {
    print("✗ Check failed: ${e}")
    failed = true
} finally {
    print("Cleanup...")
}

if failed {
    stop 1
}
```

## Documentation

- **[Complete Stdlib Reference]docs/stdlib.md** — All built-in functions and modules
- **[Examples]examples/** — Real-world scripts showcasing features
- **[GitHub Issues]https://github.com/kaelvalen/latch-lang/issues** — Questions & bug reports

## Examples Included

- `hello.lt` — Feature overview with print, math, loops, file I/O
- `ci-check.lt` — Run tests and verify required files
- `fetch-data.lt` — HTTP requests and JSON parsing
- `parallel-tasks.lt` — Pool-based parallel execution
| **While loops** | `while condition { ... }` |
| **Break/Continue** | `break`, `continue` |
| **Constants** | `const PI = 3.14` |
| **Generators/Yield** | `yield value` |
| **List comprehension** | `[x*2 for x in list if x > 0]` |
| **Default args** | `fn greet(name = "World")` |
| **Class/OOP** | `class Point { x: int }` |
| **Export/Import** | `export { foo }`, `import { foo } from "module"` |
| **Safe access** | `resp?.headers`, `val?.field` |
| **Pipe operator** | `list \|> sort() \|> filter(fn(x) { return x > 2 })` |
| **Membership test** | `"x" in list`, `"key" in dict` |
| **Range literal** | `1..10``[1, 2, ..., 9]` |
| **Compound assign** | `count += 1`, `total *= 2` |
| **Modulo** | `10 % 3``1` |
| **Exit codes** | `stop 0` / `stop 1` |
| **Null literal** | `x := null`, `x == null` |
| **File I/O** | `fs.read`, `fs.write`, `fs.append`, `fs.readlines`, `fs.exists`, `fs.glob`, `fs.mkdir`, `fs.remove`, `fs.stat` |
| **Shell commands** | `proc.exec("cmd")`, `proc.exec(["git", "status"])`, `proc.pipe([...])` |
| **HTTP** | `http.get(url)`, `http.post(url, body)` → HttpResponse |
| **JSON** | `json.parse(str)`, `json.stringify(value)` |
| **Env vars** | `env.get(key)`, `env.set(k, v)`, `env.list()` |
| **Path utils** | `path.join`, `path.basename`, `path.dirname`, `path.ext`, `path.abs` |
| **Time** | `time.now()`, `time.sleep(ms)` |
| **AI** | `ai.ask(prompt)`, `ai.summarize(text)` |
| **Index mutation** | `list[0] = 5`, `dict["key"] = val` |
| **Higher-order** | `sort(list)`, `filter(list, fn)`, `map(list, fn)`, `each(list, fn)` |
| **String utils** | `lower`, `upper`, `starts_with`, `ends_with`, `trim`, `split`, `replace` |
| **Comments** | `# hash` and `// line` comments |
| **REPL** | `latch repl` |

## CLI

```sh
latch run <file.lt>      # Run a script
latch check <file.lt>    # Static analysis (no execution)
latch repl               # Interactive REPL
latch version            # Print version
```

## Operators

| Operator | Description | Precedence |
|----------|-------------|------------|
| `\|>` | Pipe (inject as first arg) | 1 (lowest) |
| `or` | Error fallback | 2 |
| `??` | Null coalesce | 3 |
| `\|\|` | Logical OR | 4 |
| `&&` | Logical AND | 5 |
| `==` `!=` | Equality | 6 |
| `<` `>` `<=` `>=` `in` | Comparison / membership | 7 |
| `..` | Range | 8 |
| `+` `-` | Add / subtract / concat | 9 |
| `*` `/` `%` | Multiply / divide / modulo | 10 |
| `!` `-` | Unary not / negate | 11 |
| `.` `?.` `[]` `()` | Access / safe access / index / call | 12 (highest) |

Compound: `+=` `-=` `*=` `/=` `%=`

## Standard Library

### Built-in Functions

```python
print("hello")              # Print to stdout
len([1, 2, 3])              # → 3
str(42)                     # → "42"
int("7")                    # → 7
float("3.14")               # → 3.14
typeof(x)                   # → "string"
push([1, 2], 3)             # → [1, 2, 3]
keys({"a": 1})              # → ["a"]
values({"a": 1})            # → [1]
range(0, 5)                 # → [0, 1, 2, 3, 4]
split("a,b,c", ",")         # → ["a", "b", "c"]
trim("  hi  ")              # → "hi"
lower("HELLO")              # → "hello"
upper("hello")              # → "HELLO"
starts_with("hello", "he")  # → true
ends_with("hello", "lo")    # → true
contains("hello", "ell")    # → true
replace("foo", "o", "0")    # → "f00"
sort([3, 1, 2])             # → [1, 2, 3]
filter(list, fn(x) { return x > 0 })
map(list, fn(x) { return x * 2 })
each(list, fn(x) { print(x) })
```

### Modules

```python
# fs — File System
content := fs.read("file.txt")
fs.write("out.txt", content)
fs.append("log.txt", "new entry\n")
lines := fs.readlines("data.csv")
fs.exists("path")
files := fs.glob("**/*.lt")
fs.mkdir("build/output")
fs.remove("tmp/cache")
info := fs.stat("file.txt")     # → {size, is_file, is_dir, readonly}

# proc — Processes
result := proc.exec("ls -la")
result := proc.exec(["git", "status"])   # array form (no shell)
piped := proc.pipe(["cat log.txt", "grep ERROR", "wc -l"])

# http — HTTP Client (returns HttpResponse)
resp := http.get("https://api.example.com/data")
print(resp.status)     # 200
print(resp.body)       # response body
print(resp.headers)    # headers dict

resp := http.post("https://api.example.com", "{\"key\": \"value\"}")

# json — JSON
data := json.parse("{\"name\": \"latch\"}")
back := json.stringify(data)

# env — Environment Variables
home := env.get("HOME") or "/tmp"
env.set("MODE", "production")   # current process only
all := env.list()

# path — Path Utilities
full := path.join("/home", "user/file.txt")
print(path.basename("/a/b/c.txt"))   # → c.txt
print(path.dirname("/a/b/c.txt"))    # → /a/b
print(path.ext("file.tar.gz"))       # → gz

# time — Time
now := time.now()           # RFC 3339 timestamp
time.sleep(500)             # Sleep 500ms

# ai — AI (requires LATCH_AI_KEY env var)
answer := ai.ask("Explain Rust in one sentence")
summary := ai.summarize(fs.read("article.txt"))
```

## Error Messages

Latch produces structured, actionable errors:

```
[latch] Semantic Error
  file: deploy.lt
  line: 12  col: 5
  → result := undeclared_var + 1
  reason: Undefined variable 'undeclared_var'
  hint: Declare the variable first with ':='
```

## Parallel Execution

Parallel blocks run all workers to completion. If any worker fails, the first error is returned after every worker has finished — no silent partial failures.

```python
servers := ["web-1", "web-2", "web-3", "web-4"]
parallel s in servers workers=4 {
    proc.exec("ssh ${s} 'systemctl restart app'")
}
```

## Use as CI Exit Code

```python
result := proc.exec("cargo test")
if result.code != 0 {
    print("Tests failed!")
    stop 1
}
stop 0
```

## Examples

See the [examples/](examples/) directory:

- [`hello.lt`]examples/hello.lt — Feature showcase
- [`ci-check.lt`]examples/ci-check.lt — CI gate example
- [`v02_test.lt`]examples/v02_test.lt — v0.4.3 feature tests

## Full Reference

See [docs/stdlib.md](docs/stdlib.md) for the complete standard library reference.

## License

MIT — see [LICENSE](LICENSE)