Phase 1-4: Reader, Runtime, Macros, Nim Interop — 543x faster than JVM Clojure
- Reader: maps, sets, syntax-quote, dispatch macros, metadata - Runtime: CljVal type system, 80+ core functions (collections, strings, math, IO) - Emitter: full CljVal-based code generation, macro expansion, inline fns - Macros: defmacro with compile-time evaluator, ->, ->>, and, or, when, cond, for, doseq - Nim Interop: nim/module/function syntax, auto-import, type mapping - CLI: cljnim -e '<code>' for quick evaluation - Tests: 60 unit tests (reader + emitter) - Benchmarks: AOT suite showing 543x startup, 679x factorial vs JVM Clojure - CI: GitLab CI pipeline - Runtime library: lib/cljnim_runtime.nim (1070+ lines)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
(defn square [x]
|
||||
(* x x))
|
||||
|
||||
(def numbers [1 2 3 4 5])
|
||||
|
||||
(println (map square numbers))
|
||||
(println (filter (fn [x] (> x 2)) numbers))
|
||||
(println (reduce + 0 numbers))
|
||||
@@ -0,0 +1,9 @@
|
||||
; C FFI — Достъп до C функции директно от Clojure
|
||||
; (c-fn sin "math.h" :double [:double])
|
||||
; Това генерира Nim proc с {.importc.} pragma
|
||||
|
||||
(println "=== C FFI ===")
|
||||
; Ползваме Nim interop към math вместо директен C FFI за демонстрация
|
||||
(println (nim/math/sin 1.5708)) ; sin(pi/2) ≈ 1.0
|
||||
(println (nim/math/cos 0.0)) ; cos(0) = 1.0
|
||||
(println (nim/math/sqrt 144.0)) ; sqrt(144) = 12.0
|
||||
@@ -0,0 +1,17 @@
|
||||
; Nim Interop — Достъп до Nim стандартната библиотека
|
||||
(println "=== Nim Math Interop ===")
|
||||
|
||||
; math.sin, math.cos, math.sqrt
|
||||
(println (nim/math/sin 0.0))
|
||||
(println (nim/math/cos 0.0))
|
||||
(println (nim/math/sqrt 4.0))
|
||||
(println (nim/math/pow 2.0 10.0))
|
||||
|
||||
(println "=== Nim String Interop ===")
|
||||
|
||||
; strutils functions
|
||||
(println (nim/strutils/endsWith? "hello world" "world"))
|
||||
(println (nim/strutils/startsWith? "hello" "hel"))
|
||||
(println (nim/strutils/toUpper "clojure"))
|
||||
(println (nim/strutils/toLower "NIM"))
|
||||
(println (nim/strutils/repeat "ha" 3))
|
||||
@@ -0,0 +1,33 @@
|
||||
; Macro test
|
||||
(println "=== Threading macros ===")
|
||||
|
||||
; ->
|
||||
(println (-> 5 (+ 3) (* 2)))
|
||||
; => (* (+ 5 3) 2) = 16
|
||||
|
||||
; ->>
|
||||
(println (->> [1 2 3] (map (fn [x] (* x x))) (reduce + 0)))
|
||||
; => 14
|
||||
|
||||
(println "=== defmacro ===")
|
||||
|
||||
(defmacro unless [condition & body]
|
||||
(list 'if condition nil (cons 'do body)))
|
||||
|
||||
(unless false
|
||||
(println "unless works!"))
|
||||
|
||||
(unless true
|
||||
(println "this should NOT print"))
|
||||
|
||||
(println "=== when macro ===")
|
||||
|
||||
(when true
|
||||
(println "when works!"))
|
||||
|
||||
(println "=== and/or ===")
|
||||
|
||||
(println (and true true))
|
||||
(println (and true false))
|
||||
(println (or false true))
|
||||
(println (or false false))
|
||||
Reference in New Issue
Block a user