pas2.red 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % PASCAL BASED MINI-LISP
  4. %
  5. % File: PAS2.RED - Basic LISP Functions
  6. % ChangeDate: 10:42pm Wednesday, 15 July 1981
  7. % By: M. L. Griss
  8. % Change to add Features for Schlumberger Demo
  9. %
  10. % All RIGHTS RESERVED
  11. % COPYRIGHT (C) - 1981 - M. L. GRISS
  12. % Computer Science Department
  13. % University of Utah
  14. %
  15. % Do Not distribute with out written consent of M. L. Griss
  16. %
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. SYMBOLIC PROCEDURE PAIRP X;
  19. IF PAIRP X THEN T ELSE NIL;
  20. SMACRO PROCEDURE NOTNULL(X); %For readability.
  21. X;
  22. SYMBOLIC PROCEDURE NOT X;
  23. X EQ NIL;
  24. SYMBOLIC PROCEDURE CODEP X;
  25. IF CODEP X THEN T ELSE NIL;
  26. SYMBOLIC PROCEDURE CONSTANTP X;
  27. NULL (PAIRP X OR IDP X);
  28. SYMBOLIC PROCEDURE EQN(A,B);
  29. A EQ B;
  30. %. List entries (+ CONS, NCONS, XCONS)
  31. SYMBOLIC PROCEDURE LIST2(R1,R2);
  32. R1 . NCONS R2;
  33. SYMBOLIC PROCEDURE LIST3(R1,R2,R3);
  34. R1 . LIST2(R2,R3);
  35. SYMBOLIC PROCEDURE LIST4(R1,R2,R3,R4);
  36. R1 . LIST3(R2,R3,R4);
  37. SYMBOLIC PROCEDURE LIST5(R1,R2,R3,R4,R5);
  38. R1 . LIST4(R2,R3,R4,R5);
  39. SYMBOLIC PROCEDURE REVERSE U;
  40. REV U;
  41. SYMBOLIC PROCEDURE APPEND(U,V);
  42. BEGIN U:=REVERSE U;
  43. WHILE PAIRP U DO <<V :=CAR U . V; U:=CDR U>>;
  44. RETURN V
  45. END;
  46. %. procedures to support GET and PUT, FLAG, etc.
  47. SYMBOLIC PROCEDURE MEMBER(A,B);
  48. IF NULL B THEN A ELSE IF A EQ CAR B THEN B ELSE A MEMBER CDR B;
  49. SYMBOLIC PROCEDURE PAIR(U,V);
  50. IF U AND V THEN (CAR U . CAR V) . PAIR(CDR U,CDR V)
  51. ELSE IF U OR V THEN ERROR(0,'PAIR)
  52. ELSE NIL;
  53. SYMBOLIC PROCEDURE SASSOC(U,V,FN);
  54. IF NOT PAIRP V THEN APPLY(FN,'(NIL))
  55. ELSE IF U EQ CAAR V THEN CAR V
  56. ELSE SASSOC(U,CDR V,FN);
  57. SYMBOLIC PROCEDURE SUBLIS(X,Y);
  58. IF NOT PAIRP X THEN Y
  59. ELSE BEGIN SCALAR U;
  60. U := ASSOC(Y,X);
  61. RETURN IF U THEN CDR U
  62. ELSE IF ATOM Y THEN Y
  63. ELSE SUBLIS(X,CAR Y) . SUBLIS(X,CDR Y)
  64. END;
  65. SYMBOLIC PROCEDURE SUBST(U,V,W);
  66. IF NULL V THEN NIL
  67. ELSE IF V EQ W THEN U
  68. ELSE IF ATOM W THEN W
  69. ELSE SUBST(U,V,CAR W) . SUBST(U,V,CDR W);
  70. SYMBOLIC PROCEDURE MEMQ(U,V);
  71. IF NOT PAIRP V THEN V
  72. ELSE IF U EQ CAR V THEN V ELSE MEMQ(U,CDR V);
  73. SYMBOLIC PROCEDURE ATSOC(U,V);
  74. IF NOT PAIRP V THEN V
  75. ELSE IF (NOT PAIRP CAR V)
  76. OR NOT(U EQ CAAR V) THEN ATSOC(U,CDR V)
  77. ELSE CAR V;
  78. SYMBOLIC PROCEDURE ASSOC(U,V);
  79. IF NOT PAIRP V THEN NIL
  80. ELSE IF ATOM CAR V THEN ERROR(100,LIST(V,'ASSOC))
  81. ELSE IF U EQ CAAR V THEN CAR V
  82. ELSE ASSOC(U,CDR V);
  83. SYMBOLIC PROCEDURE DEFLIST(U,IND);
  84. IF NOT PAIRP U THEN NIL
  85. ELSE (<<PUT(CAAR U,IND,CADAR U); CAAR U>>) . DEFLIST(CDR U,IND);
  86. SYMBOLIC PROCEDURE DELETE(U,V);
  87. IF NOT PAIRP V THEN NIL
  88. ELSE IF U=CAR V THEN CDR V
  89. ELSE CAR V . DELETE(U,CDR V);
  90. SYMBOLIC PROCEDURE DELQ(U,V);
  91. IF NOT PAIRP V THEN V
  92. ELSE IF U EQ CAR V THEN CDR V
  93. ELSE CAR V . DELQ(U,CDR V); % Recopy
  94. SYMBOLIC PROCEDURE DELATQ(U,V);
  95. IF NOT PAIRP V THEN V
  96. ELSE IF (NOT PAIRP CAR V)
  97. OR NOT(U EQ CAAR V) THEN (CAR V . DELATQ(U,CDR V))
  98. ELSE CDR V;
  99. SYMBOLIC PROCEDURE GET(U,V);
  100. IF NOT IDP U THEN NIL
  101. ELSE IF PAIRP (U:=ATSOC(V,PLIST U)) THEN CDR U ELSE NIL;
  102. SYMBOLIC PROCEDURE PUT(U,V,WW);
  103. BEGIN SCALAR L;
  104. IF NOT IDP U THEN RETURN WW;
  105. L:=PLIST U;
  106. IF ATSOC(V,L) THEN L:=DELATQ(V,L);
  107. IF NOTNULL WW THEN L:=(V . WW) . L;
  108. SETPLIST(U,L);
  109. RETURN WW;
  110. END;
  111. SYMBOLIC PROCEDURE REMPROP(U,V);
  112. PUT(U,V,NIL);
  113. SYMBOLIC PROCEDURE LENGTH L;
  114. IF NOT PAIRP L THEN 0
  115. ELSE 1+LENGTH CDR L;
  116. SYMBOLIC PROCEDURE ERRPRT L;
  117. <<PRIN2 '!*!*!*!*! ; PRINT L>>;
  118. SYMBOLIC PROCEDURE MSGPRT L;
  119. <<PRIN2 '!*!*!*! ; PRINT L>>;
  120. SYMBOLIC PROCEDURE FLAGP(NAM,FLG);
  121. IDP NAM AND FLG MEMQ PLIST NAM;
  122. SYMBOLIC PROCEDURE FLAG(NAML,FLG);
  123. IF NOT PAIRP NAML THEN NIL
  124. ELSE <<FLAG1(CAR NAML,FLG); FLAG(CDR NAML,FLG)>>;
  125. SYMBOLIC PROCEDURE FLAG1(NAM,FLG);
  126. IF NOT IDP NAM THEN NIL
  127. ELSE IF FLG MEMQ PLIST NAM THEN NIL
  128. ELSE SETPLIST(NAM, FLG . PLIST(NAM));
  129. SYMBOLIC PROCEDURE REMFLAG(NAML,FLG);
  130. IF NOT PAIRP NAML THEN NIL
  131. ELSE <<REMFLAG1(CAR NAMl,FLG); REMFLAG(CDR NAML,FLG)>>;
  132. SYMBOLIC PROCEDURE REMFLAG1(NAM,FLG);
  133. IF NOT IDP NAM THEN NIL
  134. ELSE IF NOT(FLG MEMQ PLIST NAM)THEN NIL
  135. ELSE SETPLIST(NAM,DELQ(FLG, PLIST(NAM)));
  136. % Interpreter entries for some important OPEN-coded functions;
  137. SYMBOLIC PROCEDURE EQ(U,V);
  138. IF U EQ V THEN T ELSE NIL; % Careful, only bool-test opencoded
  139. SYMBOLIC PROCEDURE EQCAR(U,V);
  140. IF PAIRP U THEN IF(CAR U EQ V) THEN T ELSE NIL;
  141. SYMBOLIC PROCEDURE NULL U;
  142. U EQ NIL;
  143. SYMBOLIC PROCEDURE PLIST U;
  144. PLIST U;
  145. SYMBOLIC PROCEDURE VALUE U;
  146. VALUE U;
  147. SYMBOLIC PROCEDURE FUNCELL U;
  148. FUNCELL U;
  149. SYMBOLIC PROCEDURE SETPLIST(U,V);
  150. SETPLIST(U,V);
  151. SYMBOLIC PROCEDURE SETVALUE(U,V);
  152. SETVALUE(U,V);
  153. SYMBOLIC PROCEDURE SETFUNCELL(U,V);
  154. SETFUNCELL(U,V);
  155. %. Support for ALGebra
  156. SYMBOLIC PROCEDURE ORDERP(X,Y); %. Compare ID orders
  157. !*INF(X) <= !*INF(Y);
  158. SYMBOLIC PROCEDURE TOKEN; %. Renaming
  159. BEGIN TOK!*:=RDTOK();
  160. IF CHARP TOK!* THEN TOK!*:=CHAR2ID TOK!*;
  161. RETURN TOK!*;
  162. END;
  163. % Can get confused if user changes from non-hashed to hashed cons.
  164. SYMBOLIC PROCEDURE EQUAL(X,Y);
  165. IF ATOM(X) THEN IF ATOM(Y) THEN X EQ Y ELSE NIL
  166. ELSE IF ATOM(Y) THEN NIL ELSE EQUAL(CAR X, CAR Y) AND EQUAL(CDR X, CDR Y);
  167. %--------- CATCH/THROW and ERROR handler ---------------
  168. SYMBOLIC PROCEDURE ERROR(X,Y);
  169. <<PRINT LIST('!*!*!*!*! ERROR! ,X,Y);
  170. EMSG!* := Y; ENUM!* := X;
  171. THROW X>>;
  172. SYMBOLIC PROCEDURE ERRORSET(FORM,MSGP,TRACEP);
  173. BEGIN SCALAR VAL;
  174. THROWING!* :=NIL;
  175. VAL:=CATCH FORM;
  176. IF NOT THROWING!* THEN RETURN LIST VAL;
  177. THROWING!*:=NIL;
  178. IF MSGP THEN PRINT LIST('!*!*!*!*,ENUM!*,EMSG!*);
  179. RETURN VAL
  180. END;
  181. % More ARITHMETIC
  182. SYMBOLIC PROCEDURE FIXP X; NUMBERP X;
  183. SYMBOLIC PROCEDURE ABS X;
  184. IF X < 0 THEN (-X) ELSE X;
  185. SYMBOLIC PROCEDURE SUB1 X;
  186. PLUS2(X,MINUS 1);
  187. SYMBOLIC PROCEDURE ZEROP X;
  188. X=0;
  189. SYMBOLIC PROCEDURE ONEP X;
  190. X=1;
  191. SYMBOLIC PROCEDURE IDP X;
  192. IF IDP X THEN T ELSE NIL;
  193. SYMBOLIC PROCEDURE EXPT(A,B);
  194. IF B EQ 0 THEN 1
  195. ELSE IF B <0 THEN 0 % Error ?
  196. ELSE TIMES2(A,A**SUB1 B);
  197. SYMBOLIC PROCEDURE FIX X; X;
  198. SYMBOLIC PROCEDURE FLOAT X; X;
  199. % Should BE MACROS, check problem?
  200. SYMBOLIC MACRO PROCEDURE MAX X; EXPAND(CDR X,'MAX2);
  201. SYMBOLIC MACRO PROCEDURE MIN X; EXPAND(CDR X,'MIN2);
  202. SYMBOLIC MACRO PROCEDURE PLUS X; EXPAND(CDR X,'PLUS2);
  203. SYMBOLIC MACRO PROCEDURE TIMES X; EXPAND(CDR X,'TIMES2);
  204. SYMBOLIC PROCEDURE MAX2(A,B); IF A>B THEN A ELSE B;
  205. SYMBOLIC PROCEDURE MIN2(A,B); IF A<B THEN A ELSE B;
  206. SYMBOLIC FEXPR PROCEDURE FUNCTION X; CAR X;
  207. SYMBOLIC PROCEDURE EXPAND(L,FN);
  208. IF NULL CDR L THEN CAR L ELSE LIST(FN,CAR L,EXPAND(CDR L,FN));
  209. SYMBOLIC PROCEDURE NUMBERP X;
  210. IF NUMBERP X THEN T ELSE NIL;
  211. SYMBOLIC PROCEDURE ATOM X;
  212. IF ATOM X THEN T ELSE NIL;
  213. SYMBOLIC PROCEDURE MINUSP X;
  214. IF NUMBERP X AND X <=(-1) THEN T ELSE NIL;
  215. SYMBOLIC PROCEDURE SET(A,B);
  216. IF (NOT IDP(A)) OR (A EQ 'T) OR (A EQ 'NIL) THEN
  217. ('SET . A . B . NIL) % Error value
  218. ELSE <<SETVALUE(A,B); B>>;
  219. SYMBOLIC PROCEDURE PRINC X;
  220. PRIN2 X;
  221. SYMBOLIC PROCEDURE PRIN1 X;
  222. PRIN2 X;
  223. SYMBOLIC PROCEDURE PRINT X;
  224. <<PRIN1 X; TERPRI(); X>>;
  225. SYMBOLIC PROCEDURE PRIN2T X;
  226. <<PRIN2 X; TERPRI(); X>>;
  227. %. a) Simple Binding for LAMBDA eval
  228. % Later convert to bstack in PAS0, will need GC hooks
  229. FLUID '(BSTK!*); % The Binding stack, list of (id . oval)
  230. % For Special cell model
  231. SYMBOLIC PROCEDURE LBIND1(IDNAME,NVAL); %. For LAMBDA
  232. <<BSTK!*:=(IDNAME . VALUE(IDNAME)) . BSTK!*;
  233. SETVALUE(IDNAME,NVAL)>>;
  234. SYMBOLIC PROCEDURE PBIND1(IDNAME); %. Prog Bind 1 id
  235. <<BSTK!*:=(IDNAME . VALUE(IDNAME)) . BSTK!*;
  236. SETVALUE(IDNAME,'NIL)>>;
  237. SYMBOLIC PROCEDURE UNBIND1; %. Unbind 1 item
  238. IF PAIRP BSTK!* THEN <<SETVALUE(CAAR BSTK!*,CDAR BSTK!*);
  239. BSTK!*:=CDR BSTK!*>>
  240. ELSE ERROR(99,'BNDUNDERFLOW);
  241. SYMBOLIC PROCEDURE UNBINDN N; %. Unbind N items
  242. WHILE N>0 DO <<UNBIND1(); N:=N-1>>;
  243. SYMBOLIC PROCEDURE UNBINDTO(RETVAL,OLDSTK); %. Unbind to CATCH-mark
  244. <<WHILE PAIRP BSTK!* AND NOT(BSTK!* EQ OLDSTK)
  245. DO UNBIND1();
  246. RETVAL>>;
  247. % b) Simple LAMBDA evaluator
  248. SYMBOLIC PROCEDURE EVLAM(LAM,ARGS); %. Will PAD args NILs
  249. BEGIN SCALAR VARS,BOD;
  250. IF NOT (PAIRP LAM AND CAR LAM EQ 'LAMBDA)
  251. THEN RETURN ERROR(99,'Not! defined);
  252. LAM:=CDR LAM;
  253. VARS:=CAR LAM;
  254. LBINDN(VARS,ARGS); % Set up BSTK!*
  255. BOD:=P!.N CDR LAM; % and do PROGN eval
  256. UNBINDN LENGTH VARS; % restore BSTK!*
  257. RETURN BOD
  258. END;
  259. SYMBOLIC PROCEDURE LBINDN(VARS,ARGS); %. Bind each element of VARS to ARGS
  260. IF NOT PAIRP VARS THEN NIL
  261. ELSE IF NOT PAIRP ARGS THEN PBINDN VARS % rest to NIL
  262. ELSE <<LBIND1(CAR VARS,CAR ARGS);
  263. LBINDN(CDR VARS,CDR ARGS)>>;
  264. SYMBOLIC PROCEDURE PBINDN VARS; %. Bind each element of VARS to NIL
  265. IF NOT PAIRP VARS THEN NIL
  266. ELSE <<PBIND1 CAR VARS;
  267. PBINDN CDR VARS>>;
  268. END$