Add AI-first REPL with JSON mode

This commit is contained in:
2026-05-08 16:51:05 +03:00
parent 87d6028487
commit 5971c062d1
5 changed files with 595 additions and 363 deletions
+39 -23
View File
@@ -1,17 +1,15 @@
# Clojure/Nim Compiler
import os, osproc
import reader, emitter, types, macros
# Clojure/Nim — AI-First Clojure Compiler
import os, osproc, strutils
import reader, emitter, types, repl
proc getLibPath(): string =
let appDir = getAppDir()
# Try relative to binary location
let candidate1 = appDir / "lib"
let candidate2 = appDir.parentDir / "lib"
let candidate3 = getCurrentDir() / "lib"
if dirExists(candidate1): return candidate1
if dirExists(candidate2): return candidate2
if dirExists(candidate3): return candidate3
# Fallback: assume lib is in current dir
return "lib"
proc compileFile*(inputPath: string, outputPath: string) =
@@ -55,36 +53,38 @@ proc runFile*(inputPath: string) =
quit(1)
proc main() =
initBuiltinMacros()
let args = commandLineParams()
if args.len == 0:
echo "Clojure/Nim Compiler"
echo "Clojure/Nim — AI-First Clojure Compiler"
echo ""
echo "Usage:"
echo " cljnim compile <file.clj> [output.nim] Compile to Nim"
echo " cljnim run <file.clj> Compile and run"
echo " cljnim compile <file.clj> [output.nim] Compile to Nim source"
echo " cljnim run <file.clj> Compile and run binary"
echo " cljnim read <file.clj> Parse and print AST"
echo " cljnim repl [--json] Start REPL (human or AI mode)"
echo " cljnim -e '<code>' Evaluate expression"
echo " cljnim -M -e '<code>' Evaluate with full runtime"
echo ""
echo "REPL Commands:"
echo " :quit, :q Exit REPL"
echo " :defs List defined vars"
echo " :clear Clear definitions"
echo " :ns Show current namespace"
echo ""
echo "AI Mode (JSON):"
echo " cljnim repl --json"
echo " Input: {\"op\":\"eval\",\"form\":\"(+ 1 2)\"}"
echo " Output: {\"status\":\"ok\",\"result\":{...},\"meta\":{...}}"
quit(0)
let cmd = args[0]
# Handle -e and -M -e flags
if cmd == "-e" or cmd == "-M":
var code = ""
var idx = 1
if cmd == "-M" and args.len > 1 and args[1] == "-e":
idx = 2
elif cmd == "-e":
idx = 1
else:
stderr.writeLine("Usage: cljnim [-M] -e '<code>'")
quit(1)
if idx >= args.len:
# Handle -e flag
if cmd == "-e":
if args.len < 2:
stderr.writeLine("Error: Missing expression after -e")
quit(1)
code = args[idx]
let code = args[1]
let tmpDir = getTempDir()
let nimPath = tmpDir / "cljnim_expr.nim"
let binPath = tmpDir / "cljnim_expr"
@@ -122,6 +122,22 @@ proc main() =
for form in forms:
echo $form
of "repl":
var mode = rmHuman
for i in 1..<args.len:
if args[i] == "--json":
mode = rmJson
elif args[i] == "--edn":
mode = rmEdn
var state = initReplState(mode)
try:
case mode
of rmHuman: runHumanRepl(state)
of rmJson: runJsonRepl(state)
of rmEdn: runJsonRepl(state)
finally:
state.cleanup()
else:
stderr.writeLine("Unknown command: " & cmd)
quit(1)