diff options
Diffstat (limited to '')
-rw-r--r-- | src/lang/compiler.clj | 52 | ||||
-rw-r--r-- | test2.lang | 2 |
2 files changed, 27 insertions, 27 deletions
diff --git a/src/lang/compiler.clj b/src/lang/compiler.clj index 18343db1d..f1a78ed89 100644 --- a/src/lang/compiler.clj +++ b/src/lang/compiler.clj @@ -141,8 +141,10 @@ (defcompiler ^:private compile-global [::&analyser/global ?owner-class ?name] (do ;; (prn 'GLOBAL ?owner-class ?name *type*) + ;; (prn 'compile-global (->class (str ?owner-class "$" ?name)) "_datum") (doto *writer* - (.visitFieldInsn Opcodes/GETSTATIC (->class ?owner-class) ?name (->java-sig *type*))))) + (.visitFieldInsn Opcodes/GETSTATIC (->class (str ?owner-class "$" ?name)) "_datum" "Ljava/lang/Object;" ;; (->java-sig *type*) + )))) ;; (defcompiler ^:private compile-call ;; [::&analyser/call ?fn ?args] @@ -310,7 +312,7 @@ init-signature (if (not= 0 num-captured) (str "(" (apply str counter-sig (repeat num-captured clo-field-sig)) ")" "V") (str "()" "V"))] - ;; (.visitInnerClass writer current-class outer-class nil (+ Opcodes/ACC_STATIC Opcodes/ACC_SYNTHETIC)) + (.visitInnerClass writer current-class outer-class nil (+ Opcodes/ACC_STATIC Opcodes/ACC_SYNTHETIC)) (let [=class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS) (.visit Opcodes/V1_5 (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_SUPER) current-class nil "java/lang/Object" (into-array ["test2/Function"])) @@ -383,16 +385,33 @@ (write-file (str current-class ".class") (.toByteArray =class))) )) +(defn compile-field [writer class-name ?name body state] + (let [outer-class (->class class-name) + datum-sig (->type-signature "java.lang.Object") + current-class (str outer-class "$" ?name)] + (.visitInnerClass writer current-class outer-class nil (+ Opcodes/ACC_STATIC Opcodes/ACC_SYNTHETIC)) + (let [=class (doto (new ClassWriter ClassWriter/COMPUTE_MAXS) + (.visit Opcodes/V1_5 (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_SUPER) + current-class nil "java/lang/Object" (into-array ["test2/Function"])) + (-> (.visitField (+ Opcodes/ACC_PUBLIC Opcodes/ACC_FINAL Opcodes/ACC_STATIC) "_datum" datum-sig nil nil) + (doto (.visitEnd))) + (-> (.visitMethod Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil) + (doto (.visitCode) + (->> (assoc state :form body :writer) compile-form) + (.visitFieldInsn Opcodes/PUTSTATIC current-class "_datum" datum-sig) + (.visitInsn Opcodes/RETURN) + (.visitMaxs 0 0) + (.visitEnd))) + (.visitEnd))] + (write-file (str current-class ".class") (.toByteArray =class))) + )) + (defcompiler ^:private compile-def [::&analyser/def ?form ?body] (do ;; (prn 'compile-def ?form) (match ?form (?name :guard string?) - (let [=type (:type ?body) - ;; _ (prn '?body ?body) - ] - (doto (.visitField *writer* (+ Opcodes/ACC_PUBLIC Opcodes/ACC_STATIC) ?name (->java-sig =type) nil nil) - (.visitEnd))) + (compile-field *writer* *class-name* ?name ?body *state*) [?name ?args] (do ;; (prn 'compile-def `(~'def (~(symbol ?name) ~@(map symbol ?args)))) @@ -661,24 +680,5 @@ (doseq [input inputs] (when (not (compile-form (assoc state :form input))) (assert false input))) - (when-let [constants (seq (for [input inputs - :let [payload (match (:form input) - [::&analyser/def (?name :guard string?) ?body] - [?name ?body] - _ - nil)] - :when payload] - payload))] - (let [=init (doto (.visitMethod =class Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil) - (.visitCode)) - state* (assoc state :writer =init) - class-name* (->class class-name)] - (doseq [[?name ?body] constants] - (do (assert (compile-form (assoc state* :form ?body)) (str "Couldn't compile init: " (pr-str ?body))) - (.visitFieldInsn =init Opcodes/PUTSTATIC class-name* ?name (->java-sig (:type ?body))))) - (doto =init - (.visitInsn Opcodes/RETURN) - (.visitMaxs 0 0) - (.visitEnd)))) (.visitEnd =class) (.toByteArray =class))) diff --git a/test2.lang b/test2.lang index 90db0230b..a7892945f 100644 --- a/test2.lang +++ b/test2.lang @@ -23,7 +23,7 @@ (def (main args) (if true (let f (lambda [x] (lambda [y] (x y))) - (let g (lambda [x] x) + (let g (lambda [x] const) (:: (:: System out) (println (f g "WE'VE GOT CLOSURES!"))))) (:: (:: System out) (println "FALSE")))) |