mini-eval-apply.red 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. % MINI-EVAL-APPLY.RED - A small EVAL, uses P-APPLY-LAP
  2. On syslisp;
  3. Procedure InitEval;
  4. Begin
  5. Put('Quote,'TYPE,'FEXPR);
  6. Put('Setq,'TYPE,'FEXPR);
  7. Put('Cond,'TYPE,'FEXPR);
  8. Put('Progn,'TYPE,'FEXPR);
  9. Put('While,'TYPE,'FEXPR);
  10. Put('List,'TYPE,'NEXPR);
  11. Put('De,'TYPE,'FEXPR);
  12. Put('Df,'TYPE,'FEXPR);
  13. Put('Dn,'TYPE,'FEXPR);
  14. Put('Dm,'TYPE,'FEXPR);
  15. End;
  16. syslsp procedure Eval x;
  17. If IDP x then SYMVAL(IdInf x)
  18. else if not PairP x then x
  19. else begin scalar fn,a,FnType;
  20. fn:=car x; a:=cdr x;
  21. if LambdaP fn then Return LambdaEvalApply(GetLambda fn, a);
  22. if CodeP fn then Return CodeEvalApply(fn,a);
  23. if not Idp fn then Return <<Prin2('"**** Non-ID function in EVAL: ");
  24. Print fn;
  25. NIL>>;
  26. if FunBoundP fn then Return <<Prin2('"**** UnBound Function in EVAL: ");
  27. Print fn;
  28. NIL>>;
  29. FnType :=GetFnType Fn;
  30. if FnType = 'FEXPR then return IDApply1(a, Fn);
  31. if FnType = 'NEXPR then return IDApply1(Evlis a, Fn);
  32. if FnType = 'MACRO then return Eval IDApply1(x, Fn);
  33. if FLambdaLinkP fn then return LambdaEvalApply(GetLambda fn,a);
  34. return CodeEvalApply(GetFcodePointer fn, a);
  35. end;
  36. procedure Apply(fn,a);
  37. Begin scalar N;
  38. If LambdaP fn then return LambdaApply(fn,a);
  39. If CodeP fn then CodeApply(fn,a);
  40. If Not Idp Fn then return
  41. <<prin2 '" **** Non-ID function in APPLY: ";
  42. prin1 fn; prin2 " "; Print a;
  43. NIL>>;
  44. if FLambdaLinkP fn then return LambdaApply(GetLambda fn,a);
  45. If FunBoundP Fn then return
  46. <<prin2 '" **** Unbound function in APPLY: ";
  47. prin1 fn; prin2 " "; Print a;
  48. NIL>>;
  49. Return CodeApply(GetFcodePointer Fn,a);
  50. End;
  51. % -- User Function Hooks ---
  52. Procedure LambdaApply(x,a);
  53. Begin scalar v,b;
  54. x:=cdr x;
  55. v:=car x;
  56. b:=cdr x;
  57. Return DoLambda(v,b,a)
  58. End;
  59. Procedure LambdaEvalApply(x,y);
  60. LambdaApply(x,Evlis y);
  61. Procedure DoLambda(vars,body,args);
  62. % Args already EVAL'd as appropriate
  63. Begin scalar N,x,a;
  64. N:=Length vars;
  65. For each v in VARS do
  66. <<if pairp args then <<a:=car args; args:=cdr args>>
  67. else a:=Nil;
  68. LBIND1(v,a)>>;
  69. %/ Should try BindEVAL here
  70. x:=EvProgn Body;
  71. UnBindN N;
  72. Return x;
  73. End;
  74. Procedure LambdaP(x);
  75. EqCar(x,'LAMBDA);
  76. Procedure GetLambda(fn);
  77. Get(fn,'!*LambdaLink);
  78. off syslisp;
  79. End;