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:
+56
-8
@@ -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 =
|
||||
@@ -113,14 +113,45 @@ proc runFile*(inputPath: string) =
|
||||
let baseName = inputPath.splitFile.name
|
||||
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)
|
||||
if compileResult != 0:
|
||||
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,17 +230,34 @@ 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:
|
||||
case mode
|
||||
of rmHuman: runHumanRepl(state)
|
||||
of rmJson: runJsonRepl(state)
|
||||
of rmEdn: runJsonRepl(state)
|
||||
if tcpPort > 0:
|
||||
runTcpRepl(state, tcpPort)
|
||||
else:
|
||||
case mode
|
||||
of rmHuman: runHumanRepl(state)
|
||||
of rmJson: runJsonRepl(state)
|
||||
of rmEdn: runJsonRepl(state)
|
||||
finally:
|
||||
state.cleanup()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user