summaryrefslogtreecommitdiff
path: root/modules/language/python/for.scm
blob: f9cdd1e28054ea29a2d382cbc4a01949be2e0e40 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(define-module (language python for)
  #:use-module (language python yield)
  #:use-module (oop pf-objects)
  #:use-module (language python exceptions)
  #:use-module (oop goops)
  #:use-module (ice-9 control)
  #:export (for break next wrap-in))

(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))

(eval-when (compile eval load)
  (define (generate-temporaries2 x)
    (map (lambda (x) (generate-temporaries x)) x)))

(define-syntax-parameter break (lambda (x) #f))

(define-syntax for
  (syntax-rules (:)
    ((for ((x ... : E) ...) ((c n) ...) code ... #:final fin)
     (for-work #f ((x ... : E) ...) ((c n) ...) (code ...) fin))
    
    ((for lp ((x ... : E) ...) ((c n) ...) code ... #:final fin)
     (for-work lp ((x ... : E) ...) ((c n) ...) (code ...) fin))
    
    ((for ((x ... : E) ...) ((c n) ...) code ...)
     (for-work #f ((x ... : E) ...) ((c n) ...) (code ...) (values)))
    
    ((for lp ((x ... : E) ...) ((c n) ...) code ...)
     (for-work lp ((x ... : E) ...) ((c n) ...) (code ...) (values)))))
    
(define-syntax for-work
  (lambda (x)
    (define (wrap-continue lp code)
      (if (syntax->datum lp)
          #`(lambda () (let/ec #,lp #,@code))
          #`(lambda ()              #,@code)))
      
    (syntax-case x ()
      ((for lp ((x ... : E) ...) ((c n) ...) (code ...) fin)
       (with-syntax (((It ...)       (generate-temporaries #'(E ...)))
                     ((cc ...)       (generate-temporaries #'(c ...)))
                     (((x1 ...) ...) (generate-temporaries2 #'((x ...) ...)))
                     (((x2 ...) ...) (generate-temporaries2 #'((x ...) ...)))
                     (llp            (if (syntax->datum #'lp) #'lp #'lpu)))
         
         #`(let/ec lp-break
             (syntax-parameterize ((break (lambda (z)
                                            (syntax-case z ()
                                              ((_ . l)
                                               #'(lp-break . l))
                                              (_ #'lp-break)))))
                                  
               (let ((It (wrap-in E)) ...
                     (c  n          ) ...
                     (x  'None      ) ... ...
                     (x1 #f         ) ... ...)
                 (catch StopIteration
                   (lambda ()
                     (let llp ((cc c) ...)                                
                       (set! c cc) ...
                       (call-with-values
                           (lambda () (next It))
                         (lambda (x2 ...)
                           (set! x1 x2) ...))
                       ...
                       (set! x x1)
                       ... ...
                       (call-with-values
                           #,(wrap-continue #'lp #'(code ...))
                         (lambda (cc ... . q) (llp cc ...)))))
                   (lambda q fin))))))))))

(define-class <scm-list>   () l)
(define-class <scm-string> () s i)
  
(define-method (next (l <scm-list>))
  (let ((ll (slot-ref l 'l)))
    (if (pair? ll)
        (begin
          (slot-set! l 'l (cdr ll))
          (car ll))
        (throw StopIteration))))

(define-method (next (l <scm-string>))
  (let ((s (slot-ref l 's))
        (i (slot-ref l 'i)))
    (if (= i (string-length s))
        (throw StopIteration)
        (begin
          (slot-set! l 'i (+ i 1))
          (string-ref s i)))))

(define-method (next (l <yield>))
  (let ((k (slot-ref l 'k))
        (s (slot-ref l 's)))
    (if k
        (k (lambda () 'None))
        (s))))


(define-method (next (l <p>))
  ((ref l '__next__)))

(define-method (wrap-in  (x <p>))
  (aif it (ref x '__iter__ #f)
       (it)
       x))

(define-method (wrap-in x)
  (cond
   ((pair? x)
    (let ((o (make <scm-list>)))
      (slot-set! o 'l x)
      o))
   
   ((string? x)
    (let ((o (make <scm-string>)))
      (slot-set! o 's x)
      (slot-set! o 'i 0)
      o))
   
   (else
    x)))


#;
(pk
 (for c ((x : (gen '(1 2 3)))) ((s 0))
      (pk x)
      (if (> x 2) (c s))
      (+ s x)

      #:final
      s))