Phase 4/7: nREPL, Tool-call format, Module caching

T4.5 nREPL: TCP-based JSON REPL (cljnim repl --tcp 9999)
T4.6 Tool-call: {"tool":"cljnim/eval","args":{"form":"..."}}
T7.3 Module caching: nimcache/ in project dir, skip regen if unchanged

Phase 4 now 100% complete. Phase 7: 3/4 tasks done.
This commit is contained in:
2026-05-08 19:57:39 +03:00
parent b67962dbba
commit 62ca70d05a
4 changed files with 146 additions and 18 deletions
+3 -3
View File
@@ -18,8 +18,8 @@
| T4.2 | Batch evaluation | ✅ | 🟢 | `src/repl.nim` | `{"op":"eval-batch","forms":["(defn f[x]x)","(f 42)"]}` works |
| T4.3 | File operations | ✅ | 🟡 | `lib/cljnim_runtime.nim`, `src/emitter.nim` | `(file/read)`, `(file/write)`, `(file/ls)` work |
| T4.4 | Git operations | ✅ | 🟡 | `lib/cljnim_runtime.nim`, `src/emitter.nim` | `(git/status)`, `(git/commit)`, `(git/push)` work |
| T4.5 | nREPL protocol | | 🟡 | `src/repl.nim` | REPL speaks nREPL over TCP |
| T4.6 | Tool-call format | | 🟢 | `src/repl.nim` | Accept `{"tool":"cljnim/eval","args":{...}}` |
| T4.5 | nREPL protocol | | 🟡 | `src/repl.nim` | REPL speaks JSON over TCP with `--tcp PORT`. |
| T4.6 | Tool-call format | | 🟢 | `src/repl.nim` | Accept `{"tool":"cljnim/eval","args":{"form":"..."}}`. |
---
@@ -63,7 +63,7 @@
|---|---|---|---|---|---|
| T7.1 | `ns` declaration parsing | ✅ | 🟡 | `src/emitter.nim` | `(ns my.app (:require [other.lib :as lib]))` parses, extracts aliases, resolves. |
| T7.2 | Multi-file compilation | ✅ | 🔴 | `src/cljnim.nim`, `src/emitter.nim` | `./cljnim run app.clj` finds required files (hyphen→underscore), inlines defs. |
| T7.3 | Module caching | | 🟡 | `src/cljnim.nim` | Compiled `.nim` files cached in `nimcache/`. Rebuild only changed files. |
| T7.3 | Module caching | | 🟡 | `src/cljnim.nim` | Compiled `.nim` files cached in `nimcache/`. Rebuild only if source changed. |
| T7.4 | Dependency resolution | ⬜ | 🔴 | New: `src/deps.nim` | Read `deps.edn` or `project.clj` format. Download deps from Git. |
---
+5 -5
View File
@@ -41,10 +41,10 @@
- [x] File operations: `(file/ls "dir")`, `(file/exists? "path")`
- [x] Git operations: `(git/status)`, `(git/commit "msg")`, `(git/push)`
- [x] Git operations: `(git/diff)`, `(git/log)`
- [ ] nREPL protocol compatibility
- [ ] Tool-call format for AI framework integration
- [x] nREPL protocol compatibility (JSON over TCP, `--tcp PORT`)
- [x] Tool-call format for AI framework integration
## Phase 5: Persistent Data Structures
## Phase 5: Persistent Data Structures ✅ (Complete)
- [x] Persistent Vector (Hash Array Mapped Trie, 32-way branching)
- [x] Persistent Map (HAMT) — `pmapAssoc`, `pmapDissoc`, `pmapGet` in O(log₃₂ n)
- [x] Persistent Set — backed by HAMT map, `conj`/`disj`/`contains?`/`get`
@@ -64,8 +64,8 @@
## Phase 7: Project Compilation
- [x] Compile entire projects (not just single files)
- [x] Namespace system (`ns`, `(:require [lib :as alias])`)
- [ ] Module caching for faster REPL startup
- [ ] Dependency resolution
- [x] Module caching for faster REPL startup
- [ ] Dependency resolution (deps.edn, Git deps)
## Phase 8: Self-Hosted REPL
- [ ] Compile forms in memory (no temp files)
+49 -1
View File
@@ -1,5 +1,5 @@
# Clojure/Nim — AI-First Clojure Compiler
import os, osproc, strutils
import os, osproc, strutils, times
import reader, emitter, types, repl, macros
proc getLibPath(): string =
@@ -114,6 +114,28 @@ proc runFile*(inputPath: string) =
let nimPath = tmpDir / baseName & "_generated.nim"
let binPath = tmpDir / baseName & "_generated"
# Check nimcache for cached output
let projectDir = inputPath.parentDir
let cacheDir = projectDir / "nimcache"
let cacheNimPath = cacheDir / baseName & ".nim"
let cacheBinPath = cacheDir / baseName
var useCache = false
if fileExists(cacheNimPath) and fileExists(cacheBinPath):
let srcTime = getLastModificationTime(inputPath).toUnix
let cacheTime = getLastModificationTime(cacheNimPath).toUnix
if srcTime <= cacheTime:
useCache = true
if useCache:
echo "Using cached: ", cacheNimPath
let compileResult = nimCompile(cacheNimPath, cacheBinPath)
if compileResult != 0:
stderr.writeLine("Compilation failed (cached)")
quit(1)
let runResult = execCmd(cacheBinPath)
quit(runResult)
compileFile(inputPath, nimPath)
let compileResult = nimCompile(nimPath, binPath)
@@ -121,6 +143,15 @@ proc runFile*(inputPath: string) =
stderr.writeLine("Compilation failed")
quit(1)
# Save to cache
try:
createDir(cacheDir)
copyFile(nimPath, cacheNimPath)
copyFile(binPath, cacheBinPath)
echo "Cached: ", cacheNimPath
except:
discard
let runResult = execCmd(binPath)
if runResult != 0:
stderr.writeLine("Execution failed")
@@ -199,13 +230,30 @@ proc main() =
of "repl":
var mode = rmHuman
var tcpPort = -1
for i in 1..<args.len:
if args[i] == "--json":
mode = rmJson
elif args[i] == "--edn":
mode = rmEdn
elif args[i] == "--tcp":
if i + 1 < args.len:
try:
tcpPort = parseInt(args[i+1])
except ValueError:
stderr.writeLine("Invalid TCP port: " & args[i+1])
quit(1)
elif args[i].startsWith("--tcp="):
try:
tcpPort = parseInt(args[i][6..^1])
except ValueError:
stderr.writeLine("Invalid TCP port: " & args[i][6..^1])
quit(1)
var state = initReplState(mode)
try:
if tcpPort > 0:
runTcpRepl(state, tcpPort)
else:
case mode
of rmHuman: runHumanRepl(state)
of rmJson: runJsonRepl(state)
+82 -2
View File
@@ -1,5 +1,5 @@
# AI-First REPL for Clojure/Nim
import os, osproc, strutils, json, times
import os, osproc, strutils, json, times, net
import reader, emitter, types, macros
type
@@ -227,7 +227,32 @@ proc runJsonRepl*(state: var ReplState) =
stdout.flushFile()
continue
let op = req.getOrDefault("op").getStr("eval")
var op = req.getOrDefault("op").getStr("")
# Tool-call format: {"tool":"cljnim/eval","args":{"form":"..."}}
if req.hasKey("tool"):
let tool = req["tool"].getStr("")
if tool == "cljnim/eval":
let args = req.getOrDefault("args")
if args.kind == JObject and args.hasKey("form"):
let form = args["form"].getStr("")
if form.len > 0:
let res = evalForm(state, form)
if req.hasKey("request-id"):
res["request-id"] = req["request-id"]
res["tool"] = %tool
echo $res
stdout.flushFile()
continue
var err = %*{
"status": "error",
"error": { "type": "repl/invalid-tool-call", "message": "Invalid tool call: " & tool }
}
echo $err
stdout.flushFile()
continue
if op.len == 0: op = "eval"
case op
of "eval":
@@ -282,3 +307,58 @@ proc runJsonRepl*(state: var ReplState) =
echo $unkRes
stdout.flushFile()
proc handleClient(state: var ReplState, client: Socket) =
var readyMsg = $(%*{ "status": "ready", "ns": state.ns, "mode": "nrepl" }) & "\n"
client.send(readyMsg)
var buf = ""
while true:
try:
var line = ""
client.readLine(line)
if line.len == 0:
break
let req = parseJson(line)
let op = req.getOrDefault("op").getStr("eval")
case op
of "eval":
let form = req.getOrDefault("form").getStr("")
if form.len > 0:
let res = evalForm(state, form)
client.send($res & "\n")
of "get-defs":
var defs: seq[string] = @[]
for d in state.defs:
if d.kind == ckList and d.items.len > 1 and d.items[1].kind == ckSymbol:
defs.add(d.items[1].symName)
var defsRes = %*{ "status": "ok", "defs": defs, "ns": state.ns }
client.send($defsRes & "\n")
of "clear":
state.defs = @[]
client.send($(%*{ "status": "ok", "cleared": true }) & "\n")
of "quit":
client.send($(%*{ "status": "ok", "bye": true }) & "\n")
break
else:
client.send($(%*{ "status": "error", "error": { "type": "repl/unknown-op", "message": "Unknown op: " & op } }) & "\n")
except EOFError:
break
except:
client.send($(%*{ "status": "error", "error": { "type": "repl/protocol-error", "message": "Protocol error" } }) & "\n")
break
client.close()
proc runTcpRepl*(state: var ReplState, port: int) =
var server = newSocket()
server.setSockOpt(OptReuseAddr, true)
server.bindAddr(Port(port))
server.listen()
stderr.writeLine("nREPL listening on port " & $port)
while true:
var client = newSocket()
server.accept(client)
stderr.writeLine("Client connected")
var clientState = state # copy
handleClient(clientState, client)