diff options
author | Eduardo Julian | 2014-12-09 00:58:48 -0400 |
---|---|---|
committer | Eduardo Julian | 2014-12-09 00:58:48 -0400 |
commit | bad2695169969e6ee7551d2a2c23d9c4e1b656fb (patch) | |
tree | 60786df17a1d5d955657c752a1ebc9732b053a83 /src/lang | |
parent | 36de85ef43d60e29f4aeb622fb449cabb506e2d8 (diff) |
Now can define constants in modules.
Diffstat (limited to '')
-rw-r--r-- | src/lang.clj | 6 | ||||
-rw-r--r-- | src/lang/compiler.clj | 22 |
2 files changed, 23 insertions, 5 deletions
diff --git a/src/lang.clj b/src/lang.clj index 22492eac6..97465a7a8 100644 --- a/src/lang.clj +++ b/src/lang.clj @@ -11,9 +11,9 @@ (comment (let [source-code (slurp "test2.lang") tokens (&lexer/lex source-code) - _ (prn 'tokens tokens) + ;; _ (prn 'tokens tokens) syntax (&parser/parse tokens) - _ (prn 'syntax syntax) + ;; _ (prn 'syntax syntax) class-data (&compiler/compile "test2" syntax)] (write-file "test2.class" class-data)) @@ -23,7 +23,6 @@ (&compiler/compile "test2") (write-file "test2.class")) - ;; TODO: Define the Function abstract class. ;; TODO: Define functions as classes inheriting Function. ;; TODO: Add tuples. ;; TODO: Add let. @@ -36,7 +35,6 @@ ;; TODO: Add Java-interop. ;; TODO: Add signatures & structures. ;; TODO: Add type-system. - ;; TODO: Allow defining constants. ;; TODO: Allow importing Java classes. ;; TODO: Allow using other modules. ;; TODO: diff --git a/src/lang/compiler.clj b/src/lang/compiler.clj index f305d8556..d2b24eb02 100644 --- a/src/lang/compiler.clj +++ b/src/lang/compiler.clj @@ -69,7 +69,7 @@ (do (doseq [arg ?args] (compile-form (assoc *state* :form arg))) (doto *writer* - (.visitMethodInsn Opcodes/INVOKESTATIC *name* ?fn "(Ljava/lang/Object;)Ljava/lang/Object;")))) + (.visitMethodInsn Opcodes/INVOKESTATIC (->class *name*) ?fn "(Ljava/lang/Object;)Ljava/lang/Object;")))) (defcompiler ^:private compile-static-access [::&parser/static-access ?class ?member] @@ -123,6 +123,9 @@ (.visitInsn Opcodes/ARETURN) (.visitMaxs 0 0) (.visitEnd)))) + [::&parser/ident ?name] + (doto (.visitField *writer* (+ Opcodes/ACC_PUBLIC Opcodes/ACC_STATIC) ?name "Ljava/lang/Object;" nil nil) + (.visitEnd)) )) (defcompiler ^:private compile-module @@ -192,5 +195,22 @@ ;; (.visitMaxs 0 0) ;; (.visitEnd)) (doall (map #(compile-form (assoc state :form %)) inputs)) + (when-let [constants (seq (for [input inputs + :let [payload (match input + [::&parser/def [::&parser/ident ?name] ?body] + [?name ?body] + _ + nil)] + :when payload] + payload))] + (let [=init (doto (.visitMethod =class Opcodes/ACC_PUBLIC "<clinit>" "()V" nil nil) + (.visitCode))] + (doseq [[?name ?body] constants] + (do (compile-form (assoc state :writer =init :form ?body)) + (.visitFieldInsn =init Opcodes/PUTSTATIC (->class class-name) ?name "Ljava/lang/Object;"))) + (doto =init + (.visitInsn Opcodes/RETURN) + (.visitMaxs 0 0) + (.visitEnd)))) (.visitEnd =class) (.toByteArray =class))) |