aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lang.clj6
-rw-r--r--src/lang/compiler.clj22
-rw-r--r--test2.lang4
3 files changed, 26 insertions, 6 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)))
diff --git a/test2.lang b/test2.lang
index ecf6a234b..8e878641f 100644
--- a/test2.lang
+++ b/test2.lang
@@ -15,7 +15,9 @@
(definterface Function
(: apply (-> [java.lang.Object] java.lang.Object)))
-#( (ann id #type (All [x] (-> [x] x))) )#
+(def const "IDENTITY")
+
+## (ann id #type (All [x] (-> [x] x)))
(def (id x)
x)