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-defun list-length (list)
47 (when (endp fast
) (return length
))
48 (when (endp (cdr fast
)) (return (+ length
1)))
49 (when (and (eq fast slow
) (> length
0)) (return nil
))
50 (setf fast
(cddr fast
)))))
53 (swf-defmemfun listp
(a)
54 (or (%typep a cons-type
) (eq a nil
)))
59 ;; PUSH, POP in cl-conses
61 ;; FIRST - TENTH in cl-conses
63 (swf-defmemfun nth
(n list
)
64 (car (dotimes (x n list
)
65 (setf list
(cdr list
)))))
67 ;; ENDP, NULL in cl-conses
68 (swf-defmemfun nconc
(&arest lists
)
69 (let* ((a (if (zerop (slot-value lists
'%flash
:length
))
73 (dotimes (i (1- (slot-value lists
'%flash
:length
)) a
)
74 (let ((next (%aref-1 lists
(1+ i
))))
75 (rplacd (last end
) next
)
80 ;;Function REVAPPEND, NRECONC
82 ;;Function BUTLAST, NBUTLAST
84 ;; fixme: add optional count arg
85 (swf-defmemfun last
(a)
90 (unless (consp (cdr a
))
95 ;;Function LDIFF, TAILP
98 (swf-defmemfun nthcdr
(n list
)
100 (setf list
(cdr list
))))
102 (swf-defmemfun rest
(a)
105 ;;Function MEMBER, MEMBER-IF, MEMBER-IF-NOT
107 ;;Function MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON
111 ;;Function ASSOC, ASSOC-IF, ASSOC-IF-NOT
113 ;;Function COPY-ALIST
117 ;;Function RASSOC, RASSOC-IF, RASSOC-IF-NOT
119 ;;Function GET-PROPERTIES
125 ;;Function INTERSECTION, NINTERSECTION
131 ;;Function SET-DIFFERENCE, NSET-DIFFERENCE
133 ;;Function SET-EXCLUSIVE-OR, NSET-EXCLUSIVE-OR
137 ;;Function UNION, NUNION
141 (swf-defmacro %reverse-list
(list)
142 `(let ((reversed nil
))
143 (dolist (value ,list reversed
)
144 (push value reversed
))))
146 ;; macro due to lack of &key in functions
147 (swf-defmacro %reduce-list
(function sequence
&key key from-end
(start 0) end
(initial-value nil initial-value-p
))
148 `(let* ((list (if ,from-end
149 (nthcdr ,start
(%reverse-list
,sequence
))
150 (nthcdr ,start
,sequence
)))
153 ((,initial-value-p
) ,initial-value
)
154 ((null list
) (%funcall
,function nil
))
158 (setf list
(cdr list
)))))))
159 (dolist (a list result
)
160 (when (>= count
,end
) (return result
))
161 (setf result
(if ,key
162 (%funcall
,function nil result
(%funcall
,key a
))
163 (%funcall
,function nil result a
))))))