1 (in-package #:avm2-compiler
)
3 ;;; higher level functions from conses dictionary
4 ;;; (mainly things that need iteration constructs)
6 ;;; not all match CL semantics very closely yet...
8 (let ((*symbol-table
* *cl-symbol-table
*))
10 ;; Function SUBLIS, NSUBLIS
12 ;; Function SUBST, SUBST-IF, SUBST-IF-NOT, NSUBST, NSUBST-IF, NSUBST-IF-NOT
14 ;; Function TREE-EQUAL
16 ;; fixme: write iterative version of copy-list
17 (swf-defmemfun copy-list
(list)
18 (%flet
(do-copy (list)
20 (cons (car list
) (do-copy (cdr list
)))
22 (if (not (listp list
))
23 (%type-error
"COPY-LIST" list
)
24 (call-%flet do-copy list
))))
26 (swf-defmemfun list
(&arest rest
)
28 (length (%get-property rest
:length
)))
29 (dotimes (i length list
)
30 (push (%aref-1 rest
(- length i
1)) list
))))
32 (swf-defmemfun list
* (&arest rest
)
33 (when (zerop (%get-property rest
:length
))
34 (%error
"not enough arguments"))
35 (let* ((length (%get-property rest
:length
))
36 (list (%aref-1 rest
(1- length
))))
37 (dotimes (i (1- length
) list
)
38 (push (%aref-1 rest
(- length i
2)) list
))))
43 (swf-defmemfun list-length
(list)
49 (when (endp fast
) (return length
))
50 (when (endp (cdr fast
)) (return (+ length
1)))
51 (when (and (eq fast slow
) (> length
0)) (return nil
))
53 (setf fast
(cddr fast
))))))
56 (swf-defmemfun listp
(a)
57 (or (%typep a cons-type
) (eq a nil
)))
62 ;; PUSH, POP in cl-conses
64 ;; FIRST - TENTH in cl-conses
66 (swf-defmemfun nth
(n list
)
67 (car (dotimes (x n list
)
68 (setf list
(cdr list
)))))
70 ;; ENDP, NULL in cl-conses
71 (swf-defmemfun nconc
(&arest lists
)
72 (let* ((a (if (zerop (slot-value lists
'%flash
:length
))
76 (dotimes (i (1- (slot-value lists
'%flash
:length
)) a
)
77 (let ((next (%aref-1 lists
(1+ i
))))
78 (rplacd (last end
) next
)
83 ;;Function REVAPPEND, NRECONC
85 ;;Function BUTLAST, NBUTLAST
87 ;; fixme: add optional count arg
88 (swf-defmemfun last
(a)
93 (unless (consp (cdr a
))
98 ;;Function LDIFF, TAILP
101 (swf-defmemfun nthcdr
(n list
)
103 (setf list
(cdr list
))))
105 (swf-defmemfun rest
(a)
108 ;;Function MEMBER, MEMBER-IF, MEMBER-IF-NOT
110 ;;Function MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON
114 ;;Function ASSOC, ASSOC-IF, ASSOC-IF-NOT
116 ;;Function COPY-ALIST
120 ;;Function RASSOC, RASSOC-IF, RASSOC-IF-NOT
122 ;;Function GET-PROPERTIES
128 ;;Function INTERSECTION, NINTERSECTION
134 ;;Function SET-DIFFERENCE, NSET-DIFFERENCE
136 ;;Function SET-EXCLUSIVE-OR, NSET-EXCLUSIVE-OR
140 ;;Function UNION, NUNION
144 (swf-defmacro %reverse-list
(list)
145 `(let ((reversed nil
))
146 (dolist (value ,list reversed
)
147 (push value reversed
))))
149 ;; macro due to lack of &key in functions
150 (swf-defmacro %reduce-list
(function sequence
&key key from-end
(start 0) end
(initial-value nil initial-value-p
))
151 `(let* ((list (if ,from-end
152 (nthcdr ,start
(%reverse-list
,sequence
))
153 (nthcdr ,start
,sequence
)))
156 ((,initial-value-p
) ,initial-value
)
157 ((null list
) (%funcall
,function nil
))
161 (setf list
(cdr list
)))))))
162 (dolist (a list result
)
163 (when (>= count
,end
) (return result
))
164 (setf result
(if ,key
165 (%funcall
,function nil result
(%funcall
,key a
))
166 (%funcall
,function nil result a
))))))