gparser.red 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. module gparser; %% GENTRAN Parser Module %%
  2. %% Author: Barbara L. Gates %%
  3. %% December 1986 %%
  4. % Entry Point: GentranParse
  5. symbolic$
  6. % GENTRAN Global Variable %
  7. global '(!*reservedops!*)$
  8. !*reservedops!* := '(and rblock cond difference equal expt for geq go
  9. greaterp leq lessp mat minus neq not or plus
  10. procedure progn quotient read recip repeat return
  11. setq times while write)$ %reserved operators
  12. symbolic procedure gentranparse forms;
  13. begin scalar found_error;
  14. for each f in forms do
  15. if not(gpstmtp f or gpexpp f or gpdefnp f) then
  16. <<
  17. gentranerr('e, f, "CANNOT BE TRANSLATED", nil);
  18. % If we are processing a template (for example) then this will
  19. % not result in a hard error, so make Gentran aware that
  20. % something went wrong:
  21. found_error := 't;
  22. >>;
  23. return not found_error;
  24. end$
  25. procedure gpexpp exp;
  26. % exp ::= id | number | (PLUS exp exp') | (MINUS exp) | %
  27. % (DIFFERENCE exp exp) | (TIMES exp exp exp') | %
  28. % (RECIP exp) |(QUOTIENT exp exp) | (EXPT exp exp) | (id arg') %
  29. if atom exp then
  30. idp exp or numberp exp
  31. else if car exp memq '(!:rd!: !:cr!: !:crn!: !:gi!:) then
  32. t
  33. else
  34. if car exp eq 'plus then
  35. length exp >= 2 and gpexpp cadr exp and gpexp1p cddr exp
  36. else if car exp memq '(minus recip) then
  37. length exp=2 and gpexpp cadr exp
  38. else if car exp memq '(difference quotient expt) then
  39. length exp=3 and gpexpp cadr exp and gpexpp caddr exp
  40. else if car exp eq 'times then
  41. length exp >= 3 and gpexpp cadr exp and gpexpp caddr exp and
  42. gpexp1p cdddr exp
  43. else if car exp eq '!:rd!: then t
  44. else if car exp memq '(!:cr!: !:crn!: !:gi!:) then t
  45. else if unresidp car exp then
  46. gparg1p cdr exp$
  47. procedure gpexp1p exp;
  48. % exp' ::= exp exp' | eps %
  49. null exp or (gpexpp car exp and gpexp1p cdr exp)$
  50. procedure gplogexpp exp;
  51. % logexp ::= id | (EQUAL exp exp) | (NEQ exp exp) | %
  52. % (GREATERP exp exp) |(GEQ exp exp) | (LESSP exp exp) | %
  53. % (LEQ exp exp) | (NOT logexp) | (AND logexp logexp logexp')%
  54. % | (OR logexp logexp logexp') | (id arg') %
  55. if atom exp then
  56. idp exp
  57. else
  58. if car exp memq '(equal neq greaterp geq lessp leq) then
  59. length exp=3 and gpexpp cadr exp and gpexpp caddr exp
  60. else if car exp eq 'not then
  61. length exp=2 and gplogexpp cadr exp
  62. else if car exp memq '(and or) then
  63. length exp >= 3 and gplogexpp cadr exp and gplogexpp caddr exp
  64. and gplogexp1p cdddr exp
  65. else if unresidp car exp then
  66. gparg1p cdr exp$
  67. procedure gplogexp1p exp;
  68. % logexp' ::= logexp logexp' | eps %
  69. null exp or (gplogexpp car exp and gplogexp1p cdr exp)$
  70. procedure gpargp exp;
  71. % arg ::= string | exp | logexp %
  72. stringp exp or gpexpp exp or gplogexpp exp$
  73. procedure gparg1p exp;
  74. % arg' ::= arg arg' | eps %
  75. null exp or (gpargp car exp and gparg1p cdr exp)$
  76. procedure gpvarp exp;
  77. % var ::= id | (id exp exp') %
  78. if atom exp then
  79. idp exp
  80. else
  81. if unresidp car exp then
  82. length exp >= 2 and gpexpp cadr exp and gpexp1p cddr exp$
  83. procedure gplistp exp;
  84. % list ::= (exp exp') %
  85. if pairp exp then
  86. length exp >= 1 and gpexpp car exp and gpexp1p cdr exp$
  87. procedure gplist1p exp;
  88. % list' ::= list list' | eps %
  89. null exp or (gplistp car exp and gplist1p cdr exp)$
  90. procedure gpid1p exp;
  91. % id' ::= id id' | eps %
  92. null exp or (idp car exp and gpid1p cdr exp)$
  93. procedure gpstmtp exp;
  94. % stmt ::= id | (SETQ setq') | (COND cond') | (WHILE logexp stmt) | %
  95. % (REPEAT stmt logexp) | (FOR var (exp exp exp) DO stmt) | %
  96. % (GO id) | (RETURN arg) | (WRITE arg arg') | %
  97. % (PROGN stmt stmt') | (BLOCK (id') stmt') | (id arg') %
  98. if atom exp then
  99. idp exp
  100. else if car exp memq '(!:rd!: !:cr!: !:crn!: !:gi!:) then
  101. nil
  102. else
  103. if car exp eq 'setq then
  104. gpsetq1p cdr exp
  105. else if car exp eq 'cond then
  106. gpcond1p cdr exp
  107. else if car exp eq 'while then
  108. length exp=3 and gplogexpp cadr exp and gpstmtp caddr exp
  109. else if car exp eq 'repeat then
  110. length exp=3 and gpstmtp cadr exp and gplogexpp caddr exp
  111. else if car exp eq 'for then
  112. length exp=5 and gpvarp cadr exp and pairp caddr exp and
  113. (length caddr exp=3 and gpexpp car caddr exp and
  114. gpexpp cadr caddr exp and gpexpp caddr caddr exp) and
  115. cadddr exp eq 'do and gpstmtp car cddddr exp
  116. else if car exp eq 'go then
  117. length exp=2 and idp cadr exp
  118. else if car exp eq 'return then
  119. length exp=2 and gpargp cadr exp
  120. else if car exp eq 'write then
  121. length exp >= 2 and gpargp cadr exp and gparg1p cddr exp
  122. else if car exp eq 'progn then
  123. length exp >= 2 and gpstmtp cadr exp and gpstmt1p cddr exp
  124. else if car exp eq 'rblock then
  125. length exp >= 2 and gpid1p cadr exp and gpstmt1p cddr exp
  126. else if unresidp car exp then
  127. gparg1p cdr exp$
  128. procedure gpsetq1p exp;
  129. % setq' ::= id setq'' | (id exp exp') setq''' %
  130. if exp and length exp=2 then
  131. if atom car exp then
  132. idp car exp and gpsetq2p cdr exp
  133. else
  134. (length car exp >= 2 and idp car car exp
  135. and unresidp car car exp and gpexpp cadr car exp
  136. and gpexp1p cddr car exp) and gpsetq3p cdr exp$
  137. procedure gpsetq2p exp;
  138. % setq'' ::= (MAT list list') | setq''' %
  139. if exp then
  140. if eqcar(car exp, 'mat) then
  141. onep length exp and (gplistp cadar exp and gplist1p cddar exp)
  142. else
  143. gpsetq3p exp$
  144. procedure gpsetq3p exp;
  145. % setq''' ::= (FOR var (exp exp exp) forop exp) | (READ) | exp | logexp
  146. if exp and onep length exp then
  147. gpexpp car exp or
  148. gplogexpp car exp or
  149. (if caar exp eq 'for then
  150. length car exp=5 and gpvarp cadar exp and
  151. (pairp caddar exp and length caddar exp=3 and
  152. gpexpp car caddar exp and gpexpp cadr caddar exp and
  153. gpexpp caddr caddar exp) and gpforopp car cdddar exp and
  154. gpexpp cadr cdddar exp
  155. else if caar exp eq 'read then
  156. onep length car exp)$
  157. procedure gpforopp exp;
  158. % forop ::= SUM | PRODUCT %
  159. exp memq '(sum product)$
  160. procedure gpcond1p exp;
  161. % cond' ::= (logexp stmt) cond' | eps %
  162. null exp or
  163. (pairp car exp and length car exp=2 and gplogexpp caar exp and
  164. gpstmtp cadar exp and gpcond1p cdr exp)$
  165. procedure gpstmt1p exp;
  166. % stmt' ::= stmt stmt' | eps %
  167. null exp or (gpstmtp car exp and gpstmt1p cdr exp)$
  168. procedure gpdefnp exp;
  169. % defn ::= (PROCEDURE id NIL EXPR (id') stmt) %
  170. eqcar(exp, 'procedure) and length exp=6 and
  171. idp cadr exp and null caddr exp and atom cadddr exp and
  172. gpid1p car cddddr exp and gpstmtp cadr cddddr exp
  173. and not idp cadr cddddr exp$
  174. %% %%
  175. %% Predicates %%
  176. %% %%
  177. procedure unresidp id;
  178. not (id memq !*reservedops!*)$
  179. endmodule;
  180. end;