summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-11 20:07:51 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-11 20:07:51 +0200
commitac93ca810e2fe04a5dc56a415e8d9a836ce151f9 (patch)
tree8bcf72cafb81b858601e5f6c55c092c1096b3a1d
parentad7e9183675e07d8e0698f7c064858a6052ebc7d (diff)
equal magics
-rw-r--r--modules/language/python/compile.scm10
-rw-r--r--modules/language/python/dict.scm14
-rw-r--r--modules/language/python/list.scm12
-rw-r--r--modules/language/python/module/python.scm2
-rw-r--r--modules/language/python/number.scm2
-rw-r--r--modules/language/python/set.scm9
-rw-r--r--modules/language/python/string.scm4
-rw-r--r--modules/language/python/tuple.scm4
-rw-r--r--modules/oop/pf-objects.scm17
9 files changed, 51 insertions, 23 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm
index 3e16386..b7a52b2 100644
--- a/modules/language/python/compile.scm
+++ b/modules/language/python/compile.scm
@@ -1117,7 +1117,9 @@
(letrec ((,f
(case-lambda
((,q)
- (apply ,f ,q))
+ (if (pair? ,q)
+ (apply ,f ,q)
+ (apply ,f (,(L 'to-list) ,q))))
(,vars
,@(map (lambda (l v) (make-set vs op l v))
l vars)))))
@@ -1182,13 +1184,13 @@
(match op
((or "<" ">" "<=" ">=")
(list (G (string->symbol op)) x y))
- ("!=" (list (G 'not) (list (G 'equal?) x y)))
- ("==" (list (G 'equal?) x y))
+ ("!=" (list (G 'not) (list (O 'equal?) x y)))
+ ("==" (list (O 'equal?) x y))
("is" (list (G 'eq?) x y))
("isnot" (list (G 'not) (list (G 'eq?) x y)))
("in" (list (L 'in) x y))
("notin" (list (G 'not) (list (L 'in) x y)))
- ("<>" (list (G 'not) (list (G 'equal?) x y)))))
+ ("<>" (list (G 'not) (list (O 'equal?) x y)))))
(tr op (exp vs x) (exp vs y)))))
(define (exp vs x)
diff --git a/modules/language/python/dict.scm b/modules/language/python/dict.scm
index f4d13a5..b9ab116 100644
--- a/modules/language/python/dict.scm
+++ b/modules/language/python/dict.scm
@@ -20,13 +20,19 @@
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
(define (h x n) (modulo (py-hash x) n))
+(define (py-assoc k l)
+ (if (pair? l)
+ (if (equal? (caar l) k)
+ (car l)
+ (py-assoc k (cdr l)))
+ #f))
(define (py-hash-ref . l)
- (apply hashx-ref h assoc l))
+ (apply hashx-ref h py-assoc l))
(define (py-hash-set! . l)
- (apply hashx-set! h assoc l))
+ (apply hashx-set! h py-assoc l))
(define (py-hash-remove! . l)
- (apply hashx-remove! h assoc l))
+ (apply hashx-remove! h py-assoc l))
(set! (@@ (language python def) hset!) py-hash-set!)
@@ -430,7 +436,7 @@
(format port "{}")
(format port "{~a: ~a~{, ~a: ~a~}}" (car li) (cadr li) (cddr li))))
-(define-method (equal? (o1 <py-hashtable>) (o2 <py-hashtable>))
+(define-method (py-equal? (o1 <py-hashtable>) (o2 <py-hashtable>))
(and
(equal? (slot-ref o1 'n) (slot-ref o2 'n))
(equal? (slot-ref o1 'h) (slot-ref o2 'h))
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm
index e9c301c..a35ceae 100644
--- a/modules/language/python/list.scm
+++ b/modules/language/python/list.scm
@@ -454,7 +454,10 @@
(next-method)))
;; equal?
-(define-method (equal? (o1 <py-list>) (o2 <py-list>))
+(define-method (py-equal? (o1 <py-list>) (o2 <py-list>))
+ (equal o1 o2))
+
+(define (equal o1 o2)
(let ((n1 (slot-ref o1 'n))
(n2 (slot-ref o2 'n))
(vec1 (slot-ref o1 'vec))
@@ -466,10 +469,6 @@
(and (equal? (vector-ref vec1 i) (vector-ref vec2 i))
(lp (+ i 1)))
#t)))))
-
-(define-method (equal? (o1 <py-list>) o2) #f)
-(define-method (equal? o1 (o2 <py-list>)) #f)
-
(define-class <py-seq-iter> () o i n d)
(define-class <py-list-iter> (<py-list>) i d)
@@ -833,10 +832,9 @@
(__init__ self)
(for ((i : it)) () (pylist-append! self i))))))
__init__)))
-
+
(define pylist list)
-
(define-method (py-class (o <py-list>) list))
(define (pylist-listing)
diff --git a/modules/language/python/module/python.scm b/modules/language/python/module/python.scm
index 418c325..492e013 100644
--- a/modules/language/python/module/python.scm
+++ b/modules/language/python/module/python.scm
@@ -221,7 +221,7 @@
(lambda z
(lp2 (cdr l) (append (reverse z) r))))
(begin
- (apply yield (reverse r))
+ (yield (reverse r))
(lp))))))))))
diff --git a/modules/language/python/number.scm b/modules/language/python/number.scm
index d0218d3..2d90554 100644
--- a/modules/language/python/number.scm
+++ b/modules/language/python/number.scm
@@ -67,7 +67,7 @@
(mk-biop1 b0 <= __lt__)
(mk-biop1 b0 >= __gt__)
(mk-biop2 b0 rexpt expt __pow__ __rpow__)
-(b0 equal?)
+(b0 py-equal?)
(define-method (py-lshift (o1 <integer>) (o2 <integer>))
(ash o1 o2))
diff --git a/modules/language/python/set.scm b/modules/language/python/set.scm
index d69b7c9..ef4abe7 100644
--- a/modules/language/python/set.scm
+++ b/modules/language/python/set.scm
@@ -198,7 +198,14 @@
(let* ((d (slot-ref self 'dict))
(t (slot-ref d 't)))
(not (eq? miss (py-hash-ref t x miss))))))
-
+
+ (define __eq__
+ (lambda (self x)
+ (and
+ (is-a? x <p>)
+ (eq? (ref self '__class__ 1) (ref x '__class__ 2))
+ (equal? (ref self 'd 1) (ref x 'd 2)))))
+
(define __iter__
(make-generator (self)
(lambda (yield self)
diff --git a/modules/language/python/string.scm b/modules/language/python/string.scm
index d8f4da7..16d9d0b 100644
--- a/modules/language/python/string.scm
+++ b/modules/language/python/string.scm
@@ -513,9 +513,9 @@
(define-method (pyhash (o <py-string>)) (hash (slot-ref o 'str) pyhash-N))
-(define-method (equal? (o <py-string>) x)
+(define-method (py-equal? (o <py-string>) x)
(equal? (slot-ref o 'str) x))
-(define-method (equal? x (o <py-string>))
+(define-method (py-equal? x (o <py-string>))
(equal? (slot-ref o 'str) x))
(define-class <string-iter> (<py-string>) str i d)
diff --git a/modules/language/python/tuple.scm b/modules/language/python/tuple.scm
index 87a6ed0..5362cc9 100644
--- a/modules/language/python/tuple.scm
+++ b/modules/language/python/tuple.scm
@@ -9,8 +9,8 @@
(define-class <py-tuple> () l)
(define-method (py-hash (o <py-tuple>)) (py-hash (slot-ref o 'l)))
(define-method (py-class (o <py-tuple>) tuple))
-(define-method (equal? (o1 <py-tuple>) o2) (equal? (slot-ref o1 'l) o2))
-(define-method (equal? o1 (o2 <py-tuple>)) (equal? o1 (slot-ref o2 'l)))
+(define-method (py-equal? (o1 <py-tuple>) o2) (equal? (slot-ref o1 'l) o2))
+(define-method (py-equal? o1 (o2 <py-tuple>)) (equal? o1 (slot-ref o2 'l)))
(define-method (wrap-in (o <py-tuple>))
(wrap-in (slot-ref o 'l)))
diff --git a/modules/oop/pf-objects.scm b/modules/oop/pf-objects.scm
index c0dae32..d7ca1a3 100644
--- a/modules/oop/pf-objects.scm
+++ b/modules/oop/pf-objects.scm
@@ -2,6 +2,7 @@
#:use-module (oop goops)
#:use-module (ice-9 vlist)
#:use-module (ice-9 match)
+ #:replace (equal?)
#:export (set ref make-pf <p> <py> <pf> <pyf> <property>
call with copy fset fcall make-p put put!
pcall pcall! get fset-x pyclass? refq
@@ -11,7 +12,7 @@
def-py-class mk-py-class make-py-class
define-python-class get-type py-class
object-method class-method static-method
- py-super-mac py-super
+ py-super-mac py-super py-equal?
*class* *self*
))
#|
@@ -891,3 +892,17 @@ explicitly tell it to not update etc.
(lp n r)
(lp n (cons x r))))
(reverse r))))
+
+(define-method (py-equal? (x <p>) y)
+ (aif it (ref x '__eq__)
+ (it y)
+ (next-method)))
+
+(define-method (py-equal? y (x <p>))
+ (aif it (ref x '__eq__)
+ (it y)
+ (next-method)))
+
+(define-method (py-equal? x y) ((@ (guile) equal?) x y))
+
+(define (equal? x y) (or (eq? x y) (py-equal? x y)))