rlisp.lsp 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767
  1. ;
  2. ; Create RLISP. Use this via mkrlisp0.lsp or mkrlisp1.lsp
  3. ;
  4. ; Standard LISP equivalent of BOOT.RED.
  5. (proclaim '(special *blockp *mode))
  6. (proclaim '(special oldchan*))
  7. (proclaim '(special crchar* cursym* fname* nxtsym* ttype* $eol$ !$eof!$))
  8. (setq *blockp nil) (setq *mode nil) (setq oldchan* nil)
  9. (setq crchar* nil) (setq cursym* nil) (setq fname* nil)
  10. (setq nxtsym* nil) (setq ttype* nil)
  11. (defun compress (l)
  12. (prog (r c)
  13. (setq c (car l))
  14. (if (or (eq c '|"|) (digit c)) (return (compress1 l)))
  15. (while l
  16. (setq c (car l))
  17. (setq l (cdr l))
  18. (when (eq c '|!|) (setq c (car l)) (setq l (cdr l)))
  19. (setq r (cons c (cons '|\\| r))))
  20. (return (compress1 (nreverse r)))))
  21. (setq $eol$ (compress1 (list '|\\| (special-char 1))))
  22. (setq $eof$ (special-char 8))
  23. (put '|;| 'switch* '(nil *semicol*))
  24. (put '|(| 'switch* '(nil *lpar*))
  25. (put '|)| 'switch* '(nil *rpar*))
  26. (put '|,| 'switch* '(nil *comma*))
  27. (put '|.| 'switch* '(nil cons))
  28. (put '|:| 'switch* '(((= nil setq)) *colon*))
  29. (put '*comma* 'infix 1)
  30. (put 'setq 'infix 2)
  31. (put 'cons 'infix 3)
  32. (flag '(*comma*) 'nary)
  33. (flag '(*colon* *semicol* end then else) 'delim)
  34. (put 'begin 'stat 'blockstat)
  35. (put 'if 'stat 'ifstat)
  36. (put 'symbolic 'stat 'procstat)
  37. (de begin2 nil
  38. (prog nil
  39. (setq cursym* '*semicol*)
  40. a (cond
  41. ((eq cursym* 'end) (progn (rds oldchan*) (return nil)))
  42. (t (prin2 (errorset '(eval (form (xread nil))) t t)) ))
  43. (go a)))
  44. (de form (u) u)
  45. (de xread (u) (progn (scan) (xread1 u)))
  46. (de xread1 (u)
  47. (prog (v w x y z z2)
  48. a (setq z cursym*)
  49. a1 (cond
  50. ((or (null (atom z)) (numberp z)) (setq y nil))
  51. ((flagp z 'delim) (go end1))
  52. ((eq z '*lpar*) (go lparen))
  53. ((eq z '*rpar*) (go end1))
  54. ((setq y (get z 'infix)) (go infx))
  55. ((setq y (get z 'stat)) (go stat)))
  56. a3 (setq w (cons z w))
  57. next (setq z (scan))
  58. (go a1)
  59. lparen(setq y nil)
  60. (cond
  61. ((eq (scan) '*rpar*)
  62. (and w (setq w (cons (list (car w)) (cdr w)))) )
  63. ((eqcar (setq z (xread1 'paren)) '*comma*)
  64. (setq w (cons (cons (car w) (cdr z)) (cdr w))))
  65. (t (go a3)))
  66. (go next)
  67. infx (setq z2 (mkvar (car w) z))
  68. un1 (setq w (cdr w))
  69. (cond
  70. ((null w) (go un2))
  71. (t (setq z2 (cons (car w) (list z2)))) )
  72. (go un1)
  73. un2 (setq v (cons z2 v))
  74. preced(cond ((null x) (go pr4)) ((lessp y (car (car x))) (go pr2)))
  75. pr1 (setq x (cons (cons y z) x))
  76. (go next)
  77. pr2 (setq v
  78. (cons
  79. (cond
  80. ((and (eqcar (car v) (cdar x)) (flagp (cdar x) 'nary))
  81. (cons (cdar x) (cons (cadr v) (cdar v))))
  82. (t (cons (cdar x) (list (cadr v) (car v)))) )
  83. (cdr (cdr v))))
  84. (setq x (cdr x))
  85. (go preced)
  86. stat (setq w (cons (eval (list y)) w))
  87. (setq y nil)
  88. (go a)
  89. end1 (cond
  90. ((and (and (null v) (null w)) (null x)) (return nil))
  91. (t (setq y 0)))
  92. (go infx)
  93. pr4 (cond ((null (equal y 0)) (go pr1)) (t (return (car v)))) ))
  94. (de eqcar (u v) (and (null (atom u)) (eq (car u) v)))
  95. (de mksetq (u v) (list 'setq u v))
  96. (de mkvar (u v) u)
  97. (de rread nil
  98. (prog (x)
  99. (setq x (token))
  100. (return
  101. (cond
  102. ((and (equal ttype* 3) (eq x '|(|)) (rrdls))
  103. (t x)))) )
  104. (de reverse2 (a b)
  105. (prog nil
  106. a (cond ((null a) (return b)))
  107. (setq b (cons (car a) b))
  108. (setq a (cdr a))))
  109. (de rrdls nil
  110. (prog (x r)
  111. a (setq x (rread))
  112. (cond
  113. ((null (equal ttype* 3)) (go b))
  114. ((eq x '|)|) (return (reversip r)))
  115. ((null (eq x '|.|)) (go b)))
  116. (setq x (rread))
  117. (token)
  118. (return (reverse2 r x))
  119. b (setq r (cons x r))
  120. (go a)))
  121. (de token nil
  122. (prog (x y)
  123. (setq x crchar*)
  124. a (cond
  125. ((seprp x) (go sepr))
  126. ((digit x) (go number))
  127. ((liter x) (go letter))
  128. ((eq x '|%|) (go coment))
  129. ((eq x '|!|) (go escape))
  130. ((eq x '|'|) (go quote))
  131. ((eq x '|"|) (go string)))
  132. (setq ttype* 3)
  133. (cond ((delcp x) (go d)))
  134. (setq nxtsym* x)
  135. a1 (setq crchar* (readch))
  136. (go c)
  137. escape(setq y (cons x y))
  138. (setq x (readch))
  139. letter(setq ttype* 0)
  140. let1 (setq y (cons x y))
  141. (cond
  142. ((or (digit (setq x (readch))) (liter x)) (go let1))
  143. ((eq x '|!|) (go escape)))
  144. (setq nxtsym* (intern (compress (reverse y))))
  145. b (setq crchar* x)
  146. c (return nxtsym*)
  147. number(setq ttype* 2)
  148. num1 (setq y (cons x y))
  149. (cond ((digit (setq x (readch))) (go num1)))
  150. (setq nxtsym* (compress (reverse y)))
  151. (go b)
  152. quote (setq crchar* (readch))
  153. (setq nxtsym* (list 'quote (rread)))
  154. (setq ttype* 4)
  155. (go c)
  156. string(prog (raise)
  157. (setq raise *raise)
  158. (setq *raise nil)
  159. strinx(setq y (cons x y))
  160. (cond ((null (eq (setq x (readch)) '|"|)) (go strinx)))
  161. (setq y (cons x y))
  162. (setq nxtsym* (mkstrng (compress (reverse y))))
  163. (setq *raise raise))
  164. (setq ttype* 1)
  165. (go a1)
  166. coment(cond ((null (eq (readch) $eol$)) (go coment)))
  167. sepr (setq x (readch))
  168. (go a)
  169. d (setq nxtsym* x)
  170. (setq crchar* '| |)
  171. (go c)))
  172. (setq crchar* '| |)
  173. (de delcp (u) (or (eq u '|;|) (eq u '|$|)))
  174. (de mkstrng (u) u)
  175. (de scan nil
  176. (prog (x y)
  177. (cond ((null (eq cursym* '*semicol*)) (go b)))
  178. a (setq nxtsym* (token))
  179. b (cond
  180. ((or (null (atom nxtsym*)) (numberp nxtsym*)) (go l))
  181. ((and (setq x (get nxtsym* 'newnam)) (setq nxtsym* x))
  182. (go b))
  183. ((eq nxtsym* 'comment) (go comm))
  184. ((and
  185. (eq nxtsym* '|'|)
  186. (setq cursym* (list 'quote (rread))))
  187. (go l1))
  188. ((null (setq x (get nxtsym* 'switch*))) (go l))
  189. ((eq (cadr x) '*semicol*)
  190. (return (setq cursym* (cadr x)))) )
  191. sw1 (setq nxtsym* (token))
  192. (cond
  193. ((or
  194. (null (car x))
  195. (null (setq y (assoc nxtsym* (car x)))) )
  196. (return (setq cursym* (cadr x)))) )
  197. (setq x (cdr y))
  198. (go sw1)
  199. comm (cond ((eq (readch) '|;|) (setq crchar* '| |)) (t (go comm)))
  200. (go a)
  201. l (setq cursym*
  202. (cond
  203. ((null (eqcar nxtsym* 'string)) nxtsym*)
  204. (t (cons 'quote (cdr nxtsym*)))) )
  205. l1 (setq nxtsym* (token))
  206. (return cursym*)))
  207. (de ifstat nil
  208. (prog (condx condit)
  209. a (setq condx (xread t))
  210. (setq condit (nconc condit (list (list condx (xread t)))) )
  211. (cond
  212. ((null (eq cursym* 'else)) (go b))
  213. ((eq (scan) 'if) (go a))
  214. (t (setq condit
  215. (nconc condit (list (list t (xread1 t)))) )))
  216. b (return (cons 'cond condit))))
  217. (de procstat nil
  218. (prog (x y)
  219. (cond ((eq cursym* 'symbolic) (scan)))
  220. (cond
  221. ((eq cursym* '*semicol*)
  222. (return (null (setq *mode 'symbolic)))) )
  223. (setq fname* (scan))
  224. (cond ((atom (setq x (xread1 nil))) (setq x (list x))))
  225. (setq y (xread nil))
  226. (cond ((flagp (car x) 'lose) (return nil)))
  227. (putd (car x) 'expr (list 'lambda (cdr x) y))
  228. (setq fname* nil)
  229. (return (list 'quote (car x)))) )
  230. (de blockstat nil
  231. (prog (x hold varlis *blockp)
  232. a0 (setq *blockp t)
  233. (scan)
  234. (cond
  235. ((null (or (eq cursym* 'integer) (eq cursym* 'scalar)))
  236. (go a)))
  237. (setq x (xread nil))
  238. (setq varlis
  239. (nconc
  240. (cond ((eqcar x '*comma*) (cdr x)) (t (list x)))
  241. varlis))
  242. (go a0)
  243. a (setq hold (nconc hold (list (xread1 nil))))
  244. (setq x cursym*)
  245. (scan)
  246. (cond ((not (eq x 'end)) (go a)))
  247. (return (mkprog varlis hold))))
  248. (de mkprog (u v) (cons 'prog (cons u v)))
  249. (de gostat nil
  250. (prog (x) (scan) (setq x (scan)) (scan) (return (list 'go x))))
  251. (put 'go 'stat 'gostat)
  252. (de rlis nil
  253. (prog (x)
  254. (setq x cursym*)
  255. (return (cond ((not (flagp (scan) 'delim))
  256. (list x (list 'quote (list (xread1 t)))))
  257. (t (list x))))))
  258. (begin2)
  259. %
  260. % This file is "rlisp.red" taken from the REDUCE 3.3 sources, and can be
  261. % used to reconstruct the CSL files compiler.lsp and ccomp.lsp from
  262. % the associated RLISP source files.
  263. %
  264. % module module; % Support for module use.
  265. % Author: Anthony C. Hearn.
  266. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  267. fluid '(!*mode);
  268. global '(exportslist!* importslist!* module!-name!* old!-mode!*);
  269. !*mode := 'symbolic; % initial value.
  270. symbolic procedure exports u;
  271. begin exportslist!* := union(u,exportslist!*); end;
  272. symbolic procedure imports u;
  273. begin importslist!* := union(u,importslist!*); end;
  274. symbolic procedure module u;
  275. %Sets up a module definition;
  276. begin
  277. if null module!-name!* then old!-mode!* := !*mode;
  278. module!-name!* := car u . module!-name!*;
  279. !*mode := 'symbolic
  280. end;
  281. symbolic procedure endmodule;
  282. begin
  283. if null module!-name!*
  284. then rederr "ENDMODULE called outside module";
  285. exportslist!* := nil;
  286. importslist!* := nil;
  287. module!-name!* := cdr module!-name!*;
  288. if module!-name!* then return nil;
  289. !*mode := old!-mode!*;
  290. old!-mode!* := nil
  291. end;
  292. deflist('((exports rlis) (imports rlis) (module rlis)),'stat);
  293. if null get('endmodule, 'stat) then put('endmodule,'stat,'rlis);
  294. % For bootstrap only
  295. flag('(endmodule),'go);
  296. % endmodule;
  297. module newtok; % Functions for introducing infix tokens to the system.
  298. % Author: Anthony C. Hearn.
  299. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  300. fluid '(!*redeflg!*);
  301. global '(!*msg preclis!*);
  302. %Several operators in REDUCE are used in an infix form (e.g.,
  303. %+,- ). The internal alphanumeric names associated with these
  304. %operators are introduced by the function NEWTOK defined below.
  305. %This association, and the precedence of each infix operator, is
  306. %initialized in this section. We also associate printing characters
  307. %with each internal alphanumeric name as well;
  308. preclis!*:= '(or and not member memq equal neq eq geq greaterp leq
  309. lessp freeof plus difference times quotient expt cons);
  310. deflist ('(
  311. (not not)
  312. (plus plus)
  313. (difference minus)
  314. (minus minus)
  315. (times times)
  316. (quotient recip)
  317. (recip recip)
  318. ), 'unary);
  319. flag ('(and or !*comma!* plus times),'nary);
  320. flag ('(cons setq plus times),'right);
  321. deflist ('((minus plus) (recip times)),'alt);
  322. symbolic procedure mkprec;
  323. begin scalar x,y,z;
  324. x := 'where . ('!*comma!* . ('setq . preclis!*));
  325. y := 1;
  326. a: if null x then return nil;
  327. put(car x,'infix,y);
  328. put(car x,'op,list list(y,y)); %for RPRINT;
  329. if z := get(car x,'unary) then put(z,'infix,y);
  330. if and(z,null flagp(z,'nary)) then put(z,'op,list(nil,y));
  331. x := cdr x;
  332. y := add1 y;
  333. go to a
  334. end;
  335. mkprec();
  336. symbolic procedure newtok u;
  337. begin scalar !*redeflg!*,x,y;
  338. if atom u or atom car u or null idp caar u
  339. then typerr(u,"NEWTOK argument");
  340. % set up SWITCH* property.
  341. put(caar u,'switch!*,
  342. cdr newtok1(car u,cadr u,get(caar u,'switch!*)));
  343. % set up PRTCH property.
  344. y := intern compress consescc car u;
  345. if !*redeflg!* then lprim list(y,"redefined");
  346. put(cadr u,'prtch,y);
  347. if x := get(cadr u,'unary) then put(x,'prtch,y)
  348. end;
  349. symbolic procedure newtok1(charlist,name,propy);
  350. if null propy then lstchr(charlist,name)
  351. else if null cdr charlist
  352. then begin
  353. if cdr propy and !*msg then !*redeflg!* := t;
  354. return list(car charlist,car propy,name)
  355. end
  356. else car charlist . newtok2(cdr charlist,name,car propy)
  357. . cdr propy;
  358. symbolic procedure newtok2(charlist,name,assoclist);
  359. if null assoclist then list lstchr(charlist,name)
  360. else if car charlist eq caar assoclist
  361. then newtok1(charlist,name,cdar assoclist) . cdr assoclist
  362. else car assoclist . newtok2(charlist,name,cdr assoclist);
  363. symbolic procedure consescc u;
  364. if null u then nil else '!! . car u . consescc cdr u;
  365. symbolic procedure lstchr(u,v);
  366. if null cdr u then list(car u,nil,v)
  367. else list(car u,list lstchr(cdr u,v));
  368. newtok '((!$) !*semicol!*);
  369. newtok '((!;) !*semicol!*);
  370. newtok '((!+) plus);
  371. newtok '((!-) difference);
  372. newtok '((!*) times);
  373. newtok '((!^) expt);
  374. newtok '((!* !*) expt);
  375. newtok '((!/) quotient);
  376. newtok '((!=) equal);
  377. newtok '((!,) !*comma!*);
  378. newtok '((!() !*lpar!*);
  379. newtok '((!)) !*rpar!*);
  380. newtok '((!:) !*colon!*);
  381. newtok '((!: !=) setq);
  382. newtok '((!.) cons);
  383. newtok '((!<) lessp);
  384. newtok '((!< !=) leq);
  385. newtok '((!< !<) !*lsqb!*);
  386. newtok '((!>) greaterp);
  387. newtok '((!> !=) geq);
  388. newtok '((!> !>) !*rsqb!*);
  389. put('expt,'prtch,'!*!*); % To ensure that FORTRAN output is correct.
  390. flag('(difference minus plus setq),'spaced);
  391. flag('(newtok),'eval);
  392. endmodule;
  393. module support; % Basic functions needed to support RLISP and REDUCE.
  394. % Author: Anthony C. Hearn.
  395. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  396. symbolic procedure aconc(u,v);
  397. %adds element v to the tail of u. u is destroyed in process;
  398. nconc(u,list v);
  399. symbolic procedure r!-arrayp u; get(u,'rtype) eq 'array;
  400. symbolic procedure idlistp u;
  401. % True if u is a list of id's.
  402. null u or null atom u and idp car u and idlistp cdr u;
  403. symbolic procedure mkprog(u,v); 'prog . (u . v);
  404. symbolic procedure mksetq(u,v); list('setq,u,v);
  405. symbolic procedure pairvars(u,vars,mode);
  406. % Sets up pairings of parameters and modes.
  407. begin scalar x;
  408. a: if null u then return append(reversip!* x,vars)
  409. else if null idp car u then symerr("Invalid parameter",nil);
  410. x := (car u . mode) . x;
  411. u := cdr u;
  412. go to a
  413. end;
  414. symbolic procedure prin2t u; progn(prin2 u, terpri(), u);
  415. symbolic procedure smemq(u,v);
  416. %true if id U is a member of V at any level (excluding
  417. %quoted expressions);
  418. if atom v then u eq v
  419. else if car v eq 'quote then nil
  420. else smemq(u,car v) or smemq(u,cdr v);
  421. symbolic procedure u neq v; null(u=v);
  422. symbolic procedure setdiff(u,v);
  423. if null v then u else setdiff(delete(car v,u),cdr v);
  424. % List changing alternates (may also be defined as copying functions)
  425. symbolic procedure aconc!*(u,v); nconc(u,list v); % append(u,list v);
  426. symbolic procedure nconc!*(u,v); nconc(u,v); % append(u,v);
  427. symbolic procedure reversip!* u; reversip u; % reverse u;
  428. symbolic procedure rplaca!*(u,v); rplaca(u,v); % v . cdr u;
  429. symbolic procedure rplacd!*(u,v); rplacd(u,v); % car u . v;
  430. % The following functions should be provided in the compiler for
  431. % efficient coding.
  432. %symbolic procedure apply1(u,v); apply(u,list v);
  433. %symbolic procedure apply2(u,v,w); apply(u,list(v,w));
  434. %symbolic procedure apply3(u,v,w,x); apply(u,list(v,w,x));
  435. % The following function is needed by several modules. It is more
  436. % REDUCE-specific than other functions in this module, but since it
  437. % needs to be defined early on, it might as well go here.
  438. symbolic procedure gettype u;
  439. % Returns a REDUCE-related type for the expression U.
  440. % It needs to be more table driven than the current definition.
  441. if numberp u then 'number
  442. else if null atom u or null u or null idp u then 'form
  443. else if get(u,'simpfn) then 'operator
  444. else if get(u,'avalue) then 'variable
  445. else if getd u then 'procedure
  446. else if globalp u then 'global
  447. else if fluidp u then 'fluid
  448. else if flagp(u,'parm) then 'parameter
  449. else get(u,'rtype);
  450. endmodule;
  451. module slfns; % Complete list of Standard LISP functions.
  452. % Author: Anthony C. Hearn.
  453. global '(!*argnochk slfns!*);
  454. slfns!* := '(
  455. (abs 1)
  456. (add1 1)
  457. (append 2)
  458. (apply 2)
  459. (assoc 2)
  460. (atom 1)
  461. (car 1)
  462. (cdr 1)
  463. (caar 1)
  464. (cadr 1)
  465. (cdar 1)
  466. (cddr 1)
  467. (caaar 1)
  468. (caadr 1)
  469. (cadar 1)
  470. (caddr 1)
  471. (cdaar 1)
  472. (cdadr 1)
  473. (cddar 1)
  474. (cdddr 1)
  475. (caaaar 1)
  476. (caaadr 1)
  477. (caadar 1)
  478. (caaddr 1)
  479. (cadaar 1)
  480. (cadadr 1)
  481. (caddar 1)
  482. (cadddr 1)
  483. (cdaaar 1)
  484. (cdaadr 1)
  485. (cdadar 1)
  486. (cdaddr 1)
  487. (cddaar 1)
  488. (cddadr 1)
  489. (cdddar 1)
  490. (cddddr 1)
  491. (close 1)
  492. (codep 1)
  493. (compress 1)
  494. (cons 2)
  495. (constantp 1)
  496. (de 3)
  497. (deflist 2)
  498. (delete 2)
  499. % (DF 3) conflicts with algebraic operator DF
  500. (difference 2)
  501. (digit 1)
  502. (divide 2)
  503. (dm 3)
  504. (dn 3)
  505. (ds 3)
  506. (eject 0)
  507. (eq 2)
  508. (eqn 2)
  509. (equal 2)
  510. (error 2)
  511. (errorset 3)
  512. (eval 1)
  513. (evlis 1)
  514. (expand 2)
  515. (explode 1)
  516. (expt 2)
  517. (fix 1)
  518. (fixp 1)
  519. (flag 2)
  520. (flagp 2)
  521. (float 1)
  522. (floatp 1)
  523. (fluid 1)
  524. (fluidp 1)
  525. (function 1)
  526. (gensym 0)
  527. (get 2)
  528. (getd 1)
  529. (getv 2)
  530. (global 1)
  531. (globalp 1)
  532. (go 1)
  533. (greaterp 2)
  534. (idp 1)
  535. (intern 1)
  536. (length 1)
  537. (lessp 2)
  538. (linelength 1)
  539. (liter 1)
  540. (lposn 0)
  541. (map 2)
  542. (mapc 2)
  543. (mapcan 2)
  544. (mapcar 2)
  545. (mapcon 2)
  546. (maplist 2)
  547. (max2 2)
  548. (member 2)
  549. (memq 2)
  550. (minus 1)
  551. (minusp 1)
  552. (min2 2)
  553. (mkvect 1)
  554. (nconc 2)
  555. (not 1)
  556. (null 1)
  557. (numberp 1)
  558. (onep 1)
  559. (open 2)
  560. (pagelength 1)
  561. (pair 2)
  562. (pairp 1)
  563. (plus2 2)
  564. (posn 0)
  565. (print 1)
  566. (prin1 1)
  567. (prin2 1)
  568. (prog2 2)
  569. (put 3)
  570. (putd 3)
  571. (putv 3)
  572. (quote 1)
  573. (quotient 2)
  574. (rds 1)
  575. (read 0)
  576. (readch 0)
  577. (remainder 2)
  578. (remd 1)
  579. (remflag 2)
  580. (remob 1)
  581. (remprop 2)
  582. (return 1)
  583. (reverse 1)
  584. (rplaca 2)
  585. (rplacd 2)
  586. (sassoc 3)
  587. (set 2)
  588. (setq 2)
  589. (stringp 1)
  590. (sublis 2)
  591. (subst 3)
  592. (sub1 1)
  593. (terpri 0)
  594. (times2 2)
  595. (unfluid 1)
  596. (upbv 1)
  597. (vectorp 1)
  598. (wrs 1)
  599. (zerop 1)
  600. );
  601. if !*argnochk then deflist(slfns!*,'number!-of!-args);
  602. endmodule;
  603. module superv; % REDUCE supervisory functions.
  604. % Author: Anthony C. Hearn.
  605. % Modified by: Jed B. Marti.
  606. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  607. fluid '(!*backtrace
  608. !*defn
  609. !*errcont
  610. !*int
  611. !*mode
  612. !*slin
  613. !*time
  614. dfprint!*
  615. lreadfn!*
  616. semic!*
  617. tslin!*);
  618. global '(!$eof!$
  619. !*byeflag!*
  620. !*demo
  621. !*echo
  622. !*extraecho
  623. !*lessspace
  624. !*micro!-version
  625. !*nosave!*
  626. !*output
  627. !*pret
  628. !*rlisp2
  629. !*strind
  630. !*struct
  631. cloc!*
  632. cmsg!*
  633. crbuf!*
  634. crbuflis!*
  635. crbuf1!*
  636. cursym!*
  637. eof!*
  638. erfg!*
  639. ifl!*
  640. ipl!*
  641. initl!*
  642. inputbuflis!*
  643. key!*
  644. ofl!*
  645. opl!*
  646. ogctime!*
  647. otime!*
  648. program!*
  649. programl!*
  650. resultbuflis!*
  651. st!*
  652. statcounter
  653. symchar!*
  654. tok!*
  655. ttype!*
  656. ws);
  657. !*output := t;
  658. eof!* := 0;
  659. initl!* := '(fname!* outl!*);
  660. statcounter := 0;
  661. % The true REDUCE supervisory function is BEGIN, again defined in the
  662. % system dependent part of this program. However, most of the work is
  663. % done by BEGIN1, which is called by BEGIN for every file encountered
  664. % on input;
  665. symbolic procedure errorp u;
  666. %returns true if U is an ERRORSET error format;
  667. atom u or cdr u;
  668. symbolic procedure flagp!*!*(u,v); idp u and flagp(u,v);
  669. symbolic procedure setcloc!*;
  670. % Used to set for file input a global variable CLOC!* to dotted pair
  671. % of file name and dotted pair of line and page being read.
  672. % Currently a place holder for system specific function, since not
  673. % supported in Standard LISP. CLOC!* is used in the INTER and RCREF
  674. % modules.
  675. cloc!* := if null ifl!* then nil else car ifl!* . nil;
  676. symbolic procedure command;
  677. begin scalar x;
  678. if !*demo and (x := ifl!*)
  679. then progn(terpri(),rds nil,readch(),rds cadr x);
  680. if null !*slin
  681. then if !*rlisp2
  682. then progn(s!&(),
  683. key!* := tok!*,
  684. m!-metarlisp(),
  685. (if st!* then x := car st!* else x := nil),
  686. st!* := nil)
  687. else progn(scan(), setcloc!*(), key!* := cursym!*,
  688. x := xread1 nil)
  689. else progn(key!* := (semic!* := '!;),
  690. setcloc!*(),
  691. x := (if lreadfn!* then apply(lreadfn!*,nil)
  692. else read()),
  693. if key!* eq '!;
  694. then key!* := if atom x then x else car x);
  695. if !*struct then x := structchk x;
  696. if !*pret then progn(terpri(),rprint x);
  697. if null !*slin then x := form x;
  698. return x
  699. end;
  700. symbolic procedure begin1;
  701. begin scalar mode,parserr,result,x;
  702. if !*rlisp2 then prolog 'm!-metarlisp;
  703. otime!* := time();
  704. % the next line is that way for bootstrapping purposes.
  705. if getd 'gctime then ogctime!* := gctime() else ogctime!* := 0;
  706. a0: cursym!* := '!*semicol!*;
  707. a: if null terminalp() or !*nosave!* then go to b
  708. else if statcounter>0 then add2buflis();
  709. statcounter := statcounter + 1;
  710. crbuf1!* := nil; % For input string editor.
  711. !*strind := 0; % Used by some versions of input editor.
  712. setpchar compress1('!| . append(explodec statcounter,
  713. if null symchar!* or !*mode eq 'algebraic
  714. then '(!: ! !|) else '(!* ! !|)));
  715. b: parserr := nil;
  716. !*nosave!* := nil;
  717. if !*time then eval '(showtime); %Since a STAT;
  718. if !*output and null ofl!* and terminalp() and null !*defn
  719. and null !*lessspace
  720. then terpri();
  721. if tslin!*
  722. then progn(!*slin := car tslin!*,
  723. lreadfn!* := cdr tslin!*,
  724. tslin!* := nil);
  725. mapcar(function sinitl, initl!*);
  726. if !*int then erfg!* := nil; %to make editing work properly;
  727. if null !*rlisp2 and cursym!* eq 'end
  728. then progn(comm1 'end, return nil);
  729. program!* := errorset('(command),t,!*backtrace);
  730. if !*rlisp2
  731. then if tok!* eq '!*semic!* then semic!* := '!;
  732. else semic!* := '!$;
  733. condterpri();
  734. if errorp program!* then go to err1;
  735. program!* := car program!*;
  736. if eofcheck() then go to c else eof!* := 0;
  737. if !*rlisp2 then if program!* = '(end) then return nil else nil
  738. else if cursym!* eq 'end
  739. then if !*micro!-version and terminalp() then go to a0
  740. else progn(comm1 'end, return nil)
  741. else if eqcar(program!*,'retry) then program!* := programl!*;
  742. %The following section decides what the target mode should be.
  743. %That mode is also assumed to be the printing mode;
  744. if flagp!*!*(key!*,'modefn) then mode := key!*
  745. else if null atom program!* % and null !*micro!-version
  746. and null(car program!* eq 'quote)
  747. and (null(idp car program!*
  748. and (flagp(car program!*,'nochange)
  749. or flagp(car program!*,'intfn)
  750. or car program!* eq 'list))
  751. or car program!* memq '(setq setel setf)
  752. and eqcar(caddr program!*,'quote))
  753. then mode := 'symbolic
  754. else if key!* eq 'input
  755. and (x := rassoc!*(program!*,inputbuflis!*))
  756. then mode := cddr x
  757. else mode := !*mode;
  758. program!* := convertmode1(program!*,nil,'symbolic,mode);
  759. add2inputbuf(program!*,!*mode);
  760. % This used to be MODE, but then ED n wouldn't work.
  761. if null !*rlisp2 and null atom program!*
  762. and car program!* memq '(bye quit)
  763. then if getd 'bye then progn(eval program!*, go to b)
  764. else progn(!*byeflag!* := t, return nil)
  765. else if null !*rlisp2 and eqcar(program!*,'ed)
  766. then progn((if getd 'cedit and terminalp()
  767. then cedit cdr program!*
  768. else lprim "ED not supported"),
  769. go to b)
  770. else if !*defn
  771. then if erfg!* then go to a
  772. else if null flagp!*!*(key!*,'ignore)
  773. and null eqcar(program!*,'quote)
  774. then go to d;
  775. b1: if !*output and ifl!* and !*echo and null !*lessspace
  776. then terpri();
  777. result := errorset((if mode eq 'symbolic then program!*
  778. else list('assgneval,mkquote program!*)),
  779. t,!*backtrace);
  780. if errorp result or erfg!*
  781. then progn(programl!* := program!*,go to err2)
  782. else if !*defn then go to a;
  783. if null(mode eq 'symbolic)
  784. then progn(program!* := cdar result,
  785. result := list caar result);
  786. add2resultbuf(car result,mode);
  787. if null !*output then go to a
  788. else if (null !*rlisp2 and semic!* eq '!;)
  789. or (!*rlisp2 and tok!* eq '!*semic!*)
  790. then if mode eq 'symbolic
  791. then if null car result and null(!*mode eq 'symbolic)
  792. then nil
  793. else begin
  794. terpri();
  795. result := errorset(list('print,mkquote car result),
  796. t,!*backtrace)
  797. end
  798. else if car result
  799. then result := errorset(list('varpri,mkquote car result,
  800. mkquote program!*,
  801. mkquote 'only),
  802. t,!*backtrace);
  803. if errorp result then go to err3 else go to a;
  804. c: if crbuf1!* then
  805. progn(lprim "Closing object improperly removed. Redo edit.",
  806. crbuf1!* := nil, go to a)
  807. else if eof!*>4
  808. then progn(lprim "End-of-file read", return eval '(bye))
  809. else if terminalp() then progn(crbuf!* := nil, go to b)
  810. else return nil;
  811. d: if program!* then dfprint program!*;
  812. if null flagp!*!*(key!*,'eval) then go to a else go to b1;
  813. err1:
  814. if eofcheck() or eof!*>0 then go to c
  815. else if program!*="BEGIN invalid" then go to a;
  816. parserr := t;
  817. err2:
  818. resetparser(); %in case parser needs to be modified;
  819. err3:
  820. erfg!* := t;
  821. if null !*int and null !*errcont
  822. then progn(!*defn := t,
  823. !*echo := t,
  824. (if null cmsg!*
  825. then lprie "Continuing with parsing only ..."),
  826. cmsg!* := t)
  827. else if null !*errcont
  828. then progn(result := pause1 parserr,
  829. (if result then return null eval result),
  830. erfg!* := nil)
  831. else erfg!* := nil;
  832. go to a
  833. end;
  834. flag ('(deflist flag fluid global remflag remprop unfluid),'eval);
  835. symbolic procedure assgneval u;
  836. % Evaluate (possible) assignment statements and return results in a
  837. % form that allows required printing of such assignments.
  838. begin scalar x,y;
  839. a: if atom u then go to b
  840. else if car u eq 'setq then x := ('setq . cadr u) . x
  841. else if car u eq 'setel
  842. then x := ('setel . mkquote eval cadr u) . x
  843. else if car u eq 'setk
  844. then x := ('setk . mkquote if atom (y := eval cadr u) then y
  845. else car y . revlis cdr y) . x
  846. else go to b;
  847. u := caddr u;
  848. go to a;
  849. b: u := mkquote eval u;
  850. c: if null x then return(eval u . u);
  851. u := list(caar x,cdar x,u);
  852. x := cdr x;
  853. go to c
  854. end;
  855. symbolic procedure rassoc!*(u,v);
  856. % Finds term in which U is the first term in the right part of a term
  857. % in the association list V, or NIL if term is not found;
  858. if null v then nil
  859. else if u = cadar v then car v
  860. else rassoc!*(u,cdr v);
  861. symbolic procedure close!-input!-files;
  862. % Close all input files currently open;
  863. begin
  864. if ifl!* then progn(rds nil,ifl!* := nil);
  865. aa: if null ipl!* then return nil;
  866. close cdar ipl!*;
  867. ipl!* := cdr ipl!*;
  868. go to aa
  869. end;
  870. symbolic procedure close!-output!-files;
  871. % Close all output files currently open;
  872. begin
  873. if ofl!* then progn(wrs nil,ofl!* := nil);
  874. aa: if null opl!* then return nil;
  875. close cdar opl!*;
  876. opl!* := cdr opl!*;
  877. go to aa
  878. end;
  879. symbolic procedure add2buflis;
  880. begin
  881. if null crbuf!* then return nil;
  882. crbuf!* := reversip crbuf!*; %put in right order;
  883. a: if crbuf!* and seprp car crbuf!*
  884. then progn(crbuf!* := cdr crbuf!*, go to a);
  885. crbuflis!* := (statcounter . crbuf!*) . crbuflis!*;
  886. crbuf!* := nil
  887. end;
  888. symbolic procedure add2inputbuf(u,mode);
  889. begin
  890. if null terminalp() or !*nosave!* then return nil;
  891. inputbuflis!* := (statcounter . u . mode) . inputbuflis!*
  892. end;
  893. symbolic procedure add2resultbuf(u,mode);
  894. begin
  895. if mode eq 'symbolic or null u or !*nosave!* then return nil;
  896. ws := u;
  897. if terminalp()
  898. then resultbuflis!* := (statcounter . u) . resultbuflis!*
  899. end;
  900. symbolic procedure condterpri;
  901. !*output and !*echo and !*extraecho and (null !*int or ifl!*)
  902. and null !*defn and terpri();
  903. symbolic procedure eofcheck;
  904. % true if an end-of-file has been read in current input sequence;
  905. program!* eq !$eof!$ and ttype!*=3 and (eof!* := eof!*+1);
  906. symbolic procedure resetparser;
  907. %resets the parser after an error;
  908. if null !*slin then comm1 t;
  909. symbolic procedure terminalp;
  910. %true if input is coming from an interactive terminal;
  911. !*int and null ifl!*;
  912. symbolic procedure dfprint u;
  913. %Looks for special action on a form, otherwise prettyprints it;
  914. if dfprint!* then apply(dfprint!*,list u)
  915. else if cmsg!* then nil
  916. else if null eqcar(u,'progn) then prettyprint u
  917. else begin
  918. a: u := cdr u;
  919. if null u then return nil;
  920. dfprint car u;
  921. go to a
  922. end;
  923. symbolic procedure showtime;
  924. begin scalar x,y;
  925. x := otime!*;
  926. otime!* := time();
  927. x := otime!*-x;
  928. y := ogctime!*;
  929. ogctime!* := gctime();
  930. y := ogctime!* - y;
  931. x := x - y;
  932. terpri();
  933. prin2 "Time: "; prin2 x; prin2 " ms";
  934. if y = 0 then return terpri();
  935. prin2 " plus GC time: "; prin2 y; prin2 " ms"
  936. end;
  937. symbolic procedure sinitl u;
  938. set(u,get(u,'initl));
  939. endmodule;
  940. module tok; % Identifier and reserved character reading.
  941. % Author: Anthony C. Hearn.
  942. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  943. fluid '(semic!*);
  944. global '(!$eof!$
  945. !$eol!$
  946. !*quotenewnam
  947. !*raise
  948. !*lower
  949. crbuf!*
  950. crbuf1!*
  951. crchar!*
  952. curline!*
  953. cursym!*
  954. eof!*
  955. ifl!*
  956. nxtsym!*
  957. outl!*
  958. ttype!*);
  959. !*quotenewnam := t;
  960. crchar!* := '! ;
  961. curline!* := 1;
  962. % The function TOKEN defined below is used for reading identifiers
  963. % and reserved characters (such as parentheses and infix operators).
  964. % It is called by the function SCAN, which translates reserved
  965. % characters into their internal name, and sets up the output of the
  966. % input line. The following definitions of TOKEN and SCAN are quite
  967. % general, but also inefficient. The reading process can often be
  968. % speeded up considerably if these functions (especially token) are
  969. % written in terms of the explicit LISP used.
  970. symbolic procedure prin2x u;
  971. outl!* := u . outl!*;
  972. symbolic procedure mkstrng u;
  973. %converts the uninterned id U into a string;
  974. %if strings are not constants, this should be replaced by
  975. %list('string,u);
  976. u;
  977. symbolic procedure readch1;
  978. begin scalar x;
  979. if null terminalp()
  980. then progn(x := readch(),
  981. x eq !$eol!$ and (curline!* := curline!*+1),
  982. return x)
  983. else if crbuf1!*
  984. then begin x := car crbuf1!*; crbuf1!* := cdr crbuf1!* end
  985. else x := readch();
  986. crbuf!* := x . crbuf!*;
  987. return x
  988. end;
  989. symbolic procedure token1;
  990. begin scalar x,y,z;
  991. x := crchar!*;
  992. a: if seprp x then progn(x := readch1(), go to a)
  993. else if digit x then go to number
  994. else if liter x or x eq '!_ then go to letter
  995. else if x eq '!% then go to coment
  996. else if x eq '!! then go to escape
  997. else if x eq '!'
  998. then progn(crchar!* := readch1(),
  999. nxtsym!* := mkquote rread(),
  1000. ttype!* := 4,
  1001. return nxtsym!*)
  1002. else if x eq '!" then go to string;
  1003. ttype!* := 3;
  1004. if x eq !$eof!$ then prog2(crchar!* := '! ,filenderr());
  1005. nxtsym!* := x;
  1006. a1: if delcp x then crchar!*:= '! else crchar!*:= readch1();
  1007. go to c;
  1008. escape:
  1009. begin scalar raise, !*lower;
  1010. raise := !*raise;
  1011. !*raise := !*lower := nil;
  1012. y := x . y;
  1013. x := readch1();
  1014. !*raise := raise
  1015. end;
  1016. letter:
  1017. ttype!* := 0;
  1018. let1:
  1019. y := x . y;
  1020. if digit (x := readch1()) or liter x or x eq '!_ then go to let1
  1021. else if x eq '!! then go to escape;
  1022. nxtsym!* := intern compress reversip!* y;
  1023. b: crchar!* := x;
  1024. c: return nxtsym!*;
  1025. number:
  1026. ttype!* := 2;
  1027. num1:
  1028. y := x . y;
  1029. z := x;
  1030. if digit (x := readch1())
  1031. or x eq '!.
  1032. or x eq 'e
  1033. or z eq 'e
  1034. then go to num1;
  1035. nxtsym!* := compress reversip!* y;
  1036. go to b;
  1037. string:
  1038. begin scalar raise, !*lower;
  1039. raise := !*raise;
  1040. !*raise := !*lower := nil;
  1041. strinx:
  1042. y := x . y;
  1043. if null((x := readch1()) eq '!") then go to strinx;
  1044. y := x . y;
  1045. nxtsym!* := mkstrng compress reversip!* y;
  1046. !*raise := raise
  1047. end;
  1048. ttype!* := 1;
  1049. go to a1;
  1050. coment:
  1051. if null(readch1() eq !$eol!$) then go to coment;
  1052. x := readch1();
  1053. go to a
  1054. end;
  1055. symbolic procedure token;
  1056. %This provides a hook for a faster TOKEN;
  1057. token1();
  1058. symbolic procedure filenderr;
  1059. begin
  1060. eof!* := eof!*+1;
  1061. if terminalp() then error1()
  1062. else error(99,if ifl!*
  1063. then list("End-of-file read in file",car ifl!*)
  1064. else "End-of-file read")
  1065. end;
  1066. symbolic procedure ptoken;
  1067. begin scalar x;
  1068. x := token();
  1069. if x eq '!) and eqcar(outl!*,'! ) then outl!*:= cdr outl!*;
  1070. %an explicit reference to OUTL!* used here;
  1071. prin2x x;
  1072. if null ((x eq '!() or (x eq '!))) then prin2x '! ;
  1073. return x
  1074. end;
  1075. symbolic procedure rread1;
  1076. % Modified to use QUOTENEWNAM's for ids.
  1077. begin scalar x,y;
  1078. x := ptoken();
  1079. if null (ttype!*=3)
  1080. then return if null idp x
  1081. or null !*quotenewnam
  1082. or null(y := get(x,'quotenewnam))
  1083. then x
  1084. else y
  1085. else if x eq '!( then return rrdls()
  1086. else if null (x eq '!+ or x eq '!-) then return x;
  1087. y := ptoken();
  1088. if null numberp y
  1089. then progn(nxtsym!* := " ",
  1090. symerr("Syntax error: improper number",nil))
  1091. else if x eq '!- then y := apply('minus,list y);
  1092. %we need this construct for bootstrapping purposes;
  1093. return y
  1094. end;
  1095. symbolic procedure rrdls;
  1096. begin scalar x,y,z;
  1097. a: x := rread1();
  1098. if null (ttype!*=3) then go to b
  1099. else if x eq '!) then return z
  1100. else if null (x eq '!.) then go to b;
  1101. x := rread1();
  1102. y := ptoken();
  1103. if null (ttype!*=3) or null (y eq '!))
  1104. then progn(nxtsym!* := " ",symerr("Invalid S-expression",nil))
  1105. else return nconc(z,x);
  1106. b: z := nconc(z,list x);
  1107. go to a
  1108. end;
  1109. symbolic procedure rread;
  1110. progn(prin2x " '",rread1());
  1111. %-- symbolic procedure scan;
  1112. %-- begin scalar x,y;
  1113. %-- if null (cursym!* eq '!*semicol!*) then go to b;
  1114. %-- a: nxtsym!* := token();
  1115. %-- b: if null atom nxtsym!* then go to q1
  1116. %-- else if nxtsym!* eq 'else or cursym!* eq '!*semicol!*
  1117. %-- then outl!* := nil;
  1118. %-- prin2x nxtsym!*;
  1119. %-- c: if null idp nxtsym!* then go to l
  1120. %-- else if (x:=get(nxtsym!*,'newnam)) and
  1121. %-- (null (x=nxtsym!*)) then go to new
  1122. %-- else if nxtsym!* eq 'comment OR NXTSYM!* EQ '!% AND TTYPE!*=3
  1123. %-- THEN GO TO COMM
  1124. %-- ELSE IF NULL(TTYPE!* = 3) THEN GO TO L
  1125. %-- ELSE IF NXTSYM!* EQ !$eof!$ then return filenderr()
  1126. %-- else if nxtsym!* eq '!' then go to quote
  1127. %-- else if null (x:= get(nxtsym!*,'switch!*)) then go to l
  1128. %-- else if eqcar(cdr x,'!*semicol!*) then go to delim;
  1129. %-- sw1: nxtsym!* := token();
  1130. %-- if null(ttype!* = 3) then go to sw2
  1131. %-- else if nxtsym!* eq !$eof!$ then return filenderr()
  1132. %-- else if car x then go to sw3;
  1133. %-- sw2: cursym!*:=cadr x;
  1134. %-- if cursym!* eq '!*rpar!* then go to l2
  1135. %-- else return cursym!*;
  1136. %-- sw3: if null (y:= atsoc(nxtsym!*,car x)) then go to sw2;
  1137. %-- prin2x nxtsym!*;
  1138. %-- x := cdr y;
  1139. %-- go to sw1;
  1140. %-- comm: if delcp crchar!* then go to com1;
  1141. %-- crchar!* := readch();
  1142. %-- go to comm;
  1143. %-- com1: crchar!* := '! ;
  1144. %-- condterpri();
  1145. %-- go to a;
  1146. %-- delim:
  1147. %-- semic!*:=nxtsym!*;
  1148. %-- return (cursym!*:='!*semicol!*);
  1149. %-- new: nxtsym!* := x;
  1150. %-- if stringp x then go to l
  1151. %-- else if atom x then go to c
  1152. %-- else go to l;
  1153. %-- quote:
  1154. %-- nxtsym!* := mkquote rread1();
  1155. %-- go to l;
  1156. %-- q1: if null (car nxtsym!* eq 'string) then go to l;
  1157. %-- prin2x " ";
  1158. %-- prin2x cadr(nxtsym!* := mkquote cadr nxtsym!*);
  1159. %-- l: cursym!*:=nxtsym!*;
  1160. %-- l1: nxtsym!* := token();
  1161. %-- if nxtsym!* eq !$eof!$ and ttype!* = 3 then return filenderr();
  1162. %-- l2: if numberp nxtsym!*
  1163. %-- or (atom nxtsym!* and null get(nxtsym!*,'switch!*))
  1164. %-- then prin2x " ";
  1165. %-- return cursym!*
  1166. %-- end;
  1167. global '(!*eoldelimp comment!*);
  1168. symbolic procedure scan;
  1169. begin scalar bool,x,y;
  1170. if null (cursym!* eq '!*semicol!*) then go to b;
  1171. a: nxtsym!* := token();
  1172. b: if null atom nxtsym!* then go to q1
  1173. else if nxtsym!* eq 'else or cursym!* eq '!*semicol!*
  1174. then outl!* := nil;
  1175. prin2x nxtsym!*;
  1176. c: if null idp nxtsym!* then go to l
  1177. else if (x:=get(nxtsym!*,'newnam)) and
  1178. (null (x=nxtsym!*)) then go to new
  1179. else if nxtsym!* eq 'Comment then go to comm
  1180. else if nxtsym!* eq '!#if then go to conditional
  1181. else if nxtsym!* eq '!#else then progn(nxtsym!* := x := nil,
  1182. go to skipping)
  1183. else if nxtsym!* eq '!#endif then go to a
  1184. else if nxtsym!* eq '!% and ttype!*=3
  1185. % then progn(prin2t "****** Tell Hearn you got to SCAN comment",
  1186. % go to comm)
  1187. then go to comm
  1188. else if null(ttype!* = 3) then go to l
  1189. else if nxtsym!* eq !$eof!$ then return filenderr()
  1190. else if nxtsym!* eq '!' then rederr "Invalid QUOTE"
  1191. else if !*eoldelimp and nxtsym!* eq !$eol!$ then go to delim
  1192. else if null (x:= get(nxtsym!*,'switch!*)) then go to l
  1193. else if eqcar(cdr x,'!*semicol!*) then go to delim;
  1194. bool := seprp crchar!*;
  1195. sw1: nxtsym!* := token();
  1196. if null(ttype!* = 3) then go to sw2
  1197. else if nxtsym!* eq !$eof!$ then return filenderr()
  1198. else if car x then go to sw3;
  1199. sw2: cursym!*:=cadr x;
  1200. bool := nil;
  1201. if cursym!* eq '!*rpar!* then go to l2 else return cursym!*;
  1202. sw3: if bool or null (y:= atsoc(nxtsym!*,car x)) then go to sw2;
  1203. prin2x nxtsym!*;
  1204. x := cdr y;
  1205. if null car x and cadr x eq '!*Comment!*
  1206. then progn(comment!* := read!-comment(),go to a);
  1207. go to sw1;
  1208. conditional:
  1209. % The conditional expression used here must be written in Lisp form
  1210. x := errorset(rread(), !*backtrace, nil);
  1211. % errors in evaluation count as NIL
  1212. if null errorp x and car x then go to a;
  1213. x := nil;
  1214. skipping:
  1215. % I support nesting of conditional inclusion.
  1216. if nxtsym!* eq '!#endif then
  1217. if null x then go to a else x := cdr x
  1218. else if nxtsym!* eq '!#if then x := nil . x
  1219. else if (nxtsym!* eq '!#else) and null x then go to a;
  1220. nxtsym!* := token();
  1221. go to skipping;
  1222. comm: if delcp crchar!* and null(crchar!* eq !$eol!$)
  1223. then progn(crchar!* := '! , condterpri(), go to a);
  1224. crchar!* := readch();
  1225. go to comm;
  1226. delim:
  1227. semic!*:=nxtsym!*;
  1228. return (cursym!*:='!*semicol!*);
  1229. new: nxtsym!* := x;
  1230. if stringp x then go to l
  1231. else if atom x then go to c
  1232. else go to l;
  1233. q1: if null (car nxtsym!* eq 'string) then go to l;
  1234. prin2x " ";
  1235. prin2x cadr(nxtsym!* := mkquote cadr nxtsym!*);
  1236. l: cursym!*:=nxtsym!*;
  1237. nxtsym!* := token();
  1238. if nxtsym!* eq !$eof!$ and ttype!* = 3 then return filenderr();
  1239. l2: if numberp nxtsym!*
  1240. or (atom nxtsym!* and null get(nxtsym!*,'switch!*))
  1241. then prin2x " ";
  1242. return cursym!*
  1243. end;
  1244. endmodule;
  1245. module xread; % Routines for parsing REDUCE input.
  1246. % Author: Anthony C. Hearn.
  1247. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  1248. fluid '(!*blockp);
  1249. global '(cursym!* nxtsym!*);
  1250. % The conversion of a REDUCE expression to LISP prefix form is carried
  1251. % out by the function XREAD. This function initiates the scanning
  1252. % process, and then calls the auxiliary function XREAD1 to perform the
  1253. % actual parsing. Both XREAD and XREAD1 are used by many functions
  1254. % whenever an expression must be read;
  1255. flag ('(end !*colon!* !*semicol!*),'delim);
  1256. symbolic procedure chknewnam u;
  1257. % Check to see if U has a newnam, and return it else return U.
  1258. begin scalar x;
  1259. return if null(x := get(u,'newnam)) or x eq u then u
  1260. else if idp x then chknewnam x
  1261. else x
  1262. end;
  1263. symbolic procedure mkvar(u,v); u;
  1264. symbolic procedure remcomma u;
  1265. if eqcar(u,'!*comma!*) then cdr u else list u;
  1266. symbolic procedure xread1 u;
  1267. begin scalar v,w,x,y,z,z1,z2;
  1268. % v: expression being built
  1269. % w: prefix operator stack
  1270. % x: infix operator stack
  1271. % y: infix value or stat property
  1272. % z: current symbol
  1273. % z1: next symbol
  1274. % z2: temporary storage;
  1275. a: z := cursym!*;
  1276. a1: if null idp z then nil
  1277. else if z eq '!*lpar!* then go to lparen
  1278. else if z eq '!*rpar!* then go to rparen
  1279. else if y := get(z,'infix) then go to infx
  1280. % The next line now commented out was intended to allow a STAT
  1281. % to be used as a label. However, it prevents the definition of
  1282. % a diphthong whose first character is a colon.
  1283. % else if nxtsym!* eq '!: then nil
  1284. else if flagp(z,'delim) then go to delimit
  1285. else if y := get(z,'stat) then go to stat;
  1286. a2: y := nil;
  1287. a3: w := z . w;
  1288. if numberp z
  1289. and idp (z1 := chknewnam nxtsym!*)
  1290. and null flagp(z1,'delim)
  1291. and null(get(z1,'switch!*) and null(z1 eq '!())
  1292. and null get(z1,'infix)
  1293. then progn(cursym!* := 'times, go to a);
  1294. % allow for implicit * after a number.
  1295. next: z := scan();
  1296. go to a1;
  1297. lparen:
  1298. y := nil;
  1299. if scan() eq '!*rpar!* then go to lp1 % no args
  1300. else if flagpcar(w,'struct) then z := xread1 car w
  1301. else z := xread1 'paren;
  1302. if flagp(u,'struct) then progn(z := remcomma z, go to a3)
  1303. else if null eqcar(z,'!*comma!*) then go to a3
  1304. else if null w
  1305. then (if u eq 'lambda then go to a3
  1306. else symerr("Improper delimiter",nil))
  1307. else w := (car w . cdr z) . cdr w;
  1308. go to next;
  1309. lp1: if w then w := list car w . cdr w; %function of no args;
  1310. go to next;
  1311. rparen:
  1312. if null u or u eq 'group or u eq 'proc
  1313. then symerr("Too many right parentheses",nil)
  1314. else go to end1;
  1315. infx: if z eq '!*comma!* or null atom (z1 := scan())
  1316. or numberp z1 then go to in1
  1317. else if z1 eq '!*rpar!*%infix operator used as variable;
  1318. or z1 eq '!*comma!*
  1319. or flagp(z1,'delim)
  1320. then go to in2
  1321. else if z1 eq '!*lpar!*%infix operator in prefix position;
  1322. and null atom(z1 := xread 'paren)
  1323. and car z1 eq '!*comma!*
  1324. and (z := z . cdr z1)
  1325. then go to a1;
  1326. in1: if w then go to unwind
  1327. else if null(z := get(z,'unary))
  1328. then symerr("Redundant operator",nil);
  1329. v := '!*!*un!*!* . v;
  1330. go to pr1;
  1331. in2: y := nil;
  1332. w := z . w;
  1333. in3: z := z1;
  1334. go to a1;
  1335. unwind:
  1336. z2 := mkvar(car w,z);
  1337. un1: w:= cdr w;
  1338. if null w then go to un2
  1339. else if numberp car w then symerr("Missing operator",nil);
  1340. z2 := list(car w,z2);
  1341. go to un1;
  1342. un2: v:= z2 . v;
  1343. preced:
  1344. if null x then if y=0 then go to end2 else nil
  1345. else if y<caar x
  1346. or (y=caar x
  1347. and ((z eq cdar x and null flagp(z,'nary)
  1348. and null flagp(z,'right))
  1349. or get(cdar x,'alt)))
  1350. then go to pr2;
  1351. pr1: x:= (y . z) . x;
  1352. if null(z eq '!*comma!*) then go to in3
  1353. else if cdr x or null u or u memq '(lambda paren)
  1354. or flagp(u,'struct)
  1355. then go to next
  1356. else go to end2;
  1357. pr2: %if cdar x eq 'setq then go to assign else;
  1358. if cadr v eq '!*!*un!*!*
  1359. then (if car v eq '!*!*un!*!* then go to pr1
  1360. else z2 := list(cdar x,car v))
  1361. else z2 := cdar x .
  1362. if eqcar(car v,cdar x) and flagp(cdar x,'nary)
  1363. then (cadr v . cdar v)
  1364. else list(cadr v,car v);
  1365. x:= cdr x;
  1366. v := z2 . cddr v;
  1367. go to preced;
  1368. stat: if null(flagp(z,'go)
  1369. or null(u eq 'proc) and (flagp(y,'endstat)
  1370. or (null delcp nxtsym!* and null (nxtsym!* eq '!,))))
  1371. then go to a2;
  1372. w := apply(y,nil) . w;
  1373. y := nil;
  1374. go to a;
  1375. delimit:
  1376. if z eq '!*colon!* and null(u eq 'for)
  1377. and (null !*blockp or null w or null atom car w or cdr w)
  1378. or flagp(z,'nodel)
  1379. and (null u
  1380. or u eq 'group and null (z memq '(!*rsqb!* !*rcbkt!*)))
  1381. then symerr("Improper delimiter",nil)
  1382. else if idp u and (u eq 'paren or flagp(u,'struct))
  1383. then symerr("Too few right parentheses",nil);
  1384. end1: if y then symerr("Improper delimiter",nil)
  1385. else if null v and null w and null x then return nil;
  1386. y := 0;
  1387. go to unwind;
  1388. end2: if null cdr v then return car v
  1389. else symerr("Improper delimiter",nil)
  1390. end;
  1391. %symbolic procedure getels u;
  1392. % getel(car u . !*evlis cdr u);
  1393. %symbolic procedure !*evlis u;
  1394. % mapcar(u,function eval);
  1395. flag ('(endstat retstat),'endstat);
  1396. flag ('(else until),'nodel);
  1397. flag ('(begin),'go);
  1398. symbolic procedure xread u;
  1399. progn(scan(),xread1 u);
  1400. flag('(xread),'opfn); %to make it an operator;
  1401. endmodule;
  1402. module lpri; % Functions for printing diagnostic and error messages.
  1403. % Author: Anthony C. Hearn.
  1404. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  1405. fluid '(!*defn !*int);
  1406. global '(!*echo !*fort !*msg !*nat !*rlisp2 cursym!* erfg!* ofl!*
  1407. outl!*);
  1408. symbolic procedure lpri u;
  1409. begin
  1410. a: if null u then return nil;
  1411. prin2 car u;
  1412. prin2 " ";
  1413. u := cdr u;
  1414. go to a
  1415. end;
  1416. symbolic procedure lpriw (u,v);
  1417. begin scalar x;
  1418. u := u . if v and atom v then list v else v;
  1419. if ofl!* and (!*fort or not !*nat or !*defn) then go to c;
  1420. terpri();
  1421. a: lpri u;
  1422. terpri();
  1423. if null x then go to b;
  1424. wrs cdr x;
  1425. return nil;
  1426. b: if null ofl!* then return nil;
  1427. c: x := ofl!*;
  1428. wrs nil;
  1429. go to a
  1430. end;
  1431. symbolic procedure lprim u;
  1432. !*msg and lpriw("***",u);
  1433. symbolic procedure lprie u;
  1434. begin scalar x;
  1435. if !*int then go to a;
  1436. x:= !*defn;
  1437. !*defn := nil;
  1438. a: erfg!* := t;
  1439. lpriw ("*****",u);
  1440. if null !*int then !*defn := x
  1441. end;
  1442. symbolic procedure printty u;
  1443. begin scalar ofl;
  1444. if null !*fort and !*nat then print u;
  1445. if null ofl!* then return nil;
  1446. ofl := ofl!*;
  1447. wrs nil;
  1448. print u;
  1449. wrs cdr ofl
  1450. end;
  1451. symbolic procedure rederr u;
  1452. begin lprie u; error1() end;
  1453. symbolic procedure symerr(u,v);
  1454. begin scalar x;
  1455. erfg!* := t;
  1456. if numberp cursym!* or not(x := get(cursym!*,'prtch))
  1457. then x := cursym!*;
  1458. terpri();
  1459. if !*echo then terpri();
  1460. outl!*:=car outl!* . '!$!$!$ . cdr outl!*;
  1461. comm1 t;
  1462. mapcar(function prin2, reversip!* outl!*);
  1463. terpri();
  1464. outl!* := nil;
  1465. if null v then rederr u
  1466. else rederr(x . ("invalid" .
  1467. (if u then list("in",u,"statement") else nil)))
  1468. end;
  1469. symbolic procedure typerr(u,v); rederr list(u,"invalid as",v);
  1470. endmodule;
  1471. module parser; % Functions for parsing RLISP expressions.
  1472. % Author: Anthony C. Hearn.
  1473. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  1474. fluid '(!*backtrace !*mode);
  1475. global '(cursym!* letl!* nxtsym!*);
  1476. %With the exception of assignment statements, which are handled by
  1477. %XREAD, statements in REDUCE are introduced by a key-word, which
  1478. %initiates a reading process peculiar to that statement. The key-word
  1479. %is recognized (in XREAD1) by the indicator STAT on its property list.
  1480. %The corresponding property is the name of the function (of no
  1481. %arguments) which carries out the reading sequence.
  1482. % ***** COMMENTS *****
  1483. symbolic procedure comm1 u;
  1484. begin scalar bool;
  1485. if u eq 'end then go to b;
  1486. a: if cursym!* eq '!*semicol!*
  1487. or u eq 'end
  1488. and cursym!* memq
  1489. '(end else then until !*rpar!* !*rsqb!*)
  1490. then return nil
  1491. else if u eq 'end and null bool
  1492. then progn(lprim list("END-COMMENT NO LONGER SUPPORTED"),
  1493. bool := t);
  1494. b: scan();
  1495. go to a
  1496. end;
  1497. % ***** CONDITIONAL STATEMENT *****
  1498. symbolic procedure ifstat;
  1499. begin scalar condx,condit;
  1500. a: condx := xread t;
  1501. if not cursym!* eq 'then then symerr('if,t);
  1502. condit := aconc!*(condit,list(condx,xread t));
  1503. if not cursym!* eq 'else then nil
  1504. else if scan() eq 'if then go to a
  1505. else condit := aconc!*(condit,list(t,xread1 t));
  1506. return ('cond . condit)
  1507. end;
  1508. put('if,'stat,'ifstat);
  1509. flag ('(then else),'delim);
  1510. % ***** LAMBDA STATEMENT *****
  1511. symbolic procedure lamstat;
  1512. begin scalar x,y;
  1513. x:= xread 'lambda;
  1514. % x := flagtype(if null x then nil else remcomma x,'scalar);
  1515. if x then x := remcomma x;
  1516. y := list('lambda,x,xread t);
  1517. % remtype x;
  1518. return y
  1519. end;
  1520. put ('lambda,'stat,'lamstat);
  1521. % ***** GROUP STATEMENT *****
  1522. symbolic procedure mkprogn;
  1523. %Expects a list of statements terminated by a >>;
  1524. begin scalar lst;
  1525. a: lst := aconc!*(lst,xread 'group);
  1526. if null(cursym!* eq '!*rsqb!*) then go to a;
  1527. scan();
  1528. return ('progn . lst)
  1529. end;
  1530. put('!*lsqb!*,'stat,'mkprogn);
  1531. flag('(!*rsqb!*),'delim);
  1532. flag('(!*rsqb!*),'nodel);
  1533. % ***** END STATEMENT *****
  1534. symbolic procedure endstat;
  1535. %This procedure can also be used for any key-words which take no
  1536. %arguments;
  1537. begin scalar x; x := cursym!*; comm1 'end; return list x end;
  1538. put('end,'stat,'endstat);
  1539. put('endmodule,'stat,'endstat);
  1540. put('bye,'stat,'endstat);
  1541. put('quit,'stat,'endstat);
  1542. flag('(bye quit),'eval);
  1543. put('showtime,'stat,'endstat);
  1544. endmodule;
  1545. module block; % Block statement and related operators.
  1546. % Author: Anthony C. Hearn.
  1547. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  1548. fluid '(!*blockp);
  1549. global '(!*vars!* cursym!* nxtsym!*);
  1550. % ***** GO statement *****
  1551. symbolic procedure gostat;
  1552. begin scalar var;
  1553. var := if eq(scan(),'to) then scan() else cursym!*;
  1554. scan();
  1555. return list('go,var)
  1556. end;
  1557. put('go,'stat,'gostat);
  1558. put('goto,'newnam,'go);
  1559. % ***** Declaration Statement *****
  1560. symbolic procedure decl u;
  1561. begin scalar varlis,w;
  1562. a: if cursym!* eq '!*semicol!* then go to c
  1563. else if not flagp!*!*(cursym!*,'type) then return varlis
  1564. else if cursym!* eq 'dcl then go to dclr;
  1565. w := cursym!*;
  1566. if scan() eq 'procedure then return procstat1 w;
  1567. varlis := append(varlis,pairvars(remcomma xread1 nil,nil,w));
  1568. b: if not cursym!* eq '!*semicol!* then symerr(nil,t)
  1569. else if null u then return list('dcl,mkquote varlis);
  1570. %top level declaration;
  1571. c: scan();
  1572. go to a;
  1573. dclr: varlis := append(varlis,dclstat1());
  1574. go to b
  1575. end;
  1576. flag ('(dcl real integer scalar),'type);
  1577. symbolic procedure dclstat; list('dcl,mkquote dclstat1());
  1578. symbolic procedure dclstat1;
  1579. begin scalar x,y;
  1580. a: x := xread nil;
  1581. if not cursym!* eq '!*colon!* then symerr('dcl,t);
  1582. y := append(y,pairvars(remcomma x,nil,scan()));
  1583. if scan() eq '!*semicol!* then return y
  1584. else if not cursym!* eq '!*comma!* then symerr('dcl,t)
  1585. else go to a
  1586. end;
  1587. symbolic procedure dcl u;
  1588. %U is a list of (id, mode) pairs, which are declared as global vars;
  1589. begin scalar x;
  1590. !*vars!* := append(u,!*vars!*);
  1591. x := mapcar(function car, u);
  1592. global x;
  1593. flag(x,'share);
  1594. a: if null u then return nil;
  1595. set(caar u,get(cdar u,'initvalue));
  1596. u := cdr u;
  1597. go to a
  1598. end;
  1599. put('integer,'initvalue,0);
  1600. put('dcl,'stat,'dclstat);
  1601. symbolic procedure decstat;
  1602. %only called if a declaration occurs at the top level or not first
  1603. %in a block;
  1604. begin scalar x,y,z;
  1605. if !*blockp then symerr('block,t);
  1606. x := cursym!*;
  1607. y := nxtsym!*;
  1608. z := decl nil;
  1609. if y neq 'procedure then rederr list(x,"invalid outside block");
  1610. return z
  1611. end;
  1612. put('integer,'stat,'decstat);
  1613. put('real,'stat,'decstat);
  1614. put('scalar,'stat,'decstat);
  1615. % ***** Block Statement *****
  1616. symbolic procedure blockstat;
  1617. begin scalar hold,varlis,x,!*blockp;
  1618. !*blockp := t;
  1619. scan();
  1620. if cursym!* memq '(nil !*rpar!*) then rederr "BEGIN invalid";
  1621. varlis := decl t;
  1622. a: if cursym!* eq 'end and not nxtsym!* eq '!: then go to b;
  1623. x := xread1 nil;
  1624. if eqcar(x,'end) then go to c;
  1625. not cursym!* eq 'end and scan();
  1626. if x then hold := aconc!*(hold,x);
  1627. go to a;
  1628. b: comm1 'end;
  1629. c: return mkblock(varlis,hold)
  1630. end;
  1631. symbolic procedure mkblock(u,v); 'reduce!-block . (u . v);
  1632. putd('reduce!-block,'macro,
  1633. '(lambda (u) (cons 'prog
  1634. (cons (mapcar (function car) (cadr u)) (cddr u)))));
  1635. symbolic procedure formblock(u,vars,mode);
  1636. 'prog . append(initprogvars cadr u,
  1637. formprog1(cddr u,append(cadr u,vars),mode));
  1638. symbolic procedure initprogvars u;
  1639. begin scalar x,y,z;
  1640. a: if null u then return(reversip!* x . reversip!* y)
  1641. else if z := get(cdar u,'initvalue)
  1642. then y := mksetq(caar u,z) . y;
  1643. x := caar u . x;
  1644. u := cdr u;
  1645. go to a
  1646. end;
  1647. symbolic procedure formprog(u,vars,mode);
  1648. 'prog . cadr u . formprog1(cddr u,pairvars(cadr u,vars,mode),mode);
  1649. symbolic procedure formprog1(u,vars,mode);
  1650. if null u then nil
  1651. else if atom car u then car u . formprog1(cdr u,vars,mode)
  1652. else if idp caar u and flagp(caar u,'modefn)
  1653. then formc(cadar u,vars,caar u) . formprog1(cdr u,vars,mode)
  1654. else formc(car u,vars,mode) . formprog1(cdr u,vars,mode);
  1655. put('reduce!-block,'formfn,'formblock);
  1656. put('prog,'formfn,'formprog);
  1657. put('begin,'stat,'blockstat);
  1658. % ***** Return Statement *****
  1659. symbolic procedure retstat;
  1660. if not !*blockp then symerr(nil,t)
  1661. else list('return,
  1662. if flagp!*!*(scan(),'delim) then nil else xread1 t);
  1663. put('return,'stat,'retstat);
  1664. endmodule;
  1665. module form; % Performs a mode analysis of parsed forms.
  1666. % Author: Anthony C. Hearn.
  1667. % Modifications by: Jed Marti.
  1668. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  1669. fluid '(!*!*a2sfn !*cref !*defn !*mode current!-modulus);
  1670. global '(!*argnochk !*composites !*force !*micro!-version !*vars!*);
  1671. !*!*a2sfn := 'aeval;
  1672. flag('(algebraic symbolic),'modefn);
  1673. symbolic procedure formcond(u,vars,mode);
  1674. 'cond . formcond1(cdr u,vars,mode);
  1675. symbolic procedure formcond1(u,vars,mode);
  1676. if null u then nil
  1677. else list(formbool(caar u,vars,mode),form1(cadar u,vars,mode))
  1678. % FORMC here would add REVAL
  1679. . formcond1(cdr u,vars,mode);
  1680. put('cond,'formfn,'formcond);
  1681. symbolic procedure formlamb(u,vars,mode);
  1682. list('lambda,cadr u,form1(caddr u,pairvars(cadr u,vars,mode),mode));
  1683. put('lambda,'formfn,'formlamb);
  1684. symbolic procedure formprogn(u,vars,mode);
  1685. 'progn . formclis(cdr u,vars,mode);
  1686. put('progn,'formfn,'formprogn);
  1687. symbolic procedure expdrmacro u;
  1688. %returns the macro form for U if expansion is permitted;
  1689. begin scalar x;
  1690. if null(x := getrmacro u) or flagp(u,'noexpand) then return nil
  1691. else if null !*cref and (null !*defn or car x eq 'smacro)
  1692. or flagp(u,'expand) or !*force
  1693. then return x
  1694. else return nil
  1695. end;
  1696. symbolic procedure getrmacro u;
  1697. %returns a Reduce macro definition for U, if one exists,
  1698. %in GETD format;
  1699. begin scalar x;
  1700. return if not idp u then nil
  1701. else if (x := getd u) and car x eq 'macro then x
  1702. else if (x := get(u,'smacro)) then 'smacro . x
  1703. % else if (x := get(u,'nmacro)) then 'nmacro . x;
  1704. else nil
  1705. end;
  1706. symbolic procedure applmacro(u,v,w); apply1(u,w . v);
  1707. %symbolic procedure applnmacro(u,v,w);
  1708. % apply(u,if flagp(w,'nospread) then list v else v);
  1709. % symbolic procedure applsmacro(u,v,w);
  1710. % %We could use an atom sublis here, eg SUBLA;
  1711. % sublis(pair(cadr u,v),caddr u);
  1712. put('macro,'macrofn,'applmacro);
  1713. %put('nmacro,'macrofn,'applnmacro);
  1714. put('smacro,'macrofn,'applsmacro);
  1715. flag('(ed go quote),'noform);
  1716. symbolic procedure set!-global!-mode u;
  1717. begin !*mode := u end;
  1718. symbolic procedure form1(u,vars,mode);
  1719. begin scalar x,y;
  1720. if atom u
  1721. then return if not idp u then u
  1722. else if u eq 'ed then list u
  1723. else if flagp(u,'modefn) then set!-global!-mode u
  1724. else if x:= get(mode,'idfn)
  1725. then apply2(x,u,vars)
  1726. else u
  1727. else if not atom car u
  1728. then if caar u eq 'lambda then return formlis(u,vars,mode)
  1729. else typerr(car u,"operator")
  1730. else if not idp car u then typerr(car u,"operator")
  1731. else if get(car u, 'localfnname)
  1732. then return form1(get(car u,'localfnname) . cdr u,vars,mode)
  1733. else if flagp(car u,'noform) then return u
  1734. else if r!-arrayp car u
  1735. and (mode eq 'symbolic or intexprlisp(cdr u,vars))
  1736. then return list('getel,intargfn(u,vars,mode))
  1737. else if flagp(car u,'modefn)
  1738. then return convertmode(cadr u,vars,mode,car u)
  1739. else if (x := get(car u,'formfn))
  1740. then return macrochk(apply(x,list(u,vars,mode)),mode)
  1741. else if get(car u,'stat) eq 'rlis
  1742. then return macrochk(formrlis(u,vars,mode),mode)
  1743. % else if (x := getd car u) and eqcar(x, 'macro) and
  1744. % not(mode eq 'algebraic) then
  1745. % return << x := apply(cdr x, list(u, vars, mode));
  1746. % formc(x, vars, mode) >>
  1747. ;
  1748. argnochk u;
  1749. x := formlis(cdr u,vars,mode);
  1750. y := if x=cdr u then u else car u . x;
  1751. return if mode eq 'symbolic
  1752. or get(car u,'stat)
  1753. or cdr u and eqcar(cadr u,'quote)
  1754. and null !*micro!-version
  1755. or intexprnp(y,vars) and null !*composites
  1756. and null current!-modulus
  1757. then macrochk(y,mode)
  1758. else if not(mode eq 'algebraic)
  1759. then convertmode(y,vars,mode,'algebraic)
  1760. else ('list . algid(car u,vars) . x)
  1761. end;
  1762. symbolic procedure argnochk u;
  1763. begin scalar x;
  1764. if null !*argnochk then nil
  1765. else if (x := argsofopr car u) and x neq length cdr u
  1766. then rederr list(car u,"called with",
  1767. length cdr u,
  1768. if length cdr u=1 then "argument"
  1769. else "arguments",
  1770. "instead of",x)
  1771. end;
  1772. symbolic procedure argsofopr u;
  1773. % This function may be optimizable in various implementations.
  1774. get(u,'number!-of!-args);
  1775. symbolic procedure intexprnp(u,vars);
  1776. %determines if U is an integer expression;
  1777. if atom u then if numberp u then fixp u
  1778. else if (u := atsoc(u,vars)) then cdr u eq 'integer
  1779. else nil
  1780. else idp car u and flagp(car u,'intfn) and intexprlisp(cdr u,vars);
  1781. symbolic procedure intexprlisp(u,vars);
  1782. null u or intexprnp(car u,vars) and intexprlisp(cdr u,vars);
  1783. flag('(difference minus plus times),'intfn);
  1784. % EXPT is not included in this list, because a negative exponent can
  1785. % cause problems (i.e., result can be rational);
  1786. symbolic procedure formlis(u,vars,mode);
  1787. mapcar(function (lambda x; form1(x,vars,mode)), u);
  1788. symbolic procedure formclis(u,vars,mode);
  1789. mapcar(function (lambda x; formc(x,vars,mode)), u);
  1790. symbolic procedure form u; form1(u,!*vars!*,!*mode);
  1791. symbolic procedure macrochk(u,mode);
  1792. begin scalar y;
  1793. %expands U if CAR U is a macro and expansion allowed;
  1794. if atom u then return u
  1795. else if (y := expdrmacro car u)
  1796. and (mode eq 'symbolic or idp car u and flagp(car u,'opfn))
  1797. then return apply(get(car y,'macrofn),list(cdr y,cdr u,car u))
  1798. else return u
  1799. end;
  1800. put('symbolic,'idfn,'symbid);
  1801. symbolic procedure symbid(u,vars); u;
  1802. % if atsoc(u,vars) or fluidp u or globalp u or u memq '(nil t)
  1803. % or flagp(u,'share) then u
  1804. % else <<lprim list(u,"Non-Local Identifier");% u>>;
  1805. put('algebraic,'idfn,'algid);
  1806. symbolic procedure algid(u,vars);
  1807. if atsoc(u,vars) or flagp(u,'share) then u else mkquote u;
  1808. put('integer,'idfn,'intid);
  1809. symbolic procedure intid(u,vars);
  1810. begin scalar x,y;
  1811. return if (x := atsoc(u,vars))
  1812. then if cdr x eq 'integer then u
  1813. else if y := get(cdr x,'integer)
  1814. then apply2(y,u,vars)
  1815. else if cdr x eq 'scalar then !*!*a2i(u,vars)
  1816. else rederr list(cdr x,"not convertable to INTEGER")
  1817. else !*!*a2i(mkquote u,vars)
  1818. end;
  1819. symbolic procedure convertmode(exprn,vars,target,source);
  1820. convertmode1(form1(exprn,vars,source),vars,target,source);
  1821. symbolic procedure convertmode1(exprn,vars,target,source);
  1822. begin scalar x;
  1823. if source eq 'real then source := 'algebraic;
  1824. if target eq 'real then target := 'algebraic;
  1825. if target eq source then return exprn
  1826. else if idp exprn and (x := atsoc(exprn,vars))
  1827. and not(cdr x memq '(integer scalar real))
  1828. and not(cdr x eq source)
  1829. then return convertmode(exprn,vars,target,cdr x)
  1830. else if not (x := get(source,target))
  1831. then typerr(source,target)
  1832. else return apply2(x,exprn,vars)
  1833. end;
  1834. put('algebraic,'symbolic,'!*!*a2s);
  1835. put('symbolic,'algebraic,'!*!*s2a);
  1836. symbolic procedure !*!*a2s(u,vars);
  1837. % It would be nice if we could include the ATSOC(U,VARS) line,
  1838. % since in many cases that would save recomputation. However,
  1839. % in any sequential process, assignments or subsititution rules
  1840. % can change the value of a variable, so we have to check its
  1841. % value again. More comprehensive analysis could certainly
  1842. % optimize this.
  1843. if u = '(quote nil) then nil
  1844. else if null u or constantp u and null fixp u
  1845. or intexprnp(u,vars) and null !*composites
  1846. and null current!-modulus
  1847. or not atom u and idp car u
  1848. and flagp(car u,'nochange) and not(car u eq 'getel)
  1849. % or atsoc(u,vars) % means it was already evaluated
  1850. then u
  1851. else list(!*!*a2sfn,u);
  1852. symbolic procedure !*!*s2a(u,vars); u;
  1853. symbolic procedure formc(u,vars,mode);
  1854. %this needs to be generalized;
  1855. if mode eq 'algebraic and intexprnp(u,vars) then u
  1856. else convertmode(u,vars,'symbolic,mode);
  1857. symbolic procedure intargfn(u,vars,mode);
  1858. % transforms array element U into expression with integer arguments.
  1859. % Array name is treated as an algebraic variable;
  1860. 'list . form1(car u,vars,'algebraic) .
  1861. mapcar(function (lambda x;
  1862. convertmode(x,vars,'integer,mode)), cdr u);
  1863. put('algebraic,'integer,'!*!*a2i);
  1864. symbolic procedure !*!*a2i(u,vars);
  1865. if intexprnp(u,vars) then u else list('ieval,u);
  1866. symbolic procedure ieval u; !*s2i reval u;
  1867. flag('(ieval),'opfn); % To make it a symbolic operator.
  1868. flag('(ieval),'nochange);
  1869. put('symbolic,'integer,'!*!*s2i);
  1870. symbolic procedure !*!*s2i(u,vars);
  1871. if fixp u then u else list('!*s2i,u);
  1872. symbolic procedure !*s2i u;
  1873. if fixp u then u else typerr(u,"integer");
  1874. put('integer,'symbolic,'ridentity);
  1875. symbolic procedure ridentity(u,vars); u;
  1876. symbolic procedure formbool(u,vars,mode);
  1877. if mode eq 'symbolic then form1(u,vars,mode)
  1878. else if atom u then if not idp u or atsoc(u,vars) or u eq 't
  1879. then u
  1880. else formc!*(u,vars,mode)
  1881. else if intexprlisp(cdr u,vars) and get(car u,'boolfn) then u
  1882. else if idp car u and get(car u,'boolfn)
  1883. then get(car u,'boolfn) . formclis(cdr u,vars,mode)
  1884. else if idp car u and flagp(car u,'boolean)
  1885. then car u .
  1886. mapcar(function (lambda x;
  1887. if flagp(car u,'boolargs)
  1888. then formbool(x,vars,mode)
  1889. else formc!*(x,vars,mode)), cdr u)
  1890. else formc!*(u,vars,mode);
  1891. symbolic procedure formc!*(u,vars,mode);
  1892. begin scalar !*!*a2sfn;
  1893. !*!*a2sfn := 'reval;
  1894. return formc(u,vars,mode)
  1895. end;
  1896. % Functions with side effects must be handled carefully in this model,
  1897. % otherwise they are not always evaluated within blocks.
  1898. symbolic procedure formrederr(u,vars,mode);
  1899. begin scalar x;
  1900. x := formc!*(cadr u,vars,mode);
  1901. return list('rederr,x)
  1902. end;
  1903. put('rederr,'formfn,'formrederr);
  1904. symbolic procedure formreturn(u,vars,mode);
  1905. begin scalar x;
  1906. x := form1(cadr u,vars,mode); % FORMC here would add REVAL
  1907. if not(mode memq '(symbolic integer real))
  1908. and eqcar(x,'setq) % Should this be more general?
  1909. then x := list(!*!*a2sfn,x);
  1910. return list('return,x)
  1911. end;
  1912. put('return,'formfn,'formreturn);
  1913. symbolic procedure formsetq(u,vars,mode);
  1914. begin scalar target,x,y;
  1915. u := cdr u;
  1916. if eqcar(cadr u,'quote) then mode := 'symbolic;
  1917. if idp car u
  1918. and (y := atsoc(car u,vars)) and not(cdr y eq 'scalar)
  1919. then target := 'symbolic % used to be CDR Y
  1920. else target := 'symbolic;
  1921. % Make target always SYMBOLIC so that algebraic expressions
  1922. % are evaluated before being stored.
  1923. x := convertmode(cadr u,vars,target,mode);
  1924. return if not atom car u
  1925. then if not idp caar u then typerr(car u,"assignment")
  1926. else if r!-arrayp caar u
  1927. then list('setel,intargfn(car u,vars,mode),x)
  1928. else if y := get(caar u,'setqfn)
  1929. then form1((y . append(cdar u,cdr u)),vars,mode)
  1930. % else if y := get(caar u, 'access)
  1931. % then list('m!-setf,
  1932. % list(caar u, form1(cadar u, vars, mode)),
  1933. % x)
  1934. else list('setk,form1(car u,vars,'algebraic),x)
  1935. % algebraic needed above, since SETK expects it.
  1936. else if not idp car u then typerr(car u,"assignment")
  1937. else if mode eq 'symbolic or y or flagp(car u,'share)
  1938. or eqcar(x,'quote)
  1939. then mksetq(car u,x)
  1940. else list('setk,mkquote car u,x)
  1941. end;
  1942. put('car,'setqfn,'rplaca);
  1943. put('cdr,'setqfn,'rplacd);
  1944. put('setq,'formfn,'formsetq);
  1945. symbolic procedure formfunc(u,vars,mode);
  1946. if idp cadr u then if getrmacro cadr u
  1947. then rederr list("Macro",cadr u,"Used as Function")
  1948. else list('function,cadr u)
  1949. else list('function,form1(cadr u,vars,mode));
  1950. put('function,'formfn,'formfunc);
  1951. % RLIS is a parser function that reads a list of arguments and returns
  1952. % this list as one argument. It needs to be defined in this module for
  1953. % bootstrapping purposes since this definition only works with its form
  1954. % function.
  1955. symbolic procedure rlis;
  1956. begin scalar x;
  1957. x := cursym!*;
  1958. return if flagp!*!*(scan(),'delim) then list(x,nil)
  1959. else x . remcomma xread1 'lambda
  1960. end;
  1961. symbolic procedure flagop u; begin flag(u,'flagop); rlistat u end;
  1962. symbolic procedure rlistat u;
  1963. begin
  1964. a: if null u then return nil;
  1965. put(car u,'stat,'rlis);
  1966. u := cdr u;
  1967. go to a
  1968. end;
  1969. rlistat '(flagop);
  1970. symbolic procedure formrlis(u,vars,mode);
  1971. if not flagp(car u,'flagop)
  1972. then list(car u,'list . formlis(cdr u,vars,'algebraic))
  1973. else if not idlistp cdr u
  1974. then typerr('!*comma!* . cdr u,"identifier list")
  1975. else mkprog(nil,list('flag,mkquote cdr u,mkquote car u)
  1976. . get(car u,'simpfg));
  1977. symbolic procedure mkarg(u,vars);
  1978. % Returns the "unevaled" form of U.
  1979. if null u or constantp u then u
  1980. else if atom u then if atsoc(u,vars) then u else mkquote u
  1981. else if car u eq 'quote then mkquote u
  1982. else 'list . mapcar(function (lambda x; mkarg(x,vars)), u);
  1983. endmodule;
  1984. module proc; % Procedure statement.
  1985. % Author: Anthony C. Hearn.
  1986. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  1987. fluid '(!*backtrace);
  1988. global '(!*argnochk !*comp !*lose cursym!* erfg!* fname!* ftypes!*);
  1989. fluid '(!*defn);
  1990. !*lose := t;
  1991. ftypes!* := '(expr fexpr macro);
  1992. symbolic procedure putc(name,type,body);
  1993. %defines a non-standard function, such as an smacro. Returns NAME;
  1994. begin
  1995. if !*comp and flagp(type,'compile) then compd(name,type,body)
  1996. else put(name,type,body);
  1997. return name
  1998. end;
  1999. % flag('(putc),'eval);
  2000. symbolic procedure formproc(u,vars,mode);
  2001. begin scalar body,name,type,varlis,x,y;
  2002. u := cdr u;
  2003. name := car u;
  2004. if cadr u then mode := cadr u; % overwrite previous mode
  2005. u := cddr u;
  2006. type := car u;
  2007. if flagp(name,'lose) and (!*lose or null !*defn)
  2008. then return progn(lprim list(name,
  2009. "not defined (LOSE flag)"),
  2010. nil);
  2011. varlis := cadr u;
  2012. u := caddr u;
  2013. x := if eqcar(u,'reduce!-block) then cadr u else nil;
  2014. y := pairxvars(varlis,x,vars,mode);
  2015. if x then u := car u . rplaca!*(cdr u,cdr y);
  2016. body:= form1(u,car y,mode); % FORMC here would add REVAL
  2017. if type eq 'expr then body := list('de,name,varlis,body)
  2018. else if type eq 'fexpr then error(0, "FEXPR definition")
  2019. else if type eq 'macro then body := list('defmacro,name,'!&whole . varlis,body)
  2020. else if type eq 'emb then return embfn(name,varlis,body)
  2021. else body := list('put,
  2022. mkquote name,
  2023. mkquote type,
  2024. mkquote list('lambda,varlis,body));
  2025. if not(mode eq 'symbolic)
  2026. then body := list('progn,
  2027. list('flag,mkquote list name,mkquote 'opfn),
  2028. body);
  2029. if !*argnochk and type memq '(expr smacro)
  2030. then body := list('progn,
  2031. list('put,mkquote name,
  2032. mkquote 'number!-of!-args,
  2033. length varlis),
  2034. body);
  2035. if !*defn and type memq '(fexpr macro smacro) then eval body;
  2036. return body
  2037. end;
  2038. put('procedure,'formfn,'formproc);
  2039. symbolic procedure pairxvars(u,v,vars,mode);
  2040. %Pairs procedure variables and their modes, taking into account
  2041. %the convention which allows a top level prog to change the mode
  2042. %of such a variable;
  2043. begin scalar x,y;
  2044. a: if null u then return append(reversip!* x,vars) . v
  2045. else if (y := atsoc(car u,v))
  2046. then <<v := delete(y,v);
  2047. if not(cdr y eq 'scalar) then x := (car u . cdr y) . x
  2048. else x := (car u . mode) . x>>
  2049. else x := (car u . mode) . x;
  2050. u := cdr u;
  2051. go to a
  2052. end;
  2053. symbolic procedure procstat1 mode;
  2054. begin scalar bool,u,type,x,y,z;
  2055. bool := erfg!*;
  2056. if fname!* then go to b
  2057. else if cursym!* eq 'procedure then type := 'expr
  2058. else progn(type := cursym!*,scan());
  2059. if not cursym!* eq 'procedure then go to c;
  2060. x := errorset('(xread (quote proc)),nil,!*backtrace);
  2061. if errorp x then go to a
  2062. else if atom (x := car x) then x := list x; %no arguments;
  2063. fname!* := car x; %function name;
  2064. if idp fname!* %AND NOT(TYPE MEMQ FTYPES!*);
  2065. then if null fname!* or (z := gettype fname!*)
  2066. and not z memq '(procedure operator)
  2067. then go to d
  2068. else if not getd fname!* then flag(list fname!*,'fnc);
  2069. %to prevent invalid use of function name in body;
  2070. u := cdr x;
  2071. y := u;
  2072. x := car x . y;
  2073. a: z := errorset('(xread t),nil,!*backtrace);
  2074. if not errorp z then z := car z;
  2075. if null erfg!* then z:=list('procedure,car x,mode,type,y,z);
  2076. remflag(list fname!*,'fnc);
  2077. fname!*:=nil;
  2078. if erfg!* then progn(z := nil,if not bool then error1());
  2079. return z;
  2080. b: bool := t;
  2081. c: errorset('(symerr (quote procedure) t),nil,!*backtrace);
  2082. go to a;
  2083. d: typerr(list(z,fname!*),"procedure");
  2084. go to a
  2085. end;
  2086. symbolic procedure procstat; procstat1 nil;
  2087. deflist ('((procedure procstat) (expr procstat) (fexpr procstat)
  2088. (emb procstat) (macro procstat) (smacro procstat)),
  2089. 'stat);
  2090. % Next line refers to bootstrapping process.
  2091. if get('symbolic,'stat) eq 'procstat then remprop('symbolic,'stat);
  2092. deflist('((lisp symbolic)),'newnam);
  2093. endmodule;
  2094. module forstat; % Definition of REDUCE FOR loops.
  2095. % Author: Anthony C. Hearn.
  2096. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2097. fluid '(!*blockp);
  2098. global '(cursym!* foractions!*);
  2099. comment the syntax of the FOR statement is as follows:
  2100. {step i3 until}
  2101. {i := i1 { } i2 }
  2102. { { : } }
  2103. for { } <action> <expr>
  2104. { { in } }
  2105. { each i { } <list> }
  2106. { on }
  2107. In all cases, the <expr> is evaluated algebraically within the scope of
  2108. the current value of i. If <action> is DO, then nothing else happens.
  2109. In other cases, <action> is a binary operator that causes a result to be
  2110. built up and returned by FOR. In each case, the loop is initialized to
  2111. a default value. The test for the end condition is made before any
  2112. action is taken.
  2113. The effect of the definition here is to replace all for loops by
  2114. semantically equivalent blocks. As a result, none of the mapping
  2115. functions are needed in REDUCE.
  2116. To declare a set of actions, one says;
  2117. foractions!* := '(do collect conc product sum);
  2118. remflag(foractions!*,'delim); % For bootstrapping purposes.
  2119. % To associate a binary function with an action, one says:
  2120. deflist('((product times) (sum plus)),'bin);
  2121. % And to give these an initial value in a loop:
  2122. deflist('((product 1) (sum 0)),'initval);
  2123. % NB: We need to reset for and let delims if an error occurs. It's
  2124. % probably best to do this in the begin1 loop.
  2125. flag('(for),'nochange);
  2126. symbolic procedure forstat;
  2127. begin scalar !*blockp;
  2128. return if scan() eq 'all then forallstat()
  2129. else if cursym!* eq 'each then foreachstat()
  2130. else forloop()
  2131. end;
  2132. put('for,'stat,'forstat);
  2133. symbolic procedure forloop;
  2134. begin scalar action,bool,incr,var,x;
  2135. flag('(step),'delim);
  2136. x := errorset('(xread1 'for),t,t);
  2137. remflag('(step),'delim);
  2138. if errorp x then error1() else x := car x;
  2139. if not eqcar(x,'setq) or not idp(var := cadr x)
  2140. then symerr('for,t);
  2141. x := caddr x;
  2142. if cursym!* eq 'step
  2143. then <<flag('(until),'delim);
  2144. incr := xread t;
  2145. remflag('(until),'delim);
  2146. if not cursym!* eq 'until then symerr('for,t)>>
  2147. else if cursym!* eq '!*colon!* then incr := 1
  2148. else symerr('for,t);
  2149. if flagp(car foractions!*,'delim) then bool := t % nested loop
  2150. else flag(foractions!*,'delim);
  2151. incr := list(x,incr,xread t);
  2152. if null bool then remflag(foractions!*,'delim);
  2153. if not((action := cursym!*) memq foractions!*)
  2154. then symerr('for,t);
  2155. return list('for,var,incr,action,xread t)
  2156. end;
  2157. symbolic procedure formfor(u,vars,mode);
  2158. begin scalar action,algp,body,endval,incr,initval,var,x;
  2159. %ALGP is used to determine if the loop calculation must be
  2160. %done algebraically or not;
  2161. var := cadr u;
  2162. incr := caddr u;
  2163. incr := list(formc(car incr,vars,mode),
  2164. formc(cadr incr,vars,mode),
  2165. formc(caddr incr,vars,mode));
  2166. if intexprnp(car incr,vars) and intexprnp(cadr incr,vars)
  2167. and not atsoc(var,vars)
  2168. then vars := (var . 'integer) . vars;
  2169. action := cadddr u;
  2170. body :=
  2171. formc(car cddddr u,
  2172. (var .
  2173. if intexprlisp(caddr u,vars) then 'integer else mode)
  2174. . vars,mode);
  2175. algp := algmodep car incr or algmodep cadr incr
  2176. or algmodep caddr incr;
  2177. initval := car incr;
  2178. endval := caddr incr;
  2179. incr := cadr incr;
  2180. x := if algp then list('list,''difference,endval,var)
  2181. else list('difference,endval,var);
  2182. if incr neq 1
  2183. then x := if algp then list('list,''times,incr,x)
  2184. else list('times,incr,x);
  2185. % We could consider simplifying X here (via reval).
  2186. x := if algp then list('aminusp!:,x) else list('minusp,x);
  2187. return forformat(action,body,initval,x,
  2188. list('plus2,incr),var,vars,mode)
  2189. end;
  2190. put('for,'formfn,'formfor);
  2191. symbolic procedure algmodep u; eqcar(u,'aeval);
  2192. symbolic procedure aminusp!: u;
  2193. begin scalar x;
  2194. u := aeval u;
  2195. x := u;
  2196. if fixp x then return minusp x
  2197. else if not eqcar(x,'!*sq)
  2198. then msgpri(nil,reval u,"invalid in FOR statement",nil,t);
  2199. x := cadr x;
  2200. if fixp car x and fixp cdr x then return minusp car x
  2201. else if not cdr x = 1
  2202. or not (atom(x := car x) or atom car x)
  2203. % Should be DOMAINP, but SMACROs not yet defined.
  2204. then msgpri(nil,reval u,"invalid in FOR statement",nil,t)
  2205. else return apply('!:minusp,list x)
  2206. end;
  2207. symbolic procedure foreachstat;
  2208. begin scalar w,x,y,z;
  2209. if not idp(x := scan()) or not (y := scan()) memq '(in on)
  2210. then symerr("FOR EACH",t)
  2211. else if flagp(car foractions!*,'delim) then w := t
  2212. else flag(foractions!*,'delim);
  2213. z := xread t;
  2214. if null w then remflag(foractions!*,'delim);
  2215. w := cursym!*;
  2216. if not w memq foractions!* then symerr("FOR EACH",t);
  2217. return list('foreach,x,y,z,w,xread t)
  2218. end;
  2219. put('foreach,'stat,'foreachstat);
  2220. symbolic procedure formforeach(u,vars,mode);
  2221. begin scalar action,body,lst,mod,var;
  2222. var := cadr u; u := cddr u;
  2223. mod := car u; u := cdr u;
  2224. lst := formc(car u,vars,mode); u := cdr u;
  2225. if not(mode eq 'symbolic) then lst := list('getrlist,lst);
  2226. action := car u; u := cdr u;
  2227. body := formc(car u,(var . mode) . vars,mode);
  2228. if mod eq 'in
  2229. then body := list(list('lambda,list var,body),list('car,var))
  2230. else if not(mode eq 'symbolic) then typerr(mod,'action);
  2231. return forformat(action,body,lst,
  2232. list('null,var),list 'cdr,var,vars,mode)
  2233. end;
  2234. put('foreach,'formfn,'formforeach);
  2235. symbolic procedure forformat(action,body,initval,
  2236. testexp,updform,var,vars,mode);
  2237. begin scalar result;
  2238. result := gensym();
  2239. return
  2240. sublis(list('body2 .
  2241. if mode eq 'symbolic or intexprnp(body,vars)
  2242. then list(get(action,'bin),body,result)
  2243. else list('aeval,list('list,mkquote get(action,'bin),
  2244. body,result)),
  2245. 'body3 .
  2246. if mode eq 'symbolic then body
  2247. else list('getrlist,body),
  2248. 'body . body,
  2249. 'initval . initval,
  2250. 'nillist . if mode eq 'symbolic then nil else ''(list),
  2251. 'result . result,
  2252. 'initresult . get(action,'initval),
  2253. 'resultlist . if mode eq 'symbolic then result
  2254. else list('cons,''list,result),
  2255. 'testexp . testexp,
  2256. 'updfn . car updform,
  2257. 'updval . cdr updform,
  2258. 'var . var),
  2259. if action eq 'do
  2260. then '(prog (var)
  2261. (setq var initval)
  2262. lab (cond (testexp (return nil)))
  2263. body
  2264. (setq var (updfn var . updval))
  2265. (go lab))
  2266. else if action eq 'collect
  2267. then '(prog (var result endptr)
  2268. (setq var initval)
  2269. (cond (testexp (return nillist)))
  2270. (setq result (setq endptr (cons body nil)))
  2271. looplabel
  2272. (setq var (updfn var . updval))
  2273. (cond (testexp (return resultlist)))
  2274. (rplacd endptr (cons body nil))
  2275. (setq endptr (cdr endptr))
  2276. (go looplabel))
  2277. else if action eq 'conc
  2278. then '(prog (var result endptr)
  2279. (setq var initval)
  2280. startover
  2281. (cond (testexp (return nillist)))
  2282. (setq result body)
  2283. (setq endptr (lastpair resultlist))
  2284. (setq var (updfn var . updval))
  2285. (cond ((atom endptr) (go startover)))
  2286. looplabel
  2287. (cond (testexp (return result)))
  2288. (rplacd endptr body3)
  2289. (setq endptr (lastpair endptr))
  2290. (setq var (updfn var . updval))
  2291. (go looplabel))
  2292. else '(prog (var result)
  2293. (setq var initval)
  2294. (setq result initresult)
  2295. lab1
  2296. (cond (testexp (return result)))
  2297. (setq result body2)
  2298. (setq var (updfn var . updval))
  2299. (go lab1)))
  2300. end;
  2301. symbolic procedure lastpair u;
  2302. % Return the last pair of the list u.
  2303. if atom u or atom cdr u then u else lastpair cdr u;
  2304. put('join,'newnam,'conc); % alternative for CONC
  2305. endmodule;
  2306. module loops; % Looping forms other than the FOR statement.
  2307. % Author: Anthony C. Hearn
  2308. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2309. fluid '(!*blockp);
  2310. global '(cursym!*);
  2311. % ***** REPEAT STATEMENT *****
  2312. symbolic procedure repeatstat;
  2313. begin scalar body,!*blockp;
  2314. flag('(until),'delim);
  2315. body:= xread t;
  2316. remflag('(until),'delim);
  2317. if not cursym!* eq 'until then symerr('repeat,t);
  2318. return list('repeat,body,xread t);
  2319. end;
  2320. symbolic macro procedure repeat u;
  2321. begin scalar body,bool,lab;
  2322. body := cadr u; bool := caddr u;
  2323. lab := gensym();
  2324. return mkprog(nil,list(lab,body,
  2325. list('cond,list(list('not,bool),list('go,lab)))))
  2326. end;
  2327. put('repeat,'stat,'repeatstat);
  2328. flag('(repeat),'nochange);
  2329. symbolic procedure formrepeat(u,vars,mode);
  2330. list('repeat,formc(cadr u,vars,mode),formbool(caddr u,vars,mode));
  2331. put('repeat,'formfn,'formrepeat);
  2332. % ***** WHILE STATEMENT *****
  2333. symbolic procedure whilstat;
  2334. begin scalar bool,!*blockp;
  2335. flag('(do),'delim);
  2336. bool := xread t;
  2337. remflag('(do),'delim);
  2338. if not cursym!* eq 'do then symerr('while,t);
  2339. return list('while,bool,xread t)
  2340. end;
  2341. symbolic macro procedure while u;
  2342. begin scalar body,bool,lab;
  2343. bool := cadr u; body := caddr u;
  2344. lab := gensym();
  2345. return mkprog(nil,list(lab,list('cond,list(list('not,bool),
  2346. list('return,nil))),body,list('go,lab)))
  2347. end;
  2348. put('while,'stat,'whilstat);
  2349. flag('(while),'nochange);
  2350. symbolic procedure formwhile(u,vars,mode);
  2351. list('while,formbool(cadr u,vars,mode),formc(caddr u,vars,mode));
  2352. put('while,'formfn,'formwhile);
  2353. endmodule;
  2354. module write; % Miscellaneous statement definitions.
  2355. % Author: Anthony C. Hearn.
  2356. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2357. % ***** DEFINE STATEMENT *****
  2358. remprop('define,'stat);
  2359. symbolic procedure define u;
  2360. for each x in u do
  2361. if not eqcar(x,'equal) or not idp cadr x
  2362. then typerr(x,"DEFINE declaration")
  2363. else put(cadr x,'newnam,caddr x);
  2364. put('define,'stat,'rlis);
  2365. flag('(define),'eval);
  2366. % ***** WRITE STATEMENT *****
  2367. symbolic procedure formwrite(u,vars,mode);
  2368. begin scalar bool1,bool2,x,z;
  2369. u := cdr u;
  2370. bool1 := mode eq 'symbolic;
  2371. while u do
  2372. <<x := formc(car u,vars,mode);
  2373. z := (if bool1 then list('prin2,x)
  2374. else list('writepri,mkarg1(x,vars),
  2375. if not cdr u then if not bool2 then ''only else ''last
  2376. else if not bool2 then ''first else nil)) .
  2377. z;
  2378. bool2 := t;
  2379. u := cdr u>>;
  2380. return mkprog(nil,reversip!* z)
  2381. end;
  2382. symbolic procedure writepri(u,v);
  2383. begin scalar x; x := assgneval u; return varpri(car x,cdr x,v) end;
  2384. symbolic procedure mkarg1(u,vars);
  2385. % Returns the "unevaled" form of U for the WRITE command.
  2386. if null u or constantp u then u
  2387. else if atom u then if atsoc(u,vars)
  2388. then list('mkquote,u) else mkquote u
  2389. else if car u eq 'quote then mkquote u
  2390. else if car u eq 'setq
  2391. then list('list,''setq,mkquote cadr u,mkarg1(caddr u,vars))
  2392. else 'list . for each x in u collect mkarg1(x,vars);
  2393. put('write,'stat,'rlis);
  2394. put('write,'formfn,'formwrite);
  2395. endmodule;
  2396. module smacro; % Support for SMACRO expansion.
  2397. % Author: Anthony C. Hearn.
  2398. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2399. symbolic procedure applsmacro(u,vals,name);
  2400. % U is smacro body of form (lambda <varlist> <body>), VALS is
  2401. % argument list, NAME is name of smacro.
  2402. begin scalar body,remvars,varlist,w;
  2403. varlist := cadr u;
  2404. body := caddr u;
  2405. if length varlist neq length vals
  2406. then rederr list("Argument mismatch for SMACRO",name);
  2407. if no!-side!-effect!-listp vals or one!-entry!-listp(varlist,body)
  2408. then return subla!-q(pair(varlist,vals),body)
  2409. else if length varlist>1
  2410. then <<w := for each x in varlist collect (x . gensym());
  2411. body := subla!-q(w,body);
  2412. varlist := for each x in w collect cdr x>>;
  2413. for each x in vals do
  2414. <<if no!-side!-effectp x or one!-entryp(car varlist,body)
  2415. then body := subla!-q(list(car varlist . x),body)
  2416. else remvars := aconc(remvars,car varlist . x);
  2417. varlist := cdr varlist>>;
  2418. if null remvars then return body
  2419. else <<w := list('lambda,
  2420. for each x in remvars collect car x,
  2421. body) .
  2422. for each x in remvars collect cdr x;
  2423. % IF NOT EQCAR(CADR W,'SETQ)
  2424. % THEN <<PRIN2 "*** SMACRO: "; PRINT CDR W>>;
  2425. return w>>
  2426. end;
  2427. symbolic procedure no!-side!-effectp u;
  2428. if atom u then numberp u or idp u and not(fluidp u or globalp u)
  2429. else if car u eq 'quote then t
  2430. else if flagp!*!*(car u,'nosideeffects)
  2431. then no!-side!-effect!-listp u
  2432. else nil;
  2433. symbolic procedure no!-side!-effect!-listp u;
  2434. null u or no!-side!-effectp car u and no!-side!-effect!-listp cdr u;
  2435. flag('(car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr
  2436. cddar cdddr cons),'nosideeffects);
  2437. symbolic procedure one!-entryp(u,v);
  2438. % determines if id U occurs less than twice in V.
  2439. if atom v then t
  2440. else if smemq(u,car v)
  2441. then if smemq(u,cdr v) then nil else one!-entryp(u,car v)
  2442. else one!-entryp(u,cdr v);
  2443. symbolic procedure one!-entry!-listp(u,v);
  2444. null u or one!-entryp(car u,v) and one!-entry!-listp(cdr u,v);
  2445. symbolic procedure subla!-q(u,v);
  2446. begin scalar x;
  2447. if null u or null v then return v
  2448. else if atom v
  2449. then return if x:= atsoc(v,u) then cdr x else v
  2450. else if car v eq 'quote then return v
  2451. else return(subla!-q(u,car v) . subla!-q(u,cdr v))
  2452. end;
  2453. endmodule;
  2454. module infix; % Functions for introducing new infix operators.
  2455. % Author: Anthony C. Hearn.
  2456. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2457. fluid '(!*mode);
  2458. global '(preclis!*);
  2459. symbolic procedure infix x;
  2460. begin scalar y;
  2461. a: if null x then go to b;
  2462. y := car x;
  2463. if !*mode eq 'algebraic then mkop y;
  2464. if not(y member preclis!*) then preclis!* := y . preclis!*;
  2465. x := cdr x;
  2466. go to a;
  2467. b: mkprec()
  2468. end;
  2469. symbolic procedure precedence u;
  2470. begin scalar x,y,z;
  2471. preclis!* := delete(car u,preclis!*);
  2472. y := cadr u;
  2473. x := preclis!*;
  2474. a: if null x then rederr list (y,"not found")
  2475. else if y eq car x
  2476. then <<preclis!* :=
  2477. nconc!*(reversip!* z,car x . (car u . cdr x));
  2478. mkprec();
  2479. return nil>>;
  2480. z := car x . z;
  2481. x := cdr x;
  2482. go to a
  2483. end;
  2484. deflist('((infix rlis) (precedence rlis)),'stat);
  2485. flag('(infix precedence),'eval);
  2486. endmodule;
  2487. module where; % Support for a where construct.
  2488. % Author: Anthony C. Hearn.
  2489. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2490. symbolic procedure formwhere(u,vars,mode);
  2491. begin scalar expn,equivs,y,z;
  2492. expn := cadr u;
  2493. equivs := caddr u;
  2494. if eqcar(equivs,'!*comma!*) then equivs := cdr equivs
  2495. else equivs := list equivs;
  2496. for each x in equivs do
  2497. if not atom x and car x memq '(equal setq)
  2498. then <<y := caddr x . y; z := cadr x . z>>
  2499. else rederr list(x,"invalid in WHERE statement");
  2500. return formc(list('lambda,reversip z,expn) . reversip y,
  2501. vars,mode)
  2502. end;
  2503. put('where,'formfn,'formwhere);
  2504. % infix where; % We do this explicitly to avoid changing preclis*.
  2505. deflist('((where 1)),'infix);
  2506. put('where,'op,'((1 1)));
  2507. endmodule;
  2508. module list; % Define a list as a list of expressions in curly brackets.
  2509. % Author: Anthony C. Hearn.
  2510. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2511. fluid '(orig!* posn!*);
  2512. global '(cursym!*);
  2513. % Add to system table.
  2514. put('list,'tag,'list);
  2515. put('list,'rtypefn,'(lambda (x) 'list));
  2516. % Parsing interface.
  2517. symbolic procedure xreadlist;
  2518. % expects a list of expressions enclosed by {, }.
  2519. % also allows expressions separated by ; --- treats these as progn.
  2520. begin scalar cursym,delim,lst;
  2521. if scan() eq '!*rcbkt!* then <<scan(); return list 'list>>;
  2522. a: lst := aconc(lst,xread1 'group);
  2523. cursym := cursym!*;
  2524. scan();
  2525. if cursym eq '!*rcbkt!*
  2526. then return if delim eq '!*semicol!* then 'progn . lst
  2527. else 'list . lst
  2528. else if null delim then delim := cursym
  2529. else if not(delim eq cursym)
  2530. then symerr("syntax error: mixed , and ; in list",nil);
  2531. go to a
  2532. end;
  2533. put('!*lcbkt!*,'stat,'xreadlist);
  2534. newtok '((!{) !*lcbkt!*);
  2535. newtok '((!}) !*rcbkt!*);
  2536. flag('(!*rcbkt!*),'delim);
  2537. flag('(!*rcbkt!*),'nodel);
  2538. % Evaluation interface.
  2539. put('list,'evfn,'listeval);
  2540. symbolic procedure getrlist u;
  2541. if eqcar(u,'list) then cdr u
  2542. else typerr(if eqcar(u,'!*sq) then prepsq cadr u else u,"list");
  2543. symbolic procedure listeval(u,v);
  2544. if atom u then listeval(get(u,'rvalue),v)
  2545. else car u . for each j in cdr u collect reval1(j,v);
  2546. % Length interface.
  2547. put('list,'lengthfn,'(lambda (x) (length (cdr x))));
  2548. % Printing interface.
  2549. put('list,'prifn,'listpri);
  2550. symbolic procedure listpri l;
  2551. % This definition is basically that of INPRINT, except that it
  2552. % decides when to split at the comma by looking at the size of
  2553. % the argument.
  2554. begin scalar orig,split,u;
  2555. u := l;
  2556. l := cdr l;
  2557. prin2!* get('!*lcbkt!*,'prtch);
  2558. % Do it this way so table can change.
  2559. orig := orig!*;
  2560. orig!* := if posn!*<18 then posn!* else orig!*+3;
  2561. if null l then go to b;
  2562. split := treesizep(l,40); % 40 is arbitrary choice.
  2563. a: maprint(negnumberchk car l,0);
  2564. l := cdr l;
  2565. if null l then go to b;
  2566. oprin '!*comma!*;
  2567. if split then terpri!* t;
  2568. go to a;
  2569. b: prin2!* get('!*rcbkt!*,'prtch);
  2570. % terpri!* nil;
  2571. orig!* := orig;
  2572. return u
  2573. end;
  2574. symbolic procedure treesizep(u,n);
  2575. % true if u has recursively more pairs than n.
  2576. treesizep1(u,n)=0;
  2577. symbolic procedure treesizep1(u,n);
  2578. if atom u then n-1
  2579. else if (n := treesizep1(car u,n))>0 then treesizep1(cdr u,n)
  2580. else 0;
  2581. % Definitions of operations on lists
  2582. symbolic procedure rfirst u;
  2583. <<argnochk ('first . u);
  2584. if null(getrtype(u := reval car u) eq 'list)
  2585. then typerr(u,"list")
  2586. else if null cdr u then parterr(u,1)
  2587. else cadr u>>;
  2588. put('first,'psopfn,'rfirst);
  2589. symbolic procedure parterr(u,v);
  2590. msgpri("Expression",u,"does not have part",v,t);
  2591. symbolic procedure rsecond u;
  2592. <<argnochk ('second . u);
  2593. if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
  2594. else if null cdr u or null cddr u then parterr(u,2)
  2595. else caddr u>>;
  2596. put('second,'psopfn,'rsecond);
  2597. symbolic procedure rthird u;
  2598. <<argnochk ('third . u);
  2599. if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
  2600. else if null cdr u or null cddr u or null cdddr u
  2601. then parterr(u,3)
  2602. else cadddr u>>;
  2603. put('third,'psopfn,'rthird);
  2604. symbolic procedure rrest u;
  2605. <<argnochk ('rest . u);
  2606. if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
  2607. else if null cdr u then typerr(u,"non-empty list")
  2608. else 'list . cddr u>>;
  2609. put('rest,'psopfn,'rrest);
  2610. symbolic procedure rappend u;
  2611. begin scalar x,y;
  2612. argnochk ('append . u);
  2613. if null(getrtype(x := reval car u) eq 'list)
  2614. then typerr(x,"list")
  2615. else if null(getrtype(y := reval cadr u) eq 'list)
  2616. then typerr(y,"list")
  2617. else return 'list .append(cdr x,cdr y)
  2618. end;
  2619. put('append,'psopfn,'rappend);
  2620. symbolic procedure rcons u;
  2621. begin scalar x,y;
  2622. argnochk ('cons . u);
  2623. if (y := getrtype(x := reval cadr u)) eq 'vector
  2624. then return prepsq simpdot u
  2625. else if not(y eq 'list) then typerr(x,"list")
  2626. else return 'list . reval car u . cdr x
  2627. end;
  2628. put('cons,'psopfn,'rcons);
  2629. symbolic procedure rreverse u;
  2630. <<argnochk ('reverse . u);
  2631. if null(getrtype(u := reval car u) eq 'list) then typerr(u,"list")
  2632. else 'list . reverse cdr u>>;
  2633. put('reverse,'psopfn,'rreverse);
  2634. endmodule;
  2635. module array; % Array statement.
  2636. % Author: Anthony C. Hearn.
  2637. % Modifications by: Nancy Kirkwood.
  2638. % These definitions are very careful about bounds checking. Appropriate
  2639. % optimizations in a given system might really speed things up.
  2640. global '(erfg!*);
  2641. symbolic procedure getel u;
  2642. % Returns the value of the array element U.
  2643. getel1(get(car u,'rvalue),cdr u,get(car u,'dimension));
  2644. symbolic procedure getel1(u,v,dims);
  2645. if length v neq length dims
  2646. then rederr "Incorrect array reference"
  2647. else if null v then u
  2648. else if car v geq car dims then rederr "Array out of bounds"
  2649. else getel1(getv(u,car v),cdr v,cdr dims);
  2650. symbolic procedure setel(u,v);
  2651. % Sets array element U to V and returns V.
  2652. setel1(get(car u,'rvalue),cdr u,v,get(car u,'dimension));
  2653. symbolic procedure setel1(u,v,w,dims);
  2654. if length v neq length dims then rederr "Incorrect array reference"
  2655. else if car v geq car dims then rederr "Array out of bounds"
  2656. else if null cdr v then putv(u,car v,w)
  2657. else setel1(getv(u,car v),cdr v,w,cdr dims);
  2658. symbolic procedure dimension u; get(u,'dimension);
  2659. comment further support for REDUCE arrays;
  2660. symbolic procedure typechk(u,v);
  2661. begin scalar x;
  2662. if (x := gettype u) eq v or x eq 'parameter
  2663. then lprim list(v,u,"redefined")
  2664. else if x then typerr(list(x,u),v)
  2665. end;
  2666. symbolic procedure arrayfn(u,v);
  2667. % U is the defining mode, V a list of lists, assumed syntactically
  2668. % correct. ARRAYFN declares each element as an array unless a
  2669. % semantic mismatch occurs.
  2670. begin scalar y;
  2671. for each x in v do
  2672. <<typechk(car x,'array);
  2673. y := add1lis for each z in cdr x collect eval z;
  2674. if null erfg!*
  2675. then <<put(car x,'rtype,'array);
  2676. put(car x,'rvalue,mkarray(y,u));
  2677. put(car x,'dimension,y)>>>>
  2678. end;
  2679. symbolic procedure add1lis u;
  2680. if null u then nil else (car u+1) . add1lis cdr u;
  2681. symbolic procedure mkarray(u,v);
  2682. %U is a list of positive integers representing array bounds, V
  2683. %the defining mode. Value is an array structure;
  2684. if null u then if v eq 'symbolic then nil else 0
  2685. else begin integer n; scalar x;
  2686. n := car u-1;
  2687. x := mkvect n;
  2688. for i:=0:n do putv(x,i,mkarray(cdr u,v));
  2689. return x
  2690. end;
  2691. rlistat '(array);
  2692. flag ('(array arrayfn),'eval);
  2693. symbolic procedure formarray(u,vars,mode);
  2694. begin scalar x;
  2695. x := cdr u;
  2696. while x do <<if atom x then typerr(x,"Array List")
  2697. else if atom car x or not idp caar x
  2698. or not listp cdar x
  2699. then typerr(car x,"Array declaration");
  2700. x := cdr x>>;
  2701. u := for each z in cdr u collect intargfn(z,vars,mode);
  2702. %ARRAY arguments must be returned as quoted structures;
  2703. return list('arrayfn,mkquote mode,'list . u)
  2704. end;
  2705. symbolic procedure listp u;
  2706. % Returns T if U is a top level list.
  2707. null u or not atom u and listp cdr u;
  2708. put('array,'formfn,'formarray);
  2709. put('array,'rtypefn,'arraychk);
  2710. symbolic procedure arraychk u;
  2711. % If arraychk receives NIL, it means that array name is being used
  2712. % as an identifier. We no longer permit this.
  2713. if null u then 'array else nil;
  2714. % nil;
  2715. put('array,'evfn,'arrayeval);
  2716. symbolic procedure arrayeval(u,v);
  2717. % Eventually we'll support this.
  2718. rederr "Array arithmetic not defined";
  2719. put('array,'lengthfn,'arraylength);
  2720. symbolic procedure arraylength u; 'list . get(u,'dimension);
  2721. endmodule;
  2722. module switch; % Support for switches and ON and OFF statements.
  2723. % Author: Anthony C. Hearn.
  2724. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2725. global '(!*switchcheck switchlist!*);
  2726. % No references to RPLAC-based functions in this module.
  2727. symbolic procedure on u; onoff(u,t);
  2728. symbolic procedure off u; onoff(u,nil);
  2729. symbolic procedure onoff(u,bool);
  2730. for each j in u do
  2731. begin scalar x,y;
  2732. if not idp j then typerr(j,"switch")
  2733. else if not flagp(j,'switch)
  2734. then if !*switchcheck
  2735. then rederr list(j,"not defined as switch")
  2736. else lpriw("*****",list(j,"not defined as switch"));
  2737. x := intern compress append(explode '!*,explode j);
  2738. if !*switchcheck and eval x eq bool then return nil
  2739. else if y := atsoc(bool,get(j,'simpfg))
  2740. then eval mkprog(nil,cdr y);
  2741. set(x,bool)
  2742. end;
  2743. symbolic procedure switch u;
  2744. % Declare list u as switches.
  2745. for each x in u do
  2746. begin scalar y;
  2747. if not idp x then typerr(x,"switch");
  2748. if not u memq switchlist!*
  2749. then switchlist!* := x . switchlist!*;
  2750. flag(list x,'switch);
  2751. y := intern compress append(explode '!*,explode x);
  2752. if not fluidp y and not globalp y then fluid list y
  2753. end;
  2754. deflist('((switch rlis)),'stat); % we use deflist since it's flagged
  2755. % eval
  2756. rlistat '(off on);
  2757. flag ('(off on),'ignore);
  2758. % Symbolic mode switches:
  2759. switch backtrace,comp,defn,demo,echo,errcont,int,msg,output,pret,
  2760. quotenewnam,raise,time; % switchcheck.
  2761. % The following are compiler switches that may not be supported in all
  2762. % versions:
  2763. switch pgwd,plap,pwrds;
  2764. % flag('(switch),'eval);
  2765. endmodule;
  2766. module io; % Reduce functions for handling input and output of files.
  2767. % Author: Anthony C. Hearn.
  2768. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2769. fluid '(!*backtrace !*int semic!*);
  2770. global '(!*echo contl!* curline!* ifl!* ipl!* linelist!* ofl!* opl!*
  2771. techo!*);
  2772. symbolic procedure file!-transform(u,v);
  2773. % Performs a transformation on the file u. V is name of function
  2774. % used for the transformation;
  2775. begin scalar echo,ichan,oldichan,val;
  2776. echo := !*echo;
  2777. !*echo := nil;
  2778. ichan := open(u,'input);
  2779. oldichan := rds ichan;
  2780. val := errorset(list v,t,!*backtrace);
  2781. !*echo := echo;
  2782. close ichan;
  2783. rds oldichan;
  2784. if not errorp val then return car val
  2785. end;
  2786. symbolic procedure infile u;
  2787. % loads the single file u into REDUCE without echoing;
  2788. begin scalar !*int;
  2789. return file!-transform(u,function begin1)
  2790. end;
  2791. symbolic procedure in u;
  2792. begin scalar chan,echo,echop,type;
  2793. echop := semic!* eq '!;; %record echo character from input;
  2794. echo := !*echo; %save current echo status;
  2795. if null ifl!* then techo!* := !*echo; %terminal echo status;
  2796. for each fl in u do
  2797. <<if fl eq 't then fl := nil;
  2798. if null fl then <<!*echo := techo!*; rds nil; ifl!* := nil>>
  2799. else <<chan := open(fl := mkfil fl,'input);
  2800. rds chan;
  2801. % if assoc(fl,linelist!*) then nil;
  2802. curline!* := 1;
  2803. ifl!* := list(fl,chan,1)>>;
  2804. ipl!* := ifl!* . ipl!*; %add to input file stack;
  2805. !*echo := echop;
  2806. type := filetype fl;
  2807. if type and (type := get(type,'action)) then eval list type
  2808. else begin1();
  2809. if chan then close chan;
  2810. if fl eq caar ipl!* then ipl!* := cdr ipl!*
  2811. else errach list("FILE STACK CONFUSION",fl,ipl!*)>>;
  2812. !*echo := echo; %restore echo status;
  2813. if ipl!* and null contl!* then ifl!* := car ipl!*
  2814. else ifl!* := nil;
  2815. if ifl!* then <<rds cadr ifl!*; curline!* := caddr ifl!*>>
  2816. else rds nil
  2817. end;
  2818. symbolic procedure out u;
  2819. %U is a list of one file;
  2820. begin integer n; scalar chan,fl,x;
  2821. n := linelength nil;
  2822. if null u then return nil
  2823. else if car u eq 't then return <<wrs(ofl!* := nil); nil>>;
  2824. fl := mkfil car u;
  2825. if not (x := assoc(fl,opl!*))
  2826. then <<chan := open(fl,'output);
  2827. if chan
  2828. then <<ofl!*:= fl . chan; opl!*:= ofl!* . opl!*>>>>
  2829. else ofl!* := x;
  2830. wrs cdr ofl!*;
  2831. linelength n
  2832. end;
  2833. symbolic procedure shut u;
  2834. %U is a list of names of files to be shut;
  2835. begin scalar fl1;
  2836. for each fl in u do
  2837. <<if fl1 := assoc((fl := mkfil fl),opl!*)
  2838. then <<opl!* := delete(fl1,opl!*);
  2839. if fl1=ofl!* then <<ofl!* := nil; wrs nil>>;
  2840. close cdr fl1>>
  2841. else if not (fl1 := assoc(fl,ipl!*))
  2842. then rederr list(fl,"not open")
  2843. else if fl1 neq ifl!*
  2844. then <<close cadr fl1; ipl!* := delete(fl1,ipl!*)>>
  2845. else rederr list("Cannot shut current input file",car fl1)>>
  2846. end;
  2847. deflist ('((in rlis) (out rlis) (shut rlis)),'stat);
  2848. flag ('(in out shut),'eval);
  2849. flag ('(in out shut),'ignore);
  2850. endmodule;
  2851. module inter; % Functions for interactive support.
  2852. % Author: Anthony C. Hearn.
  2853. % Copyright (c) 1987 The RAND Corporation. All rights reserved.
  2854. fluid '(!*int);
  2855. global '(!$eof!$
  2856. !*echo
  2857. !*lessspace
  2858. cloc!*
  2859. contl!*
  2860. curline!*
  2861. edit!*
  2862. eof!*
  2863. erfg!*
  2864. flg!*
  2865. ifl!*
  2866. ipl!*
  2867. key!*
  2868. ofl!*
  2869. opl!*
  2870. techo!*);
  2871. symbolic procedure pause;
  2872. %Must appear at the top-most level;
  2873. if null !*int then nil
  2874. else if key!* eq 'pause then pause1 nil
  2875. else %typerr('pause,"lower level command");
  2876. pause1 nil; %Allow at lower level for now;
  2877. symbolic procedure pause1 bool;
  2878. begin
  2879. if bool then
  2880. if getd 'edit1 and erfg!* and cloc!* and yesp "Edit?"
  2881. then return <<contl!* := nil;
  2882. if ofl!* then <<lprim list(car ofl!*,'shut);
  2883. close cdr ofl!*;
  2884. opl!* := delete(ofl!*,opl!*);
  2885. ofl!* := nil>>;
  2886. edit1(cloc!*,nil)>>
  2887. else if flg!* then return (edit!* := nil);
  2888. if null ifl!* or yesp "Cont?" then return nil;
  2889. ifl!* := list(car ifl!*,cadr ifl!*,curline!*);
  2890. contl!* := ifl!* . !*echo . contl!*;
  2891. rds (ifl!* := nil);
  2892. !*echo := techo!*
  2893. end;
  2894. symbolic procedure yesp u;
  2895. begin scalar bool,ifl,ofl,x,y,z;
  2896. if ifl!*
  2897. then <<ifl := ifl!* := list(car ifl!*,cadr ifl!*,curline!*);
  2898. rds nil>>;
  2899. if ofl!* then <<ofl:= ofl!*; wrs nil>>;
  2900. if null !*lessspace then terpri();
  2901. if atom u then prin2 u else lpri u;
  2902. prin2t " (Y or N)";
  2903. if null !*lessspace then terpri();
  2904. z := setpchar '!?;
  2905. a: x := read();
  2906. % Assume an end-of-file is the same as "yes".
  2907. if (y := x eq 'y or x eq !$eof!$) or x eq 'n then go to b;
  2908. if null bool then prin2t "TYPE Y OR N";
  2909. bool := t;
  2910. go to a;
  2911. b: setpchar z;
  2912. if ofl then wrs cdr ofl;
  2913. if ifl then rds cadr ifl;
  2914. cursym!* := '!*semicol!*;
  2915. return y
  2916. end;
  2917. symbolic procedure cont;
  2918. begin scalar fl,techo;
  2919. if ifl!* then return nil %CONT only active from terminal;
  2920. else if null contl!* then rederr "No file open";
  2921. fl := car contl!*;
  2922. techo := cadr contl!*;
  2923. contl!* := cddr contl!*;
  2924. if car fl=caar ipl!* and cadr fl=cadar ipl!*
  2925. then <<ifl!* := fl;
  2926. if fl then <<rds cadr fl; curline!* := caddr fl>>
  2927. else rds nil;
  2928. !*echo := techo>>
  2929. else <<eof!* := 1; lprim list(fl,"not open"); error1()>>
  2930. end;
  2931. deflist ('((cont endstat) (pause endstat) (retry endstat)),'stat);
  2932. flag ('(cont),'ignore);
  2933. endmodule;
  2934. comment Codemist Standard Lisp based REDUCE "back-end";
  2935. % this file defines the system dependent code necessary to run REDUCE
  2936. % under Codemist Standard Lisp
  2937. comment the following functions, which are referenced in the basic
  2938. REDUCE source (rlisp, alg1, alg2, matr and phys) should be defined to
  2939. complete the definition of REDUCE:
  2940. bye
  2941. delcp
  2942. error1
  2943. filetype
  2944. mkfil
  2945. orderp
  2946. quit
  2947. random
  2948. seprp
  2949. setpchar.
  2950. prototypical descriptions of these functions are as follows;
  2951. remprop('bye,'stat);
  2952. symbolic procedure bye;
  2953. % returns control to the computer's operating system command level.
  2954. % the current REDUCE job cannot be restarted;
  2955. stop 0;
  2956. deflist('((bye endstat)),'stat);
  2957. remprop('quit,'stat);
  2958. symbolic procedure quit;
  2959. % returns control to the computer's operating system command level.
  2960. % the current REDUCE job cannot be restarted;
  2961. stop 0;
  2962. deflist('((quit endstat)),'stat);
  2963. symbolic procedure delcp u;
  2964. if u = '!; or u = '!$ then t else nil;
  2965. symbolic procedure filetype u; nil;
  2966. symbolic procedure mkfil u;
  2967. % converts file descriptor u into valid system filename;
  2968. if idp u then u
  2969. else if stringp u then string2file u
  2970. else if eqcar(u,'quote) then mkfil cadr u
  2971. else if atom u then nil
  2972. else for each z in u collect mkfil z;
  2973. symbolic procedure string2file s;
  2974. % converts a string into a valid file name.
  2975. s;
  2976. comment the following functions are only referenced if various flags are
  2977. set, or the functions are actually defined. they are defined in another
  2978. module, which is not needed to build the basic system. the name of the
  2979. flag follows the function name, enclosed in parentheses:
  2980. bfquotient!: (bigfloat)
  2981. cedit (?)
  2982. compd (comp)
  2983. edit1 this function provides a link to an editor. however, a
  2984. definition is not necessary, since REDUCE checks to see
  2985. if it has a function value.
  2986. embfn (?)
  2987. ezgcdf (ezgcd)
  2988. factorf (factor)
  2989. load!-module (?)
  2990. prettyprint (defn --- also called by dfprint)
  2991. this function is used in particular for output of RLISP
  2992. expressions in Lisp syntax. if that feature is needed,
  2993. and the prettyprint module is not available, then it
  2994. should be defined as print
  2995. rprint (pret)
  2996. texpt!: (bigfloat)
  2997. texpt!:any (bigfloat)
  2998. time (time) returns elapsed time from some arbitrary initial
  2999. point in milliseconds;
  3000. comment we also need to define a function begin, which acts as the top-
  3001. level call to REDUCE, and sets the appropriate variables. the following is
  3002. a minimum definition;
  3003. fluid '(!*int !*mode);
  3004. global '(crchar!* date!* ifl!* ipl!* ofl!* !*extraecho !*echo);
  3005. remflag('(begin),'go);
  3006. symbolic procedure begin;
  3007. begin
  3008. !*int := not batchp();
  3009. !*echo := not !*int;
  3010. !*extraecho := t;
  3011. ifl!* := ipl!* := ofl!* := nil;
  3012. if null date!* then go to a;
  3013. % verbos nil; % leave verbos flag as it had been
  3014. linelength 79;
  3015. prin2 date!*;
  3016. prin2t " ...";
  3017. !*mode := if getd 'addsq then 'algebraic else 'symbolic;
  3018. initreduce(); % resets date!*;
  3019. erfg!* := !*defn := cmsg!* := nil; % reset error status;
  3020. a: crchar!* := '! ;
  3021. if errorp errorset('(begin1),t,!*backtrace) then go to a;
  3022. if not yesp "are you sure you want to leave REDUCE & enter Lisp?" then
  3023. go to a;
  3024. prin2t "entering Lisp ... ";
  3025. prin2t "type (begin) to re-enter REDUCE, (stop) to exit from Lisp";
  3026. end;
  3027. flag('(begin),'go);
  3028. comment initial setups for REDUCE;
  3029. global '(spare!* statcounter);
  3030. spare!* := 10;
  3031. symbolic procedure initreduce; %. initial declarations for REDUCE
  3032. <<statcounter := 0;
  3033. date!* := compress('!" . append(explode2 "Bootstrap RLISP, ",
  3034. append(explode2 date(),list '!")));
  3035. crbuf!* := crbuflis!* := inputbuflis!* := resultbuflis!* := nil>>;
  3036. flag('(reclaim rdf),'opfn);
  3037. % flag('(explode2 printc princ ttab unglobal random next!-random!-number), 'lose);
  3038. % redo showtime for Codemist Lisp interpretation of gctime vs. time
  3039. % remflag('(showtime), 'lose);
  3040. symbolic procedure showtime;
  3041. begin scalar x,y;
  3042. x := otime!*;
  3043. otime!* := time();
  3044. x := otime!*-x;
  3045. y := ogctime!*;
  3046. ogctime!* := gctime();
  3047. y := ogctime!* - y;
  3048. % x := x - y; % not for Codemist Lisp
  3049. terpri();
  3050. prin2 "Time: "; prin2 x; prin2 " ms";
  3051. if y = 0 then return terpri();
  3052. prin2 " plus GC time: "; prin2 y; prin2 " ms"
  3053. end;
  3054. % flag('(lengthc), 'lose);
  3055. symbolic procedure typerr(u, v);
  3056. % redefined when I get to alg1.red, but smashes before then
  3057. % need some support, I guess
  3058. << terpri();
  3059. princ "+++++ typerr: ";
  3060. prin u;
  3061. princ " ";
  3062. prin v;
  3063. terpri();
  3064. error "typerr called"
  3065. >>;
  3066. symbolic procedure flush();
  3067. begin
  3068. scalar c;
  3069. while (c:=readch()) neq !$eol!$ and c neq !$eof!$ do nil;
  3070. return nil
  3071. end;
  3072. % flag('(cedit1), 'lose);
  3073. % redefinition of yesp to improve prompt generation
  3074. % remflag('(yesp), 'lose);
  3075. symbolic procedure yesp u;
  3076. begin scalar bool,ifl,ofl,x,y,z;
  3077. if ifl!*
  3078. then <<ifl := ifl!* := list(car ifl!*,cadr ifl!*,curline!*);
  3079. rds nil>>;
  3080. if ofl!* then <<ofl:= ofl!*; wrs nil>>;
  3081. if null !*lessspace then terpri();
  3082. if atom u then prin2 u else lpri u;
  3083. prin2t " (y or n)";
  3084. if null !*lessspace then terpri();
  3085. z := setpchar '!?;
  3086. a: prin2 '!?;
  3087. x := read();
  3088. % assume an end-of-file is the same as "yes".
  3089. if (y := x eq 'y or x eq !$eof!$) or x eq 'n then go to b;
  3090. if null bool then prin2t "type y or n";
  3091. bool := t;
  3092. go to a;
  3093. b: setpchar z;
  3094. if ofl then wrs cdr ofl;
  3095. if ifl then rds cadr ifl;
  3096. cursym!* := '!*semicol!*;
  3097. return y
  3098. end;
  3099. % support for some more floating point stuff
  3100. global '(ft!-tolerance!* e!-value!* pi!-value!*);
  3101. ft!-tolerance!* := float 1 / float 1000000000000;
  3102. e!-value!* := exp 1.0;
  3103. pi!-value!* := 4.0 * atan 1.0;
  3104. comment now set the system name;
  3105. global '(systemname!*);
  3106. systemname!* := 'ccl;
  3107. initreduce();
  3108. preserve 'begin;