rvector.red 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. module rvector; % Definition of RLISP vectors and operations on them.
  2. % Author: Anthony C. Hearn.
  3. % Copyright (c) 1990 The RAND Corporation. All rights reserved.
  4. fluid '(!*fastvector);
  5. global '(cursym!*);
  6. switch fastvector;
  7. % Add to system table.
  8. flag('(vec!*),'vecfn);
  9. % Parsing interface.
  10. symbolic procedure xreadvec;
  11. % Expects a list of expressions enclosed by [, ].
  12. begin scalar cursym,delim,lst;
  13. if scan() eq '!*rsqb!* then <<scan(); return list 'list>>;
  14. a: lst := aconc(lst,xread1 'group);
  15. cursym := cursym!*;
  16. scan();
  17. if cursym eq '!*rsqb!*
  18. then return if delim eq '!*semicol!* then 'progn . lst
  19. else list('vec!*,'list . lst)
  20. else if null delim then delim := cursym
  21. else if not(delim eq cursym)
  22. then symerr("Syntax error: mixed , and ; in vector",nil);
  23. go to a
  24. end;
  25. put('!*lsqb!*,'stat,'xreadvec);
  26. newtok '((![) !*lsqb!*);
  27. newtok '((!]) !*rsqb!*);
  28. flag('(!*rsqb!*),'delim);
  29. flag('(!*rsqb!*),'nodel);
  30. symbolic procedure vec!* u;
  31. % Make a vector out of elements of u.
  32. begin scalar n,x;
  33. n := length u - 1;
  34. x := mkvect n;
  35. for i:= 0:n do <<putv(x,i,car u); u := cdr u>>;
  36. return x
  37. end;
  38. % Evaluation interface.
  39. % symbolic procedure setv(u,v);
  40. % <<set(u,v); put(u,'rtype,'vector); v>>;
  41. % Length interface.
  42. % Printing interface.
  43. % Definitions of operations on vectors.
  44. symbolic procedure getvect(u,vars,mode);
  45. expandgetv(symbid(car u,vars),formlis(evalvecarg cdr u,vars,mode));
  46. symbolic procedure expandgetv(u,v);
  47. if null v then u
  48. else expandgetv(list(if !*fastvector then 'igetv else 'getv,
  49. u,car v),
  50. cdr v);
  51. symbolic procedure putvect(u,vars,mode);
  52. expandputv(symbid(caar u,vars),formlis(evalvecarg cdar u,vars,mode),
  53. form1(cadr u,vars,mode));
  54. symbolic procedure expandputv(u,v,w);
  55. if null cdr v
  56. then list(if !*fastvector then 'iputv else 'putv,u,car v,w)
  57. else expandputv(list(if !*fastvector then 'igetv else 'getv,
  58. u,car v),
  59. cdr v,w);
  60. symbolic procedure evalvecarg u;
  61. % if u and null cdr u and vectorp car u
  62. % then for i:=0:upbv car u collect getv(car u,i) else
  63. if u and null cdr u and eqcar(car u,'vec!*)
  64. and eqcar(cadar u,'list)
  65. then cdadar u
  66. else u;
  67. % Support for arrays defined in terms of vectors.
  68. symbolic procedure mkar1 u;
  69. begin scalar x;
  70. x := mkvect car u;
  71. if cdr u then for i:= 0:upbv x do putv(x,i,mkar1 cdr u);
  72. return x
  73. end;
  74. symbolic macro procedure array u;
  75. % Create an array from the elements in u.
  76. list('vec!*,'list . cdr u);
  77. endmodule;
  78. end;