pre.red 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. module pre; %% GENTRAN Preprocessing Module %%
  2. %% Author: Barbara L. Gates %%
  3. %% December 1986 %%
  4. % Entry Point: Preproc
  5. symbolic$
  6. procedure preproc exp;
  7. begin
  8. scalar r;
  9. r := preproc1 exp;
  10. if r then
  11. return car r
  12. else
  13. return r
  14. end$
  15. % This switch causes gentran to attempt to automatically generate type
  16. % declarations, without use of the 'declare' statement. mcd 12/11/87.
  17. fluid '(!*getdecs)$
  18. !*getdecs := nil$
  19. switch getdecs$
  20. % This global variable is the default type given when 'getdecs' is on:
  21. global '(deftype!*)$
  22. share deftype!*$
  23. deftype!* := 'real$
  24. % Bfloat defined in arith.red.
  25. % symbolic procedure bfloat x; if floatp x then fl2bf x else
  26. % normbf(if atom x then read!:num x else x);
  27. symbolic procedure preproc1 exp;
  28. % Amended mcd 12/11/87,13/11/87,14/10/91.
  29. if atom exp then
  30. list exp
  31. else if car exp = '!:rd!: then
  32. list if smallfloatp cdr exp then bfloat cdr exp else exp
  33. else if car exp = '!:dn!: then
  34. preproc1 decimal2internal(cadr exp,cddr exp)
  35. else if car exp eq '!*sq then
  36. % (!*SQ dpexp) --> (PREPSQ dpexp) %
  37. preproc1 prepsq cadr exp
  38. else if car exp eq 'procedure then
  39. <<
  40. % Store subprogram name & parameters in symbol table %
  41. symtabput(cadr exp, '!*params!*, car cddddr exp);
  42. % Store subprogram type and parameters types in symbol table
  43. % if !*getdecs switch is on. Use default type unless
  44. % procedure is declared as either:
  45. % INTEGER PROCEDURE ... or REAL PROCEDURE ...
  46. if !*getdecs then
  47. if caddr exp memq '(real integer) then
  48. <<
  49. symtabput(cadr exp,cadr exp,list caddr exp);
  50. for each v in car cddddr exp do
  51. symtabput(cadr exp,v,list caddr exp);
  52. list nconc(list ('procedure,cadr exp,'nil),
  53. for each e in cdddr exp conc preproc1 e)
  54. >>
  55. else
  56. <<
  57. for each v in car cddddr exp do
  58. symtabput(cadr exp,v,list deftype!*);
  59. list for each e in exp
  60. conc preproc1 e
  61. >>
  62. else
  63. list for each e in exp
  64. conc preproc1 e
  65. >>
  66. else if car exp eq 'declare then
  67. <<
  68. % Store type declarations in symbol table %
  69. exp := car preproc1 cdr exp;
  70. exp := preprocdec exp;
  71. for each dec in exp do
  72. for each var in cdr dec do
  73. if car dec memq '(subroutine function) then
  74. symtabput(var, '!*type!*, car dec)
  75. else
  76. symtabput(nil,
  77. if atom var then var else car var,
  78. if atom var then list car dec
  79. else (car dec . cdr var));
  80. nil
  81. >>
  82. else if car exp eq 'setq and pairp caddr exp and
  83. memq(caaddr exp,'(cond progn) ) then
  84. migrate!-setqs exp
  85. else if memq(car exp, '(plus times difference quotient minus) ) then
  86. begin scalar simp_exp;
  87. return if pairp numr (simp_exp:=simp!* exp)
  88. and memq(car numr simp_exp,'(!:cr!: !:crn!: !:gi!:)) then
  89. if onep denr simp_exp then
  90. list numr simp_exp
  91. else
  92. list list('quotient,numr simp_exp,
  93. car preproc1 prepsq !*f2q denr simp_exp)
  94. else
  95. list for each e in exp conc preproc1 e;
  96. end
  97. else
  98. <<
  99. % The next statement stores the index of a for loop in the symbol
  100. % table, assigning them the type integer,
  101. % if the switch 'getdecs' is on.
  102. if !*getdecs and (car exp memq '(!~FOR for)) then
  103. symtabput(nil,cadr exp, '(integer));
  104. list for each e in exp
  105. conc preproc1 e
  106. >>$
  107. symbolic procedure preprocdec arg;
  108. % (TIMES type int) --> type!*int %
  109. % (IMPLICIT type) --> IMPLICIT! type %
  110. % (DIFFERENCE v1 v2) --> v1!-v2 %
  111. if atom arg then
  112. arg
  113. else if car arg eq 'times then
  114. if equal(length arg,3) and fixp(caddr arg) then
  115. intern
  116. compress
  117. append( append( explode cadr arg, explode '!* ),
  118. explode caddr arg )
  119. else
  120. begin scalar result;
  121. for i:=1:length(arg) do
  122. result := append(result,
  123. if equal(nth(arg,i),'times)
  124. then '(!*)
  125. else explode nth(arg,i));
  126. return intern compress result;
  127. end
  128. else if car arg eq 'implicit then
  129. intern
  130. compress
  131. append( explode 'implicit! , explode preprocdec cadr arg )
  132. else if car arg eq 'difference then
  133. intern
  134. compress
  135. append( append( explode cadr arg, explode '!- ),
  136. explode caddr arg )
  137. else
  138. for each a in arg collect
  139. preprocdec a$
  140. symbolic procedure migrate!-setqs exp;
  141. % Move setq's within a progn or cond so that we can translate things
  142. % like gentran x := if ... then ...
  143. list migrate!-setqs1(cadr exp,caddr exp)$
  144. symbolic procedure migrate!-setqs1(var,exp);
  145. if atom exp then
  146. preproc list('setq,var,exp)
  147. else if eqcar(exp,'cond) then
  148. ('cond . for each u in cdr exp collect
  149. list (preproc car u,migrate!-setqs1(var,cadr u)) )
  150. else if eqcar(exp,'progn) then
  151. reverse rplaca(exp := reverse exp,migrate!-setqs1(var,car exp))
  152. else
  153. preproc list('setq,var,exp)$
  154. endmodule;
  155. end;