lispfile.lsp 113 KB

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