summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-05-09 15:07:09 +0000
committerRich Hickey <richhickey@gmail.com>2006-05-09 15:07:09 +0000
commit92f063f24226b5b933dbd0bc0b0bb2e153954cfd (patch)
tree20c69407191f818f197cab1ff84014b6c25321d8 /src
parentc403c6e6f13d19e43d4f7dbab9dcb40fe9781f76 (diff)
added instance?
Diffstat (limited to 'src')
-rw-r--r--src/lisp/clojure.lisp34
-rw-r--r--src/lisp/test.lisp5
2 files changed, 37 insertions, 2 deletions
diff --git a/src/lisp/clojure.lisp b/src/lisp/clojure.lisp
index e251c9ce..0281938b 100644
--- a/src/lisp/clojure.lisp
+++ b/src/lisp/clojure.lisp
@@ -418,6 +418,7 @@
(|break| (analyze-break context form))
(|try| (analyze-try context form))
(|bind| (analyze-bind context form))
+ (|instance?| (analyze-instance? context form))
((|char| |boolean| |byte| |short| |int| |long| |float| |double|)
(analyze-cast context form))
(t (analyze-invoke context op form))))
@@ -453,7 +454,8 @@
(:bind(emit-bind context expr))
(:quoted-aggregate (emit-quoted-aggregate context expr))
(:host-symbol (emit-host-static-member context expr))
- (:cast (emit-cast context expr))))
+ (:cast (emit-cast context expr))
+ (:instance? (emit-instance? context expr))))
(t (emit-other context expr))))
(defun emit-other (context expr)
@@ -530,7 +532,7 @@
(format t "~A" (munge-name (@ :symbol expr))))))
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cast ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cast/instance? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun analyze-cast (context form)
(declare (ignore context))
@@ -546,6 +548,30 @@
(emit :expression (@ :expr expr))
(format t "))"))))
+(defun analyze-instance? (context form)
+ (declare (ignore context))
+ (assert (host-type-symbol? (third form)))
+ (newobj :type :instance?
+ :expr (analyze :expression (macroexpand (second form)))
+ :sym (analyze-symbol :statement (third form))))
+
+(defun emit-instance? (context expr)
+ (ccase context
+ (:return (emit-return expr))
+ (:expression
+ (format t "(")
+ (emit :expression (@ :expr expr))
+ (format t" ~A ~A?RT.T:null)"
+ (instanceof-string)
+ (multiple-value-bind (class-name member-name)
+ (host-class-and-member-strings (@ :symbol (@ :sym expr)))
+ ;trim off any assembly cruft
+ (subseq class-name 0 (position #\, class-name)))))))
+
+(defun instanceof-string ()
+ (ccase *host*
+ (:jvm "instanceof")
+ (:cli "is")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; set ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -1424,6 +1450,10 @@
(defun host-symbol? (sym)
(find #\. (string sym) :start 1))
+(defun host-type-symbol? (sym)
+ (and (host-symbol? sym)
+ (= 1 (length (subseq (string sym) (position #\. (string sym) :from-end t))))))
+
(defun host-class-and-member-strings (host-symbol)
(let* ((host-name (symbol-name host-symbol))
(dot-pos (position #\. host-name :from-end t ))
diff --git a/src/lisp/test.lisp b/src/lisp/test.lisp
index b5a82a98..a469186e 100644
--- a/src/lisp/test.lisp
+++ b/src/lisp/test.lisp
@@ -137,6 +137,11 @@
Math.PI)
(set Math.PI 3.14))
+(defn finst (x)
+ (if (instance? x String.)
+ 1
+ 2))
+
(defn prn (x)
#+:JVM (.println System.out x)
#+:CLI (Console.WriteLine x))