define.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Store a function definition
  2. //
  3. // Example:
  4. //
  5. // f(x,y)=x^y
  6. //
  7. // For this definition, p1 points to the following structure.
  8. //
  9. // p1
  10. // |
  11. // ___v__ ______ ______
  12. // |CONS |->|CONS |--------------------->|CONS |
  13. // |______| |______| |______|
  14. // | | |
  15. // ___v__ ___v__ ______ ______ ___v__ ______ ______
  16. // |SETQ | |CONS |->|CONS |->|CONS | |CONS |->|CONS |->|CONS |
  17. // |______| |______| |______| |______| |______| |______| |______|
  18. // | | | | | |
  19. // ___v__ ___v__ ___v__ ___v__ ___v__ ___v__
  20. // |SYM f | |SYM x | |SYM y | |POWER | |SYM x | |SYM y |
  21. // |______| |______| |______| |______| |______| |______|
  22. //
  23. // We have
  24. //
  25. // caadr(p1) points to f
  26. // cdadr(p1) points to the list (x y)
  27. // caddr(p1) points to (power x y)
  28. #include "stdafx.h"
  29. #include "defs.h"
  30. #define F p3 // F points to the function name
  31. #define A p4 // A points to the argument list
  32. #define B p5 // B points to the function body
  33. void
  34. define_user_function(void)
  35. {
  36. F = caadr(p1);
  37. A = cdadr(p1);
  38. B = caddr(p1);
  39. if (!issymbol(F))
  40. stop("function name?");
  41. // evaluate function body (maybe)
  42. if (car(B) == symbol(EVAL)) {
  43. push(cadr(B));
  44. eval();
  45. B = pop();
  46. }
  47. set_binding_and_arglist(F, B, A);
  48. // return value is nil
  49. push_symbol(NIL);
  50. }