Phase 4: Macro expansion in compiler
- resolve-macro: resolve symbols to macro vars via ctx - Macro expansion in analyze-form: detects macro heads, expands, re-analyzes - compile-ast: emits Janet data structures with resolved core fn values - compile-and-eval uses compile-ast (no source parse roundtrip) - eval-string routes macros through compiler (expanded at analyze time) - Fix - mapping: core-sub (core-- doesn't exist) - All 317 tests pass + 6 new Phase 4 macro tests
This commit is contained in:
parent
ab7ff85816
commit
a8c453183f
9 changed files with 448 additions and 188 deletions
|
|
@ -42,22 +42,24 @@
|
|||
|
||||
(defn eval-string
|
||||
"Evaluate a Clojure source string in a Jolt context.
|
||||
When :compile? is enabled, compiles to Janet source and evaluates via Janet.
|
||||
Stateful forms (def, defmacro, ns, deftype) always use the interpreter."
|
||||
When :compile? is enabled, compiles to Janet and evaluates.
|
||||
Macros are expanded at compile time.
|
||||
Context-modifying forms (ns, defmacro, deftype, require, in-ns, defmulti, defmethod)
|
||||
always use the interpreter."
|
||||
[ctx s]
|
||||
(let [compile? (get (ctx :env) :compile?)
|
||||
form (parse-string s)]
|
||||
(if (and compile? (array? form))
|
||||
# Check if this is a stateful form that needs the interpreter
|
||||
(let [first-form (first form)
|
||||
head-name (if (and (struct? first-form) (= :symbol (first-form :jolt/type)))
|
||||
(first-form :name)
|
||||
nil)]
|
||||
(if (or (= head-name "def") (= head-name "defmacro") (= head-name "ns")
|
||||
(= head-name "deftype") (= head-name "defmulti") (= head-name "defmethod")
|
||||
(= head-name "require") (= head-name "in-ns"))
|
||||
nil)
|
||||
stateful? (or (= head-name "defmacro") (= head-name "ns")
|
||||
(= head-name "deftype") (= head-name "defmulti") (= head-name "defmethod")
|
||||
(= head-name "require") (= head-name "in-ns"))]
|
||||
(if stateful?
|
||||
(eval-form ctx @{} form)
|
||||
(compile-and-eval form)))
|
||||
(compile-and-eval form ctx)))
|
||||
(eval-form ctx @{} form))))
|
||||
|
||||
(defn eval-string*
|
||||
|
|
|
|||
|
|
@ -1,10 +1,15 @@
|
|||
# Jolt Compiler
|
||||
# Source-to-source: Clojure forms → Janet source
|
||||
# Two-phase: analyze-form (classify) → emit-ast (generate)
|
||||
#
|
||||
# When ctx is passed to analyze-form, macros are expanded at analyze time.
|
||||
|
||||
(use ./types)
|
||||
(use ./core)
|
||||
|
||||
(def- core-renames
|
||||
@{"+" "core-+"
|
||||
"-" "core--"
|
||||
"-" "core-sub"
|
||||
"*" "core-*"
|
||||
"/" "core-/"
|
||||
"inc" "core-inc"
|
||||
|
|
@ -101,11 +106,125 @@
|
|||
(= name "defmulti") (= name "defmethod") (= name "locking")))
|
||||
|
||||
# ============================================================
|
||||
# Analyzer
|
||||
# Macro resolution
|
||||
# ============================================================
|
||||
|
||||
(defn- resolve-macro
|
||||
"Resolve a symbol struct to a macro var. Returns the var or nil."
|
||||
[ctx sym-s]
|
||||
(when ctx
|
||||
(let [name (sym-s :name)
|
||||
ns-sym (sym-s :ns)]
|
||||
(if ns-sym
|
||||
(let [target-ns (ctx-find-ns ctx ns-sym)
|
||||
v (ns-find target-ns name)]
|
||||
(if (and v (var-macro? v)) v))
|
||||
(let [current-ns-name (ctx-current-ns ctx)
|
||||
current-ns (ctx-find-ns ctx current-ns-name)
|
||||
v (ns-find current-ns name)]
|
||||
(if v
|
||||
(if (var-macro? v) v)
|
||||
(let [core-ns (ctx-find-ns ctx "clojure.core")
|
||||
cv (ns-find core-ns name)]
|
||||
(if (and cv (var-macro? cv)) cv))))))))
|
||||
|
||||
# ============================================================
|
||||
# Core function value lookup — resolved at compile time
|
||||
# ============================================================
|
||||
|
||||
(def- core-fn-values
|
||||
(let [t @{}]
|
||||
(put t "core-+" core-+)
|
||||
(put t "core-sub" core-sub)
|
||||
(put t "core-*" core-*)
|
||||
(put t "core-/" core-/)
|
||||
(put t "core-inc" core-inc)
|
||||
(put t "core-dec" core-dec)
|
||||
(put t "core-=" core-=)
|
||||
(put t "core-not=" core-not=)
|
||||
(put t "core-<" core-<)
|
||||
(put t "core->" core->)
|
||||
(put t "core-<=" core-<=)
|
||||
(put t "core->=" core->=)
|
||||
(put t "core-nil?" core-nil?)
|
||||
(put t "core-not" core-not)
|
||||
(put t "core-some?" core-some?)
|
||||
(put t "core-string?" core-string?)
|
||||
(put t "core-number?" core-number?)
|
||||
(put t "core-fn?" core-fn?)
|
||||
(put t "core-keyword?" core-keyword?)
|
||||
(put t "core-symbol?" core-symbol?)
|
||||
(put t "core-vector?" core-vector?)
|
||||
(put t "core-map?" core-map?)
|
||||
(put t "core-seq?" core-seq?)
|
||||
(put t "core-coll?" core-coll?)
|
||||
(put t "core-true?" core-true?)
|
||||
(put t "core-false?" core-false?)
|
||||
(put t "core-identical?" core-identical?)
|
||||
(put t "core-zero?" core-zero?)
|
||||
(put t "core-pos?" core-pos?)
|
||||
(put t "core-neg?" core-neg?)
|
||||
(put t "core-even?" core-even?)
|
||||
(put t "core-odd?" core-odd?)
|
||||
(put t "core-empty?" core-empty?)
|
||||
(put t "core-every?" core-every?)
|
||||
(put t "core-first" core-first)
|
||||
(put t "core-rest" core-rest)
|
||||
(put t "core-next" core-next)
|
||||
(put t "core-cons" core-cons)
|
||||
(put t "core-conj" core-conj)
|
||||
(put t "core-assoc" core-assoc)
|
||||
(put t "core-dissoc" core-dissoc)
|
||||
(put t "core-get" core-get)
|
||||
(put t "core-get-in" core-get-in)
|
||||
(put t "core-contains?" core-contains?)
|
||||
(put t "core-count" core-count)
|
||||
(put t "core-seq" core-seq)
|
||||
(put t "core-vec" core-vec)
|
||||
(put t "core-map" core-map)
|
||||
(put t "core-filter" core-filter)
|
||||
(put t "core-remove" core-remove)
|
||||
(put t "core-reduce" core-reduce)
|
||||
(put t "core-str" core-str)
|
||||
(put t "core-prn" core-prn)
|
||||
(put t "core-println" core-println)
|
||||
(put t "core-print" core-print)
|
||||
(put t "core-identity" core-identity)
|
||||
(put t "core-comp" core-comp)
|
||||
(put t "core-partial" core-partial)
|
||||
(put t "core-complement" core-complement)
|
||||
(put t "core-constantly" core-constantly)
|
||||
(put t "core-memoize" core-memoize)
|
||||
(put t "core-range" core-range)
|
||||
(put t "core-take" core-take)
|
||||
(put t "core-drop" core-drop)
|
||||
(put t "core-take-while" core-take-while)
|
||||
(put t "core-drop-while" core-drop-while)
|
||||
(put t "core-reverse" core-reverse)
|
||||
(put t "core-into" core-into)
|
||||
(put t "core-merge" core-merge)
|
||||
(put t "core-merge-with" core-merge-with)
|
||||
(put t "core-keys" core-keys)
|
||||
(put t "core-vals" core-vals)
|
||||
(put t "core-zipmap" core-zipmap)
|
||||
(put t "core-select-keys" core-select-keys)
|
||||
(put t "core-max" core-max)
|
||||
(put t "core-min" core-min)
|
||||
(put t "core-quot" core-quot)
|
||||
(put t "core-rem" core-rem)
|
||||
(put t "core-mod" core-mod)
|
||||
(put t "core-apply" apply)
|
||||
(put t "core-some" core-some?)
|
||||
(put t "core-pr-str" core-str)
|
||||
(put t "core-nth" core-get)
|
||||
t))
|
||||
# ============================================================
|
||||
|
||||
(defn analyze-form
|
||||
[form bindings]
|
||||
"Analyze a Clojure form and return an AST node with :op key.
|
||||
Takes bindings (table) and optional ctx (for macro expansion)."
|
||||
[form bindings &opt ctx]
|
||||
(default ctx nil)
|
||||
(cond
|
||||
(literal? form)
|
||||
{:op :const :val form}
|
||||
|
|
@ -126,78 +245,87 @@
|
|||
head-name (if (and (struct? first-form) (= :symbol (first-form :jolt/type)))
|
||||
(first-form :name)
|
||||
nil)]
|
||||
(if head-name
|
||||
(match head-name
|
||||
"quote" {:op :quote :expr (in form 1)}
|
||||
"do" (let [all-statements (array/slice form 1)
|
||||
n (length all-statements)
|
||||
analyzed (map |(analyze-form $ bindings) all-statements)]
|
||||
{:op :do
|
||||
:statements (array/slice analyzed 0 (- n 1))
|
||||
:ret (in analyzed (- n 1))})
|
||||
"if" {:op :if
|
||||
:test (analyze-form (in form 1) bindings)
|
||||
:then (analyze-form (in form 2) bindings)
|
||||
:else (if (> (length form) 3)
|
||||
(analyze-form (in form 3) bindings)
|
||||
{:op :const :val nil})}
|
||||
"def" {:op :def
|
||||
:name (in form 1)
|
||||
:init (analyze-form (in form 2) bindings)}
|
||||
"fn*" (let [params (in form 1)
|
||||
body-bindings (do
|
||||
(var bb @{})
|
||||
(loop [[k v] :pairs bindings] (put bb k v))
|
||||
(each p params
|
||||
(put bb (if (struct? p) (p :name) p) :jolt/local))
|
||||
bb)
|
||||
body-exprs (tuple/slice form 2)
|
||||
analyzed-body (map |(analyze-form $ body-bindings) body-exprs)
|
||||
n-body (length analyzed-body)]
|
||||
{:op :fn :params params
|
||||
:body (if (> n-body 1)
|
||||
{:op :do
|
||||
:statements (array/slice analyzed-body 0 (- n-body 1))
|
||||
:ret (last analyzed-body)}
|
||||
(first analyzed-body))})
|
||||
"let*" (let [bind-vec (in form 1)
|
||||
body-exprs (tuple/slice form 2)
|
||||
binding-pairs (do
|
||||
(var pairs @[])
|
||||
(var i 0)
|
||||
(let [n (length bind-vec)]
|
||||
(while (< i n)
|
||||
(let [sym-s (in bind-vec i)
|
||||
name (if (struct? sym-s) (sym-s :name) sym-s)
|
||||
val-form (if (< (+ i 1) n) (in bind-vec (+ i 1)) nil)
|
||||
val-ast (if val-form (analyze-form val-form bindings) {:op :const :val nil})]
|
||||
(array/push pairs {:name name :init val-ast})
|
||||
(+= i 2))))
|
||||
pairs)
|
||||
body-bindings (do
|
||||
(var bb @{})
|
||||
(loop [[k v] :pairs bindings] (put bb k v))
|
||||
(each bp binding-pairs
|
||||
(put bb (bp :name) :jolt/local))
|
||||
bb)
|
||||
analyzed-body (map |(analyze-form $ body-bindings) body-exprs)
|
||||
n-body (length analyzed-body)]
|
||||
{:op :let
|
||||
:binding-pairs binding-pairs
|
||||
:body (if (> n-body 1)
|
||||
{:op :do
|
||||
:statements (array/slice analyzed-body 0 (- n-body 1))
|
||||
:ret (last analyzed-body)}
|
||||
(first analyzed-body))})
|
||||
(let [f-ast (analyze-form first-form bindings)
|
||||
args (map |(analyze-form $ bindings) (tuple/slice form 1))]
|
||||
{:op :invoke :fn f-ast :args args}))
|
||||
(let [f-ast (analyze-form first-form bindings)
|
||||
args (map |(analyze-form $ bindings) (tuple/slice form 1))]
|
||||
{:op :invoke :fn f-ast :args args})))
|
||||
# Macro expansion: if ctx is provided and head resolves to a macro,
|
||||
# expand it and re-analyze the expanded form
|
||||
(if (and ctx head-name
|
||||
(not (special-form? head-name))
|
||||
(resolve-macro ctx first-form))
|
||||
(let [macro-var (resolve-macro ctx first-form)
|
||||
macro-fn (var-get macro-var)
|
||||
expanded (apply macro-fn (tuple/slice form 1))]
|
||||
(analyze-form expanded bindings ctx))
|
||||
(if head-name
|
||||
(match head-name
|
||||
"quote" {:op :quote :expr (in form 1)}
|
||||
"do" (let [all-statements (array/slice form 1)
|
||||
n (length all-statements)
|
||||
analyzed (map |(analyze-form $ bindings ctx) all-statements)]
|
||||
{:op :do
|
||||
:statements (array/slice analyzed 0 (- n 1))
|
||||
:ret (in analyzed (- n 1))})
|
||||
"if" {:op :if
|
||||
:test (analyze-form (in form 1) bindings ctx)
|
||||
:then (analyze-form (in form 2) bindings ctx)
|
||||
:else (if (> (length form) 3)
|
||||
(analyze-form (in form 3) bindings ctx)
|
||||
{:op :const :val nil})}
|
||||
"def" {:op :def
|
||||
:name (in form 1)
|
||||
:init (analyze-form (in form 2) bindings ctx)}
|
||||
"fn*" (let [params (in form 1)
|
||||
body-bindings (do
|
||||
(var bb @{})
|
||||
(loop [[k v] :pairs bindings] (put bb k v))
|
||||
(each p params
|
||||
(put bb (if (struct? p) (p :name) p) :jolt/local))
|
||||
bb)
|
||||
body-exprs (tuple/slice form 2)
|
||||
analyzed-body (map |(analyze-form $ body-bindings ctx) body-exprs)
|
||||
n-body (length analyzed-body)]
|
||||
{:op :fn :params params
|
||||
:body (if (> n-body 1)
|
||||
{:op :do
|
||||
:statements (array/slice analyzed-body 0 (- n-body 1))
|
||||
:ret (last analyzed-body)}
|
||||
(first analyzed-body))})
|
||||
"let*" (let [bind-vec (in form 1)
|
||||
body-exprs (tuple/slice form 2)
|
||||
binding-pairs (do
|
||||
(var pairs @[])
|
||||
(var i 0)
|
||||
(let [n (length bind-vec)]
|
||||
(while (< i n)
|
||||
(let [sym-s (in bind-vec i)
|
||||
name (if (struct? sym-s) (sym-s :name) sym-s)
|
||||
val-form (if (< (+ i 1) n) (in bind-vec (+ i 1)) nil)
|
||||
val-ast (if val-form (analyze-form val-form bindings ctx) {:op :const :val nil})]
|
||||
(array/push pairs {:name name :init val-ast})
|
||||
(+= i 2))))
|
||||
pairs)
|
||||
body-bindings (do
|
||||
(var bb @{})
|
||||
(loop [[k v] :pairs bindings] (put bb k v))
|
||||
(each bp binding-pairs
|
||||
(put bb (bp :name) :jolt/local))
|
||||
bb)
|
||||
analyzed-body (map |(analyze-form $ body-bindings ctx) body-exprs)
|
||||
n-body (length analyzed-body)]
|
||||
{:op :let
|
||||
:binding-pairs binding-pairs
|
||||
:body (if (> n-body 1)
|
||||
{:op :do
|
||||
:statements (array/slice analyzed-body 0 (- n-body 1))
|
||||
:ret (last analyzed-body)}
|
||||
(first analyzed-body))})
|
||||
(let [f-ast (analyze-form first-form bindings ctx)
|
||||
args (map |(analyze-form $ bindings ctx) (tuple/slice form 1))]
|
||||
{:op :invoke :fn f-ast :args args}))
|
||||
(let [f-ast (analyze-form first-form bindings ctx)
|
||||
args (map |(analyze-form $ bindings ctx) (tuple/slice form 1))]
|
||||
{:op :invoke :fn f-ast :args args}))))
|
||||
|
||||
(tuple? form)
|
||||
(let [items (map |(analyze-form $ bindings) form)]
|
||||
(let [items (map |(analyze-form $ bindings ctx) form)]
|
||||
{:op :vector :items items})
|
||||
|
||||
(struct? form)
|
||||
|
|
@ -211,8 +339,7 @@
|
|||
|
||||
(var emit-ast nil)
|
||||
|
||||
(defn- emit-const-str
|
||||
[val buf]
|
||||
(defn- emit-const-str [val buf]
|
||||
(cond
|
||||
(nil? val) (buffer/push buf "nil")
|
||||
(= true val) (buffer/push buf "true")
|
||||
|
|
@ -313,30 +440,110 @@
|
|||
:quote (emit-quote-str (ast :expr) buf)
|
||||
(buffer/push buf (string "/* unhandled op: " (ast :op) " */")))))
|
||||
|
||||
# ============================================================
|
||||
# Emitter — AST → Janet data structure (for direct eval)
|
||||
# ============================================================
|
||||
|
||||
(var emit-expr nil)
|
||||
|
||||
(defn- emit-const-expr [val] val)
|
||||
|
||||
(defn- emit-do-expr [statements ret]
|
||||
(def exprs @['do])
|
||||
(each s statements (array/push exprs (emit-expr s)))
|
||||
(when ret (array/push exprs (emit-expr ret)))
|
||||
(tuple/slice (tuple ;exprs)))
|
||||
|
||||
(defn- emit-if-expr [test then else]
|
||||
(def exprs @['if])
|
||||
(array/push exprs (emit-expr test))
|
||||
(array/push exprs (emit-expr then))
|
||||
(when else (array/push exprs (emit-expr else)))
|
||||
(tuple/slice (tuple ;exprs)))
|
||||
|
||||
(defn- emit-def-expr [name-sym init]
|
||||
['def (symbol (name-sym :name)) (emit-expr init)])
|
||||
|
||||
(defn- emit-fn-expr [params body]
|
||||
(def param-syms @[])
|
||||
(each p params
|
||||
(array/push param-syms (symbol (if (struct? p) (p :name) p))))
|
||||
['fn (tuple/slice (tuple ;param-syms)) (emit-expr body)])
|
||||
|
||||
(defn- emit-let-expr [binding-pairs body]
|
||||
(def bind-tuple @[])
|
||||
(each bp binding-pairs
|
||||
(array/push bind-tuple (symbol (bp :name)))
|
||||
(array/push bind-tuple (emit-expr (bp :init))))
|
||||
['let (tuple/slice (tuple ;bind-tuple)) (emit-expr body)])
|
||||
|
||||
(defn- emit-invoke-expr [f-ast args]
|
||||
(def exprs @[(emit-expr f-ast)])
|
||||
(each arg args (array/push exprs (emit-expr arg)))
|
||||
(tuple/slice (tuple ;exprs)))
|
||||
|
||||
(defn- emit-symbol-expr [name] (symbol name))
|
||||
(defn- emit-local-expr [name] (symbol name))
|
||||
|
||||
(defn- emit-core-symbol-expr [janet-name]
|
||||
(or (get core-fn-values janet-name)
|
||||
(error (string "Core fn not found: " janet-name))))
|
||||
|
||||
(defn- emit-qualified-symbol-expr [ns name]
|
||||
(error (string "Cannot eval qualified symbol at compile time: " ns "/" name)))
|
||||
|
||||
(defn- emit-vector-expr [items]
|
||||
(def exprs @[])
|
||||
(each item items (array/push exprs (emit-expr item)))
|
||||
(tuple/slice (tuple ;exprs)))
|
||||
|
||||
(defn- emit-map-expr [form] form)
|
||||
|
||||
(defn- emit-quote-expr [expr]
|
||||
['quote (analyze-form expr @{})])
|
||||
|
||||
(set emit-expr
|
||||
(fn [ast]
|
||||
(match (ast :op)
|
||||
:const (emit-const-expr (ast :val))
|
||||
:symbol (emit-symbol-expr (ast :name))
|
||||
:local (emit-local-expr (ast :name))
|
||||
:core-symbol (emit-core-symbol-expr (ast :janet-name))
|
||||
:qualified-symbol (emit-qualified-symbol-expr (ast :ns) (ast :name))
|
||||
:do (emit-do-expr (ast :statements) (ast :ret))
|
||||
:if (emit-if-expr (ast :test) (ast :then) (ast :else))
|
||||
:def (emit-def-expr (ast :name) (ast :init))
|
||||
:fn (emit-fn-expr (ast :params) (ast :body))
|
||||
:let (emit-let-expr (ast :binding-pairs) (ast :body))
|
||||
:invoke (emit-invoke-expr (ast :fn) (ast :args))
|
||||
:vector (emit-vector-expr (ast :items))
|
||||
:map (emit-map-expr (ast :form))
|
||||
:quote (emit-quote-expr (ast :expr))
|
||||
(error (string "Unhandled op: " (ast :op))))))
|
||||
|
||||
# ============================================================
|
||||
# Public API
|
||||
# ============================================================
|
||||
|
||||
(defn compile-form
|
||||
"Compile a Clojure form to a Janet source string."
|
||||
[form]
|
||||
(let [ast (analyze-form form @{})
|
||||
"Compile a Clojure form to a Janet source string.
|
||||
Pass ctx for macro expansion."
|
||||
[form &opt ctx]
|
||||
(default ctx nil)
|
||||
(let [ast (analyze-form form @{} ctx)
|
||||
buf @""]
|
||||
(emit-ast ast buf)
|
||||
(string buf)))
|
||||
|
||||
(defn eval-janet-source
|
||||
"Parse and evaluate a Janet source string.
|
||||
Uses the proper parser→produce→eval pipeline so special forms work."
|
||||
[source]
|
||||
(def p (parser/new))
|
||||
(parser/consume p source)
|
||||
(parser/eof p)
|
||||
(def form (parser/produce p))
|
||||
(eval form))
|
||||
(defn compile-ast
|
||||
"Compile a Clojure form to an eval-able Janet data structure.
|
||||
Core function symbols are resolved to actual function values."
|
||||
[form &opt ctx]
|
||||
(default ctx nil)
|
||||
(emit-expr (analyze-form form @{} ctx)))
|
||||
|
||||
(defn compile-and-eval
|
||||
"Compile a Clojure form to Janet source and evaluate it.
|
||||
Returns the result value."
|
||||
[form]
|
||||
(eval-janet-source (compile-form form)))
|
||||
"Compile a Clojure form and evaluate it as Janet.
|
||||
Emits Janet data structures with resolved core functions."
|
||||
[form ctx]
|
||||
(eval (compile-ast form ctx)))
|
||||
|
|
|
|||
|
|
@ -50,9 +50,8 @@
|
|||
(put cache ns-name cached))
|
||||
|
||||
(each form forms
|
||||
(let [janet-src (compile-form form)]
|
||||
(array/push cached janet-src)
|
||||
(eval-janet-source janet-src)))
|
||||
(array/push cached form)
|
||||
(compile-and-eval form ctx))
|
||||
ns-name)
|
||||
# Interpreter path
|
||||
(do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue