compat.lsp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. % This file defines functions and variables needed to make REDUCE
  2. % and the underlying CSL system compatible. it should
  3. % be loaded as the first file whenever REDUCE services are required.
  4. (setpchar "> ")
  5. (remflag '(geq leq neq logand logor logxor leftshift princ printc
  6. evenp reversip seprp atsoc eqcar flagp!*!* flagpcar get!*
  7. prin1 prin2 apply0 apply1 apply2 apply3 smemq spaces
  8. subla gcdn printprompt pair) 'lose)
  9. (symbol!-make!-fastget 32)
  10. (symbol!-make!-fastget 'noncom 0) % built into the kernel
  11. (symbol!-make!-fastget 'lose 1)
  12. (flag '(raise lower echo comp plap pgwd pwrds savedef) 'switch)
  13. (make!-special '!*echo)
  14. (setq !*echo nil)
  15. (make!-special '!*raise)
  16. (setq !*raise nil)
  17. (make!-special '!*lower)
  18. (setq !*lower t)
  19. (make!-special '!*savedef)
  20. % I only nil out !*savedef if it is not already present because of
  21. % some bootstrapping delicacies when this file is re-loaded.
  22. (if (not (boundp '!*savedef)) (setq !*savedef nil))
  23. (make!-special '!*comp)
  24. (setq !*comp nil)
  25. (make!-special '!*plap)
  26. (setq !*plap nil)
  27. (make!-special '!*pgwd)
  28. (setq !*pgwd nil)
  29. (make!-special '!*pwrds)
  30. (setq !*pwrds t)
  31. % Until the following lines have been executed the
  32. % bitwise operations listed here will not work.
  33. (progn
  34. (symbol!-set!-env 'logand 1)
  35. (symbol!-set!-env 'logxor 6)
  36. (symbol!-set!-env 'logor 7)
  37. (symbol!-set!-env 'logeqv 9))
  38. (make!-special '!!fleps1)
  39. (setq !!fleps1 1.0e-12)
  40. (symbol!-set!-env 'safe!-fp!-plus '!!fleps1)
  41. (de rplacw (a b) (progn (rplaca a (car b)) (rplacd a (cdr b))))
  42. (de expand (l fn)
  43. (cond
  44. ((null (cdr l)) (car l))
  45. (t (list fn (car l) (expand (cdr l) fn)))))
  46. (dm plus (a)
  47. (cond ((null (cdr a)) 0)
  48. (t (expand (cdr a) 'plus2))))
  49. (dm times (a)
  50. (cond ((null (cdr a)) 1)
  51. (t (expand (cdr a) 'times2))))
  52. (de mapcar (l fn)
  53. (prog (r)
  54. top (cond ((null l) (return (reversip r))))
  55. (setq r (cons (funcall fn (car l)) r))
  56. (setq l (cdr l))
  57. (go top)))
  58. (de maplist (l fn)
  59. (prog (r)
  60. top (cond ((null l) (return (reversip r))))
  61. (setq r (cons (funcall fn l) r))
  62. (setq l (cdr l))
  63. (go top)))
  64. (de mapcan (l fn)
  65. (cond ((null l) nil)
  66. (t (nconc (funcall fn (car l)) (mapcan (cdr l) fn)))))
  67. (de mapcon (l fn)
  68. (cond ((null l) nil)
  69. (t (nconc (funcall fn l) (mapcon (cdr l) fn)))))
  70. (de mapc (l fn)
  71. (prog ()
  72. top (cond ((null l) (return nil)))
  73. (funcall fn (car l))
  74. (setq l (cdr l))
  75. (go top)))
  76. (de map (l fn)
  77. (prog ()
  78. top (cond ((null l) (return nil)))
  79. (funcall fn l)
  80. (setq l (cdr l))
  81. (go top)))
  82. (de copy (a)
  83. (cond
  84. ((atom a) a)
  85. (t (cons (copy (car a)) (copy (cdr a))))))
  86. (de sassoc (a l fn)
  87. (cond
  88. ((atom l) (funcall fn))
  89. ((equal a (caar l)) (car l))
  90. (t (sassoc a (cdr l) fn))))
  91. (de rassoc (x l) % Not in Standard Lisp
  92. (prog ()
  93. loop (cond ((atom l) (return nil))
  94. ((equal x (cdar l)) (return (car l)))
  95. (t (setq l (cdr l)) (go loop))) ))
  96. (de lastcar (x) % Not in Standard Lisp
  97. (cond
  98. ((null x) nil)
  99. ((null (cdr x)) (car x))
  100. (t (lastcar (cdr x)))))
  101. % The system-coded primitive function ~OPEN opens a file, and takes a second
  102. % argument that shows what options are wanted. See "print.c" for an
  103. % explanation of the bits.
  104. (de open (a b)
  105. (cond
  106. ((eq b 'input) (!~open a (plus 1 64))) % if-does-not-exist error
  107. ((eq b 'output) (!~open a (plus 2 20 32))) % if-does-not-exist create,
  108. % if-exists new-version
  109. ((eq b 'append) (!~open a (plus 2 8 32))) % if-exists append
  110. (t (error "bad direction ~A in open" b))))
  111. (de binopen (a b)
  112. (cond
  113. ((eq b 'input) (!~open a (plus 1 64 128)))
  114. ((eq b 'output) (!~open a (plus 2 20 32 128)))
  115. ((eq b 'append) (!~open a (plus 2 8 32 128)))
  116. (t (error "bad direction ~A in binopen" b))))
  117. (de pipe!-open (c d)
  118. (cond
  119. ((eq d 'input) (!~open c (plus 1 256)))
  120. ((eq d 'output) (!~open c (plus 2 256)))
  121. (t (error "bad direction ~A in pipe-open" d))))
  122. (de putd (a type b)
  123. (progn
  124. (cond
  125. ((eqcar b 'funarg) (setq b (cons 'lambda (cddr b)))))
  126. (cond
  127. ((flagp a 'lose) (progn
  128. (terpri) (princ "+++ ") (prin a)
  129. (printc " not defined (LOSE flag)")
  130. nil))
  131. (t (progn
  132. (cond
  133. ((and !*redefmsg (getd a)) (progn
  134. (terpri) (princ "+++ ") (prin a) (printc " redefined"))))
  135. (cond
  136. ((eq type 'expr) (symbol!-set!-definition a b))
  137. ((eq type 'subr) (symbol!-set!-definition a b))
  138. ((and (eq type 'macro) (eqcar b 'lambda))
  139. (eval (list!* 'dm a (cdr b))))
  140. (t (error "Bad type ~S in putd" type)))
  141. a))))))
  142. (de putc (a b c)
  143. (put a b c))
  144. (de traceset1 (name)
  145. (prog (w !*comp)
  146. (setq w (getd name))
  147. (cond ((not (and (eqcar w 'expr) (eqcar (cdr w) 'lambda)))
  148. (princ "+++++ ") (prin name)
  149. (printc " should be interpreted for traceset to work")
  150. (return nil)))
  151. (putd name 'expr (subst 'noisy!-setq 'setq (cdr w)))
  152. (trace (list name))))
  153. (de untraceset1 (name)
  154. (prog (w !*comp)
  155. (setq w (getd name))
  156. (cond ((not (and (eqcar w 'expr) (eqcar (cdr w) 'lambda)))
  157. (princ "+++++ ") (prin name)
  158. (printc " should be interpreted for untraceset to work")
  159. (return nil)))
  160. (putd name 'expr (subst 'setq 'noisy!-setq (cdr w)))
  161. (untrace (list name))))
  162. (de traceset (l)
  163. (mapc l (function traceset1)))
  164. (de untraceset (l)
  165. (mapc l (function untraceset1)))
  166. (de deflist (a b)
  167. (prog (r)
  168. top (cond ((null a) (return (reversip r))))
  169. (put (caar a) b (cadar a))
  170. (setq r (cons (caar a) r))
  171. (setq a (cdr a))
  172. (go top)))
  173. (de global (l)
  174. (prog nil
  175. top (cond ((null l) (return nil)))
  176. (make!-global (car l))
  177. (cond ((not (boundp (car l))) (set (car l) nil)))
  178. (setq l (cdr l))
  179. (go top)))
  180. (de fluid (l)
  181. (prog nil
  182. top (cond ((null l) (return nil)))
  183. (make!-special (car l))
  184. (cond ((not (boundp (car l))) (set (car l) nil)))
  185. (setq l (cdr l))
  186. (go top)))
  187. (de unglobal (l)
  188. (prog ()
  189. top (cond ((null l) (return nil)))
  190. (unmake!-global (car l))
  191. (setq l (cdr l))
  192. (go top)))
  193. (de unfluid (l)
  194. (prog ()
  195. top (cond ((null l) (return nil)))
  196. (unmake!-special (car l))
  197. (setq l (cdr l))
  198. (go top)))
  199. (global '(ofl!*))
  200. (de printprompt (u) nil)
  201. (global '(program!* ttype!* eof!*))
  202. (global '(crbuf!*))
  203. (global '(blank !$eol!$ tab !$eof!$ esc!*))
  204. (fluid '(!*notailcall !*carcheckflag))
  205. (fluid '(!*terminal!-io!* !*standard!-input!* !*standard!-output!*
  206. !*error!-output!* !*trace!-output!* !*debug!-io!* !*query!-io!*))
  207. (setq !*notailcall nil)
  208. (setq !*carcheckflag t)
  209. (de carcheck (n)
  210. (prog (old)
  211. (cond ((zerop n) (setq n nil)))
  212. (setq old !*carcheckflag)
  213. (setq !*carcheckflag n)
  214. (return old)))
  215. (progn
  216. (setq blank (compress (list '!! (special!-char 0))))
  217. (setq !$eol!$ (compress (list '!! (special!-char 1))))
  218. (setq tab (compress (list '!! (special!-char 3))))
  219. (setq esc!* (compress (list '!! (special!-char 9))))
  220. (setq !$eof!$ (special!-char 8))
  221. nil)
  222. (setq crbuf!* (list !$eol!$)) % may not be necessary
  223. (de symerr (u v)
  224. (progn (terpri)
  225. (print (list 'symerr u v))
  226. (error 'failure)))
  227. (de s!:oblist (v r)
  228. (prog (n a)
  229. (setq n (upbv v))
  230. top (cond ((minusp n) (return r)))
  231. (setq a (getv v n))
  232. (cond
  233. ((and (idp a)
  234. % I list things that have a function value of some sort or that have
  235. % a non-empty property-list. Symbols that have been mentioned but which do
  236. % not have properties or values are missed out since they are dull and
  237. % seeing them listed is probably not very helpful. People may disagree
  238. % about that... if so it would be very easy to remove the tests here and
  239. %end up listing everything
  240. (or (symbol!-function a)
  241. (macro!-function a)
  242. (special!-form!-p a)
  243. (fluidp a)
  244. (globalp a)
  245. (not (null (plist a)))))
  246. (setq r (cons a r))))
  247. (setq n (sub1 n))
  248. (go top)))
  249. (de s!:oblist1 (v r)
  250. (cond
  251. ((null v) r)
  252. ((vectorp v) (s!:oblist v r))
  253. % This allows for segmented object-vectors
  254. (t (s!:oblist (car v) (s!:oblist1 (cdr v) r)))))
  255. (de oblist ()
  256. (sort (s!:oblist1 (getv !*package!* 1) nil)
  257. (function orderp)))
  258. % Now a few things not needed by Standard Lisp but maybe helpful
  259. % when using Lisp directly.
  260. (de s!:make!-psetq!-vars (u)
  261. (if (null u)
  262. nil
  263. (if (null (cdr u))
  264. (error "odd number of items in psetq")
  265. (cons (gensym) (s!:make!-psetq!-vars (cddr u))))))
  266. (de s!:make!-psetq!-bindings (vars u)
  267. (if (null u)
  268. nil
  269. (cons
  270. (list (car vars) (cadr u))
  271. (s!:make!-psetq!-bindings (cdr vars) (cddr u)))))
  272. (de s!:make!-psetq!-assignments (vars u)
  273. (if (null u)
  274. nil
  275. (cons
  276. (list 'setq (car u) (car vars))
  277. (s!:make!-psetq!-assignments (cdr vars) (cddr u)))))
  278. (dm psetq (x)
  279. (!~let ((vars (s!:make!-psetq!-vars (cdr x))))
  280. `(let!* ,(s!:make!-psetq!-bindings vars (cdr x))
  281. ,@(s!:make!-psetq!-assignments vars (cdr x)))))
  282. % (do ((v i s) ..)
  283. % (end result ...)
  284. % body)
  285. (de s!:do!-bindings (u)
  286. (if (null u)
  287. nil
  288. (if (atom (car u))
  289. (cons (car u) (s!:do!-bindings (cdr u)))
  290. (if (null (cdar u))
  291. (cons (list (caar u) nil) (s!:do!-bindings (cdr u)))
  292. (cons (list (caar u) (cadar u)) (s!:do!-bindings (cdr u)))))))
  293. (de s!:do!-endtest (u)
  294. (if (null u)
  295. nil
  296. (car u)))
  297. (de s!:do!-result (u)
  298. (if (null u)
  299. nil
  300. (cdr u)))
  301. (de s!:do!-updates (u)
  302. (if (null u)
  303. nil
  304. (!~let ((v (car u))
  305. (x (s!:do!-updates (cdr u))))
  306. (if (or (atom v)
  307. (null (cdr v))
  308. (null (cddr v)))
  309. x
  310. (cons (car v) (cons (caddr v) x))))))
  311. (de s!:expand!-do (u letter setter)
  312. (let!* ((bindings (s!:do!-bindings (car u)))
  313. (result (s!:do!-result (cadr u)))
  314. (updates (s!:do!-updates (car u)))
  315. (body (cddr u))
  316. (endtest (s!:do!-endtest (cadr u)))
  317. (upd (if updates (list (cons setter updates)) nil))
  318. (res (if (null result)
  319. nil
  320. (if (null (cdr result))
  321. (car result)
  322. (cons 'progn result))))
  323. (x (if (null endtest) nil
  324. `((if ,endtest (return ,res)))))
  325. (g (gensym)))
  326. (if bindings
  327. `(,letter ,bindings
  328. (prog nil
  329. ,g ,@x
  330. ,@body
  331. ,@upd
  332. (go ,g)))
  333. `(prog nil
  334. ,g ,@x
  335. ,@body
  336. ,@upd
  337. (go ,g)))))
  338. (dm do (u) (s!:expand!-do (cdr u) '!~let 'psetq))
  339. (dm do!* (u) (s!:expand!-do (cdr u) 'let!* 'setq))
  340. (de s!:expand!-dolist (vir b)
  341. (prog (l v var init res)
  342. (setq var (car vir))
  343. (setq init (car (setq vir (cdr vir))))
  344. (setq res (cdr vir))
  345. (setq v (gensym))
  346. (setq l (gensym))
  347. (return `(prog (,v ,var)
  348. (setq ,v ,init)
  349. ,l (cond ((null ,v) (return (progn ,@res))))
  350. (setq ,var (car ,v))
  351. ,@b
  352. (setq ,v (cdr ,v))
  353. (go ,l)))))
  354. (dm dolist (u) (s!:expand!-dolist (cadr u) (cddr u)))
  355. (de s!:expand!-dotimes (vnr b)
  356. (prog (l v var count res)
  357. (setq var (car vnr))
  358. (setq count (car (setq vnr (cdr vnr))))
  359. (setq res (cdr vnr))
  360. (setq v (gensym))
  361. (setq l (gensym))
  362. (return `(prog (,v ,var)
  363. (setq ,v ,count)
  364. (setq ,var 0)
  365. ,l (cond ((not (lessp ,var ,v)) (return (progn ,@res))))
  366. ,@b
  367. (setq ,var (add1 ,var))
  368. (go ,l)))))
  369. (dm dotimes (u) (s!:expand!-dotimes (cadr u) (cddr u)))
  370. (flag '(geq leq neq logand logor logxor leftshift princ printc
  371. evenp reversip seprp atsoc eqcar flagp!*!* flagpcar get!*
  372. prin1 prin2 apply0 apply1 apply2 apply3 smemq spaces
  373. subla gcdn printprompt pair) 'lose)
  374. % end of compat.lsp