type-conversions.red 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. %
  2. % TYPE-CONVERSIONS.RED - Functions for converting between various data types
  3. %
  4. % Author: Eric Benson
  5. % Symbolic Computation Group
  6. % Computer Science Dept.
  7. % University of Utah
  8. % Date: 28 August 1981
  9. % Copyright (c) 1981 University of Utah
  10. % <PSL.VAX-INTERP>TYPE-CONVERSIONS.RED.2, 20-Jan-82 02:10:24, Edit by GRISS
  11. % Fix list2vector for NIL case
  12. % The functions in this file are named `argument-type'2`result-type'.
  13. % The number 2 is used rather than `To' only for compatibility with old
  14. % versions. Any other suggestions for a consistent naming scheme are welcomed.
  15. % Perhaps they should also be `result-type'From`argument-type'.
  16. % Float and Fix are in ARITH.RED
  17. CompileTime flag('(Sys2FIXN), 'InternalFunction);
  18. on SysLisp;
  19. syslsp procedure ID2Int U; %. Return ID index as Lisp number
  20. if IDP U then MkINT IDInf U
  21. else NonIDError(U, 'ID2Int);
  22. syslsp procedure Int2ID U; %. Return ID corresponding to index
  23. begin scalar StripU;
  24. return if IntP U then
  25. << StripU := IntInf U;
  26. if StripU >= 0 then MkID StripU
  27. else TypeError(U, 'Int2ID, '"positive integer") >>
  28. else NonIntegerError(U, 'Int2ID);
  29. end;
  30. syslsp procedure Int2Sys N; %. Convert Lisp integer to untagged
  31. if IntP N then IntInf N
  32. else if FixNP N then FixVal FixInf N
  33. else NonIntegerError(N, 'Int2Sys);
  34. syslsp procedure Lisp2Char U; %. Convert Lisp item to syslsp char
  35. begin scalar C; % integers, IDs and strings are legal
  36. return if IntP U and (C := IntInf U) >= 0 and C <= 127 then C
  37. else if IDP U then % take first char of ID print name
  38. StrByt(StrInf SymNam IDInf U, 0)
  39. else if StringP U then
  40. StrByt(StrInf U, 0) % take first character of Lisp string
  41. else NonCharacterError(U, 'Lisp2Char);
  42. end;
  43. syslsp procedure Int2Code N; %. Convert Lisp integer to code pointer
  44. MkCODE N;
  45. syslsp procedure Sys2Int N; %. Convert word to Lisp number
  46. if SignedField(N, InfStartingBit - 1, InfBitLength + 1) eq N then N
  47. else Sys2FIXN N;
  48. syslsp procedure Sys2FIXN N;
  49. begin scalar FX;
  50. FX := GtFIXN();
  51. FixVal FX := N;
  52. return MkFIXN FX;
  53. end;
  54. syslsp procedure ID2String U; %. Return print name of U (not copy)
  55. if IDP U then SymNam IDInf U
  56. else NonIDError(U, 'ID2String);
  57. % The functions for converting strings to IDs are Intern and NewID. Intern
  58. % returns an interned ID, NewID returns an uninterned ID. They are both found
  59. % in OBLIST.RED
  60. syslsp procedure String2Vector U; %. Make vector of ASCII values in U
  61. if StringP U then begin scalar StripU, V, N;
  62. N := StrLen StrInf U;
  63. V := GtVECT N;
  64. StripU := StrInf U; % in case GC occurred
  65. for I := 0 step 1 until N do
  66. VecItm(V, I) := MkINT StrByt(StripU, I);
  67. return MkVEC V;
  68. end else NonStringError(U, 'String2Vector);
  69. syslsp procedure Vector2String V; %. Make string with ASCII values in V
  70. if VectorP V then begin scalar StripV, S, N, Ch;
  71. N := VecLen VecInf V;
  72. S := GtSTR N;
  73. StripV := VecInf V; % in case GC occurred
  74. for I := 0 step 1 until N do
  75. StrByt(S, I) := Lisp2Char VecItm(StripV, I);
  76. return MkSTR S;
  77. end else NonVectorError(V, 'Vector2String);
  78. syslsp procedure List2String P; %. Make string with ASCII values in P
  79. if null P then '""
  80. else if PairP P then begin scalar S, N;
  81. N := IntInf Length P - 1;
  82. S := GtSTR N;
  83. for I := 0 step 1 until N do
  84. << StrByt(S, I) := Lisp2Char car P;
  85. P := cdr P >>;
  86. return MkSTR S;
  87. end else NonPairError(P, 'List2String);
  88. syslsp procedure String2List S; %. Make list with ASCII values in S
  89. if StringP S then begin scalar L, N;
  90. L := NIL;
  91. N := StrLen StrInf S;
  92. for I := N step -1 until 0 do
  93. L := MkINT StrByt(StrInf S, I) . L; % strip S each time in case GC
  94. return L;
  95. end else NonStringError(S, 'String2List);
  96. syslsp procedure List2Vector L; %. convert list to vector
  97. if PairP L or NULL L then begin scalar V, N;% this function is used by READ
  98. N := IntInf Length L - 1;
  99. V := GtVECT N;
  100. for I := 0 step 1 until N do
  101. << VecItm(V, I) := car L;
  102. L := cdr L >>;
  103. return MkVEC V;
  104. end else NonPairError(L, 'List2Vector);
  105. syslsp procedure Vector2List V; %. Convert vector to list
  106. if VectorP V then begin scalar L, N;
  107. L := NIL;
  108. N := VecLen VecInf V;
  109. for I := N step -1 until 0 do
  110. L := VecItm(VecInf V, I) . L; % strip V each time in case GC
  111. return L;
  112. end else NonVectorError(V, 'Vector2List);
  113. off SysLisp;
  114. END;