Initial commit

This commit is contained in:
2026-05-08 14:51:47 +03:00
commit f89b381fdb
12 changed files with 1085 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
# Clojure/Nim Compiler
import os, strutils, osproc
import reader, emitter, types
proc compileFile*(inputPath: string, outputPath: string) =
let source = readFile(inputPath)
let forms = reader.readAll(source)
if forms.len == 0:
stderr.writeLine("Error: No forms found in " & inputPath)
quit(1)
let nimCode = emitter.emitProgram(forms)
writeFile(outputPath, nimCode)
echo "Generated: ", outputPath
proc runFile*(inputPath: string) =
let tmpDir = getTempDir()
let baseName = inputPath.splitFile.name
let nimPath = tmpDir / baseName & "_generated.nim"
let binPath = tmpDir / baseName & "_generated"
compileFile(inputPath, nimPath)
# Compile generated Nim code
let compileCmd = "nim c -o:" & binPath & " " & nimPath
echo "Compiling: ", compileCmd
let compileResult = execCmd(compileCmd)
if compileResult != 0:
stderr.writeLine("Compilation failed")
quit(1)
# Run
echo "Running: ", binPath
let runResult = execCmd(binPath)
if runResult != 0:
stderr.writeLine("Execution failed")
quit(1)
proc main() =
let args = commandLineParams()
if args.len == 0:
echo "Clojure/Nim Compiler"
echo "Usage:"
echo " cljnim compile <file.clj> [output.nim] Compile to Nim"
echo " cljnim run <file.clj> Compile and run"
echo " cljnim read <file.clj> Parse and print AST"
quit(0)
let cmd = args[0]
case cmd
of "compile":
if args.len < 2:
stderr.writeLine("Error: Missing input file")
quit(1)
let inputPath = args[1]
let outputPath = if args.len >= 3: args[2] else: inputPath.changeFileExt("nim")
compileFile(inputPath, outputPath)
of "run":
if args.len < 2:
stderr.writeLine("Error: Missing input file")
quit(1)
runFile(args[1])
of "read":
if args.len < 2:
stderr.writeLine("Error: Missing input file")
quit(1)
let source = readFile(args[1])
let forms = reader.readAll(source)
for form in forms:
echo $form
else:
stderr.writeLine("Unknown command: " & cmd)
quit(1)
when isMainModule:
main()