statement.g 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* S T A T E M E N T S */
  8. /* $Id$ */
  9. {
  10. #include <assert.h>
  11. #include <em_arith.h>
  12. #include <em_label.h>
  13. #include "idf.h"
  14. #include "LLlex.h"
  15. #include "scope.h"
  16. #include "def.h"
  17. #include "type.h"
  18. #include "node.h"
  19. static int loopcount = 0; /* Count nested loops */
  20. extern t_node *EmptyStatement;
  21. }
  22. statement(register t_node **pnd;)
  23. {
  24. register t_node *nd;
  25. extern int return_occurred;
  26. } :
  27. /*
  28. * This part is not in the reference grammar. The reference grammar
  29. * states : assignment | ProcedureCall | ...
  30. * but this gives LL(1) conflicts
  31. */
  32. designator(pnd)
  33. [ { nd = dot2node(Stat, *pnd, NULLNODE);
  34. nd->nd_symb = '(';
  35. nd->nd_lineno = (*pnd)->nd_lineno;
  36. }
  37. ActualParameters(&(nd->nd_RIGHT))?
  38. |
  39. [ BECOMES
  40. | %erroneous '='
  41. { error("':=' expected instead of '='");
  42. DOT = BECOMES;
  43. }
  44. ]
  45. { nd = dot2node(Stat, *pnd, NULLNODE); }
  46. expression(&(nd->nd_RIGHT))
  47. ]
  48. { *pnd = nd; }
  49. /*
  50. * end of changed part
  51. */
  52. |
  53. IfStatement(pnd)
  54. |
  55. CaseStatement(pnd)
  56. |
  57. WHILE { *pnd = nd = dot2leaf(Stat); }
  58. expression(&(nd->nd_LEFT))
  59. DO
  60. StatementSequence(&(nd->nd_RIGHT))
  61. END
  62. |
  63. REPEAT { *pnd = nd = dot2leaf(Stat); }
  64. StatementSequence(&(nd->nd_LEFT))
  65. UNTIL
  66. expression(&(nd->nd_RIGHT))
  67. |
  68. { loopcount++; }
  69. LOOP { *pnd = nd = dot2leaf(Stat); }
  70. StatementSequence(&((*pnd)->nd_RIGHT))
  71. END
  72. { loopcount--; }
  73. |
  74. ForStatement(pnd)
  75. |
  76. WITH { *pnd = nd = dot2leaf(Stat); }
  77. designator(&(nd->nd_LEFT))
  78. DO
  79. StatementSequence(&(nd->nd_RIGHT))
  80. END
  81. |
  82. EXIT
  83. { if (!loopcount) error("EXIT not in a LOOP");
  84. *pnd = dot2leaf(Stat);
  85. }
  86. |
  87. ReturnStatement(pnd)
  88. { return_occurred = 1; }
  89. |
  90. /* empty */ { *pnd = EmptyStatement; }
  91. ;
  92. /*
  93. * The next two rules in-line in "Statement", because of an LL(1) conflict
  94. assignment:
  95. designator BECOMES expression
  96. ;
  97. ProcedureCall:
  98. designator ActualParameters?
  99. ;
  100. */
  101. StatementSequence(register t_node **pnd;)
  102. {
  103. t_node *nd;
  104. register t_node *nd1;
  105. } :
  106. statement(pnd)
  107. [ %persistent
  108. ';'
  109. statement(&nd)
  110. { if (nd != EmptyStatement) {
  111. nd1 = dot2node(Link, *pnd, nd);
  112. *pnd = nd1;
  113. nd1->nd_symb = ';';
  114. pnd = &(nd1->nd_RIGHT);
  115. }
  116. }
  117. ]*
  118. ;
  119. IfStatement(t_node **pnd;)
  120. {
  121. register t_node *nd;
  122. } :
  123. IF { nd = dot2leaf(Stat);
  124. *pnd = nd;
  125. }
  126. expression(&(nd->nd_LEFT))
  127. THEN { nd->nd_RIGHT = dot2leaf(Link);
  128. nd = nd->nd_RIGHT;
  129. }
  130. StatementSequence(&(nd->nd_LEFT))
  131. [
  132. ELSIF { nd->nd_RIGHT = dot2leaf(Stat);
  133. nd = nd->nd_RIGHT;
  134. nd->nd_symb = IF;
  135. }
  136. expression(&(nd->nd_LEFT))
  137. THEN { nd->nd_RIGHT = dot2leaf(Link);
  138. nd = nd->nd_RIGHT;
  139. }
  140. StatementSequence(&(nd->nd_LEFT))
  141. ]*
  142. [
  143. ELSE
  144. StatementSequence(&(nd->nd_RIGHT))
  145. |
  146. ]
  147. END
  148. ;
  149. CaseStatement(t_node **pnd;)
  150. {
  151. register t_node *nd;
  152. t_type *tp = 0;
  153. } :
  154. CASE { *pnd = nd = dot2leaf(Stat); }
  155. expression(&(nd->nd_LEFT))
  156. OF
  157. case(&(nd->nd_RIGHT), &tp)
  158. { nd = nd->nd_RIGHT; }
  159. [
  160. '|'
  161. case(&(nd->nd_RIGHT), &tp)
  162. { nd = nd->nd_RIGHT; }
  163. ]*
  164. [ ELSE StatementSequence(&(nd->nd_RIGHT))
  165. |
  166. ]
  167. END
  168. ;
  169. case(t_node **pnd; t_type **ptp;) :
  170. [ CaseLabelList(ptp, pnd)
  171. ':' { *pnd = dot2node(Link, *pnd, NULLNODE); }
  172. StatementSequence(&((*pnd)->nd_RIGHT))
  173. |
  174. ]
  175. { *pnd = dot2node(Link, *pnd, NULLNODE);
  176. (*pnd)->nd_symb = '|';
  177. }
  178. ;
  179. /* inline in statement; lack of space
  180. WhileStatement(t_node **pnd;)
  181. {
  182. register t_node *nd;
  183. }:
  184. WHILE { *pnd = nd = dot2leaf(Stat); }
  185. expression(&(nd->nd_LEFT))
  186. DO
  187. StatementSequence(&(nd->nd_RIGHT))
  188. END
  189. ;
  190. RepeatStatement(t_node **pnd;)
  191. {
  192. register t_node *nd;
  193. }:
  194. REPEAT { *pnd = nd = dot2leaf(Stat); }
  195. StatementSequence(&(nd->nd_LEFT))
  196. UNTIL
  197. expression(&(nd->nd_RIGHT))
  198. ;
  199. */
  200. ForStatement(t_node **pnd;)
  201. {
  202. register t_node *nd, *nd1;
  203. }:
  204. FOR { *pnd = nd = dot2leaf(Stat); }
  205. IDENT { nd1 = dot2leaf(Name); }
  206. BECOMES { nd->nd_LEFT = dot2node(Stat, nd1, dot2leaf(Link));
  207. nd1 = nd->nd_LEFT->nd_RIGHT;
  208. nd1->nd_symb = TO;
  209. }
  210. expression(&(nd1->nd_LEFT))
  211. TO
  212. expression(&(nd1->nd_RIGHT))
  213. { nd->nd_RIGHT = nd1 = dot2leaf(Link);
  214. nd1->nd_symb = BY;
  215. }
  216. [
  217. BY
  218. ConstExpression(&(nd1->nd_LEFT))
  219. { if (!(nd1->nd_LEFT->nd_type->tp_fund & T_INTORCARD)) {
  220. error("illegal type in BY clause");
  221. }
  222. }
  223. |
  224. { nd1->nd_LEFT = dot2leaf(Value);
  225. nd1->nd_LEFT->nd_INT = 1;
  226. }
  227. ]
  228. DO
  229. StatementSequence(&(nd1->nd_RIGHT))
  230. END
  231. ;
  232. /* inline in Statement; lack of space
  233. LoopStatement(t_node **pnd;):
  234. LOOP { *pnd = dot2leaf(Stat); }
  235. StatementSequence(&((*pnd)->nd_RIGHT))
  236. END
  237. ;
  238. WithStatement(t_node **pnd;)
  239. {
  240. register t_node *nd;
  241. }:
  242. WITH { *pnd = nd = dot2leaf(Stat); }
  243. designator(&(nd->nd_LEFT))
  244. DO
  245. StatementSequence(&(nd->nd_RIGHT))
  246. END
  247. ;
  248. */
  249. ReturnStatement(t_node **pnd;)
  250. {
  251. register t_def *df = CurrentScope->sc_definedby;
  252. register t_type *tp = df->df_type ? ResultType(df->df_type) : 0;
  253. register t_node *nd;
  254. } :
  255. RETURN { *pnd = nd = dot2leaf(Stat); }
  256. [
  257. expression(&(nd->nd_RIGHT))
  258. { if (scopeclosed(CurrentScope)) {
  259. error("a module body cannot return a value");
  260. }
  261. else if (! tp) {
  262. error("procedure \"%s\" is not a function, so cannot return a value", df->df_idf->id_text);
  263. }
  264. }
  265. |
  266. { if (tp) {
  267. error("function procedure \"%s\" must return a value", df->df_idf->id_text);
  268. }
  269. }
  270. ]
  271. ;